| 1 | n/a | import importlib.util |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import py_compile |
|---|
| 4 | n/a | import shutil |
|---|
| 5 | n/a | import stat |
|---|
| 6 | n/a | import sys |
|---|
| 7 | n/a | import tempfile |
|---|
| 8 | n/a | import unittest |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | from test import support |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | class PyCompileTests(unittest.TestCase): |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | def setUp(self): |
|---|
| 16 | n/a | self.directory = tempfile.mkdtemp() |
|---|
| 17 | n/a | self.source_path = os.path.join(self.directory, '_test.py') |
|---|
| 18 | n/a | self.pyc_path = self.source_path + 'c' |
|---|
| 19 | n/a | self.cache_path = importlib.util.cache_from_source(self.source_path) |
|---|
| 20 | n/a | self.cwd_drive = os.path.splitdrive(os.getcwd())[0] |
|---|
| 21 | n/a | # In these tests we compute relative paths. When using Windows, the |
|---|
| 22 | n/a | # current working directory path and the 'self.source_path' might be |
|---|
| 23 | n/a | # on different drives. Therefore we need to switch to the drive where |
|---|
| 24 | n/a | # the temporary source file lives. |
|---|
| 25 | n/a | drive = os.path.splitdrive(self.source_path)[0] |
|---|
| 26 | n/a | if drive: |
|---|
| 27 | n/a | os.chdir(drive) |
|---|
| 28 | n/a | with open(self.source_path, 'w') as file: |
|---|
| 29 | n/a | file.write('x = 123\n') |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def tearDown(self): |
|---|
| 32 | n/a | shutil.rmtree(self.directory) |
|---|
| 33 | n/a | if self.cwd_drive: |
|---|
| 34 | n/a | os.chdir(self.cwd_drive) |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | def test_absolute_path(self): |
|---|
| 37 | n/a | py_compile.compile(self.source_path, self.pyc_path) |
|---|
| 38 | n/a | self.assertTrue(os.path.exists(self.pyc_path)) |
|---|
| 39 | n/a | self.assertFalse(os.path.exists(self.cache_path)) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def test_do_not_overwrite_symlinks(self): |
|---|
| 42 | n/a | # In the face of a cfile argument being a symlink, bail out. |
|---|
| 43 | n/a | # Issue #17222 |
|---|
| 44 | n/a | try: |
|---|
| 45 | n/a | os.symlink(self.pyc_path + '.actual', self.pyc_path) |
|---|
| 46 | n/a | except (NotImplementedError, OSError): |
|---|
| 47 | n/a | self.skipTest('need to be able to create a symlink for a file') |
|---|
| 48 | n/a | else: |
|---|
| 49 | n/a | assert os.path.islink(self.pyc_path) |
|---|
| 50 | n/a | with self.assertRaises(FileExistsError): |
|---|
| 51 | n/a | py_compile.compile(self.source_path, self.pyc_path) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | @unittest.skipIf(not os.path.exists(os.devnull) or os.path.isfile(os.devnull), |
|---|
| 54 | n/a | 'requires os.devnull and for it to be a non-regular file') |
|---|
| 55 | n/a | def test_do_not_overwrite_nonregular_files(self): |
|---|
| 56 | n/a | # In the face of a cfile argument being a non-regular file, bail out. |
|---|
| 57 | n/a | # Issue #17222 |
|---|
| 58 | n/a | with self.assertRaises(FileExistsError): |
|---|
| 59 | n/a | py_compile.compile(self.source_path, os.devnull) |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | def test_cache_path(self): |
|---|
| 62 | n/a | py_compile.compile(self.source_path) |
|---|
| 63 | n/a | self.assertTrue(os.path.exists(self.cache_path)) |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def test_cwd(self): |
|---|
| 66 | n/a | with support.change_cwd(self.directory): |
|---|
| 67 | n/a | py_compile.compile(os.path.basename(self.source_path), |
|---|
| 68 | n/a | os.path.basename(self.pyc_path)) |
|---|
| 69 | n/a | self.assertTrue(os.path.exists(self.pyc_path)) |
|---|
| 70 | n/a | self.assertFalse(os.path.exists(self.cache_path)) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def test_relative_path(self): |
|---|
| 73 | n/a | py_compile.compile(os.path.relpath(self.source_path), |
|---|
| 74 | n/a | os.path.relpath(self.pyc_path)) |
|---|
| 75 | n/a | self.assertTrue(os.path.exists(self.pyc_path)) |
|---|
| 76 | n/a | self.assertFalse(os.path.exists(self.cache_path)) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
|---|
| 79 | n/a | 'non-root user required') |
|---|
| 80 | n/a | @unittest.skipIf(os.name == 'nt', |
|---|
| 81 | n/a | 'cannot control directory permissions on Windows') |
|---|
| 82 | n/a | def test_exceptions_propagate(self): |
|---|
| 83 | n/a | # Make sure that exceptions raised thanks to issues with writing |
|---|
| 84 | n/a | # bytecode. |
|---|
| 85 | n/a | # http://bugs.python.org/issue17244 |
|---|
| 86 | n/a | mode = os.stat(self.directory) |
|---|
| 87 | n/a | os.chmod(self.directory, stat.S_IREAD) |
|---|
| 88 | n/a | try: |
|---|
| 89 | n/a | with self.assertRaises(IOError): |
|---|
| 90 | n/a | py_compile.compile(self.source_path, self.pyc_path) |
|---|
| 91 | n/a | finally: |
|---|
| 92 | n/a | os.chmod(self.directory, mode.st_mode) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | def test_bad_coding(self): |
|---|
| 95 | n/a | bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py') |
|---|
| 96 | n/a | with support.captured_stderr(): |
|---|
| 97 | n/a | self.assertIsNone(py_compile.compile(bad_coding, doraise=False)) |
|---|
| 98 | n/a | self.assertFalse(os.path.exists( |
|---|
| 99 | n/a | importlib.util.cache_from_source(bad_coding))) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | @unittest.skipIf(sys.flags.optimize > 0, 'test does not work with -O') |
|---|
| 102 | n/a | def test_double_dot_no_clobber(self): |
|---|
| 103 | n/a | # http://bugs.python.org/issue22966 |
|---|
| 104 | n/a | # py_compile foo.bar.py -> __pycache__/foo.cpython-34.pyc |
|---|
| 105 | n/a | weird_path = os.path.join(self.directory, 'foo.bar.py') |
|---|
| 106 | n/a | cache_path = importlib.util.cache_from_source(weird_path) |
|---|
| 107 | n/a | pyc_path = weird_path + 'c' |
|---|
| 108 | n/a | head, tail = os.path.split(cache_path) |
|---|
| 109 | n/a | penultimate_tail = os.path.basename(head) |
|---|
| 110 | n/a | self.assertEqual( |
|---|
| 111 | n/a | os.path.join(penultimate_tail, tail), |
|---|
| 112 | n/a | os.path.join( |
|---|
| 113 | n/a | '__pycache__', |
|---|
| 114 | n/a | 'foo.bar.{}.pyc'.format(sys.implementation.cache_tag))) |
|---|
| 115 | n/a | with open(weird_path, 'w') as file: |
|---|
| 116 | n/a | file.write('x = 123\n') |
|---|
| 117 | n/a | py_compile.compile(weird_path) |
|---|
| 118 | n/a | self.assertTrue(os.path.exists(cache_path)) |
|---|
| 119 | n/a | self.assertFalse(os.path.exists(pyc_path)) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def test_optimization_path(self): |
|---|
| 122 | n/a | # Specifying optimized bytecode should lead to a path reflecting that. |
|---|
| 123 | n/a | self.assertIn('opt-2', py_compile.compile(self.source_path, optimize=2)) |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | if __name__ == "__main__": |
|---|
| 127 | n/a | unittest.main() |
|---|