Skip to content

Commit 732c090

Browse files
committed
Helps if you commit the actual changes...
1 parent 1b9f373 commit 732c090

26 files changed

Lines changed: 270 additions & 174 deletions

Lib/py_compile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ def main():
194194
else:
195195
filenames = args.filenames
196196
for filename in filenames:
197+
cfilename = (None if sys.implementation.cache_tag
198+
else f"{filename.rpartition('.')[0]}.pyc")
197199
try:
198-
compile(filename, doraise=True)
200+
compile(filename, cfilename, doraise=True)
199201
except PyCompileError as error:
200202
if args.quiet:
201203
parser.exit(1)

Lib/test/support/import_helper.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import importlib.machinery
55
import importlib.util
66
import os
7+
import py_compile
78
import shutil
89
import sys
910
import textwrap
@@ -49,20 +50,30 @@ def forget(modname):
4950
# combinations of PEP 3147/488 and legacy pyc files.
5051
unlink(source + 'c')
5152
for opt in ('', 1, 2):
52-
unlink(importlib.util.cache_from_source(source, optimization=opt))
53+
try:
54+
unlink(importlib.util.cache_from_source(source, optimization=opt))
55+
except NotImplementedError:
56+
pass
5357

5458

55-
def make_legacy_pyc(source):
59+
def make_legacy_pyc(source, allow_compile=False):
5660
"""Move a PEP 3147/488 pyc file to its legacy pyc location.
5761
5862
:param source: The file system path to the source file. The source file
59-
does not need to exist, however the PEP 3147/488 pyc file must exist.
63+
does not need to exist, however the PEP 3147/488 pyc file must exist or
64+
allow_compile must be set.
65+
:param allow_compile: If True, uses py_compile to create a .pyc if it does
66+
not exist. This should be passed as True if cache_tag may be None.
6067
:return: The file system path to the legacy pyc file.
6168
"""
62-
pyc_file = importlib.util.cache_from_source(source)
6369
assert source.endswith('.py')
6470
legacy_pyc = source + 'c'
65-
shutil.move(pyc_file, legacy_pyc)
71+
try:
72+
pyc_file = importlib.util.cache_from_source(source)
73+
except NotImplementedError:
74+
py_compile.compile(source, legacy_pyc, doraise=True)
75+
else:
76+
shutil.move(pyc_file, legacy_pyc)
6677
return legacy_pyc
6778

6879

Lib/test/test_argparse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7162,9 +7162,8 @@ def make_script(self, dirname, basename, *, compiled=False):
71627162
script_name = script_helper.make_script(dirname, basename, self.source)
71637163
if not compiled:
71647164
return script_name
7165-
py_compile.compile(script_name, doraise=True)
7165+
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
71667166
os.remove(script_name)
7167-
pyc_file = import_helper.make_legacy_pyc(script_name)
71687167
return pyc_file
71697168

71707169
def make_zip_script(self, script_name, name_in_zip=None):

Lib/test/test_capi/test_import.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ def check_executecode_pathnames(self, execute_code_func, object=False):
289289
self.check_executecodemodule(execute_code_func, NULL, pathname)
290290

291291
# Test NULL pathname and non-NULL cpathname
292-
pyc_filename = importlib.util.cache_from_source(__file__)
292+
try:
293+
pyc_filename = importlib.util.cache_from_source(__file__)
294+
except NotImplementedError:
295+
return
293296
py_filename = importlib.util.source_from_cache(pyc_filename)
294297
origin = self.check_executecodemodule(execute_code_func, NULL, pyc_filename)
295298
if not object:

Lib/test/test_cmd_line_script.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ def test_script_abspath(self):
240240
def test_script_compiled(self):
241241
with os_helper.temp_dir() as script_dir:
242242
script_name = _make_test_script(script_dir, 'script')
243-
py_compile.compile(script_name, doraise=True)
243+
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
244244
os.remove(script_name)
245-
pyc_file = import_helper.make_legacy_pyc(script_name)
246245
self._check_script(pyc_file, pyc_file,
247246
pyc_file, script_dir, None,
248247
importlib.machinery.SourcelessFileLoader)
@@ -257,9 +256,8 @@ def test_directory(self):
257256
def test_directory_compiled(self):
258257
with os_helper.temp_dir() as script_dir:
259258
script_name = _make_test_script(script_dir, '__main__')
260-
py_compile.compile(script_name, doraise=True)
259+
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
261260
os.remove(script_name)
262-
pyc_file = import_helper.make_legacy_pyc(script_name)
263261
self._check_script(script_dir, pyc_file, script_dir,
264262
script_dir, '',
265263
importlib.machinery.SourcelessFileLoader)
@@ -279,8 +277,8 @@ def test_zipfile(self):
279277
def test_zipfile_compiled_timestamp(self):
280278
with os_helper.temp_dir() as script_dir:
281279
script_name = _make_test_script(script_dir, '__main__')
282-
compiled_name = py_compile.compile(
283-
script_name, doraise=True,
280+
compiled_name = script_name + 'c'
281+
py_compile.compile(script_name, compiled_name, doraise=True,
284282
invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
285283
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
286284
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -289,8 +287,8 @@ def test_zipfile_compiled_timestamp(self):
289287
def test_zipfile_compiled_checked_hash(self):
290288
with os_helper.temp_dir() as script_dir:
291289
script_name = _make_test_script(script_dir, '__main__')
292-
compiled_name = py_compile.compile(
293-
script_name, doraise=True,
290+
compiled_name = script_name + 'c'
291+
py_compile.compile(script_name, compiled_name, doraise=True,
294292
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
295293
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
296294
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -299,8 +297,8 @@ def test_zipfile_compiled_checked_hash(self):
299297
def test_zipfile_compiled_unchecked_hash(self):
300298
with os_helper.temp_dir() as script_dir:
301299
script_name = _make_test_script(script_dir, '__main__')
302-
compiled_name = py_compile.compile(
303-
script_name, doraise=True,
300+
compiled_name = script_name + 'c'
301+
py_compile.compile(script_name, compiled_name, doraise=True,
304302
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH)
305303
zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
306304
self._check_script(zip_name, run_name, zip_name, zip_name, '',
@@ -353,9 +351,8 @@ def test_package_compiled(self):
353351
pkg_dir = os.path.join(script_dir, 'test_pkg')
354352
make_pkg(pkg_dir)
355353
script_name = _make_test_script(pkg_dir, '__main__')
356-
compiled_name = py_compile.compile(script_name, doraise=True)
354+
pyc_file = import_helper.make_legacy_pyc(script_name, allow_compile=True)
357355
os.remove(script_name)
358-
pyc_file = import_helper.make_legacy_pyc(script_name)
359356
self._check_script(["-m", "test_pkg"], pyc_file,
360357
pyc_file, script_dir, 'test_pkg',
361358
importlib.machinery.SourcelessFileLoader,

Lib/test/test_compileall.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
from test.support.os_helper import FakePath
3434

3535

36+
if sys.implementation.cache_tag is None:
37+
raise unittest.SkipTest('requires sys.implementation.cache_tag is not None')
38+
39+
3640
def get_pyc(script, opt):
3741
if not opt:
3842
# Replace None and 0 with ''

Lib/test/test_import/__init__.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777

7878
skip_if_dont_write_bytecode = unittest.skipIf(
79-
sys.dont_write_bytecode,
79+
sys.dont_write_bytecode or sys.implementation.cache_tag is None,
8080
"test meaningful only when writing bytecode")
8181

8282

@@ -504,7 +504,7 @@ def test_module_with_large_stack(self, module='longlist'):
504504
try:
505505
# Compile & remove .py file; we only need .pyc.
506506
# Bytecode must be relocated from the PEP 3147 bytecode-only location.
507-
py_compile.compile(filename)
507+
make_legacy_pyc(filename, allow_compile=True)
508508
finally:
509509
unlink(filename)
510510

@@ -514,7 +514,6 @@ def test_module_with_large_stack(self, module='longlist'):
514514

515515
namespace = {}
516516
try:
517-
make_legacy_pyc(filename)
518517
# This used to crash.
519518
exec('import ' + module, None, namespace)
520519
finally:
@@ -1399,7 +1398,10 @@ def func():
13991398
"""
14001399
dir_name = os.path.abspath(TESTFN)
14011400
file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
1402-
compiled_name = importlib.util.cache_from_source(file_name)
1401+
try:
1402+
compiled_name = importlib.util.cache_from_source(file_name)
1403+
except NotImplementedError:
1404+
compiled_name = None
14031405

14041406
def setUp(self):
14051407
self.sys_path = sys.path[:]
@@ -1417,7 +1419,8 @@ def tearDown(self):
14171419
else:
14181420
unload(self.module_name)
14191421
unlink(self.file_name)
1420-
unlink(self.compiled_name)
1422+
if self.compiled_name:
1423+
unlink(self.compiled_name)
14211424
rmtree(self.dir_name)
14221425

14231426
def import_module(self):
@@ -1436,6 +1439,8 @@ def test_basics(self):
14361439
self.assertEqual(mod.code_filename, self.file_name)
14371440
self.assertEqual(mod.func_filename, self.file_name)
14381441

1442+
@unittest.skipIf(sys.implementation.cache_tag is None,
1443+
'requires sys.implementation.cache_tag is not None')
14391444
def test_incorrect_code_name(self):
14401445
py_compile.compile(self.file_name, dfile="another_module.py")
14411446
mod = self.import_module()
@@ -1445,28 +1450,31 @@ def test_incorrect_code_name(self):
14451450

14461451
def test_module_without_source(self):
14471452
target = "another_module.py"
1448-
py_compile.compile(self.file_name, dfile=target)
1453+
pyc_file = self.file_name + 'c'
1454+
py_compile.compile(self.file_name, pyc_file, dfile=target)
14491455
os.remove(self.file_name)
1450-
pyc_file = make_legacy_pyc(self.file_name)
14511456
importlib.invalidate_caches()
14521457
mod = self.import_module()
14531458
self.assertEqual(mod.module_filename, pyc_file)
14541459
self.assertEqual(mod.code_filename, target)
14551460
self.assertEqual(mod.func_filename, target)
14561461

14571462
def test_foreign_code(self):
1458-
py_compile.compile(self.file_name)
1459-
with open(self.compiled_name, "rb") as f:
1463+
compiled_name = self.compiled_name or (self.file_name + 'c')
1464+
py_compile.compile(self.file_name, compiled_name)
1465+
with open(compiled_name, "rb") as f:
14601466
header = f.read(16)
14611467
code = marshal.load(f)
14621468
constants = list(code.co_consts)
14631469
foreign_code = importlib.import_module.__code__
14641470
pos = constants.index(1000)
14651471
constants[pos] = foreign_code
14661472
code = code.replace(co_consts=tuple(constants))
1467-
with open(self.compiled_name, "wb") as f:
1473+
with open(compiled_name, "wb") as f:
14681474
f.write(header)
14691475
marshal.dump(code, f)
1476+
if not self.compiled_name:
1477+
os.remove(self.file_name)
14701478
mod = self.import_module()
14711479
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
14721480

Lib/test/test_importlib/source/test_file_loader.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,21 @@ def manipulate_bytecode(self,
213213
del sys.modules['_temp']
214214
except KeyError:
215215
pass
216-
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
217-
if not del_source:
218-
bytecode_path = self.util.cache_from_source(mapping[name])
216+
if sys.implementation.cache_tag is None:
217+
if del_source:
218+
bytecode_path = mapping[name] + 'c'
219+
py_compile.compile(mapping[name], bytecode_path,
220+
invalidation_mode=invalidation_mode)
221+
os.unlink(mapping[name])
222+
else:
223+
raise unittest.SkipTest('requires sys.implementation.cache_tag')
219224
else:
220-
os.unlink(mapping[name])
221-
bytecode_path = make_legacy_pyc(mapping[name])
225+
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
226+
if not del_source:
227+
bytecode_path = self.util.cache_from_source(mapping[name])
228+
else:
229+
os.unlink(mapping[name])
230+
bytecode_path = make_legacy_pyc(mapping[name])
222231
if manipulator:
223232
with open(bytecode_path, 'rb') as file:
224233
bc = file.read()

Lib/test/test_importlib/source/test_finder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def run_test(self, test, create=None, *, compile_=None, unlink=None):
5757
"""
5858
if create is None:
5959
create = {test}
60+
if (compile_ or unlink) and sys.implementation.cache_tag is None:
61+
raise unittest.SkipTest('requires sys.implementation.cache_tag')
6062
with util.create_modules(*create) as mapping:
6163
if compile_:
6264
for name in compile_:

Lib/test/test_importlib/test_abc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ class SourceLoader(SourceOnlyLoader):
533533

534534
def __init__(self, path, magic=None):
535535
super().__init__(path)
536-
self.bytecode_path = self.util.cache_from_source(self.path)
536+
try:
537+
self.bytecode_path = self.util.cache_from_source(self.path)
538+
except NotImplementedError:
539+
self.bytecode_path = None
537540
self.source_size = len(self.source)
538541
if magic is None:
539542
magic = self.util.MAGIC_NUMBER
@@ -579,7 +582,10 @@ def setUp(self, *, is_package=True, **kwargs):
579582
module_name = 'mod'
580583
self.path = os.path.join(self.package, '.'.join(['mod', 'py']))
581584
self.name = '.'.join([self.package, module_name])
582-
self.cached = self.util.cache_from_source(self.path)
585+
try:
586+
self.cached = self.util.cache_from_source(self.path)
587+
except NotImplementedError:
588+
self.cached = None
583589
self.loader = self.loader_mock(self.path, **kwargs)
584590

585591
def verify_module(self, module):
@@ -656,6 +662,8 @@ def test_get_source_encoding(self):
656662

657663

658664
@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true")
665+
@unittest.skipIf(sys.implementation.cache_tag is None,
666+
"sys.implementation.cache_tag is None")
659667
class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
660668

661669
"""Test importlib.abc.SourceLoader's use of bytecode.

0 commit comments

Comments
 (0)