bpo-40275: lazy import modules in test.support (GH-20128)

Automerge-Triggered-By: @vstinner
This commit is contained in:
Hai Shi 2020-05-16 18:01:39 +08:00 committed by GitHub
parent 4541086232
commit 372fa3ead5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -6,23 +6,19 @@ if __name__ != 'test.support':
import collections.abc import collections.abc
import contextlib import contextlib
import errno import errno
import faulthandler
import fnmatch import fnmatch
import functools import functools
import gc
import glob import glob
import importlib import importlib
import importlib.util import importlib.util
import os import os
import platform import platform
import re import re
import shutil
import stat import stat
import struct import struct
import subprocess import subprocess
import sys import sys
import sysconfig import sysconfig
import tempfile
import _thread import _thread
import threading import threading
import time import time
@ -439,6 +435,7 @@ else:
_rmdir = os.rmdir _rmdir = os.rmdir
def _rmtree(path): def _rmtree(path):
import shutil
try: try:
shutil.rmtree(path) shutil.rmtree(path)
return return
@ -891,6 +888,7 @@ def temp_dir(path=None, quiet=False):
created, only a warning is issued. created, only a warning is issued.
""" """
import tempfile
dir_created = False dir_created = False
if path is None: if path is None:
path = tempfile.mkdtemp() path = tempfile.mkdtemp()
@ -1436,6 +1434,7 @@ def gc_collect():
longer than expected. This function tries its best to force all garbage longer than expected. This function tries its best to force all garbage
objects to disappear. objects to disappear.
""" """
import gc
gc.collect() gc.collect()
if is_jython: if is_jython:
time.sleep(0.1) time.sleep(0.1)
@ -1444,6 +1443,7 @@ def gc_collect():
@contextlib.contextmanager @contextlib.contextmanager
def disable_gc(): def disable_gc():
import gc
have_gc = gc.isenabled() have_gc = gc.isenabled()
gc.disable() gc.disable()
try: try:
@ -2136,6 +2136,7 @@ def reap_children():
@contextlib.contextmanager @contextlib.contextmanager
def start_threads(threads, unlock=None): def start_threads(threads, unlock=None):
import faulthandler
threads = list(threads) threads = list(threads)
started = [] started = []
try: try:
@ -2401,6 +2402,7 @@ class PythonSymlink:
_can_xattr = None _can_xattr = None
def can_xattr(): def can_xattr():
import tempfile
global _can_xattr global _can_xattr
if _can_xattr is not None: if _can_xattr is not None:
return _can_xattr return _can_xattr
@ -2445,6 +2447,7 @@ def skip_if_pgo_task(test):
def fs_is_case_insensitive(directory): def fs_is_case_insensitive(directory):
"""Detects if the file system for the specified directory is case-insensitive.""" """Detects if the file system for the specified directory is case-insensitive."""
import tempfile
with tempfile.NamedTemporaryFile(dir=directory) as base: with tempfile.NamedTemporaryFile(dir=directory) as base:
base_path = base.name base_path = base.name
case_path = base_path.upper() case_path = base_path.upper()
@ -2740,6 +2743,8 @@ def setswitchinterval(interval):
@contextlib.contextmanager @contextlib.contextmanager
def disable_faulthandler(): def disable_faulthandler():
import faulthandler
# use sys.__stderr__ instead of sys.stderr, since regrtest replaces # use sys.__stderr__ instead of sys.stderr, since regrtest replaces
# sys.stderr with a StringIO which has no file descriptor when a test # sys.stderr with a StringIO which has no file descriptor when a test
# is run with -W/--verbose3. # is run with -W/--verbose3.