2018-10-10 12:43:14 -04:00
|
|
|
import functools
|
2013-06-15 14:07:21 -04:00
|
|
|
import importlib.util
|
2011-11-25 23:36:48 -06:00
|
|
|
import os
|
|
|
|
import py_compile
|
|
|
|
import shutil
|
2013-04-14 12:48:15 -04:00
|
|
|
import stat
|
2020-07-25 22:58:45 +03:00
|
|
|
import subprocess
|
2014-12-01 18:15:26 -05:00
|
|
|
import sys
|
2011-11-25 23:36:48 -06:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2014-09-01 12:33:12 +03:00
|
|
|
from test import support
|
2020-07-25 22:58:45 +03:00
|
|
|
from test.support import os_helper, script_helper
|
2014-09-01 12:33:12 +03:00
|
|
|
|
2011-11-25 23:36:48 -06:00
|
|
|
|
2018-10-10 12:43:14 -04:00
|
|
|
def without_source_date_epoch(fxn):
|
|
|
|
"""Runs function with SOURCE_DATE_EPOCH unset."""
|
|
|
|
@functools.wraps(fxn)
|
|
|
|
def wrapper(*args, **kwargs):
|
2020-07-06 17:12:49 +08:00
|
|
|
with os_helper.EnvironmentVarGuard() as env:
|
2018-10-10 12:43:14 -04:00
|
|
|
env.unset('SOURCE_DATE_EPOCH')
|
|
|
|
return fxn(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def with_source_date_epoch(fxn):
|
|
|
|
"""Runs function with SOURCE_DATE_EPOCH set."""
|
|
|
|
@functools.wraps(fxn)
|
|
|
|
def wrapper(*args, **kwargs):
|
2020-07-06 17:12:49 +08:00
|
|
|
with os_helper.EnvironmentVarGuard() as env:
|
2018-10-10 12:43:14 -04:00
|
|
|
env['SOURCE_DATE_EPOCH'] = '123456789'
|
|
|
|
return fxn(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
# Run tests with SOURCE_DATE_EPOCH set or unset explicitly.
|
|
|
|
class SourceDateEpochTestMeta(type(unittest.TestCase)):
|
|
|
|
def __new__(mcls, name, bases, dct, *, source_date_epoch):
|
|
|
|
cls = super().__new__(mcls, name, bases, dct)
|
|
|
|
|
|
|
|
for attr in dir(cls):
|
|
|
|
if attr.startswith('test_'):
|
|
|
|
meth = getattr(cls, attr)
|
|
|
|
if source_date_epoch:
|
|
|
|
wrapper = with_source_date_epoch(meth)
|
|
|
|
else:
|
|
|
|
wrapper = without_source_date_epoch(meth)
|
|
|
|
setattr(cls, attr, wrapper)
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
|
|
|
class PyCompileTestsBase:
|
2011-11-25 23:36:48 -06:00
|
|
|
|
|
|
|
def setUp(self):
|
2019-12-16 10:35:22 -08:00
|
|
|
self.directory = tempfile.mkdtemp(dir=os.getcwd())
|
2011-11-25 23:36:48 -06:00
|
|
|
self.source_path = os.path.join(self.directory, '_test.py')
|
|
|
|
self.pyc_path = self.source_path + 'c'
|
2013-06-15 14:07:21 -04:00
|
|
|
self.cache_path = importlib.util.cache_from_source(self.source_path)
|
2011-11-26 11:37:02 -06:00
|
|
|
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
|
|
|
|
# on different drives. Therefore we need to switch to the drive where
|
|
|
|
# the temporary source file lives.
|
|
|
|
drive = os.path.splitdrive(self.source_path)[0]
|
|
|
|
if drive:
|
|
|
|
os.chdir(drive)
|
2011-11-25 23:36:48 -06:00
|
|
|
with open(self.source_path, 'w') as file:
|
|
|
|
file.write('x = 123\n')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.directory)
|
2011-11-26 11:37:02 -06:00
|
|
|
if self.cwd_drive:
|
|
|
|
os.chdir(self.cwd_drive)
|
2011-11-25 23:36:48 -06:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2013-06-14 18:33:00 -04:00
|
|
|
def test_do_not_overwrite_symlinks(self):
|
|
|
|
# In the face of a cfile argument being a symlink, bail out.
|
|
|
|
# Issue #17222
|
|
|
|
try:
|
|
|
|
os.symlink(self.pyc_path + '.actual', self.pyc_path)
|
2013-06-14 22:50:57 -04:00
|
|
|
except (NotImplementedError, OSError):
|
2013-06-14 18:33:00 -04:00
|
|
|
self.skipTest('need to be able to create a symlink for a file')
|
|
|
|
else:
|
|
|
|
assert os.path.islink(self.pyc_path)
|
|
|
|
with self.assertRaises(FileExistsError):
|
|
|
|
py_compile.compile(self.source_path, self.pyc_path)
|
|
|
|
|
|
|
|
@unittest.skipIf(not os.path.exists(os.devnull) or os.path.isfile(os.devnull),
|
|
|
|
'requires os.devnull and for it to be a non-regular file')
|
|
|
|
def test_do_not_overwrite_nonregular_files(self):
|
|
|
|
# In the face of a cfile argument being a non-regular file, bail out.
|
|
|
|
# Issue #17222
|
|
|
|
with self.assertRaises(FileExistsError):
|
|
|
|
py_compile.compile(self.source_path, os.devnull)
|
|
|
|
|
2011-11-25 23:36:48 -06:00
|
|
|
def test_cache_path(self):
|
|
|
|
py_compile.compile(self.source_path)
|
|
|
|
self.assertTrue(os.path.exists(self.cache_path))
|
|
|
|
|
2011-11-28 09:27:32 -06:00
|
|
|
def test_cwd(self):
|
2020-07-06 20:29:49 +08:00
|
|
|
with os_helper.change_cwd(self.directory):
|
2015-09-06 14:13:25 +03:00
|
|
|
py_compile.compile(os.path.basename(self.source_path),
|
|
|
|
os.path.basename(self.pyc_path))
|
2011-11-28 09:27:32 -06:00
|
|
|
self.assertTrue(os.path.exists(self.pyc_path))
|
|
|
|
self.assertFalse(os.path.exists(self.cache_path))
|
|
|
|
|
2011-11-25 23:36:48 -06:00
|
|
|
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))
|
|
|
|
|
2022-06-27 20:27:19 +02:00
|
|
|
@os_helper.skip_if_dac_override
|
2013-04-24 16:34:07 -04:00
|
|
|
@unittest.skipIf(os.name == 'nt',
|
|
|
|
'cannot control directory permissions on Windows')
|
2022-06-06 19:24:11 +02:00
|
|
|
@os_helper.skip_unless_working_chmod
|
2013-04-14 12:48:15 -04:00
|
|
|
def test_exceptions_propagate(self):
|
|
|
|
# Make sure that exceptions raised thanks to issues with writing
|
|
|
|
# bytecode.
|
|
|
|
# http://bugs.python.org/issue17244
|
|
|
|
mode = os.stat(self.directory)
|
|
|
|
os.chmod(self.directory, stat.S_IREAD)
|
|
|
|
try:
|
|
|
|
with self.assertRaises(IOError):
|
|
|
|
py_compile.compile(self.source_path, self.pyc_path)
|
|
|
|
finally:
|
|
|
|
os.chmod(self.directory, mode.st_mode)
|
|
|
|
|
2014-08-22 20:17:32 +03:00
|
|
|
def test_bad_coding(self):
|
2023-09-12 09:37:42 +03:00
|
|
|
bad_coding = os.path.join(os.path.dirname(__file__),
|
|
|
|
'tokenizedata',
|
|
|
|
'bad_coding2.py')
|
2014-09-01 12:29:53 +03:00
|
|
|
with support.captured_stderr():
|
|
|
|
self.assertIsNone(py_compile.compile(bad_coding, doraise=False))
|
2014-08-22 20:52:15 +03:00
|
|
|
self.assertFalse(os.path.exists(
|
|
|
|
importlib.util.cache_from_source(bad_coding)))
|
2011-11-25 23:36:48 -06:00
|
|
|
|
2018-01-24 22:26:18 +01:00
|
|
|
def test_source_date_epoch(self):
|
2018-10-10 12:43:14 -04:00
|
|
|
py_compile.compile(self.source_path, self.pyc_path)
|
2018-01-24 22:26:18 +01:00
|
|
|
self.assertTrue(os.path.exists(self.pyc_path))
|
|
|
|
self.assertFalse(os.path.exists(self.cache_path))
|
|
|
|
with open(self.pyc_path, 'rb') as fp:
|
|
|
|
flags = importlib._bootstrap_external._classify_pyc(
|
|
|
|
fp.read(), 'test', {})
|
2018-10-10 12:43:14 -04:00
|
|
|
if os.environ.get('SOURCE_DATE_EPOCH'):
|
|
|
|
expected_flags = 0b11
|
|
|
|
else:
|
|
|
|
expected_flags = 0b00
|
|
|
|
|
|
|
|
self.assertEqual(flags, expected_flags)
|
2018-01-24 22:26:18 +01:00
|
|
|
|
2015-04-13 16:28:11 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize > 0, 'test does not work with -O')
|
2014-12-01 17:10:10 -05:00
|
|
|
def test_double_dot_no_clobber(self):
|
|
|
|
# http://bugs.python.org/issue22966
|
|
|
|
# py_compile foo.bar.py -> __pycache__/foo.cpython-34.pyc
|
|
|
|
weird_path = os.path.join(self.directory, 'foo.bar.py')
|
|
|
|
cache_path = importlib.util.cache_from_source(weird_path)
|
|
|
|
pyc_path = weird_path + 'c'
|
2014-12-02 11:30:43 -05:00
|
|
|
head, tail = os.path.split(cache_path)
|
|
|
|
penultimate_tail = os.path.basename(head)
|
2014-12-01 17:10:10 -05:00
|
|
|
self.assertEqual(
|
2014-12-02 11:30:43 -05:00
|
|
|
os.path.join(penultimate_tail, tail),
|
|
|
|
os.path.join(
|
|
|
|
'__pycache__',
|
|
|
|
'foo.bar.{}.pyc'.format(sys.implementation.cache_tag)))
|
2014-12-01 17:10:10 -05:00
|
|
|
with open(weird_path, 'w') as file:
|
|
|
|
file.write('x = 123\n')
|
|
|
|
py_compile.compile(weird_path)
|
|
|
|
self.assertTrue(os.path.exists(cache_path))
|
|
|
|
self.assertFalse(os.path.exists(pyc_path))
|
|
|
|
|
2015-04-13 14:21:02 -04:00
|
|
|
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))
|
|
|
|
|
2017-12-09 10:26:52 -08:00
|
|
|
def test_invalidation_mode(self):
|
|
|
|
py_compile.compile(
|
|
|
|
self.source_path,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
|
|
|
|
)
|
|
|
|
with open(self.cache_path, 'rb') as fp:
|
|
|
|
flags = importlib._bootstrap_external._classify_pyc(
|
|
|
|
fp.read(), 'test', {})
|
|
|
|
self.assertEqual(flags, 0b11)
|
|
|
|
py_compile.compile(
|
|
|
|
self.source_path,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
|
|
|
|
)
|
|
|
|
with open(self.cache_path, 'rb') as fp:
|
|
|
|
flags = importlib._bootstrap_external._classify_pyc(
|
|
|
|
fp.read(), 'test', {})
|
|
|
|
self.assertEqual(flags, 0b1)
|
|
|
|
|
2019-05-28 13:29:04 -03:00
|
|
|
def test_quiet(self):
|
2023-09-12 09:37:42 +03:00
|
|
|
bad_coding = os.path.join(os.path.dirname(__file__),
|
|
|
|
'tokenizedata',
|
|
|
|
'bad_coding2.py')
|
2019-05-28 13:29:04 -03:00
|
|
|
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.assertEqual(stderr.getvalue(), '')
|
|
|
|
with self.assertRaises(py_compile.PyCompileError):
|
|
|
|
py_compile.compile(bad_coding, doraise=True, quiet=1)
|
|
|
|
|
2014-12-01 17:10:10 -05:00
|
|
|
|
2018-10-10 12:43:14 -04:00
|
|
|
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
|
|
|
|
unittest.TestCase,
|
|
|
|
metaclass=SourceDateEpochTestMeta,
|
|
|
|
source_date_epoch=True):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
|
|
|
|
unittest.TestCase,
|
|
|
|
metaclass=SourceDateEpochTestMeta,
|
|
|
|
source_date_epoch=False):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-07-25 22:58:45 +03:00
|
|
|
class PyCompileCLITestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.directory = tempfile.mkdtemp()
|
|
|
|
self.source_path = os.path.join(self.directory, '_test.py')
|
2024-02-19 19:02:51 +02:00
|
|
|
self.cache_path = importlib.util.cache_from_source(self.source_path,
|
|
|
|
optimization='' if __debug__ else 1)
|
2020-07-25 22:58:45 +03:00
|
|
|
with open(self.source_path, 'w') as file:
|
|
|
|
file.write('x = 123\n')
|
|
|
|
|
|
|
|
def tearDown(self):
|
2020-08-07 23:18:38 +08:00
|
|
|
os_helper.rmtree(self.directory)
|
2020-07-25 22:58:45 +03:00
|
|
|
|
2022-01-25 09:09:06 +02:00
|
|
|
@support.requires_subprocess()
|
2020-07-25 22:58:45 +03:00
|
|
|
def pycompilecmd(self, *args, **kwargs):
|
|
|
|
# assert_python_* helpers don't return proc object. We'll just use
|
|
|
|
# subprocess.run() instead of spawn_python() and its friends to test
|
|
|
|
# stdin support of the CLI.
|
2022-10-19 11:05:08 +01:00
|
|
|
opts = '-m' if __debug__ else '-Om'
|
2020-07-25 22:58:45 +03:00
|
|
|
if args and args[0] == '-' and 'input' in kwargs:
|
2022-10-19 11:05:08 +01:00
|
|
|
return subprocess.run([sys.executable, opts, 'py_compile', '-'],
|
2020-07-25 22:58:45 +03:00
|
|
|
input=kwargs['input'].encode(),
|
|
|
|
capture_output=True)
|
2022-10-19 11:05:08 +01:00
|
|
|
return script_helper.assert_python_ok(opts, 'py_compile', *args, **kwargs)
|
2020-07-25 22:58:45 +03:00
|
|
|
|
|
|
|
def pycompilecmd_failure(self, *args):
|
|
|
|
return script_helper.assert_python_failure('-m', 'py_compile', *args)
|
|
|
|
|
|
|
|
def test_stdin(self):
|
2024-02-19 19:02:51 +02:00
|
|
|
self.assertFalse(os.path.exists(self.cache_path))
|
2020-07-25 22:58:45 +03:00
|
|
|
result = self.pycompilecmd('-', input=self.source_path)
|
|
|
|
self.assertEqual(result.returncode, 0)
|
|
|
|
self.assertEqual(result.stdout, b'')
|
|
|
|
self.assertEqual(result.stderr, b'')
|
|
|
|
self.assertTrue(os.path.exists(self.cache_path))
|
|
|
|
|
|
|
|
def test_with_files(self):
|
|
|
|
rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path)
|
|
|
|
self.assertEqual(rc, 0)
|
|
|
|
self.assertEqual(stdout, b'')
|
|
|
|
self.assertEqual(stderr, b'')
|
|
|
|
self.assertTrue(os.path.exists(self.cache_path))
|
|
|
|
|
|
|
|
def test_bad_syntax(self):
|
2023-09-12 09:37:42 +03:00
|
|
|
bad_syntax = os.path.join(os.path.dirname(__file__),
|
|
|
|
'tokenizedata',
|
|
|
|
'badsyntax_3131.py')
|
2020-07-25 22:58:45 +03:00
|
|
|
rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
self.assertEqual(stdout, b'')
|
|
|
|
self.assertIn(b'SyntaxError', stderr)
|
|
|
|
|
|
|
|
def test_bad_syntax_with_quiet(self):
|
2023-09-12 09:37:42 +03:00
|
|
|
bad_syntax = os.path.join(os.path.dirname(__file__),
|
|
|
|
'tokenizedata',
|
|
|
|
'badsyntax_3131.py')
|
2020-07-25 22:58:45 +03:00
|
|
|
rc, stdout, stderr = self.pycompilecmd_failure('-q', bad_syntax)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
self.assertEqual(stdout, b'')
|
|
|
|
self.assertEqual(stderr, b'')
|
|
|
|
|
|
|
|
def test_file_not_exists(self):
|
|
|
|
should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
|
|
|
|
rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
self.assertEqual(stdout, b'')
|
2021-05-08 16:03:41 +08:00
|
|
|
self.assertIn(b'no such file or directory', stderr.lower())
|
2020-07-25 22:58:45 +03:00
|
|
|
|
|
|
|
def test_file_not_exists_with_quiet(self):
|
|
|
|
should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
|
|
|
|
rc, stdout, stderr = self.pycompilecmd_failure('-q', self.source_path, should_not_exists)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
self.assertEqual(stdout, b'')
|
|
|
|
self.assertEqual(stderr, b'')
|
|
|
|
|
|
|
|
|
2011-11-25 23:36:48 -06:00
|
|
|
if __name__ == "__main__":
|
2013-04-14 12:51:36 -04:00
|
|
|
unittest.main()
|