]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121617: Include <string.h> for Py_CLEAR() macro (#144666)
authorVictor Stinner <vstinner@python.org>
Thu, 12 Feb 2026 16:03:55 +0000 (17:03 +0100)
committerGitHub <noreply@github.com>
Thu, 12 Feb 2026 16:03:55 +0000 (17:03 +0100)
Python.h now also includes <string.h> in the limited C API version 3.11
and newer to fix the Py_CLEAR() macro which uses memcpy().

Add a Py_CLEAR() test in test_cext.

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

Doc/c-api/intro.rst
Include/Python.h
Include/pyport.h
Lib/test/test_cext/extension.c
Misc/NEWS.d/next/C_API/2026-02-10-14-49-49.gh-issue-121617.57vMqa.rst [new file with mode: 0644]

index a5dfbe7f4e1305bda16161fea3093135081a6575..c3a80234f861166be3f55353e6eacd2447164686 100644 (file)
@@ -123,6 +123,7 @@ System includes
    * ``<limits.h>``
    * ``<math.h>``
    * ``<stdarg.h>``
+   * ``<string.h>``
    * ``<wchar.h>``
    * ``<sys/types.h>`` (if present)
 
@@ -138,7 +139,6 @@ System includes
    * ``<errno.h>``
    * ``<stdio.h>``
    * ``<stdlib.h>``
-   * ``<string.h>``
 
 .. note::
 
index 78083bbf31db751926a58f96ddfc3fa07114f3a6..17cbc083241514fc703b93e18cc1a4c2d7d1d2d9 100644 (file)
 #include <limits.h>               // INT_MAX
 #include <math.h>                 // HUGE_VAL
 #include <stdarg.h>               // va_list
+#include <string.h>               // memcpy()
 #include <wchar.h>                // wchar_t
 #ifdef HAVE_SYS_TYPES_H
 #  include <sys/types.h>          // ssize_t
 #endif
 
-// <errno.h>, <stdio.h>, <stdlib.h> and <string.h> headers are no longer used
+// <errno.h>, <stdio.h> and <stdlib.h> headers are no longer used
 // by Python, but kept for the backward compatibility of existing third party C
 // extensions. They are not included by limited C API version 3.11 and newer.
 //
@@ -37,7 +38,6 @@
 #  include <errno.h>              // errno
 #  include <stdio.h>              // FILE*
 #  include <stdlib.h>             // getenv()
-#  include <string.h>             // memcpy()
 #endif
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000
 #  include <ctype.h>              // tolower()
index 61e2317976eed1db97373e4b7ff1a318ffb8612c..1e1702abd99a2c34c90e58681a4390a28701d54b 100644 (file)
@@ -567,8 +567,11 @@ extern "C" {
 //
 // Example: _Py_TYPEOF(x) x_copy = (x);
 //
-// The macro is only defined if GCC or clang compiler is used.
-#if defined(__GNUC__) || defined(__clang__)
+// On C23, use typeof(). Otherwise, the macro is only defined
+// if GCC or clang compiler is used.
+#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#  define _Py_TYPEOF(expr) typeof(expr)
+#elif defined(__GNUC__) || defined(__clang__)
 #  define _Py_TYPEOF(expr) __typeof__(expr)
 #endif
 
index a2f6151d8b36ed4fe6ca76dffb522b2baff022d5..a6b30fd627fe99a7ea0cfcffeb31e26be1a39bd2 100644 (file)
@@ -76,7 +76,7 @@ static PyMethodDef _testcext_methods[] = {
 static int
 _testcext_exec(PyObject *module)
 {
-    PyObject *result;
+    PyObject *result, *obj;
 
 #ifdef __STDC_VERSION__
     if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
@@ -92,6 +92,10 @@ _testcext_exec(PyObject *module)
     Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
     assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
 
+    // Test Py_CLEAR()
+    obj = NULL;
+    Py_CLEAR(obj);
+
     return 0;
 }
 
diff --git a/Misc/NEWS.d/next/C_API/2026-02-10-14-49-49.gh-issue-121617.57vMqa.rst b/Misc/NEWS.d/next/C_API/2026-02-10-14-49-49.gh-issue-121617.57vMqa.rst
new file mode 100644 (file)
index 0000000..cf84f8b
--- /dev/null
@@ -0,0 +1,3 @@
+``Python.h`` now also includes ``<string.h>`` in the limited C API version 3.11
+and newer to fix the :c:macro:`Py_CLEAR` macro which uses ``memcpy()``. Patch
+by Victor Stinner.