Skip to content

bpo-45459: Add pytypedefs.h header file #31527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2022
Merged

bpo-45459: Add pytypedefs.h header file #31527

merged 1 commit into from
Feb 24, 2022

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Feb 23, 2022

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

https://bugs.python.org/issue45459

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
@vstinner
Copy link
Member Author

Last years, more and more interdependencies have been added. Examples:

  • pybuffer.h uses PyObject before it's defined by object.h (included later).
  • object.h must forward declare PyModuleDef since it's used before it's defined by moduleobject.h (included after object.h).
  • object.h uses struct PyMethodDef which is defined later by methodobject.h (included after object.h). object.h has to use struct PyMethodDef instead of PyMethodDef to solve the interdpendency issue.

This PR cleans up header files by moving forward declarations at the top of Python.h.

With this PR, it becomes possible to use type names like PyObject or PyModuleDef rather than using structure names like struct _object or struct PyModuleDef. I didn't make this change in this PR to keep it easier to review. I created GH-31528 draft PR (see the second commit of this PR) to use type names.

@vstinner
Copy link
Member Author

vstinner commented Feb 23, 2022

@vstinner
Copy link
Member Author

If you don't want to go this way, I wrote the bare minimum fix for the clang compiler warning: GH-31539.

But as I wrote in my previous comment, IMO it's now time to cleanup these forward declarations to have a sane way to fix this issuse in a single place. There are too many scattered forward declarations in the Python C API.

@vstinner
Copy link
Member Author

If you don't want to go this way, I wrote the bare minimum fix for the clang compiler warning: #31539.

Defining types in multiple header files is tricky. At my first attempt, I defined PyObject inside #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 and it didn't work when building xxlimited35 which targets the limited C API version 3.5...

@erlend-aasland
Copy link
Contributor

This looks nice and clean. AFAICS, there is no downside; approved post-merge :)

asvetlov pushed a commit that referenced this pull request Feb 26, 2022
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
itamaro added a commit to itamaro/cpython that referenced this pull request May 24, 2022
pythonGH-31527 moved this typedef to `Include/pytypedefs.h`, so this comment should point at the correct location
vstinner pushed a commit that referenced this pull request Jul 9, 2022
GH-31527 moved this typedef to `Include/pytypedefs.h`, so this comment
should point at the correct location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants