From 7513994c927857679544449392744be308d36586 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 30 Sep 2023 22:06:45 +0200 Subject: gh-110014: Include explicitly header (#110155) * Remove unused includes. * Remove unused include in traceback.h. * Remove redundant and includes. They are already included by "Python.h". * Remove include in faulthandler.c. Python.h already includes it. * Add missing in pycore_pythread.h if HAVE_PTHREAD_STUBS is defined. * Fix also warnings in pthread_stubs.h: don't redefine macros if they are already defined, like the __NEED_pthread_t macro. --- Doc/whatsnew/3.13.rst | 3 ++- Include/cpython/pthread_stubs.h | 30 +++++++++++++++++++++++------- Include/internal/pycore_pythread.h | 3 ++- Modules/_io/fileio.c | 34 +++++++++++++++++++--------------- Modules/_randommodule.c | 7 +++++-- Modules/faulthandler.c | 9 ++++++--- Modules/posixmodule.c | 9 ++++----- Objects/fileobject.c | 18 +++++++++++------- Parser/myreadline.c | 6 +++++- Parser/tokenizer.c | 8 +++++--- Python/bltinmodule.c | 5 +++++ Python/bootstrap_hash.c | 14 ++++++++++---- Python/fileutils.c | 13 ++++++++----- Python/frozenmain.c | 6 +++++- Python/pylifecycle.c | 3 +++ Python/sysmodule.c | 4 +++- Python/traceback.c | 7 ++++--- 17 files changed, 120 insertions(+), 59 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index d6188e6..56618b9 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -988,7 +988,8 @@ Porting to Python 3.13 * ``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()``. + functions: ``read()``, ``write()``, ``close()``, ``isatty()``, ``lseek()``, + ``getpid()``, ``getcwd()``, ``sysconf()`` and ``getpagesize()``. As a consequence, ``_POSIX_SEMAPHORES`` and ``_POSIX_THREADS`` macros are no longer defined by ``Python.h``. The ``HAVE_UNISTD_H`` and ``HAVE_PTHREAD_H`` macros defined by ``Python.h`` can be used to decide if ```` and diff --git a/Include/cpython/pthread_stubs.h b/Include/cpython/pthread_stubs.h index d95ee03..5246968 100644 --- a/Include/cpython/pthread_stubs.h +++ b/Include/cpython/pthread_stubs.h @@ -21,13 +21,29 @@ #ifdef __wasi__ // WASI's bits/alltypes.h provides type definitions when __NEED_ is set. // The header file can be included multiple times. -# define __NEED_pthread_cond_t 1 -# define __NEED_pthread_condattr_t 1 -# define __NEED_pthread_mutex_t 1 -# define __NEED_pthread_mutexattr_t 1 -# define __NEED_pthread_key_t 1 -# define __NEED_pthread_t 1 -# define __NEED_pthread_attr_t 1 +// +// may also define these macros. +# ifndef __NEED_pthread_cond_t +# define __NEED_pthread_cond_t 1 +# endif +# ifndef __NEED_pthread_condattr_t +# define __NEED_pthread_condattr_t 1 +# endif +# ifndef __NEED_pthread_mutex_t +# define __NEED_pthread_mutex_t 1 +# endif +# ifndef __NEED_pthread_mutexattr_t +# define __NEED_pthread_mutexattr_t 1 +# endif +# ifndef __NEED_pthread_key_t +# define __NEED_pthread_key_t 1 +# endif +# ifndef __NEED_pthread_t +# define __NEED_pthread_t 1 +# endif +# ifndef __NEED_pthread_attr_t +# define __NEED_pthread_attr_t 1 +# endif # include #else typedef struct { void *__x; } pthread_cond_t; diff --git a/Include/internal/pycore_pythread.h b/Include/internal/pycore_pythread.h index 98019c5..f679c1b 100644 --- a/Include/internal/pycore_pythread.h +++ b/Include/internal/pycore_pythread.h @@ -8,7 +8,6 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif - // Get _POSIX_THREADS and _POSIX_SEMAPHORES macros if available #if (defined(HAVE_UNISTD_H) && !defined(_POSIX_THREADS) \ && !defined(_POSIX_SEMAPHORES)) @@ -44,6 +43,8 @@ extern "C" { #if defined(HAVE_PTHREAD_STUBS) +#include // bool + // pthread_key struct py_stub_tls_entry { bool in_use; diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index fb41670..8a73ea0 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -5,20 +5,23 @@ #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() -#include +#include // bool +#ifdef HAVE_UNISTD_H +# include // lseek() +#endif #ifdef HAVE_SYS_TYPES_H -#include +# include #endif #ifdef HAVE_SYS_STAT_H -#include +# include #endif #ifdef HAVE_IO_H -#include +# include #endif #ifdef HAVE_FCNTL_H -#include +# include // open() #endif -#include /* For offsetof */ + #include "_iomodule.h" /* @@ -35,22 +38,23 @@ */ #ifdef MS_WINDOWS -/* can simulate truncate with Win32 API functions; see file_truncate */ -#define HAVE_FTRUNCATE -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include + // can simulate truncate with Win32 API functions; see file_truncate +# define HAVE_FTRUNCATE +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include #endif #if BUFSIZ < (8*1024) -#define SMALLCHUNK (8*1024) +# define SMALLCHUNK (8*1024) #elif (BUFSIZ >= (2 << 25)) -#error "unreasonable BUFSIZ > 64 MiB defined" +# error "unreasonable BUFSIZ > 64 MiB defined" #else -#define SMALLCHUNK BUFSIZ +# define SMALLCHUNK BUFSIZ #endif + /*[clinic input] module _io class _io.FileIO "fileio *" "clinic_state()->PyFileIO_Type" diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 18811d0..d41093c 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -74,12 +74,15 @@ #include "pycore_long.h" // _PyLong_AsByteArray() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pylifecycle.h" // _PyOS_URandomNonblock() + +#ifdef HAVE_UNISTD_H +# include // getpid() +#endif #ifdef HAVE_PROCESS_H # include // getpid() #endif - #ifdef MS_WINDOWS -# include +# include // GetCurrentProcessId() #endif /* Period parameters -- These are all magic. Don't change. */ diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 4b6bf68..a2e3c23 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -6,8 +6,10 @@ #include "pycore_sysmodule.h" // _PySys_GetAttr() #include "pycore_traceback.h" // _Py_DumpTracebackThreads -#include -#include +#ifdef HAVE_UNISTD_H +# include // _exit() +#endif +#include // sigaction() #include // abort() #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H) # include @@ -16,7 +18,7 @@ # include #endif #ifdef HAVE_SYS_RESOURCE_H -# include +# include // setrlimit() #endif #if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H) && defined(HAVE_SYS_AUXV_H) @@ -24,6 +26,7 @@ # include // getauxval() #endif + /* Allocate at maximum 100 MiB of the stack to raise the stack overflow */ #define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d7d3e36..0b25209 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -24,6 +24,10 @@ #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_signal.h" // Py_NSIG +#ifdef HAVE_UNISTD_H +# include // symlink() +#endif + #ifdef MS_WINDOWS # include # if !defined(MS_WINDOWS_GAMES) || defined(MS_WINDOWS_DESKTOP) @@ -37,7 +41,6 @@ # endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */ #endif - #ifndef MS_WINDOWS # include "posixmodule.h" #else @@ -285,10 +288,6 @@ corresponding Unix manual entries for more information on calls."); # include #endif -#ifdef HAVE_COPY_FILE_RANGE -# include // copy_file_range() -#endif - #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) # undef HAVE_SCHED_SETAFFINITY #endif diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 066172b..5522eba 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -4,15 +4,19 @@ #include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_runtime.h" // _PyRuntime +#ifdef HAVE_UNISTD_H +# include // isatty() +#endif + #if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER) -/* clang MemorySanitizer doesn't yet understand getc_unlocked. */ -#define GETC(f) getc_unlocked(f) -#define FLOCKFILE(f) flockfile(f) -#define FUNLOCKFILE(f) funlockfile(f) + /* clang MemorySanitizer doesn't yet understand getc_unlocked. */ +# define GETC(f) getc_unlocked(f) +# define FLOCKFILE(f) flockfile(f) +# define FUNLOCKFILE(f) funlockfile(f) #else -#define GETC(f) getc(f) -#define FLOCKFILE(f) -#define FUNLOCKFILE(f) +# define GETC(f) getc(f) +# define FLOCKFILE(f) +# define FUNLOCKFILE(f) #endif /* Newline flags */ diff --git a/Parser/myreadline.c b/Parser/myreadline.c index 8153873..719a178 100644 --- a/Parser/myreadline.c +++ b/Parser/myreadline.c @@ -14,11 +14,15 @@ #include "pycore_pystate.h" // _PyThreadState_GET() #ifdef MS_WINDOWS # ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN # endif # include "windows.h" #endif /* MS_WINDOWS */ +#ifdef HAVE_UNISTD_H +# include // isatty() +#endif + // Export the symbol since it's used by the readline shared extension PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState; diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 46b7159..41d0d16 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -4,10 +4,12 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallNoArgs() -#include +#include "tokenizer.h" // struct tok_state +#include "errcode.h" // E_OK -#include "tokenizer.h" -#include "errcode.h" +#ifdef HAVE_UNISTD_H +# include // read() +#endif /* Alternate tab spacing */ #define ALTTABSIZE 1 diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 69056bf..c373585 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -17,6 +17,11 @@ #include "clinic/bltinmodule.c.h" +#ifdef HAVE_UNISTD_H +# include // isatty() +#endif + + static PyObject* update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs) { diff --git a/Python/bootstrap_hash.c b/Python/bootstrap_hash.c index ef693e5..86a1691 100644 --- a/Python/bootstrap_hash.c +++ b/Python/bootstrap_hash.c @@ -4,22 +4,28 @@ #include "pycore_pylifecycle.h" // _PyOS_URandomNonblock() #include "pycore_runtime.h" // _PyRuntime +#undef HAVE_GETRANDOM +#undef HAVE_GETENTROPY + +#ifdef HAVE_UNISTD_H +# include // close() +#endif #ifdef MS_WINDOWS # include # include #else -# include +# include // O_RDONLY # ifdef HAVE_SYS_STAT_H # include # endif # ifdef HAVE_LINUX_RANDOM_H -# include +# include // GRND_NONBLOCK # endif # if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY)) -# include +# include // getrandom() # endif # if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL) -# include +# include // SYS_getrandom # endif #endif diff --git a/Python/fileutils.c b/Python/fileutils.c index 9bc1de2..17a4ae5 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -2,8 +2,11 @@ #include "pycore_fileutils.h" // fileutils definitions #include "pycore_runtime.h" // _PyRuntime #include "osdefs.h" // SEP -#include + #include // mbstowcs() +#ifdef HAVE_UNISTD_H +# include // getcwd() +#endif #ifdef MS_WINDOWS # include @@ -19,7 +22,7 @@ extern int winerror_to_errno(int); #endif #ifdef HAVE_LANGINFO_H -#include +# include // nl_langinfo(CODESET) #endif #ifdef HAVE_SYS_IOCTL_H @@ -27,12 +30,12 @@ extern int winerror_to_errno(int); #endif #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION -#include +# include // iconv_open() #endif #ifdef HAVE_FCNTL_H -#include -#endif /* HAVE_FCNTL_H */ +# include // fcntl(F_GETFD) +#endif #ifdef O_CLOEXEC /* Does open() support the O_CLOEXEC flag? Possible values: diff --git a/Python/frozenmain.c b/Python/frozenmain.c index 767f980..3ce9476 100644 --- a/Python/frozenmain.c +++ b/Python/frozenmain.c @@ -3,7 +3,11 @@ #include "Python.h" #include "pycore_pystate.h" // _Py_GetConfig() #include "pycore_runtime.h" // _PyRuntime_Initialize() -#include + +#ifdef HAVE_UNISTD_H +# include // isatty() +#endif + #ifdef MS_WINDOWS extern void PyWinFreeze_ExeInit(void); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 23f66ec..f3ed77e 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -37,6 +37,9 @@ #include // setlocale() #include // getenv() +#ifdef HAVE_UNISTD_H +# include // isatty() +#endif #if defined(__APPLE__) # include diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 7ba7be1..b003017 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -40,7 +40,9 @@ Data members: #include "osdefs.h" // DELIM #include "stdlib_module_names.h" // _Py_stdlib_module_names -#include +#ifdef HAVE_UNISTD_H +# include // getpid() +#endif #ifdef MS_WINDOWS # define WIN32_LEAN_AND_MEAN diff --git a/Python/traceback.c b/Python/traceback.c index 7e791d0..5de1bff 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -20,13 +20,14 @@ #include "frameobject.h" // PyFrame_New() #include "osdefs.h" // SEP -#ifdef HAVE_FCNTL_H -# include +#ifdef HAVE_UNISTD_H +# include // lseek() #endif -#define OFF(x) offsetof(PyTracebackObject, x) +#define OFF(x) offsetof(PyTracebackObject, x) #define PUTS(fd, str) (void)_Py_write_noraise(fd, str, (int)strlen(str)) + #define MAX_STRING_LENGTH 500 #define MAX_FRAME_DEPTH 100 #define MAX_NTHREADS 100 -- cgit v0.12