Skip to content

Commit 85d4c17

Browse files
committed
gh-110014: Include explicitly <unistd.h> header
* Remove unused <locale.h> include. * Remove <fcntl.h> include in traceback.h. * Remove redundant include: <assert.h> and <stddef.h> are already included by "Python.h". * Remove <object.h> include in faulthandler.c. * 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 85d4c17

File tree

17 files changed

+92
-55
lines changed

17 files changed

+92
-55
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: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
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+
#include <unistd.h> // lseek()
910
#ifdef HAVE_SYS_TYPES_H
10-
#include <sys/types.h>
11+
# include <sys/types.h>
1112
#endif
1213
#ifdef HAVE_SYS_STAT_H
13-
#include <sys/stat.h>
14+
# include <sys/stat.h>
1415
#endif
1516
#ifdef HAVE_IO_H
16-
#include <io.h>
17+
# include <io.h>
1718
#endif
1819
#ifdef HAVE_FCNTL_H
19-
#include <fcntl.h>
20+
# include <fcntl.h> // open()
2021
#endif
21-
#include <stddef.h> /* For offsetof */
22+
2223
#include "_iomodule.h"
2324

2425
/*
@@ -35,22 +36,23 @@
3536
*/
3637

3738
#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>
39+
// can simulate truncate with Win32 API functions; see file_truncate
40+
# define HAVE_FTRUNCATE
41+
# ifndef WIN32_LEAN_AND_MEAN
42+
# define WIN32_LEAN_AND_MEAN
43+
# endif
44+
# include <windows.h>
4445
#endif
4546

4647
#if BUFSIZ < (8*1024)
47-
#define SMALLCHUNK (8*1024)
48+
# define SMALLCHUNK (8*1024)
4849
#elif (BUFSIZ >= (2 << 25))
49-
#error "unreasonable BUFSIZ > 64 MiB defined"
50+
# error "unreasonable BUFSIZ > 64 MiB defined"
5051
#else
51-
#define SMALLCHUNK BUFSIZ
52+
# define SMALLCHUNK BUFSIZ
5253
#endif
5354

55+
5456
/*[clinic input]
5557
module _io
5658
class _io.FileIO "fileio *" "clinic_state()->PyFileIO_Type"

Modules/_randommodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@
7474
#include "pycore_long.h" // _PyLong_AsByteArray()
7575
#include "pycore_moduleobject.h" // _PyModule_GetState()
7676
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
77+
78+
#include <unistd.h> // getpid()
7779
#ifdef HAVE_PROCESS_H
7880
# include <process.h> // getpid()
7981
#endif
80-
8182
#ifdef MS_WINDOWS
82-
# include <windows.h>
83+
# include <windows.h> // GetCurrentProcessId()
8384
#endif
8485

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

Modules/faulthandler.c

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

9-
#include <object.h>
10-
#include <signal.h>
9+
#include <unistd.h> // _exit()
10+
#include <signal.h> // sigaction()
1111
#include <stdlib.h> // abort()
1212
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
1313
# include <pthread.h>
@@ -16,14 +16,15 @@
1616
# include <windows.h>
1717
#endif
1818
#ifdef HAVE_SYS_RESOURCE_H
19-
# include <sys/resource.h>
19+
# include <sys/resource.h> // setrlimit()
2020
#endif
2121

2222
#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H) && defined(HAVE_SYS_AUXV_H)
2323
# include <linux/auxvec.h> // AT_MINSIGSTKSZ
2424
# include <sys/auxv.h> // getauxval()
2525
#endif
2626

27+
2728
/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
2829
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
2930

Modules/posixmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2525
#include "pycore_signal.h" // Py_NSIG
2626

27+
#include <unistd.h> // symlink()
2728
#ifdef MS_WINDOWS
2829
# include <windows.h>
2930
# if !defined(MS_WINDOWS_GAMES) || defined(MS_WINDOWS_DESKTOP)

Objects/fileobject.c

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

7+
#include <unistd.h> // isatty()
8+
79
#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)
10+
/* clang MemorySanitizer doesn't yet understand getc_unlocked. */
11+
# define GETC(f) getc_unlocked(f)
12+
# define FLOCKFILE(f) flockfile(f)
13+
# define FUNLOCKFILE(f) funlockfile(f)
1214
#else
13-
#define GETC(f) getc(f)
14-
#define FLOCKFILE(f)
15-
#define FUNLOCKFILE(f)
15+
# define GETC(f) getc(f)
16+
# define FLOCKFILE(f)
17+
# define FUNLOCKFILE(f)
1618
#endif
1719

1820
/* Newline flags */

Parser/myreadline.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
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+
#include <unistd.h> // isatty()
23+
2224

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

Parser/tokenizer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
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+
#include <unistd.h> // read()
1111

1212
/* Alternate tab spacing */
1313
#define ALTTABSIZE 1

0 commit comments

Comments
 (0)