Title: C API: Investigate how the PyTypeObject members can be removed from the public C API · Issue #105970 · python/cpython · GitHub
Open Graph Title: C API: Investigate how the PyTypeObject members can be removed from the public C API · Issue #105970 · python/cpython
X Title: C API: Investigate how the PyTypeObject members can be removed from the public C API · Issue #105970 · python/cpython
Description: I propose to investigate an incompatible C API change: make the PyTypeObject and PyHeapTypeObject 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...
Open Graph Description: I propose to investigate an incompatible C API change: make the PyTypeObject and PyHeapTypeObject structures opaque, remove their members from the public C API (move them to the internal C API). We...
X Description: I propose to investigate an incompatible C API change: make the PyTypeObject and PyHeapTypeObject structures opaque, remove their members from the public C API (move them to the internal C API). We...
Opengraph URL: https://github.com/python/cpython/issues/105970
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"C API: Investigate how the PyTypeObject members can be removed from the public C API","articleBody":"I propose to investigate an incompatible C API change: make the ``PyTypeObject`` and ``PyHeapTypeObject`` 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.\r\n\r\nThe PyTypeObject structure is exposed as part of the public Python C API. For example, ``Py_TYPE(obj)-\u003etp_name`` directly gets a type name (as a UTF-8 encoded byte string, ``char*``).\r\n\r\nThe PyTypeObject members are **NOT** part of the limited C API (PEP 384).\r\n\r\n---\r\n\r\nIn 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:\r\n\r\n```c\r\n/* Test if an object has a GC head */\r\n#define PyObject_IS_GC(o) \\\r\n (PyType_IS_GC(Py_TYPE(o)) \\\r\n \u0026\u0026 (Py_TYPE(o)-\u003etp_is_gc == NULL || Py_TYPE(o)-\u003etp_is_gc(o)))\r\n```\r\n\r\nwhereas Python 3.9 only provides an opaque function call:\r\n\r\n```c\r\n/* Test if an object implements the garbage collector protocol */\r\nPyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);\r\n```\r\n\r\nAt the ABI level, the direct access to the ``PyTypeObject.tp_is_gc`` member became an opaque function call.\r\n\r\n---\r\n\r\nChanging PyTypeObject API and ABI caused a lot of troubles in the past. Example:\r\n\r\n* issue GH-81431: Cython broken by ``PyTypeObject.tp_print`` removal -- related to ``PyTypeObject.tp_vectorcall_offset`` addition\r\n* issue GH-76569: ABI issue with the addition of ``PyTypeObject.tp_finalize`` (PEP 442). Is ``Py_TPFLAGS_HAVE_FINALIZE`` needed for ABI compatibility? See also the [python-dev thread](https://mail.python.org/pipermail/python-dev/2017-December/151345.html).\r\n* issue #86913: remove ``Py_TPFLAGS_HAVE_VERSION_TAG`` flag (ABI compatibility)\r\n* issue GH-91271: Documentation inconsistency with the stable ABI (PyTypeObject)\r\n* The [PEP 620](https://peps.python.org/pep-0620/#the-c-api-blocks-cpython-evolutions) uses PyTypeObject incompatible changes in its rationale.\r\n\r\n---\r\n\r\nFor 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](https://peps.python.org/pep-0630/).\r\n\r\nThe API and ABI for heap type was also enhanced over the years. Examples:\r\n\r\n* Python 3.12 adds ``PyType_FromMetaclass()``\r\n* Add the buffer protocol to the limited C API (to PyType_FromSpec()): issue #89622\r\n* issue GH-86201: get tp_name with PyType_GetSlot()\r\n\r\n---\r\n\r\nIn the past, other structure members were removed:\r\n\r\n* PyInterpreterState: Python 3.8\r\n* PyGC_Head: Python 3.9\r\n* PyFrameObject: Python 3.11\r\n\r\nThe work was also prepared for:\r\n\r\n* PyObject: issue #83754, Python 3.10 and Python 3.11, add Py_SET_REFCNT() and Py_SET_TYPE()\r\n* PyTheadState: issue #84128, Python 3.11","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2023-06-21T14:32:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/105970/cpython/issues/105970"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ea756e3b-f7e6-d377-01fe-276de4573bad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8910:C2480:995666:D5EBC8:696A794A |
| html-safe-nonce | cb72e0b2900957cfb57af7010ca48128cedef4712fc7f96809959808089cd4df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTEwOkMyNDgwOjk5NTY2NjpENUVCQzg6Njk2QTc5NEEiLCJ2aXNpdG9yX2lkIjoiMjk2ODczNDQwMDk1NTE4NTQ4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 20f904aff303e626ad0f16d1e684f2254ac529da99723fc930456a1a5659383c |
| hovercard-subject-tag | issue:1767748969 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/105970/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5643349ccbe8cab1520dd1420f9d5d721fd6123e24f32214a2350f263df5c7ed/python/cpython/issues/105970 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5643349ccbe8cab1520dd1420f9d5d721fd6123e24f32214a2350f263df5c7ed/python/cpython/issues/105970 |
| og:image:alt | I propose to investigate an incompatible C API change: make the PyTypeObject and PyHeapTypeObject structures opaque, remove their members from the public C API (move them to the internal C API). We... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5b774e44f85c14a75886edd04ddda4e5a25ddebbb241bcbb590b08a3048730e8 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cc5f4eee261b3601c1e98e217ceaf28508b9567e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width