ยปCore Development>Code coverage>Lib/test/test_py_compile.py

Python code coverage for Lib/test/test_py_compile.py

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