Skip to content

Commit 6f98288

Browse files
committed
test: use import_helper
1 parent c2baf11 commit 6f98288

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+160
-135
lines changed

Lib/test/libregrtest/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from test.libregrtest.setup import setup_tests
2121
from test.libregrtest.utils import removepy, count, format_duration, printlist
2222
from test import support
23-
from test.support import os_helper
23+
from test.support import os_helper, import_helper
2424

2525

2626
# When tests are run from the Python build directory, it is best practice
@@ -412,7 +412,7 @@ def run_tests_sequential(self):
412412
# Unload the newly imported modules (best effort finalization)
413413
for module in sys.modules.keys():
414414
if module not in save_modules and module.startswith("test."):
415-
support.unload(module)
415+
import_helper.unload(module)
416416

417417
if previous_test:
418418
print(previous_test)

Lib/test/libregrtest/runtest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import unittest
1212

1313
from test import support
14-
from test.support import os_helper
14+
from test.support import os_helper, import_helper
1515
from test.libregrtest.refleak import dash_R, clear_caches
1616
from test.libregrtest.save_env import saved_test_environment
1717
from test.libregrtest.utils import print_warning
@@ -202,7 +202,7 @@ def _runtest_inner2(ns, test_name):
202202
abstest = get_abs_module(ns, test_name)
203203

204204
# remove the module from sys.module to reload it if it was already imported
205-
support.unload(abstest)
205+
import_helper.unload(abstest)
206206

207207
the_module = importlib.import_module(abstest)
208208

Lib/test/support/script_helper.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import zipfile
1212

1313
from importlib.util import source_from_cache
14-
from test.support import make_legacy_pyc, strip_python_stderr
14+
from test.support import strip_python_stderr
15+
from test.support.import_helper import make_legacy_pyc
1516

1617

1718
# Cached result of the expensive test performed in the function below.

Lib/test/test_asyncgen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import types
33
import unittest
44

5-
from test.support import import_module
5+
from test.support.import_helper import import_module
66
asyncio = import_module("asyncio")
77

88

Lib/test/test_bisect.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import sys
22
import unittest
33
from test import support
4+
from test.support import import_helper
45
from collections import UserList
56

6-
py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
7-
c_bisect = support.import_fresh_module('bisect', fresh=['bisect'])
7+
py_bisect = import_helper.import_fresh_module('bisect', blocked=['_bisect'])
8+
c_bisect = import_helper.import_fresh_module('bisect', fresh=['bisect'])
89

910
class Range(object):
1011
"""A trivial range()-like object that has an insert() method."""

Lib/test/test_bytes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import test.support
1919
import test.string_tests
2020
import test.list_tests
21-
from test.support import bigaddrspacetest, MAX_Py_ssize_t
21+
from test.support import bigaddrspacetest, MAX_Py_ssize_t, import_helper
2222
from test.support.script_helper import assert_python_failure
2323

2424

@@ -977,7 +977,7 @@ def test_translate(self):
977977
self.assertEqual(c, b'hllo')
978978

979979
def test_sq_item(self):
980-
_testcapi = test.support.import_module('_testcapi')
980+
_testcapi = import_helper.import_module('_testcapi')
981981
obj = self.type2test((42,))
982982
with self.assertRaises(IndexError):
983983
_testcapi.sequence_getitem(obj, -2)
@@ -1034,8 +1034,8 @@ def __bytes__(self):
10341034

10351035
# Test PyBytes_FromFormat()
10361036
def test_from_format(self):
1037-
ctypes = test.support.import_module('ctypes')
1038-
_testcapi = test.support.import_module('_testcapi')
1037+
ctypes = import_helper.import_module('ctypes')
1038+
_testcapi = import_helper.import_module('_testcapi')
10391039
from ctypes import pythonapi, py_object
10401040
from ctypes import (
10411041
c_int, c_uint,

Lib/test/test_cmd.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import unittest
1010
import io
1111
from test import support
12+
from test.support import import_helper
1213

1314
class samplecmdclass(cmd.Cmd):
1415
"""
@@ -225,7 +226,7 @@ def test_main(verbose=None):
225226
support.run_unittest(TestAlternateInput)
226227

227228
def test_coverage(coverdir):
228-
trace = support.import_module('trace')
229+
trace = import_helper.import_module('trace')
229230
tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
230231
trace=0, count=1)
231232
tracer.run('import importlib; importlib.reload(cmd); test_main()')

Lib/test/test_cmd_line.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def test_xdev(self):
718718

719719
def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False):
720720
if use_pywarning:
721-
code = ("import sys; from test.support import import_fresh_module; "
721+
code = ("import sys; from test.support.import_helper import import_fresh_module; "
722722
"warnings = import_fresh_module('warnings', blocked=['_warnings']); ")
723723
else:
724724
code = "import sys, warnings; "

Lib/test/test_cmd_line_script.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from test.support.script_helper import (
1818
make_pkg, make_script, make_zip_pkg, make_zip_script,
1919
assert_python_ok, assert_python_failure, spawn_python, kill_python)
20-
from test.support import os_helper
20+
from test.support import os_helper, import_helper
2121

2222
verbose = support.verbose
2323

@@ -238,7 +238,7 @@ def test_script_compiled(self):
238238
script_name = _make_test_script(script_dir, 'script')
239239
py_compile.compile(script_name, doraise=True)
240240
os.remove(script_name)
241-
pyc_file = support.make_legacy_pyc(script_name)
241+
pyc_file = import_helper.make_legacy_pyc(script_name)
242242
self._check_script(pyc_file, pyc_file,
243243
pyc_file, script_dir, None,
244244
importlib.machinery.SourcelessFileLoader)
@@ -255,7 +255,7 @@ def test_directory_compiled(self):
255255
script_name = _make_test_script(script_dir, '__main__')
256256
py_compile.compile(script_name, doraise=True)
257257
os.remove(script_name)
258-
pyc_file = support.make_legacy_pyc(script_name)
258+
pyc_file = import_helper.make_legacy_pyc(script_name)
259259
self._check_script(script_dir, pyc_file, script_dir,
260260
script_dir, '',
261261
importlib.machinery.SourcelessFileLoader)
@@ -355,7 +355,7 @@ def test_package_compiled(self):
355355
script_name = _make_test_script(pkg_dir, '__main__')
356356
compiled_name = py_compile.compile(script_name, doraise=True)
357357
os.remove(script_name)
358-
pyc_file = support.make_legacy_pyc(script_name)
358+
pyc_file = import_helper.make_legacy_pyc(script_name)
359359
self._check_script(["-m", "test_pkg"], pyc_file,
360360
pyc_file, script_dir, 'test_pkg',
361361
importlib.machinery.SourcelessFileLoader,

Lib/test/test_code_module.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from textwrap import dedent
55
from contextlib import ExitStack
66
from unittest import mock
7-
from test import support
7+
from test.support import import_helper
88

9-
code = support.import_module('code')
9+
code = import_helper.import_module('code')
1010

1111

1212
class TestInteractiveConsole(unittest.TestCase):

Lib/test/test_dbm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import unittest
44
import glob
55
import test.support
6-
from test.support import os_helper
6+
from test.support import os_helper, import_helper
77

88
# Skip tests if dbm module doesn't exist.
9-
dbm = test.support.import_module('dbm')
9+
dbm = import_helper.import_module('dbm')
1010

1111
try:
1212
from dbm import ndbm
@@ -181,7 +181,7 @@ def setUp(self):
181181
self.filename = os_helper.TESTFN
182182
self.d = dbm.open(self.filename, 'c')
183183
self.d.close()
184-
self.dbm = test.support.import_fresh_module('dbm')
184+
self.dbm = import_helper.import_fresh_module('dbm')
185185

186186
def test_keys(self):
187187
self.d = dbm.open(self.filename, 'c')

Lib/test/test_decimal.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
import locale
3535
from test.support import (run_unittest, run_doctest, is_resource_enabled,
3636
requires_IEEE_754, requires_docstrings)
37-
from test.support import (import_fresh_module, TestFailed,
38-
run_with_locale, cpython_only)
37+
from test.support import (TestFailed, run_with_locale,
38+
cpython_only)
39+
from test.support.import_helper import import_fresh_module
3940
import random
4041
import inspect
4142
import threading

Lib/test/test_exceptions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
cpython_only, gc_collect,
1313
no_tracing, script_helper,
1414
SuppressCrashReport,
15-
import_module, check_warnings,)
15+
check_warnings,)
1616
from test.support.os_helper import TESTFN, unlink
17+
from test.support.import_helper import import_module
1718
from test import support
1819

1920
class NaiveException(Exception):

Lib/test/test_functools.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from random import choice
99
import sys
1010
from test import support
11+
from test.support import import_helper
1112
import threading
1213
import time
1314
import typing
@@ -18,10 +19,10 @@
1819

1920
import functools
2021

21-
py_functools = support.import_fresh_module('functools', blocked=['_functools'])
22-
c_functools = support.import_fresh_module('functools', fresh=['_functools'])
22+
py_functools = import_helper.import_fresh_module('functools', blocked=['_functools'])
23+
c_functools = import_helper.import_fresh_module('functools', fresh=['_functools'])
2324

24-
decimal = support.import_fresh_module('decimal', fresh=['_decimal'])
25+
decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])
2526

2627
@contextlib.contextmanager
2728
def replaced_module(name, replacement):

Lib/test/test_future.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ast
55
import unittest
66
from test import support
7+
from test.support import import_helper
78
from textwrap import dedent
89
import os
910
import re
@@ -24,17 +25,17 @@ def check_syntax_error(self, err, basename, lineno, offset=1):
2425
self.assertEqual(err.offset, offset)
2526

2627
def test_future1(self):
27-
with support.CleanImport('future_test1'):
28+
with import_helper.CleanImport('future_test1'):
2829
from test import future_test1
2930
self.assertEqual(future_test1.result, 6)
3031

3132
def test_future2(self):
32-
with support.CleanImport('future_test2'):
33+
with import_helper.CleanImport('future_test2'):
3334
from test import future_test2
3435
self.assertEqual(future_test2.result, 6)
3536

3637
def test_future3(self):
37-
with support.CleanImport('test_future3'):
38+
with import_helper.CleanImport('test_future3'):
3839
from test import test_future3
3940

4041
def test_badfuture3(self):
@@ -120,7 +121,7 @@ def test_parserhack(self):
120121
self.fail("syntax error didn't occur")
121122

122123
def test_multiple_features(self):
123-
with support.CleanImport("test.test_future5"):
124+
with import_helper.CleanImport("test.test_future5"):
124125
from test import test_future5
125126

126127
def test_unicode_literals_exec(self):

Lib/test/test_gzip.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import unittest
1212
from subprocess import PIPE, Popen
1313
from test import support
14-
from test.support import _4G, bigmemtest, os_helper
14+
from test.support import _4G, bigmemtest, os_helper, import_helper
1515
from test.support.script_helper import assert_python_ok, assert_python_failure
1616

17-
gzip = support.import_module('gzip')
17+
gzip = import_helper.import_module('gzip')
1818

1919
data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
2020
PyObject *RetVal;

Lib/test/test_hashlib.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import unittest
1919
import warnings
2020
from test import support
21-
from test.support import _4G, bigmemtest, import_fresh_module
21+
from test.support import _4G, bigmemtest
22+
from test.support.import_helper import import_fresh_module
2223
from http.client import HTTPException
2324

2425
# Were we compiled --with-pydebug or with #define Py_DEBUG?

Lib/test/test_heapq.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import doctest
66

77
from test import support
8+
from test.support import import_helper
89
from unittest import TestCase, skipUnless
910
from operator import itemgetter
1011

11-
py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
12-
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
12+
py_heapq = import_helper.import_fresh_module('heapq', blocked=['_heapq'])
13+
c_heapq = import_helper.import_fresh_module('heapq', fresh=['_heapq'])
1314

1415
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
1516
# _heapq is imported, so check them there

Lib/test/test_imp.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import py_compile
66
import sys
77
from test import support
8-
from test.support import script_helper, os_helper
8+
from test.support import script_helper, os_helper, import_helper
99
import unittest
1010
import warnings
1111
with warnings.catch_warnings():
@@ -211,7 +211,7 @@ def test_issue9319(self):
211211

212212
def test_load_from_source(self):
213213
# Verify that the imp module can correctly load and find .py files
214-
# XXX (ncoghlan): It would be nice to use support.CleanImport
214+
# XXX (ncoghlan): It would be nice to use import_helper.CleanImport
215215
# here, but that breaks because the os module registers some
216216
# handlers in copy_reg on import. Since CleanImport doesn't
217217
# revert that registration, the module is left in a broken
@@ -408,12 +408,12 @@ def test_source(self):
408408
imp.reload(os)
409409

410410
def test_extension(self):
411-
with support.CleanImport('time'):
411+
with import_helper.CleanImport('time'):
412412
import time
413413
imp.reload(time)
414414

415415
def test_builtin(self):
416-
with support.CleanImport('marshal'):
416+
with import_helper.CleanImport('marshal'):
417417
import marshal
418418
imp.reload(marshal)
419419

Lib/test/test_importlib/import_/test_packages.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import unittest
44
from test import support
5+
from test.support import import_helper
56

67

78
class ParentModuleTests:
@@ -98,7 +99,7 @@ def module_injection():
9899
try:
99100
submodule = self.__import__(subname)
100101
finally:
101-
support.unload(subname)
102+
import_helper.unload(subname)
102103

103104

104105
(Frozen_ParentTests,

Lib/test/test_importlib/source/test_file_loader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import unittest
1818
import warnings
1919

20-
from test.support import make_legacy_pyc, unload
20+
from test.support.import_helper import unload, make_legacy_pyc
2121

2222
from test.test_py_compile import without_source_date_epoch
2323
from test.test_py_compile import SourceDateEpochTestMeta

Lib/test/test_importlib/source/test_finder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import stat
1010
import sys
1111
import tempfile
12-
from test.support import make_legacy_pyc
12+
from test.support.import_helper import make_legacy_pyc
1313
import unittest
1414
import warnings
1515

0 commit comments

Comments
 (0)