Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(35138)

Side by Side Diff: Lib/test/test_compileall.py

Issue 16104: Use multiprocessing in compileall script
Patch Set: Created 5 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/compileall.py ('K') | « Lib/compileall.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 import compileall 2 import compileall
3 import importlib.util 3 import importlib.util
4 import os 4 import os
5 import py_compile 5 import py_compile
6 import shutil 6 import shutil
7 import struct 7 import struct
8 import tempfile 8 import tempfile
9 import time 9 import time
10 import unittest 10 import unittest
11 import io 11 import io
12
13 from unittest import mock, skipUnless
14 try:
15 from concurrent.futures import ProcessPoolExecutor
16 _have_multiprocessing = True
17 except ImportError:
18 _have_multiprocessing = False
12 19
13 from test import support, script_helper 20 from test import support, script_helper
14 21
15 class CompileallTests(unittest.TestCase): 22 class CompileallTests(unittest.TestCase):
16 23
17 def setUp(self): 24 def setUp(self):
18 self.directory = tempfile.mkdtemp() 25 self.directory = tempfile.mkdtemp()
19 self.source_path = os.path.join(self.directory, '_test.py') 26 self.source_path = os.path.join(self.directory, '_test.py')
20 self.bc_path = importlib.util.cache_from_source(self.source_path) 27 self.bc_path = importlib.util.cache_from_source(self.source_path)
21 with open(self.source_path, 'w') as file: 28 with open(self.source_path, 'w') as file:
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 compileall.compile_dir(self.directory, quiet=True, optimize=optimize) 105 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
99 cached = importlib.util.cache_from_source(self.source_path, 106 cached = importlib.util.cache_from_source(self.source_path,
100 debug_override=not optimize) 107 debug_override=not optimize)
101 self.assertTrue(os.path.isfile(cached)) 108 self.assertTrue(os.path.isfile(cached))
102 cached2 = importlib.util.cache_from_source(self.source_path2, 109 cached2 = importlib.util.cache_from_source(self.source_path2,
103 debug_override=not optimize) 110 debug_override=not optimize)
104 self.assertTrue(os.path.isfile(cached2)) 111 self.assertTrue(os.path.isfile(cached2))
105 cached3 = importlib.util.cache_from_source(self.source_path3, 112 cached3 = importlib.util.cache_from_source(self.source_path3,
106 debug_override=not optimize) 113 debug_override=not optimize)
107 self.assertTrue(os.path.isfile(cached3)) 114 self.assertTrue(os.path.isfile(cached3))
115
116 @skipUnless(_have_multiprocessing, "requires multiprocessing")
117 @mock.patch('compileall.ProcessPoolExecutor')
118 def test_compile_processes(self, pool_mock):
119 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
120 compileall.compile_dir(self.directory, quiet=True, processes=5)
121 self.assertTrue(pool_mock.called)
Jim.J.Jewett 2014/04/25 22:33:30 Please add an XXX comment about verifying that mor
122
123 @skipUnless(_have_multiprocessing, "requires multiprocessing")
124 @mock.patch('compileall.ProcessPoolExecutor')
125 @mock.patch('compileall.compile_file')
126 def test_compile_processes_on_one(self, compile_mock, pool_mock):
127 # Test that `compile_dir` doesn't use ProcessPoolExecutor
128 # when passing processes=1
129 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
130 compileall.compile_dir(self.directory, quiet=True, processes=1)
131 self.assertFalse(pool_mock.called)
132 self.assertTrue(compile_mock.called)
133
134 @mock.patch('compileall._have_multiprocessing', False)
135 def test_compile_missing_multiprocessing(self):
136 with self.assertRaises(ValueError) as cm:
137 compileall.compile_dir(self.directory, quiet=True, processes=5)
138 self.assertEqual(str(cm.exception),
139 "multiprocessing support not available")
Jim.J.Jewett 2014/04/25 22:33:30 Please also test 0 (because it has different logic
108 140
109 141
110 class EncodingTest(unittest.TestCase): 142 class EncodingTest(unittest.TestCase):
111 """Issue 6716: compileall should escape source code when printing errors 143 """Issue 6716: compileall should escape source code when printing errors
112 to stdout.""" 144 to stdout."""
113 145
114 def setUp(self): 146 def setUp(self):
115 self.directory = tempfile.mkdtemp() 147 self.directory = tempfile.mkdtemp()
116 self.source_path = os.path.join(self.directory, '_test.py') 148 self.source_path = os.path.join(self.directory, '_test.py')
117 with open(self.source_path, 'w', encoding='utf-8') as file: 149 with open(self.source_path, 'w', encoding='utf-8') as file:
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, 403 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
372 bingfn, self.barfn) 404 bingfn, self.barfn)
373 self.assertRegex(out, b'rror') 405 self.assertRegex(out, b'rror')
374 self.assertNotCompiled(bingfn) 406 self.assertNotCompiled(bingfn)
375 self.assertCompiled(self.initfn) 407 self.assertCompiled(self.initfn)
376 self.assertCompiled(self.barfn) 408 self.assertCompiled(self.barfn)
377 409
378 def test_invalid_arg_produces_message(self): 410 def test_invalid_arg_produces_message(self):
379 out = self.assertRunOK('badfilename') 411 out = self.assertRunOK('badfilename')
380 self.assertRegex(out, b"Can't list 'badfilename'") 412 self.assertRegex(out, b"Can't list 'badfilename'")
413
414 @skipUnless(_have_multiprocessing, "requires multiprocessing")
415 def test_processes(self):
416 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
417 files = []
418 for suffix in range(5):
419 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
420 os.mkdir(pkgdir)
421 fn = script_helper.make_script(pkgdir, '__init__', '')
422 files.append(script_helper.make_script(pkgdir, 'bar2', ''))
423
424 self.assertRunOK(self.directory, '-j', '0')
425 self.assertCompiled(bar2fn)
426 for file in files:
427 self.assertCompiled(file)
428
429 @mock.patch('compileall.compile_dir')
430 def test_processes_available_cores(self, compile_dir):
431 with mock.patch("sys.argv",
432 new=[sys.executable, self.directory, "-j0"]):
433 compileall.main()
434 self.assertTrue(compile_dir.called)
435 self.assertEqual(compile_dir.call_args[-1]['processes'],
436 os.cpu_count())
381 437
382 438
383 if __name__ == "__main__": 439 if __name__ == "__main__":
384 unittest.main() 440 unittest.main()
OLDNEW
« Lib/compileall.py ('K') | « Lib/compileall.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+