Skip to content
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

bpo-43688: Support the limited C API in debug mode #25131

Merged
merged 1 commit into from
Apr 2, 2021
Merged

bpo-43688: Support the limited C API in debug mode #25131

merged 1 commit into from
Apr 2, 2021

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Apr 1, 2021

The limited C API is now supported if Python is built in debug mode
(if the Py_DEBUG macro is defined). In the limited C API, the
Py_INCREF() and Py_DECREF() functions are now implemented as opaque
function calls, rather than accessing directly the PyObject.ob_refcnt
member, if Python is built in debug mode and the Py_LIMITED_API macro
targets Python 3.10 or newer. It became possible to support the
limited C API in debug mode because the PyObject structure is the
same in release and debug mode since Python 3.8 (see bpo-36465).

The limited C API is still not supported in the --with-trace-refs
special build (Py_TRACE_REFS macro).

https://bugs.python.org/issue43688

Include/object.h Outdated
static inline void _Py_INCREF(PyObject *op)
{
#if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030A0000 || defined(Py_REF_DEBUG))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to make Py_INCREF() opaque for Python 3.9 and older, but we cannot magically add _Py_IncRef() to old Python versions :-(

@vstinner
Copy link
Member Author

vstinner commented Apr 1, 2021

cc @encukou

@vstinner
Copy link
Member Author

vstinner commented Apr 1, 2021

I merged the uncontroversial part in separated commits in https://bugs.python.org/issue43688 I rebased my PR on top on merged changes.

self.add(Extension('xxlimited', ['xxlimited.c'],
define_macros=[('Py_LIMITED_API', '0x030a0000')]))
self.add(Extension('xxlimited_35', ['xxlimited_35.c'],
define_macros=[('Py_LIMITED_API', '0x03050000')]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I found Py_LIMITED_API=0x03060000 in https://github.com/python/cpython/blob/master/PCbuild/xxlimited_35.vcxproj#L97.

I am not sure which one is right.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module name says "35": Python 3.5. See the C file header:


/* This module is compiled using limited API from Python 3.5,
 * making sure that it works as expected.
 *
 * See the xxlimited module for an extension module template.
 */

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's a good reason to put the define into the C file directly, rather than putting it in the recipe to build the file :-D

The limited C API is now supported if Python is built in debug mode
(if the Py_DEBUG macro is defined). In the limited C API, the
Py_INCREF() and Py_DECREF() functions are now implemented as opaque
function calls, rather than accessing directly the PyObject.ob_refcnt
member, if Python is built in debug mode and the Py_LIMITED_API macro
targets Python 3.10 or newer. It became possible to support the
limited C API in debug mode because the PyObject structure is the
same in release and debug mode since Python 3.8 (see bpo-36465).

The limited C API is still not supported in the --with-trace-refs
special build (Py_TRACE_REFS macro).
@vstinner
Copy link
Member Author

vstinner commented Apr 2, 2021

Windows x86: test_asyncio failed.

@vstinner
Copy link
Member Author

vstinner commented Apr 2, 2021

I re-run the CI jobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants