-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
C API: Investigate how the PyTypeObject members can be removed from the public C API #105970
Comments
For |
One thing I don't know how to do with the spec-based API is adding additional C fields to a type without exposing them to Python. That is something I have a use-case for in PyObjC and can currently do by embedding a I want to try moving PyObjC to the spec-based API during the 3.13 release cycle if I have enough free time, but that's a fairly big "if". The longer term goal with that is to support sub interpreters. |
Isn't it the purpose of PEP 697 – Limited C API for Extending Opaque Types? It's said to be fully implemented in Python 3.12. cc @encukou |
Changing In general, most members can be get with Attributes (read-only):
Attributes:
Internal functions for the GC:
Internal attributes and methods:
Async methods:
Buffer methods:
Mapping methods:
Number methods:
Sequence methods:
Attribute lookup, get/attr instance attributes:
Methods and how to call them:
PyHeapTypeObject internal members:
PyHeapTypeObject members:
Unused members:
|
Creating a static type Creating a heap type can be done with the The Python stdlib is a bad example, it still has many static types in Python 3.13: issue #84258. Moreover, some static types may remain implemented as static types since they are exposed in the C API, such as What changed recently is that many data are now "static": generated at build type and constant. "Static" types means more and more something really "static", like it's no longer needed to release their memory at Python exit (issue #103276). |
PyType_GetDict() is excluded from the limited C API, and _PyObject_GetDictPtr() is part of the internal API. |
Using tp_name to format an error message is a very common code pattern. I created issue gh-111696 for that. |
Let's revisit that once PEP 737 is accepted and implementated, it's one of the most common consumer of the PyTypeObject. |
I propose to investigate an incompatible C API change: make the
PyTypeObject
andPyHeapTypeObject
structures opaque, remove their members from the public C API (move them to the internal C API). We have to investigate how it's outside in 3rd party C extensions (ex: Cython, pybind11, etc.), and design a smooth migration plan.The PyTypeObject structure is exposed as part of the public Python C API. For example,
Py_TYPE(obj)->tp_name
directly gets a type name (as a UTF-8 encoded byte string,char*
).The PyTypeObject members are NOT part of the limited C API (PEP 384).
In Python 3.9 (2020), I reworked the C API to avoid accessing directly PyTypeObject members at the ABI level: issue #84351. For example, Python 3.8 implements
PyObject_IS_GC()
as a macro:whereas Python 3.9 only provides an opaque function call:
At the ABI level, the direct access to the
PyTypeObject.tp_is_gc
member became an opaque function call.Changing PyTypeObject API and ABI caused a lot of troubles in the past. Example:
PyTypeObject.tp_print
removal -- related toPyTypeObject.tp_vectorcall_offset
additionPyTypeObject.tp_finalize
(PEP 442). IsPy_TPFLAGS_HAVE_FINALIZE
needed for ABI compatibility? See also the python-dev thread.Py_TPFLAGS_HAVE_VERSION_TAG
flag (ABI compatibility)For many years, there is a work-in-progress to convert all Python built-in types and types of stdlib extensions from static types to heap types. See for example issue #84258 and PEP 630.
The API and ABI for heap type was also enhanced over the years. Examples:
PyType_FromMetaclass()
In the past, other structure members were removed:
The work was also prepared for:
The text was updated successfully, but these errors were encountered: