Skip to content

Commit

Permalink
bpo-45459: Add pytypedefs.h header file (pythonGH-31527)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vstinner committed Feb 24, 2022
1 parent a52d252 commit ec091bd
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions Include/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
extern "C" {
#endif

typedef struct PyCodeObject PyCodeObject;

#ifndef Py_LIMITED_API
# define Py_CPYTHON_CODE_H
# include "cpython/code.h"
Expand Down
5 changes: 0 additions & 5 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Include/descrobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Include/longobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
1 change: 0 additions & 1 deletion Include/methodobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -82,7 +82,7 @@ typedef struct PyModuleDef{
traverseproc m_traverse;
inquiry m_clear;
freefunc m_free;
} PyModuleDef;
};


// Internal C API
Expand Down
11 changes: 3 additions & 8 deletions Include/object.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef Py_OBJECT_H
#define Py_OBJECT_H

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -765,4 +760,4 @@ static inline int PyType_CheckExact(PyObject *op) {
#ifdef __cplusplus
}
#endif
#endif /* !Py_OBJECT_H */
#endif // !Py_OBJECT_H
4 changes: 0 additions & 4 deletions Include/pybuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 0 additions & 2 deletions Include/pyframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

Expand Down
10 changes: 0 additions & 10 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
29 changes: 29 additions & 0 deletions Include/pytypedefs.h
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Include/structmember.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
<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" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@
<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>
Expand Down

0 comments on commit ec091bd

Please sign in to comment.