8000 gh-130931: Add pycore_typedefs.h internal header (#131396) · python/cpython@b8367e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8367e7

Browse files
authored
gh-130931: Add pycore_typedefs.h internal header (#131396)
Declare _PyInterpreterFrame and _PyRuntimeState types before declaring their structure members. Break reference cycles between header files.
1 parent a0478c9 commit b8367e7

14 files changed

+57
-28
lines changed

Include/internal/pycore_ceval.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ extern "C" {
1212

1313
#include "pycore_interp.h" // PyInterpreterState.eval_frame
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
15+
#include "pycore_typedefs.h" // _PyInterpreterFrame
16+
1517

1618
/* Forward declarations */
17-
struct pyruntimestate;
1819
struct _ceval_runtime_state;
1920

2021
// Export for '_lsprof' shared extension
@@ -109,7 +110,7 @@ extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
109110
#endif
110111

111112
static inline PyObject*
112-
_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
113+
_PyEval_EvalFrame(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
113114
{
114115
EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
115116
if (tstate->interp->eval_frame == NULL) {
@@ -256,7 +257,7 @@ static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate) {
256257
static inline void _Py_LeaveRecursiveCall(void) {
257258
}
258259

259-
extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
260+
extern _PyInterpreterFrame* _PyEval_GetFrame(void);
260261

261262
PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
262263

Include/internal/pycore_frame.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313
#include "pycore_code.h" // STATS
1414
#include "pycore_stackref.h" // _PyStackRef
1515
#include "pycore_stats.h"
16+
#include "pycore_typedefs.h" // _PyInterpreterFrame
1617

1718
/* See InternalDocs/frames.md for an explanation of the frame stack
1819
* including explanation of the PyFrameObject and _PyInterpreterFrame
@@ -22,7 +23,7 @@ extern "C" {
2223
struct _frame {
2324
PyObject_HEAD
2425
PyFrameObject *f_back; /* previous frame, or NULL */
25-
struct _PyInterpreterFrame *f_frame; /* points to the frame data */
26+
_PyInterpreterFrame *f_frame; /* points to the frame data */
2627
PyObject *f_trace; /* Trace function */
2728
int f_lineno; /* Current line number. Only valid if non-zero */
2829
char f_trace_lines; /* Emit per-line trace events? */
@@ -61,7 +62,7 @@ enum _frameowner {
6162
FRAME_OWNED_BY_CSTACK = 4,
6263
};
6364

64-
typedef struct _PyInterpreterFrame {
65+
struct _PyInterpreterFrame {
6566
_PyStackRef f_executable; /* Deferred or strong reference (code object or None) */
6667
struct _PyInterpreterFrame *previous;
6768
_PyStackRef f_funcobj; /* Deferred or strong reference. Only valid if not on C stack */
@@ -85,7 +86,7 @@ typedef struct _PyInterpreterFrame {
8586
#endif
8687
/* Locals and stack */
8788
_PyStackRef localsplus[1];
88-
} _PyInterpreterFrame;
89+
};
8990

9091
#define _PyInterpreterFrame_LASTI(IF) \
9192
((int)((IF)->instr_ptr - _PyFrame_GetBytecode((IF))))

Include/internal/pycore_gc.h

Lines changed: 3 additions & 2 deletions
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_runtime_structs.h"
1211
#include "pycore_pystate.h"
12+
#include "pycore_runtime_structs.h"
13+
#include "pycore_typedefs.h" // _PyInterpreterFrame
1314

1415

1516
/* Get an object's GC head */
@@ -348,7 +349,7 @@ extern void _Py_RunGC(PyThreadState *tstate);
348349
union _PyStackRef;
349350

350351
// GC visit callback for tracked interpreter frames
351-
extern int _PyGC_VisitFrameStack(struct _PyInterpreterFrame *frame, visitproc visit, void *arg);
352+
extern int _PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg);
352353
extern int _PyGC_VisitStackRef(union _PyStackRef *ref, visitproc visit, void *arg);
353354

354355
// Like Py_VISIT but for _PyStackRef fields

Include/internal/pycore_genobject.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_frame.h"
12+
#include "pycore_typedefs.h" // _PyInterpreterFrame
13+
1214

1315
/* _PyGenObject_HEAD defines the initial segment of generator
1416
and coroutine objects. */
@@ -27,7 +29,7 @@ extern "C" {
2729
char prefix##_running_async; \
2830
/* The frame */ \
2931
int8_t prefix##_frame_state; \
30-
struct _PyInterpreterFrame prefix##_iframe; \
32+
_PyInterpreterFrame prefix##_iframe; \
3133

3234
struct _PyGenObject {
3335
/* The gi_ prefix is intended to remind of generator-iterator. */

Include/internal/pycore_initconfig.h

Lines changed: 2 additions & 3 deletions
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-
/* Forward declaration */
12-
struct pyruntimestate;
11+
#include "pycore_typedefs.h" // _PyRuntimeState
1312

1413
/* --- PyStatus ----------------------------------------------- */
1514

@@ -177,7 +176,7 @@ extern PyStatus _PyConfig_InitPathConfig(
177176
extern PyStatus _PyConfig_InitImportConfig(PyConfig *config);
178177
extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
179178
extern PyStatus _PyConfig_Write(const PyConfig *config,
180-
struct pyruntimestate *runtime);
179+
_PyRuntimeState *runtime);
181180
extern PyStatus _PyConfig_SetPyArgv(
182181
PyConfig *config,
183182
const _PyArgv *args);

Include/internal/pycore_instruments.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#endif
77

88
#include "pycore_structs.h" // _Py_CODEUNIT
9+
#include "pycore_typedefs.h" // _PyInterpreterFrame
910

1011
#ifdef __cplusplus
1112
extern "C" {
@@ -34,32 +35,32 @@ int _PyMonitoring_GetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringE
3435

3536
extern int
3637
_Py_call_instrumentation(PyThreadState *tstate, int event,
37-
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
38+
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
3839

3940
extern int
40-
_Py_call_instrumentation_line(PyThreadState *tstate, struct _PyInterpreterFrame* frame,
41+
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
4142
_Py_CODEUNIT *instr, _Py_CODEUNIT *prev);
4243

4344
extern int
4445
_Py_call_instrumentation_instruction(
45-
PyThreadState *tstate, struct _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
46+
PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
4647

4748
_Py_CODEUNIT *
4849
_Py_call_instrumentation_jump(
4950
_Py_CODEUNIT *instr, PyThreadState *tstate, int event,
50-
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
51+
_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
5152

5253
extern int
5354
_Py_call_instrumentation_arg(PyThreadState *tstate, int event,
54-
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
55+
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
5556

5657
extern int
5758
_Py_call_instrumentation_2args(PyThreadState *tstate, int event,
58-
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
59+
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
5960

6061
extern void
6162
_Py_call_instrumentation_exc2(PyThreadState *tstate, int event,
62-
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
63+
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
6364

6465
extern int
6566
_Py_Instrumentation_GetLine(PyCodeObject *code, int index);

Include/internal/pycore_interp_structs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
extern "C" {
55
#endif
66

7-
#include "pycore_structs.h"
8-
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
9-
#include "pycore_llist.h"
107
#include "pycore_ast_state.h" // struct ast_state
8+
#include "pycore_llist.h"
9+
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
10+
#include "pycore_structs.h"
11+
#include "pycore_typedefs.h" // _PyRuntimeState
1112

1213

1314
/* This file contains the struct definitions for interpreter state
@@ -802,7 +803,7 @@ struct _is {
802803
/* Reference to the _PyRuntime global variable. This field exists
803804
to not have to pass runtime in addition to tstate to a function.
804805
Get runtime from tstate: tstate->interp->runtime. */
805-
struct pyruntimestate *runtime;
806+
_PyRuntimeState *runtime;
806807

807808
/* Set by Py_EndInterpreter().
808809

Include/internal/pycore_optimizer.h

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

11+
#include "pycore_typedefs.h" // _PyInterpreterFrame
1112
#include "pycore_uop_ids.h"
1213
#include <stdbool.h>
1314

@@ -117,7 +118,7 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
117118

118119
#define TRACE_STACK_SIZE 5
119120

120-
int _Py_uop_analyze_and_optimize(struct _PyInterpreterFrame *frame,
121+
int _Py_uop_analyze_and_optimize(_PyInterpreterFrame *frame,
121122
_PyUOpInstruction *trace, int trace_len, int curr_stackentries,
122123
_PyBloomFilter *dependencies);
123124

@@ -291,7 +292,7 @@ extern int _Py_uop_frame_pop(JitOptContext *ctx);
291292

292293
PyAPI_FUNC(PyObject *) _Py_uop_symbols_test(PyObject *self, PyObject *ignored);
293294

294-
PyAPI_FUNC(int) _PyOptimizer_Optimize(struct _PyInterpreterFrame *frame, _Py_CODEUNIT *start, _PyExecutorObject **exec_ptr, int chain_depth);
295+
PyAPI_FUNC(int) _PyOptimizer_Optimize(_PyInterpreterFrame *frame, _Py_CODEUNIT *start, _PyExecutorObject **exec_ptr, int chain_depth);
295296

296297
static inline int is_terminator(const _PyUOpInstruction *uop)
297298
{

Include/internal/pycore_pylifecycle.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ extern "C" {
1212

1313
/* Forward declarations */
1414
struct _PyArgv;
15-
struct pyruntimestate;
1615

1716
extern int _Py_SetFileSystemEncoding(
1817
const char *encoding,

Include/internal/pycore_runtime_structs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct _Py_static_objects {
143143
That data is exported by the internal API as a global variable
144144
(_PyRuntime, defined near the top of pylifecycle.c).
145145
*/
146-
typedef struct pyruntimestate {
146+
struct pyruntimestate {
147147
/* This field must be first to facilitate locating it by out of process
148148
* debuggers. Out of process debuggers will use the offsets contained in this
149149
* field to be able to locate other fields in several interpreter structures
@@ -307,7 +307,7 @@ typedef struct pyruntimestate {
307307
PyInterpreterState _main_interpreter;
308308
// _main_interpreter should be the last field of _PyRuntimeState.
309309
// See https://github.com/python/cpython/issues/127117.
310-
} _PyRuntimeState;
310+
};
311311

312312

313313

Include/internal/pycore_typedefs.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef Py_INTERNAL_TYPEDEFS_H
2+
#define Py_INTERNAL_TYPEDEFS_H
3+
4+
#ifndef Py_BUILD_CORE
5+
# error "this header requires Py_BUILD_CORE define"
6+
#endif
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
typedef struct _PyInterpreterFrame _PyInterpreterFrame;
13+
typedef struct pyruntimestate _PyRuntimeState;
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif
18+
#endif // !Py_INTERNAL_TYPEDEFS_H

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ PYTHON_HEADERS= \
13161316
$(srcdir)/Include/internal/pycore_tracemalloc.h \
13171317
$(srcdir)/Include/internal/pycore_tstate.h \
13181318
$(srcdir)/Include/internal/pycore_tuple.h \
1319+
$(srcdir)/Include/internal/pycore_typedefs.h \
13191320
$(srcdir)/Include/internal/pycore_uniqueid.h \
13201321
$(srcdir)/Include/internal/pycore_typeobject.h \
13211322
$(srcdir)/Include/internal/pycore_typevarobject.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
<ClInclude Include="..\Include\internal\pycore_tracemalloc.h" />
312312
<ClInclude Include="..\Include\internal\pycore_tstate.h" />
313313
<ClInclude Include="..\Include\internal\pycore_tuple.h" />
314+
<ClInclude Include="..\Include\internal\pycore_typedefs.h" />
314315
<ClInclude Include="..\Include\internal\pycore_typeobject.h" />
315316
<ClInclude Include="..\Include\internal\pycore_typevarobject.h" />
316317
<ClInclude Include="..\Include\internal\pycore_ucnhash.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@
843843
<ClInclude Include="..\Include\internal\pycore_tuple.h">
844844
<Filter>Include\internal</Filter>
845845
</ClInclude>
846+
<ClInclude Include="..\Include\internal\pycore_typedefs.h">
847+
<Filter>Include\internal</Filter>
848+
</ClInclude>
846849
<ClInclude Include="..\Include\internal\pycore_typeobject.h">
847850
<Filter>Include\internal</Filter>
848851
</ClInclude>

0 commit comments

Comments
 (0)
0