From b9dc732e715d346931b25731e5b702525608a34c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 18 Oct 2021 12:25:45 +0200 Subject: [PATCH 1/3] bpo-45434: Only exclude in Python 3.11 limited C API 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. --- Doc/whatsnew/3.11.rst | 15 +++------------ Include/Python.h | 10 ++++++---- Include/pyport.h | 4 +++- .../2021-10-11-23-03-49.bpo-45434.tsS8I_.rst | 8 +++----- .../2021-10-15-00-30-45.bpo-45434.XLtsbK.rst | 5 ----- Modules/xxlimited.c | 3 +-- 6 files changed, 16 insertions(+), 29 deletions(-) delete mode 100644 Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index c589139b15488fb..bfb6f90f8b7b212 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -564,18 +564,9 @@ Porting to Python 3.11 (Contributed by Victor Stinner in :issue:`39573`.) -* The ```` header file no longer includes ````. C - extensions using ```` must now include it explicitly. - The system ```` header provides functions like: - ``malloc()``/``free()``, ``getenv()``, ``strtol()``, ``abs()``, ``strtol()``, - ``exit()`` and ``abort()``. - (Contributed by Victor Stinner in :issue:`45434`.) - -* The ```` header file no longer includes ```` if the - ``Py_LIMITED_API`` macro is defined. Functions expecting ``FILE*`` are - excluded from the limited C API (:pep:`384`). C extensions using - ```` must now include it explicitly. The system ```` - header provides functions like ``printf()`` and ``fopen()``. +* The Python 3.11 limited C API no longer includes ````, + ````, ```` and ````. C extensions using these + headers must now include them explicitly. (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 c0a621ad44afd1f..6e3303ac9a3b0c8 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -16,12 +16,14 @@ # define _SGI_MP_SOURCE #endif -#include // 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 # include // FILE* -#endif -#ifdef HAVE_ERRNO_H # include // errno +# include // memcpy() #endif #ifndef MS_WINDOWS # include diff --git a/Include/pyport.h b/Include/pyport.h index 0bec2a9b38f7c9a..fb779b0641f4179 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -203,7 +203,9 @@ typedef Py_ssize_t Py_ssize_clean_t; /* Py_MEMCPY is kept for backwards compatibility, * see https://bugs.python.org/issue28126 */ -#define Py_MEMCPY memcpy +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 +# define Py_MEMCPY memcpy +#endif #ifdef HAVE_IEEEFP_H #include /* 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 95c5f0d1150c819..d17fd988f4c4fab 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,4 @@ -The ```` header file no longer includes ````. C -extensions using ```` must now include it explicitly. -The system ```` header provides functions like: -``malloc()``/``free()``, ``getenv()``, ``strtol()``, ``abs()``, ``strtol()``, -``exit()`` and ``abort()``. +The Python 3.11 limited C API no longer includes ````, ````, +```` and ````. C extensions using these headers must now +include them explicitly. 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 4a06635d179d9b9..000000000000000 --- a/Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst +++ /dev/null @@ -1,5 +0,0 @@ -The ```` header file no longer includes ```` if the -``Py_LIMITED_API`` macro is defined. Functions expecting ``FILE*`` are excluded -from the limited C API (:pep:`384`). C extensions using ```` must now -include it explicitly. -Patch by Victor Stinner. diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c index 9bd9a5407d43c4c..93895c4f1214c3c 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" From cf230f7c02d32247a4e0bd2d4a09c8656eae3aee Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 18 Oct 2021 14:08:01 +0200 Subject: [PATCH 2/3] Address Christian's review --- Doc/whatsnew/3.11.rst | 5 +++-- Include/pyport.h | 5 ++--- .../next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index bfb6f90f8b7b212..83bfd08daa713e4 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -565,8 +565,9 @@ Porting to Python 3.11 (Contributed by Victor Stinner in :issue:`39573`.) * The Python 3.11 limited C API no longer includes ````, - ````, ```` and ````. C extensions using these - headers must now include them explicitly. + ````, ```` and ````: + if ``Py_LIMITED_API >= 0x030b0000``. C extensions using these headers must + now include them explicitly. (Contributed by Victor Stinner in :issue:`45434`.) * The non-limited API files ``cellobject.h``, ``classobject.h``, ``context.h``, diff --git a/Include/pyport.h b/Include/pyport.h index fb779b0641f4179..61ca3a97c186bfc 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -201,9 +201,8 @@ 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 */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 +// bpo-28126: Py_MEMCPY is kept for backwards compatibility, +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 # define Py_MEMCPY memcpy #endif 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 d17fd988f4c4fab..3fd22490def514f 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,4 +1,4 @@ The Python 3.11 limited C API no longer includes ````, ````, -```` and ````. C extensions using these headers must now -include them explicitly. +```` and ````: if ``Py_LIMITED_API >= 0x030b0000``. +C extensions using these headers must now include them explicitly. Patch by Victor Stinner. From 12b65e168f0529d7bb0d0073cd4df3aecd1beab8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 18 Oct 2021 15:29:39 +0200 Subject: [PATCH 3/3] Rephrase the NEWS change --- Doc/whatsnew/3.11.rst | 8 ++++---- .../next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 83bfd08daa713e4..16d5d2f596d4a13 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -564,10 +564,10 @@ Porting to Python 3.11 (Contributed by Victor Stinner in :issue:`39573`.) -* The Python 3.11 limited C API no longer includes ````, - ````, ```` and ````: - if ``Py_LIMITED_API >= 0x030b0000``. C extensions using these headers must - now include them explicitly. +* ```` no longer includes the header files ````, + ````, ```` and ```` 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 ``. (Contributed by Victor Stinner in :issue:`45434`.) * The non-limited API files ``cellobject.h``, ``classobject.h``, ``context.h``, 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 3fd22490def514f..627f687272f5a9f 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,4 +1,5 @@ -The Python 3.11 limited C API no longer includes ````, ````, -```` and ````: if ``Py_LIMITED_API >= 0x030b0000``. -C extensions using these headers must now include them explicitly. +```` no longer includes the header files ````, +````, ```` and ```` 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 ``. Patch by Victor Stinner.