René's URL Explorer Experiment


Title: Refactorization of `_cursesmodule.c` to fix reference leaks · Issue #123961 · python/cpython · GitHub

Open Graph Title: Refactorization of `_cursesmodule.c` to fix reference leaks · Issue #123961 · python/cpython

X Title: Refactorization of `_cursesmodule.c` to fix reference leaks · Issue #123961 · python/cpython

Description: Feature or enhancement gh-123962: global dictionary removal In _cursesmodule.c, there is a global reference to the module's dictionary. The reason is that we cannot add all constants when the module is being initialized because some cons...

Open Graph Description: Feature or enhancement gh-123962: global dictionary removal In _cursesmodule.c, there is a global reference to the module's dictionary. The reason is that we cannot add all constants when the modul...

X Description: Feature or enhancement gh-123962: global dictionary removal In _cursesmodule.c, there is a global reference to the module's dictionary. The reason is that we cannot add all constants when the m...

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

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.3


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Refactorization of  `_cursesmodule.c` to fix reference leaks","articleBody":"# Feature or enhancement\r\n\r\n### gh-123962: global dictionary removal\r\n\r\nIn `_cursesmodule.c`, there is a global reference to the module's dictionary. The reason is that we cannot add all constants when the module is being initialized because some constants require `curses.initscr()` to be called beforehand. \r\n\r\nInstead of storing the module's dictionary in a global variable, we can access it in `curses.initscr()` using `PyModule_GetDict`. \r\n\r\n### gh-124047: global variable name cleanup\r\n\r\nSome global variables are modified in `_cursesmodule.c` by other functions and their names can be confused with a local variable name. I wanted to use uppercase names but Victor argued that this should be reserved for C macros (https://github.com/python/cpython/pull/123910#discussion_r1754335272) and suggested keeping prefixed lowercase names.\r\n\r\n### gh-124729: use a global state object to hold global variables\r\n\r\nOnce we are done with the above changes, we introduce a global module's state object. This state will hold the error and window type (which needs to be converted into a heap type) that are currently global variables. \r\n\r\n### gh-124907: improve C API cleanup\r\n\r\nWe improve the interface for creating and destroying the underlying C API and its associated capsule. Once we transform the Window type into a heap type, we'll also add the corresponding `traverse()` and `clear()` callbacks to the capsule object.\r\n\r\n### gh-124934: transform window type into a heap type\r\n\r\nIn this task, we make curses window type a heap type instead of a static one. Since the module is still not yet a multi-phase initialization module, we cannot entirely clean-up all references.\r\n\r\n### gh-125000: make the module ~~free-threaded friendly~~ process-wide\r\n\r\nThe module should not be allowed to be exec'ed() more than once by process. This is achieved by adding a global flag. ~~In addition, we should make the read/write operations on tbe global flags atomic so that free-threaded builds do not have race conditions.~~\r\n\r\n### gh-124965: implement PEP-489 for `curses`\r\n\r\nThe last task takes care of changing the module into a PEP-489 compliant one. It will also take care of the long-standing reference leaks in curses.\r\n\r\n---\r\n\r\nSome extra and optional tasks at the end:\r\n\r\n### Error messages improvements\r\n\r\nSome functions say that \"X() returned ERR\" but the function X is not always the correct one (e.g., in `_curses_window_echochar_impl`, it says that `echochar` returned ERR but this depends on whether we used `echochar` or `wechochar`). I think we need to really use the name of the function that was called at runtime (some functions do this but not all).\r\n\r\nThe idea is to extend the exception type so that it also has two attributes, namely `called_funcname` and `curses_funcname` (subject to bikeshedding). The `called_funcname` value would contain the incriminating function (i.e. where the exception was raised) while `curses_funcname` would contain the runtime C function that was used (which may differ in many cases from the actual curses Python function).\r\n\r\nTo that end, we need the exception type to be a heap type, since a static type would be 1) an overkill 2) still leaks (and we don't like leaks).\r\n\r\n### Cosmetic changes\r\n\r\nI can't say I'm maintaining curses but I can say that it's tiring to work with a file of more 5k lines. Many lines are actually caused by the fact that we have both the implementation for the window object and for the module's method. But these two can probably be split into two files. I don't know if this is suitable but it would probably help maintining the code (both take roughly 2k lines of code). Here are the various cosmetic points I'd like to address:\r\n\r\n- Make the macros more readable (there are packed and honestly, for a first-time contributor, hard to parse).\r\n- ~Split the module into multiple `.c` files. This is not necessarily mandatory because we would probably need to export some functions that are shared or add some internal header so it might overcomplicate the task.~\r\n- Simplify some code paths (just some if/else that could be cleaner or things like that). \r\n- Put whitespaces in function calls (honestly, when you see `func(y,x,  something)`, it's hard to read). \r\n\r\nThose cosmetic changes could be helpful to newcomers, since no one is really interested in this module. But those are just things I had in mind while refactoring the module, none of them are really required (and I'm now quite used to).\r\n\r\n#### EDIT (03/10/24)\r\n\r\nI'll just give up on splitting the files. There are too many inter-dependencies between functions that it makes any PR for that too hard to review. In addition, we would end up exporting too many un-necessary symbols.\r\n\r\n### Related Work\r\n\r\n* 2938c3dec99390087490124c2ef50e1592671e72 (tracked by [gh-123290](123290))\r\n* e49d1b44d3ef2542c0ae165e14a7e5ffbc32b2d1 (tracked by [gh-124044](124044))\r\n\r\n\u003c!-- gh-linked-prs -->\r\n### Linked PRs\r\n* gh-123962\r\n* gh-124047\r\n* gh-124729\r\n* gh-124907\r\n* gh-124934\r\n* gh-124965\r\n* gh-125000\r\n\u003c!-- /gh-linked-prs -->\r\n","author":{"url":"https://github.com/picnixz","@type":"Person","name":"picnixz"},"datePublished":"2024-09-11T15:56:02Z","url":"https://github.com/python/cpython/issues/123961"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9710b6dd-fc75-4250-863d-7a79eb640c96
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE0C2:1E80BA:E5BAF6:139090E:6B06584A
html-safe-noncef586235857aa139f80f7fc58a4227aaddfb54962ff1f087fb302b32bfbfcb0e2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEMyOjFFODBCQTpFNUJBRjY6MTM5MDkwRTo2QjA2NTg0QSIsInZpc2l0b3JfaWQiOiI2NTE5MTEyNDg3MDc2NzgwMTA2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac04f3c3ab5bbcaec23c7a56715abfc8247799a6636ea4f87f17a48bc692281374
hovercard-subject-tagissue:2520131951
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/123961/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d3b7c4c18c2e3f726a84669ae9b3cebe2bfbaebcc702d73d5894461f21aa7b2a/python/cpython/issues/123961
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d3b7c4c18c2e3f726a84669ae9b3cebe2bfbaebcc702d73d5894461f21aa7b2a/python/cpython/issues/123961
og:image:altFeature or enhancement gh-123962: global dictionary removal In _cursesmodule.c, there is a global reference to the module's dictionary. The reason is that we cannot add all constants when the modul...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepicnixz
hostnamegithub.com
expected-hostnamegithub.com
None6324e6ac990fb6befbec840cc0e102a1071c339bfe8f9826bb12de2e3ba1d297
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
release7f0aa75f4141d440c6110dc31af7ea7797abb6c4
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/123961#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F123961
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F123961
Sign uphttps://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/123961
Reloadhttps://github.com/python/cpython/issues/123961
Reloadhttps://github.com/python/cpython/issues/123961
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/123961
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/123961
Refactorization of _cursesmodule.c to fix reference leakshttps://github.com/python/cpython/issues/123961#top
https://github.com/picnixz
extension-moduleshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-featurehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Sep 11, 2024https://github.com/python/cpython/issues/123961#issue-2520131951
gh-123962https://github.com/python/cpython/pull/123962
gh-124047https://github.com/python/cpython/pull/124047
#123910 (comment)https://github.com/python/cpython/pull/123910#discussion_r1754335272
gh-124729https://github.com/python/cpython/pull/124729
gh-124907https://github.com/python/cpython/pull/124907
gh-124934https://github.com/python/cpython/pull/124934
gh-125000https://github.com/python/cpython/pull/125000
gh-124965https://github.com/python/cpython/pull/124965
PEP-489https://peps.python.org/489
PEP-489https://peps.python.org/489
2938c3dhttps://github.com/python/cpython/commit/2938c3dec99390087490124c2ef50e1592671e72
gh-123290https://github.com/python/cpython/issues/123290
e49d1b4https://github.com/python/cpython/commit/e49d1b44d3ef2542c0ae165e14a7e5ffbc32b2d1
gh-124044https://github.com/python/cpython/issues/124044
gh-123961: remove global variable ModDict in _cursesmodule.c #123962https://github.com/python/cpython/pull/123962
gh-123961: add curses prefix to global variable names in _cursesmodule.c #124047https://github.com/python/cpython/pull/124047
gh-123961: move PyCursesError in _cursesmodule.c to a global state variable #124729https://github.com/python/cpython/pull/124729
gh-123961: Improve curses C API cleanup #124907https://github.com/python/cpython/pull/124907
gh-123961: convert curses.window static type into a heap type #124934https://github.com/python/cpython/pull/124934
gh-123961: convert curses to a multi-phase initialization module (PEP-489) #124965https://github.com/python/cpython/pull/124965
gh-123961: make curses global flags free-threaded friendly #125000https://github.com/python/cpython/pull/125000
picnixzhttps://github.com/picnixz
type-featurehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
on Sep 11, 2024https://github.com/python/cpython/issues/123961#event-14218798457
picnixzhttps://github.com/picnixz
on Sep 11, 2024https://github.com/python/cpython/issues/123961#event-14218798533
picnixzhttps://github.com/picnixz
extension-moduleshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
on Sep 11, 2024https://github.com/python/cpython/issues/123961#event-14218806383
picnixzhttps://github.com/picnixz
Curses issueshttps://github.com/orgs/python/projects/18
on Sep 11, 2024https://github.com/python/cpython/issues/123961#event-14218808148
picnixzhttps://github.com/picnixz
Curses issueshttps://github.com/orgs/python/projects/18
on Sep 11, 2024https://github.com/python/cpython/issues/123961
picnixzhttps://github.com/picnixz
Curses issueshttps://github.com/orgs/python/projects/18
on Sep 11, 2024https://github.com/python/cpython/issues/123961
vstinnerhttps://github.com/vstinner
on Sep 13, 2024https://github.com/python/cpython/issues/123961#event-14246875766
gh-123961: Remove global variable `ModDict` in `_cursesmodule.c` (#12…https://github.com/python/cpython/commit/403f3ddedcab14f6c16ea78a93bb4acf49d06a07
gh-123961https://github.com/python/cpython/issues/123961
#12…https://github.com/python/cpython/pull/123962
403f3ddhttps://github.com/python/cpython/commit/403f3ddedcab14f6c16ea78a93bb4acf49d06a07
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Sep 13, 2024https://github.com/python/cpython/issues/123961#issuecomment-2348618249
#123962https://github.com/python/cpython/pull/123962
picnixzhttps://github.com/picnixz
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 13, 2024https://github.com/python/cpython/issues/123961#event-14246903363
github-project-automationhttps://github.com/apps/github-project-automation
Curses issueshttps://github.com/orgs/python/projects/18
on Sep 13, 2024https://github.com/python/cpython/issues/123961
picnixzhttps://github.com/picnixz
on Sep 13, 2024https://github.com/python/cpython/issues/123961#event-14247949383
picnixzhttps://github.com/picnixz
on Sep 13, 2024https://github.com/python/cpython/issues/123961#event-14247951762
picnixzhttps://github.com/picnixz
Curses issueshttps://github.com/orgs/python/projects/18
on Sep 13, 2024https://github.com/python/cpython/issues/123961
vstinnerhttps://github.com/vstinner
on Sep 13, 2024https://github.com/python/cpython/issues/123961#event-14248388306
gh-123961: Add `curses` prefix to global variables in `_cursesmodule.…https://github.com/python/cpython/commit/acb3f875fba0552d55989f1f2686c334fecaecca
gh-123961https://github.com/python/cpython/issues/123961
acb3f87https://github.com/python/cpython/commit/acb3f875fba0552d55989f1f2686c334fecaecca
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Sep 27, 2024https://github.com/python/cpython/issues/123961#issuecomment-2379902707
vstinnerhttps://github.com/vstinner
on Sep 29, 2024https://github.com/python/cpython/issues/123961#event-14445542466
gh-123961: Add a global state to _curses (#124729)https://github.com/python/cpython/commit/4d8e7c40a0d37a84da1e0c130e7d585eb39438ad
gh-123961https://github.com/python/cpython/issues/123961
#124729https://github.com/python/cpython/pull/124729
4d8e7c4https://github.com/python/cpython/commit/4d8e7c40a0d37a84da1e0c130e7d585eb39438ad
gh-123961: Clean up curses C API/capsule (#124907)https://github.com/python/cpython/commit/e6dd71da3acc21edcc205d024d18004c7a3c7f45
gh-123961https://github.com/python/cpython/issues/123961
#124907https://github.com/python/cpython/pull/124907
e6dd71dhttps://github.com/python/cpython/commit/e6dd71da3acc21edcc205d024d18004c7a3c7f45
gh-123961: Convert `curses.window` static type into a heap type (#124934https://github.com/python/cpython/commit/f66d785861d3a210f1b6b14ebd129728b24f95bf
gh-123961https://github.com/python/cpython/issues/123961
#124934https://github.com/python/cpython/pull/124934
f66d785https://github.com/python/cpython/commit/f66d785861d3a210f1b6b14ebd129728b24f95bf
gh-123961: Convert _curses to a multi-phase init module (PEP-489) (#1…https://github.com/python/cpython/commit/e4292c041018dd9e831e541b515fec1119f92690
gh-123961https://github.com/python/cpython/issues/123961
PEP-489https://peps.python.org/489
#1…https://github.com/python/cpython/pull/124965
e4292c0https://github.com/python/cpython/commit/e4292c041018dd9e831e541b515fec1119f92690
efimov-mikhailhttps://github.com/efimov-mikhail
on Oct 9, 2024https://github.com/python/cpython/issues/123961#event-14570076843
pythongh-123961: Convert _curses to a multi-phase init module (PEP-48…https://github.com/efimov-mikhail/cpython/commit/c4812df2f7aeed89b2622e5c50907571883a2a9c
pythongh-123961https://github.com/python/cpython/issues/123961
c4812dfhttps://github.com/efimov-mikhail/cpython/commit/c4812df2f7aeed89b2622e5c50907571883a2a9c
picnixzhttps://github.com/picnixz
on Oct 14, 2024https://github.com/python/cpython/issues/123961#event-14636609319
https://github.com/picnixz
picnixzhttps://github.com/picnixz
on Oct 14, 2024https://github.com/python/cpython/issues/123961#issuecomment-2411342440
picnixzhttps://github.com/picnixz
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Oct 14, 2024https://github.com/python/cpython/issues/123961#event-14636673197
github-project-automationhttps://github.com/apps/github-project-automation
Curses issueshttps://github.com/orgs/python/projects/18
on Oct 14, 2024https://github.com/python/cpython/issues/123961
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/123961
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/123961
picnixzhttps://github.com/picnixz
extension-moduleshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-featurehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
Curses issueshttps://github.com/orgs/python/projects/18
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

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.