From 594b00057e667e0d8d4e41748be056cdd829e919 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 2 Sep 2023 16:50:18 +0200 Subject: [PATCH] gh-108765: Python.h no longer includes (#108783) --- Doc/whatsnew/3.13.rst | 5 +++++ Include/Python.h | 11 +++++----- ...-09-01-21-10-29.gh-issue-108765.eeXtYF.rst | 4 ++++ Modules/_ctypes/malloc_closure.c | 13 ++++++----- Modules/_posixsubprocess.c | 22 +++++++++---------- Modules/_testcapimodule.c | 3 --- Modules/grpmodule.c | 3 ++- Modules/mmapmodule.c | 3 +++ Modules/posixmodule.c | 2 +- Modules/pwdmodule.c | 3 ++- Modules/resource.c | 11 +++++----- Modules/selectmodule.c | 3 +++ Modules/socketmodule.c | 2 +- Programs/_freeze_module.c | 2 +- Python/dup2.c | 6 ++--- Python/perf_trampoline.c | 4 ++-- 16 files changed, 55 insertions(+), 42 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2023-09-01-21-10-29.gh-issue-108765.eeXtYF.rst diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index eb3d8323daa..401de11b34e 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -929,6 +929,11 @@ Porting to Python 3.13 also the ``HAVE_IEEEFP_H`` macro. (Contributed by Victor Stinner in :gh:`108765`.) +* ``Python.h`` no longer includes the ```` standard header file. If + needed, it should now be included explicitly. For example, it provides the + functions: ``close()``, ``getpagesize()``, ``getpid()`` and ``sysconf()``. + (Contributed by Victor Stinner in :gh:`108765`.) + Deprecated ---------- diff --git a/Include/Python.h b/Include/Python.h index 002a79dbdc9..4cc72bb23ce 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -26,14 +26,13 @@ #ifdef HAVE_STDDEF_H # include // size_t #endif -#ifndef MS_WINDOWS -# include // sysconf() +#ifdef HAVE_SYS_TYPES_H +# include // ssize_t #endif -// errno.h, stdio.h, stdlib.h and string.h headers are no longer used by Python -// headers, but kept for backward compatibility (no introduce new compiler -// warnings). They are not included by the limited C API version 3.11 and -// above. +// errno.h, stdio.h, stdlib.h and string.h headers are no longer used by +// Python, but kept for backward compatibility (avoid compiler warnings). +// They are no longer included by limited C API version 3.11 and newer. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 # include // errno # include // FILE* diff --git a/Misc/NEWS.d/next/C API/2023-09-01-21-10-29.gh-issue-108765.eeXtYF.rst b/Misc/NEWS.d/next/C API/2023-09-01-21-10-29.gh-issue-108765.eeXtYF.rst new file mode 100644 index 00000000000..ff8f79998fa --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-09-01-21-10-29.gh-issue-108765.eeXtYF.rst @@ -0,0 +1,4 @@ +``Python.h`` no longer includes the ```` standard header file. If +needed, it should now be included explicitly. For example, it provides the +functions: ``close()``, ``getpagesize()``, ``getpid()`` and ``sysconf()``. +Patch by Victor Stinner. diff --git a/Modules/_ctypes/malloc_closure.c b/Modules/_ctypes/malloc_closure.c index 3a859322772..bb4f8f21bd3 100644 --- a/Modules/_ctypes/malloc_closure.c +++ b/Modules/_ctypes/malloc_closure.c @@ -1,16 +1,17 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif + #include #include #ifdef MS_WIN32 -#include +# include #else -#include -#include -# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) -# define MAP_ANONYMOUS MAP_ANON -# endif +# include +# include // sysconf() +# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +# define MAP_ANONYMOUS MAP_ANON +# endif #endif #include "ctypes.h" diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index ac2b0d4f554..ef76d26282e 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -8,28 +8,28 @@ #include "pycore_pystate.h" #include "pycore_signal.h" // _Py_RestoreSignals() #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE) -# define _GNU_SOURCE +# define _GNU_SOURCE #endif -#include -#include +#include // close() +#include // fcntl() #ifdef HAVE_SYS_TYPES_H -#include +# include #endif #if defined(HAVE_SYS_STAT_H) -#include +# include // stat() #endif #ifdef HAVE_SYS_SYSCALL_H -#include +# include #endif #if defined(HAVE_SYS_RESOURCE_H) -#include +# include #endif #ifdef HAVE_DIRENT_H -#include +# include // opendir() +#endif +#if defined(HAVE_SETGROUPS) +# include // setgroups() #endif -#ifdef HAVE_GRP_H -#include -#endif /* HAVE_GRP_H */ #include "posixmodule.h" diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 4fc354ae79b..ab33702cdfd 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -24,9 +24,6 @@ #include // FLT_MAX #include #include // offsetof() -#ifndef MS_WINDOWS -# include -#endif #ifdef HAVE_SYS_WAIT_H # include // W_STOPCODE diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c index f5709296334..20e83de84e8 100644 --- a/Modules/grpmodule.c +++ b/Modules/grpmodule.c @@ -4,7 +4,8 @@ #include "Python.h" #include "posixmodule.h" -#include +#include // getgrgid_r() +#include // sysconf() #include "clinic/grpmodule.c.h" /*[clinic input] diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index c8cd7e59dba..d11200a4042 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -28,6 +28,9 @@ #include "pycore_fileutils.h" // _Py_stat_struct #include // offsetof() +#ifndef MS_WINDOWS +# include // close() +#endif // to support MS_WINDOWS_SYSTEM OpenFileMappingA / CreateFileMappingA // need to be replaced with OpenFileMappingW / CreateFileMappingW diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 761542866d8..6e829b200fa 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -286,7 +286,7 @@ corresponding Unix manual entries for more information on calls."); #endif #ifdef HAVE_COPY_FILE_RANGE -# include +# include // copy_file_range() #endif #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index cc2e2a43893..b7034369c47 100644 --- a/Modules/pwdmodule.c +++ b/Modules/pwdmodule.c @@ -4,7 +4,8 @@ #include "Python.h" #include "posixmodule.h" -#include +#include // getpwuid() +#include // sysconf() #include "clinic/pwdmodule.c.h" /*[clinic input] diff --git a/Modules/resource.c b/Modules/resource.c index 4614f5e98cc..f5d9972d9a8 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -1,13 +1,12 @@ - #include "Python.h" -#include +#include // errno +#include +#include // getrusage() #ifdef HAVE_SYS_TIME_H -#include +# include #endif #include -#include -#include -#include +#include // getpagesize() /* On some systems, these aren't in any header file. On others they are, with inconsistent prototypes. diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 4987cf0f206..c56e682b21e 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -17,6 +17,9 @@ #include "pycore_time.h" // _PyTime_t #include // offsetof() +#ifndef MS_WINDOWS +# include // close() +#endif #ifdef HAVE_SYS_DEVPOLL_H #include diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 2f12c9cedbd..74b1c1c6616 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -269,7 +269,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\ #ifdef HAVE_NETDB_H # include #endif -# include +#include // close() /* Headers needed for inet_ntoa() and inet_addr() */ # include diff --git a/Programs/_freeze_module.c b/Programs/_freeze_module.c index e55f1d56745..f6c46fa629e 100644 --- a/Programs/_freeze_module.c +++ b/Programs/_freeze_module.c @@ -19,7 +19,7 @@ #include #include #ifndef MS_WINDOWS -#include +# include #endif uint32_t _Py_next_func_version = 1; diff --git a/Python/dup2.c b/Python/dup2.c index a1df0492099..936211f27ec 100644 --- a/Python/dup2.c +++ b/Python/dup2.c @@ -11,9 +11,9 @@ * Return fd2 if all went well; return BADEXIT otherwise. */ -#include -#include -#include +#include // errno +#include // fcntl() +#include // close() #define BADEXIT -1 diff --git a/Python/perf_trampoline.c b/Python/perf_trampoline.c index b8885a30397..10675bf9f82 100644 --- a/Python/perf_trampoline.c +++ b/Python/perf_trampoline.c @@ -140,9 +140,9 @@ any DWARF information available for them). #include #include #include -#include +#include // mmap() #include -#include +#include // sysconf() #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) #define PY_HAVE_INVALIDATE_ICACHE