Skip to content

C API: Use _Py_PACK_VERSION() in Py_LIMITED_API version checks - #161622

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:use_pack_version
Open

vstinner wants to merge 2 commits into
python:mainfrom
vstinner:use_pack_version

Conversation

@vstinner

@vstinner vstinner commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Move _Py_PACK_VERSION() from pymacro.h to patchlevel.h.

PR generated with these shell commands on Linux:

HEADERS=$(find Include -name "*.h")
sed -i -e "s#0x030[aA]0000#_Py_PACK_VERSION(3, 10)#g" $HEADERS
sed -i -e "s#0x030[bB]0000#_Py_PACK_VERSION(3, 11)#g" $HEADERS
sed -i -e "s#0x030[cC]0000#_Py_PACK_VERSION(3, 12)#g" $HEADERS
sed -i -e "s#0x030[dD]0000#_Py_PACK_VERSION(3, 13)#g" $HEADERS
sed -i -e "s#0x030[eE]0000#_Py_PACK_VERSION(3, 14)#g" $HEADERS
sed -i -e "s#0x030[fF]0000#_Py_PACK_VERSION(3, 15)#g" $HEADERS
sed -i -e "s#0x030a00A7#_Py_PACK_VERSION(3, 10)#g" Include/refcount.h

@vstinner

vstinner commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

cc @encukou @serhiy-storchaka

I'm never able to remind the value of hexadecimal version numbers such as 0x030d0000. I have to run a Python REPL to convert 0xd to decimal. _Py_PACK_VERSION(3, 13) avoids this issue, I can directly read the Python version number.

@encukou

encukou commented Nov 9, 2026

Copy link
Copy Markdown
Member

This looks like churn to me -- especially with the pre-3.10 versions.

@vstinner

vstinner commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

To me, Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 9) is more readable than Py_LIMITED_API+0 >= 0x03090000 and there are less risk of typos. I would prefer to have a consistent code style, always use _Py_PACK_VERSION(), for Py_LIMITED_API checks.

@ZeroIntensity

Copy link
Copy Markdown
Member

I agree that this is a significant readability improvement. I can't read hexadecimals either.

@encukou

encukou commented Nov 10, 2026

Copy link
Copy Markdown
Member

I won't block this. But it's the kind of thing that makes backports annoying.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Can we at least limit this to 3.10+?

I have no strong opinion. It is a churn, but hexadecimal versions are less readable and more errorprone.

@brettcannon
brettcannon removed their request for review November 10, 2026 17:57
Move _Py_PACK_VERSION() from pymacro.h to patchlevel.h.

PR generated with these shell commands on Linux:

    HEADERS=$(find Include -name "*.h")
    sed -i -e "s#0x030[aA]0000#_Py_PACK_VERSION(3, 10)#g" $HEADERS
    sed -i -e "s#0x030[bB]0000#_Py_PACK_VERSION(3, 11)#g" $HEADERS
    sed -i -e "s#0x030[cC]0000#_Py_PACK_VERSION(3, 12)#g" $HEADERS
    sed -i -e "s#0x030[dD]0000#_Py_PACK_VERSION(3, 13)#g" $HEADERS
    sed -i -e "s#0x030[eE]0000#_Py_PACK_VERSION(3, 14)#g" $HEADERS
    sed -i -e "s#0x030[fF]0000#_Py_PACK_VERSION(3, 15)#g" $HEADERS
    sed -i -e "s#0x030a00A7#_Py_PACK_VERSION(3, 10)#g" Include/refcount.h
@vstinner

Copy link
Copy Markdown
Member Author

@serhiy-storchaka, @ZeroIntensity: Would you mind reviewing the updated PR? I reverted changes on versions 3.0 to 3.9.

@serhiy-storchaka:

Can we at least limit this to 3.10+?

Ok. I regenerated the PR to only replace versions 3.10 and newer. The PR is now way smaller.

Note: I used git rebase to be able to rebase on main and to rewrite the commit message.

Revert also pyabi.h changes.

@ZeroIntensity ZeroIntensity left a comment

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.

I think pre-3.10 stuff would be fine to do as well, but I'm happy with this too.

@vstinner

Copy link
Copy Markdown
Member Author

Sanitizers / TSan (free-threading): test_capi fails. It should be fixed by #162154.

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.

4 participants