]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45459: Add pytypedefs.h header file (GH-31527)
authorVictor Stinner <vstinner@python.org>
Thu, 24 Feb 2022 16:07:12 +0000 (17:07 +0100)
committerGitHub <noreply@github.com>
Thu, 24 Feb 2022 16:07:12 +0000 (17:07 +0100)
Move forward declarations of Python C API types to a new pytypedefs.h
header file to solve interdependency issues between header files.

pytypedefs.h contains forward declarations of the following types:

* PyCodeObject
* PyFrameObject
* PyGetSetDef
* PyInterpreterState
* PyLongObject
* PyMemberDef
* PyMethodDef
* PyModuleDef
* PyObject
* PyThreadState
* PyTypeObject

16 files changed:
Include/Python.h
Include/code.h
Include/cpython/object.h
Include/descrobject.h
Include/longobject.h
Include/methodobject.h
Include/moduleobject.h
Include/object.h
Include/pybuffer.h
Include/pyframe.h
Include/pystate.h
Include/pytypedefs.h [new file with mode: 0644]
Include/structmember.h
Makefile.pre.in
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters

index 5bc8cc656af0de6e5d67b2d477fc3a2ab197da9b..4dc2edb4dbbe235296c553a5757a526895661c31 100644 (file)
@@ -39,6 +39,7 @@
 #include "pymacro.h"
 #include "pymath.h"
 #include "pymem.h"
+#include "pytypedefs.h"
 #include "pybuffer.h"
 #include "object.h"
 #include "objimpl.h"
index 2dea3c261053181a0894558bd790d3b5d0457b6b..0245c32bc25442cbbf63f0391a44355c9de011bb 100644 (file)
@@ -6,8 +6,6 @@
 extern "C" {
 #endif
 
-typedef struct PyCodeObject PyCodeObject;
-
 #ifndef Py_LIMITED_API
 #  define Py_CPYTHON_CODE_H
 #  include "cpython/code.h"
index 6cc3d72ca7084978f57ae9945a7cb93b8ff4fa0d..65d7d85b7075d5b00ccc06ac08a94613a8baa9d4 100644 (file)
@@ -262,10 +262,8 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
 PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
 PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
 PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
-struct PyModuleDef;
 PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *);
 
-struct _Py_Identifier;
 PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
 PyAPI_FUNC(void) _Py_BreakPoint(void);
 PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
@@ -462,9 +460,6 @@ partially-deallocated object. To check this, the tp_dealloc function must be
 passed as second argument to Py_TRASHCAN_BEGIN().
 */
 
-/* Forward declarations for PyThreadState */
-struct _ts;
-
 /* Python 3.9 private API, invoked by the macros below. */
 PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op);
 PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
index 36802f4c55afafe1fa34277162aa401cc34a6c44..aefa7600346b52ad0f0ec28d23f0891178420a2b 100644 (file)
@@ -8,13 +8,13 @@ extern "C" {
 typedef PyObject *(*getter)(PyObject *, void *);
 typedef int (*setter)(PyObject *, PyObject *, void *);
 
-typedef struct PyGetSetDef {
+struct PyGetSetDef {
     const char *name;
     getter get;
     setter set;
     const char *doc;
     void *closure;
-} PyGetSetDef;
+};
 
 PyAPI_DATA(PyTypeObject) PyClassMethodDescr_Type;
 PyAPI_DATA(PyTypeObject) PyGetSetDescr_Type;
index 7fe8f58cb3e959f1514b25b1eed3e07f9548c022..81ba1239a69ecf502965cd248a94ef8dafaabfb3 100644 (file)
@@ -7,8 +7,6 @@ extern "C" {
 
 /* Long (arbitrary precision) integer object interface */
 
-typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */
-
 PyAPI_DATA(PyTypeObject) PyLong_Type;
 
 #define PyLong_Check(op) \
index 1be5873a30569fadedc2193ca4bf322e0f325dd2..5d2e06c3e7cea7f187300703ef68343b5c2df888 100644 (file)
@@ -39,7 +39,6 @@ struct PyMethodDef {
                                describe the args expected by the C func */
     const char  *ml_doc;    /* The __doc__ attribute, or NULL */
 };
-typedef struct PyMethodDef PyMethodDef;
 
 /* PyCFunction_New is declared as a function for stable ABI (declaration is
  * needed for e.g. GCC with -fvisibility=hidden), but redefined as a macro
index 49b116ca1c358716612d39b92a7055f6995bcb77..0f406832b58eaba34e440e050f77ca1951b47daf 100644 (file)
@@ -72,7 +72,7 @@ typedef struct PyModuleDef_Slot{
 
 #endif /* New in 3.5 */
 
-typedef struct PyModuleDef{
+struct PyModuleDef {
   PyModuleDef_Base m_base;
   const char* m_name;
   const char* m_doc;
@@ -82,7 +82,7 @@ typedef struct PyModuleDef{
   traverseproc m_traverse;
   inquiry m_clear;
   freefunc m_free;
-} PyModuleDef;
+};
 
 
 // Internal C API
index 3566c736a535c429f36ab534dd8281a6374535aa..5313ea6f867b9fcc761c1bdb3a2580fc38b02092 100644 (file)
@@ -1,6 +1,5 @@
 #ifndef Py_OBJECT_H
 #define Py_OBJECT_H
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -61,10 +60,6 @@ whose size is determined when the object is allocated.
 #  error Py_LIMITED_API is incompatible with Py_TRACE_REFS
 #endif
 
-/* PyTypeObject structure is defined in cpython/object.h.
-   In Py_LIMITED_API, PyTypeObject is an opaque structure. */
-typedef struct _typeobject PyTypeObject;
-
 #ifdef Py_TRACE_REFS
 /* Define pointers to support a doubly-linked list of all live heap objects. */
 #define _PyObject_HEAD_EXTRA            \
@@ -102,11 +97,11 @@ typedef struct _typeobject PyTypeObject;
  * by hand.  Similarly every pointer to a variable-size Python object can,
  * in addition, be cast to PyVarObject*.
  */
-typedef struct _object {
+struct _object {
     _PyObject_HEAD_EXTRA
     Py_ssize_t ob_refcnt;
     PyTypeObject *ob_type;
-} PyObject;
+};
 
 /* Cast argument to PyObject* type. */
 #define _PyObject_CAST(op) ((PyObject*)(op))
@@ -765,4 +760,4 @@ static inline int PyType_CheckExact(PyObject *op) {
 #ifdef __cplusplus
 }
 #endif
-#endif /* !Py_OBJECT_H */
+#endif   // !Py_OBJECT_H
index 31795b780710fa7c2b752de6f74df6050f24bbf5..6893505e66e3e812448d651451cca5e194e0f88f 100644 (file)
@@ -17,10 +17,6 @@ extern "C" {
  *
  */
 
-// Forward declaration to be able to include pybuffer.h before object.h:
-// pybuffer.h uses PyObject and object.h uses Py_buffer.
-typedef struct _object PyObject;
-
 typedef struct {
     void *buf;
     PyObject *obj;        /* owned reference */
index 3816224201c7e44725d4aa0bf32d1b9da9256bc5..feac16f69de698c2b4a2053abe30c2874c3e9562 100644 (file)
@@ -9,8 +9,6 @@
 extern "C" {
 #endif
 
-typedef struct _frame PyFrameObject;
-
 /* Return the line of code the frame is currently executing. */
 PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
 
index b6ee0ede81dea0240373df6c281d2c058cdee101..5b4245ed7f396082e0a63f5b99a896a79c782ea6 100644 (file)
@@ -11,16 +11,6 @@ extern "C" {
 removed (with effort). */
 #define MAX_CO_EXTRA_USERS 255
 
-/* Forward declarations for PyFrameObject, PyThreadState
-   and PyInterpreterState */
-struct _ts;
-struct _is;
-
-/* struct _ts is defined in cpython/pystate.h */
-typedef struct _ts PyThreadState;
-/* struct _is is defined in internal/pycore_interp.h */
-typedef struct _is PyInterpreterState;
-
 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
 PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
 PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
diff --git a/Include/pytypedefs.h b/Include/pytypedefs.h
new file mode 100644 (file)
index 0000000..5dd841e
--- /dev/null
@@ -0,0 +1,29 @@
+// Forward declarations of types of the Python C API.
+// Declare them at the same place since redefining typedef is a C11 feature.
+// Only use a forward declaration if there is an interdependency between two
+// header files.
+
+#ifndef Py_PYTYPEDEFS_H
+#define Py_PYTYPEDEFS_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct PyModuleDef PyModuleDef;
+typedef struct PyMethodDef PyMethodDef;
+typedef struct PyGetSetDef PyGetSetDef;
+typedef struct PyMemberDef PyMemberDef;
+
+typedef struct _object PyObject;
+typedef struct _longobject PyLongObject;
+typedef struct _typeobject PyTypeObject;
+typedef struct PyCodeObject PyCodeObject;
+typedef struct _frame PyFrameObject;
+
+typedef struct _ts PyThreadState;
+typedef struct _is PyInterpreterState;
+
+#ifdef __cplusplus
+}
+#endif
+#endif   // !Py_PYTYPEDEFS_H
index 93b7aff8091848fd8550da9bd6a0843ec82727dd..acce4381d4e0056de446da8a8b18dc78741ad5c6 100644 (file)
@@ -15,13 +15,13 @@ extern "C" {
    flag is set).  The array must be terminated with an entry whose name
    pointer is NULL. */
 
-typedef struct PyMemberDef {
+struct PyMemberDef {
     const char *name;
     int type;
     Py_ssize_t offset;
     int flags;
     const char *doc;
-} PyMemberDef;
+};
 
 /* Types */
 #define T_SHORT     0
index 2a3e0fbceb4c07cec5977fb91ab90c2bc327b081..0383853901df1efc043ff51dc4362216ac675f7e 100644 (file)
@@ -1488,6 +1488,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/pystrtod.h \
                $(srcdir)/Include/pythonrun.h \
                $(srcdir)/Include/pythread.h \
+               $(srcdir)/Include/pytypedefs.h \
                $(srcdir)/Include/rangeobject.h \
                $(srcdir)/Include/setobject.h \
                $(srcdir)/Include/sliceobject.h \
index c2f1a016bc11155e28a58f70a2d7274cca409876..96996988ec36b9035a77fe436798fa76022a7ed0 100644 (file)
     <ClInclude Include="..\Include\pystrtod.h" />
     <ClInclude Include="..\Include\pythonrun.h" />
     <ClInclude Include="..\Include\pythread.h" />
+    <ClInclude Include="..\Include\pytypedefs.h" />
     <ClInclude Include="..\Include\rangeobject.h" />
     <ClInclude Include="..\Include\setobject.h" />
     <ClInclude Include="..\Include\sliceobject.h" />
index b300103bbb42062de1c1a7e1766dfed48c2d2cca..92c6bf8535b98d765bee8e87791e97c6f7e86614 100644 (file)
     <ClInclude Include="..\Include\pythread.h">
       <Filter>Include</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\pytypedefs.h">
+      <Filter>Include</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\rangeobject.h">
       <Filter>Include</Filter>
     </ClInclude>