Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Feb 10, 2026

If the _Py_TYPEOF macro is not available, include <string.h> to get memcpy() for the Py_CLEAR() macro. The limited C API version 3.11 and newer doesn't include <string.h> anymore.

Add a Py_CLEAR() test in test_cext.

Modify also _Py_TYPEOF to use C23 typeof() if available.

If the _Py_TYPEOF macro is not available, include <string.h> to get
memcpy() for the Py_CLEAR() macro. The limited C API version 3.11 and
newer doesn't include <string.h> anymore.

Add a Py_CLEAR() test in test_cext.

Modify also _Py_TYPEOF to use C23 typeof() if available.
@vstinner
Copy link
Member Author

If this change is merged in the main branch, I will write a backport to the 3.14 branch without the C23 change using typeof().

@vstinner
Copy link
Member Author

Note: Py_SETREF() and Py_XSETREF() macros also use memcpy(), but these macros are only defined if the limited C API is not used. In this case, Python.h includes <string.h> and so we are already good (no change needed). Only Py_CLEAR() of the limited C API version 3.11 and newer is affected by the issue.

@encukou
Copy link
Member

encukou commented Feb 10, 2026

The documentation also needs an update. (3.14 has the list here.)

It's somewhat hard to explain, unfortunately. Is it enough to add something vague like “<string.h> (on certain compilers)”?

@vstinner
Copy link
Member Author

I added the following sentence to the doc:

The <string.h> header is also included when using Limited API 3.11 or newer if Python is unable to get a typeof() implementation.

I'm not sure if it's worth it to document this corner case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants