mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-135455: Fix version and architecture detection in PC/layout script. (GH-135461)
(cherry picked from commit afc5ab6cce)
Co-authored-by: Steve Dower <steve.dower@python.org>
This commit is contained in:
parent
c260ed15d1
commit
0430135aac
3 changed files with 95 additions and 12 deletions
|
|
@ -6,6 +6,8 @@
|
|||
__version__ = "3.8"
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
|
||||
|
|
@ -13,9 +15,15 @@
|
|||
def _unpack_hexversion():
|
||||
try:
|
||||
hexversion = int(os.getenv("PYTHON_HEXVERSION"), 16)
|
||||
return struct.pack(">i", hexversion)
|
||||
except (TypeError, ValueError):
|
||||
hexversion = sys.hexversion
|
||||
return struct.pack(">i", hexversion)
|
||||
pass
|
||||
if os.getenv("PYTHONINCLUDE"):
|
||||
try:
|
||||
return _read_patchlevel_version(pathlib.Path(os.getenv("PYTHONINCLUDE")))
|
||||
except OSError:
|
||||
pass
|
||||
return struct.pack(">i", sys.hexversion)
|
||||
|
||||
|
||||
def _get_suffix(field4):
|
||||
|
|
@ -26,6 +34,39 @@ def _get_suffix(field4):
|
|||
return ""
|
||||
|
||||
|
||||
def _read_patchlevel_version(sources):
|
||||
if not sources.match("Include"):
|
||||
sources /= "Include"
|
||||
values = {}
|
||||
with open(sources / "patchlevel.h", "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
m = re.match(r'#\s*define\s+(PY_\S+?)\s+(\S+)', line.strip(), re.I)
|
||||
if m and m.group(2):
|
||||
v = m.group(2)
|
||||
if v.startswith('"'):
|
||||
v = v[1:-1]
|
||||
else:
|
||||
v = values.get(v, v)
|
||||
if isinstance(v, str):
|
||||
try:
|
||||
v = int(v, 16 if v.startswith("0x") else 10)
|
||||
except ValueError:
|
||||
pass
|
||||
values[m.group(1)] = v
|
||||
return (
|
||||
values["PY_MAJOR_VERSION"],
|
||||
values["PY_MINOR_VERSION"],
|
||||
values["PY_MICRO_VERSION"],
|
||||
values["PY_RELEASE_LEVEL"] << 4 | values["PY_RELEASE_SERIAL"],
|
||||
)
|
||||
|
||||
|
||||
def check_patchlevel_version(sources):
|
||||
got = _read_patchlevel_version(sources)
|
||||
if got != (VER_MAJOR, VER_MINOR, VER_MICRO, VER_FIELD4):
|
||||
return f"{got[0]}.{got[1]}.{got[2]}{_get_suffix(got[3])}"
|
||||
|
||||
|
||||
VER_MAJOR, VER_MINOR, VER_MICRO, VER_FIELD4 = _unpack_hexversion()
|
||||
VER_SUFFIX = _get_suffix(VER_FIELD4)
|
||||
VER_FIELD3 = VER_MICRO << 8 | VER_FIELD4
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue