[Python-checkins] bpo-45434: Only exclude <stdlib.h> in Python 3.11 limited C API (GH-29027)

vstinner webhook-mailer at python.org
Tue Oct 19 06:10:27 EDT 2021


https://github.com/python/cpython/commit/52af0756b2ffc6788e364971d05cdaf127d77d5a
commit: 52af0756b2ffc6788e364971d05cdaf127d77d5a
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-10-19T12:10:22+02:00
summary:

bpo-45434: Only exclude <stdlib.h> in Python 3.11 limited C API (GH-29027)

The Python 3.11 limited C API no longer includes stdlib.h, stdio.h,
string.h and errno.h.

* Exclude Py_MEMCPY() from Python 3.11 limited C API.
* xxlimited C extension is now built with Python 3.11 limited C API.

files:
D Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst
M Doc/whatsnew/3.11.rst
M Include/Python.h
M Include/pyport.h
M Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst
M Modules/xxlimited.c

diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 0776265779849..2e57f0cea53db 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -564,18 +564,10 @@ Porting to Python 3.11
 
   (Contributed by Victor Stinner in :issue:`39573`.)
 
-* The ``<Python.h>`` header file no longer includes ``<stdlib.h>``. C
-  extensions using ``<stdlib.h>`` must now include it explicitly.
-  The system ``<stdlib.h>`` header provides functions like:
-  ``malloc()``/``free()``, ``getenv()``, ``strtol()``, ``abs()``, ``strtol()``,
-  ``exit()`` and ``abort()``.
-  (Contributed by Victor Stinner in :issue:`45434`.)
-
-* The ``<Python.h>`` header file no longer includes ``<stdio.h>`` if the
-  ``Py_LIMITED_API`` macro is defined. Functions expecting ``FILE*`` are
-  excluded from the limited C API (:pep:`384`). C extensions using
-  ``<stdio.h>`` must now include it explicitly.  The system ``<stdio.h>``
-  header provides functions like ``printf()`` and ``fopen()``.
+* ``<Python.h>`` no longer includes the header files ``<stdlib.h>``,
+  ``<stdio.h>``, ``<errno.h>`` and ``<string.h>`` when the ``Py_LIMITED_API``
+  macro is set to ``0x030b0000`` (Python 3.11) or higher. C extensions should
+  explicitly include the header files after ``#include <Python.h>``.
   (Contributed by Victor Stinner in :issue:`45434`.)
 
 * The non-limited API files ``cellobject.h``, ``classobject.h``, ``context.h``,
diff --git a/Include/Python.h b/Include/Python.h
index c0a621ad44afd..6e3303ac9a3b0 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -16,12 +16,14 @@
 #  define _SGI_MP_SOURCE
 #endif
 
-#include <string.h>               // memcpy()
-#ifndef Py_LIMITED_API
+// stdlib.h, stdio.h, errno.h and string.h headers are not used by Python
+// headers, but kept for backward compatibility. They are excluded from the
+// limited C API of Python 3.11.
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
+#  include <stdlib.h>
 #  include <stdio.h>              // FILE*
-#endif
-#ifdef HAVE_ERRNO_H
 #  include <errno.h>              // errno
+#  include <string.h>             // memcpy()
 #endif
 #ifndef MS_WINDOWS
 #  include <unistd.h>
diff --git a/Include/pyport.h b/Include/pyport.h
index 0bec2a9b38f7c..61ca3a97c186b 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -201,9 +201,10 @@ typedef Py_ssize_t Py_ssize_clean_t;
 #  define Py_LOCAL_INLINE(type) static inline type
 #endif
 
-/* Py_MEMCPY is kept for backwards compatibility,
- * see https://bugs.python.org/issue28126 */
-#define Py_MEMCPY memcpy
+// bpo-28126: Py_MEMCPY is kept for backwards compatibility,
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
+#  define Py_MEMCPY memcpy
+#endif
 
 #ifdef HAVE_IEEEFP_H
 #include <ieeefp.h>  /* needed for 'finite' declaration on some platforms */
diff --git a/Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst b/Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst
index 95c5f0d1150c8..627f687272f5a 100644
--- a/Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst	
+++ b/Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst	
@@ -1,6 +1,5 @@
-The ``<Python.h>`` header file no longer includes ``<stdlib.h>``. C
-extensions using ``<stdlib.h>`` must now include it explicitly.
-The system ``<stdlib.h>`` header provides functions like:
-``malloc()``/``free()``, ``getenv()``, ``strtol()``, ``abs()``, ``strtol()``,
-``exit()`` and ``abort()``.
+``<Python.h>`` no longer includes the header files ``<stdlib.h>``,
+``<stdio.h>``, ``<errno.h>`` and ``<string.h>`` when the ``Py_LIMITED_API``
+macro is set to ``0x030b0000`` (Python 3.11) or higher. C extensions should
+explicitly include the header files after ``#include <Python.h>``.
 Patch by Victor Stinner.
diff --git a/Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst b/Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst
deleted file mode 100644
index 4a06635d179d9..0000000000000
--- a/Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst	
+++ /dev/null
@@ -1,5 +0,0 @@
-The ``<Python.h>`` header file no longer includes ``<stdio.h>`` if the
-``Py_LIMITED_API`` macro is defined. Functions expecting ``FILE*`` are excluded
-from the limited C API (:pep:`384`). C extensions using ``<stdio.h>`` must now
-include it explicitly.
-Patch by Victor Stinner.
diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c
index 9bd9a5407d43c..93895c4f1214c 100644
--- a/Modules/xxlimited.c
+++ b/Modules/xxlimited.c
@@ -1,4 +1,3 @@
-
 /* Use this file as a template to start implementing a module that
    also declares object types. All occurrences of 'Xxo' should be changed
    to something reasonable for your objects. After that, all other
@@ -55,7 +54,7 @@
           pass
    */
 
-#define Py_LIMITED_API 0x030a0000
+#define Py_LIMITED_API 0x030b0000
 
 #include "Python.h"
 



More information about the Python-checkins mailing list