Skip to content

gh-121617: Include <string.h> for Py_CLEAR() macro#144666

Merged
vstinner merged 4 commits intopython:mainfrom
vstinner:typeof
Feb 12, 2026
Merged

gh-121617: Include <string.h> for Py_CLEAR() macro#144666
vstinner merged 4 commits intopython:mainfrom
vstinner:typeof

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.

@encukou
Copy link
Member

encukou commented Feb 11, 2026

It looks hard to explain. Is it a good idea?

Looking more into this, I see <string.h> also defines NULL and size_t (as used in vectorcall).
These are also defined in these other headers that Python.h currently includes:

  • <stdio.h> & <stdlib.h> -- not included in limited API 3.11 and below
  • <wchar.h> -- which seems much less “fundamental” that string.h

Plus, I think we want to be able to use memcpy in macros/static inline functions -- as this bug shows, people tend to do that without thinking :)

I think we should go ahead and always include <string.h>.

@vstinner
Copy link
Member Author

I think we should go ahead and always include <string.h>.

Ok, let's do that.

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 12, 2026
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @encukou for commit b6082ac 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F144666%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 12, 2026
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Thanks!

Let's wait for buildbots; they've been grumpy about test_c[pp]ext lately.

@vstinner vstinner merged commit 9e5e1f9 into python:main Feb 12, 2026
118 of 119 checks passed
@vstinner vstinner deleted the typeof branch February 12, 2026 16:03
@vstinner
Copy link
Member Author

Merged. Thanks for the review @encukou.

@vstinner
Copy link
Member Author

Let's wait for buildbots; they've been grumpy about test_c[pp]ext lately.

The PEP 810 implementation introduced a minor incompatibility. It has been fixed quickly by commit 7854597.

 typedef enum {
     PyImport_LAZY_NORMAL,
     PyImport_LAZY_ALL,
-    PyImport_LAZY_NONE,
+    PyImport_LAZY_NONE
 } PyImport_LazyImportsMode;

The buildbot tests passed successfully on this PR ;-)

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.

3 participants