-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Py_INCREF()/Py_XINCREF() usage with Py_NewRef()/Py_XNewRef() #99300
Comments
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory. Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Python/ceval.c and related files.
Replace Py_INCREF() with Py_NewRef() in C files of the Parser/ directory and in the PEG generator.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Objects/dictobject.c.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Objects/dictobject.c.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Objects/listobject.c.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory. Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Python/ceval.c and related files.
Replace Py_INCREF() with Py_NewRef() in C files of the Parser/ directory and in the PEG generator.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Objects/dictobject.c.
Sorry for the conflicts :-( I know that it's unpleasant. I tried to group changes related to ceval.c in a single commit to not require people have to fix conflicts multiple times: it's a single shot. If you have troubles to fix your merge conflicts, I can help you to fix them. I have a tool to replace Py_INCREF with Py_NewRef(). Since I got positive feedbacks on other changes, I continue the work on this issue. The last big piece is the Modules/ directory. Then there are only a few remaining files which need to be changed. FYI I started to collect some patterns which look suspicious and I plan to investigate them. It seems like I found a few reference leaks, and replacing Py_INCREF with Py_NewRef helped me to identify these. I'm also considering to replace some "DECREF; INCREF; assign" patterns with Py_SETREF() when applicable. But I will open separated issues for these. |
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Modules/_datetimemodule.c and Modules/_zoneinfo.c
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Doc/ directory. Replace PyModule_AddObject() with PyModule_AddObjectRef() to simplify reference counting.
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the PC/ directory.
For the record, see gh-23170 for the PR that converted |
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Python/Python-ast.c. Update Parser/asdl_c.py to regenerate code.
* main: (8272 commits) Update Windows readme.txt to clarify Visual Studio required versions (pythonGH-99522) pythongh-99460 Emscripten trampolines on optimized METH_O and METH_NOARGS code paths (python#99461) pythongh-92647: [Enum] use final status to determine lookup or create (pythonGH-99500) pythongh-81057: Move Globals in Core Code to _PyRuntimeState (pythongh-99496) Post 3.12.0a2 pythongh-99300: Use Py_NewRef() in Python/Python-ast.c (python#99499) pythongh-93649: Split pytime and datetime tests from _testcapimodule.c (python#99494) pythongh-99370: fix test_zippath_from_non_installed_posix (pythonGH-99483) pythonGH-99205: remove `_static` field from `PyThreadState` and `PyInterpreterState` (pythonGH-99385) pythongh-81057: Move the Remaining Import State Globals to _PyRuntimeState (pythongh-99488) pythongh-87604: Avoid publishing list of active per-interpreter audit hooks via the gc module (pythonGH-99373) pythongh-93649: Split getargs tests from _testcapimodule.c (python#99346) pythongh-81057: Move Global Variables Holding Objects to _PyRuntimeState. (pythongh-99487) pythonGH-98219: reduce sleep time in `asyncio` subprocess test (python#99464) pythonGH-99388: add `loop_factory` parameter to `asyncio.run` (python#99462) pythongh-99300: Use Py_NewRef() in PC/ directory (python#99479) pythongh-99300: Use Py_NewRef() in Doc/ directory (python#99480) pythongh-99300: Use Py_NewRef() in Modules/ directory (python#99473) pythongh-99300: Use Py_NewRef() in Modules/ directory (python#99469) pythongh-99370: Calculate zip path from prefix when in a venv (pythonGH-99371) ...
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef() and Py_XNewRef().
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef() and Py_XNewRef().
26 commits later, I have converted all "INCREF + assign" and "INCREF + return" to NewRef. I am now closing the issue. |
I added Py_NewRef() and Py_XNewRef() to Python 3.10 C API. IMO using them make to code easier to read and make the code looks "more correct". Examples:
(A) Assign + INCREF:
(B) INCREF + assign:
(C) INCREF + return:
While technically, Py_INCREF() and Py_XINCREF() modify the object in-place (increment their reference counter), for me Py_NewRef() makes me sense: it creates "a new reference".
The example (A) is weird: it assigns a variable to something, and only later creates a new reference. For me, the syntax with Py_NewRef() makes more sense.
Examples (B) and (C) are shorter with Py_NewRef(), and again, IMO makes more sense and are more readable.
The text was updated successfully, but these errors were encountered: