summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_compileall.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_compileall.py')
-rw-r--r--Lib/test/test_compileall.py122
1 files changed, 110 insertions, 12 deletions
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 7b307ed..ef2b669 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -11,7 +11,15 @@ import time
import unittest
import io
-from test import support, script_helper
+from unittest import mock, skipUnless
+try:
+ from concurrent.futures import ProcessPoolExecutor
+ _have_multiprocessing = True
+except ImportError:
+ _have_multiprocessing = False
+
+from test import support
+from test.support import script_helper
class CompileallTests(unittest.TestCase):
@@ -95,18 +103,45 @@ class CompileallTests(unittest.TestCase):
def test_optimize(self):
# make sure compiling with different optimization settings than the
# interpreter's creates the correct file names
- optimize = 1 if __debug__ else 0
+ optimize, opt = (1, 1) if __debug__ else (0, '')
compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
cached = importlib.util.cache_from_source(self.source_path,
- debug_override=not optimize)
+ optimization=opt)
self.assertTrue(os.path.isfile(cached))
cached2 = importlib.util.cache_from_source(self.source_path2,
- debug_override=not optimize)
+ optimization=opt)
self.assertTrue(os.path.isfile(cached2))
cached3 = importlib.util.cache_from_source(self.source_path3,
- debug_override=not optimize)
+ optimization=opt)
self.assertTrue(os.path.isfile(cached3))
+ @mock.patch('compileall.ProcessPoolExecutor')
+ def test_compile_pool_called(self, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True, workers=5)
+ self.assertTrue(pool_mock.called)
+
+ def test_compile_workers_non_positive(self):
+ with self.assertRaisesRegex(ValueError,
+ "workers must be greater or equal to 0"):
+ compileall.compile_dir(self.directory, workers=-1)
+
+ @mock.patch('compileall.ProcessPoolExecutor')
+ def test_compile_workers_cpu_count(self, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True, workers=0)
+ self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
+
+ @mock.patch('compileall.ProcessPoolExecutor')
+ @mock.patch('compileall.compile_file')
+ def test_compile_one_worker(self, compile_file_mock, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True)
+ self.assertFalse(pool_mock.called)
+ self.assertTrue(compile_file_mock.called)
+
+ @mock.patch('compileall.ProcessPoolExecutor', new=None)
+ @mock.patch('compileall.compile_file')
+ def test_compile_missing_multiprocessing(self, compile_file_mock):
+ compileall.compile_dir(self.directory, quiet=True, workers=5)
+ self.assertTrue(compile_file_mock.called)
class EncodingTest(unittest.TestCase):
"""Issue 6716: compileall should escape source code when printing errors
@@ -231,11 +266,11 @@ class CommandLineTests(unittest.TestCase):
self.assertNotIn(b'Listing ', quiet)
# Ensure that the default behavior of compileall's CLI is to create
- # PEP 3147 pyc/pyo files.
+ # PEP 3147/PEP 488 pyc files.
for name, ext, switch in [
('normal', 'pyc', []),
- ('optimize', 'pyo', ['-O']),
- ('doubleoptimize', 'pyo', ['-OO']),
+ ('optimize', 'opt-1.pyc', ['-O']),
+ ('doubleoptimize', 'opt-2.pyc', ['-OO']),
]:
def f(self, ext=ext, switch=switch):
script_helper.assert_python_ok(*(switch +
@@ -252,13 +287,12 @@ class CommandLineTests(unittest.TestCase):
def test_legacy_paths(self):
# Ensure that with the proper switch, compileall leaves legacy
- # pyc/pyo files, and no __pycache__ directory.
+ # pyc files, and no __pycache__ directory.
self.assertRunOK('-b', '-q', self.pkgdir)
# Verify the __pycache__ directory contents.
self.assertFalse(os.path.exists(self.pkgdir_cachedir))
- opt = 'c' if __debug__ else 'o'
- expected = sorted(['__init__.py', '__init__.py' + opt, 'bar.py',
- 'bar.py' + opt])
+ expected = sorted(['__init__.py', '__init__.pyc', 'bar.py',
+ 'bar.pyc'])
self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
def test_multiple_runs(self):
@@ -301,12 +335,53 @@ class CommandLineTests(unittest.TestCase):
self.assertCompiled(subinitfn)
self.assertCompiled(hamfn)
+ def test_recursion_limit(self):
+ subpackage = os.path.join(self.pkgdir, 'spam')
+ subpackage2 = os.path.join(subpackage, 'ham')
+ subpackage3 = os.path.join(subpackage2, 'eggs')
+ for pkg in (subpackage, subpackage2, subpackage3):
+ script_helper.make_pkg(pkg)
+
+ subinitfn = os.path.join(subpackage, '__init__.py')
+ hamfn = script_helper.make_script(subpackage, 'ham', '')
+ spamfn = script_helper.make_script(subpackage2, 'spam', '')
+ eggfn = script_helper.make_script(subpackage3, 'egg', '')
+
+ self.assertRunOK('-q', '-r 0', self.pkgdir)
+ self.assertNotCompiled(subinitfn)
+ self.assertFalse(
+ os.path.exists(os.path.join(subpackage, '__pycache__')))
+
+ self.assertRunOK('-q', '-r 1', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertNotCompiled(spamfn)
+
+ self.assertRunOK('-q', '-r 2', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertNotCompiled(eggfn)
+
+ self.assertRunOK('-q', '-r 5', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertCompiled(eggfn)
+
def test_quiet(self):
noisy = self.assertRunOK(self.pkgdir)
quiet = self.assertRunOK('-q', self.pkgdir)
self.assertNotEqual(b'', noisy)
self.assertEqual(b'', quiet)
+ def test_silent(self):
+ script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
+ _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
+ _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
+ self.assertNotEqual(b'', quiet)
+ self.assertEqual(b'', silent)
+
def test_regexp(self):
self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
self.assertNotCompiled(self.barfn)
@@ -407,6 +482,29 @@ class CommandLineTests(unittest.TestCase):
out = self.assertRunOK('badfilename')
self.assertRegex(out, b"Can't list 'badfilename'")
+ @skipUnless(_have_multiprocessing, "requires multiprocessing")
+ def test_workers(self):
+ bar2fn = script_helper.make_script(self.directory, 'bar2', '')
+ files = []
+ for suffix in range(5):
+ pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
+ os.mkdir(pkgdir)
+ fn = script_helper.make_script(pkgdir, '__init__', '')
+ files.append(script_helper.make_script(pkgdir, 'bar2', ''))
+
+ self.assertRunOK(self.directory, '-j', '0')
+ self.assertCompiled(bar2fn)
+ for file in files:
+ self.assertCompiled(file)
+
+ @mock.patch('compileall.compile_dir')
+ def test_workers_available_cores(self, compile_dir):
+ with mock.patch("sys.argv",
+ new=[sys.executable, self.directory, "-j0"]):
+ compileall.main()
+ self.assertTrue(compile_dir.called)
+ self.assertEqual(compile_dir.call_args[-1]['workers'], None)
+
if __name__ == "__main__":
unittest.main()