[Python-checkins] bpo-45434: pyport.h no longer includes <stdlib.h> (GH-28914)

vstinner webhook-mailer at python.org
Wed Oct 13 13:26:00 EDT 2021


https://github.com/python/cpython/commit/aac29af6785712019d34f1a7f15b3c408a4f68ae
commit: aac29af6785712019d34f1a7f15b3c408a4f68ae
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-10-13T19:25:53+02:00
summary:

bpo-45434: pyport.h no longer includes <stdlib.h> (GH-28914)

Include <stdlib.h> explicitly in C files.

Python.h includes <wchar.h>.

files:
M Doc/whatsnew/3.11.rst
M Include/Python.h
M Include/internal/pycore_fileutils.h
M Include/pyport.h
M Misc/NEWS.d/next/C API/2021-10-11-23-03-49.bpo-45434.tsS8I_.rst
M Modules/_ctypes/_ctypes_test.c
M Modules/_gdbmmodule.c
M Modules/_lzmamodule.c
M Modules/_pickle.c
M Modules/_tracemalloc.c
M Modules/faulthandler.c
M Modules/getpath.c
M Modules/nismodule.c
M Modules/ossaudiodev.c
M Modules/posixmodule.c
M Modules/readline.c
M Objects/floatobject.c
M Objects/longobject.c
M Objects/obmalloc.c
M PC/WinMain.c
M Programs/_freeze_module.c
M Programs/_testembed.c
M Python/dtoa.c
M Python/errors.c
M Python/fileutils.c
M Python/initconfig.c
M Python/preconfig.c
M Python/pylifecycle.c
M Python/pystrhex.c

diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 21a0e1ab9c693..d18711d0b8df2 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -556,6 +556,9 @@ Porting to Python 3.11
 
 * 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`.)
 
 Deprecated
diff --git a/Include/Python.h b/Include/Python.h
index 8c84c90f9a57e..4f62103e30276 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -33,7 +33,8 @@
 #  include <stddef.h>
 #endif
 
-#include <assert.h>
+#include <assert.h>               // assert()
+#include <wchar.h>                // wchar_t
 
 #include "pyport.h"
 #include "pymacro.h"
diff --git a/Include/internal/pycore_fileutils.h b/Include/internal/pycore_fileutils.h
index 2316a978294e2..3464477bce575 100644
--- a/Include/internal/pycore_fileutils.h
+++ b/Include/internal/pycore_fileutils.h
@@ -88,6 +88,9 @@ extern int _Py_add_relfile(wchar_t *dirname,
 //      ...
 //      _Py_END_SUPPRESS_IPH
 #if defined _MSC_VER && _MSC_VER >= 1900
+
+#  include <stdlib.h>   // _set_thread_local_invalid_parameter_handler()
+
    extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
 #  define _Py_BEGIN_SUPPRESS_IPH \
     { _invalid_parameter_handler _Py_old_handler = \
diff --git a/Include/pyport.h b/Include/pyport.h
index 6e4e98020cb1f..0bec2a9b38f7c 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -205,8 +205,6 @@ typedef Py_ssize_t Py_ssize_clean_t;
  * see https://bugs.python.org/issue28126 */
 #define Py_MEMCPY memcpy
 
-#include <stdlib.h>
-
 #ifdef HAVE_IEEEFP_H
 #include <ieeefp.h>  /* needed for 'finite' declaration on some platforms */
 #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 c04dda532557d..95c5f0d1150c8 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,3 +1,6 @@
 The ``<Python.h>`` header file no longer includes ``<stdlib.h>``. C
-extensions using ``<stdlib.h>`` must now include it explicitly. Patch by
-Victor Stinner.
+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()``.
+Patch by Victor Stinner.
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
index a33d15de9c0d4..770c96c60d1f7 100644
--- a/Modules/_ctypes/_ctypes_test.c
+++ b/Modules/_ctypes/_ctypes_test.c
@@ -4,6 +4,8 @@
 #include <windows.h>
 #endif
 
+#include <stdlib.h>               // qsort()
+
 #define EXPORT(x) Py_EXPORTED_SYMBOL x
 
 /* some functions handy for testing */
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index efbf331ca302d..445500c7ee4c1 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -5,11 +5,12 @@
 
 #define PY_SSIZE_T_CLEAN
 #include "Python.h"
+#include "gdbm.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
-#include "gdbm.h"
+#include <stdlib.h>               // free()
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #if defined(WIN32) && !defined(__CYGWIN__)
 #include "gdbmerrno.h"
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index a7156ec7ddf04..e50f55b75c5dc 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -10,6 +10,7 @@
 #include "Python.h"
 #include "structmember.h"         // PyMemberDef
 
+#include <stdlib.h>               // free()
 #include <string.h>
 
 #include <lzma.h>
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index e693b506a1e91..b5131696981dc 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -12,6 +12,8 @@
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "structmember.h"         // PyMemberDef
 
+#include <stdlib.h>               // strtol()
+
 PyDoc_STRVAR(pickle_module_doc,
 "Optimized C implementation for the Python pickle module.");
 
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index 9ba0ebbbaab5c..09d273ad280bb 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -5,7 +5,10 @@
 #include "pycore_hashtable.h"
 #include <pycore_frame.h>
 
+#include <stdlib.h>               // malloc()
+
 #include "clinic/_tracemalloc.c.h"
+
 /*[clinic input]
 module _tracemalloc
 [clinic start generated code]*/
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index 868b4f4f42de6..cb2e2588e19b2 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -3,10 +3,13 @@
 #include "pycore_pyerrors.h"      // _Py_DumpExtensionModules
 #include "pycore_pystate.h"       // _PyThreadState_GET()
 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads
-#include <signal.h>
+
+#include "frameobject.h"
+
 #include <object.h>
-#include <frameobject.h>
 #include <signal.h>
+#include <signal.h>
+#include <stdlib.h>               // abort()
 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
 #  include <pthread.h>
 #endif
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 22e5ef24b5d57..1405023b39b58 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -6,8 +6,9 @@
 #include "pycore_pathconfig.h"
 #include "osdefs.h"               // DELIM
 
-#include <sys/types.h>
+#include <stdlib.h>               // getenv()
 #include <string.h>
+#include <sys/types.h>
 
 #ifdef __APPLE__
 #  include <mach-o/dyld.h>
diff --git a/Modules/nismodule.c b/Modules/nismodule.c
index 6655451ebd2fb..cdda1a6a2fb7d 100644
--- a/Modules/nismodule.c
+++ b/Modules/nismodule.c
@@ -12,6 +12,7 @@
 
 #include "Python.h"
 
+#include <stdlib.h>               // free()
 #include <sys/time.h>
 #include <sys/types.h>
 #include <rpc/rpc.h>
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index 4f2d9cb8b7c9c..4bab9a58eb104 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -21,6 +21,7 @@
 #include "Python.h"
 #include "structmember.h"         // PyMemberDef
 
+#include <stdlib.h>               // getenv()
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #else
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 9c174ee6b18df..667a3339f5ba8 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -46,7 +46,8 @@
 #  undef HAVE_FACCESSAT
 #endif
 
-#include <stdio.h>  /* needed for ctermid() */
+#include <stdio.h>                // ctermid()
+#include <stdlib.h>               // system()
 
 /*
  * A number of APIs are available on macOS from a certain macOS version.
diff --git a/Modules/readline.c b/Modules/readline.c
index c79d22f85f84e..1b616fc4f3b4e 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -6,9 +6,11 @@
 
 /* Standard definitions */
 #include "Python.h"
-#include <stddef.h>
-#include <signal.h>
+
 #include <errno.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdlib.h>               // free()
 #include <sys/time.h>
 
 #if defined(HAVE_SETLOCALE)
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index d25d97f4cbf61..5a8113eca8bd0 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -13,6 +13,7 @@
 
 #include <ctype.h>
 #include <float.h>
+#include <stdlib.h>               // strtol()
 
 /*[clinic input]
 class float "PyObject *" "&PyFloat_Type"
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 33fea6491b73d..111ef4566cda8 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -10,9 +10,10 @@
 #include "pycore_pystate.h"       // _Py_IsMainInterpreter()
 #include "longintrepr.h"
 
-#include <float.h>
 #include <ctype.h>
+#include <float.h>
 #include <stddef.h>
+#include <stdlib.h>               // abs()
 
 #include "clinic/longobject.c.h"
 /*[clinic input]
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index 2d6fedd6cf0a1..d8d6f6dea0d53 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -2,6 +2,7 @@
 #include "pycore_pymem.h"         // _PyTraceMalloc_Config
 
 #include <stdbool.h>
+#include <stdlib.h>               // malloc()
 
 
 /* Defined in tracemalloc.c */
diff --git a/PC/WinMain.c b/PC/WinMain.c
index e439bed9ed113..07e21ce3e9f2d 100644
--- a/PC/WinMain.c
+++ b/PC/WinMain.c
@@ -4,6 +4,7 @@
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <stdlib.h>  /* __argc, __wargv */
 
 int WINAPI wWinMain(
     HINSTANCE hInstance,      /* handle to current instance */
diff --git a/Programs/_freeze_module.c b/Programs/_freeze_module.c
index dd90d92e512c0..316c70d2c7824 100644
--- a/Programs/_freeze_module.c
+++ b/Programs/_freeze_module.c
@@ -12,6 +12,7 @@
 #include <pycore_import.h>
 
 #include <stdio.h>
+#include <stdlib.h>               // malloc()
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifndef MS_WINDOWS
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index fa418e276114a..b61fe3461bdc9 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -11,6 +11,7 @@
 #include <Python.h>
 #include <inttypes.h>
 #include <stdio.h>
+#include <stdlib.h>               // putenv()
 #include <wchar.h>
 
 /*********************************************************
diff --git a/Python/dtoa.c b/Python/dtoa.c
index e629b296426f3..6c44f68965176 100644
--- a/Python/dtoa.c
+++ b/Python/dtoa.c
@@ -119,6 +119,7 @@
 
 #include "Python.h"
 #include "pycore_dtoa.h"
+#include <stdlib.h>               // exit()
 
 /* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
    the following code */
diff --git a/Python/errors.c b/Python/errors.c
index f072c21496592..519f2d459effd 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -15,12 +15,13 @@ extern char *strerror(int);
 #endif
 #endif
 
+#include <ctype.h>
 #ifdef MS_WINDOWS
-#include <windows.h>
-#include <winbase.h>
+#  include <windows.h>
+#  include <winbase.h>
+#  include <stdlib.h>             // _sys_nerr
 #endif
 
-#include <ctype.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/Python/fileutils.c b/Python/fileutils.c
index a36415946e218..173d34dd23f18 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -3,6 +3,7 @@
 #include "pycore_runtime.h"       // _PyRuntime
 #include "osdefs.h"               // SEP
 #include <locale.h>
+#include <stdlib.h>               // mbstowcs()
 
 #ifdef MS_WINDOWS
 #  include <malloc.h>
diff --git a/Python/initconfig.c b/Python/initconfig.c
index ba6d19db20048..b0d54b0472fb0 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -10,7 +10,9 @@
 #include "pycore_pystate.h"       // _PyThreadState_GET()
 
 #include "osdefs.h"               // DELIM
+
 #include <locale.h>               // setlocale()
+#include <stdlib.h>               // getenv()
 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
 #  ifdef HAVE_IO_H
 #    include <io.h>
diff --git a/Python/preconfig.c b/Python/preconfig.c
index d59273159a671..afa16cccf32e9 100644
--- a/Python/preconfig.c
+++ b/Python/preconfig.c
@@ -4,7 +4,9 @@
 #include "pycore_initconfig.h"    // _PyArgv
 #include "pycore_pymem.h"         // _PyMem_GetAllocatorName()
 #include "pycore_runtime.h"       // _PyRuntime_Initialize()
+
 #include <locale.h>               // setlocale()
+#include <stdlib.h>               // getenv()
 
 
 /* Forward declarations */
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index c67a9b7fbfa2d..c5a209abae61a 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -16,6 +16,7 @@
 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads()
 
 #include <locale.h>               // setlocale()
+#include <stdlib.h>               // getenv()
 
 #if defined(__APPLE__)
 #include <mach-o/loader.h>
diff --git a/Python/pystrhex.c b/Python/pystrhex.c
index f7fe3b6eb85d0..880af44ea0e94 100644
--- a/Python/pystrhex.c
+++ b/Python/pystrhex.c
@@ -1,8 +1,8 @@
 /* Format bytes as hexadecimal */
 
 #include "Python.h"
-#include "pycore_strhex.h"       // _Py_strhex_with_sep()
-
+#include "pycore_strhex.h"        // _Py_strhex_with_sep()
+#include <stdlib.h>               // abs()
 
 static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
                                  const PyObject* sep, int bytes_per_sep_group,



More information about the Python-checkins mailing list