Skip to content

Commit 9e5e1f9

Browse files
authored
gh-121617: Include <string.h> for Py_CLEAR() macro (#144666)
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.
1 parent eb6ebdb commit 9e5e1f9

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

Doc/c-api/intro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ System includes
123123
* ``<limits.h>``
124124
* ``<math.h>``
125125
* ``<stdarg.h>``
126+
* ``<string.h>``
126127
* ``<wchar.h>``
127128
* ``<sys/types.h>`` (if present)
128129

@@ -138,7 +139,6 @@ System includes
138139
* ``<errno.h>``
139140
* ``<stdio.h>``
140141
* ``<stdlib.h>``
141-
* ``<string.h>``
142142

143143
.. note::
144144

Include/Python.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
#include <limits.h> // INT_MAX
2323
#include <math.h> // HUGE_VAL
2424
#include <stdarg.h> // va_list
25+
#include <string.h> // memcpy()
2526
#include <wchar.h> // wchar_t
2627
#ifdef HAVE_SYS_TYPES_H
2728
# include <sys/types.h> // ssize_t
2829
#endif
2930

30-
// <errno.h>, <stdio.h>, <stdlib.h> and <string.h> headers are no longer used
31+
// <errno.h>, <stdio.h> and <stdlib.h> headers are no longer used
3132
// by Python, but kept for the backward compatibility of existing third party C
3233
// extensions. They are not included by limited C API version 3.11 and newer.
3334
//
@@ -37,7 +38,6 @@
3738
# include <errno.h> // errno
3839
# include <stdio.h> // FILE*
3940
# include <stdlib.h> // getenv()
40-
# include <string.h> // memcpy()
4141
#endif
4242
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000
4343
# include <ctype.h> // tolower()

Include/pyport.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,11 @@ extern "C" {
567567
//
568568
// Example: _Py_TYPEOF(x) x_copy = (x);
569569
//
570-
// The macro is only defined if GCC or clang compiler is used.
571-
#if defined(__GNUC__) || defined(__clang__)
570+
// On C23, use typeof(). Otherwise, the macro is only defined
571+
// if GCC or clang compiler is used.
572+
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
573+
# define _Py_TYPEOF(expr) typeof(expr)
574+
#elif defined(__GNUC__) || defined(__clang__)
572575
# define _Py_TYPEOF(expr) __typeof__(expr)
573576
#endif
574577

Lib/test/test_cext/extension.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static PyMethodDef _testcext_methods[] = {
7676
static int
7777
_testcext_exec(PyObject *module)
7878
{
79-
PyObject *result;
79+
PyObject *result, *obj;
8080

8181
#ifdef __STDC_VERSION__
8282
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
@@ -92,6 +92,10 @@ _testcext_exec(PyObject *module)
9292
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
9393
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
9494

95+
// Test Py_CLEAR()
96+
obj = NULL;
97+
Py_CLEAR(obj);
98+
9599
return 0;
96100
}
97101

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``Python.h`` now also includes ``<string.h>`` in the limited C API version 3.11
2+
and newer to fix the :c:macro:`Py_CLEAR` macro which uses ``memcpy()``. Patch
3+
by Victor Stinner.

0 commit comments

Comments
 (0)