Skip to content

Commit 1d817e4

Browse files
virtualdmiss-islington
authored andcommitted
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
1 parent 9de3632 commit 1d817e4

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"]
@@ -70,9 +66,17 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
7066
workers: maximum number of parallel workers
7167
invalidation_mode: how the up-to-dateness of the pyc will be checked
7268
"""
73-
if workers is not None and workers < 0:
74-
raise ValueError('workers must be greater or equal to 0')
75-
69+
ProcessPoolExecutor = None
70+
if workers is not None:
71+
if workers < 0:
72+
raise ValueError('workers must be greater or equal to 0')
73+
elif workers != 1:
74+
try:
75+
# Only import when needed, as low resource platforms may
76+
# fail to import it
77+
from concurrent.futures import ProcessPoolExecutor
78+
except ImportError:
79+
workers = 1
7680
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
7781
ddir=ddir)
7882
success = True

Lib/test/test_compileall.py

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

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

180-
@mock.patch('compileall.ProcessPoolExecutor')
180+
@mock.patch('concurrent.futures.ProcessPoolExecutor')
181181
def test_compile_workers_cpu_count(self, pool_mock):
182182
compileall.compile_dir(self.directory, quiet=True, workers=0)
183183
self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
184184

185-
@mock.patch('compileall.ProcessPoolExecutor')
185+
@mock.patch('concurrent.futures.ProcessPoolExecutor')
186186
@mock.patch('compileall.compile_file')
187187
def test_compile_one_worker(self, compile_file_mock, pool_mock):
188188
compileall.compile_dir(self.directory, quiet=True)
189189
self.assertFalse(pool_mock.called)
190190
self.assertTrue(compile_file_mock.called)
191191

192-
@mock.patch('compileall.ProcessPoolExecutor', new=None)
192+
@mock.patch('concurrent.futures.ProcessPoolExecutor', new=None)
193193
@mock.patch('compileall.compile_file')
194194
def test_compile_missing_multiprocessing(self, compile_file_mock):
195195
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)