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

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

Issue 16104: Use multiprocessing in compileall script
Patch Set: Created 5 years, 6 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)
122
Jim.J.Jewett 2014/04/24 03:32:26 Does this really require multiprocessing to run th
Claudiu.Popa 2014/04/24 08:05:18 No, it doesn't require multiprocessing, but that's
123 @mock.patch('compileall._have_multiprocessing', False)
124 def test_compile_missing_multiprocessing(self):
125 with self.assertRaises(ValueError) as cm:
126 compileall.compile_dir(self.directory, quiet=True, processes=5)
127 self.assertEqual(str(cm.exception),
128 "multiprocessing support not available")
108 129
109 130
110 class EncodingTest(unittest.TestCase): 131 class EncodingTest(unittest.TestCase):
111 """Issue 6716: compileall should escape source code when printing errors 132 """Issue 6716: compileall should escape source code when printing errors
112 to stdout.""" 133 to stdout."""
113 134
114 def setUp(self): 135 def setUp(self):
115 self.directory = tempfile.mkdtemp() 136 self.directory = tempfile.mkdtemp()
116 self.source_path = os.path.join(self.directory, '_test.py') 137 self.source_path = os.path.join(self.directory, '_test.py')
117 with open(self.source_path, 'w', encoding='utf-8') as file: 138 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, 392 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
372 bingfn, self.barfn) 393 bingfn, self.barfn)
373 self.assertRegex(out, b'rror') 394 self.assertRegex(out, b'rror')
374 self.assertNotCompiled(bingfn) 395 self.assertNotCompiled(bingfn)
375 self.assertCompiled(self.initfn) 396 self.assertCompiled(self.initfn)
376 self.assertCompiled(self.barfn) 397 self.assertCompiled(self.barfn)
377 398
378 def test_invalid_arg_produces_message(self): 399 def test_invalid_arg_produces_message(self):
379 out = self.assertRunOK('badfilename') 400 out = self.assertRunOK('badfilename')
380 self.assertRegex(out, b"Can't list 'badfilename'") 401 self.assertRegex(out, b"Can't list 'badfilename'")
402
403 @skipUnless(_have_multiprocessing, "requires multiprocessing")
404 def test_processes(self):
405 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
406 for suffix in range(5):
407 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
408 os.mkdir(pkgdir)
409 fn = script_helper.make_script(pkgdir, '__init__', '')
410 bar2fn = script_helper.make_script(pkgdir, 'bar2', '')
411 self.assertRunOK(self.directory, '-j', '0')
412 self.assertCompiled(bar2fn)
Jim.J.Jewett 2014/04/24 03:32:26 This may just be my unfamiliarity with the testing
413
414 @mock.patch('compileall.compile_dir')
Jim.J.Jewett 2014/04/24 03:32:26 If the test above really does need multiprocessing
Claudiu.Popa 2014/04/24 08:05:18 No, because compile_dir is now a mock, so nothing
415 def test_processes_available_cores(self, compile_dir):
416 with mock.patch("sys.argv",
417 new=[sys.executable, self.directory, "-j0"]):
418 compileall.main()
419 self.assertTrue(compile_dir.called)
420 self.assertEqual(compile_dir.call_args[-1]['processes'],
421 os.cpu_count())
381 422
Jim.J.Jewett 2014/04/24 03:32:26 Is there ever any verification that the compilatio
Claudiu.Popa 2014/04/24 08:05:18 No, but I don't think that testing it is feasible.
382 423
383 if __name__ == "__main__": 424 if __name__ == "__main__":
384 unittest.main() 425 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+