✕
Skip to content

gh-161562: Change PyTypeObject.tp_flags type to uint64_t - #161565

Open
vstinner wants to merge 11 commits into
python:mainfrom
vstinner:flags64
Open

vstinner wants to merge 11 commits into
python:mainfrom
vstinner:flags64

Conversation

@vstinner

@vstinner vstinner commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Change also PyType_GetFlags() return type to uint64_t. Add _PyType_GetFlags64() and expose it as PyType_GetFlags() with a macro. Keep the existing PyType_GetFlags() function unchanged (return unsigned long) for the stable ABI.

PyType_HasFeature() now calls _PyType_GetFlags64() on the limited C API 3.16 or newer, or calls PyType_GetFlags() on the limited C API 3.15 and older.

PyType_Spec.flags type stays as unsigned long, since the structure is part of the stable ABI. Use PyType_FromSlots() if you need 64-bit flags.

Change also PyType_GetFlags() return type to uint64_t. Add
_PyType_GetFlags64() and expose it as PyType_GetFlags() with a macro.
Keep the existing PyType_GetFlags() function unchanged (return
unsigned long) for the stable ABI.

PyType_HasFeature() now calls _PyType_GetFlags64() on the limited C
API 3.16 or newer, or calls PyType_GetFlags() on the limited C API
3.15 and older.

PyType_Spec.flags type stays as unsigned long, since the structure is
part of the stable ABI. Use PyType_FromSlots() if you need 64-bit
flags.
@vstinner

vstinner commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

cc @encukou

@read-the-docs-community

read-the-docs-community Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35733133 | 📁 Comparing 557d58a against main (d7684fb)

  🔍 Preview build  

5 files changed · ± 5 modified

± Modified

Fix support.calcobjsize(): use "0Q" alignement if the format contains
"Q" (unsigned long).
@vstinner
vstinner requested a review from pablogsal as a code owner November 7, 2026 19:49
@vstinner

vstinner commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

I'm not sure how debuggers/profilers are supposed to deal with PyTypeObject.tp_flags type change. I had to update _remote_debugging to change the member type (size):

-    if ((GET_MEMBER(unsigned long, type_obj, unwinder->debug_offsets.type_object.tp_flags) & Py_TPFLAGS_LONG_SUBCLASS)) {
+    if ((GET_MEMBER(uint64_t, type_obj, unwinder->debug_offsets.type_object.tp_flags) & Py_TPFLAGS_LONG_SUBCLASS)) {

Comment thread Doc/c-api/type.rst
per-type. It still returns the current version tag.

.. c:function:: unsigned long PyType_GetFlags(PyTypeObject* type)
.. c:function:: uint64_t PyType_GetFlags(PyTypeObject* type)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is in the stable ABI, this change is an ABI change on 32-bit platforms and 64-bit windows.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should maybe elaborate the documentation. The unsigned long PyType_GetFlags(PyTypeObject* type) function remains unchanged with my change, it still returns unsigned long. What changes is the PyType_GetFlags "API": when you write PyType_GetFlags() in your code, in fact, _PyType_GetFlags64() will be called at the ABI level, and _PyType_GetFlags64() return value is uint64_t.

This replacement trick is only done if you don't use use the limited C API or if you target the limited C API 3.16 or newer.

In practice:

PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
PyAPI_FUNC(uint64_t) _PyType_GetFlags64(PyTypeObject*);

#if !defined(Py_LIMITED_API) || Py_LIMITED_API >= _Py_PACK_VERSION(3, 16)
#  define PyType_GetFlags _PyType_GetFlags64
#endif

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the doc. Does it make sense to you?

@hugovk
hugovk removed their request for review November 8, 2026 12:12
@vstinner
vstinner requested review from a team and encukou as code owners November 8, 2026 13:35
@StanFromIreland
StanFromIreland removed their request for review November 8, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants