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

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 from unittest import mock
eric.araujo 2014/03/12 06:48:47 Minor: please keep all the “import X” imports grou
12 from contextlib import ExitStack
11 import io 13 import io
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)
(...skipping 77 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 def test_compile_processes(self):
112 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
113 with ExitStack() as ctx:
eric.araujo 2014/03/12 06:48:47 I would use the decorator form of mock.patch: @
114 pool = ctx.enter_context(
115 mock.patch('compileall.ProcessPoolExecutor',
116 new=mock.MagicMock()))
117 as_completed = ctx.enter_context(
118 mock.patch('compileall.as_completed'))
119
120 compileall.compile_dir(self.directory, quiet=True, processes=5)
121 self.assertTrue(pool.called)
108 122
109 123
110 class EncodingTest(unittest.TestCase): 124 class EncodingTest(unittest.TestCase):
111 """Issue 6716: compileall should escape source code when printing errors 125 """Issue 6716: compileall should escape source code when printing errors
112 to stdout.""" 126 to stdout."""
113 127
114 def setUp(self): 128 def setUp(self):
115 self.directory = tempfile.mkdtemp() 129 self.directory = tempfile.mkdtemp()
116 self.source_path = os.path.join(self.directory, '_test.py') 130 self.source_path = os.path.join(self.directory, '_test.py')
117 with open(self.source_path, 'w', encoding='utf-8') as file: 131 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, 385 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
372 bingfn, self.barfn) 386 bingfn, self.barfn)
373 self.assertRegex(out, b'rror') 387 self.assertRegex(out, b'rror')
374 self.assertNotCompiled(bingfn) 388 self.assertNotCompiled(bingfn)
375 self.assertCompiled(self.initfn) 389 self.assertCompiled(self.initfn)
376 self.assertCompiled(self.barfn) 390 self.assertCompiled(self.barfn)
377 391
378 def test_invalid_arg_produces_message(self): 392 def test_invalid_arg_produces_message(self):
379 out = self.assertRunOK('badfilename') 393 out = self.assertRunOK('badfilename')
380 self.assertRegex(out, b"Can't list 'badfilename'") 394 self.assertRegex(out, b"Can't list 'badfilename'")
395
396 def test_processes(self):
397 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
398 for suffix in range(5):
399 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
400 os.mkdir(pkgdir)
401 fn = script_helper.make_script(pkgdir, '__init__', '')
402 bar2fn = script_helper.make_script(pkgdir, 'bar2', '')
403 self.assertRunOK(self.directory, '-j', '5')
404 self.assertCompiled(bar2fn)
381 405
382 406
383 if __name__ == "__main__": 407 if __name__ == "__main__":
384 unittest.main() 408 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+