Skip to content

Commit aa89063

Browse files
authored
bpo-40275: test.support imports subprocess lazily (GH-20471)
test.support module now imports the platform and subprocess modules lazily to reduce the number of modules imported by "import test.support". With this change, the threading module is no longer imported indirectly by "import test.support". Use sys.version rather than platform.machine() to detect the Windows ARM32 buildbot.
1 parent b0461e1 commit aa89063

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

‎Lib/test/support/__init__.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
import importlib
1313
import importlib.util
1414
import os
15-
import platform
1615
import re
1716
import stat
1817
import struct
19-
import subprocess
2018
import sys
2119
import sysconfig
2220
import time
@@ -81,7 +79,7 @@
8179
# The timeout should be long enough for connect(), recv() and send() methods
8280
# of socket.socket.
8381
LOOPBACK_TIMEOUT = 5.0
84-
if sys.platform == 'win32' and platform.machine() == 'ARM':
82+
if sys.platform == 'win32' and ' 32 bit (ARM)' in sys.version:
8583
# bpo-37553: test_socket.SendfileUsingSendTest is taking longer than 2
8684
# seconds on Windows ARM32 buildbot
8785
LOOPBACK_TIMEOUT = 10
@@ -481,6 +479,7 @@ def forget(modname):
481479
def _is_gui_available():
482480
if hasattr(_is_gui_available, 'result'):
483481
return _is_gui_available.result
482+
import platform
484483
reason = None
485484
if sys.platform.startswith('win') and platform.win32_is_iot():
486485
reason = "gui is not available on Windows IoT Core"
@@ -581,6 +580,7 @@ def _requires_unix_version(sysname, min_version):
581580
def decorator(func):
582581
@functools.wraps(func)
583582
def wrapper(*args, **kw):
583+
import platform
584584
if platform.system() == sysname:
585585
version_txt = platform.release().split('-', 1)[0]
586586
try:
@@ -627,6 +627,7 @@ def decorator(func):
627627
@functools.wraps(func)
628628
def wrapper(*args, **kw):
629629
if sys.platform == 'darwin':
630+
import platform
630631
version_txt = platform.mac_ver()[0]
631632
try:
632633
version = tuple(map(int, version_txt.split('.')))
@@ -1607,6 +1608,7 @@ def start(self):
16071608
sys.stderr.flush()
16081609
return
16091610

1611+
import subprocess
16101612
with f:
16111613
watchdog_script = findfile("memory_watchdog.py")
16121614
self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script],
@@ -2088,11 +2090,13 @@ def swap_item(obj, item, new_val):
20882090
def args_from_interpreter_flags():
20892091
"""Return a list of command-line arguments reproducing the current
20902092
settings in sys.flags and sys.warnoptions."""
2093+
import subprocess
20912094
return subprocess._args_from_interpreter_flags()
20922095

20932096
def optim_args_from_interpreter_flags():
20942097
"""Return a list of command-line arguments reproducing the current
20952098
optimization settings in sys.flags."""
2099+
import subprocess
20962100
return subprocess._optim_args_from_interpreter_flags()
20972101

20982102

@@ -2233,6 +2237,7 @@ def __exit__(self, exc_type, exc_value, exc_tb):
22332237
print("failed to clean up {}: {}".format(link, ex))
22342238

22352239
def _call(self, python, args, env, returncode):
2240+
import subprocess
22362241
cmd = [python, *args]
22372242
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
22382243
stderr=subprocess.PIPE, env=env)
@@ -2261,6 +2266,7 @@ def can_xattr():
22612266
if not hasattr(os, "setxattr"):
22622267
can = False
22632268
else:
2269+
import platform
22642270
tmp_dir = tempfile.mkdtemp()
22652271
tmp_fp, tmp_name = tempfile.mkstemp(dir=tmp_dir)
22662272
try:
@@ -2445,6 +2451,7 @@ def __enter__(self):
24452451
pass
24462452

24472453
if sys.platform == 'darwin':
2454+
import subprocess
24482455
# Check if the 'Crash Reporter' on OSX was configured
24492456
# in 'Developer' mode and warn that it will get triggered
24502457
# when it is.
@@ -2591,6 +2598,7 @@ def setswitchinterval(interval):
25912598
if is_android and interval < minimum_interval:
25922599
global _is_android_emulator
25932600
if _is_android_emulator is None:
2601+
import subprocess
25942602
_is_android_emulator = (subprocess.check_output(
25952603
['getprop', 'ro.kernel.qemu']).strip() == b'1')
25962604
if _is_android_emulator:

0 commit comments

Comments
 (0)