Skip to content

Commit 59b1570

Browse files
miss-islingtonitamaroclaude
authored
[3.14] gh-136267: Read the target version from patchlevel.h in build-details.json (GH-161532) (#162302)
gh-136267: Read the target version from patchlevel.h in build-details.json (GH-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. (cherry picked from commit b09a7e9) Co-authored-by: Itamar Oren <itamarost@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 4ab8d5e commit 59b1570

3 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎Lib/test/test_build_details.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import sys
66
import sysconfig
77
import string
8+
import textwrap
89
import unittest
910
from pathlib import Path
1011
from unittest import mock
1112

12-
from test.support import is_android, is_apple_mobile, is_wasm32
13+
from test.support import is_android, is_apple_mobile, is_wasm32, os_helper
1314

1415
BASE_PATH = Path(
1516
__file__, # Lib/test/test_build_details.py
@@ -353,6 +354,44 @@ def test_no_dynamic_loading(self):
353354
data['implementation']['supports_isolated_interpreters'])
354355

355356

357+
@unittest.skipIf(
358+
generate_build_details is None,
359+
"Failed to import generate-build-details"
360+
)
361+
class BuildDetailsVersionTests(unittest.TestCase):
362+
def test_native(self):
363+
version_info = generate_build_details.get_version_info()
364+
self.assertEqual(version_info,
365+
generate_build_details.version_info_to_dict(
366+
sys.version_info))
367+
self.assertEqual(generate_build_details.hexversion(version_info),
368+
sys.hexversion)
369+
370+
def test_patchlevel(self):
371+
patchlevel_h = os.path.join(os_helper.TESTFN, 'patchlevel.h')
372+
self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
373+
os.mkdir(os_helper.TESTFN)
374+
with open(patchlevel_h, 'w', encoding='utf-8') as f:
375+
f.write(textwrap.dedent('''\
376+
#define PY_RELEASE_LEVEL_GAMMA 0xC /* For release candidates */
377+
#define PY_MAJOR_VERSION 3
378+
#define PY_MINOR_VERSION 99
379+
#define PY_MICRO_VERSION 7
380+
#define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_GAMMA
381+
#define PY_RELEASE_SERIAL 2
382+
#define PY_VERSION "3.99.7rc2"
383+
'''))
384+
with mock.patch.object(generate_build_details, 'PATCHLEVEL_H',
385+
patchlevel_h):
386+
version_info = generate_build_details.get_version_info()
387+
self.assertEqual(version_info, {
388+
'major': 3, 'minor': 99, 'micro': 7,
389+
'releaselevel': 'candidate', 'serial': 2,
390+
})
391+
self.assertEqual(generate_build_details.hexversion(version_info),
392+
0x036307c2)
393+
394+
356395
@unittest.skipIf(
357396
generate_build_details is None,
358397
"Failed to import generate-build-details"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``build-details.json`` now takes ``language.version_info``,
2+
``implementation.version`` and ``implementation.hexversion`` from
3+
:file:`Include/patchlevel.h` instead of the interpreter running the generator,
4+
which can be a different patch release under :option:`--with-build-python`.

‎Tools/build/generate-build-details.py‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import importlib.machinery
1111
import json
1212
import os
13+
import re
1314
import sys
1415
import sysconfig
1516

@@ -18,11 +19,52 @@
1819
from typing import Any
1920

2021

22+
PATCHLEVEL_H = os.path.join(
23+
os.path.dirname(__file__), '..', '..', 'Include', 'patchlevel.h',
24+
)
25+
RELEASE_LEVELS = {0xA: 'alpha', 0xB: 'beta', 0xC: 'candidate', 0xF: 'final'}
26+
27+
2128
def version_info_to_dict(obj: sys._version_info) -> dict[str, Any]:
2229
field_names = ('major', 'minor', 'micro', 'releaselevel', 'serial')
2330
return {field: getattr(obj, field) for field in field_names}
2431

2532

33+
def get_version_info() -> dict[str, Any]:
34+
"""Return the target's version as a version_info dict.
35+
36+
sys.version_info describes the running interpreter, which under
37+
--with-build-python can differ from the target in micro version and
38+
release level. Read Include/patchlevel.h instead when it is available.
39+
"""
40+
try:
41+
with open(PATCHLEVEL_H, encoding='utf-8') as f:
42+
patchlevel = f.read()
43+
except FileNotFoundError:
44+
return version_info_to_dict(sys.version_info)
45+
defines = dict(re.findall(r'^#define\s+(PY_\w+)\s+(\S+)', patchlevel, re.M))
46+
# PY_RELEASE_LEVEL is defined as another macro, e.g. PY_RELEASE_LEVEL_ALPHA.
47+
level = int(defines[defines['PY_RELEASE_LEVEL']], 16)
48+
return {
49+
'major': int(defines['PY_MAJOR_VERSION']),
50+
'minor': int(defines['PY_MINOR_VERSION']),
51+
'micro': int(defines['PY_MICRO_VERSION']),
52+
'releaselevel': RELEASE_LEVELS[level],
53+
'serial': int(defines['PY_RELEASE_SERIAL']),
54+
}
55+
56+
57+
def hexversion(version_info: dict[str, Any]) -> int:
58+
level = {name: value for value, name in RELEASE_LEVELS.items()}
59+
return int(
60+
version_info['major'] << 24
61+
| version_info['minor'] << 16
62+
| version_info['micro'] << 8
63+
| level[version_info['releaselevel']] << 4
64+
| version_info['serial']
65+
)
66+
67+
2668
def get_dict_key(container: dict[str, Any], key: str) -> dict[str, Any]:
2769
for part in key.split('.'):
2870
container = container[part]
@@ -87,11 +129,13 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]:
87129
)
88130
data['platform'] = sysconfig.get_platform()
89131

132+
version_info = get_version_info()
90133
data['language']['version'] = sysconfig.get_python_version()
91-
data['language']['version_info'] = version_info_to_dict(sys.version_info)
134+
data['language']['version_info'] = version_info
92135

93136
data['implementation'] = vars(sys.implementation).copy()
94-
data['implementation']['version'] = version_info_to_dict(sys.implementation.version)
137+
data['implementation']['version'] = version_info
138+
data['implementation']['hexversion'] = hexversion(version_info)
95139
# Fix cross-compilation
96140
if '_multiarch' in data['implementation']:
97141
data['implementation']['_multiarch'] = sysconfig.get_config_var('MULTIARCH')

0 commit comments

Comments
 (0)