René's URL Explorer Experiment


Title: Argument clinic: improve generated code for self converter type checks · Issue #101409 · python/cpython · GitHub

Open Graph Title: Argument clinic: improve generated code for self converter type checks · Issue #101409 · python/cpython

X Title: Argument clinic: improve generated code for self converter type checks · Issue #101409 · python/cpython

Description: Feature or enhancement Currently, typically for __new__ and __init__ methods, Argument Clinic will spell out the self type check as such: if kind == METHOD_NEW: type_check = ('({0} == {1} ||\n ' ' {0}->tp_init == {2}tp_init)' ).format(se...

Open Graph Description: Feature or enhancement Currently, typically for __new__ and __init__ methods, Argument Clinic will spell out the self type check as such: if kind == METHOD_NEW: type_check = ('({0} == {1} ||\n ' ' ...

X Description: Feature or enhancement Currently, typically for __new__ and __init__ methods, Argument Clinic will spell out the self type check as such: if kind == METHOD_NEW: type_check = ('({0} == {1} ||\n ...

Opengraph URL: https://github.com/python/cpython/issues/101409

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argument clinic: improve generated code for self converter type checks","articleBody":"# Feature or enhancement\r\n\r\nCurrently, typically for `__new__` and `__init__` methods, Argument Clinic will spell out the `self` type check as such:\r\n\r\n```python\r\nif kind == METHOD_NEW:\r\n    type_check = ('({0} == {1} ||\\n        '\r\n                  ' {0}-\u003etp_init == {2}tp_init)'\r\n                 ).format(self.name, type_object, prefix)\r\nelse:\r\n    type_check = ('(Py_IS_TYPE({0}, {1}) ||\\n        '\r\n                  ' Py_TYPE({0})-\u003etp_new == {2}tp_new)'\r\n                 ).format(self.name, type_object, prefix)\r\n```\r\n\r\n`prefix` is a slightly modified variant of `type_object`, depending on if the latter is a pointer. This works swell in most cases, but with module state and heap types, the generated code is not optimal. First, let's just quote the AC docs on how to declare a class:\r\n\r\n\u003e When you declare a class, you must also specify two aspects of its type in C: the type declaration you’d use for a pointer to an instance of this class, and a pointer to the [PyTypeObject](https://docs.python.org/3.10/c-api/type.html#c.PyTypeObject) for this class.\r\n\r\nFor heap types with module state, you'd typically do something like this:\r\n\r\n```c\r\ntypedef struct {\r\n   ...\r\n} myclass_object;\r\n\r\ntypedef struct {\r\n    PyTypeObject myclass_type;\r\n} module_state;\r\n\r\nstatic inline module_state *\r\nfind_state_by_type(PyTypeObject *tp)\r\n{\r\n    PyObject *mod = PyType_GetModuleByDef(\u0026moduledef); // Potentially slow!\r\n    void *state = PyModule_GetState(mod);\r\n    return (module_state*)state;\r\n}\r\n\r\n#define clinic_state() (find_state_by_type(type))\r\n#include \"clinic/mymod.c.h\"\r\n#undef clinic_state\r\n\r\n/*[clinic input]\r\nmodule mymod\r\nclass mymod.myclass \"myclass_object *\" \"clinic_state()-\u003emyclass_type\"\r\n[clinic start generated code]*/\r\n```\r\n\r\nCurrently, this generates clinic code like this:\r\n\r\n```c\r\nmymod_myclass_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)\r\n{\r\n    ...\r\n\r\n    if ((type == clinic_state()-\u003eRowType ||\r\n         type-\u003etp_init == clinic_state()-\u003eRowType-\u003etp_init) \u0026\u0026\r\n```\r\n\r\n... _potentially_ calling `PyType_GetModuleByDef` twice in the self type check.\r\n\r\n# Pitch\r\n\r\nSuggesting to modify clinic to store the self type pointer in a local variable, and use that variable in the self type check. Proof-of-concept diff from the `_sqlite` extension module clinic code\r\n\r\n```diff\r\ndiff --git a/Modules/_sqlite/clinic/cursor.c.h b/Modules/_sqlite/clinic/cursor.c.h\r\nindex 36b8d0051a..633ad2e73d 100644\r\n--- a/Modules/_sqlite/clinic/cursor.c.h\r\n+++ b/Modules/_sqlite/clinic/cursor.c.h\r\n@@ -16,10 +16,11 @@ static int\r\n pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs)\r\n {\r\n     int return_value = -1;\r\n+    PyTypeObject *self_tp = clinic_state()-\u003eCursorType;\r\n     pysqlite_Connection *connection;\r\n \r\n-    if ((Py_IS_TYPE(self, clinic_state()-\u003eCursorType) ||\r\n-         Py_TYPE(self)-\u003etp_new == clinic_state()-\u003eCursorType-\u003etp_new) \u0026\u0026\r\n+    if ((Py_IS_TYPE(self, self_tp) ||\r\n+         Py_TYPE(self)-\u003etp_new == self_tp-\u003etp_new) \u0026\u0026\r\n         !_PyArg_NoKeywords(\"Cursor\", kwargs)) {\r\n         goto exit;\r\n     }\r\n@@ -318,4 +319,4 @@ pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))\r\n {\r\n     return pysqlite_cursor_close_impl(self);\r\n }\r\n-/*[clinic end generated code: output=e53e75a32a9d92bd input=a9049054013a1b77]*/\r\n+/*[clinic end generated code: output=e5eac0cbe29c88ad input=a9049054013a1b77]*/\r\n```\r\n\r\n# Previous discussion\r\n\r\nhttps://github.com/python/cpython/pull/101302#discussion_r1089924859\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-101411\r\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/erlend-aasland","@type":"Person","name":"erlend-aasland"},"datePublished":"2023-01-29T22:42:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/101409/cpython/issues/101409"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1418aa50-2b88-1abd-df43-c4f2ba3b6a61
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8C56:37C592:F17D34:1419A5A:696991B8
html-safe-nonce3d01f03bb9d003e1231a667af77931e60e56ad7e33686d6b7d6d1758e5a425f9
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzU2OjM3QzU5MjpGMTdEMzQ6MTQxOUE1QTo2OTY5OTFCOCIsInZpc2l0b3JfaWQiOiIzNzgwNzE2NzYzMDQwODEzNDk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac51e0a6d707662c7f50b252e0e915b239793549f0ea165bc20d57a4e6048b3696
hovercard-subject-tagissue:1561489769
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/101409/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4bcb79c2d71d4688ae712729e14f18e157c3f31358f1a8f70e4dbc1b007c3d2a/python/cpython/issues/101409
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4bcb79c2d71d4688ae712729e14f18e157c3f31358f1a8f70e4dbc1b007c3d2a/python/cpython/issues/101409
og:image:altFeature or enhancement Currently, typically for __new__ and __init__ methods, Argument Clinic will spell out the self type check as such: if kind == METHOD_NEW: type_check = ('({0} == {1} ||\n ' ' ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameerlend-aasland
hostnamegithub.com
expected-hostnamegithub.com
None3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseaf80af7cc9e3de9c336f18b208a600950a3c187c
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/101409#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F101409
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F101409
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/101409
Reloadhttps://github.com/python/cpython/issues/101409
Reloadhttps://github.com/python/cpython/issues/101409
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/101409
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/101409
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/101409
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/101409
Argument clinic: improve generated code for self converter type checkshttps://github.com/python/cpython/issues/101409#top
https://github.com/erlend-aasland
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-argument-clinichttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-argument-clinic%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/erlend-aasland
https://github.com/erlend-aasland
erlend-aaslandhttps://github.com/erlend-aasland
on Jan 29, 2023https://github.com/python/cpython/issues/101409#issue-1561489769
PyTypeObjecthttps://docs.python.org/3.10/c-api/type.html#c.PyTypeObject
#101302 (comment)https://github.com/python/cpython/pull/101302#discussion_r1089924859
gh-101409: Improve generated clinic code for self type checks #101411https://github.com/python/cpython/pull/101411
erlend-aaslandhttps://github.com/erlend-aasland
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-argument-clinichttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-argument-clinic%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.