Skip to content

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

Merged
vstinner merged 5 commits into
python:mainfrom
vstinner:use_pack_version
Nov 23, 2026
Merged

vstinner merged 5 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.

@vstinner

Copy link
Copy Markdown
Member Author

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

The TSan PR has been merged. I updated this PR branch to retrieve the fix.

@vstinner vstinner added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 23, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit e1d5655 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F161622%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 23, 2026
@vstinner

Copy link
Copy Markdown
Member Author

I merged the main branch into my branch to solve a merge conflict.

I also modifed pyabi.h to use _Py_PACK_VERSION(). I don't recall why I didn't do this change previously. I scheduled buildbot jobs to make sure that test_cext pass on buildbots. Some test_cext tests defines Py_TARGET_ABI3T: so I would like to make sure that everything is fine.

@vstinner

Copy link
Copy Markdown
Member Author

The test suite completed successfully on most buildbots: Linux, Windows, macOS, FreeBSD.


Some buildbots failed, for unrelated reasons.

AMD64 CentOS9 NoGIL Refleaks PR

ERROR: test_interrupt (test.test_multiprocessing_fork.test_processes.WithProcessesTestProcess.test_interrupt)
----------------------------------------------------------------------
  ...
  File "/home/buildbot/buildarea/pull_request.itamaro-centos-aws.refleak.nogil/build/Lib/test/_test_multiprocessing.py", line 628, in handler
    raise RuntimeError(f'join took longer than {timeout} seconds: {p}')
RuntimeError: join took longer than 67.5 seconds: <Process name='Process-563' pid=2855562 parent=2849207 started daemon>

AMD64 FreeBSD16 PR

FAIL: test_compute_rollover_W6_local (test.test_logging.TimedRotatingFileHandlerTest.test_compute_rollover_W6_local)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/buildbot/buildarea/pull_request.opsec-fbsd16/build/Lib/test/support/__init__.py", line 1256, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/pull_request.opsec-fbsd16/build/Lib/test/test_logging.py", line 7382, in test_compute_rollover_W6_local
    test(DT(2012, 3, 4, 23, 59, 59), DT(2012, 3, 5, 0, 0))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/pull_request.opsec-fbsd16/build/Lib/test/test_logging.py", line 7377, in test
    self.assertEqual(diff, 0, datetime.timedelta(seconds=diff))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 172800.0 != 0 : 2 days, 0:00:00

AMD64 Windows PGO NoGIL PR

A buildbot exception occurred, logs are empty.

buildbot/AMD64 Alpine Linux NoGIL PR — Build done.
buildbot/AMD64 Alpine Linux PR — Build done.

Known failure: 2 tests failed: test_lazy_import test_profiling.

@vstinner
vstinner merged commit b4e8f2a into python:main Nov 23, 2026
126 of 133 checks passed
@vstinner
vstinner deleted the use_pack_version branch November 23, 2026 20:31
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review @ZeroIntensity.

I restricted the scope of this change to leave versions checks on Python version 3.2 to 3.9 unchanged. Only checks on Python version 3.10 to 3.16 are updated to use _Py_PACK_VERSION().

But it's the kind of thing that makes backports annoying.

I can backport the change to 3.15, but I'm not sure about doing it. For now, I prefer to not backport, and see how it goes in main. I can backport it later if it becomes too annoying to backport C API changes. The change can be generated using shell commands giving the PR description.

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.

5 participants