]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-110014: Include explicitly <unistd.h> header (#110155)
authorVictor Stinner <vstinner@python.org>
Sat, 30 Sep 2023 20:06:45 +0000 (22:06 +0200)
committerGitHub <noreply@github.com>
Sat, 30 Sep 2023 20:06:45 +0000 (20:06 +0000)
* Remove unused <locale.h> includes.
* Remove unused <fcntl.h> include in traceback.h.
* Remove redundant <assert.h> and <stddef.h> includes. They  are already
  included by "Python.h".
* Remove <object.h> include in faulthandler.c. Python.h already includes it.
* Add missing <stdbool.h> 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.

17 files changed:
Doc/whatsnew/3.13.rst
Include/cpython/pthread_stubs.h
Include/internal/pycore_pythread.h
Modules/_io/fileio.c
Modules/_randommodule.c
Modules/faulthandler.c
Modules/posixmodule.c
Objects/fileobject.c
Parser/myreadline.c
Parser/tokenizer.c
Python/bltinmodule.c
Python/bootstrap_hash.c
Python/fileutils.c
Python/frozenmain.c
Python/pylifecycle.c
Python/sysmodule.c
Python/traceback.c

index d6188e63dd23e64a125c21bd92acef4490c31e09..56618b9af16b95b4ef84b6851e3edc654a3deda0 100644 (file)
@@ -988,7 +988,8 @@ Porting to Python 3.13
 
 * ``Python.h`` no longer includes the ``<unistd.h>`` 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 ``<unistd.h>`` and
index d95ee03d8308ce47a63c8f424bd71b0316b7f889..5246968ea05476d50099d4972fca12d8eaef1150 100644 (file)
 #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
+//
+// <sys/types.h> 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 <bits/alltypes.h>
 #else
 typedef struct { void *__x; } pthread_cond_t;
index 98019c586bc5e23794b754305d3af7db117a725b..f679c1bdb7549999fd5f7c7c56e666a0a5727882 100644 (file)
@@ -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 <stdbool.h>              // bool
+
 // pthread_key
 struct py_stub_tls_entry {
     bool in_use;
index fb416700e2252310fb8d146718eb2adb1bc2abfc..8a73ea0365b7a30fa6c9e467fba59707152b82a4 100644 (file)
@@ -5,20 +5,23 @@
 #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
 #include "pycore_pyerrors.h"      // _PyErr_ChainExceptions1()
 
-#include <stdbool.h>
+#include <stdbool.h>              // bool
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // lseek()
+#endif
 #ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
+#  include <sys/types.h>
 #endif
 #ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
+#  include <sys/stat.h>
 #endif
 #ifdef HAVE_IO_H
-#include <io.h>
+#  include <io.h>
 #endif
 #ifdef HAVE_FCNTL_H
-#include <fcntl.h>
+#  include <fcntl.h>              // open()
 #endif
-#include <stddef.h> /* For offsetof */
+
 #include "_iomodule.h"
 
 /*
  */
 
 #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 <windows.h>
+   // 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 <windows.h>
 #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"
index 18811d03adb4510485fe6c5a50227e0f95a462f8..d41093c88064767e9fd2c3088be08b82b41a2200 100644 (file)
 #include "pycore_long.h"          // _PyLong_AsByteArray()
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "pycore_pylifecycle.h"   // _PyOS_URandomNonblock()
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // getpid()
+#endif
 #ifdef HAVE_PROCESS_H
 #  include <process.h>            // getpid()
 #endif
-
 #ifdef MS_WINDOWS
-#  include <windows.h>
+#  include <windows.h>            // GetCurrentProcessId()
 #endif
 
 /* Period parameters -- These are all magic.  Don't change. */
index 4b6bf68be07202f7ca020293ef478ed2fd028a46..a2e3c2300b3ce8580e82caa37f6ce6aef705321d 100644 (file)
@@ -6,8 +6,10 @@
 #include "pycore_sysmodule.h"     // _PySys_GetAttr()
 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads
 
-#include <object.h>
-#include <signal.h>
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // _exit()
+#endif
+#include <signal.h>               // sigaction()
 #include <stdlib.h>               // abort()
 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
 #  include <pthread.h>
@@ -16,7 +18,7 @@
 #  include <windows.h>
 #endif
 #ifdef HAVE_SYS_RESOURCE_H
-#  include <sys/resource.h>
+#  include <sys/resource.h>       // setrlimit()
 #endif
 
 #if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H) && defined(HAVE_SYS_AUXV_H)
@@ -24,6 +26,7 @@
 #  include <sys/auxv.h>           // getauxval()
 #endif
 
+
 /* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
 #define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
 
index d7d3e365d2c553faf0649ce5382b56f80b1269dd..0b252092573e5c5baf961b9449b104ecece91d73 100644 (file)
 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
 #include "pycore_signal.h"        // Py_NSIG
 
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // symlink()
+#endif
+
 #ifdef MS_WINDOWS
 #  include <windows.h>
 #  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 <sched.h>
 #endif
 
-#ifdef HAVE_COPY_FILE_RANGE
-#  include <unistd.h>             // copy_file_range()
-#endif
-
 #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
 #  undef HAVE_SCHED_SETAFFINITY
 #endif
index 066172baf9f0273428142f2987673a2359619a59..5522eba34eace9b024c01e1f3d8c7c5d90ba4391 100644 (file)
@@ -4,15 +4,19 @@
 #include "pycore_call.h"          // _PyObject_CallNoArgs()
 #include "pycore_runtime.h"       // _PyRuntime
 
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // 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 */
index 815387388218c6d4b7f5b5152a29d9620c7f9e61..719a178f244a2810d9abd47a41dbb843e8a3e17c 100644 (file)
 #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 <unistd.h>             // isatty()
+#endif
+
 
 // Export the symbol since it's used by the readline shared extension
 PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;
index 46b7159ff0516b548e89ed0cd0e5e6e80221bc9f..41d0d16a471dd60cc88df8bc7a1626d2c712c9dd 100644 (file)
@@ -4,10 +4,12 @@
 #include "Python.h"
 #include "pycore_call.h"          // _PyObject_CallNoArgs()
 
-#include <assert.h>
+#include "tokenizer.h"            // struct tok_state
+#include "errcode.h"              // E_OK
 
-#include "tokenizer.h"
-#include "errcode.h"
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // read()
+#endif
 
 /* Alternate tab spacing */
 #define ALTTABSIZE 1
index 69056bf23f4058864a0e218762355888170b8a67..c373585c0986cea106f00f6f97c3c6d0fcbde187 100644 (file)
 
 #include "clinic/bltinmodule.c.h"
 
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // isatty()
+#endif
+
+
 static PyObject*
 update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
 {
index ef693e5df1fcc415ed85c6a229f8934a51a6f6af..86a16916304cab07eb06ec745e9dbbaed9cde063 100644 (file)
@@ -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 <unistd.h>             // close()
+#endif
 #ifdef MS_WINDOWS
 #  include <windows.h>
 #  include <bcrypt.h>
 #else
-#  include <fcntl.h>
+#  include <fcntl.h>              // O_RDONLY
 #  ifdef HAVE_SYS_STAT_H
 #    include <sys/stat.h>
 #  endif
 #  ifdef HAVE_LINUX_RANDOM_H
-#    include <linux/random.h>
+#    include <linux/random.h>     // GRND_NONBLOCK
 #  endif
 #  if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
-#    include <sys/random.h>
+#    include <sys/random.h>       // getrandom()
 #  endif
 #  if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
-#    include <sys/syscall.h>
+#    include <sys/syscall.h>      // SYS_getrandom
 #  endif
 #endif
 
index 9bc1de2db8400605f3915f6eb8456aabfdb84e1e..17a4ae56ef052813ea3c140dd0e0b8ac8ee766c6 100644 (file)
@@ -2,8 +2,11 @@
 #include "pycore_fileutils.h"     // fileutils definitions
 #include "pycore_runtime.h"       // _PyRuntime
 #include "osdefs.h"               // SEP
-#include <locale.h>
+
 #include <stdlib.h>               // mbstowcs()
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // getcwd()
+#endif
 
 #ifdef MS_WINDOWS
 #  include <malloc.h>
@@ -19,7 +22,7 @@ extern int winerror_to_errno(int);
 #endif
 
 #ifdef HAVE_LANGINFO_H
-#include <langinfo.h>
+#  include <langinfo.h>           // 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 <iconv.h>
+#  include <iconv.h>              // iconv_open()
 #endif
 
 #ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
+#  include <fcntl.h>              // fcntl(F_GETFD)
+#endif
 
 #ifdef O_CLOEXEC
 /* Does open() support the O_CLOEXEC flag? Possible values:
index 767f9804903a9e299e76a2e6a454b376662c3a25..3ce9476c9ad46c661e1ed4b994fb028cc476a00d 100644 (file)
@@ -3,7 +3,11 @@
 #include "Python.h"
 #include "pycore_pystate.h"       // _Py_GetConfig()
 #include "pycore_runtime.h"       // _PyRuntime_Initialize()
-#include <locale.h>
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // isatty()
+#endif
+
 
 #ifdef MS_WINDOWS
 extern void PyWinFreeze_ExeInit(void);
index 23f66ec3601df6e764e8bc927e73d820709dabde..f3ed77e516237ac2ce61ffdb68002c4f65bf05db 100644 (file)
@@ -37,6 +37,9 @@
 
 #include <locale.h>               // setlocale()
 #include <stdlib.h>               // getenv()
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // isatty()
+#endif
 
 #if defined(__APPLE__)
 #  include <mach-o/loader.h>
index 7ba7be10aacb925f49c7a33f69fe3f0506ce3586..b00301765e1890808e579b132156d487aadbd70e 100644 (file)
@@ -40,7 +40,9 @@ Data members:
 #include "osdefs.h"               // DELIM
 #include "stdlib_module_names.h"  // _Py_stdlib_module_names
 
-#include <locale.h>
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // getpid()
+#endif
 
 #ifdef MS_WINDOWS
 #  define WIN32_LEAN_AND_MEAN
index 7e791d0a59bd828312adac791dbfdd0c522a72a1..5de1bff9943c6c642645ebc31042e822568ead88 100644 (file)
 #include "frameobject.h"          // PyFrame_New()
 
 #include "osdefs.h"               // SEP
-#ifdef HAVE_FCNTL_H
-#  include <fcntl.h>
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>             // 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