https://github.com/python/cpython/commit/b09a7e92fb0ab958a52ad94e376402cdd3da09b2
commit: b09a7e92fb0ab958a52ad94e376402cdd3da09b2
branch: main
author: Itamar Oren <[email protected]>
committer: itamaro <[email protected]>
date: 2026-11-19T16:02:40-08:00
summary:

gh-136267: Read the target version from patchlevel.h in build-details.json 
(#161532)

With --with-build-python, the build Python can differ from the target in
patch version or release serial, but version_info and hexversion came from
the running interpreter. Read them from Include/patchlevel.h instead,
falling back to sys.version_info when the header is not there.

Co-authored-by: Claude Fable 5.1 <[email protected]>

files:
A Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst
M Lib/test/test_build_details.py
M Tools/build/generate-build-details.py

diff --git a/Lib/test/test_build_details.py b/Lib/test/test_build_details.py
index 552c7252c32d04..40bf1bef11a888 100644
--- a/Lib/test/test_build_details.py
+++ b/Lib/test/test_build_details.py
@@ -5,11 +5,12 @@
 import sys
 import sysconfig
 import string
+import textwrap
 import unittest
 from pathlib import Path
 from unittest import mock
 
-from test.support import is_android, is_apple_mobile, is_wasm32
+from test.support import is_android, is_apple_mobile, is_wasm32, os_helper
 
 BASE_PATH = Path(
     __file__,  # Lib/test/test_build_details.py
@@ -356,6 +357,44 @@ def test_no_dynamic_loading(self):
             data['implementation']['supports_isolated_interpreters'])
 
 
[email protected](
+    generate_build_details is None,
+    "Failed to import generate-build-details"
+)
+class BuildDetailsVersionTests(unittest.TestCase):
+    def test_native(self):
+        version_info = generate_build_details.get_version_info()
+        self.assertEqual(version_info,
+                         generate_build_details.version_info_to_dict(
+                             sys.version_info))
+        self.assertEqual(generate_build_details.hexversion(version_info),
+                         sys.hexversion)
+
+    def test_patchlevel(self):
+        patchlevel_h = os.path.join(os_helper.TESTFN, 'patchlevel.h')
+        self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
+        os.mkdir(os_helper.TESTFN)
+        with open(patchlevel_h, 'w', encoding='utf-8') as f:
+            f.write(textwrap.dedent('''\
+                #define PY_RELEASE_LEVEL_GAMMA  0xC     /* For release 
candidates */
+                #define PY_MAJOR_VERSION        3
+                #define PY_MINOR_VERSION        99
+                #define PY_MICRO_VERSION        7
+                #define PY_RELEASE_LEVEL        PY_RELEASE_LEVEL_GAMMA
+                #define PY_RELEASE_SERIAL       2
+                #define PY_VERSION              "3.99.7rc2"
+            '''))
+        with mock.patch.object(generate_build_details, 'PATCHLEVEL_H',
+                               patchlevel_h):
+            version_info = generate_build_details.get_version_info()
+        self.assertEqual(version_info, {
+            'major': 3, 'minor': 99, 'micro': 7,
+            'releaselevel': 'candidate', 'serial': 2,
+        })
+        self.assertEqual(generate_build_details.hexversion(version_info),
+                         0x036307c2)
+
+
 @unittest.skipIf(
     generate_build_details is None,
     "Failed to import generate-build-details"
diff --git 
a/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst 
b/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst
new file mode 100644
index 00000000000000..d99ae1c4213340
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst
@@ -0,0 +1,4 @@
+``build-details.json`` now takes ``language.version_info``,
+``implementation.version`` and ``implementation.hexversion`` from
+:file:`Include/patchlevel.h` instead of the interpreter running the generator,
+which can be a different patch release under :option:`--with-build-python`.
diff --git a/Tools/build/generate-build-details.py 
b/Tools/build/generate-build-details.py
index f6cb93beabec43..a6f537fc0d020d 100644
--- a/Tools/build/generate-build-details.py
+++ b/Tools/build/generate-build-details.py
@@ -10,6 +10,7 @@
 import importlib.machinery
 import json
 import os
+import re
 import sys
 import sysconfig
 
@@ -18,11 +19,52 @@
     from typing import Any
 
 
+PATCHLEVEL_H = os.path.join(
+    os.path.dirname(__file__), '..', '..', 'Include', 'patchlevel.h',
+)
+RELEASE_LEVELS = {0xA: 'alpha', 0xB: 'beta', 0xC: 'candidate', 0xF: 'final'}
+
+
 def version_info_to_dict(obj: sys._version_info) -> dict[str, Any]:
     field_names = ('major', 'minor', 'micro', 'releaselevel', 'serial')
     return {field: getattr(obj, field) for field in field_names}
 
 
+def get_version_info() -> dict[str, Any]:
+    """Return the target's version as a version_info dict.
+
+    sys.version_info describes the running interpreter, which under
+    --with-build-python can differ from the target in micro version and
+    release level. Read Include/patchlevel.h instead when it is available.
+    """
+    try:
+        with open(PATCHLEVEL_H, encoding='utf-8') as f:
+            patchlevel = f.read()
+    except FileNotFoundError:
+        return version_info_to_dict(sys.version_info)
+    defines = dict(re.findall(r'^#define\s+(PY_\w+)\s+(\S+)', patchlevel, 
re.M))
+    # PY_RELEASE_LEVEL is defined as another macro, e.g. 
PY_RELEASE_LEVEL_ALPHA.
+    level = int(defines[defines['PY_RELEASE_LEVEL']], 16)
+    return {
+        'major': int(defines['PY_MAJOR_VERSION']),
+        'minor': int(defines['PY_MINOR_VERSION']),
+        'micro': int(defines['PY_MICRO_VERSION']),
+        'releaselevel': RELEASE_LEVELS[level],
+        'serial': int(defines['PY_RELEASE_SERIAL']),
+    }
+
+
+def hexversion(version_info: dict[str, Any]) -> int:
+    level = {name: value for value, name in RELEASE_LEVELS.items()}
+    return int(
+        version_info['major'] << 24
+        | version_info['minor'] << 16
+        | version_info['micro'] << 8
+        | level[version_info['releaselevel']] << 4
+        | version_info['serial']
+    )
+
+
 def get_dict_key(container: dict[str, Any], key: str) -> dict[str, Any]:
     for part in key.split('.'):
         container = container[part]
@@ -94,11 +136,13 @@ def generate_data(schema_version: str) -> 
collections.defaultdict[str, Any]:
     )
     data['platform'] = sysconfig.get_platform()
 
+    version_info = get_version_info()
     data['language']['version'] = sysconfig.get_python_version()
-    data['language']['version_info'] = version_info_to_dict(sys.version_info)
+    data['language']['version_info'] = version_info
 
     data['implementation'] = vars(sys.implementation).copy()
-    data['implementation']['version'] = 
version_info_to_dict(sys.implementation.version)
+    data['implementation']['version'] = version_info
+    data['implementation']['hexversion'] = hexversion(version_info)
     # Fix cross-compilation
     if '_multiarch' in data['implementation']:
         data['implementation']['_multiarch'] = 
sysconfig.get_config_var('MULTIARCH')

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to