Skip to content

Commit 45b9e6a

Browse files
authored
gh-108765: Move standard includes to Python.h (#108769)
* Move <ctype.h>, <limits.h> and <stdarg.h> standard includes to Python.h. * Move "pystats.h" include from object.h to Python.h. * Remove redundant "pymem.h" include in objimpl.h and "pyport.h" include in pymem.h; Python.h already includes them earlier. * Remove redundant <wchar.h> include in unicodeobject.h; Python.h already includes it. * Move _SGI_MP_SOURCE define from Python.h to pyport.h. * pycore_condvar.h includes explicitly <unistd.h> for the _POSIX_THREADS macro.
1 parent 0e01fac commit 45b9e6a

File tree

10 files changed

+43
-59
lines changed

10 files changed

+43
-59
lines changed

Include/Python.h

+24-16
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,50 @@
55
#ifndef Py_PYTHON_H
66
#define Py_PYTHON_H
77

8-
// Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" {
8+
// Since this is a "meta-include" file, "#ifdef __cplusplus / extern "C" {"
9+
// is not needed.
10+
911

1012
// Include Python header files
1113
#include "patchlevel.h"
1214
#include "pyconfig.h"
1315
#include "pymacconfig.h"
1416

15-
#if defined(__sgi) && !defined(_SGI_MP_SOURCE)
16-
# define _SGI_MP_SOURCE
17+
18+
// Include standard header files
19+
#include <assert.h> // assert()
20+
#include <ctype.h> // tolower()
21+
#include <inttypes.h> // uintptr_t
22+
#include <limits.h> // INT_MAX
23+
#include <stdarg.h> // va_list
24+
#include <wchar.h> // wchar_t
25+
#ifdef HAVE_STDDEF_H
26+
# include <stddef.h> // size_t
27+
#endif
28+
#ifndef MS_WINDOWS
29+
# include <unistd.h> // sysconf()
1730
#endif
1831

19-
// stdlib.h, stdio.h, errno.h and string.h headers are not used by Python
20-
// headers, but kept for backward compatibility. They are excluded from the
21-
// limited C API of Python 3.11.
32+
// errno.h, stdio.h, stdlib.h and string.h headers are no longer used by Python
33+
// headers, but kept for backward compatibility (no introduce new compiler
34+
// warnings). They are not included by the limited C API version 3.11 and
35+
// above.
2236
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
23-
# include <stdlib.h>
24-
# include <stdio.h> // FILE*
2537
# include <errno.h> // errno
38+
# include <stdio.h> // FILE*
39+
# include <stdlib.h> // getenv()
2640
# include <string.h> // memcpy()
2741
#endif
28-
#ifndef MS_WINDOWS
29-
# include <unistd.h>
30-
#endif
31-
#ifdef HAVE_STDDEF_H
32-
# include <stddef.h> // size_t
33-
#endif
3442

35-
#include <assert.h> // assert()
36-
#include <wchar.h> // wchar_t
3743

44+
// Include Python header files
3845
#include "pyport.h"
3946
#include "pymacro.h"
4047
#include "pymath.h"
4148
#include "pymem.h"
4249
#include "pytypedefs.h"
4350
#include "pybuffer.h"
51+
#include "pystats.h"
4452
#include "object.h"
4553
#include "objimpl.h"
4654
#include "typeslots.h"

Include/bytesobject.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
2-
/* Bytes object interface */
1+
// Bytes object interface
32

43
#ifndef Py_BYTESOBJECT_H
54
#define Py_BYTESOBJECT_H
65
#ifdef __cplusplus
76
extern "C" {
87
#endif
98

10-
#include <stdarg.h> // va_list
11-
129
/*
1310
Type PyBytesObject represents a byte string. An extra zero byte is
1411
reserved at the end to ensure it is zero-terminated, but a size is

Include/internal/pycore_condvar.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# error "this header requires Py_BUILD_CORE define"
66
#endif
77

8+
#ifndef MS_WINDOWS
9+
# include <unistd.h> // _POSIX_THREADS
10+
#endif
11+
812
#ifndef _POSIX_THREADS
913
/* This means pthreads are not implemented in libc headers, hence the macro
1014
not present in unistd.h. But they still can be implemented as an external

Include/modsupport.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
// Module support interface
12

23
#ifndef Py_MODSUPPORT_H
34
#define Py_MODSUPPORT_H
45
#ifdef __cplusplus
56
extern "C" {
67
#endif
78

8-
/* Module support interface */
9-
10-
#include <stdarg.h> // va_list
11-
129
PyAPI_FUNC(int) PyArg_Parse(PyObject *, const char *, ...);
1310
PyAPI_FUNC(int) PyArg_ParseTuple(PyObject *, const char *, ...);
1411
PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,

Include/object.h

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ A standard interface exists for objects that contain an array of items
5151
whose size is determined when the object is allocated.
5252
*/
5353

54-
#include "pystats.h"
55-
5654
/* Py_DEBUG implies Py_REF_DEBUG. */
5755
#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
5856
# define Py_REF_DEBUG

Include/objimpl.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/* The PyObject_ memory family: high-level object memory interfaces.
2-
See pymem.h for the low-level PyMem_ family.
3-
*/
1+
// The PyObject_ memory family: high-level object memory interfaces.
2+
// See pymem.h for the low-level PyMem_ family.
43

54
#ifndef Py_OBJIMPL_H
65
#define Py_OBJIMPL_H
7-
8-
#include "pymem.h"
9-
106
#ifdef __cplusplus
117
extern "C" {
128
#endif
@@ -231,4 +227,4 @@ PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
231227
#ifdef __cplusplus
232228
}
233229
#endif
234-
#endif /* !Py_OBJIMPL_H */
230+
#endif // !Py_OBJIMPL_H

Include/pyerrors.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
// Error handling definitions
2+
13
#ifndef Py_ERRORS_H
24
#define Py_ERRORS_H
35
#ifdef __cplusplus
46
extern "C" {
57
#endif
68

7-
#include <stdarg.h> // va_list
8-
9-
/* Error handling definitions */
10-
119
PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
1210
PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
1311
PyAPI_FUNC(void) PyErr_SetString(

Include/pymem.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/* The PyMem_ family: low-level memory allocation interfaces.
2-
See objimpl.h for the PyObject_ memory family.
3-
*/
1+
// The PyMem_ family: low-level memory allocation interfaces.
2+
// See objimpl.h for the PyObject_ memory family.
43

54
#ifndef Py_PYMEM_H
65
#define Py_PYMEM_H
7-
8-
#include "pyport.h"
9-
106
#ifdef __cplusplus
117
extern "C" {
128
#endif
@@ -100,5 +96,4 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
10096
#ifdef __cplusplus
10197
}
10298
#endif
103-
104-
#endif /* !Py_PYMEM_H */
99+
#endif // !Py_PYMEM_H

Include/pyport.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
#ifndef Py_PYPORT_H
22
#define Py_PYPORT_H
33

4-
#include "pyconfig.h" /* include for defines */
5-
6-
#include <inttypes.h>
7-
8-
#include <limits.h>
94
#ifndef UCHAR_MAX
10-
# error "limits.h must define UCHAR_MAX"
5+
# error "<limits.h> header must define UCHAR_MAX"
116
#endif
127
#if UCHAR_MAX != 255
138
# error "Python's source code assumes C's unsigned char is an 8-bit type"
@@ -771,4 +766,8 @@ extern char * _getpty(int *, int, mode_t, int);
771766
# define ALIGNOF_MAX_ALIGN_T _Alignof(long double)
772767
#endif
773768

769+
#if defined(__sgi) && !defined(_SGI_MP_SOURCE)
770+
# define _SGI_MP_SOURCE
771+
#endif
772+
774773
#endif /* Py_PYPORT_H */

Include/unicodeobject.h

-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef Py_UNICODEOBJECT_H
22
#define Py_UNICODEOBJECT_H
33

4-
#include <stdarg.h> // va_list
5-
64
/*
75
86
Unicode implementation based on original code by Fredrik Lundh,
@@ -55,8 +53,6 @@ Copyright (c) Corporation for National Research Initiatives.
5553
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5654
* -------------------------------------------------------------------- */
5755

58-
#include <ctype.h>
59-
6056
/* === Internal API ======================================================= */
6157

6258
/* --- Internal Unicode Format -------------------------------------------- */
@@ -93,10 +89,6 @@ Copyright (c) Corporation for National Research Initiatives.
9389
# endif
9490
#endif
9591

96-
#ifdef HAVE_WCHAR_H
97-
# include <wchar.h>
98-
#endif
99-
10092
/* Py_UCS4 and Py_UCS2 are typedefs for the respective
10193
unicode representations. */
10294
typedef uint32_t Py_UCS4;

0 commit comments

Comments
 (0)