Skip to content

gh-139165: Make Py_SIZE, Py_IS_TYPE,Py_ SET_SIZE regular functions in stable ABI#139166

Merged
encukou merged 9 commits into
python:mainfrom
encukou:size-functions
Nov 25, 2025
Merged

gh-139165: Make Py_SIZE, Py_IS_TYPE,Py_ SET_SIZE regular functions in stable ABI#139166
encukou merged 9 commits into
python:mainfrom
encukou:size-functions

Conversation

@encukou
Copy link
Copy Markdown
Member

@encukou encukou commented Sep 19, 2025

Group them together with Py_TYPE & Py_SET_TYPE to cut down on repetitive preprocessor macros.
Format repetitive definitions in object.c more concisely.

Py_SET_TYPE is still left out of the Limited API.


📚 Documentation preview 📚: https://cpython-previews--139166.org.readthedocs.build/

Group them together with Py_TYPE & Py_SET_TYPE to cut down
on repetitive preprocessor macros.
Format repetitive definitions in object.c more concisely.

Py_SET_TYPE is still left out of the Limited API.
Comment thread Include/object.h Outdated
Comment on lines +325 to +326
#elif Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 15)
# if Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 14)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you mean >=?

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.

No, but I can expand the comments :)

Comment thread Objects/object.c
- Don't expose the "real" functions for old Limited API
- Move implementations below defines so that they pick up
  the right Py_TYPE definition
@encukou encukou marked this pull request as ready for review November 20, 2025 12:54
@encukou encukou requested review from a team and ZeroIntensity as code owners November 20, 2025 12:54
Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@encukou encukou merged commit 226011b into python:main Nov 25, 2025
52 checks passed
@encukou encukou deleted the size-functions branch November 25, 2025 13:30
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…ons in stable ABI (pythonGH-139166)

* Make Py_{SIZE,IS_TYPE,SET_SIZE} regular functions in stable ABI

Group them together with Py_TYPE & Py_SET_TYPE to cut down
on repetitive preprocessor macros.
Format repetitive definitions in object.c more concisely.

Py_SET_TYPE is still left out of the Limited API.
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
…ons in stable ABI (pythonGH-139166)

* Make Py_{SIZE,IS_TYPE,SET_SIZE} regular functions in stable ABI

Group them together with Py_TYPE & Py_SET_TYPE to cut down
on repetitive preprocessor macros.
Format repetitive definitions in object.c more concisely.

Py_SET_TYPE is still left out of the Limited API.
Comment thread Include/object.h
Comment on lines +275 to +279
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)
PyAPI_FUNC(Py_ssize_t) Py_SIZE(PyObject *ob);
PyAPI_FUNC(int) Py_IS_TYPE(PyObject *ob, PyTypeObject *type);
PyAPI_FUNC(void) Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size);
#endif
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.

I'm just checking PyO3's FFI definitions against 3.15b1 and it seems to me that this PR has changed these symbols to always be exported functions on 3.15.

That seems to differ with the issue description, which apparently intended to keep the static inline functions for the non-limited API?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Extract of object.c in the 3.15 branch:

static inline int
_Py_IS_TYPE_impl(PyObject *ob, PyTypeObject *type)
{
    return Py_TYPE(ob) == type;
}

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11)
#  define Py_IS_TYPE(ob, type) _Py_IS_TYPE_impl(_PyObject_CAST(ob), (type))
#elif Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 15)
#  define Py_IS_TYPE(ob, type) _Py_IS_TYPE_impl((ob), (type))
#else
// Limited API 3.15+: use function calls
#endif

If Py_LIMITED_API is not defined or if it's defined to Python 3.14 or older, Py_IS_TYPE() is implemented as a static inline function. The code has been modified since this PR.

Would you mind to elaborate which API is implemented as a function call? Using the limited C API or not? Which limited C API version?

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.

You're right, sorry for the confusion. I think the tooling I was using to check had a bug, and then I saw this diff and got myself confused. 🤦

Even looking at this diff again, the #define macros just below starting on 292 will implement as a static inline function.

Sorry for the noise!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No problem. Thanks for double checking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants