Skip to content

Commit 61691d8

Browse files
authored
bpo-38353: Cleanup includes in the internal C API (GH-16548)
Use forward declaration of types to avoid includes in the internal C API. Add also comment to justify other includes.
1 parent b3e7045 commit 61691d8

16 files changed

+27
-31
lines changed

Include/internal/pycore_atomic.h

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

11-
#include "dynamic_annotations.h"
12-
11+
#include "dynamic_annotations.h" /* _Py_ANNOTATE_MEMORY_ORDER */
1312
#include "pyconfig.h"
1413

1514
#if defined(HAVE_STD_ATOMIC)

Include/internal/pycore_ceval.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_atomic.h"
12-
#include "pycore_pystate.h"
13-
#include "pythread.h"
11+
/* Forward declarations */
12+
typedef struct pyruntimestate _PyRuntimeState;
13+
struct _ceval_runtime_state;
1414

1515
PyAPI_FUNC(void) _Py_FinishPendingCalls(_PyRuntimeState *runtime);
1616
PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *);

Include/internal/pycore_context.h

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

8-
#include "pycore_hamt.h"
8+
#include "pycore_hamt.h" /* PyHamtObject */
99

1010
struct _pycontextobject {
1111
PyObject_HEAD

Include/internal/pycore_gil.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_condvar.h"
12-
#include "pycore_atomic.h"
11+
#include "pycore_atomic.h" /* _Py_atomic_address */
12+
#include "pycore_condvar.h" /* PyCOND_T */
1313

1414
#ifndef Py_HAVE_CONDVAR
1515
# error You need either a POSIX-compatible or a Windows system!

Include/internal/pycore_initconfig.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_pystate.h" /* _PyRuntimeState */
11+
/* Forward declaration */
12+
typedef struct pyruntimestate _PyRuntimeState;
1213

1314
/* --- PyStatus ----------------------------------------------- */
1415

@@ -60,7 +61,7 @@ PyAPI_FUNC(PyObject*) _PyWideStringList_AsList(const PyWideStringList *list);
6061

6162
/* --- _PyArgv ---------------------------------------------------- */
6263

63-
typedef struct {
64+
typedef struct _PyArgv {
6465
Py_ssize_t argc;
6566
int use_bytes_argv;
6667
char * const *bytes_argv;

Include/internal/pycore_object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_pystate.h" /* _PyRuntime */
11+
#include "pycore_pystate.h" /* _PyRuntime.gc */
1212

1313
PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
1414
PyAPI_FUNC(int) _PyUnicode_CheckConsistency(PyObject *op, int check_content);

Include/internal/pycore_pylifecycle.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_initconfig.h" /* _PyArgv */
12-
#include "pycore_pystate.h" /* _PyRuntimeState */
11+
/* Forward declarations */
12+
typedef struct _PyArgv _PyArgv;
13+
typedef struct pyruntimestate _PyRuntimeState;
1314

1415
/* True if the main interpreter thread exited due to an unhandled
1516
* KeyboardInterrupt exception, suggesting the user pressed ^C. */

Include/internal/pycore_pymem.h

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

11-
#include "objimpl.h"
12-
#include "pymem.h"
11+
#include "pymem.h" /* PyMemAllocatorName */
1312

1413

1514
/* GC runtime state */

Include/internal/pycore_pystate.h

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "cpython/initconfig.h"
12-
#include "fileobject.h"
13-
#include "pystate.h"
14-
#include "pythread.h"
15-
#include "sysmodule.h"
16-
17-
#include "pycore_gil.h" /* _gil_runtime_state */
18-
#include "pycore_pathconfig.h"
19-
#include "pycore_pymem.h"
20-
#include "pycore_warnings.h"
11+
#include "pycore_gil.h" /* struct _gil_runtime_state */
12+
#include "pycore_pymem.h" /* struct _gc_runtime_state */
13+
#include "pycore_warnings.h" /* struct _warnings_runtime_state */
2114

2215

2316
/* ceval state */

Include/internal/pycore_traceback.h

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

11-
#include "pystate.h" /* PyInterpreterState */
11+
/* Forward declaration */
12+
typedef struct _is PyInterpreterState;
1213

1314
/* Write the Python traceback into the file 'fd'. For example:
1415

Include/internal/pycore_tupleobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "tupleobject.h"
11+
#include "tupleobject.h" /* _PyTuple_CAST() */
1212

1313
#define _PyTuple_ITEMS(op) (_PyTuple_CAST(op)->ob_item)
1414
PyAPI_FUNC(PyObject *) _PyTuple_FromArray(PyObject *const *, Py_ssize_t);

Include/internal/pycore_warnings.h

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "object.h"
12-
1311
struct _warnings_runtime_state {
1412
/* Both 'filters' and 'onceregistry' can be set in warnings.py;
1513
get_warnings_attr() will reset these variables accordingly. */

Modules/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44
#include "pycore_initconfig.h"
5+
#include "pycore_pathconfig.h"
56
#include "pycore_pylifecycle.h"
67
#include "pycore_pymem.h"
78
#include "pycore_pystate.h"

Objects/unicodeobject.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4040

4141
#define PY_SSIZE_T_CLEAN
4242
#include "Python.h"
43-
#include "pycore_initconfig.h"
4443
#include "pycore_fileutils.h"
44+
#include "pycore_initconfig.h"
4545
#include "pycore_object.h"
46+
#include "pycore_pathconfig.h"
4647
#include "pycore_pylifecycle.h"
4748
#include "pycore_pystate.h"
4849
#include "ucnhash.h"

PC/getpathp.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080

8181

8282
#include "Python.h"
83-
#include "pycore_initconfig.h"
83+
#include "pycore_initconfig.h" /* PyStatus */
84+
#include "pycore_pathconfig.h" /* _PyPathConfig */
8485
#include "pycore_pystate.h"
8586
#include "osdefs.h"
8687
#include <wchar.h>

Python/import.c

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Python-ast.h"
66
#undef Yield /* undefine macro conflicting with <winbase.h> */
7+
#include "pycore_initconfig.h"
78
#include "pycore_pyerrors.h"
89
#include "pycore_pyhash.h"
910
#include "pycore_pylifecycle.h"

0 commit comments

Comments
 (0)