Skip to content

Commit f2f1abe

Browse files
committed
gh-108765: Python.h no longer includes <unistd.h>
1 parent 4ba1809 commit f2f1abe

16 files changed

+55
-42
lines changed

Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,11 @@ Porting to Python 3.13
921921
also the ``HAVE_IEEEFP_H`` macro.
922922
(Contributed by Victor Stinner in :gh:`108765`.)
923923

924+
* ``Python.h`` no longer includes the ``<unistd.h>`` standard header file. If
925+
needed, it should now be included explicitly. For example, it provides the
926+
functions: ``close()``, ``getpagesize()``, ``getpid()`` and ``sysconf()``.
927+
(Contributed by Victor Stinner in :gh:`108765`.)
928+
924929
Deprecated
925930
----------
926931

Include/Python.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
#ifdef HAVE_STDDEF_H
2727
# include <stddef.h> // size_t
2828
#endif
29-
#ifndef MS_WINDOWS
30-
# include <unistd.h> // sysconf()
29+
#ifdef HAVE_SYS_TYPES_H
30+
# include <sys/types.h> // ssize_t
3131
#endif
3232

33-
// errno.h, stdio.h, stdlib.h and string.h headers are no longer used by Python
34-
// headers, but kept for backward compatibility (no introduce new compiler
35-
// warnings). They are not included by the limited C API version 3.11 and
36-
// above.
33+
// errno.h, stdio.h, stdlib.h and string.h headers are no longer used by
34+
// Python, but kept for backward compatibility (avoid compiler warnings).
35+
// They are no longer included by limited C API version 3.11 and newer.
3736
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
3837
# include <errno.h> // errno
3938
# include <stdio.h> // FILE*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``Python.h`` no longer includes the ``<unistd.h>`` standard header file. If
2+
needed, it should now be included explicitly. For example, it provides the
3+
functions: ``close()``, ``getpagesize()``, ``getpid()`` and ``sysconf()``.
4+
Patch by Victor Stinner.

Modules/_ctypes/malloc_closure.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#ifndef Py_BUILD_CORE_BUILTIN
22
# define Py_BUILD_CORE_MODULE 1
33
#endif
4+
45
#include <Python.h>
56
#include <ffi.h>
67
#ifdef MS_WIN32
7-
#include <windows.h>
8+
# include <windows.h>
89
#else
9-
#include <sys/mman.h>
10-
#include <unistd.h>
11-
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
12-
# define MAP_ANONYMOUS MAP_ANON
13-
# endif
10+
# include <sys/mman.h>
11+
# include <unistd.h> // sysconf()
12+
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
13+
# define MAP_ANONYMOUS MAP_ANON
14+
# endif
1415
#endif
1516
#include "ctypes.h"
1617

Modules/_posixsubprocess.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@
88
#include "pycore_pystate.h"
99
#include "pycore_signal.h" // _Py_RestoreSignals()
1010
#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
11-
# define _GNU_SOURCE
11+
# define _GNU_SOURCE
1212
#endif
13-
#include <unistd.h>
14-
#include <fcntl.h>
13+
#include <unistd.h> // close()
14+
#include <fcntl.h> // fcntl()
1515
#ifdef HAVE_SYS_TYPES_H
16-
#include <sys/types.h>
16+
# include <sys/types.h>
1717
#endif
1818
#if defined(HAVE_SYS_STAT_H)
19-
#include <sys/stat.h>
19+
# include <sys/stat.h> // stat()
2020
#endif
2121
#ifdef HAVE_SYS_SYSCALL_H
22-
#include <sys/syscall.h>
22+
# include <sys/syscall.h>
2323
#endif
2424
#if defined(HAVE_SYS_RESOURCE_H)
25-
#include <sys/resource.h>
25+
# include <sys/resource.h>
2626
#endif
2727
#ifdef HAVE_DIRENT_H
28-
#include <dirent.h>
28+
# include <dirent.h> // opendir()
29+
#endif
30+
#if defined(HAVE_SETGROUPS)
31+
# include <grp.h> // setgroups()
2932
#endif
30-
#ifdef HAVE_GRP_H
31-
#include <grp.h>
32-
#endif /* HAVE_GRP_H */
3333

3434
#include "posixmodule.h"
3535

Modules/_testcapimodule.c

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <float.h> // FLT_MAX
2525
#include <signal.h>
2626
#include <stddef.h> // offsetof()
27-
#ifndef MS_WINDOWS
28-
# include <unistd.h>
29-
#endif
3027

3128
#ifdef HAVE_SYS_WAIT_H
3229
# include <sys/wait.h> // W_STOPCODE

Modules/grpmodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "Python.h"
55
#include "posixmodule.h"
66

7-
#include <grp.h>
7+
#include <grp.h> // getgrgid_r()
8+
#include <unistd.h> // sysconf()
89

910
#include "clinic/grpmodule.c.h"
1011
/*[clinic input]

Modules/mmapmodule.c

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include "pycore_fileutils.h" // _Py_stat_struct
2929

3030
#include <stddef.h> // offsetof()
31+
#ifndef MS_WINDOWS
32+
# include <unistd.h> // close()
33+
#endif
3134

3235
// to support MS_WINDOWS_SYSTEM OpenFileMappingA / CreateFileMappingA
3336
// need to be replaced with OpenFileMappingW / CreateFileMappingW

Modules/posixmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ corresponding Unix manual entries for more information on calls.");
286286
#endif
287287

288288
#ifdef HAVE_COPY_FILE_RANGE
289-
# include <unistd.h>
289+
# include <unistd.h> // copy_file_range()
290290
#endif
291291

292292
#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)

Modules/pwdmodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "Python.h"
55
#include "posixmodule.h"
66

7-
#include <pwd.h>
7+
#include <pwd.h> // getpwuid()
8+
#include <unistd.h> // sysconf()
89

910
#include "clinic/pwdmodule.c.h"
1011
/*[clinic input]

Modules/resource.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
21
#include "Python.h"
3-
#include <sys/resource.h>
2+
#include <errno.h> // errno
3+
#include <string.h>
4+
#include <sys/resource.h> // getrusage()
45
#ifdef HAVE_SYS_TIME_H
5-
#include <sys/time.h>
6+
# include <sys/time.h>
67
#endif
78
#include <time.h>
8-
#include <string.h>
9-
#include <errno.h>
10-
#include <unistd.h>
9+
#include <unistd.h> // getpagesize()
1110

1211
/* On some systems, these aren't in any header file.
1312
On others they are, with inconsistent prototypes.

Modules/selectmodule.c

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "pycore_time.h" // _PyTime_t
1818

1919
#include <stddef.h> // offsetof()
20+
#ifndef MS_WINDOWS
21+
# include <unistd.h> // close()
22+
#endif
2023

2124
#ifdef HAVE_SYS_DEVPOLL_H
2225
#include <sys/resource.h>

Modules/socketmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
269269
#ifdef HAVE_NETDB_H
270270
# include <netdb.h>
271271
#endif
272-
# include <unistd.h>
272+
#include <unistd.h> // close()
273273

274274
/* Headers needed for inet_ntoa() and inet_addr() */
275275
# include <arpa/inet.h>

Programs/_freeze_module.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <sys/types.h>
2020
#include <sys/stat.h>
2121
#ifndef MS_WINDOWS
22-
#include <unistd.h>
22+
# include <unistd.h>
2323
#endif
2424

2525
uint32_t _Py_next_func_version = 1;

Python/dup2.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* Return fd2 if all went well; return BADEXIT otherwise.
1212
*/
1313

14-
#include <errno.h>
15-
#include <fcntl.h>
16-
#include <unistd.h>
14+
#include <errno.h> // errno
15+
#include <fcntl.h> // fcntl()
16+
#include <unistd.h> // close()
1717

1818
#define BADEXIT -1
1919

Python/perf_trampoline.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ any DWARF information available for them).
140140
#include <fcntl.h>
141141
#include <stdio.h>
142142
#include <stdlib.h>
143-
#include <sys/mman.h>
143+
#include <sys/mman.h> // mmap()
144144
#include <sys/types.h>
145-
#include <unistd.h>
145+
#include <unistd.h> // sysconf()
146146

147147
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
148148
#define PY_HAVE_INVALIDATE_ICACHE

0 commit comments

Comments
 (0)