2013-06-15 15:07:21 -03:00
|
|
|
import importlib.util
|
2011-11-26 01:36:48 -04:00
|
|
|
import os
|
|
|
|
import py_compile
|
|
|
|
import shutil
|
2013-04-14 13:48:15 -03:00
|
|
|
import stat
|
2011-11-26 01:36:48 -04:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2014-09-01 06:33:12 -03:00
|
|
|
from test import support
|
|
|
|
|
2011-11-26 01:36:48 -04:00
|
|
|
|
|
|
|
class PyCompileTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.directory = tempfile.mkdtemp()
|
|
|
|
self.source_path = os.path.join(self.directory, '_test.py')
|
|
|
|
self.pyc_path = self.source_path + 'c'
|
2013-06-15 15:07:21 -03:00
|
|
|
self.cache_path = importlib.util.cache_from_source(self.source_path)
|
2011-11-26 13:37:02 -04: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-26 01:36:48 -04:00
|
|
|
with open(self.source_path, 'w') as file:
|
|
|
|
file.write('x = 123\n')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.directory)
|
2011-11-26 13:37:02 -04:00
|
|
|
if self.cwd_drive:
|
|
|
|
os.chdir(self.cwd_drive)
|
2011-11-26 01:36:48 -04: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 19:33:00 -03: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 23:50:57 -03:00
|
|
|
except (NotImplementedError, OSError):
|
2013-06-14 19:33:00 -03: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-26 01:36:48 -04:00
|
|
|
def test_cache_path(self):
|
|
|
|
py_compile.compile(self.source_path)
|
|
|
|
self.assertTrue(os.path.exists(self.cache_path))
|
|
|
|
|
2011-11-28 11:27:32 -04:00
|
|
|
def test_cwd(self):
|
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir(self.directory)
|
|
|
|
py_compile.compile(os.path.basename(self.source_path),
|
|
|
|
os.path.basename(self.pyc_path))
|
|
|
|
os.chdir(cwd)
|
|
|
|
self.assertTrue(os.path.exists(self.pyc_path))
|
|
|
|
self.assertFalse(os.path.exists(self.cache_path))
|
|
|
|
|
2011-11-26 01:36:48 -04: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))
|
|
|
|
|
2013-10-25 04:21:51 -03:00
|
|
|
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
|
|
|
|
'non-root user required')
|
2013-04-24 17:34:07 -03:00
|
|
|
@unittest.skipIf(os.name == 'nt',
|
|
|
|
'cannot control directory permissions on Windows')
|
2013-04-14 13:48:15 -03: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 14:17:32 -03:00
|
|
|
def test_bad_coding(self):
|
|
|
|
bad_coding = os.path.join(os.path.dirname(__file__), 'bad_coding2.py')
|
2014-09-01 06:29:53 -03:00
|
|
|
with support.captured_stderr():
|
|
|
|
self.assertIsNone(py_compile.compile(bad_coding, doraise=False))
|
2014-08-22 14:52:15 -03:00
|
|
|
self.assertFalse(os.path.exists(
|
|
|
|
importlib.util.cache_from_source(bad_coding)))
|
2011-11-26 01:36:48 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-04-14 13:51:36 -03:00
|
|
|
unittest.main()
|