Skip to content

Commit ec091bd

Browse files
authored
bpo-45459: Add pytypedefs.h header file (GH-31527)
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
1 parent a52d252 commit ec091bd

16 files changed

+44
-40
lines changed

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "pymacro.h"
4040
#include "pymath.h"
4141
#include "pymem.h"
42+
#include "pytypedefs.h"
4243
#include "pybuffer.h"
4344
#include "object.h"
4445
#include "objimpl.h"

Include/code.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
extern "C" {
77
#endif
88

9-
typedef struct PyCodeObject PyCodeObject;
10-
119
#ifndef Py_LIMITED_API
1210
# define Py_CPYTHON_CODE_H
1311
# include "cpython/code.h"

Include/cpython/object.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,8 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
262262
PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
263263
PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
264264
PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
265-
struct PyModuleDef;
266265
PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *);
267266

268-
struct _Py_Identifier;
269267
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
270268
PyAPI_FUNC(void) _Py_BreakPoint(void);
271269
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
@@ -462,9 +460,6 @@ partially-deallocated object. To check this, the tp_dealloc function must be
462460
passed as second argument to Py_TRASHCAN_BEGIN().
463461
*/
464462

465-
/* Forward declarations for PyThreadState */
466-
struct _ts;
467-
468463
/* Python 3.9 private API, invoked by the macros below. */
469464
PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op);
470465
PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);

Include/descrobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ extern "C" {
88
typedef PyObject *(*getter)(PyObject *, void *);
99
typedef int (*setter)(PyObject *, PyObject *, void *);
1010

11-
typedef struct PyGetSetDef {
11+
struct PyGetSetDef {
1212
const char *name;
1313
getter get;
1414
setter set;
1515
const char *doc;
1616
void *closure;
17-
} PyGetSetDef;
17+
};
1818

1919
PyAPI_DATA(PyTypeObject) PyClassMethodDescr_Type;
2020
PyAPI_DATA(PyTypeObject) PyGetSetDescr_Type;

Include/longobject.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ extern "C" {
77

88
/* Long (arbitrary precision) integer object interface */
99

10-
typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */
11-
1210
PyAPI_DATA(PyTypeObject) PyLong_Type;
1311

1412
#define PyLong_Check(op) \

Include/methodobject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct PyMethodDef {
3939
describe the args expected by the C func */
4040
const char *ml_doc; /* The __doc__ attribute, or NULL */
4141
};
42-
typedef struct PyMethodDef PyMethodDef;
4342

4443
/* PyCFunction_New is declared as a function for stable ABI (declaration is
4544
* needed for e.g. GCC with -fvisibility=hidden), but redefined as a macro

Include/moduleobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef struct PyModuleDef_Slot{
7272

7373
#endif /* New in 3.5 */
7474

75-
typedef struct PyModuleDef{
75+
struct PyModuleDef {
7676
PyModuleDef_Base m_base;
7777
const char* m_name;
7878
const char* m_doc;
@@ -82,7 +82,7 @@ typedef struct PyModuleDef{
8282
traverseproc m_traverse;
8383
inquiry m_clear;
8484
freefunc m_free;
85-
} PyModuleDef;
85+
};
8686

8787

8888
// Internal C API

Include/object.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#ifndef Py_OBJECT_H
22
#define Py_OBJECT_H
3-
43
#ifdef __cplusplus
54
extern "C" {
65
#endif
@@ -61,10 +60,6 @@ whose size is determined when the object is allocated.
6160
# error Py_LIMITED_API is incompatible with Py_TRACE_REFS
6261
#endif
6362

64-
/* PyTypeObject structure is defined in cpython/object.h.
65-
In Py_LIMITED_API, PyTypeObject is an opaque structure. */
66-
typedef struct _typeobject PyTypeObject;
67-
6863
#ifdef Py_TRACE_REFS
6964
/* Define pointers to support a doubly-linked list of all live heap objects. */
7065
#define _PyObject_HEAD_EXTRA \
@@ -102,11 +97,11 @@ typedef struct _typeobject PyTypeObject;
10297
* by hand. Similarly every pointer to a variable-size Python object can,
10398
* in addition, be cast to PyVarObject*.
10499
*/
105-
typedef struct _object {
100+
struct _object {
106101
_PyObject_HEAD_EXTRA
107102
Py_ssize_t ob_refcnt;
108103
PyTypeObject *ob_type;
109-
} PyObject;
104+
};
110105

111106
/* Cast argument to PyObject* type. */
112107
#define _PyObject_CAST(op) ((PyObject*)(op))
@@ -765,4 +760,4 @@ static inline int PyType_CheckExact(PyObject *op) {
765760
#ifdef __cplusplus
766761
}
767762
#endif
768-
#endif /* !Py_OBJECT_H */
763+
#endif // !Py_OBJECT_H

Include/pybuffer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ extern "C" {
1717
*
1818
*/
1919

20-
// Forward declaration to be able to include pybuffer.h before object.h:
21-
// pybuffer.h uses PyObject and object.h uses Py_buffer.
22-
typedef struct _object PyObject;
23-
2420
typedef struct {
2521
void *buf;
2622
PyObject *obj; /* owned reference */

Include/pyframe.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
extern "C" {
1010
#endif
1111

12-
typedef struct _frame PyFrameObject;
13-
1412
/* Return the line of code the frame is currently executing. */
1513
PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
1614

0 commit comments

Comments
 (0)