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
Domain: github.com
IPv4 address: 140.82.114.3
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9710b6dd-fc75-4250-863d-7a79eb640c96 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E0C2:1E80BA:E5BAF6:139090E:6B06584A |
| html-safe-nonce | f586235857aa139f80f7fc58a4227aaddfb54962ff1f087fb302b32bfbfcb0e2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEMyOjFFODBCQTpFNUJBRjY6MTM5MDkwRTo2QjA2NTg0QSIsInZpc2l0b3JfaWQiOiI2NTE5MTEyNDg3MDc2NzgwMTA2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 04f3c3ab5bbcaec23c7a56715abfc8247799a6636ea4f87f17a48bc692281374 |
| hovercard-subject-tag | issue:2520131951 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/123961/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d3b7c4c18c2e3f726a84669ae9b3cebe2bfbaebcc702d73d5894461f21aa7b2a/python/cpython/issues/123961 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d3b7c4c18c2e3f726a84669ae9b3cebe2bfbaebcc702d73d5894461f21aa7b2a/python/cpython/issues/123961 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | picnixz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6324e6ac990fb6befbec840cc0e102a1071c339bfe8f9826bb12de2e3ba1d297 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7f0aa75f4141d440c6110dc31af7ea7797abb6c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width