gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293)

Changing the values requires forking and patching, which is intentional. Simply rebuilding from source does not change the implementation enough to justify changing these values - they would still be `cpython` and compatible with existing `.pyc` files. But people who maintain forks are better served by being able to easily override these values in a place that can be forward-ported reliably.
This commit is contained in:
Steve Dower
2026-02-10 00:01:17 +00:00
committed by GitHub
parent 30cfe6ee23
commit a9b6788ae6
29 changed files with 305 additions and 189 deletions
+8
View File
@@ -165,6 +165,14 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
stripdir = os.fspath(stripdir) if stripdir is not None else None
name = os.path.basename(fullname)
# Without a cache_tag, we can only create legacy .pyc files. None of our
# callers seem to expect this, so the best we can do is fail without raising
if not legacy and sys.implementation.cache_tag is None:
if not quiet:
print("No cache tag is available to generate .pyc path for",
repr(fullname))
return False
dfile = None
if ddir is not None:
+2
View File
@@ -177,6 +177,8 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
args += ["--user"]
if verbosity:
args += ["-" + "v" * verbosity]
if sys.implementation.cache_tag is None:
args += ["--no-compile"]
return _run_pip([*args, "pip"], [os.fsdecode(tmp_wheel_path)])
+3 -1
View File
@@ -194,8 +194,10 @@ def main():
else:
filenames = args.filenames
for filename in filenames:
cfilename = (None if sys.implementation.cache_tag
else f"{filename.rpartition('.')[0]}.pyc")
try:
compile(filename, doraise=True)
compile(filename, cfilename, doraise=True)
except PyCompileError as error:
if args.quiet:
parser.exit(1)
+17 -5
View File
@@ -4,6 +4,7 @@ import importlib
import importlib.machinery
import importlib.util
import os
import py_compile
import shutil
import sys
import textwrap
@@ -49,20 +50,31 @@ def forget(modname):
# combinations of PEP 3147/488 and legacy pyc files.
unlink(source + 'c')
for opt in ('', 1, 2):
unlink(importlib.util.cache_from_source(source, optimization=opt))
try:
unlink(importlib.util.cache_from_source(source, optimization=opt))
except NotImplementedError:
pass
def make_legacy_pyc(source):
def make_legacy_pyc(source, allow_compile=False):
"""Move a PEP 3147/488 pyc file to its legacy pyc location.
:param source: The file system path to the source file. The source file
does not need to exist, however the PEP 3147/488 pyc file must exist.
does not need to exist, however the PEP 3147/488 pyc file must exist or
allow_compile must be set.
:param allow_compile: If True, uses py_compile to create a .pyc if it does
not exist. This should be passed as True if cache_tag may be None.
:return: The file system path to the legacy pyc file.
"""
pyc_file = importlib.util.cache_from_source(source)
assert source.endswith('.py')
legacy_pyc = source + 'c'
shutil.move(pyc_file, legacy_pyc)
try:
pyc_file = importlib.util.cache_from_source(source)
shutil.move(pyc_file, legacy_pyc)
except (FileNotFoundError, NotImplementedError):
if not allow_compile:
raise
py_compile.compile(source, legacy_pyc, doraise=True)
return legacy_pyc
+1 -3
View File
@@ -7,7 +7,6 @@ import inspect
import io
import operator
import os
import py_compile
import shutil
import stat
import sys
@@ -7162,9 +7161,8 @@ class TestProgName(TestCase):
script_name = script_helper.make_script(dirname, basename, self.source)
if not compiled:
return script_name
py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
return pyc_file
def make_zip_script(self, script_name, name_in_zip=None):
+4 -1
View File
@@ -289,7 +289,10 @@ class ImportTests(unittest.TestCase):
self.check_executecodemodule(execute_code_func, NULL, pathname)
# Test NULL pathname and non-NULL cpathname
pyc_filename = importlib.util.cache_from_source(__file__)
try:
pyc_filename = importlib.util.cache_from_source(__file__)
except NotImplementedError:
return
py_filename = importlib.util.source_from_cache(pyc_filename)
origin = self.check_executecodemodule(execute_code_func, NULL, pyc_filename)
if not object:
+9 -12
View File
@@ -240,9 +240,8 @@ class CmdLineTest(unittest.TestCase):
def test_script_compiled(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script')
py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
self._check_script(pyc_file, pyc_file,
pyc_file, script_dir, None,
importlib.machinery.SourcelessFileLoader)
@@ -257,9 +256,8 @@ class CmdLineTest(unittest.TestCase):
def test_directory_compiled(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__')
py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
self._check_script(script_dir, pyc_file, script_dir,
script_dir, '',
importlib.machinery.SourcelessFileLoader)
@@ -279,8 +277,8 @@ class CmdLineTest(unittest.TestCase):
def test_zipfile_compiled_timestamp(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__')
compiled_name = py_compile.compile(
script_name, doraise=True,
compiled_name = script_name + 'c'
py_compile.compile(script_name, compiled_name, doraise=True,
invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -289,8 +287,8 @@ class CmdLineTest(unittest.TestCase):
def test_zipfile_compiled_checked_hash(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__')
compiled_name = py_compile.compile(
script_name, doraise=True,
compiled_name = script_name + 'c'
py_compile.compile(script_name, compiled_name, doraise=True,
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -299,8 +297,8 @@ class CmdLineTest(unittest.TestCase):
def test_zipfile_compiled_unchecked_hash(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__')
compiled_name = py_compile.compile(
script_name, doraise=True,
compiled_name = script_name + 'c'
py_compile.compile(script_name, compiled_name, doraise=True,
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH)
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -353,9 +351,8 @@ class CmdLineTest(unittest.TestCase):
pkg_dir = os.path.join(script_dir, 'test_pkg')
make_pkg(pkg_dir)
script_name = _make_test_script(pkg_dir, '__main__')
compiled_name = py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
self._check_script(["-m", "test_pkg"], pyc_file,
pyc_file, script_dir, 'test_pkg',
importlib.machinery.SourcelessFileLoader,
+4
View File
@@ -33,6 +33,10 @@ from test.test_py_compile import SourceDateEpochTestMeta
from test.support.os_helper import FakePath
if sys.implementation.cache_tag is None:
raise unittest.SkipTest('requires sys.implementation.cache_tag is not None')
def get_pyc(script, opt):
if not opt:
# Replace None and 0 with ''
+14 -8
View File
@@ -12,6 +12,12 @@ import ensurepip
import ensurepip._uninstall
if sys.implementation.cache_tag is None:
COMPILE_OPT = ["--no-compile"]
else:
COMPILE_OPT = []
class TestPackages(unittest.TestCase):
def touch(self, directory, filename):
fullname = os.path.join(directory, filename)
@@ -85,7 +91,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "pip",
unittest.mock.ANY, *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -99,7 +105,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "--root", "/foo/bar/",
unittest.mock.ANY, "--root", "/foo/bar/", *COMPILE_OPT,
"pip",
],
unittest.mock.ANY,
@@ -111,7 +117,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "--user", "pip",
unittest.mock.ANY, "--user", *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -122,7 +128,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "--upgrade", "pip",
unittest.mock.ANY, "--upgrade", *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -133,7 +139,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "-v", "pip",
unittest.mock.ANY, "-v", *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -144,7 +150,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "-vv", "pip",
unittest.mock.ANY, "-vv", *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -155,7 +161,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "-vvv", "pip",
unittest.mock.ANY, "-vvv", *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
@@ -312,7 +318,7 @@ class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "pip",
unittest.mock.ANY, *COMPILE_OPT, "pip",
],
unittest.mock.ANY,
)
+18 -10
View File
@@ -77,7 +77,7 @@ except ImportError:
skip_if_dont_write_bytecode = unittest.skipIf(
sys.dont_write_bytecode,
sys.dont_write_bytecode or sys.implementation.cache_tag is None,
"test meaningful only when writing bytecode")
@@ -505,7 +505,7 @@ class ImportTests(unittest.TestCase):
try:
# Compile & remove .py file; we only need .pyc.
# Bytecode must be relocated from the PEP 3147 bytecode-only location.
py_compile.compile(filename)
make_legacy_pyc(filename, allow_compile=True)
finally:
unlink(filename)
@@ -515,7 +515,6 @@ class ImportTests(unittest.TestCase):
namespace = {}
try:
make_legacy_pyc(filename)
# This used to crash.
exec('import ' + module, None, namespace)
finally:
@@ -1400,7 +1399,10 @@ func_filename = func.__code__.co_filename
"""
dir_name = os.path.abspath(TESTFN)
file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
compiled_name = importlib.util.cache_from_source(file_name)
try:
compiled_name = importlib.util.cache_from_source(file_name)
except NotImplementedError:
compiled_name = None
def setUp(self):
self.sys_path = sys.path[:]
@@ -1418,7 +1420,8 @@ func_filename = func.__code__.co_filename
else:
unload(self.module_name)
unlink(self.file_name)
unlink(self.compiled_name)
if self.compiled_name:
unlink(self.compiled_name)
rmtree(self.dir_name)
def import_module(self):
@@ -1437,6 +1440,8 @@ func_filename = func.__code__.co_filename
self.assertEqual(mod.code_filename, self.file_name)
self.assertEqual(mod.func_filename, self.file_name)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag is not None')
def test_incorrect_code_name(self):
py_compile.compile(self.file_name, dfile="another_module.py")
mod = self.import_module()
@@ -1446,9 +1451,9 @@ func_filename = func.__code__.co_filename
def test_module_without_source(self):
target = "another_module.py"
py_compile.compile(self.file_name, dfile=target)
pyc_file = self.file_name + 'c'
py_compile.compile(self.file_name, pyc_file, dfile=target)
os.remove(self.file_name)
pyc_file = make_legacy_pyc(self.file_name)
importlib.invalidate_caches()
mod = self.import_module()
self.assertEqual(mod.module_filename, pyc_file)
@@ -1456,8 +1461,9 @@ func_filename = func.__code__.co_filename
self.assertEqual(mod.func_filename, target)
def test_foreign_code(self):
py_compile.compile(self.file_name)
with open(self.compiled_name, "rb") as f:
compiled_name = self.compiled_name or (self.file_name + 'c')
py_compile.compile(self.file_name, compiled_name)
with open(compiled_name, "rb") as f:
header = f.read(16)
code = marshal.load(f)
constants = list(code.co_consts)
@@ -1465,9 +1471,11 @@ func_filename = func.__code__.co_filename
pos = constants.index(1000)
constants[pos] = foreign_code
code = code.replace(co_consts=tuple(constants))
with open(self.compiled_name, "wb") as f:
with open(compiled_name, "wb") as f:
f.write(header)
marshal.dump(code, f)
if not self.compiled_name:
os.remove(self.file_name)
mod = self.import_module()
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
@@ -213,12 +213,21 @@ class BadBytecodeTest:
del sys.modules['_temp']
except KeyError:
pass
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
if not del_source:
bytecode_path = self.util.cache_from_source(mapping[name])
if sys.implementation.cache_tag is None:
if del_source:
bytecode_path = mapping[name] + 'c'
py_compile.compile(mapping[name], bytecode_path,
invalidation_mode=invalidation_mode)
os.unlink(mapping[name])
else:
raise unittest.SkipTest('requires sys.implementation.cache_tag')
else:
os.unlink(mapping[name])
bytecode_path = make_legacy_pyc(mapping[name])
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
if not del_source:
bytecode_path = self.util.cache_from_source(mapping[name])
else:
os.unlink(mapping[name])
bytecode_path = make_legacy_pyc(mapping[name])
if manipulator:
with open(bytecode_path, 'rb') as file:
bc = file.read()
@@ -57,6 +57,8 @@ class FinderTests(abc.FinderTests):
"""
if create is None:
create = {test}
if (compile_ or unlink) and sys.implementation.cache_tag is None:
raise unittest.SkipTest('requires sys.implementation.cache_tag')
with util.create_modules(*create) as mapping:
if compile_:
for name in compile_:
+10 -2
View File
@@ -533,7 +533,10 @@ class SourceLoader(SourceOnlyLoader):
def __init__(self, path, magic=None):
super().__init__(path)
self.bytecode_path = self.util.cache_from_source(self.path)
try:
self.bytecode_path = self.util.cache_from_source(self.path)
except NotImplementedError:
self.bytecode_path = None
self.source_size = len(self.source)
if magic is None:
magic = self.util.MAGIC_NUMBER
@@ -579,7 +582,10 @@ class SourceLoaderTestHarness:
module_name = 'mod'
self.path = os.path.join(self.package, '.'.join(['mod', 'py']))
self.name = '.'.join([self.package, module_name])
self.cached = self.util.cache_from_source(self.path)
try:
self.cached = self.util.cache_from_source(self.path)
except NotImplementedError:
self.cached = None
self.loader = self.loader_mock(self.path, **kwargs)
def verify_module(self, module):
@@ -656,6 +662,8 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true")
@unittest.skipIf(sys.implementation.cache_tag is None,
"sys.implementation.cache_tag is None")
class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
"""Test importlib.abc.SourceLoader's use of bytecode.
-4
View File
@@ -231,7 +231,6 @@ class ReloadTests:
# Start as a plain module.
self.init.invalidate_caches()
path = os.path.join(cwd, name + '.py')
cached = self.util.cache_from_source(path)
expected = {'__name__': name,
'__package__': '',
'__file__': path,
@@ -251,7 +250,6 @@ class ReloadTests:
# Change to a package.
self.init.invalidate_caches()
init_path = os.path.join(cwd, name, '__init__.py')
cached = self.util.cache_from_source(init_path)
expected = {'__name__': name,
'__package__': name,
'__file__': init_path,
@@ -281,7 +279,6 @@ class ReloadTests:
# Start as a namespace package.
self.init.invalidate_caches()
bad_path = os.path.join(cwd, name, '__init.py')
cached = self.util.cache_from_source(bad_path)
expected = {'__name__': name,
'__package__': name,
'__doc__': None,
@@ -310,7 +307,6 @@ class ReloadTests:
# Change to a regular package.
self.init.invalidate_caches()
init_path = os.path.join(cwd, name, '__init__.py')
cached = self.util.cache_from_source(init_path)
expected = {'__name__': name,
'__package__': name,
'__file__': init_path,
+6 -3
View File
@@ -39,9 +39,12 @@ class TestImport(unittest.TestCase):
self.remove_modules()
def rewrite_file(self, contents):
compiled_path = cache_from_source(self.module_path)
if os.path.exists(compiled_path):
os.remove(compiled_path)
try:
compiled_path = cache_from_source(self.module_path)
if os.path.exists(compiled_path):
os.remove(compiled_path)
except NotImplementedError:
pass
with open(self.module_path, 'w', encoding='utf-8') as f:
f.write(contents)
+14 -3
View File
@@ -52,7 +52,10 @@ class ModuleSpecTests:
def setUp(self):
self.name = 'spam'
self.path = 'spam.py'
self.cached = self.util.cache_from_source(self.path)
try:
self.cached = self.util.cache_from_source(self.path)
except NotImplementedError:
self.cached = None
self.loader = TestLoader()
self.spec = self.machinery.ModuleSpec(self.name, self.loader)
self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader,
@@ -184,6 +187,8 @@ class ModuleSpecTests:
self.assertIs(spec.cached, None)
@unittest.skipIf(sys.implementation.cache_tag is None,
"sys.implementation.cache_tag is None")
def test_cached_source(self):
expected = self.util.cache_from_source(self.path)
@@ -224,7 +229,10 @@ class ModuleSpecMethodsTests:
def setUp(self):
self.name = 'spam'
self.path = 'spam.py'
self.cached = self.util.cache_from_source(self.path)
try:
self.cached = self.util.cache_from_source(self.path)
except NotImplementedError:
self.cached = None
self.loader = TestLoader()
self.spec = self.machinery.ModuleSpec(self.name, self.loader)
self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader,
@@ -349,7 +357,10 @@ class FactoryTests:
def setUp(self):
self.name = 'spam'
self.path = os.path.abspath('spam.py')
self.cached = self.util.cache_from_source(self.path)
try:
self.cached = self.util.cache_from_source(self.path)
except NotImplementedError:
self.cached = None
self.loader = TestLoader()
self.fileloader = TestLoader(self.path)
self.pkgloader = TestLoader(self.path, True)
+30
View File
@@ -345,6 +345,8 @@ class PEP3147Tests:
with self.assertRaises(NotImplementedError):
self.util.cache_from_source('whatever.py')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_no_dot(self):
# Directory with a dot, filename without dot.
path = os.path.join('foo.bar', 'file')
@@ -353,12 +355,16 @@ class PEP3147Tests:
self.assertEqual(self.util.cache_from_source(path, optimization=''),
expect)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_cwd(self):
path = 'foo.py'
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
self.assertEqual(self.util.cache_from_source(path, optimization=''),
expect)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_optimization_empty_string(self):
# Setting 'optimization' to '' leads to no optimization tag (PEP 488).
path = 'foo.py'
@@ -366,6 +372,8 @@ class PEP3147Tests:
self.assertEqual(self.util.cache_from_source(path, optimization=''),
expect)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_optimization_None(self):
# Setting 'optimization' to None uses the interpreter's optimization.
# (PEP 488)
@@ -382,6 +390,8 @@ class PEP3147Tests:
self.assertEqual(self.util.cache_from_source(path, optimization=None),
expect)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_optimization_set(self):
# The 'optimization' parameter accepts anything that has a string repr
# that passes str.alnum().
@@ -399,6 +409,8 @@ class PEP3147Tests:
with self.assertRaises(ValueError):
self.util.cache_from_source(path, optimization='path/is/bad')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_cache_from_source_debug_override_optimization_both_set(self):
# Can only set one of the optimization-related parameters.
with warnings.catch_warnings():
@@ -408,6 +420,8 @@ class PEP3147Tests:
@unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
'test meaningful only where os.altsep is defined')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_sep_altsep_and_sep_cache_from_source(self):
# Windows path and PEP 3147 where sep is right of altsep.
self.assertEqual(
@@ -440,44 +454,60 @@ class PEP3147Tests:
with self.assertRaises(NotImplementedError):
self.util.source_from_cache(path)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_bad_path(self):
# When the path to a pyc file is not in PEP 3147 format, a ValueError
# is raised.
self.assertRaises(
ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_no_slash(self):
# No slashes at all in path -> ValueError
self.assertRaises(
ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_too_few_dots(self):
# Too few dots in final path component -> ValueError
self.assertRaises(
ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_too_many_dots(self):
with self.assertRaises(ValueError):
self.util.source_from_cache(
'__pycache__/foo.cpython-32.opt-1.foo.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_not_opt(self):
# Non-`opt-` path component -> ValueError
self.assertRaises(
ValueError, self.util.source_from_cache,
'__pycache__/foo.cpython-32.foo.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_no__pycache__(self):
# Another problem with the path -> ValueError
self.assertRaises(
ValueError, self.util.source_from_cache,
'/foo/bar/foo.cpython-32.foo.pyc')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_optimized_bytecode(self):
# Optimized bytecode is not an issue.
path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag))
self.assertEqual(self.util.source_from_cache(path), 'foo.py')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag to not be None')
def test_source_from_cache_missing_optimization(self):
# An empty optimization level is a no-no.
path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag))
+3
View File
@@ -292,6 +292,9 @@ def writes_bytecode_files(fxn):
tests that require it to be set to False."""
if sys.dont_write_bytecode:
return unittest.skip("relies on writing bytecode")(fxn)
if sys.implementation.cache_tag is None:
return unittest.skip("requires sys.implementation.cache_tag to not be None")(fxn)
@functools.wraps(fxn)
def wrapper(*args, **kwargs):
original = sys.dont_write_bytecode
+2 -1
View File
@@ -6527,7 +6527,8 @@ class TestMain(unittest.TestCase):
self.assertIn(module.__name__, output)
self.assertIn(module.__spec__.origin, output)
self.assertIn(module.__file__, output)
self.assertIn(module.__spec__.cached, output)
if module.__spec__.cached:
self.assertIn(module.__spec__.cached, output)
self.assertEqual(err, b'')
@@ -196,9 +196,8 @@ class MultiProcessingCmdLineMixin():
def test_script_compiled(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script')
py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
self._check_script(pyc_file)
def test_directory(self):
@@ -213,9 +212,8 @@ class MultiProcessingCmdLineMixin():
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__',
source=source)
py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
self._check_script(script_dir)
def test_zipfile(self):
@@ -231,7 +229,8 @@ class MultiProcessingCmdLineMixin():
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, '__main__',
source=source)
compiled_name = py_compile.compile(script_name, doraise=True)
compiled_name = script_name + 'c'
py_compile.compile(script_name, compiled_name, doraise=True)
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
self._check_script(zip_name)
@@ -273,9 +272,8 @@ class MultiProcessingCmdLineMixin():
make_pkg(pkg_dir)
script_name = _make_test_script(pkg_dir, '__main__',
source=source)
compiled_name = py_compile.compile(script_name, doraise=True)
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
pyc_file = import_helper.make_legacy_pyc(script_name)
launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
self._check_script(launch_name)
+52 -16
View File
@@ -56,7 +56,10 @@ class PyCompileTestsBase:
self.directory = tempfile.mkdtemp(dir=os.getcwd())
self.source_path = os.path.join(self.directory, '_test.py')
self.pyc_path = self.source_path + 'c'
self.cache_path = importlib.util.cache_from_source(self.source_path)
try:
self.cache_path = importlib.util.cache_from_source(self.source_path)
except NotImplementedError:
self.cache_path = None
self.cwd_drive = os.path.splitdrive(os.getcwd())[0]
# In these tests we compute relative paths. When using Windows, the
# current working directory path and the 'self.source_path' might be
@@ -73,10 +76,31 @@ class PyCompileTestsBase:
if self.cwd_drive:
os.chdir(self.cwd_drive)
def assert_cache_path_exists(self, should_exist=True):
if self.cache_path:
if should_exist:
self.assertTrue(os.path.exists(self.cache_path))
else:
self.assertFalse(os.path.exists(self.cache_path))
return
cache_dir = os.path.join(self.directory, '__pycache__')
if not os.path.isdir(cache_dir):
if should_exist:
self.fail('no __pycache__ directory exists')
return
for f in os.listdir(cache_dir):
if f.startswith('_test.') and f.endswith('.pyc'):
if should_exist:
return
self.fail(f'__pycache__/{f} was created')
else:
if should_exist:
self.fail('no __pycache__/_test.*.pyc file exists')
def test_absolute_path(self):
py_compile.compile(self.source_path, self.pyc_path)
self.assertTrue(os.path.exists(self.pyc_path))
self.assertFalse(os.path.exists(self.cache_path))
self.assert_cache_path_exists(False)
def test_do_not_overwrite_symlinks(self):
# In the face of a cfile argument being a symlink, bail out.
@@ -98,22 +122,24 @@ class PyCompileTestsBase:
with self.assertRaises(FileExistsError):
py_compile.compile(self.source_path, os.devnull)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag is not None')
def test_cache_path(self):
py_compile.compile(self.source_path)
self.assertTrue(os.path.exists(self.cache_path))
self.assert_cache_path_exists(True)
def test_cwd(self):
with os_helper.change_cwd(self.directory):
py_compile.compile(os.path.basename(self.source_path),
os.path.basename(self.pyc_path))
self.assertTrue(os.path.exists(self.pyc_path))
self.assertFalse(os.path.exists(self.cache_path))
self.assert_cache_path_exists(False)
def test_relative_path(self):
py_compile.compile(os.path.relpath(self.source_path),
os.path.relpath(self.pyc_path))
self.assertTrue(os.path.exists(self.pyc_path))
self.assertFalse(os.path.exists(self.cache_path))
self.assert_cache_path_exists(False)
@os_helper.skip_if_dac_override
@unittest.skipIf(os.name == 'nt',
@@ -136,14 +162,14 @@ class PyCompileTestsBase:
'tokenizedata',
'bad_coding2.py')
with support.captured_stderr():
self.assertIsNone(py_compile.compile(bad_coding, doraise=False))
self.assertFalse(os.path.exists(
importlib.util.cache_from_source(bad_coding)))
self.assertIsNone(py_compile.compile(bad_coding, self.pyc_path,
doraise=False))
self.assertFalse(os.path.exists(self.pyc_path))
def test_source_date_epoch(self):
py_compile.compile(self.source_path, self.pyc_path)
self.assertTrue(os.path.exists(self.pyc_path))
self.assertFalse(os.path.exists(self.cache_path))
self.assert_cache_path_exists(False)
with open(self.pyc_path, 'rb') as fp:
flags = importlib._bootstrap_external._classify_pyc(
fp.read(), 'test', {})
@@ -155,6 +181,8 @@ class PyCompileTestsBase:
self.assertEqual(flags, expected_flags)
@unittest.skipIf(sys.flags.optimize > 0, 'test does not work with -O')
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag is not None')
def test_double_dot_no_clobber(self):
# http://bugs.python.org/issue22966
# py_compile foo.bar.py -> __pycache__/foo.cpython-34.pyc
@@ -174,6 +202,8 @@ class PyCompileTestsBase:
self.assertTrue(os.path.exists(cache_path))
self.assertFalse(os.path.exists(pyc_path))
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag is not None')
def test_optimization_path(self):
# Specifying optimized bytecode should lead to a path reflecting that.
self.assertIn('opt-2', py_compile.compile(self.source_path, optimize=2))
@@ -181,17 +211,19 @@ class PyCompileTestsBase:
def test_invalidation_mode(self):
py_compile.compile(
self.source_path,
self.pyc_path,
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
)
with open(self.cache_path, 'rb') as fp:
with open(self.pyc_path, 'rb') as fp:
flags = importlib._bootstrap_external._classify_pyc(
fp.read(), 'test', {})
self.assertEqual(flags, 0b11)
py_compile.compile(
self.source_path,
self.pyc_path,
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
)
with open(self.cache_path, 'rb') as fp:
with open(self.pyc_path, 'rb') as fp:
flags = importlib._bootstrap_external._classify_pyc(
fp.read(), 'test', {})
self.assertEqual(flags, 0b1)
@@ -201,11 +233,11 @@ class PyCompileTestsBase:
'tokenizedata',
'bad_coding2.py')
with support.captured_stderr() as stderr:
self.assertIsNone(py_compile.compile(bad_coding, doraise=False, quiet=2))
self.assertIsNone(py_compile.compile(bad_coding, doraise=True, quiet=2))
self.assertIsNone(py_compile.compile(bad_coding, self.pyc_path, doraise=False, quiet=2))
self.assertIsNone(py_compile.compile(bad_coding, self.pyc_path, doraise=True, quiet=2))
self.assertEqual(stderr.getvalue(), '')
with self.assertRaises(py_compile.PyCompileError):
py_compile.compile(bad_coding, doraise=True, quiet=1)
py_compile.compile(bad_coding, self.pyc_path, doraise=True, quiet=1)
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
@@ -227,8 +259,12 @@ class PyCompileCLITestCase(unittest.TestCase):
def setUp(self):
self.directory = tempfile.mkdtemp()
self.source_path = os.path.join(self.directory, '_test.py')
self.cache_path = importlib.util.cache_from_source(self.source_path,
optimization='' if __debug__ else 1)
try:
self.cache_path = importlib.util.cache_from_source(self.source_path,
optimization='' if __debug__ else 1)
except NotImplementedError:
# py_compile.main() assumes legacy pyc path if there is no cache_tag
self.cache_path = self.source_path + 'c'
with open(self.source_path, 'w') as file:
file.write('x = 123\n')
+4 -2
View File
@@ -1006,6 +1006,8 @@ class PydocDocTest(unittest.TestCase):
os = import_helper.import_fresh_module('os')
expected = os.__doc__.splitlines()[0]
filename = os.__spec__.cached
if not filename:
raise unittest.SkipTest('requires .pyc files')
synopsis = pydoc.synopsis(filename)
self.assertEqual(synopsis, expected)
@@ -1013,10 +1015,10 @@ class PydocDocTest(unittest.TestCase):
def test_synopsis_sourceless_empty_doc(self):
with os_helper.temp_cwd() as test_dir:
init_path = os.path.join(test_dir, 'foomod42.py')
cached_path = importlib.util.cache_from_source(init_path)
cached_path = init_path + 'c'
with open(init_path, 'w') as fobj:
fobj.write("foo = 1")
py_compile.compile(init_path)
py_compile.compile(init_path, cached_path)
synopsis = pydoc.synopsis(init_path, {})
self.assertIsNone(synopsis)
synopsis_cached = pydoc.synopsis(cached_path, {})
+5 -2
View File
@@ -695,8 +695,11 @@ class LongReprTest(unittest.TestCase):
source_path_len += 2 * (len(self.longname) + 1)
# a path separator + `module_name` + ".py"
source_path_len += len(module_name) + 1 + len(".py")
cached_path_len = (source_path_len +
len(importlib.util.cache_from_source("x.py")) - len("x.py"))
try:
cached_path_len = (source_path_len +
len(importlib.util.cache_from_source("x.py")) - len("x.py"))
except NotImplementedError:
cached_path_len = source_path_len
if os.name == 'nt' and cached_path_len >= 258:
# Under Windows, the max path len is 260 including C's terminating
# NUL character.
+16 -11
View File
@@ -320,14 +320,16 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin):
self.check_code_execution(create_ns, expected_ns)
importlib.invalidate_caches()
__import__(mod_name)
os.remove(mod_fname)
if not sys.dont_write_bytecode:
make_legacy_pyc(mod_fname)
make_legacy_pyc(mod_fname, allow_compile=True)
unload(mod_name) # In case loader caches paths
os.remove(mod_fname)
importlib.invalidate_caches()
if verbose > 1: print("Running from compiled:", mod_name)
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
self.check_code_execution(create_ns, expected_ns)
else:
os.remove(mod_fname)
finally:
self._del_pkg(pkg_dir)
if verbose > 1: print("Module executed successfully")
@@ -360,14 +362,16 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin):
self.check_code_execution(create_ns, expected_ns)
importlib.invalidate_caches()
__import__(mod_name)
os.remove(mod_fname)
if not sys.dont_write_bytecode:
make_legacy_pyc(mod_fname)
make_legacy_pyc(mod_fname, allow_compile=True)
unload(mod_name) # In case loader caches paths
os.remove(mod_fname)
if verbose > 1: print("Running from compiled:", pkg_name)
importlib.invalidate_caches()
self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
self.check_code_execution(create_ns, expected_ns)
else:
os.remove(mod_fname)
finally:
self._del_pkg(pkg_dir)
if verbose > 1: print("Package executed successfully")
@@ -420,7 +424,7 @@ from ..uncle.cousin import nephew
importlib.invalidate_caches()
__import__(mod_name)
os.remove(mod_fname)
if not sys.dont_write_bytecode:
if not sys.dont_write_bytecode and sys.implementation.cache_tag:
make_legacy_pyc(mod_fname)
unload(mod_name) # In case the loader caches paths
if verbose > 1: print("Running from compiled:", mod_name)
@@ -676,6 +680,8 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)
@unittest.skipIf(sys.implementation.cache_tag is None,
'requires sys.implementation.cache_tag')
def test_script_compiled(self):
with temp_dir() as script_dir:
mod_name = 'script'
@@ -696,12 +702,10 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
with temp_dir() as script_dir:
mod_name = '__main__'
script_name = self._make_test_script(script_dir, mod_name)
compiled_name = py_compile.compile(script_name, doraise=True)
legacy_pyc = make_legacy_pyc(script_name, allow_compile=True)
os.remove(script_name)
if not sys.dont_write_bytecode:
legacy_pyc = make_legacy_pyc(script_name)
self._check_script(script_dir, "<run_path>", legacy_pyc,
script_dir, mod_name=mod_name)
self._check_script(script_dir, "<run_path>", legacy_pyc,
script_dir, mod_name=mod_name)
def test_directory_error(self):
with temp_dir() as script_dir:
@@ -722,7 +726,8 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
with temp_dir() as script_dir:
mod_name = '__main__'
script_name = self._make_test_script(script_dir, mod_name)
compiled_name = py_compile.compile(script_name, doraise=True)
compiled_name = script_name + 'c'
py_compile.compile(script_name, compiled_name, doraise=True)
zip_name, fname = make_zip_script(script_dir, 'test_zip',
compiled_name)
self._check_script(zip_name, "<run_path>", fname, zip_name,
-1
View File
@@ -60,7 +60,6 @@ TEMP_DIR = os.path.abspath("junk95142")
TEMP_ZIP = os.path.abspath("junk95142.zip")
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "zipimport_data")
pyc_file = importlib.util.cache_from_source(TESTMOD + '.py')
pyc_ext = '.pyc'
+39 -83
View File
@@ -2223,10 +2223,10 @@ class PyZipFile(ZipFile):
basename = name
if self.debug:
print("Adding package in", pathname, "as", basename)
fname, arcname = self._get_codename(initname[0:-3], basename)
arcname, bytecode = self._get_code(initname[0:-3], basename)
if self.debug:
print("Adding", arcname)
self.write(fname, arcname)
self.writestr(arcname, bytecode)
dirlist = sorted(os.listdir(pathname))
dirlist.remove("__init__.py")
# Add all *.py files and package subdirectories
@@ -2243,11 +2243,10 @@ class PyZipFile(ZipFile):
if self.debug:
print('file %r skipped by filterfunc' % path)
continue
fname, arcname = self._get_codename(path[0:-3],
basename)
arcname, bytecode = self._get_code(path[0:-3], basename)
if self.debug:
print("Adding", arcname)
self.write(fname, arcname)
self.writestr(arcname, bytecode)
else:
# This is NOT a package directory, add its files at top level
if self.debug:
@@ -2260,101 +2259,58 @@ class PyZipFile(ZipFile):
if self.debug:
print('file %r skipped by filterfunc' % path)
continue
fname, arcname = self._get_codename(path[0:-3],
basename)
arcname, bytecode = self._get_code(path[0:-3], basename)
if self.debug:
print("Adding", arcname)
self.write(fname, arcname)
self.writestr(arcname, bytecode)
else:
if pathname[-3:] != ".py":
raise RuntimeError(
'Files added with writepy() must end with ".py"')
fname, arcname = self._get_codename(pathname[0:-3], basename)
arcname, bytecode = self._get_code(pathname[0:-3], basename)
if self.debug:
print("Adding file", arcname)
self.write(fname, arcname)
self.writestr(arcname, bytecode)
def _get_codename(self, pathname, basename):
"""Return (filename, archivename) for the path.
def _get_code(self, pathname, basename):
"""Return (arcname, bytecode) for the path.
Given a module name path, return the correct file path and
archive name, compiling if necessary. For example, given
/python/lib/string, return (/python/lib/string.pyc, string).
Given a module name path, return the bytecode and archive
name. For example, given /python/lib/string, return
('string', b'<bytecode of string>').
"""
def _compile(file, optimize=-1):
import py_compile
if self.debug:
print("Compiling", file)
try:
py_compile.compile(file, doraise=True, optimize=optimize)
except py_compile.PyCompileError as err:
print(err.msg)
return False
return True
import importlib._bootstrap_external
import importlib.machinery
file_py = pathname + ".py"
file_pyc = pathname + ".pyc"
pycache_opt0 = importlib.util.cache_from_source(file_py, optimization='')
pycache_opt1 = importlib.util.cache_from_source(file_py, optimization=1)
pycache_opt2 = importlib.util.cache_from_source(file_py, optimization=2)
if self._optimize == -1:
# legacy mode: use whatever file is present
if (os.path.isfile(file_pyc) and
os.stat(file_pyc).st_mtime >= os.stat(file_py).st_mtime):
# Use .pyc file.
arcname = fname = file_pyc
elif (os.path.isfile(pycache_opt0) and
os.stat(pycache_opt0).st_mtime >= os.stat(file_py).st_mtime):
# Use the __pycache__/*.pyc file, but write it to the legacy pyc
# file name in the archive.
fname = pycache_opt0
arcname = file_pyc
elif (os.path.isfile(pycache_opt1) and
os.stat(pycache_opt1).st_mtime >= os.stat(file_py).st_mtime):
# Use the __pycache__/*.pyc file, but write it to the legacy pyc
# file name in the archive.
fname = pycache_opt1
arcname = file_pyc
elif (os.path.isfile(pycache_opt2) and
os.stat(pycache_opt2).st_mtime >= os.stat(file_py).st_mtime):
# Use the __pycache__/*.pyc file, but write it to the legacy pyc
# file name in the archive.
fname = pycache_opt2
arcname = file_pyc
else:
# Compile py into PEP 3147 pyc file.
if _compile(file_py):
if sys.flags.optimize == 0:
fname = pycache_opt0
elif sys.flags.optimize == 1:
fname = pycache_opt1
else:
fname = pycache_opt2
arcname = file_pyc
else:
fname = arcname = file_py
archivename = os.path.split(file_pyc)[1]
loader = importlib.machinery.SourceFileLoader('<py_compile>', file_py)
source_bytes = loader.get_data(file_py)
try:
if self.debug:
print("Compiling", file_py)
code = loader.source_to_code(source_bytes, archivename)
except Exception as err:
# Historically, this function prints messages here rather than raising
# (see test_zipfile.test_write_filtered_python_package)
from py_compile import PyCompileError
print(PyCompileError(type(err), err, file_py).msg)
archivename = os.path.split(file_py)[1]
bytecode = source_bytes
else:
# new mode: use given optimization level
if self._optimize == 0:
fname = pycache_opt0
arcname = file_pyc
else:
arcname = file_pyc
if self._optimize == 1:
fname = pycache_opt1
elif self._optimize == 2:
fname = pycache_opt2
else:
msg = "invalid value for 'optimize': {!r}".format(self._optimize)
raise ValueError(msg)
if not (os.path.isfile(fname) and
os.stat(fname).st_mtime >= os.stat(file_py).st_mtime):
if not _compile(file_py, optimize=self._optimize):
fname = arcname = file_py
archivename = os.path.split(arcname)[1]
# Historically this function has used timestamp comparisons, so we
# keep using it until someone makes that specific improvement.
source_stats = loader.path_stats(file_py)
bytecode = importlib._bootstrap_external._code_to_timestamp_pyc(
code, source_stats['mtime'], source_stats['size'])
if basename:
archivename = "%s/%s" % (basename, archivename)
return (fname, archivename)
return archivename, bytecode
def main(args=None):
@@ -0,0 +1,5 @@
Enables defining the ``_PY_IMPL_NAME`` and ``_PY_IMPL_CACHE_TAG`` preprocessor
definitions to override :data:`sys.implementation` at build time. Definitions
need to include quotes when setting to a string literal. Setting the cache tag
to ``NULL`` has the effect of completely disabling automatic creation and use of
``.pyc`` files.
+1 -1
View File
@@ -22,7 +22,7 @@ static PyObject *
pyimport_getmagictag(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
const char *tag = PyImport_GetMagicTag();
return PyUnicode_FromString(tag);
return tag ? PyUnicode_FromString(tag) : Py_NewRef(Py_None);
}
+17 -8
View File
@@ -3568,16 +3568,22 @@ make_version_info(PyThreadState *tstate)
}
/* sys.implementation values */
#define NAME "cpython"
const char *_PySys_ImplName = NAME;
#ifndef _PY_IMPL_NAME
#define _PY_IMPL_NAME "cpython"
#endif
const char *_PySys_ImplName = _PY_IMPL_NAME;
#ifndef _PY_IMPL_CACHE_TAG
#define MAJOR Py_STRINGIFY(PY_MAJOR_VERSION)
#define MINOR Py_STRINGIFY(PY_MINOR_VERSION)
#define TAG NAME "-" MAJOR MINOR
const char *_PySys_ImplCacheTag = TAG;
#undef NAME
#define _PY_IMPL_CACHE_TAG _PY_IMPL_NAME "-" MAJOR MINOR
#endif
const char *_PySys_ImplCacheTag = _PY_IMPL_CACHE_TAG;
#ifdef MAJOR
#undef MAJOR
#endif
#ifdef MINOR
#undef MINOR
#undef TAG
#endif
static PyObject *
make_impl_info(PyObject *version_info)
@@ -3599,9 +3605,12 @@ make_impl_info(PyObject *version_info)
if (res < 0)
goto error;
value = PyUnicode_FromString(_PySys_ImplCacheTag);
if (value == NULL)
value = _PySys_ImplCacheTag
? PyUnicode_FromString(_PySys_ImplCacheTag)
: Py_NewRef(Py_None);
if (value == NULL) {
goto error;
}
res = PyDict_SetItemString(impl_info, "cache_tag", value);
Py_DECREF(value);
if (res < 0)