Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7513994

Browse files
authored
gh-110014: Include explicitly <unistd.h> header (#110155)
* 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.
1 parent 0def8c7 commit 7513994

File tree

17 files changed

+120
-59
lines changed

17 files changed

+120
-59
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,8 @@ Porting to Python 3.13
988988

989989
* ``Python.h`` no longer includes the ``<unistd.h>`` standard header file. If
990990
needed, it should now be included explicitly. For example, it provides the
991-
functions: ``close()``, ``getpagesize()``, ``getpid()`` and ``sysconf()``.
991+
functions: ``read()``, ``write()``, ``close()``, ``isatty()``, ``lseek()``,
992+
``getpid()``, ``getcwd()``, ``sysconf()`` and ``getpagesize()``.
992993
As a consequence, ``_POSIX_SEMAPHORES`` and ``_POSIX_THREADS`` macros are no
993994
longer defined by ``Python.h``. The ``HAVE_UNISTD_H`` and ``HAVE_PTHREAD_H``
994995
macros defined by ``Python.h`` can be used to decide if ``<unistd.h>`` and

Include/cpython/pthread_stubs.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,29 @@
2121
#ifdef __wasi__
2222
// WASI's bits/alltypes.h provides type definitions when __NEED_ is set.
2323
// The header file can be included multiple times.
24-
# define __NEED_pthread_cond_t 1
25-
# define __NEED_pthread_condattr_t 1
26-
# define __NEED_pthread_mutex_t 1
27-
# define __NEED_pthread_mutexattr_t 1
28-
# define __NEED_pthread_key_t 1
29-
# define __NEED_pthread_t 1
30-
# define __NEED_pthread_attr_t 1
24+
//
25+
// <sys/types.h> may also define these macros.
26+
# ifndef __NEED_pthread_cond_t
27+
# define __NEED_pthread_cond_t 1
28+
# endif
29+
# ifndef __NEED_pthread_condattr_t
30+
# define __NEED_pthread_condattr_t 1
31+
# endif
32+
# ifndef __NEED_pthread_mutex_t
33+
# define __NEED_pthread_mutex_t 1
34+
# endif
35+
# ifndef __NEED_pthread_mutexattr_t
36+
# define __NEED_pthread_mutexattr_t 1
37+
# endif
38+
# ifndef __NEED_pthread_key_t
39+
# define __NEED_pthread_key_t 1
40+
# endif
41+
# ifndef __NEED_pthread_t
42+
# define __NEED_pthread_t 1
43+
# endif
44+
# ifndef __NEED_pthread_attr_t
45+
# define __NEED_pthread_attr_t 1
46+
# endif
3147
# include <bits/alltypes.h>
3248
#else
3349
typedef struct { void *__x; } pthread_cond_t;

Include/internal/pycore_pythread.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
1211
// Get _POSIX_THREADS and _POSIX_SEMAPHORES macros if available
1312
#if (defined(HAVE_UNISTD_H) && !defined(_POSIX_THREADS) \
1413
&& !defined(_POSIX_SEMAPHORES))
@@ -44,6 +43,8 @@ extern "C" {
4443

4544

4645
#if defined(HAVE_PTHREAD_STUBS)
46+
#include <stdbool.h> // bool
47+
4748
// pthread_key
4849
struct py_stub_tls_entry {
4950
bool in_use;

Modules/_io/fileio.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
66
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
77

8-
#include <stdbool.h>
8+
#include <stdbool.h> // bool
9+
#ifdef HAVE_UNISTD_H
10+
# include <unistd.h> // lseek()
11+
#endif
912
#ifdef HAVE_SYS_TYPES_H
10-
#include <sys/types.h>
13+
# include <sys/types.h>
1114
#endif
1215
#ifdef HAVE_SYS_STAT_H
13-
#include <sys/stat.h>
16+
# include <sys/stat.h>
1417
#endif
1518
#ifdef HAVE_IO_H
16-
#include <io.h>
19+
# include <io.h>
1720
#endif
1821
#ifdef HAVE_FCNTL_H
19-
#include <fcntl.h>
22+
# include <fcntl.h> // open()
2023
#endif
21-
#include <stddef.h> /* For offsetof */
24+
2225
#include "_iomodule.h"
2326

2427
/*
@@ -35,22 +38,23 @@
3538
*/
3639

3740
#ifdef MS_WINDOWS
38-
/* can simulate truncate with Win32 API functions; see file_truncate */
39-
#define HAVE_FTRUNCATE
40-
#ifndef WIN32_LEAN_AND_MEAN
41-
#define WIN32_LEAN_AND_MEAN
42-
#endif
43-
#include <windows.h>
41+
// can simulate truncate with Win32 API functions; see file_truncate
42+
# define HAVE_FTRUNCATE
43+
# ifndef WIN32_LEAN_AND_MEAN
44+
# define WIN32_LEAN_AND_MEAN
45+
# endif
46+
# include <windows.h>
4447
#endif
4548

4649
#if BUFSIZ < (8*1024)
47-
#define SMALLCHUNK (8*1024)
50+
# define SMALLCHUNK (8*1024)
4851
#elif (BUFSIZ >= (2 << 25))
49-
#error "unreasonable BUFSIZ > 64 MiB defined"
52+
# error "unreasonable BUFSIZ > 64 MiB defined"
5053
#else
51-
#define SMALLCHUNK BUFSIZ
54+
# define SMALLCHUNK BUFSIZ
5255
#endif
5356

57+
5458
/*[clinic input]
5559
module _io
5660
class _io.FileIO "fileio *" "clinic_state()->PyFileIO_Type"

Modules/_randommodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@
7474
#include "pycore_long.h" // _PyLong_AsByteArray()
7575
#include "pycore_moduleobject.h" // _PyModule_GetState()
7676
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
77+
78+
#ifdef HAVE_UNISTD_H
79+
# include <unistd.h> // getpid()
80+
#endif
7781
#ifdef HAVE_PROCESS_H
7882
# include <process.h> // getpid()
7983
#endif
80-
8184
#ifdef MS_WINDOWS
82-
# include <windows.h>
85+
# include <windows.h> // GetCurrentProcessId()
8386
#endif
8487

8588
/* Period parameters -- These are all magic. Don't change. */

Modules/faulthandler.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include "pycore_sysmodule.h" // _PySys_GetAttr()
77
#include "pycore_traceback.h" // _Py_DumpTracebackThreads
88

9-
#include <object.h>
10-
#include <signal.h>
9+
#ifdef HAVE_UNISTD_H
10+
# include <unistd.h> // _exit()
11+
#endif
12+
#include <signal.h> // sigaction()
1113
#include <stdlib.h> // abort()
1214
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
1315
# include <pthread.h>
@@ -16,14 +18,15 @@
1618
# include <windows.h>
1719
#endif
1820
#ifdef HAVE_SYS_RESOURCE_H
19-
# include <sys/resource.h>
21+
# include <sys/resource.h> // setrlimit()
2022
#endif
2123

2224
#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H) && defined(HAVE_SYS_AUXV_H)
2325
# include <linux/auxvec.h> // AT_MINSIGSTKSZ
2426
# include <sys/auxv.h> // getauxval()
2527
#endif
2628

29+
2730
/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
2831
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
2932

Modules/posixmodule.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2525
#include "pycore_signal.h" // Py_NSIG
2626

27+
#ifdef HAVE_UNISTD_H
28+
# include <unistd.h> // symlink()
29+
#endif
30+
2731
#ifdef MS_WINDOWS
2832
# include <windows.h>
2933
# if !defined(MS_WINDOWS_GAMES) || defined(MS_WINDOWS_DESKTOP)
@@ -37,7 +41,6 @@
3741
# endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
3842
#endif
3943

40-
4144
#ifndef MS_WINDOWS
4245
# include "posixmodule.h"
4346
#else
@@ -285,10 +288,6 @@ corresponding Unix manual entries for more information on calls.");
285288
# include <sched.h>
286289
#endif
287290

288-
#ifdef HAVE_COPY_FILE_RANGE
289-
# include <unistd.h> // copy_file_range()
290-
#endif
291-
292291
#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
293292
# undef HAVE_SCHED_SETAFFINITY
294293
#endif

Objects/fileobject.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
#include "pycore_call.h" // _PyObject_CallNoArgs()
55
#include "pycore_runtime.h" // _PyRuntime
66

7+
#ifdef HAVE_UNISTD_H
8+
# include <unistd.h> // isatty()
9+
#endif
10+
711
#if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER)
8-
/* clang MemorySanitizer doesn't yet understand getc_unlocked. */
9-
#define GETC(f) getc_unlocked(f)
10-
#define FLOCKFILE(f) flockfile(f)
11-
#define FUNLOCKFILE(f) funlockfile(f)
12+
/* clang MemorySanitizer doesn't yet understand getc_unlocked. */
13+
# define GETC(f) getc_unlocked(f)
14+
# define FLOCKFILE(f) flockfile(f)
15+
# define FUNLOCKFILE(f) funlockfile(f)
1216
#else
13-
#define GETC(f) getc(f)
14-
#define FLOCKFILE(f)
15-
#define FUNLOCKFILE(f)
17+
# define GETC(f) getc(f)
18+
# define FLOCKFILE(f)
19+
# define FUNLOCKFILE(f)
1620
#endif
1721

1822
/* Newline flags */

Parser/myreadline.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
1515
#ifdef MS_WINDOWS
1616
# ifndef WIN32_LEAN_AND_MEAN
17-
# define WIN32_LEAN_AND_MEAN
17+
# define WIN32_LEAN_AND_MEAN
1818
# endif
1919
# include "windows.h"
2020
#endif /* MS_WINDOWS */
2121

22+
#ifdef HAVE_UNISTD_H
23+
# include <unistd.h> // isatty()
24+
#endif
25+
2226

2327
// Export the symbol since it's used by the readline shared extension
2428
PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;

Parser/tokenizer.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include "Python.h"
55
#include "pycore_call.h" // _PyObject_CallNoArgs()
66

7-
#include <assert.h>
7+
#include "tokenizer.h" // struct tok_state
8+
#include "errcode.h" // E_OK
89

9-
#include "tokenizer.h"
10-
#include "errcode.h"
10+
#ifdef HAVE_UNISTD_H
11+
# include <unistd.h> // read()
12+
#endif
1113

1214
/* Alternate tab spacing */
1315
#define ALTTABSIZE 1

Python/bltinmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
#include "clinic/bltinmodule.c.h"
1919

20+
#ifdef HAVE_UNISTD_H
21+
# include <unistd.h> // isatty()
22+
#endif
23+
24+
2025
static PyObject*
2126
update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
2227
{

Python/bootstrap_hash.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
55
#include "pycore_runtime.h" // _PyRuntime
66

7+
#undef HAVE_GETRANDOM
8+
#undef HAVE_GETENTROPY
9+
10+
#ifdef HAVE_UNISTD_H
11+
# include <unistd.h> // close()
12+
#endif
713
#ifdef MS_WINDOWS
814
# include <windows.h>
915
# include <bcrypt.h>
1016
#else
11-
# include <fcntl.h>
17+
# include <fcntl.h> // O_RDONLY
1218
# ifdef HAVE_SYS_STAT_H
1319
# include <sys/stat.h>
1420
# endif
1521
# ifdef HAVE_LINUX_RANDOM_H
16-
# include <linux/random.h>
22+
# include <linux/random.h> // GRND_NONBLOCK
1723
# endif
1824
# if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
19-
# include <sys/random.h>
25+
# include <sys/random.h> // getrandom()
2026
# endif
2127
# if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
22-
# include <sys/syscall.h>
28+
# include <sys/syscall.h> // SYS_getrandom
2329
# endif
2430
#endif
2531

Python/fileutils.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
#include "pycore_fileutils.h" // fileutils definitions
33
#include "pycore_runtime.h" // _PyRuntime
44
#include "osdefs.h" // SEP
5-
#include <locale.h>
5+
66
#include <stdlib.h> // mbstowcs()
7+
#ifdef HAVE_UNISTD_H
8+
# include <unistd.h> // getcwd()
9+
#endif
710

811
#ifdef MS_WINDOWS
912
# include <malloc.h>
@@ -19,20 +22,20 @@ extern int winerror_to_errno(int);
1922
#endif
2023

2124
#ifdef HAVE_LANGINFO_H
22-
#include <langinfo.h>
25+
# include <langinfo.h> // nl_langinfo(CODESET)
2326
#endif
2427

2528
#ifdef HAVE_SYS_IOCTL_H
2629
#include <sys/ioctl.h>
2730
#endif
2831

2932
#ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
30-
#include <iconv.h>
33+
# include <iconv.h> // iconv_open()
3134
#endif
3235

3336
#ifdef HAVE_FCNTL_H
34-
#include <fcntl.h>
35-
#endif /* HAVE_FCNTL_H */
37+
# include <fcntl.h> // fcntl(F_GETFD)
38+
#endif
3639

3740
#ifdef O_CLOEXEC
3841
/* Does open() support the O_CLOEXEC flag? Possible values:

Python/frozenmain.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
#include "Python.h"
44
#include "pycore_pystate.h" // _Py_GetConfig()
55
#include "pycore_runtime.h" // _PyRuntime_Initialize()
6-
#include <locale.h>
6+
7+
#ifdef HAVE_UNISTD_H
8+
# include <unistd.h> // isatty()
9+
#endif
10+
711

812
#ifdef MS_WINDOWS
913
extern void PyWinFreeze_ExeInit(void);

Python/pylifecycle.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
#include <locale.h> // setlocale()
3939
#include <stdlib.h> // getenv()
40+
#ifdef HAVE_UNISTD_H
41+
# include <unistd.h> // isatty()
42+
#endif
4043

4144
#if defined(__APPLE__)
4245
# include <mach-o/loader.h>

Python/sysmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Data members:
4040
#include "osdefs.h" // DELIM
4141
#include "stdlib_module_names.h" // _Py_stdlib_module_names
4242

43-
#include <locale.h>
43+
#ifdef HAVE_UNISTD_H
44+
# include <unistd.h> // getpid()
45+
#endif
4446

4547
#ifdef MS_WINDOWS
4648
# define WIN32_LEAN_AND_MEAN

Python/traceback.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include "frameobject.h" // PyFrame_New()
2121

2222
#include "osdefs.h" // SEP
23-
#ifdef HAVE_FCNTL_H
24-
# include <fcntl.h>
23+
#ifdef HAVE_UNISTD_H
24+
# include <unistd.h> // lseek()
2525
#endif
2626

27-
#define OFF(x) offsetof(PyTracebackObject, x)
2827

28+
#define OFF(x) offsetof(PyTracebackObject, x)
2929
#define PUTS(fd, str) (void)_Py_write_noraise(fd, str, (int)strlen(str))
30+
3031
#define MAX_STRING_LENGTH 500
3132
#define MAX_FRAME_DEPTH 100
3233
#define MAX_NTHREADS 100

0 commit comments

Comments
 (0)