Skip to content

Commit

Permalink
bpo-45434: pyport.h no longer includes <stdlib.h> (GH-28914)
Browse files Browse the repository at this point in the history
Include <stdlib.h> explicitly in C files.

Python.h includes <wchar.h>.
  • Loading branch information
vstinner authored Oct 13, 2021
1 parent db2b6a2 commit aac29af
Show file tree
Hide file tree
Showing 29 changed files with 60 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand Down
2 changes: 0 additions & 2 deletions Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions Modules/_ctypes/_ctypes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <windows.h>
#endif

#include <stdlib.h> // qsort()

#define EXPORT(x) Py_EXPORTED_SYMBOL x

/* some functions handy for testing */
Expand Down
7 changes: 4 additions & 3 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Python.h"
#include "structmember.h" // PyMemberDef

#include <stdlib.h> // free()
#include <string.h>

#include <lzma.h>
Expand Down
2 changes: 2 additions & 0 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
3 changes: 3 additions & 0 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]*/
Expand Down
7 changes: 5 additions & 2 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions Modules/nismodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Python.h"

#include <stdlib.h> // free()
#include <sys/time.h>
#include <sys/types.h>
#include <rpc/rpc.h>
Expand Down
1 change: 1 addition & 0 deletions Modules/ossaudiodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Python.h"
#include "structmember.h" // PyMemberDef

#include <stdlib.h> // getenv()
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
Expand Down
3 changes: 2 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions Modules/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <ctype.h>
#include <float.h>
#include <stdlib.h> // strtol()

/*[clinic input]
class float "PyObject *" "&PyFloat_Type"
Expand Down
3 changes: 2 additions & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "pycore_pymem.h" // _PyTraceMalloc_Config

#include <stdbool.h>
#include <stdlib.h> // malloc()


/* Defined in tracemalloc.c */
Expand Down
1 change: 1 addition & 0 deletions PC/WinMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions Programs/_freeze_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <Python.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h> // putenv()
#include <wchar.h>

/*********************************************************
Expand Down
1 change: 1 addition & 0 deletions Python/dtoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
7 changes: 4 additions & 3 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
1 change: 1 addition & 0 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions Python/preconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions Python/pystrhex.c
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit aac29af

Please sign in to comment.