]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130931: Add pycore_typedefs.h internal header (#131396)
authorVictor Stinner <vstinner@python.org>
Wed, 19 Mar 2025 14:23:32 +0000 (15:23 +0100)
committerGitHub <noreply@github.com>
Wed, 19 Mar 2025 14:23:32 +0000 (15:23 +0100)
Declare _PyInterpreterFrame and _PyRuntimeState types before
declaring their structure members. Break reference cycles between
header files.

14 files changed:
Include/internal/pycore_ceval.h
Include/internal/pycore_frame.h
Include/internal/pycore_gc.h
Include/internal/pycore_genobject.h
Include/internal/pycore_initconfig.h
Include/internal/pycore_instruments.h
Include/internal/pycore_interp_structs.h
Include/internal/pycore_optimizer.h
Include/internal/pycore_pylifecycle.h
Include/internal/pycore_runtime_structs.h
Include/internal/pycore_typedefs.h [new file with mode: 0644]
Makefile.pre.in
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters

index 5618539f2df73eb68c7c284d0b12ccb3f5c83448..436e5dc1d7dce87dfc8b3a6fdf4ba29f50cdb2ce 100644 (file)
@@ -12,9 +12,10 @@ extern "C" {
 
 #include "pycore_interp.h"        // PyInterpreterState.eval_frame
 #include "pycore_pystate.h"       // _PyThreadState_GET()
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
+
 
 /* Forward declarations */
-struct pyruntimestate;
 struct _ceval_runtime_state;
 
 // Export for '_lsprof' shared extension
@@ -109,7 +110,7 @@ extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks;
 #endif
 
 static inline PyObject*
-_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
+_PyEval_EvalFrame(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
 {
     EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
     if (tstate->interp->eval_frame == NULL) {
@@ -256,7 +257,7 @@ static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate)  {
 static inline void _Py_LeaveRecursiveCall(void)  {
 }
 
-extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
+extern _PyInterpreterFrame* _PyEval_GetFrame(void);
 
 PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);
 
index fc395d8a4491fe598e47c0a50588729c88c0e860..31a98723baf7bff0bf4ccfb2fdf9e311a9c42bf9 100644 (file)
@@ -13,6 +13,7 @@ extern "C" {
 #include "pycore_code.h"          // STATS
 #include "pycore_stackref.h"      // _PyStackRef
 #include "pycore_stats.h"
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
 
 /* See InternalDocs/frames.md for an explanation of the frame stack
  * including explanation of the PyFrameObject and _PyInterpreterFrame
@@ -22,7 +23,7 @@ extern "C" {
 struct _frame {
     PyObject_HEAD
     PyFrameObject *f_back;      /* previous frame, or NULL */
-    struct _PyInterpreterFrame *f_frame; /* points to the frame data */
+    _PyInterpreterFrame *f_frame; /* points to the frame data */
     PyObject *f_trace;          /* Trace function */
     int f_lineno;               /* Current line number. Only valid if non-zero */
     char f_trace_lines;         /* Emit per-line trace events? */
@@ -61,7 +62,7 @@ enum _frameowner {
     FRAME_OWNED_BY_CSTACK = 4,
 };
 
-typedef struct _PyInterpreterFrame {
+struct _PyInterpreterFrame {
     _PyStackRef f_executable; /* Deferred or strong reference (code object or None) */
     struct _PyInterpreterFrame *previous;
     _PyStackRef f_funcobj; /* Deferred or strong reference. Only valid if not on C stack */
@@ -85,7 +86,7 @@ typedef struct _PyInterpreterFrame {
 #endif
     /* Locals and stack */
     _PyStackRef localsplus[1];
-} _PyInterpreterFrame;
+};
 
 #define _PyInterpreterFrame_LASTI(IF) \
     ((int)((IF)->instr_ptr - _PyFrame_GetBytecode((IF))))
index 107a86178a4d7f0861ab8e2645d4682bdb61bd9f..c33b98cdc43fb40adb84ca797d456275fd4822ca 100644 (file)
@@ -8,8 +8,9 @@ extern "C" {
 #  error "this header requires Py_BUILD_CORE define"
 #endif
 
-#include "pycore_runtime_structs.h"
 #include "pycore_pystate.h"
+#include "pycore_runtime_structs.h"
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
 
 
 /* Get an object's GC head */
@@ -348,7 +349,7 @@ extern void _Py_RunGC(PyThreadState *tstate);
 union _PyStackRef;
 
 // GC visit callback for tracked interpreter frames
-extern int _PyGC_VisitFrameStack(struct _PyInterpreterFrame *frame, visitproc visit, void *arg);
+extern int _PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg);
 extern int _PyGC_VisitStackRef(union _PyStackRef *ref, visitproc visit, void *arg);
 
 // Like Py_VISIT but for _PyStackRef fields
index f6d7e6d367177b620fac5a76af3f220818895f5c..b0472c5b9ead36f051159b1dabc4e92691fd4c24 100644 (file)
@@ -9,6 +9,8 @@ extern "C" {
 #endif
 
 #include "pycore_frame.h"
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
+
 
 /* _PyGenObject_HEAD defines the initial segment of generator
    and coroutine objects. */
@@ -27,7 +29,7 @@ extern "C" {
     char prefix##_running_async;                                            \
     /* The frame */                                                         \
     int8_t prefix##_frame_state;                                            \
-    struct _PyInterpreterFrame prefix##_iframe;                             \
+    _PyInterpreterFrame prefix##_iframe;                                    \
 
 struct _PyGenObject {
     /* The gi_ prefix is intended to remind of generator-iterator. */
index 25ec8cec3bd249deebc3c3af59292c695042c766..368dafb90635d7bb7664efd2229b8fd5ac6df148 100644 (file)
@@ -8,8 +8,7 @@ extern "C" {
 #  error "this header requires Py_BUILD_CORE define"
 #endif
 
-/* Forward declaration */
-struct pyruntimestate;
+#include "pycore_typedefs.h"      // _PyRuntimeState
 
 /* --- PyStatus ----------------------------------------------- */
 
@@ -177,7 +176,7 @@ extern PyStatus _PyConfig_InitPathConfig(
 extern PyStatus _PyConfig_InitImportConfig(PyConfig *config);
 extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
 extern PyStatus _PyConfig_Write(const PyConfig *config,
-    struct pyruntimestate *runtime);
+    _PyRuntimeState *runtime);
 extern PyStatus _PyConfig_SetPyArgv(
     PyConfig *config,
     const _PyArgv *args);
index 0e85f97073dd675541d7f37b81da13ed1febd048..7658adca719e86dbfa09366e52eb10f318e6d920 100644 (file)
@@ -6,6 +6,7 @@
 #endif
 
 #include "pycore_structs.h"       // _Py_CODEUNIT
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
 
 #ifdef __cplusplus
 extern "C" {
@@ -34,32 +35,32 @@ int _PyMonitoring_GetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringE
 
 extern int
 _Py_call_instrumentation(PyThreadState *tstate, int event,
-    struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
+    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
 
 extern int
-_Py_call_instrumentation_line(PyThreadState *tstate, struct _PyInterpreterFrame* frame,
+_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
                               _Py_CODEUNIT *instr, _Py_CODEUNIT *prev);
 
 extern int
 _Py_call_instrumentation_instruction(
-    PyThreadState *tstate, struct _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
+    PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
 
 _Py_CODEUNIT *
 _Py_call_instrumentation_jump(
     _Py_CODEUNIT *instr, PyThreadState *tstate, int event,
-    struct _PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
+    _PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
 
 extern int
 _Py_call_instrumentation_arg(PyThreadState *tstate, int event,
-    struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
+    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
 
 extern int
 _Py_call_instrumentation_2args(PyThreadState *tstate, int event,
-    struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
+    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
 
 extern void
 _Py_call_instrumentation_exc2(PyThreadState *tstate, int event,
-    struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
+    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
 
 extern int
 _Py_Instrumentation_GetLine(PyCodeObject *code, int index);
index ec788de316fcefc40d1c5455a7c4814b06d26cac..bdeb7f0199853b4a795c179721a7b163bb09760e 100644 (file)
@@ -4,10 +4,11 @@
 extern "C" {
 #endif
 
-#include "pycore_structs.h"
-#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
-#include "pycore_llist.h"
 #include "pycore_ast_state.h"     // struct ast_state
+#include "pycore_llist.h"
+#include "pycore_pymath.h"        // _PY_SHORT_FLOAT_REPR
+#include "pycore_structs.h"
+#include "pycore_typedefs.h"      // _PyRuntimeState
 
 
 /* This file contains the struct definitions for interpreter state
@@ -802,7 +803,7 @@ struct _is {
     /* Reference to the _PyRuntime global variable. This field exists
        to not have to pass runtime in addition to tstate to a function.
        Get runtime from tstate: tstate->interp->runtime. */
-    struct pyruntimestate *runtime;
+    _PyRuntimeState *runtime;
 
     /* Set by Py_EndInterpreter().
 
index 79861167f65e639e3b044f7d407ca787f49ebe4e..03c2708e0a050535856a771bc8b0648306ee9ae0 100644 (file)
@@ -8,6 +8,7 @@ extern "C" {
 #  error "this header requires Py_BUILD_CORE define"
 #endif
 
+#include "pycore_typedefs.h"      // _PyInterpreterFrame
 #include "pycore_uop_ids.h"
 #include <stdbool.h>
 
@@ -117,7 +118,7 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
 
 #define TRACE_STACK_SIZE 5
 
-int _Py_uop_analyze_and_optimize(struct _PyInterpreterFrame *frame,
+int _Py_uop_analyze_and_optimize(_PyInterpreterFrame *frame,
     _PyUOpInstruction *trace, int trace_len, int curr_stackentries,
     _PyBloomFilter *dependencies);
 
@@ -291,7 +292,7 @@ extern int _Py_uop_frame_pop(JitOptContext *ctx);
 
 PyAPI_FUNC(PyObject *) _Py_uop_symbols_test(PyObject *self, PyObject *ignored);
 
-PyAPI_FUNC(int) _PyOptimizer_Optimize(struct _PyInterpreterFrame *frame, _Py_CODEUNIT *start, _PyExecutorObject **exec_ptr, int chain_depth);
+PyAPI_FUNC(int) _PyOptimizer_Optimize(_PyInterpreterFrame *frame, _Py_CODEUNIT *start, _PyExecutorObject **exec_ptr, int chain_depth);
 
 static inline int is_terminator(const _PyUOpInstruction *uop)
 {
index a5f9418ec057183a2280c9e529fd0b9d853fe011..53ffe7f8fecdba68004c1c05304bd463ade40be5 100644 (file)
@@ -12,7 +12,6 @@ extern "C" {
 
 /* Forward declarations */
 struct _PyArgv;
-struct pyruntimestate;
 
 extern int _Py_SetFileSystemEncoding(
     const char *encoding,
index 2ea1d795f24789a565dff41ed5da62141576bf24..81f53afa3c080d09d6492e78f8a2d584ace7f0cc 100644 (file)
@@ -143,7 +143,7 @@ struct _Py_static_objects {
    That data is exported by the internal API as a global variable
    (_PyRuntime, defined near the top of pylifecycle.c).
    */
-typedef struct pyruntimestate {
+struct pyruntimestate {
     /* This field must be first to facilitate locating it by out of process
      * debuggers. Out of process debuggers will use the offsets contained in this
      * field to be able to locate other fields in several interpreter structures
@@ -307,7 +307,7 @@ typedef struct pyruntimestate {
     PyInterpreterState _main_interpreter;
     // _main_interpreter should be the last field of _PyRuntimeState.
     // See https://github.com/python/cpython/issues/127117.
-} _PyRuntimeState;
+};
 
 
 
diff --git a/Include/internal/pycore_typedefs.h b/Include/internal/pycore_typedefs.h
new file mode 100644 (file)
index 0000000..d330cc4
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef Py_INTERNAL_TYPEDEFS_H
+#define Py_INTERNAL_TYPEDEFS_H
+
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct _PyInterpreterFrame _PyInterpreterFrame;
+typedef struct pyruntimestate _PyRuntimeState;
+
+#ifdef __cplusplus
+}
+#endif
+#endif  // !Py_INTERNAL_TYPEDEFS_H
index 05ff1b959bdbaab3e140e2be1cece41a0381e9c9..076312807f3cc700015040feaf19d6fd051f9325 100644 (file)
@@ -1316,6 +1316,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/internal/pycore_tracemalloc.h \
                $(srcdir)/Include/internal/pycore_tstate.h \
                $(srcdir)/Include/internal/pycore_tuple.h \
+               $(srcdir)/Include/internal/pycore_typedefs.h \
                $(srcdir)/Include/internal/pycore_uniqueid.h \
                $(srcdir)/Include/internal/pycore_typeobject.h \
                $(srcdir)/Include/internal/pycore_typevarobject.h \
index 3a06d71d8aa6d8d86cdfb7202b575a2e1d5c6dff..2a245e931fc9b8458e9521ba2a54ab56ad90e74f 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_tracemalloc.h" />
     <ClInclude Include="..\Include\internal\pycore_tstate.h" />
     <ClInclude Include="..\Include\internal\pycore_tuple.h" />
+    <ClInclude Include="..\Include\internal\pycore_typedefs.h" />
     <ClInclude Include="..\Include\internal\pycore_typeobject.h" />
     <ClInclude Include="..\Include\internal\pycore_typevarobject.h" />
     <ClInclude Include="..\Include\internal\pycore_ucnhash.h" />
index 14eebd978db73ec02c418b91f9ad3c00e4ec9cbe..28cd3ec507ac3389f09b3857530afb2373a05a77 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_tuple.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\internal\pycore_typedefs.h">
+      <Filter>Include\internal</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_typeobject.h">
       <Filter>Include\internal</Filter>
     </ClInclude>