From 53a914990dfb949d4e8e7c621d6eea1fb4287836 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 12 Jul 2020 13:35:13 +0800 Subject: [PATCH 1/3] Use new test.support helper submodules in tests --- Lib/test/test_import/__init__.py | 4 ++++ Lib/test/test_importlib/fixtures.py | 4 ++-- .../test_importlib/import_/test_packages.py | 3 ++- .../test_importlib/source/test_file_loader.py | 2 +- Lib/test/test_importlib/source/test_finder.py | 2 +- Lib/test/test_importlib/test_abc.py | 5 +++-- Lib/test/test_importlib/test_api.py | 18 ++++++++++-------- Lib/test/test_importlib/test_pkg_import.py | 2 +- Lib/test/test_importlib/test_spec.py | 2 +- .../test_importlib/test_threaded_import.py | 6 +++--- Lib/test/test_importlib/test_windows.py | 3 ++- Lib/test/test_importlib/util.py | 11 ++++++----- Lib/test/test_inspect.py | 4 +++- Lib/test/test_site.py | 4 ++-- Lib/test/test_tools/test_fixcid.py | 15 ++++++++------- Lib/test/test_tools/test_i18n.py | 2 +- Lib/test/test_tools/test_lll.py | 3 ++- Lib/test/test_tools/test_pathfix.py | 9 +++++---- Lib/test/test_tools/test_pindent.py | 3 ++- Lib/test/test_tools/test_sundry.py | 6 ++++-- 20 files changed, 63 insertions(+), 45 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index f4a83d2e7a13a1..026e6664dfdd5e 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -27,6 +27,10 @@ TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE, temp_dir) from test.support import script_helper from test.support import threading_helper +from test.support.import_helper import (forget, make_legacy_pyc, unlink, + unload, DirsOnSysPath) +from test.support.os_helper import (TESTFN, rmtree, temp_umask, + TESTFN_UNENCODABLE, temp_dir) from test.test_importlib.util import uncache from types import ModuleType diff --git a/Lib/test/test_importlib/fixtures.py b/Lib/test/test_importlib/fixtures.py index 2e55d14b9aab97..985277f64615fe 100644 --- a/Lib/test/test_importlib/fixtures.py +++ b/Lib/test/test_importlib/fixtures.py @@ -213,11 +213,11 @@ def build_files(file_defs, prefix=pathlib.Path()): class FileBuilder: def unicode_filename(self): try: - import test.support + from test.support import os_helper except ImportError: # outside CPython, hard-code a unicode snowman return '☃' - return test.support.FS_NONASCII or \ + return os_helper.FS_NONASCII or \ self.skip("File system does not support non-ascii.") diff --git a/Lib/test/test_importlib/import_/test_packages.py b/Lib/test/test_importlib/import_/test_packages.py index 24396044a5bcf1..c73ac63f6eef3d 100644 --- a/Lib/test/test_importlib/import_/test_packages.py +++ b/Lib/test/test_importlib/import_/test_packages.py @@ -2,6 +2,7 @@ import sys import unittest from test import support +from test.support import import_helper class ParentModuleTests: @@ -98,7 +99,7 @@ def module_injection(): try: submodule = self.__import__(subname) finally: - support.unload(subname) + import_helper.unload(subname) (Frozen_ParentTests, diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py index ab44722146e37a..cbd1533b676644 100644 --- a/Lib/test/test_importlib/source/test_file_loader.py +++ b/Lib/test/test_importlib/source/test_file_loader.py @@ -17,7 +17,7 @@ import unittest import warnings -from test.support import make_legacy_pyc, unload +from test.support.import_helper import make_legacy_pyc, unload from test.test_py_compile import without_source_date_epoch from test.test_py_compile import SourceDateEpochTestMeta diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py index f372b850dc7c15..e9207deedc75c8 100644 --- a/Lib/test/test_importlib/source/test_finder.py +++ b/Lib/test/test_importlib/source/test_finder.py @@ -9,7 +9,7 @@ import stat import sys import tempfile -from test.support import make_legacy_pyc +from test.support.import_helper import make_legacy_pyc import unittest import warnings diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py index 9816b35ef829a7..605738fae2e378 100644 --- a/Lib/test/test_importlib/test_abc.py +++ b/Lib/test/test_importlib/test_abc.py @@ -3,6 +3,7 @@ import os import sys from test import support +from test.support import import_helper import types import unittest from unittest import mock @@ -579,8 +580,8 @@ class InspectLoaderLoadModuleTests: module_name = 'blah' def setUp(self): - support.unload(self.module_name) - self.addCleanup(support.unload, self.module_name) + import_helper.unload(self.module_name) + self.addCleanup(import_helper.unload, self.module_name) def load(self, loader): spec = self.util.spec_from_loader(self.module_name, loader) diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index 0fb1346f9eec8f..fd60634e093339 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -7,6 +7,8 @@ import os.path import sys from test import support +from test.support import import_helper +from test.support import os_helper import types import unittest import warnings @@ -200,7 +202,7 @@ class ReloadTests: def test_reload_modules(self): for mod in ('tokenize', 'time', 'marshal'): with self.subTest(module=mod): - with support.CleanImport(mod): + with import_helper.CleanImport(mod): module = self.init.import_module(mod) self.init.reload(module) @@ -221,7 +223,7 @@ def code(): self.assertEqual(reloaded.spam, 3) def test_reload_missing_loader(self): - with support.CleanImport('types'): + with import_helper.CleanImport('types'): import types loader = types.__loader__ del types.__loader__ @@ -232,7 +234,7 @@ def test_reload_missing_loader(self): self.assertEqual(reloaded.__loader__.path, loader.path) def test_reload_loader_replaced(self): - with support.CleanImport('types'): + with import_helper.CleanImport('types'): import types types.__loader__ = None self.init.invalidate_caches() @@ -244,9 +246,9 @@ def test_reload_loader_replaced(self): def test_reload_location_changed(self): name = 'spam' - with support.temp_cwd(None) as cwd: + with os_helper.temp_cwd(None) as cwd: with test_util.uncache('spam'): - with support.DirsOnSysPath(cwd): + with import_helper.DirsOnSysPath(cwd): # Start as a plain module. self.init.invalidate_caches() path = os.path.join(cwd, name + '.py') @@ -257,7 +259,7 @@ def test_reload_location_changed(self): '__cached__': cached, '__doc__': None, } - support.create_empty_file(path) + os_helper.create_empty_file(path) module = self.init.import_module(name) ns = vars(module).copy() loader = ns.pop('__loader__') @@ -295,9 +297,9 @@ def test_reload_location_changed(self): def test_reload_namespace_changed(self): name = 'spam' - with support.temp_cwd(None) as cwd: + with os_helper.temp_cwd(None) as cwd: with test_util.uncache('spam'): - with support.DirsOnSysPath(cwd): + with import_helper.DirsOnSysPath(cwd): # Start as a namespace package. self.init.invalidate_caches() bad_path = os.path.join(cwd, name, '__init.py') diff --git a/Lib/test/test_importlib/test_pkg_import.py b/Lib/test/test_importlib/test_pkg_import.py index 6181dcfab280cf..36e78afa10bfc4 100644 --- a/Lib/test/test_importlib/test_pkg_import.py +++ b/Lib/test/test_importlib/test_pkg_import.py @@ -7,7 +7,7 @@ import unittest from importlib.util import cache_from_source -from test.support import create_empty_file +from test.support.os_helper import create_empty_file class TestImport(unittest.TestCase): diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py index 20dacec8664e1d..eed90f29f9286c 100644 --- a/Lib/test/test_importlib/test_spec.py +++ b/Lib/test/test_importlib/test_spec.py @@ -6,7 +6,7 @@ import os.path import pathlib -from test.support import CleanImport +from test.support.import_helper import CleanImport import unittest import sys import warnings diff --git a/Lib/test/test_importlib/test_threaded_import.py b/Lib/test/test_importlib/test_threaded_import.py index 06da18ed396d98..c6a6e1715abade 100644 --- a/Lib/test/test_importlib/test_threaded_import.py +++ b/Lib/test/test_importlib/test_threaded_import.py @@ -14,9 +14,9 @@ import threading import unittest from unittest import mock -from test.support import ( - verbose, run_unittest, TESTFN, - forget, unlink, rmtree) +from test.support import (verbose, run_unittest) +from test.support.import_helper import forget +from test.support.os_helper import (TESTFN, unlink, rmtree) from test.support import threading_helper def task(N, done, done_tasks, errors): diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py index 005b685cc03cd9..8b3f2009a850da 100644 --- a/Lib/test/test_importlib/test_windows.py +++ b/Lib/test/test_importlib/test_windows.py @@ -6,11 +6,12 @@ import sys import unittest from test import support +from test.support import import_helper from distutils.util import get_platform from contextlib import contextmanager from .util import temp_module -support.import_module('winreg', required_on=['win']) +import_helper.import_module('winreg', required_on=['win']) from winreg import ( CreateKey, HKEY_CURRENT_USER, SetValue, REG_SZ, KEY_ALL_ACCESS, diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py index 2745c9b7e3c3bf..5c0375e04a6ffb 100644 --- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -13,6 +13,7 @@ from pathlib import Path, PurePath from test import support from test.support import import_helper +from test.support import os_helper import unittest import sys import tempfile @@ -159,9 +160,9 @@ def uncache(*names): @contextlib.contextmanager def temp_module(name, content='', *, pkg=False): conflicts = [n for n in sys.modules if n.partition('.')[0] == name] - with support.temp_cwd(None) as cwd: + with os_helper.temp_cwd(None) as cwd: with uncache(name, *conflicts): - with support.DirsOnSysPath(cwd): + with import_helper.DirsOnSysPath(cwd): invalidate_caches() location = os.path.join(cwd, name) @@ -397,7 +398,7 @@ def create_modules(*names): state_manager.__exit__(None, None, None) if uncache_manager is not None: uncache_manager.__exit__(None, None, None) - support.rmtree(temp_dir) + os_helper.rmtree(temp_dir) def mock_path_hook(*entries, importer): @@ -573,8 +574,8 @@ def tearDownClass(cls): pass def setUp(self): - modules = support.modules_setup() - self.addCleanup(support.modules_cleanup, *modules) + modules = import_helper.modules_setup() + self.addCleanup(import_helper.modules_cleanup, *modules) class ZipSetup(ZipSetupBase): diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index e3e2be52076c6a..6667dc91edbcec 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -24,8 +24,10 @@ except ImportError: ThreadPoolExecutor = None -from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only +from test.support import run_unittest, cpython_only from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ +from test.support.import_helper import DirsOnSysPath +from test.support.os_helper import TESTFN from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index ec86c645981b36..5901939725e185 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -8,8 +8,8 @@ import test.support from test import support from test.support import socket_helper -from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard, - change_cwd) +from test.support import captured_stderr +from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd import builtins import encodings import glob diff --git a/Lib/test/test_tools/test_fixcid.py b/Lib/test/test_tools/test_fixcid.py index bce029b1aac83a..3df13680437194 100644 --- a/Lib/test/test_tools/test_fixcid.py +++ b/Lib/test/test_tools/test_fixcid.py @@ -5,6 +5,7 @@ import runpy import sys from test import support +from test.support import os_helper from test.test_tools import skip_if_missing, scriptsdir import unittest @@ -57,15 +58,15 @@ def test_alter_comments(self): ) def test_directory(self): - os.mkdir(support.TESTFN) - self.addCleanup(support.rmtree, support.TESTFN) - c_filename = os.path.join(support.TESTFN, "file.c") + os.mkdir(os_helper.TESTFN) + self.addCleanup(os_helper.rmtree, os_helper.TESTFN) + c_filename = os.path.join(os_helper.TESTFN, "file.c") with open(c_filename, "w") as file: file.write("int xx;\n") - with open(os.path.join(support.TESTFN, "file.py"), "w") as file: + with open(os.path.join(os_helper.TESTFN, "file.py"), "w") as file: file.write("xx = 'unaltered'\n") script = os.path.join(scriptsdir, "fixcid.py") - output = self.run_script(args=(support.TESTFN,)) + output = self.run_script(args=(os_helper.TESTFN,)) self.assertMultiLineEqual(output, "{}:\n" "1\n" @@ -74,10 +75,10 @@ def test_directory(self): ) def run_script(self, input="", *, args=("-",), substfile="xx yy\n"): - substfilename = support.TESTFN + ".subst" + substfilename = os_helper.TESTFN + ".subst" with open(substfilename, "w") as file: file.write(substfile) - self.addCleanup(support.unlink, substfilename) + self.addCleanup(os_helper.unlink, substfilename) argv = ["fixcid.py", "-s", substfilename] + list(args) script = os.path.join(scriptsdir, "fixcid.py") diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index 42e20f8f7716db..8da657907eab87 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -7,7 +7,7 @@ from test.support.script_helper import assert_python_ok from test.test_tools import skip_if_missing, toolsdir -from test.support import temp_cwd, temp_dir +from test.support.os_helper import temp_cwd, temp_dir skip_if_missing() diff --git a/Lib/test/test_tools/test_lll.py b/Lib/test/test_tools/test_lll.py index 568cbfb5e47460..ec0c97334fdebd 100644 --- a/Lib/test/test_tools/test_lll.py +++ b/Lib/test/test_tools/test_lll.py @@ -3,6 +3,7 @@ import os import tempfile from test import support +from test.support import os_helper from test.test_tools import skip_if_missing, import_tool import unittest @@ -14,7 +15,7 @@ class lllTests(unittest.TestCase): def setUp(self): self.lll = import_tool('lll') - @support.skip_unless_symlink + @os_helper.skip_unless_symlink def test_lll_multiple_dirs(self): with tempfile.TemporaryDirectory() as dir1, \ tempfile.TemporaryDirectory() as dir2: diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 03ed29d3f974f2..ff61935298b920 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -3,6 +3,7 @@ import sys import unittest from test import support +from test.support import os_helper from test.test_tools import scriptsdir, skip_if_missing @@ -14,7 +15,7 @@ class TestPathfixFunctional(unittest.TestCase): script = os.path.join(scriptsdir, 'pathfix.py') def setUp(self): - self.addCleanup(support.unlink, support.TESTFN) + self.addCleanup(os_helper.unlink, os_helper.TESTFN) def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='', directory=''): @@ -24,7 +25,7 @@ def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='', filename = os.path.join(directory, 'script-A_1.py') pathfix_arg = directory else: - filename = support.TESTFN + filename = os_helper.TESTFN pathfix_arg = filename with open(filename, 'w', encoding='utf8') as f: @@ -56,8 +57,8 @@ def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='', return new_shebang def test_recursive(self): - tmpdir = support.TESTFN + '.d' - self.addCleanup(support.rmtree, tmpdir) + tmpdir = os_helper.TESTFN + '.d' + self.addCleanup(os_helper.rmtree, tmpdir) os.mkdir(tmpdir) expected_stderr = f"recursedown('{os.path.basename(tmpdir)}')\n" self.assertEqual( diff --git a/Lib/test/test_tools/test_pindent.py b/Lib/test/test_tools/test_pindent.py index e293bc872ce51e..e7a547ad7d6127 100644 --- a/Lib/test/test_tools/test_pindent.py +++ b/Lib/test/test_tools/test_pindent.py @@ -6,6 +6,7 @@ import subprocess import textwrap from test import support +from test.support import os_helper from test.support.script_helper import assert_python_ok from test.test_tools import scriptsdir, skip_if_missing @@ -34,7 +35,7 @@ def lstriplines(self, data): def test_selftest(self): self.maxDiff = None - with support.temp_dir() as directory: + with os_helper.temp_dir() as directory: data_path = os.path.join(directory, '_test.py') with open(self.script) as f: closed = f.read() diff --git a/Lib/test/test_tools/test_sundry.py b/Lib/test/test_tools/test_sundry.py index 10eb6941b3be6f..c1648649d4c149 100644 --- a/Lib/test/test_tools/test_sundry.py +++ b/Lib/test/test_tools/test_sundry.py @@ -9,6 +9,8 @@ import sys import unittest from test import support +from test.support import import_helper +from test.support import os_helper from test.test_tools import scriptsdir, import_tool, skip_if_missing @@ -30,7 +32,7 @@ class TestSundryScripts(unittest.TestCase): skiplist = blacklist + whitelist + windows_only + other def test_sundry(self): - old_modules = support.modules_setup() + old_modules = import_helper.modules_setup() try: for fn in os.listdir(scriptsdir): if not fn.endswith('.py'): @@ -43,7 +45,7 @@ def test_sundry(self): import_tool(name) finally: # Unload all modules loaded in this test - support.modules_cleanup(*old_modules) + import_helper.modules_cleanup(*old_modules) @unittest.skipIf(sys.platform != "win32", "Windows-only test") def test_sundry_windows(self): From 3c88711d8a9421ccb9525ae1011a459de24c3de5 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 12 Jul 2020 13:45:48 +0800 Subject: [PATCH 2/3] remove unused module --- Lib/test/test_tools/test_sundry.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_tools/test_sundry.py b/Lib/test/test_tools/test_sundry.py index c1648649d4c149..8b5a963e25bd15 100644 --- a/Lib/test/test_tools/test_sundry.py +++ b/Lib/test/test_tools/test_sundry.py @@ -8,9 +8,7 @@ import os import sys import unittest -from test import support from test.support import import_helper -from test.support import os_helper from test.test_tools import scriptsdir, import_tool, skip_if_missing From 0be887e0d4509993314ad53aba3cb4330c14e3e5 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 4 Aug 2020 13:09:07 +0800 Subject: [PATCH 3/3] remove duplicated codes --- Lib/test/test_import/__init__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 026e6664dfdd5e..f4a83d2e7a13a1 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -27,10 +27,6 @@ TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE, temp_dir) from test.support import script_helper from test.support import threading_helper -from test.support.import_helper import (forget, make_legacy_pyc, unlink, - unload, DirsOnSysPath) -from test.support.os_helper import (TESTFN, rmtree, temp_umask, - TESTFN_UNENCODABLE, temp_dir) from test.test_importlib.util import uncache from types import ModuleType