Skip to content

Commit 879f5f3

Browse files
bpo-29877: compileall: import ProcessPoolExecutor only when needed (GH-4856)
Importing ProcessPoolExecutor may hang or cause an error when the import accesses urandom on a low resource platform https://bugs.python.org/issue29877 (cherry picked from commit 1d817e4) Co-authored-by: Dustin Spicuzza <[email protected]>
1 parent 832da87 commit 879f5f3

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Lib/compileall.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
import py_compile
1717
import struct
1818

19-
try:
20-
from concurrent.futures import ProcessPoolExecutor
21-
except ImportError:
22-
ProcessPoolExecutor = None
2319
from functools import partial
2420

2521
__all__ = ["compile_dir","compile_file","compile_path"]
@@ -68,9 +64,17 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
6864
optimize: optimization level or -1 for level of the interpreter
6965
workers: maximum number of parallel workers
7066
"""
71-
if workers is not None and workers < 0:
72-
raise ValueError('workers must be greater or equal to 0')
73-
67+
ProcessPoolExecutor = None
68+
if workers is not None:
69+
if workers < 0:
70+
raise ValueError('workers must be greater or equal to 0')
71+
elif workers != 1:
72+
try:
73+
# Only import when needed, as low resource platforms may
74+
# fail to import it
75+
from concurrent.futures import ProcessPoolExecutor
76+
except ImportError:
77+
workers = 1
7478
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
7579
ddir=ddir)
7680
success = True

Lib/test/test_compileall.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def test_compile_dir_pathlike(self):
161161
self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
162162
self.assertTrue(os.path.isfile(self.bc_path))
163163

164-
@mock.patch('compileall.ProcessPoolExecutor')
164+
@mock.patch('concurrent.futures.ProcessPoolExecutor')
165165
def test_compile_pool_called(self, pool_mock):
166166
compileall.compile_dir(self.directory, quiet=True, workers=5)
167167
self.assertTrue(pool_mock.called)
@@ -171,19 +171,19 @@ def test_compile_workers_non_positive(self):
171171
"workers must be greater or equal to 0"):
172172
compileall.compile_dir(self.directory, workers=-1)
173173

174-
@mock.patch('compileall.ProcessPoolExecutor')
174+
@mock.patch('concurrent.futures.ProcessPoolExecutor')
175175
def test_compile_workers_cpu_count(self, pool_mock):
176176
compileall.compile_dir(self.directory, quiet=True, workers=0)
177177
self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
178178

179-
@mock.patch('compileall.ProcessPoolExecutor')
179+
@mock.patch('concurrent.futures.ProcessPoolExecutor')
180180
@mock.patch('compileall.compile_file')
181181
def test_compile_one_worker(self, compile_file_mock, pool_mock):
182182
compileall.compile_dir(self.directory, quiet=True)
183183
self.assertFalse(pool_mock.called)
184184
self.assertTrue(compile_file_mock.called)
185185

186-
@mock.patch('compileall.ProcessPoolExecutor', new=None)
186+
@mock.patch('concurrent.futures.ProcessPoolExecutor', new=None)
187187
@mock.patch('compileall.compile_file')
188188
def test_compile_missing_multiprocessing(self, compile_file_mock):
189189
compileall.compile_dir(self.directory, quiet=True, workers=5)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compileall: import ProcessPoolExecutor only when needed, preventing hangs on
2+
low resource platforms

0 commit comments

Comments
 (0)