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