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

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

Issue 16104: Use multiprocessing in compileall script
Patch Set: Created 5 years, 4 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")
berkerpeksag 2014/04/27 15:57:05 This should be @mock.patch('compileall.Proces
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)
122
123 def test_compile_processes_non_positive(self):
124 # Test that `compile_dir` doesn't use ProcessPoolExecutor
125 # when passing processes=1
126 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
127 for value in (0, -1):
128 with self.assertRaises(ValueError) as cm:
129 compileall.compile_dir(self.directory, processes=value)
130 self.assertEqual(str(cm.exception),
131 "processes must be greater than 0")
132
133 @mock.patch('compileall.ProcessPoolExecutor', None)
berkerpeksag 2014/04/27 15:57:05 It would be good if you use a keyword-only argumen
134 def test_compile_missing_multiprocessing(self):
135 with self.assertRaises(ValueError) as cm:
136 compileall.compile_dir(self.directory, quiet=True, processes=5)
137 self.assertEqual(str(cm.exception),
138 "multiprocessing support not available")
108 139
109 140
110 class EncodingTest(unittest.TestCase): 141 class EncodingTest(unittest.TestCase):
111 """Issue 6716: compileall should escape source code when printing errors 142 """Issue 6716: compileall should escape source code when printing errors
112 to stdout.""" 143 to stdout."""
113 144
114 def setUp(self): 145 def setUp(self):
115 self.directory = tempfile.mkdtemp() 146 self.directory = tempfile.mkdtemp()
116 self.source_path = os.path.join(self.directory, '_test.py') 147 self.source_path = os.path.join(self.directory, '_test.py')
117 with open(self.source_path, 'w', encoding='utf-8') as file: 148 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, 402 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
372 bingfn, self.barfn) 403 bingfn, self.barfn)
373 self.assertRegex(out, b'rror') 404 self.assertRegex(out, b'rror')
374 self.assertNotCompiled(bingfn) 405 self.assertNotCompiled(bingfn)
375 self.assertCompiled(self.initfn) 406 self.assertCompiled(self.initfn)
376 self.assertCompiled(self.barfn) 407 self.assertCompiled(self.barfn)
377 408
378 def test_invalid_arg_produces_message(self): 409 def test_invalid_arg_produces_message(self):
379 out = self.assertRunOK('badfilename') 410 out = self.assertRunOK('badfilename')
380 self.assertRegex(out, b"Can't list 'badfilename'") 411 self.assertRegex(out, b"Can't list 'badfilename'")
412
413 @skipUnless(_have_multiprocessing, "requires multiprocessing")
414 def test_processes(self):
415 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
416 files = []
417 for suffix in range(5):
418 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
419 os.mkdir(pkgdir)
420 fn = script_helper.make_script(pkgdir, '__init__', '')
421 files.append(script_helper.make_script(pkgdir, 'bar2', ''))
422
423 self.assertRunOK(self.directory, '-j', '0')
424 self.assertCompiled(bar2fn)
425 for file in files:
426 self.assertCompiled(file)
427
428 @mock.patch('compileall.compile_dir')
429 def test_processes_available_cores(self, compile_dir):
430 with mock.patch("sys.argv",
431 new=[sys.executable, self.directory, "-j0"]):
432 compileall.main()
433 self.assertTrue(compile_dir.called)
434 self.assertEqual(compile_dir.call_args[-1]['processes'],
435 os.cpu_count())
381 436
382 437
383 if __name__ == "__main__": 438 if __name__ == "__main__":
384 unittest.main() 439 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+