test_py_compile.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import functools
  2. import importlib.util
  3. import os
  4. import py_compile
  5. import shutil
  6. import stat
  7. import subprocess
  8. import sys
  9. import tempfile
  10. import unittest
  11. from test import support
  12. from test.support import os_helper, script_helper
  13. def without_source_date_epoch(fxn):
  14. """Runs function with SOURCE_DATE_EPOCH unset."""
  15. @functools.wraps(fxn)
  16. def wrapper(*args, **kwargs):
  17. with os_helper.EnvironmentVarGuard() as env:
  18. env.unset('SOURCE_DATE_EPOCH')
  19. return fxn(*args, **kwargs)
  20. return wrapper
  21. def with_source_date_epoch(fxn):
  22. """Runs function with SOURCE_DATE_EPOCH set."""
  23. @functools.wraps(fxn)
  24. def wrapper(*args, **kwargs):
  25. with os_helper.EnvironmentVarGuard() as env:
  26. env['SOURCE_DATE_EPOCH'] = '123456789'
  27. return fxn(*args, **kwargs)
  28. return wrapper
  29. # Run tests with SOURCE_DATE_EPOCH set or unset explicitly.
  30. class SourceDateEpochTestMeta(type(unittest.TestCase)):
  31. def __new__(mcls, name, bases, dct, *, source_date_epoch):
  32. cls = super().__new__(mcls, name, bases, dct)
  33. for attr in dir(cls):
  34. if attr.startswith('test_'):
  35. meth = getattr(cls, attr)
  36. if source_date_epoch:
  37. wrapper = with_source_date_epoch(meth)
  38. else:
  39. wrapper = without_source_date_epoch(meth)
  40. setattr(cls, attr, wrapper)
  41. return cls
  42. class PyCompileTestsBase:
  43. def setUp(self):
  44. self.directory = tempfile.mkdtemp(dir=os.getcwd())
  45. self.source_path = os.path.join(self.directory, '_test.py')
  46. self.pyc_path = self.source_path + 'c'
  47. self.cache_path = importlib.util.cache_from_source(self.source_path)
  48. self.cwd_drive = os.path.splitdrive(os.getcwd())[0]
  49. # In these tests we compute relative paths. When using Windows, the
  50. # current working directory path and the 'self.source_path' might be
  51. # on different drives. Therefore we need to switch to the drive where
  52. # the temporary source file lives.
  53. drive = os.path.splitdrive(self.source_path)[0]
  54. if drive:
  55. os.chdir(drive)
  56. with open(self.source_path, 'w') as file:
  57. file.write('x = 123\n')
  58. def tearDown(self):
  59. shutil.rmtree(self.directory)
  60. if self.cwd_drive:
  61. os.chdir(self.cwd_drive)
  62. def test_absolute_path(self):
  63. py_compile.compile(self.source_path, self.pyc_path)
  64. self.assertTrue(os.path.exists(self.pyc_path))
  65. self.assertFalse(os.path.exists(self.cache_path))
  66. def test_do_not_overwrite_symlinks(self):
  67. # In the face of a cfile argument being a symlink, bail out.
  68. # Issue #17222
  69. try:
  70. os.symlink(self.pyc_path + '.actual', self.pyc_path)
  71. except (NotImplementedError, OSError):
  72. self.skipTest('need to be able to create a symlink for a file')
  73. else:
  74. assert os.path.islink(self.pyc_path)
  75. with self.assertRaises(FileExistsError):
  76. py_compile.compile(self.source_path, self.pyc_path)
  77. @unittest.skipIf(not os.path.exists(os.devnull) or os.path.isfile(os.devnull),
  78. 'requires os.devnull and for it to be a non-regular file')
  79. def test_do_not_overwrite_nonregular_files(self):
  80. # In the face of a cfile argument being a non-regular file, bail out.
  81. # Issue #17222
  82. with self.assertRaises(FileExistsError):
  83. py_compile.compile(self.source_path, os.devnull)
  84. def test_cache_path(self):
  85. py_compile.compile(self.source_path)
  86. self.assertTrue(os.path.exists(self.cache_path))
  87. def test_cwd(self):
  88. with os_helper.change_cwd(self.directory):
  89. py_compile.compile(os.path.basename(self.source_path),
  90. os.path.basename(self.pyc_path))
  91. self.assertTrue(os.path.exists(self.pyc_path))
  92. self.assertFalse(os.path.exists(self.cache_path))
  93. def test_relative_path(self):
  94. py_compile.compile(os.path.relpath(self.source_path),
  95. os.path.relpath(self.pyc_path))
  96. self.assertTrue(os.path.exists(self.pyc_path))
  97. self.assertFalse(os.path.exists(self.cache_path))
  98. @os_helper.skip_if_dac_override
  99. @unittest.skipIf(os.name == 'nt',
  100. 'cannot control directory permissions on Windows')
  101. @os_helper.skip_unless_working_chmod
  102. def test_exceptions_propagate(self):
  103. # Make sure that exceptions raised thanks to issues with writing
  104. # bytecode.
  105. # http://bugs.python.org/issue17244
  106. mode = os.stat(self.directory)
  107. os.chmod(self.directory, stat.S_IREAD)
  108. try:
  109. with self.assertRaises(IOError):
  110. py_compile.compile(self.source_path, self.pyc_path)
  111. finally:
  112. os.chmod(self.directory, mode.st_mode)
  113. def test_bad_coding(self):
  114. bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py')
  115. with support.captured_stderr():
  116. self.assertIsNone(py_compile.compile(bad_coding, doraise=False))
  117. self.assertFalse(os.path.exists(
  118. importlib.util.cache_from_source(bad_coding)))
  119. def test_source_date_epoch(self):
  120. py_compile.compile(self.source_path, self.pyc_path)
  121. self.assertTrue(os.path.exists(self.pyc_path))
  122. self.assertFalse(os.path.exists(self.cache_path))
  123. with open(self.pyc_path, 'rb') as fp:
  124. flags = importlib._bootstrap_external._classify_pyc(
  125. fp.read(), 'test', {})
  126. if os.environ.get('SOURCE_DATE_EPOCH'):
  127. expected_flags = 0b11
  128. else:
  129. expected_flags = 0b00
  130. self.assertEqual(flags, expected_flags)
  131. @unittest.skipIf(sys.flags.optimize > 0, 'test does not work with -O')
  132. def test_double_dot_no_clobber(self):
  133. # http://bugs.python.org/issue22966
  134. # py_compile foo.bar.py -> __pycache__/foo.cpython-34.pyc
  135. weird_path = os.path.join(self.directory, 'foo.bar.py')
  136. cache_path = importlib.util.cache_from_source(weird_path)
  137. pyc_path = weird_path + 'c'
  138. head, tail = os.path.split(cache_path)
  139. penultimate_tail = os.path.basename(head)
  140. self.assertEqual(
  141. os.path.join(penultimate_tail, tail),
  142. os.path.join(
  143. '__pycache__',
  144. 'foo.bar.{}.pyc'.format(sys.implementation.cache_tag)))
  145. with open(weird_path, 'w') as file:
  146. file.write('x = 123\n')
  147. py_compile.compile(weird_path)
  148. self.assertTrue(os.path.exists(cache_path))
  149. self.assertFalse(os.path.exists(pyc_path))
  150. def test_optimization_path(self):
  151. # Specifying optimized bytecode should lead to a path reflecting that.
  152. self.assertIn('opt-2', py_compile.compile(self.source_path, optimize=2))
  153. def test_invalidation_mode(self):
  154. py_compile.compile(
  155. self.source_path,
  156. invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
  157. )
  158. with open(self.cache_path, 'rb') as fp:
  159. flags = importlib._bootstrap_external._classify_pyc(
  160. fp.read(), 'test', {})
  161. self.assertEqual(flags, 0b11)
  162. py_compile.compile(
  163. self.source_path,
  164. invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
  165. )
  166. with open(self.cache_path, 'rb') as fp:
  167. flags = importlib._bootstrap_external._classify_pyc(
  168. fp.read(), 'test', {})
  169. self.assertEqual(flags, 0b1)
  170. def test_quiet(self):
  171. bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py')
  172. with support.captured_stderr() as stderr:
  173. self.assertIsNone(py_compile.compile(bad_coding, doraise=False, quiet=2))
  174. self.assertIsNone(py_compile.compile(bad_coding, doraise=True, quiet=2))
  175. self.assertEqual(stderr.getvalue(), '')
  176. with self.assertRaises(py_compile.PyCompileError):
  177. py_compile.compile(bad_coding, doraise=True, quiet=1)
  178. class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
  179. unittest.TestCase,
  180. metaclass=SourceDateEpochTestMeta,
  181. source_date_epoch=True):
  182. pass
  183. class PyCompileTestsWithoutSourceEpoch(PyCompileTestsBase,
  184. unittest.TestCase,
  185. metaclass=SourceDateEpochTestMeta,
  186. source_date_epoch=False):
  187. pass
  188. class PyCompileCLITestCase(unittest.TestCase):
  189. def setUp(self):
  190. self.directory = tempfile.mkdtemp()
  191. self.source_path = os.path.join(self.directory, '_test.py')
  192. self.cache_path = importlib.util.cache_from_source(self.source_path)
  193. with open(self.source_path, 'w') as file:
  194. file.write('x = 123\n')
  195. def tearDown(self):
  196. os_helper.rmtree(self.directory)
  197. @support.requires_subprocess()
  198. def pycompilecmd(self, *args, **kwargs):
  199. # assert_python_* helpers don't return proc object. We'll just use
  200. # subprocess.run() instead of spawn_python() and its friends to test
  201. # stdin support of the CLI.
  202. if args and args[0] == '-' and 'input' in kwargs:
  203. return subprocess.run([sys.executable, '-m', 'py_compile', '-'],
  204. input=kwargs['input'].encode(),
  205. capture_output=True)
  206. return script_helper.assert_python_ok('-m', 'py_compile', *args, **kwargs)
  207. def pycompilecmd_failure(self, *args):
  208. return script_helper.assert_python_failure('-m', 'py_compile', *args)
  209. def test_stdin(self):
  210. result = self.pycompilecmd('-', input=self.source_path)
  211. self.assertEqual(result.returncode, 0)
  212. self.assertEqual(result.stdout, b'')
  213. self.assertEqual(result.stderr, b'')
  214. self.assertTrue(os.path.exists(self.cache_path))
  215. def test_with_files(self):
  216. rc, stdout, stderr = self.pycompilecmd(self.source_path, self.source_path)
  217. self.assertEqual(rc, 0)
  218. self.assertEqual(stdout, b'')
  219. self.assertEqual(stderr, b'')
  220. self.assertTrue(os.path.exists(self.cache_path))
  221. def test_bad_syntax(self):
  222. bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
  223. rc, stdout, stderr = self.pycompilecmd_failure(bad_syntax)
  224. self.assertEqual(rc, 1)
  225. self.assertEqual(stdout, b'')
  226. self.assertIn(b'SyntaxError', stderr)
  227. def test_bad_syntax_with_quiet(self):
  228. bad_syntax = os.path.join(os.path.dirname(__file__), 'badsyntax_3131.py')
  229. rc, stdout, stderr = self.pycompilecmd_failure('-q', bad_syntax)
  230. self.assertEqual(rc, 1)
  231. self.assertEqual(stdout, b'')
  232. self.assertEqual(stderr, b'')
  233. def test_file_not_exists(self):
  234. should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
  235. rc, stdout, stderr = self.pycompilecmd_failure(self.source_path, should_not_exists)
  236. self.assertEqual(rc, 1)
  237. self.assertEqual(stdout, b'')
  238. self.assertIn(b'no such file or directory', stderr.lower())
  239. def test_file_not_exists_with_quiet(self):
  240. should_not_exists = os.path.join(os.path.dirname(__file__), 'should_not_exists.py')
  241. rc, stdout, stderr = self.pycompilecmd_failure('-q', self.source_path, should_not_exists)
  242. self.assertEqual(rc, 1)
  243. self.assertEqual(stdout, b'')
  244. self.assertEqual(stderr, b'')
  245. if __name__ == "__main__":
  246. unittest.main()