[Python-checkins] bpo-45434: Limited Python.h no longer includes stdio.h (GH-28960)

vstinner webhook-mailer at python.org
Thu Oct 14 19:09:14 EDT 2021


https://github.com/python/cpython/commit/284994762d820d8e09cc019f8f7c4bc501e37dd4
commit: 284994762d820d8e09cc019f8f7c4bc501e37dd4
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-10-15T01:09:06+02:00
summary:

bpo-45434: Limited Python.h no longer includes stdio.h (GH-28960)

The <Python.h> header file no longer includes <stdio.h> if the
Py_LIMITED_API macro is defined.

files:
A 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

diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index e8d64a80a69a3..a6e30c77c55a0 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -565,6 +565,13 @@ Porting to Python 3.11
   ``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()``.
+  (Contributed by Victor Stinner in :issue:`45434`.)
+
 Deprecated
 ----------
 
diff --git a/Include/Python.h b/Include/Python.h
index 4f62103e30276..dc5c9b8e6384e 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -16,12 +16,10 @@
 #  define _SGI_MP_SOURCE
 #endif
 
-#include <stdio.h>                // NULL, FILE*
-#ifndef NULL
-#   error "Python.h requires that stdio.h define NULL."
-#endif
-
 #include <string.h>               // memcpy()
+#ifndef Py_LIMITED_API
+#  include <stdio.h>              // FILE*
+#endif
 #ifdef HAVE_ERRNO_H
 #  include <errno.h>              // errno
 #endif
@@ -29,8 +27,7 @@
 #  include <unistd.h>
 #endif
 #ifdef HAVE_STDDEF_H
-   // For size_t
-#  include <stddef.h>
+#  include <stddef.h>             // size_t
 #endif
 
 #include <assert.h>               // assert()
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
new file mode 100644
index 0000000000000..4a06635d179d9
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-10-15-00-30-45.bpo-45434.XLtsbK.rst	
@@ -0,0 +1,5 @@
+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.



More information about the Python-checkins mailing list