summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/tests/test_util.py
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-11-03 04:08:28 (GMT)
committerÉric Araujo <merwok@netwok.org>2011-11-03 04:08:28 (GMT)
commit880801501b778d93079afc11672dba86e60eab94 (patch)
tree32bad2da11ff3f125f67e46d93c308f18c311eab /Lib/packaging/tests/test_util.py
parentdfd232898d4ae64a4c176ed492eec5e1ab209ef8 (diff)
downloadcpython-880801501b778d93079afc11672dba86e60eab94.zip
cpython-880801501b778d93079afc11672dba86e60eab94.tar.gz
cpython-880801501b778d93079afc11672dba86e60eab94.tar.bz2
Improve byte-compilation in packaging to be independent of -O or -B.
The code I fixed to comply with PEP 3147 still had one bug: When run under python -O, some paths for pyc files would be pyo, because I called imp.cache_from_source without explicit debug_override argument in some places, and under -O that would return .pyo (this is well explained in the imp docs). Now all code (util.byte_compile, build_py, install_lib) can create .pyo files according to options given by users, without interference from the calling Python’s own optimize mode. On a related topic, I also removed the code that prevented byte compilation under python -B. The rationale is that packaging gives control over the creation of pyc files to the user with its own explicit option, and the behavior should not be changed if the calling Python happens to run with -B for whatever reason. I will argue that this is a bug fix and ask to be allowed to backport this change to distutils. Finally, I moved one nugget of information about the --compile and --optimize options from the source into the doc. It clears up a misunderstanding that I (and maybe other people) had.
Diffstat (limited to 'Lib/packaging/tests/test_util.py')
-rw-r--r--Lib/packaging/tests/test_util.py54
1 files changed, 25 insertions, 29 deletions
diff --git a/Lib/packaging/tests/test_util.py b/Lib/packaging/tests/test_util.py
index 1cf5c93..00d61e0 100644
--- a/Lib/packaging/tests/test_util.py
+++ b/Lib/packaging/tests/test_util.py
@@ -10,7 +10,7 @@ import subprocess
from io import StringIO
from packaging.errors import (
- PackagingPlatformError, PackagingByteCompileError, PackagingFileError,
+ PackagingPlatformError, PackagingFileError,
PackagingExecError, InstallationException)
from packaging import util
from packaging.dist import Distribution
@@ -138,15 +138,8 @@ class UtilTestCase(support.EnvironRestorer,
self._uname = None
os.uname = self._get_uname
- # patching POpen
- self.old_find_executable = util.find_executable
- util.find_executable = self._find_executable
- self._exes = {}
- self.old_popen = subprocess.Popen
- self.old_stdout = sys.stdout
- self.old_stderr = sys.stderr
- FakePopen.test_class = self
- subprocess.Popen = FakePopen
+ def _get_uname(self):
+ return self._uname
def tearDown(self):
# getting back the environment
@@ -161,17 +154,24 @@ class UtilTestCase(support.EnvironRestorer,
os.uname = self.uname
else:
del os.uname
+ super(UtilTestCase, self).tearDown()
+
+ def mock_popen(self):
+ self.old_find_executable = util.find_executable
+ util.find_executable = self._find_executable
+ self._exes = {}
+ self.old_popen = subprocess.Popen
+ self.old_stdout = sys.stdout
+ self.old_stderr = sys.stderr
+ FakePopen.test_class = self
+ subprocess.Popen = FakePopen
+ self.addCleanup(self.unmock_popen)
+
+ def unmock_popen(self):
util.find_executable = self.old_find_executable
subprocess.Popen = self.old_popen
sys.old_stdout = self.old_stdout
sys.old_stderr = self.old_stderr
- super(UtilTestCase, self).tearDown()
-
- def _set_uname(self, uname):
- self._uname = uname
-
- def _get_uname(self):
- return self._uname
def test_convert_path(self):
# linux/mac
@@ -283,6 +283,7 @@ class UtilTestCase(support.EnvironRestorer,
return None
def test_get_compiler_versions(self):
+ self.mock_popen()
# get_versions calls distutils.spawn.find_executable on
# 'gcc', 'ld' and 'dllwrap'
self.assertEqual(get_compiler_versions(), (None, None, None))
@@ -323,15 +324,12 @@ class UtilTestCase(support.EnvironRestorer,
res = get_compiler_versions()
self.assertEqual(res[2], None)
- def test_dont_write_bytecode(self):
- # makes sure byte_compile raise a PackagingError
- # if sys.dont_write_bytecode is True
- old_dont_write_bytecode = sys.dont_write_bytecode
+ def test_byte_compile_under_B(self):
+ # make sure byte compilation works under -B (dont_write_bytecode)
+ self.addCleanup(setattr, sys, 'dont_write_bytecode',
+ sys.dont_write_bytecode)
sys.dont_write_bytecode = True
- try:
- self.assertRaises(PackagingByteCompileError, byte_compile, [])
- finally:
- sys.dont_write_bytecode = old_dont_write_bytecode
+ byte_compile([])
def test_newer(self):
self.assertRaises(PackagingFileError, util.newer, 'xxx', 'xxx')
@@ -418,6 +416,7 @@ class UtilTestCase(support.EnvironRestorer,
self.assertRaises(ImportError, resolve_name, 'a.b.Spam')
self.assertRaises(ImportError, resolve_name, 'a.b.c.Spam')
+ @support.skip_2to3_optimize
def test_run_2to3_on_code(self):
content = "print 'test'"
converted_content = "print('test')"
@@ -431,6 +430,7 @@ class UtilTestCase(support.EnvironRestorer,
file_handle.close()
self.assertEqual(new_content, converted_content)
+ @support.skip_2to3_optimize
def test_run_2to3_on_doctests(self):
# to check if text files containing doctests only get converted.
content = ">>> print 'test'\ntest\n"
@@ -448,8 +448,6 @@ class UtilTestCase(support.EnvironRestorer,
@unittest.skipUnless(os.name in ('nt', 'posix'),
'runs only under posix or nt')
def test_spawn(self):
- # no patching of Popen here
- subprocess.Popen = self.old_popen
tmpdir = self.mkdtemp()
# creating something executable
@@ -545,8 +543,6 @@ class UtilTestCase(support.EnvironRestorer,
self.assertEqual(args['py_modules'], dist.py_modules)
def test_generate_setup_py(self):
- # undo subprocess.Popen monkey-patching before using assert_python_*
- subprocess.Popen = self.old_popen
os.chdir(self.mkdtemp())
self.write_file('setup.cfg', textwrap.dedent("""\
[metadata]