From a089d21df1ea502b995d8e8a3bcc937cce030802 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Mon, 6 Jul 2020 17:15:08 +0800 Subject: [PATCH] bpo-40275: Use new test.support helper submodules in tests (GH-21315) --- Lib/ctypes/test/__init__.py | 4 +++- Lib/ctypes/test/test_find.py | 7 ++++--- Lib/test/test_bytes.py | 7 ++++--- Lib/test/test_cgitb.py | 2 +- Lib/test/test_ctypes.py | 3 ++- Lib/test/test_dbm.py | 17 +++++++++-------- Lib/test/test_fcntl.py | 6 ++++-- Lib/test/test_file.py | 11 ++++++----- Lib/test/test_fstring.py | 2 +- Lib/test/test_httpservers.py | 11 ++++++----- Lib/test/test_linecache.py | 13 +++++++------ Lib/test/test_msilib.py | 3 ++- Lib/test/test_picklebuffer.py | 8 ++++---- Lib/test/test_profile.py | 3 ++- Lib/test/test_pty.py | 3 ++- Lib/test/test_reprlib.py | 3 ++- Lib/test/test_shelve.py | 5 +++-- Lib/test/test_tk.py | 3 ++- Lib/test/test_wsgiref.py | 3 ++- Lib/test/test_zlib.py | 4 +++- 20 files changed, 69 insertions(+), 49 deletions(-) diff --git a/Lib/ctypes/test/__init__.py b/Lib/ctypes/test/__init__.py index 26a70b76963..6e496fa5a52 100644 --- a/Lib/ctypes/test/__init__.py +++ b/Lib/ctypes/test/__init__.py @@ -1,9 +1,11 @@ import os import unittest from test import support +from test.support import import_helper + # skip tests if _ctypes was not built -ctypes = support.import_module('ctypes') +ctypes = import_helper.import_module('ctypes') ctypes_symbols = dir(ctypes) def need_symbol(name): diff --git a/Lib/ctypes/test/test_find.py b/Lib/ctypes/test/test_find.py index b99fdcba7b2..bfb6b42cbb2 100644 --- a/Lib/ctypes/test/test_find.py +++ b/Lib/ctypes/test/test_find.py @@ -2,6 +2,7 @@ import os.path import sys import test.support +from test.support import os_helper from ctypes import * from ctypes.util import find_library @@ -65,8 +66,8 @@ def test_gle(self): self.gle.gleGetJoinStyle def test_shell_injection(self): - result = find_library('; echo Hello shell > ' + test.support.TESTFN) - self.assertFalse(os.path.lexists(test.support.TESTFN)) + result = find_library('; echo Hello shell > ' + os_helper.TESTFN) + self.assertFalse(os.path.lexists(os_helper.TESTFN)) self.assertIsNone(result) @@ -100,7 +101,7 @@ def test_find_on_libpath(self): # LD_LIBRARY_PATH) self.assertIsNone(find_library(libname)) # now add the location to LD_LIBRARY_PATH - with test.support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: KEY = 'LD_LIBRARY_PATH' if KEY not in env: v = d diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 770e2c5592c..61b4b9162cc 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -16,6 +16,7 @@ import unittest import test.support +from test.support import import_helper import test.string_tests import test.list_tests from test.support import bigaddrspacetest, MAX_Py_ssize_t @@ -967,7 +968,7 @@ def test_translate(self): self.assertEqual(c, b'hllo') def test_sq_item(self): - _testcapi = test.support.import_module('_testcapi') + _testcapi = import_helper.import_module('_testcapi') obj = self.type2test((42,)) with self.assertRaises(IndexError): _testcapi.sequence_getitem(obj, -2) @@ -1024,8 +1025,8 @@ def __bytes__(self): # Test PyBytes_FromFormat() def test_from_format(self): - ctypes = test.support.import_module('ctypes') - _testcapi = test.support.import_module('_testcapi') + ctypes = import_helper.import_module('ctypes') + _testcapi = import_helper.import_module('_testcapi') from ctypes import pythonapi, py_object from ctypes import ( c_int, c_uint, diff --git a/Lib/test/test_cgitb.py b/Lib/test/test_cgitb.py index bab152d8554..590ffdea112 100644 --- a/Lib/test/test_cgitb.py +++ b/Lib/test/test_cgitb.py @@ -1,4 +1,4 @@ -from test.support import temp_dir +from test.support.os_helper import temp_dir from test.support.script_helper import assert_python_failure import unittest import sys diff --git a/Lib/test/test_ctypes.py b/Lib/test/test_ctypes.py index 68268992e9f..b0a12c97347 100644 --- a/Lib/test/test_ctypes.py +++ b/Lib/test/test_ctypes.py @@ -1,5 +1,6 @@ import unittest -from test.support import import_module +from test.support.import_helper import import_module + ctypes_test = import_module('ctypes.test') diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index 571da973aab..e02d1e16ae3 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -2,17 +2,18 @@ import unittest import glob -import test.support +from test.support import import_helper +from test.support import os_helper # Skip tests if dbm module doesn't exist. -dbm = test.support.import_module('dbm') +dbm = import_helper.import_module('dbm') try: from dbm import ndbm except ImportError: ndbm = None -_fname = test.support.TESTFN +_fname = os_helper.TESTFN # # Iterates over every database module supported by dbm currently available, @@ -34,7 +35,7 @@ def delete_files(): # we don't know the precise name the underlying database uses # so we use glob to locate all names for f in glob.glob(glob.escape(_fname) + "*"): - test.support.unlink(f) + os_helper.unlink(f) class AnyDBMTestCase: @@ -74,7 +75,7 @@ def test_anydbm_creation(self): def test_anydbm_creation_n_file_exists_with_invalid_contents(self): # create an empty file - test.support.create_empty_file(_fname) + os_helper.create_empty_file(_fname) with dbm.open(_fname, 'n') as f: self.assertEqual(len(f), 0) @@ -169,7 +170,7 @@ def test_whichdb_ndbm(self): # Issue 17198: check that ndbm which is referenced in whichdb is defined db_file = '{}_ndbm.db'.format(_fname) with open(db_file, 'w'): - self.addCleanup(test.support.unlink, db_file) + self.addCleanup(os_helper.unlink, db_file) self.assertIsNone(self.dbm.whichdb(db_file[:-3])) def tearDown(self): @@ -177,10 +178,10 @@ def tearDown(self): def setUp(self): delete_files() - self.filename = test.support.TESTFN + self.filename = os_helper.TESTFN self.d = dbm.open(self.filename, 'c') self.d.close() - self.dbm = test.support.import_fresh_module('dbm') + self.dbm = import_helper.import_fresh_module('dbm') def test_keys(self): self.d = dbm.open(self.filename, 'c') diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index 9ab68c67241..7e109208326 100644 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -6,8 +6,10 @@ import sys import unittest from multiprocessing import Process -from test.support import (verbose, TESTFN, unlink, run_unittest, import_module, - cpython_only) +from test.support import (verbose, run_unittest, cpython_only) +from test.support.import_helper import import_module +from test.support.os_helper import TESTFN, unlink + # Skip test if no fcntl module. fcntl = import_module('fcntl') diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index cd642e7aaf8..149767591d9 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -7,8 +7,9 @@ import io import _pyio as pyio -from test.support import TESTFN -from test import support +from test.support.os_helper import TESTFN +from test.support import os_helper +from test.support import warnings_helper from collections import UserList class AutoFileTests: @@ -20,7 +21,7 @@ def setUp(self): def tearDown(self): if self.f: self.f.close() - support.unlink(TESTFN) + os_helper.unlink(TESTFN) def testWeakRefs(self): # verify weak references @@ -139,7 +140,7 @@ class PyAutoFileTests(AutoFileTests, unittest.TestCase): class OtherFileTests: def tearDown(self): - support.unlink(TESTFN) + os_helper.unlink(TESTFN) def testModeStrings(self): # check invalid mode strings @@ -187,7 +188,7 @@ def testSetBufferSize(self): # make sure that explicitly setting the buffer size doesn't cause # misbehaviour especially with repeated close() calls for s in (-1, 0, 512): - with support.check_no_warnings(self, + with warnings_helper.check_no_warnings(self, message='line buffering', category=RuntimeWarning): self._checkBufferSize(s) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 0dc7dd8e254..35a62a0632e 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -12,7 +12,7 @@ import types import decimal import unittest -from test.support import temp_cwd +from test.support.os_helper import temp_cwd from test.support.script_helper import assert_python_failure a_global = 'global variable' diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 71a0511e53a..0c871afca37 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -30,6 +30,7 @@ import unittest from test import support +from test.support import os_helper from test.support import threading_helper @@ -391,13 +392,13 @@ def close_conn(): 'undecodable name cannot always be decoded on macOS') @unittest.skipIf(sys.platform == 'win32', 'undecodable name cannot be decoded on win32') - @unittest.skipUnless(support.TESTFN_UNDECODABLE, - 'need support.TESTFN_UNDECODABLE') + @unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, + 'need os_helper.TESTFN_UNDECODABLE') def test_undecodable_filename(self): enc = sys.getfilesystemencoding() - filename = os.fsdecode(support.TESTFN_UNDECODABLE) + '.txt' + filename = os.fsdecode(os_helper.TESTFN_UNDECODABLE) + '.txt' with open(os.path.join(self.tempdir, filename), 'wb') as f: - f.write(support.TESTFN_UNDECODABLE) + f.write(os_helper.TESTFN_UNDECODABLE) response = self.request(self.base_url + '/') if sys.platform == 'darwin': # On Mac OS the HFS+ filesystem replaces bytes that aren't valid @@ -414,7 +415,7 @@ def test_undecodable_filename(self): .encode(enc, 'surrogateescape'), body) response = self.request(self.base_url + '/' + quotedname) self.check_status_and_reason(response, HTTPStatus.OK, - data=support.TESTFN_UNDECODABLE) + data=os_helper.TESTFN_UNDECODABLE) def test_get(self): #constructs the path relative to the root directory of the HTTPServer diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py index 375d9c42137..cfc6ba89e77 100644 --- a/Lib/test/test_linecache.py +++ b/Lib/test/test_linecache.py @@ -6,6 +6,7 @@ import tempfile import tokenize from test import support +from test.support import os_helper FILENAME = linecache.__file__ @@ -44,7 +45,7 @@ def setUp(self): with tempfile.NamedTemporaryFile(delete=False) as fp: self.file_name = fp.name fp.write(self.file_byte_string) - self.addCleanup(support.unlink, self.file_name) + self.addCleanup(os_helper.unlink, self.file_name) class GetLineTestsGoodData(TempFile): @@ -124,10 +125,10 @@ def test_getline(self): self.assertEqual(empty, []) def test_no_ending_newline(self): - self.addCleanup(support.unlink, support.TESTFN) - with open(support.TESTFN, "w") as fp: + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with open(os_helper.TESTFN, "w") as fp: fp.write(SOURCE_3) - lines = linecache.getlines(support.TESTFN) + lines = linecache.getlines(os_helper.TESTFN) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) def test_clearcache(self): @@ -150,8 +151,8 @@ def test_clearcache(self): def test_checkcache(self): getline = linecache.getline # Create a source file and cache its contents - source_name = support.TESTFN + '.py' - self.addCleanup(support.unlink, source_name) + source_name = os_helper.TESTFN + '.py' + self.addCleanup(os_helper.unlink, source_name) with open(source_name, 'w') as source: source.write(SOURCE_1) getline(source_name, 1) diff --git a/Lib/test/test_msilib.py b/Lib/test/test_msilib.py index 743bea7c14d..e29cd4a84c5 100644 --- a/Lib/test/test_msilib.py +++ b/Lib/test/test_msilib.py @@ -1,7 +1,8 @@ """ Test suite for the code in msilib """ import os import unittest -from test.support import TESTFN, import_module, unlink +from test.support.import_helper import import_module +from test.support.os_helper import TESTFN, unlink msilib = import_module('msilib') import msilib.schema diff --git a/Lib/test/test_picklebuffer.py b/Lib/test/test_picklebuffer.py index 97981c882e8..435b3e038aa 100644 --- a/Lib/test/test_picklebuffer.py +++ b/Lib/test/test_picklebuffer.py @@ -8,7 +8,7 @@ import weakref import unittest -from test import support +from test.support import import_helper class B(bytes): @@ -75,7 +75,7 @@ def test_cycle(self): def test_ndarray_2d(self): # C-contiguous - ndarray = support.import_module("_testbuffer").ndarray + ndarray = import_helper.import_module("_testbuffer").ndarray arr = ndarray(list(range(12)), shape=(4, 3), format='