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

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
12 14
13 from test import support, script_helper 15 from test import support, script_helper
14 16
15 class CompileallTests(unittest.TestCase): 17 class CompileallTests(unittest.TestCase):
16 18
17 def setUp(self): 19 def setUp(self):
18 self.directory = tempfile.mkdtemp() 20 self.directory = tempfile.mkdtemp()
19 self.source_path = os.path.join(self.directory, '_test.py') 21 self.source_path = os.path.join(self.directory, '_test.py')
20 self.bc_path = importlib.util.cache_from_source(self.source_path) 22 self.bc_path = importlib.util.cache_from_source(self.source_path)
21 with open(self.source_path, 'w') as file: 23 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) 100 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
99 cached = importlib.util.cache_from_source(self.source_path, 101 cached = importlib.util.cache_from_source(self.source_path,
100 debug_override=not optimize) 102 debug_override=not optimize)
101 self.assertTrue(os.path.isfile(cached)) 103 self.assertTrue(os.path.isfile(cached))
102 cached2 = importlib.util.cache_from_source(self.source_path2, 104 cached2 = importlib.util.cache_from_source(self.source_path2,
103 debug_override=not optimize) 105 debug_override=not optimize)
104 self.assertTrue(os.path.isfile(cached2)) 106 self.assertTrue(os.path.isfile(cached2))
105 cached3 = importlib.util.cache_from_source(self.source_path3, 107 cached3 = importlib.util.cache_from_source(self.source_path3,
106 debug_override=not optimize) 108 debug_override=not optimize)
107 self.assertTrue(os.path.isfile(cached3)) 109 self.assertTrue(os.path.isfile(cached3))
110
111 @mock.patch('compileall.ProcessPoolExecutor')
112 def test_compile_processes(self, pool_mock):
113 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
114 compileall.compile_dir(self.directory, quiet=True, processes=5)
115 self.assertTrue(pool_mock.called)
108 116
109 117
110 class EncodingTest(unittest.TestCase): 118 class EncodingTest(unittest.TestCase):
111 """Issue 6716: compileall should escape source code when printing errors 119 """Issue 6716: compileall should escape source code when printing errors
112 to stdout.""" 120 to stdout."""
113 121
114 def setUp(self): 122 def setUp(self):
115 self.directory = tempfile.mkdtemp() 123 self.directory = tempfile.mkdtemp()
116 self.source_path = os.path.join(self.directory, '_test.py') 124 self.source_path = os.path.join(self.directory, '_test.py')
117 with open(self.source_path, 'w', encoding='utf-8') as file: 125 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, 379 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
372 bingfn, self.barfn) 380 bingfn, self.barfn)
373 self.assertRegex(out, b'rror') 381 self.assertRegex(out, b'rror')
374 self.assertNotCompiled(bingfn) 382 self.assertNotCompiled(bingfn)
375 self.assertCompiled(self.initfn) 383 self.assertCompiled(self.initfn)
376 self.assertCompiled(self.barfn) 384 self.assertCompiled(self.barfn)
377 385
378 def test_invalid_arg_produces_message(self): 386 def test_invalid_arg_produces_message(self):
379 out = self.assertRunOK('badfilename') 387 out = self.assertRunOK('badfilename')
380 self.assertRegex(out, b"Can't list 'badfilename'") 388 self.assertRegex(out, b"Can't list 'badfilename'")
389
390 def test_processes(self):
391 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
392 for suffix in range(5):
393 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
394 os.mkdir(pkgdir)
395 fn = script_helper.make_script(pkgdir, '__init__', '')
396 bar2fn = script_helper.make_script(pkgdir, 'bar2', '')
397 self.assertRunOK(self.directory, '-j', '0')
398 self.assertCompiled(bar2fn)
399
400 @mock.patch('compileall.compile_dir')
401 def test_processes_available_cores(self, compile_dir):
402 oldargv = sys.argv
403 def fixup():
404 sys.argv = oldargv
405 self.addCleanup(fixup)
eric.araujo 2014/03/13 00:01:45 mock can replace these four lines with one.
406 sys.argv = [sys.executable, self.directory, "-j0"]
407
408 compileall.main()
409 self.assertTrue(compile_dir.called)
410 self.assertEqual(compile_dir.call_args[-1]['processes'],
411 os.cpu_count())
381 412
382 413
383 if __name__ == "__main__": 414 if __name__ == "__main__":
384 unittest.main() 415 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+