From b9c29f3dc37a4c023c023a791cc34827e8b4fa0a Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 4 Jul 2020 19:20:57 +0800 Subject: [PATCH 01/12] Use new test.support helper submodules in tests --- Lib/distutils/tests/support.py | 4 +- Lib/distutils/tests/test_build_ext.py | 3 +- Lib/distutils/tests/test_filelist.py | 13 +++-- Lib/distutils/tests/test_spawn.py | 18 +++---- Lib/test/test_dbm_gnu.py | 3 +- Lib/test/test_eof.py | 3 +- Lib/test/test_hashlib.py | 3 +- Lib/test/test_idle.py | 2 +- Lib/test/test_imaplib.py | 3 +- Lib/test/test_marshal.py | 73 ++++++++++++++------------- Lib/test/test_py_compile.py | 7 +-- Lib/test/test_set.py | 3 +- Lib/test/test_socket.py | 57 +++++++++++---------- Lib/test/test_sysconfig.py | 8 +-- Lib/test/test_traceback.py | 3 +- Lib/test/test_unicode.py | 12 +++-- Lib/test/test_xml_etree.py | 21 +++++--- Lib/test/test_zipfile.py | 7 +-- Lib/test/test_zlib.py | 3 +- 19 files changed, 133 insertions(+), 113 deletions(-) diff --git a/Lib/distutils/tests/support.py b/Lib/distutils/tests/support.py index 259af882ec0e96..23b907b607efad 100644 --- a/Lib/distutils/tests/support.py +++ b/Lib/distutils/tests/support.py @@ -6,7 +6,7 @@ import unittest import sysconfig from copy import deepcopy -import test.support +from test.support import os_helper from distutils import log from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL @@ -64,7 +64,7 @@ def tearDown(self): super().tearDown() while self.tempdirs: tmpdir = self.tempdirs.pop() - test.support.rmtree(tmpdir) + os_helper.rmtree(tmpdir) def mkdtemp(self): """Create a temporary directory that will be cleaned up. diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py index 5e47e0773a9649..f9e0d766d870e0 100644 --- a/Lib/distutils/tests/test_build_ext.py +++ b/Lib/distutils/tests/test_build_ext.py @@ -15,6 +15,7 @@ import unittest from test import support +from test.support import os_helper from test.support.script_helper import assert_python_ok # http://bugs.python.org/issue4373 @@ -38,7 +39,7 @@ def setUp(self): # bpo-30132: On Windows, a .pdb file may be created in the current # working directory. Create a temporary working directory to cleanup # everything at the end of the test. - change_cwd = support.change_cwd(self.tmp_dir) + change_cwd = os_helper.change_cwd(self.tmp_dir) change_cwd.__enter__() self.addCleanup(change_cwd.__exit__, None, None, None) diff --git a/Lib/distutils/tests/test_filelist.py b/Lib/distutils/tests/test_filelist.py index 2c26c22617ed4a..cee97d439e6dfd 100644 --- a/Lib/distutils/tests/test_filelist.py +++ b/Lib/distutils/tests/test_filelist.py @@ -8,7 +8,6 @@ from distutils.filelist import glob_to_re, translate_pattern, FileList from distutils import filelist -import test.support from test.support import os_helper from test.support import captured_stdout, run_unittest from distutils.tests import support @@ -298,7 +297,7 @@ def test_process_template(self): class FindAllTestCase(unittest.TestCase): @os_helper.skip_unless_symlink def test_missing_symlink(self): - with test.support.temp_cwd(): + with os_helper.temp_cwd(): os.symlink('foo', 'bar') self.assertEqual(filelist.findall(), []) @@ -308,13 +307,13 @@ def test_basic_discovery(self): '.' as the parameter, the dot should be omitted from the results. """ - with test.support.temp_cwd(): + with os_helper.temp_cwd(): os.mkdir('foo') file1 = os.path.join('foo', 'file1.txt') - test.support.create_empty_file(file1) + os_helper.create_empty_file(file1) os.mkdir('bar') file2 = os.path.join('bar', 'file2.txt') - test.support.create_empty_file(file2) + os_helper.create_empty_file(file2) expected = [file2, file1] self.assertEqual(sorted(filelist.findall()), expected) @@ -323,9 +322,9 @@ def test_non_local_discovery(self): When findall is called with another path, the full path name should be returned. """ - with test.support.temp_dir() as temp_dir: + with os_helper.temp_dir() as temp_dir: file1 = os.path.join(temp_dir, 'file1.txt') - test.support.create_empty_file(file1) + os_helper.create_empty_file(file1) expected = [file1] self.assertEqual(filelist.findall(temp_dir), expected) diff --git a/Lib/distutils/tests/test_spawn.py b/Lib/distutils/tests/test_spawn.py index cf1faad5f4dd5d..3647bab7b1cbcd 100644 --- a/Lib/distutils/tests/test_spawn.py +++ b/Lib/distutils/tests/test_spawn.py @@ -4,7 +4,7 @@ import sys import unittest.mock from test.support import run_unittest, unix_shell -from test import support as test_support +from test.support import os_helper from distutils.spawn import find_executable from distutils.spawn import spawn @@ -44,9 +44,9 @@ def test_spawn(self): spawn([exe]) # should work without any error def test_find_executable(self): - with test_support.temp_dir() as tmp_dir: + with os_helper.temp_dir() as tmp_dir: # use TESTFN to get a pseudo-unique filename - program_noeext = test_support.TESTFN + program_noeext = os_helper.TESTFN # Give the temporary program an ".exe" suffix for all. # It's needed on Windows and not harmful on other platforms. program = program_noeext + ".exe" @@ -66,7 +66,7 @@ def test_find_executable(self): self.assertEqual(rv, filename) # test find in the current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) @@ -76,7 +76,7 @@ def test_find_executable(self): self.assertIsNone(rv) # PATH='': no match, except in the current directory - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env['PATH'] = '' with unittest.mock.patch('distutils.spawn.os.confstr', return_value=tmp_dir, create=True), \ @@ -86,12 +86,12 @@ def test_find_executable(self): self.assertIsNone(rv) # look in current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # PATH=':': explicitly looks in the current directory - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env['PATH'] = os.pathsep with unittest.mock.patch('distutils.spawn.os.confstr', return_value='', create=True), \ @@ -100,12 +100,12 @@ def test_find_executable(self): self.assertIsNone(rv) # look in current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # missing PATH: test os.confstr("CS_PATH") and os.defpath - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env.pop('PATH', None) # without confstr diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py index f1c7d34085c5ee..078bf0e9b92701 100644 --- a/Lib/test/test_dbm_gnu.py +++ b/Lib/test/test_dbm_gnu.py @@ -1,5 +1,6 @@ from test import support -gdbm = support.import_module("dbm.gnu") #skip if not supported +from test.support import import_helper +gdbm = import_helper.import_module("dbm.gnu") #skip if not supported import unittest import os from test.support import TESTFN, TESTFN_NONASCII, unlink diff --git a/Lib/test/test_eof.py b/Lib/test/test_eof.py index 51cbbd8eed664f..2cf263d27463c4 100644 --- a/Lib/test/test_eof.py +++ b/Lib/test/test_eof.py @@ -2,6 +2,7 @@ import sys from test import support +from test.support import os_helper from test.support import script_helper import unittest @@ -48,7 +49,7 @@ def test_line_continuation_EOF(self): @unittest.skipIf(not sys.executable, "sys.executable required") def test_line_continuation_EOF_from_file_bpo2180(self): """Ensure tok_nextc() does not add too many ending newlines.""" - with support.temp_dir() as temp_dir: + with os_helper.temp_dir() as temp_dir: file_name = script_helper.make_script(temp_dir, 'foo', '\\') rc, out, err = script_helper.assert_python_failure(file_name) self.assertIn(b'unexpected EOF while parsing', err) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index ba902986adb81c..4551011f5ca9ef 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -18,7 +18,8 @@ import unittest import warnings from test import support -from test.support import _4G, bigmemtest, import_fresh_module +from test.support import _4G, bigmemtest +from test.support.import_helper import import_fresh_module from test.support import threading_helper from http.client import HTTPException diff --git a/Lib/test/test_idle.py b/Lib/test/test_idle.py index 8bc01deaa33841..b205d356498605 100644 --- a/Lib/test/test_idle.py +++ b/Lib/test/test_idle.py @@ -1,5 +1,5 @@ import unittest -from test.support import import_module +from test.support.import_helper import import_module # Skip test_idle if _tkinter wasn't built, if tkinter is missing, # if tcl/tk is not the 8.5+ needed for ttk widgets, diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index b8ab677e6f6a9f..96bcb09261e9e7 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -14,6 +14,7 @@ run_with_tz, run_with_locale, cpython_only) from test.support import hashlib_helper from test.support import threading_helper +from test.support import warnings_helper import unittest from unittest import mock from datetime import datetime, timezone, timedelta @@ -575,7 +576,7 @@ def test_ssl_verified(self): # to CPython stdlib @cpython_only def test_certfile_arg_warn(self): - with support.check_warnings(('', DeprecationWarning)): + with warnings_helper.check_warnings(('', DeprecationWarning)): with mock.patch.object(self.imap_class, 'open'): with mock.patch.object(self.imap_class, '_connect'): self.imap_class('localhost', 143, certfile=CERTFILE) diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index b7f4dbb98e36d4..7bcf8e8399a817 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -1,4 +1,5 @@ from test import support +from test.support import os_helper import array import io import marshal @@ -17,13 +18,13 @@ def helper(self, sample, *extra): new = marshal.loads(marshal.dumps(sample, *extra)) self.assertEqual(sample, new) try: - with open(support.TESTFN, "wb") as f: + with open(os_helper.TESTFN, "wb") as f: marshal.dump(sample, f, *extra) - with open(support.TESTFN, "rb") as f: + with open(os_helper.TESTFN, "rb") as f: new = marshal.load(f) self.assertEqual(sample, new) finally: - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) class IntTestCase(unittest.TestCase, HelperMixin): def test_ints(self): @@ -281,20 +282,20 @@ def test_multiple_dumps_and_loads(self): ilen = len(interleaved) positions = [] try: - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: for d in data: marshal.dump(d, f) if ilen: f.write(interleaved) positions.append(f.tell()) - with open(support.TESTFN, 'rb') as f: + with open(os_helper.TESTFN, 'rb') as f: for i, d in enumerate(data): self.assertEqual(d, marshal.load(f)) if ilen: f.read(ilen) self.assertEqual(positions[i], f.tell()) finally: - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_loads_reject_unicode_strings(self): # Issue #14177: marshal.loads() should not accept unicode strings @@ -516,81 +517,81 @@ class CAPI_TestCase(unittest.TestCase, HelperMixin): def test_write_long_to_file(self): for v in range(marshal.version + 1): - _testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v) - with open(support.TESTFN, 'rb') as f: + _testcapi.pymarshal_write_long_to_file(0x12345678, os_helper.TESTFN, v) + with open(os_helper.TESTFN, 'rb') as f: data = f.read() - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(data, b'\x78\x56\x34\x12') def test_write_object_to_file(self): obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000) for v in range(marshal.version + 1): - _testcapi.pymarshal_write_object_to_file(obj, support.TESTFN, v) - with open(support.TESTFN, 'rb') as f: + _testcapi.pymarshal_write_object_to_file(obj, os_helper.TESTFN, v) + with open(os_helper.TESTFN, 'rb') as f: data = f.read() - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(marshal.loads(data), obj) def test_read_short_from_file(self): - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(b'\x34\x12xxxx') - r, p = _testcapi.pymarshal_read_short_from_file(support.TESTFN) - support.unlink(support.TESTFN) + r, p = _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(r, 0x1234) self.assertEqual(p, 2) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(b'\x12') with self.assertRaises(EOFError): - _testcapi.pymarshal_read_short_from_file(support.TESTFN) - support.unlink(support.TESTFN) + _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_read_long_from_file(self): - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(b'\x78\x56\x34\x12xxxx') - r, p = _testcapi.pymarshal_read_long_from_file(support.TESTFN) - support.unlink(support.TESTFN) + r, p = _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(r, 0x12345678) self.assertEqual(p, 4) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(b'\x56\x34\x12') with self.assertRaises(EOFError): - _testcapi.pymarshal_read_long_from_file(support.TESTFN) - support.unlink(support.TESTFN) + _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_read_last_object_from_file(self): obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) for v in range(marshal.version + 1): data = marshal.dumps(obj, v) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(data + b'xxxx') - r, p = _testcapi.pymarshal_read_last_object_from_file(support.TESTFN) - support.unlink(support.TESTFN) + r, p = _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(r, obj) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(data[:1]) with self.assertRaises(EOFError): - _testcapi.pymarshal_read_last_object_from_file(support.TESTFN) - support.unlink(support.TESTFN) + _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_read_object_from_file(self): obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) for v in range(marshal.version + 1): data = marshal.dumps(obj, v) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(data + b'xxxx') - r, p = _testcapi.pymarshal_read_object_from_file(support.TESTFN) - support.unlink(support.TESTFN) + r, p = _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) self.assertEqual(r, obj) self.assertEqual(p, len(data)) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: f.write(data[:1]) with self.assertRaises(EOFError): - _testcapi.pymarshal_read_object_from_file(support.TESTFN) - support.unlink(support.TESTFN) + _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN) + os_helper.unlink(os_helper.TESTFN) if __name__ == "__main__": diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py index d4a68c9320d04f..d8ba009ea8482e 100644 --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -9,13 +9,14 @@ import unittest from test import support +from test.support import os_helper def without_source_date_epoch(fxn): """Runs function with SOURCE_DATE_EPOCH unset.""" @functools.wraps(fxn) def wrapper(*args, **kwargs): - with support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env.unset('SOURCE_DATE_EPOCH') return fxn(*args, **kwargs) return wrapper @@ -25,7 +26,7 @@ def with_source_date_epoch(fxn): """Runs function with SOURCE_DATE_EPOCH set.""" @functools.wraps(fxn) def wrapper(*args, **kwargs): - with support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env['SOURCE_DATE_EPOCH'] = '123456789' return fxn(*args, **kwargs) return wrapper @@ -101,7 +102,7 @@ def test_cache_path(self): self.assertTrue(os.path.exists(self.cache_path)) def test_cwd(self): - with support.change_cwd(self.directory): + with os_helper.change_cwd(self.directory): py_compile.compile(os.path.basename(self.source_path), os.path.basename(self.pyc_path)) self.assertTrue(os.path.exists(self.pyc_path)) diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 68d494213e5870..e45f018d2da71b 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import warnings_helper import gc import weakref import operator @@ -953,7 +954,7 @@ def test_repr(self): class TestBasicOpsMixedStringBytes(TestBasicOps, unittest.TestCase): def setUp(self): - self._warning_filters = support.check_warnings() + self._warning_filters = warnings_helper.check_warnings() self._warning_filters.__enter__() warnings.simplefilter('ignore', BytesWarning) self.case = "string and bytes set" diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index cff07b46c7a2a4..67ac045330f3eb 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import os_helper from test.support import socket_helper from test.support import threading_helper @@ -697,7 +698,7 @@ def setUp(self): def bindSock(self, sock): path = tempfile.mktemp(dir=self.dir_path) socket_helper.bind_unix_socket(sock, path) - self.addCleanup(support.unlink, path) + self.addCleanup(os_helper.unlink, path) class UnixStreamBase(UnixSocketTestBase): """Base class for Unix-domain SOCK_STREAM tests.""" @@ -1917,14 +1918,14 @@ def test_socket_fileno_rejects_negative(self): def test_socket_fileno_requires_valid_fd(self): WSAENOTSOCK = 10038 with self.assertRaises(OSError) as cm: - socket.socket(fileno=support.make_bad_fd()) + socket.socket(fileno=os_helper.make_bad_fd()) self.assertIn(cm.exception.errno, (errno.EBADF, WSAENOTSOCK)) with self.assertRaises(OSError) as cm: socket.socket( socket.AF_INET, socket.SOCK_STREAM, - fileno=support.make_bad_fd()) + fileno=os_helper.make_bad_fd()) self.assertIn(cm.exception.errno, (errno.EBADF, WSAENOTSOCK)) def test_socket_fileno_requires_socket_fd(self): @@ -5458,35 +5459,35 @@ def testUnbound(self): def testStrAddr(self): # Test binding to and retrieving a normal string pathname. - path = os.path.abspath(support.TESTFN) + path = os.path.abspath(os_helper.TESTFN) self.bind(self.sock, path) - self.addCleanup(support.unlink, path) + self.addCleanup(os_helper.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testBytesAddr(self): # Test binding to a bytes pathname. - path = os.path.abspath(support.TESTFN) + path = os.path.abspath(os_helper.TESTFN) self.bind(self.sock, self.encoded(path)) - self.addCleanup(support.unlink, path) + self.addCleanup(os_helper.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testSurrogateescapeBind(self): # Test binding to a valid non-ASCII pathname, with the # non-ASCII bytes supplied using surrogateescape encoding. - path = os.path.abspath(support.TESTFN_UNICODE) + path = os.path.abspath(os_helper.TESTFN_UNICODE) b = self.encoded(path) self.bind(self.sock, b.decode("ascii", "surrogateescape")) - self.addCleanup(support.unlink, path) + self.addCleanup(os_helper.unlink, path) self.assertEqual(self.sock.getsockname(), path) def testUnencodableAddr(self): # Test binding to a pathname that cannot be encoded in the # file system encoding. - if support.TESTFN_UNENCODABLE is None: + if os_helper.TESTFN_UNENCODABLE is None: self.skipTest("No unencodable filename available") - path = os.path.abspath(support.TESTFN_UNENCODABLE) + path = os.path.abspath(os_helper.TESTFN_UNENCODABLE) self.bind(self.sock, path) - self.addCleanup(support.unlink, path) + self.addCleanup(os_helper.unlink, path) self.assertEqual(self.sock.getsockname(), path) @@ -5960,16 +5961,16 @@ def chunks(total, step): chunk = b"".join([random.choice(string.ascii_letters).encode() for i in range(cls.BUFSIZE)]) - with open(support.TESTFN, 'wb') as f: + with open(os_helper.TESTFN, 'wb') as f: for csize in chunks(cls.FILESIZE, cls.BUFSIZE): f.write(chunk) - with open(support.TESTFN, 'rb') as f: + with open(os_helper.TESTFN, 'rb') as f: cls.FILEDATA = f.read() assert len(cls.FILEDATA) == cls.FILESIZE @classmethod def tearDownClass(cls): - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def accept_conn(self): self.serv.settimeout(support.LONG_TIMEOUT) @@ -5996,7 +5997,7 @@ def meth_from_sock(self, sock): def _testRegularFile(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') with socket.create_connection(address) as sock, file as file: meth = self.meth_from_sock(sock) sent = meth(file) @@ -6031,9 +6032,9 @@ def testNonRegularFile(self): def _testEmptyFileSend(self): address = self.serv.getsockname() - filename = support.TESTFN + "2" + filename = os_helper.TESTFN + "2" with open(filename, 'wb'): - self.addCleanup(support.unlink, filename) + self.addCleanup(os_helper.unlink, filename) file = open(filename, 'rb') with socket.create_connection(address) as sock, file as file: meth = self.meth_from_sock(sock) @@ -6050,7 +6051,7 @@ def testEmptyFileSend(self): def _testOffset(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') with socket.create_connection(address) as sock, file as file: meth = self.meth_from_sock(sock) sent = meth(file, offset=5000) @@ -6067,7 +6068,7 @@ def testOffset(self): def _testCount(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') sock = socket.create_connection(address, timeout=support.LOOPBACK_TIMEOUT) with sock, file: @@ -6088,7 +6089,7 @@ def testCount(self): def _testCountSmall(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') sock = socket.create_connection(address, timeout=support.LOOPBACK_TIMEOUT) with sock, file: @@ -6109,7 +6110,7 @@ def testCountSmall(self): def _testCountWithOffset(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') with socket.create_connection(address, timeout=2) as sock, file as file: count = 100007 meth = self.meth_from_sock(sock) @@ -6128,7 +6129,7 @@ def testCountWithOffset(self): def _testNonBlocking(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') with socket.create_connection(address) as sock, file as file: sock.setblocking(False) meth = self.meth_from_sock(sock) @@ -6144,7 +6145,7 @@ def testNonBlocking(self): def _testWithTimeout(self): address = self.serv.getsockname() - file = open(support.TESTFN, 'rb') + file = open(os_helper.TESTFN, 'rb') sock = socket.create_connection(address, timeout=support.LOOPBACK_TIMEOUT) with sock, file: @@ -6162,7 +6163,7 @@ def testWithTimeout(self): def _testWithTimeoutTriggeredSend(self): address = self.serv.getsockname() - with open(support.TESTFN, 'rb') as file: + with open(os_helper.TESTFN, 'rb') as file: with socket.create_connection(address) as sock: sock.settimeout(0.01) meth = self.meth_from_sock(sock) @@ -6178,17 +6179,17 @@ def _test_errors(self): pass def test_errors(self): - with open(support.TESTFN, 'rb') as file: + with open(os_helper.TESTFN, 'rb') as file: with socket.socket(type=socket.SOCK_DGRAM) as s: meth = self.meth_from_sock(s) self.assertRaisesRegex( ValueError, "SOCK_STREAM", meth, file) - with open(support.TESTFN, 'rt') as file: + with open(os_helper.TESTFN, 'rt') as file: with socket.socket() as s: meth = self.meth_from_sock(s) self.assertRaisesRegex( ValueError, "binary mode", meth, file) - with open(support.TESTFN, 'rb') as file: + with open(os_helper.TESTFN, 'rb') as file: with socket.socket() as s: meth = self.meth_from_sock(s) self.assertRaisesRegex(TypeError, "positive integer", diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 44e44bf5ea995f..d07d28df607ce7 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -5,9 +5,11 @@ import shutil from copy import copy -from test.support import (import_module, TESTFN, unlink, check_warnings, - captured_stdout, skip_unless_symlink, change_cwd, - PythonSymlink) +from test.support import (captured_stdout, PythonSymlink) +from test.support.import_helper import import_module +from test.support.os_helper import (TESTFN, unlink, skip_unless_symlink, + change_cwd) +from test.support.warnings_helper import check_warnings import sysconfig from sysconfig import (get_paths, get_platform, get_config_vars, diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 925a6bc32e8ea7..c5fbd8700ae9b1 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -7,7 +7,8 @@ import unittest import re from test import support -from test.support import TESTFN, Error, captured_output, unlink, cpython_only, ALWAYS_EQ +from test.support import Error, captured_output, cpython_only, ALWAYS_EQ +from test.support.os_helper import TESTFN, unlink from test.support.script_helper import assert_python_ok import textwrap diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 59697935fe5cd8..afc95555db0269 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -15,6 +15,8 @@ import unicodedata import unittest import warnings +from test.support import import_helper +from test.support import warnings_helper from test import support, string_tests from test.support.script_helper import assert_python_failure @@ -504,7 +506,7 @@ def test_replace_id(self): self.assertIs(text.replace(pattern, pattern), text) def test_bytes_comparison(self): - with support.check_warnings(): + with warnings_helper.check_warnings(): warnings.simplefilter('ignore', BytesWarning) self.assertEqual('abc' == b'abc', False) self.assertEqual('abc' != b'abc', True) @@ -725,7 +727,7 @@ def test_isidentifier_legacy(self): import _testcapi u = 'π–€π–“π–Žπ–ˆπ–”π–‰π–Š' self.assertTrue(u.isidentifier()) - with support.check_warnings(): + with warnings_helper.check_warnings(): warnings.simplefilter('ignore', DeprecationWarning) self.assertTrue(_testcapi.unicode_legacy_string(u).isidentifier()) @@ -2507,7 +2509,7 @@ class CAPITest(unittest.TestCase): # Test PyUnicode_FromFormat() def test_from_format(self): - support.import_module('ctypes') + import_helper.import_module('ctypes') from ctypes import ( pythonapi, py_object, sizeof, c_int, c_long, c_longlong, c_ssize_t, @@ -2748,7 +2750,7 @@ def check_format(expected, format, *args): @support.cpython_only def test_aswidechar(self): from _testcapi import unicode_aswidechar - support.import_module('ctypes') + import_helper.import_module('ctypes') from ctypes import c_wchar, sizeof wchar, size = unicode_aswidechar('abcdef', 2) @@ -2786,7 +2788,7 @@ def test_aswidechar(self): @support.cpython_only def test_aswidecharstring(self): from _testcapi import unicode_aswidecharstring - support.import_module('ctypes') + import_helper.import_module('ctypes') from ctypes import c_wchar, sizeof wchar, size = unicode_aswidecharstring('abc') diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index d01649d1c31b26..63f9b92a83de24 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -24,7 +24,12 @@ from functools import partial from itertools import product, islice from test import support -from test.support import TESTFN, findfile, import_fresh_module, gc_collect, swap_attr +from test.support import os_helper +from test.support import warnings_helper +from test.support import findfile, gc_collect, swap_attr +from test.support.import_helper import import_fresh_module +from test.support.os_helper import TESTFN + # pyET is the pure-Python implementation. # @@ -601,7 +606,7 @@ def test_iterparse(self): self.assertFalse(f.closed) self.assertEqual(str(cm.exception), "unknown event 'bogus'") - with support.check_no_resource_warning(self): + with warnings_helper.check_no_resource_warning(self): with self.assertRaises(ValueError) as cm: iterparse(SIMPLE_XMLFILE, events) self.assertEqual(str(cm.exception), "unknown event 'bogus'") @@ -627,13 +632,13 @@ def test_iterparse(self): self.assertEqual(str(cm.exception), 'junk after document element: line 1, column 12') - self.addCleanup(support.unlink, TESTFN) + self.addCleanup(os_helper.unlink, TESTFN) with open(TESTFN, "wb") as f: f.write(b"junk") it = iterparse(TESTFN) action, elem = next(it) self.assertEqual((action, elem.tag), ('end', 'document')) - with support.check_no_resource_warning(self): + with warnings_helper.check_no_resource_warning(self): with self.assertRaises(ET.ParseError) as cm: next(it) self.assertEqual(str(cm.exception), @@ -3641,14 +3646,14 @@ def test_encoding(self): "" % enc).encode(enc)) def test_write_to_filename(self): - self.addCleanup(support.unlink, TESTFN) + self.addCleanup(os_helper.unlink, TESTFN) tree = ET.ElementTree(ET.XML('''''')) tree.write(TESTFN) with open(TESTFN, 'rb') as f: self.assertEqual(f.read(), b'''''') def test_write_to_text_file(self): - self.addCleanup(support.unlink, TESTFN) + self.addCleanup(os_helper.unlink, TESTFN) tree = ET.ElementTree(ET.XML('''''')) with open(TESTFN, 'w', encoding='utf-8') as f: tree.write(f, encoding='unicode') @@ -3657,7 +3662,7 @@ def test_write_to_text_file(self): self.assertEqual(f.read(), b'''''') def test_write_to_binary_file(self): - self.addCleanup(support.unlink, TESTFN) + self.addCleanup(os_helper.unlink, TESTFN) tree = ET.ElementTree(ET.XML('''''')) with open(TESTFN, 'wb') as f: tree.write(f) @@ -3666,7 +3671,7 @@ def test_write_to_binary_file(self): self.assertEqual(f.read(), b'''''') def test_write_to_binary_file_with_bom(self): - self.addCleanup(support.unlink, TESTFN) + self.addCleanup(os_helper.unlink, TESTFN) tree = ET.ElementTree(ET.XML('''''')) # test BOM writing to buffered file with open(TESTFN, 'wb') as f: diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index b7bc218d17a3d9..2851051425bf12 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -19,9 +19,10 @@ from random import randint, random, randbytes from test.support import script_helper -from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, temp_cwd, - requires_zlib, requires_bz2, requires_lzma, - captured_stdout) +from test.support import (findfile, requires_zlib, requires_bz2, + requires_lzma, captured_stdout) +from test.support.os_helper import TESTFN, unlink, rmtree, temp_dir, temp_cwd + TESTFN2 = TESTFN + "2" TESTFNDIR = TESTFN + "d" diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 02509cdf5532c9..1707b8d2033040 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import import_helper import binascii import copy import pickle @@ -7,7 +8,7 @@ import sys from test.support import bigmemtest, _1G, _4G -zlib = support.import_module('zlib') +zlib = import_helper.import_module('zlib') requires_Compress_copy = unittest.skipUnless( hasattr(zlib.compressobj(), "copy"), From b51f3caefb0f4b2fc35def615a9778337355359b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 4 Jul 2020 23:18:15 +0200 Subject: [PATCH 02/12] bpo-41204: Fix compiler warning in ast_type_init() (GH-21307) --- Parser/asdl_c.py | 9 +++++---- Python/Python-ast.c | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index b93906ba8d45d8..6fe44b99f793bb 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -688,13 +688,14 @@ def visitModule(self, mod): static int ast_type_init(PyObject *self, PyObject *args, PyObject *kw) { - Py_ssize_t i, numfields = 0; - int res = -1; - PyObject *key, *value, *fields; astmodulestate *state = get_global_ast_state(); if (state == NULL) { - goto cleanup; + return -1; } + + Py_ssize_t i, numfields = 0; + int res = -1; + PyObject *key, *value, *fields; if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { goto cleanup; } diff --git a/Python/Python-ast.c b/Python/Python-ast.c index b81b282a039659..396a6832702b35 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -1140,13 +1140,14 @@ ast_clear(AST_object *self) static int ast_type_init(PyObject *self, PyObject *args, PyObject *kw) { - Py_ssize_t i, numfields = 0; - int res = -1; - PyObject *key, *value, *fields; astmodulestate *state = get_global_ast_state(); if (state == NULL) { - goto cleanup; + return -1; } + + Py_ssize_t i, numfields = 0; + int res = -1; + PyObject *key, *value, *fields; if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { goto cleanup; } From 4ecd411c3c576676079d14193af03e6f7eb13541 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 5 Jul 2020 11:01:54 +0900 Subject: [PATCH 03/12] Uncomment Py_DEPRECATED for Py_UNICODE APIs (GH-21318) PyUnicode_EncodeDecimal and PyUnicode_TransformDecimalToASCII are deprecated since Python 3.3. But Py_DEPRECATED(3.3) was commented out. --- Include/cpython/unicodeobject.h | 4 ++-- Modules/_testcapimodule.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index 0f19b2a14bcd0a..a82eee45e0ed2e 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -974,7 +974,7 @@ Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS( */ -/* Py_DEPRECATED(3.3) */ PyAPI_FUNC(int) PyUnicode_EncodeDecimal( +Py_DEPRECATED(3.3) PyAPI_FUNC(int) PyUnicode_EncodeDecimal( Py_UNICODE *s, /* Unicode buffer */ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */ char *output, /* Output buffer; must have size >= length */ @@ -987,7 +987,7 @@ Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS( Returns a new Unicode string on success, NULL on failure. */ -/* Py_DEPRECATED(3.3) */ +Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_TransformDecimalToASCII( Py_UNICODE *s, /* Unicode buffer */ Py_ssize_t length /* Number of Py_UNICODE chars to transform */ diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 629102b12a9fe2..1e4c31fefb206f 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2024,6 +2024,10 @@ unicode_copycharacters(PyObject *self, PyObject *args) return Py_BuildValue("(Nn)", to_copy, copied); } +/* Ignore use of deprecated APIs */ +_Py_COMP_DIAG_PUSH +_Py_COMP_DIAG_IGNORE_DEPR_DECLS + static PyObject * unicode_encodedecimal(PyObject *self, PyObject *args) { @@ -2069,10 +2073,6 @@ unicode_transformdecimaltoascii(PyObject *self, PyObject *args) return PyUnicode_TransformDecimalToASCII(unicode, length); } -/* Ignore use of deprecated APIs */ -_Py_COMP_DIAG_PUSH -_Py_COMP_DIAG_IGNORE_DEPR_DECLS - static PyObject * unicode_legacy_string(PyObject *self, PyObject *args) { From 20d5076faf110f8e61dedd0f71f4337d04addcfa Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 5 Jul 2020 13:01:48 +0900 Subject: [PATCH 04/12] bpo-41211: Doc: Fix PyLong_FromUnicodeObject (GH-21325) It doesn't use PyUnicode_EncodeDecimal. It uses a private API instead. --- Doc/c-api/long.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst index 3921a93843e422..4d859fb641df3d 100644 --- a/Doc/c-api/long.rst +++ b/Doc/c-api/long.rst @@ -97,9 +97,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate. .. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base) Convert a sequence of Unicode digits in the string *u* to a Python integer - value. The Unicode string is first encoded to a byte string using - :c:func:`PyUnicode_EncodeDecimal` and then converted using - :c:func:`PyLong_FromString`. + value. .. versionadded:: 3.3 From 246b2de8b3ff1101760b19a14b93e32261c8f530 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 4 Jul 2020 23:07:43 -0600 Subject: [PATCH 05/12] bpo-39168: Remove the __new__ method of typing.Generic (GH-21327) Automerge-Triggered-By: @gvanrossum --- Lib/test/test_typing.py | 2 -- Lib/typing.py | 10 ---------- .../Library/2020-07-04-21-56-46.bpo-39168.DQWsXj.rst | 1 + 3 files changed, 1 insertion(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-07-04-21-56-46.bpo-39168.DQWsXj.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f429e883b59538..398add05a12b99 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1417,8 +1417,6 @@ def test_basics(self): def test_generic_errors(self): T = TypeVar('T') S = TypeVar('S') - with self.assertRaises(TypeError): - Generic[T]() with self.assertRaises(TypeError): Generic[T][T] with self.assertRaises(TypeError): diff --git a/Lib/typing.py b/Lib/typing.py index f94996daebd6ed..fd657caafee870 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -894,16 +894,6 @@ def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: __slots__ = () _is_protocol = False - def __new__(cls, *args, **kwds): - if cls in (Generic, Protocol): - raise TypeError(f"Type {cls.__name__} cannot be instantiated; " - "it can be used only as a base class") - if super().__new__ is object.__new__ and cls.__init__ is not object.__init__: - obj = super().__new__(cls) - else: - obj = super().__new__(cls, *args, **kwds) - return obj - @_tp_cache def __class_getitem__(cls, params): if not isinstance(params, tuple): diff --git a/Misc/NEWS.d/next/Library/2020-07-04-21-56-46.bpo-39168.DQWsXj.rst b/Misc/NEWS.d/next/Library/2020-07-04-21-56-46.bpo-39168.DQWsXj.rst new file mode 100644 index 00000000000000..667885eccd9c79 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-04-21-56-46.bpo-39168.DQWsXj.rst @@ -0,0 +1 @@ +Remove the ``__new__`` method of :class:`typing.Generic`. From bca1003be709c050c4038c5d6e782d90027737b5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 5 Jul 2020 18:53:45 +0300 Subject: [PATCH 06/12] bpo-36346: Undeprecate private function _PyUnicode_AsUnicode(). (GH-21336) --- Include/cpython/unicodeobject.h | 2 +- .../2020-06-17-11-24-00.bpo-36346.fTMr3S.rst | 2 +- Modules/clinic/posixmodule.c.h | 8 +---- Objects/unicodeobject.c | 6 ---- PC/_msi.c | 3 -- PC/clinic/_msi.c.h | 14 +------- PC/clinic/winreg.c.h | 32 +------------------ Tools/clinic/clinic.py | 6 ---- 8 files changed, 5 insertions(+), 68 deletions(-) diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index a82eee45e0ed2e..49ad32d5d199e1 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -581,7 +581,7 @@ Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode( /* Similar to PyUnicode_AsUnicode(), but raises a ValueError if the string contains null characters. */ -Py_DEPRECATED(3.3) PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode( +PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode( PyObject *unicode /* Unicode object */ ); diff --git a/Misc/NEWS.d/next/C API/2020-06-17-11-24-00.bpo-36346.fTMr3S.rst b/Misc/NEWS.d/next/C API/2020-06-17-11-24-00.bpo-36346.fTMr3S.rst index 902a0e60727e6a..1e448303a853cf 100644 --- a/Misc/NEWS.d/next/C API/2020-06-17-11-24-00.bpo-36346.fTMr3S.rst +++ b/Misc/NEWS.d/next/C API/2020-06-17-11-24-00.bpo-36346.fTMr3S.rst @@ -1,4 +1,4 @@ Mark ``Py_UNICODE_COPY``, ``Py_UNICODE_FILL``, ``PyUnicode_WSTR_LENGTH``, -``PyUnicode_FromUnicode``, ``PyUnicode_AsUnicode``, ``_PyUnicode_AsUnicode``, +``PyUnicode_FromUnicode``, ``PyUnicode_AsUnicode``, and ``PyUnicode_AsUnicodeAndSize`` as deprecated in C. Remove ``Py_UNICODE_MATCH`` which was deprecated and broken since Python 3.3. diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index 6533edfdb47d23..c15def0a0f2b8d 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -1688,10 +1688,7 @@ os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *k goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS command = _PyUnicode_AsUnicode(args[0]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ command = PyUnicode_AsWideCharString(args[0], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -7040,10 +7037,7 @@ os_startfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS operation = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ operation = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -8925,4 +8919,4 @@ os_waitstatus_to_exitcode(PyObject *module, PyObject *const *args, Py_ssize_t na #ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF #endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */ -/*[clinic end generated code: output=ba3d4b35fda2c208 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a0fbdea47249ee0c input=a9049054013a1b77]*/ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index ca68c57534b229..809ed85895f86f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3310,14 +3310,11 @@ _PyUnicode_WideCharString_Converter(PyObject *obj, void *ptr) } if (PyUnicode_Check(obj)) { #if USE_UNICODE_WCHAR_CACHE -_Py_COMP_DIAG_PUSH -_Py_COMP_DIAG_IGNORE_DEPR_DECLS *p = (wchar_t *)_PyUnicode_AsUnicode(obj); if (*p == NULL) { return 0; } return 1; -_Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ *p = PyUnicode_AsWideCharString(obj, NULL); if (*p == NULL) { @@ -3349,14 +3346,11 @@ _PyUnicode_WideCharString_Opt_Converter(PyObject *obj, void *ptr) } if (PyUnicode_Check(obj)) { #if USE_UNICODE_WCHAR_CACHE -_Py_COMP_DIAG_PUSH -_Py_COMP_DIAG_IGNORE_DEPR_DECLS *p = (wchar_t *)_PyUnicode_AsUnicode(obj); if (*p == NULL) { return 0; } return 1; -_Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ *p = PyUnicode_AsWideCharString(obj, NULL); if (*p == NULL) { diff --git a/PC/_msi.c b/PC/_msi.c index 9f1015845acff2..f725c816206e72 100644 --- a/PC/_msi.c +++ b/PC/_msi.c @@ -758,10 +758,7 @@ _msi_SummaryInformation_SetProperty_impl(msiobj *self, int field, if (PyUnicode_Check(data)) { #if USE_UNICODE_WCHAR_CACHE -_Py_COMP_DIAG_PUSH -_Py_COMP_DIAG_IGNORE_DEPR_DECLS const WCHAR *value = _PyUnicode_AsUnicode(data); -_Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ WCHAR *value = PyUnicode_AsWideCharString(data, NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ diff --git a/PC/clinic/_msi.c.h b/PC/clinic/_msi.c.h index 895bf39a779f4a..85c4d226ee408c 100644 --- a/PC/clinic/_msi.c.h +++ b/PC/clinic/_msi.c.h @@ -209,10 +209,7 @@ _msi_Record_SetString(msiobj *self, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS value = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ value = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -261,10 +258,7 @@ _msi_Record_SetStream(msiobj *self, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS value = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ value = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -568,10 +562,7 @@ _msi_Database_OpenView(msiobj *self, PyObject *arg) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS sql = _PyUnicode_AsUnicode(arg); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ sql = PyUnicode_AsWideCharString(arg, NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -670,10 +661,7 @@ _msi_OpenDatabase(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS path = _PyUnicode_AsUnicode(args[0]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ path = PyUnicode_AsWideCharString(args[0], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -725,4 +713,4 @@ _msi_CreateRecord(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=39807788326ad0e9 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=49debf733ee5cab2 input=a9049054013a1b77]*/ diff --git a/PC/clinic/winreg.c.h b/PC/clinic/winreg.c.h index 5c97eaeee9e273..3301bed9713aa9 100644 --- a/PC/clinic/winreg.c.h +++ b/PC/clinic/winreg.c.h @@ -160,10 +160,7 @@ winreg_ConnectRegistry(PyObject *module, PyObject *const *args, Py_ssize_t nargs } else if (PyUnicode_Check(args[0])) { #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS computer_name = _PyUnicode_AsUnicode(args[0]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ computer_name = PyUnicode_AsWideCharString(args[0], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -237,10 +234,7 @@ winreg_CreateKey(PyObject *module, PyObject *const *args, Py_ssize_t nargs) } else if (PyUnicode_Check(args[1])) { #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS sub_key = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ sub_key = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -373,10 +367,7 @@ winreg_DeleteKey(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS sub_key = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ sub_key = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -488,10 +479,7 @@ winreg_DeleteValue(PyObject *module, PyObject *const *args, Py_ssize_t nargs) } else if (PyUnicode_Check(args[1])) { #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS value = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ value = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -635,10 +623,7 @@ winreg_ExpandEnvironmentStrings(PyObject *module, PyObject *arg) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS string = _PyUnicode_AsUnicode(arg); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ string = PyUnicode_AsWideCharString(arg, NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -750,10 +735,7 @@ winreg_LoadKey(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS sub_key = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ sub_key = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -765,10 +747,7 @@ winreg_LoadKey(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS file_name = _PyUnicode_AsUnicode(args[2]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ file_name = PyUnicode_AsWideCharString(args[2], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -984,10 +963,7 @@ winreg_QueryValue(PyObject *module, PyObject *const *args, Py_ssize_t nargs) } else if (PyUnicode_Check(args[1])) { #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS sub_key = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ sub_key = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -1050,10 +1026,7 @@ winreg_QueryValueEx(PyObject *module, PyObject *const *args, Py_ssize_t nargs) } else if (PyUnicode_Check(args[1])) { #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS name = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ name = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -1121,10 +1094,7 @@ winreg_SaveKey(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS file_name = _PyUnicode_AsUnicode(args[1]); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ file_name = PyUnicode_AsWideCharString(args[1], NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -1378,4 +1348,4 @@ winreg_QueryReflectionKey(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=fa5f21ea6a75d0e9 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=30b1311886c13907 input=a9049054013a1b77]*/ diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3a9f4c228c22b9..92334d9195fcce 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -3414,10 +3414,7 @@ def parse_arg(self, argname, argnum): goto exit; }}}} #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS {paramname} = _PyUnicode_AsUnicode({argname}); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ {paramname} = PyUnicode_AsWideCharString({argname}, NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ @@ -3432,10 +3429,7 @@ def parse_arg(self, argname, argnum): }}}} else if (PyUnicode_Check({argname})) {{{{ #if USE_UNICODE_WCHAR_CACHE - _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS {paramname} = _PyUnicode_AsUnicode({argname}); - _Py_COMP_DIAG_POP #else /* USE_UNICODE_WCHAR_CACHE */ {paramname} = PyUnicode_AsWideCharString({argname}, NULL); #endif /* USE_UNICODE_WCHAR_CACHE */ From 78fc4bc22ac98ef421f01faed57eb6dc26e74275 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Sun, 5 Jul 2020 22:43:14 +0100 Subject: [PATCH 07/12] bpo-29727: Register array.array as a MutableSequence (GH-21338) --- Lib/test/test_array.py | 66 +++++++++++++++++++ .../2020-07-05-19-16-02.bpo-29727.Q6Z2rg.rst | 2 + Modules/arraymodule.c | 20 ++++++ 3 files changed, 88 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-07-05-19-16-02.bpo-29727.Q6Z2rg.rst diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 6af90dfb871d79..c423f545488f82 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -2,6 +2,7 @@ Roger E. Masse """ +import collections.abc import unittest from test import support from test.support import os_helper @@ -29,6 +30,10 @@ def __init__(self, typecode, newarg=None): class MiscTest(unittest.TestCase): + def test_array_is_sequence(self): + self.assertIsInstance(array.array("B"), collections.abc.MutableSequence) + self.assertIsInstance(array.array("B"), collections.abc.Reversible) + def test_bad_constructor(self): self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array, spam=42) @@ -331,6 +336,67 @@ def test_exhausted_iterator(self): self.assertEqual(list(empit), [self.outside]) self.assertEqual(list(a), list(self.example) + [self.outside]) + def test_reverse_iterator(self): + a = array.array(self.typecode, self.example) + self.assertEqual(list(a), list(self.example)) + self.assertEqual(list(reversed(a)), list(iter(a))[::-1]) + + def test_reverse_iterator_picking(self): + orig = array.array(self.typecode, self.example) + data = list(orig) + data2 = [self.outside] + data + rev_data = data[len(data)-2::-1] + [self.outside] + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + # initial iterator + itorig = reversed(orig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.insert(0, self.outside) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), rev_data) + self.assertEqual(list(a), data2) + + # running iterator + next(itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.insert(0, self.outside) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), rev_data[1:]) + self.assertEqual(list(a), data2) + + # empty iterator + for i in range(1, len(data)): + next(itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.insert(0, self.outside) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), []) + self.assertEqual(list(a), data2) + + # exhausted iterator + self.assertRaises(StopIteration, next, itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.insert(0, self.outside) + self.assertEqual(list(it), []) + self.assertEqual(list(a), data2) + + def test_exhausted_reverse_iterator(self): + a = array.array(self.typecode, self.example) + self.assertEqual(list(a), list(self.example)) + exhit = reversed(a) + empit = reversed(a) + for x in exhit: # exhaust the iterator + next(empit) # Pointing past the 0th position. + a.insert(0, self.outside) + self.assertEqual(list(exhit), []) + # The iterator index points past the 0th position so inserting + # an element in the beggining does not make it appear. + self.assertEqual(list(empit), []) + self.assertEqual(list(a), [self.outside] + list(self.example)) + def test_insert(self): a = array.array(self.typecode, self.example) a.insert(0, self.example[0]) diff --git a/Misc/NEWS.d/next/Library/2020-07-05-19-16-02.bpo-29727.Q6Z2rg.rst b/Misc/NEWS.d/next/Library/2020-07-05-19-16-02.bpo-29727.Q6Z2rg.rst new file mode 100644 index 00000000000000..85cfa4f8938112 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-05-19-16-02.bpo-29727.Q6Z2rg.rst @@ -0,0 +1,2 @@ +Register :class:`array.array` as a +:class:`~collections.abc.MutableSequence`. Patch by Pablo Galindo. diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 2d498c7e829415..2ba2ff43aa8b8a 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -2989,6 +2989,26 @@ array_modexec(PyObject *m) Py_DECREF((PyObject *)&Arraytype); return -1; } + + PyObject *abc_mod = PyImport_ImportModule("collections.abc"); + if (!abc_mod) { + Py_DECREF((PyObject *)&Arraytype); + return -1; + } + PyObject *mutablesequence = PyObject_GetAttrString(abc_mod, "MutableSequence"); + Py_DECREF(abc_mod); + if (!mutablesequence) { + Py_DECREF((PyObject *)&Arraytype); + return -1; + } + PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O", (PyObject *)&Arraytype); + Py_DECREF(mutablesequence); + if (!res) { + Py_DECREF((PyObject *)&Arraytype); + return -1; + } + Py_DECREF(res); + Py_INCREF((PyObject *)&Arraytype); if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) { Py_DECREF((PyObject *)&Arraytype); From a085bc2217a8fefd4f1d5a9c96de098c5f905241 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sun, 5 Jul 2020 22:42:24 -0300 Subject: [PATCH 08/12] bpo-26205: Specify the number of nested scopes (GH-21324) * Clarify number of scopes * Indicate 3 or 4 Co-authored-by: Terry Jan Reedy Co-authored-by: Terry Jan Reedy --- Doc/tutorial/classes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 06bdd0d93515ed..250d2a9ddb416b 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -114,8 +114,8 @@ accessible. "Directly accessible" here means that an unqualified reference to a name attempts to find the name in the namespace. Although scopes are determined statically, they are used dynamically. At any -time during execution, there are at least three nested scopes whose namespaces -are directly accessible: +time during execution, At any time during execution, there are 3 or 4 nested +scopes whose namespaces are directly accessible: * the innermost scope, which is searched first, contains the local names * the scopes of any enclosing functions, which are searched starting with the From f3ebc64a7a7d6631d6e68e8c28a8d5fe5615585f Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sun, 5 Jul 2020 22:47:15 -0300 Subject: [PATCH 09/12] bpo-28681: Clarify multiple function names in the tutorial (GH-21340) * improve control flow docs * Add also Co-authored-by: Terry Jan Reedy Co-authored-by: Terry Jan Reedy --- Doc/tutorial/controlflow.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index 26de866aab90cb..5d5b01d8132771 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -300,11 +300,10 @@ passed using *call by value* (where the *value* is always an object *reference*, not the value of the object). [#]_ When a function calls another function, a new local symbol table is created for that call. -A function definition introduces the function name in the current symbol table. -The value of the function name has a type that is recognized by the interpreter -as a user-defined function. This value can be assigned to another name which -can then also be used as a function. This serves as a general renaming -mechanism:: +A function definition associates the function name with the function object in +the current symbol table. The interpreter recognizes the object pointed to by +that name as a user-defined function. Other names can also point to that same +function object and can also be used to access the function:: >>> fib From 4b5437892e88913630698abd3c8fc8e9265eb1fa Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 6 Jul 2020 11:48:30 +0900 Subject: [PATCH 10/12] bpo-41165: Deprecate PyEval_ReleaseLock() (GH-21309) --- Include/ceval.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Include/ceval.h b/Include/ceval.h index df5253900eea71..0f372e2044a1c8 100644 --- a/Include/ceval.h +++ b/Include/ceval.h @@ -128,8 +128,12 @@ PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *); Py_DEPRECATED(3.9) PyAPI_FUNC(int) PyEval_ThreadsInitialized(void); Py_DEPRECATED(3.9) PyAPI_FUNC(void) PyEval_InitThreads(void); +/* PyEval_AcquireLock() and PyEval_ReleaseLock() are part of stable ABI. + * They will be removed from this header file in the future version. + * But they will be remained in ABI until Python 4.0. + */ Py_DEPRECATED(3.2) PyAPI_FUNC(void) PyEval_AcquireLock(void); -/* Py_DEPRECATED(3.2) */ PyAPI_FUNC(void) PyEval_ReleaseLock(void); +Py_DEPRECATED(3.2) PyAPI_FUNC(void) PyEval_ReleaseLock(void); PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate); PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate); From abbf2ff6e21ad0a62d5c22f84dc62f1982a34bc7 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Mon, 6 Jul 2020 17:12:49 +0800 Subject: [PATCH 11/12] bpo-40275: Use new test.support helper submodules in tests (GH-21314) --- Lib/test/pickletester.py | 13 +- Lib/test/test_binhex.py | 20 ++- Lib/test/test_bufio.py | 9 +- Lib/test/test_capi.py | 3 +- Lib/test/test_compileall.py | 11 +- Lib/test/test_fileio.py | 5 +- Lib/test/test_heapq.py | 5 +- Lib/test/test_httplib.py | 21 +-- Lib/test/test_importlib/util.py | 5 +- Lib/test/test_io.py | 257 ++++++++++++++++---------------- Lib/test/test_json/__init__.py | 6 +- Lib/test/test_json/test_tool.py | 9 +- Lib/test/test_netrc.py | 19 +-- Lib/test/test_nis.py | 4 +- Lib/test/test_pickle.py | 3 +- Lib/test/test_sqlite.py | 3 +- Lib/test/test_univnewlines.py | 16 +- Lib/test/test_uuid.py | 5 +- 18 files changed, 222 insertions(+), 192 deletions(-) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index a34505aab51c17..afbc2b3bf0a79c 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -22,10 +22,13 @@ _testbuffer = None from test import support +from test.support import os_helper from test.support import ( - TestFailed, TESTFN, run_with_locale, no_tracing, - _2G, _4G, bigmemtest, forget, + TestFailed, run_with_locale, no_tracing, + _2G, _4G, bigmemtest ) +from test.support.import_helper import forget +from test.support.os_helper import TESTFN from test.support import threading_helper from test.support.warnings_helper import save_restore_warnings_filters @@ -3100,7 +3103,7 @@ def test_dump_closed_file(self): f.close() self.assertRaises(ValueError, self.dump, 123, f) finally: - support.unlink(TESTFN) + os_helper.unlink(TESTFN) def test_load_closed_file(self): f = open(TESTFN, "wb") @@ -3108,7 +3111,7 @@ def test_load_closed_file(self): f.close() self.assertRaises(ValueError, self.dump, 123, f) finally: - support.unlink(TESTFN) + os_helper.unlink(TESTFN) def test_load_from_and_dump_to_file(self): stream = io.BytesIO() @@ -3139,7 +3142,7 @@ def test_dump_text_file(self): self.assertRaises(TypeError, self.dump, 123, f, proto) finally: f.close() - support.unlink(TESTFN) + os_helper.unlink(TESTFN) def test_incomplete_input(self): s = io.BytesIO(b"X''.") diff --git a/Lib/test/test_binhex.py b/Lib/test/test_binhex.py index 591f32a4f0f7f0..5e59f5761514c6 100644 --- a/Lib/test/test_binhex.py +++ b/Lib/test/test_binhex.py @@ -5,23 +5,27 @@ """ import unittest from test import support +from test.support import import_helper +from test.support import os_helper +from test.support import warnings_helper -with support.check_warnings(('', DeprecationWarning)): - binhex = support.import_fresh_module('binhex') + +with warnings_helper.check_warnings(('', DeprecationWarning)): + binhex = import_helper.import_fresh_module('binhex') class BinHexTestCase(unittest.TestCase): def setUp(self): # binhex supports only file names encodable to Latin1 - self.fname1 = support.TESTFN_ASCII + "1" - self.fname2 = support.TESTFN_ASCII + "2" - self.fname3 = support.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__" + self.fname1 = os_helper.TESTFN_ASCII + "1" + self.fname2 = os_helper.TESTFN_ASCII + "2" + self.fname3 = os_helper.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__" def tearDown(self): - support.unlink(self.fname1) - support.unlink(self.fname2) - support.unlink(self.fname3) + os_helper.unlink(self.fname1) + os_helper.unlink(self.fname2) + os_helper.unlink(self.fname3) DATA = b'Jack is my hero' diff --git a/Lib/test/test_bufio.py b/Lib/test/test_bufio.py index fea6da491e4914..17151b13615e94 100644 --- a/Lib/test/test_bufio.py +++ b/Lib/test/test_bufio.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import os_helper import io # C implementation. import _pyio as pyio # Python implementation. @@ -17,18 +18,18 @@ def try_one(self, s): # .readline()s deliver what we wrote. # Ensure we can open TESTFN for writing. - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) # Since C doesn't guarantee we can write/read arbitrary bytes in text # files, use binary mode. - f = self.open(support.TESTFN, "wb") + f = self.open(os_helper.TESTFN, "wb") try: # write once with \n and once without f.write(s) f.write(b"\n") f.write(s) f.close() - f = open(support.TESTFN, "rb") + f = open(os_helper.TESTFN, "rb") line = f.readline() self.assertEqual(line, s + b"\n") line = f.readline() @@ -37,7 +38,7 @@ def try_one(self, s): self.assertFalse(line) # Must be at EOF f.close() finally: - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def drive_one(self, pattern): for length in lengths: diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index 3e7afd5a2a1e43..55027c9cbcd43a 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -17,6 +17,7 @@ import importlib.util from test import support from test.support import MISSING_C_DOCSTRINGS +from test.support import import_helper from test.support import threading_helper from test.support.script_helper import assert_python_failure, assert_python_ok try: @@ -25,7 +26,7 @@ _posixsubprocess = None # Skip this test if the _testcapi module isn't available. -_testcapi = support.import_module('_testcapi') +_testcapi = import_helper.import_module('_testcapi') import _testinternalcapi diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 3bbc6817f8d56e..be1149a87faef1 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -23,6 +23,7 @@ _have_multiprocessing = False from test import support +from test.support import os_helper from test.support import script_helper from .test_py_compile import without_source_date_epoch @@ -356,7 +357,7 @@ def test_multiple_optimization_levels(self): except Exception: pass - @support.skip_unless_symlink + @os_helper.skip_unless_symlink def test_ignore_symlink_destination(self): # Create folders for allowed files, symlinks and prohibited area allowed_path = os.path.join(self.directory, "test", "dir", "allowed") @@ -438,7 +439,7 @@ def setUpClass(cls): sys_path_writable = False break finally: - support.unlink(str(path)) + os_helper.unlink(str(path)) if directory_created: directory.rmdir() else: @@ -477,7 +478,7 @@ def assertNotCompiled(self, fn): def setUp(self): self.directory = tempfile.mkdtemp() - self.addCleanup(support.rmtree, self.directory) + self.addCleanup(os_helper.rmtree, self.directory) self.pkgdir = os.path.join(self.directory, 'foo') os.mkdir(self.pkgdir) self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') @@ -625,7 +626,7 @@ def test_recursion_limit(self): self.assertCompiled(spamfn) self.assertCompiled(eggfn) - @support.skip_unless_symlink + @os_helper.skip_unless_symlink def test_symlink_loop(self): # Currently, compileall ignores symlinks to directories. # If that limitation is ever lifted, it should protect against @@ -823,7 +824,7 @@ def test_multiple_optimization_levels(self): except Exception: pass - @support.skip_unless_symlink + @os_helper.skip_unless_symlink def test_ignore_symlink_destination(self): # Create folders for allowed files, symlinks and prohibited area allowed_path = os.path.join(self.directory, "test", "dir", "allowed") diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 26e4500ae8cfcd..ff611a90eede3b 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -9,8 +9,9 @@ from weakref import proxy from functools import wraps -from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest, - make_bad_fd, cpython_only, swap_attr) +from test.support import (run_unittest, cpython_only, swap_attr) +from test.support.os_helper import (TESTFN, TESTFN_UNICODE, make_bad_fd) +from test.support.warnings_helper import check_warnings from collections import UserList import _io # C implementation of io diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index 6902573e8fa85a..ebbc62745707cc 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -5,11 +5,12 @@ import doctest from test import support +from test.support import import_helper from unittest import TestCase, skipUnless from operator import itemgetter -py_heapq = support.import_fresh_module('heapq', blocked=['_heapq']) -c_heapq = support.import_fresh_module('heapq', fresh=['_heapq']) +py_heapq = import_helper.import_fresh_module('heapq', blocked=['_heapq']) +c_heapq = import_helper.import_fresh_module('heapq', fresh=['_heapq']) # _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when # _heapq is imported, so check them there diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index e909980d23aac1..1ac31bf2a8e7fb 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -13,7 +13,10 @@ TestCase = unittest.TestCase from test import support +from test.support import os_helper from test.support import socket_helper +from test.support import warnings_helper + here = os.path.dirname(__file__) # Self-signed cert file for 'localhost' @@ -1766,14 +1769,14 @@ def test_local_bad_hostname(self): with self.assertRaises(ssl.CertificateError): h.request('GET', '/') # Same with explicit check_hostname=True - with support.check_warnings(('', DeprecationWarning)): + with warnings_helper.check_warnings(('', DeprecationWarning)): h = client.HTTPSConnection('localhost', server.port, context=context, check_hostname=True) with self.assertRaises(ssl.CertificateError): h.request('GET', '/') # With check_hostname=False, the mismatching is ignored context.check_hostname = False - with support.check_warnings(('', DeprecationWarning)): + with warnings_helper.check_warnings(('', DeprecationWarning)): h = client.HTTPSConnection('localhost', server.port, context=context, check_hostname=False) h.request('GET', '/nonexistent') @@ -1792,7 +1795,7 @@ def test_local_bad_hostname(self): h.close() # Passing check_hostname to HTTPSConnection should override the # context's setting. - with support.check_warnings(('', DeprecationWarning)): + with warnings_helper.check_warnings(('', DeprecationWarning)): h = client.HTTPSConnection('localhost', server.port, context=context, check_hostname=True) with self.assertRaises(ssl.CertificateError): @@ -1908,10 +1911,10 @@ def test_bytes_body(self): self.assertEqual(b'body\xc1', f.read()) def test_text_file_body(self): - self.addCleanup(support.unlink, support.TESTFN) - with open(support.TESTFN, "w") as f: + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with open(os_helper.TESTFN, "w") as f: f.write("body") - with open(support.TESTFN) as f: + with open(os_helper.TESTFN) as f: self.conn.request("PUT", "/url", f) message, f = self.get_headers_and_fp() self.assertEqual("text/plain", message.get_content_type()) @@ -1923,10 +1926,10 @@ def test_text_file_body(self): self.assertEqual(b'4\r\nbody\r\n0\r\n\r\n', f.read()) def test_binary_file_body(self): - self.addCleanup(support.unlink, support.TESTFN) - with open(support.TESTFN, "wb") as f: + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with open(os_helper.TESTFN, "wb") as f: f.write(b"body\xc1") - with open(support.TESTFN, "rb") as f: + with open(os_helper.TESTFN, "rb") as f: self.conn.request("PUT", "/url", f) message, f = self.get_headers_and_fp() self.assertEqual("text/plain", message.get_content_type()) diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py index de6e0b02560184..2745c9b7e3c3bf 100644 --- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -12,6 +12,7 @@ import os.path from pathlib import Path, PurePath from test import support +from test.support import import_helper import unittest import sys import tempfile @@ -55,8 +56,8 @@ def _extension_details(): def import_importlib(module_name): """Import a module from importlib both w/ and w/o _frozen_importlib.""" fresh = ('importlib',) if '.' in module_name else () - frozen = support.import_fresh_module(module_name) - source = support.import_fresh_module(module_name, fresh=fresh, + frozen = import_helper.import_fresh_module(module_name) + source = import_helper.import_fresh_module(module_name, fresh=fresh, blocked=('_frozen_importlib', '_frozen_importlib_external')) return {'Frozen': frozen, 'Source': source} diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index c0d67a17d8c6f1..18b8a7941668ff 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -40,8 +40,11 @@ from test import support from test.support.script_helper import ( assert_python_ok, assert_python_failure, run_python_until_end) +from test.support import import_helper +from test.support import os_helper from test.support import threading_helper -from test.support import FakePath +from test.support import warnings_helper +from test.support.os_helper import FakePath import codecs import io # C implementation of io @@ -317,10 +320,10 @@ class PyMockNonBlockWriterIO(MockNonBlockWriterIO, pyio.RawIOBase): class IOTest(unittest.TestCase): def setUp(self): - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def tearDown(self): - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def write_ops(self, f): self.assertEqual(f.write(b"blah."), 5) @@ -405,19 +408,19 @@ def test_invalid_operations(self): # Try writing on a file opened in read mode and vice-versa. exc = self.UnsupportedOperation for mode in ("w", "wb"): - with self.open(support.TESTFN, mode) as fp: + with self.open(os_helper.TESTFN, mode) as fp: self.assertRaises(exc, fp.read) self.assertRaises(exc, fp.readline) - with self.open(support.TESTFN, "wb", buffering=0) as fp: + with self.open(os_helper.TESTFN, "wb", buffering=0) as fp: self.assertRaises(exc, fp.read) self.assertRaises(exc, fp.readline) - with self.open(support.TESTFN, "rb", buffering=0) as fp: + with self.open(os_helper.TESTFN, "rb", buffering=0) as fp: self.assertRaises(exc, fp.write, b"blah") self.assertRaises(exc, fp.writelines, [b"blah\n"]) - with self.open(support.TESTFN, "rb") as fp: + with self.open(os_helper.TESTFN, "rb") as fp: self.assertRaises(exc, fp.write, b"blah") self.assertRaises(exc, fp.writelines, [b"blah\n"]) - with self.open(support.TESTFN, "r") as fp: + with self.open(os_helper.TESTFN, "r") as fp: self.assertRaises(exc, fp.write, "blah") self.assertRaises(exc, fp.writelines, ["blah\n"]) # Non-zero seeking from current or end pos @@ -538,33 +541,33 @@ def test_open_handles_NUL_chars(self): self.assertRaises(ValueError, self.open, bytes_fn, 'w') def test_raw_file_io(self): - with self.open(support.TESTFN, "wb", buffering=0) as f: + with self.open(os_helper.TESTFN, "wb", buffering=0) as f: self.assertEqual(f.readable(), False) self.assertEqual(f.writable(), True) self.assertEqual(f.seekable(), True) self.write_ops(f) - with self.open(support.TESTFN, "rb", buffering=0) as f: + with self.open(os_helper.TESTFN, "rb", buffering=0) as f: self.assertEqual(f.readable(), True) self.assertEqual(f.writable(), False) self.assertEqual(f.seekable(), True) self.read_ops(f) def test_buffered_file_io(self): - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: self.assertEqual(f.readable(), False) self.assertEqual(f.writable(), True) self.assertEqual(f.seekable(), True) self.write_ops(f) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.readable(), True) self.assertEqual(f.writable(), False) self.assertEqual(f.seekable(), True) self.read_ops(f, True) def test_readline(self): - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(b"abc\ndef\nxyzzy\nfoo\x00bar\nanother line") - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.readline(), b"abc\n") self.assertEqual(f.readline(10), b"def\n") self.assertEqual(f.readline(2), b"xy") @@ -572,7 +575,7 @@ def test_readline(self): self.assertEqual(f.readline(), b"foo\x00bar\n") self.assertEqual(f.readline(None), b"another line") self.assertRaises(TypeError, f.readline, 5.3) - with self.open(support.TESTFN, "r") as f: + with self.open(os_helper.TESTFN, "r") as f: self.assertRaises(TypeError, f.readline, 5.3) def test_readline_nonsizeable(self): @@ -607,20 +610,20 @@ def test_large_file_ops(self): support.requires( 'largefile', 'test requires %s bytes and a long time to run' % self.LARGE) - with self.open(support.TESTFN, "w+b", 0) as f: + with self.open(os_helper.TESTFN, "w+b", 0) as f: self.large_file_ops(f) - with self.open(support.TESTFN, "w+b") as f: + with self.open(os_helper.TESTFN, "w+b") as f: self.large_file_ops(f) def test_with_open(self): for bufsize in (0, 100): f = None - with self.open(support.TESTFN, "wb", bufsize) as f: + with self.open(os_helper.TESTFN, "wb", bufsize) as f: f.write(b"xxx") self.assertEqual(f.closed, True) f = None try: - with self.open(support.TESTFN, "wb", bufsize) as f: + with self.open(os_helper.TESTFN, "wb", bufsize) as f: 1/0 except ZeroDivisionError: self.assertEqual(f.closed, True) @@ -629,13 +632,13 @@ def test_with_open(self): # issue 5008 def test_append_mode_tell(self): - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(b"xxx") - with self.open(support.TESTFN, "ab", buffering=0) as f: + with self.open(os_helper.TESTFN, "ab", buffering=0) as f: self.assertEqual(f.tell(), 3) - with self.open(support.TESTFN, "ab") as f: + with self.open(os_helper.TESTFN, "ab") as f: self.assertEqual(f.tell(), 3) - with self.open(support.TESTFN, "a") as f: + with self.open(os_helper.TESTFN, "a") as f: self.assertGreater(f.tell(), 0) def test_destructor(self): @@ -655,13 +658,13 @@ def close(self): def flush(self): record.append(3) super().flush() - with support.check_warnings(('', ResourceWarning)): - f = MyFileIO(support.TESTFN, "wb") + with warnings_helper.check_warnings(('', ResourceWarning)): + f = MyFileIO(os_helper.TESTFN, "wb") f.write(b"xxx") del f support.gc_collect() self.assertEqual(record, [1, 2, 3]) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.read(), b"xxx") def _check_base_destructor(self, base): @@ -707,9 +710,9 @@ def test_TextIOBase_destructor(self): self._check_base_destructor(self.TextIOBase) def test_close_flushes(self): - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(b"xxx") - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.read(), b"xxx") def test_array_writes(self): @@ -720,25 +723,25 @@ def check(f): self.assertEqual(f.write(a), n) f.writelines((a,)) check(self.BytesIO()) - check(self.FileIO(support.TESTFN, "w")) + check(self.FileIO(os_helper.TESTFN, "w")) check(self.BufferedWriter(self.MockRawIO())) check(self.BufferedRandom(self.MockRawIO())) check(self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())) def test_closefd(self): - self.assertRaises(ValueError, self.open, support.TESTFN, 'w', + self.assertRaises(ValueError, self.open, os_helper.TESTFN, 'w', closefd=False) def test_read_closed(self): - with self.open(support.TESTFN, "w") as f: + with self.open(os_helper.TESTFN, "w") as f: f.write("egg\n") - with self.open(support.TESTFN, "r") as f: + with self.open(os_helper.TESTFN, "r") as f: file = self.open(f.fileno(), "r", closefd=False) self.assertEqual(file.read(), "egg\n") file.seek(0) file.close() self.assertRaises(ValueError, file.read) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: file = self.open(f.fileno(), "rb", closefd=False) self.assertEqual(file.read()[:3], b"egg") file.close() @@ -746,12 +749,12 @@ def test_read_closed(self): def test_no_closefd_with_filename(self): # can't use closefd in combination with a file name - self.assertRaises(ValueError, self.open, support.TESTFN, "r", closefd=False) + self.assertRaises(ValueError, self.open, os_helper.TESTFN, "r", closefd=False) def test_closefd_attr(self): - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(b"egg\n") - with self.open(support.TESTFN, "r") as f: + with self.open(os_helper.TESTFN, "r") as f: self.assertEqual(f.buffer.raw.closefd, True) file = self.open(f.fileno(), "r", closefd=False) self.assertEqual(file.buffer.raw.closefd, False) @@ -759,15 +762,15 @@ def test_closefd_attr(self): def test_garbage_collection(self): # FileIO objects are collected, and collecting them flushes # all data to disk. - with support.check_warnings(('', ResourceWarning)): - f = self.FileIO(support.TESTFN, "wb") + with warnings_helper.check_warnings(('', ResourceWarning)): + f = self.FileIO(os_helper.TESTFN, "wb") f.write(b"abcxxx") f.f = f wr = weakref.ref(f) del f support.gc_collect() self.assertIsNone(wr(), wr) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.read(), b"abcxxx") def test_unbounded_file(self): @@ -804,29 +807,29 @@ def bad_flush(): def test_flush_error_on_close(self): # raw file # Issue #5700: io.FileIO calls flush() after file closed - self.check_flush_error_on_close(support.TESTFN, 'wb', buffering=0) - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + self.check_flush_error_on_close(os_helper.TESTFN, 'wb', buffering=0) + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'wb', buffering=0) - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'wb', buffering=0, closefd=False) os.close(fd) # buffered io - self.check_flush_error_on_close(support.TESTFN, 'wb') - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + self.check_flush_error_on_close(os_helper.TESTFN, 'wb') + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'wb') - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'wb', closefd=False) os.close(fd) # text io - self.check_flush_error_on_close(support.TESTFN, 'w') - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + self.check_flush_error_on_close(os_helper.TESTFN, 'w') + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'w') - fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT) + fd = os.open(os_helper.TESTFN, os.O_WRONLY|os.O_CREAT) self.check_flush_error_on_close(fd, 'w', closefd=False) os.close(fd) def test_multi_close(self): - f = self.open(support.TESTFN, "wb", buffering=0) + f = self.open(os_helper.TESTFN, "wb", buffering=0) f.close() f.close() f.close() @@ -857,9 +860,9 @@ def test_types_have_dict(self): self.assertTrue(hasattr(obj, "__dict__")) def test_opener(self): - with self.open(support.TESTFN, "w") as f: + with self.open(os_helper.TESTFN, "w") as f: f.write("egg\n") - fd = os.open(support.TESTFN, os.O_RDONLY) + fd = os.open(os_helper.TESTFN, os.O_RDONLY) def opener(path, flags): return fd with self.open("non-existent", "r", opener=opener) as f: @@ -894,14 +897,14 @@ def test_fileio_closefd(self): f2.readline() def test_nonbuffered_textio(self): - with support.check_no_resource_warning(self): + with warnings_helper.check_no_resource_warning(self): with self.assertRaises(ValueError): - self.open(support.TESTFN, 'w', buffering=0) + self.open(os_helper.TESTFN, 'w', buffering=0) def test_invalid_newline(self): - with support.check_no_resource_warning(self): + with warnings_helper.check_no_resource_warning(self): with self.assertRaises(ValueError): - self.open(support.TESTFN, 'w', newline='invalid') + self.open(os_helper.TESTFN, 'w', newline='invalid') def test_buffered_readinto_mixin(self): # Test the implementation provided by BufferedIOBase @@ -924,10 +927,10 @@ def check_path_succeeds(path): with self.open(path, "r") as f: self.assertEqual(f.read(), "egg\n") - check_path_succeeds(FakePath(support.TESTFN)) - check_path_succeeds(FakePath(support.TESTFN.encode('utf-8'))) + check_path_succeeds(FakePath(os_helper.TESTFN)) + check_path_succeeds(FakePath(os_helper.TESTFN.encode('utf-8'))) - with self.open(support.TESTFN, "w") as f: + with self.open(os_helper.TESTFN, "w") as f: bad_path = FakePath(f.fileno()) with self.assertRaises(TypeError): self.open(bad_path, 'w') @@ -942,7 +945,7 @@ def check_path_succeeds(path): # ensure that refcounting is correct with some error conditions with self.assertRaisesRegex(ValueError, 'read/write/append mode'): - self.open(FakePath(support.TESTFN), 'rwxa') + self.open(FakePath(os_helper.TESTFN), 'rwxa') def test_RawIOBase_readall(self): # Exercise the default unlimited RawIOBase.read() and readall() @@ -1454,9 +1457,9 @@ def test_threads(self): l = list(range(256)) * N random.shuffle(l) s = bytes(bytearray(l)) - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(s) - with self.open(support.TESTFN, self.read_mode, buffering=0) as raw: + with self.open(os_helper.TESTFN, self.read_mode, buffering=0) as raw: bufio = self.tp(raw, 8) errors = [] results = [] @@ -1482,7 +1485,7 @@ def f(): c = bytes(bytearray([i])) self.assertEqual(s.count(c), N) finally: - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_unseekable(self): bufio = self.tp(self.MockUnseekableIO(b"A" * 10)) @@ -1572,9 +1575,9 @@ def test_misbehaved_io_read(self): def test_garbage_collection(self): # C BufferedReader objects are collected. # The Python version has __del__, so it ends into gc.garbage instead - self.addCleanup(support.unlink, support.TESTFN) - with support.check_warnings(('', ResourceWarning)): - rawio = self.FileIO(support.TESTFN, "w+b") + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with warnings_helper.check_warnings(('', ResourceWarning)): + rawio = self.FileIO(os_helper.TESTFN, "w+b") f = self.tp(rawio) f.f = f wr = weakref.ref(f) @@ -1791,26 +1794,26 @@ def test_destructor(self): def test_truncate(self): # Truncate implicitly flushes the buffer. - self.addCleanup(support.unlink, support.TESTFN) - with self.open(support.TESTFN, self.write_mode, buffering=0) as raw: + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with self.open(os_helper.TESTFN, self.write_mode, buffering=0) as raw: bufio = self.tp(raw, 8) bufio.write(b"abcdef") self.assertEqual(bufio.truncate(3), 3) self.assertEqual(bufio.tell(), 6) - with self.open(support.TESTFN, "rb", buffering=0) as f: + with self.open(os_helper.TESTFN, "rb", buffering=0) as f: self.assertEqual(f.read(), b"abc") def test_truncate_after_write(self): # Ensure that truncate preserves the file position after # writes longer than the buffer size. # Issue: https://bugs.python.org/issue32228 - self.addCleanup(support.unlink, support.TESTFN) - with self.open(support.TESTFN, "wb") as f: + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with self.open(os_helper.TESTFN, "wb") as f: # Fill with some buffer f.write(b'\x00' * 10000) buffer_sizes = [8192, 4096, 200] for buffer_size in buffer_sizes: - with self.open(support.TESTFN, "r+b", buffering=buffer_size) as f: + with self.open(os_helper.TESTFN, "r+b", buffering=buffer_size) as f: f.write(b'\x00' * (buffer_size + 1)) # After write write_pos and write_end are set to 0 f.read(1) @@ -1838,7 +1841,7 @@ def test_threads(self): # writing the buffer to the raw streams. This is in addition # to concurrency issues due to switching threads in the middle # of Python code. - with self.open(support.TESTFN, self.write_mode, buffering=0) as raw: + with self.open(os_helper.TESTFN, self.write_mode, buffering=0) as raw: bufio = self.tp(raw, 8) errors = [] def f(): @@ -1858,12 +1861,12 @@ def f(): self.assertFalse(errors, "the following exceptions were caught: %r" % errors) bufio.close() - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: s = f.read() for i in range(256): self.assertEqual(s.count(bytes([i])), N) finally: - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_misbehaved_io(self): rawio = self.MisbehavedRawIO() @@ -1931,9 +1934,9 @@ def test_garbage_collection(self): # C BufferedWriter objects are collected, and collecting them flushes # all data to disk. # The Python version has __del__, so it ends into gc.garbage instead - self.addCleanup(support.unlink, support.TESTFN) - with support.check_warnings(('', ResourceWarning)): - rawio = self.FileIO(support.TESTFN, "w+b") + self.addCleanup(os_helper.unlink, os_helper.TESTFN) + with warnings_helper.check_warnings(('', ResourceWarning)): + rawio = self.FileIO(os_helper.TESTFN, "w+b") f = self.tp(rawio) f.write(b"123xxx") f.x = f @@ -1941,7 +1944,7 @@ def test_garbage_collection(self): del f support.gc_collect() self.assertIsNone(wr(), wr) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.read(), b"123xxx") def test_args_error(self): @@ -2579,10 +2582,10 @@ class TextIOWrapperTest(unittest.TestCase): def setUp(self): self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def tearDown(self): - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test_constructor(self): r = self.BytesIO(b"\xc3\xa9\n\n") @@ -2918,11 +2921,11 @@ def test_error_through_destructor(self): def test_basic_io(self): for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65): for enc in "ascii", "latin-1", "utf-8" :# , "utf-16-be", "utf-16-le": - f = self.open(support.TESTFN, "w+", encoding=enc) + f = self.open(os_helper.TESTFN, "w+", encoding=enc) f._CHUNK_SIZE = chunksize self.assertEqual(f.write("abc"), 3) f.close() - f = self.open(support.TESTFN, "r+", encoding=enc) + f = self.open(os_helper.TESTFN, "r+", encoding=enc) f._CHUNK_SIZE = chunksize self.assertEqual(f.tell(), 0) self.assertEqual(f.read(), "abc") @@ -2967,7 +2970,7 @@ def multi_line_test(self, f, enc): self.assertEqual(rlines, wlines) def test_telling(self): - f = self.open(support.TESTFN, "w+", encoding="utf-8") + f = self.open(os_helper.TESTFN, "w+", encoding="utf-8") p0 = f.tell() f.write("\xff\n") p1 = f.tell() @@ -2995,9 +2998,9 @@ def test_seeking(self): u_suffix = "\u8888\n" suffix = bytes(u_suffix.encode("utf-8")) line = prefix + suffix - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(line*2) - with self.open(support.TESTFN, "r", encoding="utf-8") as f: + with self.open(os_helper.TESTFN, "r", encoding="utf-8") as f: s = f.read(prefix_size) self.assertEqual(s, str(prefix, "ascii")) self.assertEqual(f.tell(), prefix_size) @@ -3006,9 +3009,9 @@ def test_seeking(self): def test_seeking_too(self): # Regression test for a specific bug data = b'\xe0\xbf\xbf\n' - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: f.write(data) - with self.open(support.TESTFN, "r", encoding="utf-8") as f: + with self.open(os_helper.TESTFN, "r", encoding="utf-8") as f: f._CHUNK_SIZE # Just test that it exists f._CHUNK_SIZE = 2 f.readline() @@ -3022,17 +3025,17 @@ def test_seek_and_tell(self): def test_seek_and_tell_with_data(data, min_pos=0): """Tell/seek to various points within a data stream and ensure that the decoded data returned by read() is consistent.""" - f = self.open(support.TESTFN, 'wb') + f = self.open(os_helper.TESTFN, 'wb') f.write(data) f.close() - f = self.open(support.TESTFN, encoding='test_decoder') + f = self.open(os_helper.TESTFN, encoding='test_decoder') f._CHUNK_SIZE = CHUNK_SIZE decoded = f.read() f.close() for i in range(min_pos, len(decoded) + 1): # seek positions for j in [1, 5, len(decoded) - i]: # read lengths - f = self.open(support.TESTFN, encoding='test_decoder') + f = self.open(os_helper.TESTFN, encoding='test_decoder') self.assertEqual(f.read(i), decoded[:i]) cookie = f.tell() self.assertEqual(f.read(j), decoded[i:i + j]) @@ -3062,11 +3065,11 @@ def test_seek_and_tell_with_data(data, min_pos=0): StatefulIncrementalDecoder.codecEnabled = 0 def test_multibyte_seek_and_tell(self): - f = self.open(support.TESTFN, "w", encoding="euc_jp") + f = self.open(os_helper.TESTFN, "w", encoding="euc_jp") f.write("AB\n\u3046\u3048\n") f.close() - f = self.open(support.TESTFN, "r", encoding="euc_jp") + f = self.open(os_helper.TESTFN, "r", encoding="euc_jp") self.assertEqual(f.readline(), "AB\n") p0 = f.tell() self.assertEqual(f.readline(), "\u3046\u3048\n") @@ -3077,7 +3080,7 @@ def test_multibyte_seek_and_tell(self): f.close() def test_seek_with_encoder_state(self): - f = self.open(support.TESTFN, "w", encoding="euc_jis_2004") + f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004") f.write("\u00e6\u0300") p0 = f.tell() f.write("\u00e6") @@ -3085,7 +3088,7 @@ def test_seek_with_encoder_state(self): f.write("\u0300") f.close() - f = self.open(support.TESTFN, "r", encoding="euc_jis_2004") + f = self.open(os_helper.TESTFN, "r", encoding="euc_jis_2004") self.assertEqual(f.readline(), "\u00e6\u0300\u0300") f.close() @@ -3229,7 +3232,7 @@ def test_issue2282(self): def test_append_bom(self): # The BOM is not written again when appending to a non-empty file - filename = support.TESTFN + filename = os_helper.TESTFN for charset in ('utf-8-sig', 'utf-16', 'utf-32'): with self.open(filename, 'w', encoding=charset) as f: f.write('aaa') @@ -3244,7 +3247,7 @@ def test_append_bom(self): def test_seek_bom(self): # Same test, but when seeking manually - filename = support.TESTFN + filename = os_helper.TESTFN for charset in ('utf-8-sig', 'utf-16', 'utf-32'): with self.open(filename, 'w', encoding=charset) as f: f.write('aaa') @@ -3259,7 +3262,7 @@ def test_seek_bom(self): def test_seek_append_bom(self): # Same test, but first seek to the start and then to the end - filename = support.TESTFN + filename = os_helper.TESTFN for charset in ('utf-8-sig', 'utf-16', 'utf-32'): with self.open(filename, 'w', encoding=charset) as f: f.write('aaa') @@ -3271,16 +3274,16 @@ def test_seek_append_bom(self): self.assertEqual(f.read(), 'aaaxxx'.encode(charset)) def test_errors_property(self): - with self.open(support.TESTFN, "w") as f: + with self.open(os_helper.TESTFN, "w") as f: self.assertEqual(f.errors, "strict") - with self.open(support.TESTFN, "w", errors="replace") as f: + with self.open(os_helper.TESTFN, "w", errors="replace") as f: self.assertEqual(f.errors, "replace") @support.no_tracing def test_threads_write(self): # Issue6750: concurrent writes could duplicate data event = threading.Event() - with self.open(support.TESTFN, "w", buffering=1) as f: + with self.open(os_helper.TESTFN, "w", buffering=1) as f: def run(n): text = "Thread%03d\n" % n event.wait() @@ -3289,7 +3292,7 @@ def run(n): for x in range(20)] with threading_helper.start_threads(threads, event.set): time.sleep(0.02) - with self.open(support.TESTFN) as f: + with self.open(os_helper.TESTFN) as f: content = f.read() for n in range(20): self.assertEqual(content.count("Thread%03d\n" % n), 1) @@ -3732,8 +3735,8 @@ def test_garbage_collection(self): # C TextIOWrapper objects are collected, and collecting them flushes # all data to disk. # The Python version has __del__, so it ends in gc.garbage instead. - with support.check_warnings(('', ResourceWarning)): - rawio = io.FileIO(support.TESTFN, "wb") + with warnings_helper.check_warnings(('', ResourceWarning)): + rawio = io.FileIO(os_helper.TESTFN, "wb") b = self.BufferedWriter(rawio) t = self.TextIOWrapper(b, encoding="ascii") t.write("456def") @@ -3742,7 +3745,7 @@ def test_garbage_collection(self): del t support.gc_collect() self.assertIsNone(wr(), wr) - with self.open(support.TESTFN, "rb") as f: + with self.open(os_helper.TESTFN, "rb") as f: self.assertEqual(f.read(), b"456def") def test_rwpair_cleared_before_textio(self): @@ -3899,7 +3902,7 @@ class PyIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest): class MiscIOTest(unittest.TestCase): def tearDown(self): - support.unlink(support.TESTFN) + os_helper.unlink(os_helper.TESTFN) def test___all__(self): for name in self.io.__all__: @@ -3913,21 +3916,21 @@ def test___all__(self): self.assertTrue(issubclass(obj, self.IOBase)) def test_attributes(self): - f = self.open(support.TESTFN, "wb", buffering=0) + f = self.open(os_helper.TESTFN, "wb", buffering=0) self.assertEqual(f.mode, "wb") f.close() - with support.check_warnings(('', DeprecationWarning)): - f = self.open(support.TESTFN, "U") - self.assertEqual(f.name, support.TESTFN) - self.assertEqual(f.buffer.name, support.TESTFN) - self.assertEqual(f.buffer.raw.name, support.TESTFN) + with warnings_helper.check_warnings(('', DeprecationWarning)): + f = self.open(os_helper.TESTFN, "U") + self.assertEqual(f.name, os_helper.TESTFN) + self.assertEqual(f.buffer.name, os_helper.TESTFN) + self.assertEqual(f.buffer.raw.name, os_helper.TESTFN) self.assertEqual(f.mode, "U") self.assertEqual(f.buffer.mode, "rb") self.assertEqual(f.buffer.raw.mode, "rb") f.close() - f = self.open(support.TESTFN, "w+") + f = self.open(os_helper.TESTFN, "w+") self.assertEqual(f.mode, "w+") self.assertEqual(f.buffer.mode, "rb+") # Does it really matter? self.assertEqual(f.buffer.raw.mode, "rb+") @@ -3969,7 +3972,7 @@ def test_io_after_close(self): {"mode": "w+", "buffering": 2}, {"mode": "w+b", "buffering": 0}, ]: - f = self.open(support.TESTFN, **kwargs) + f = self.open(os_helper.TESTFN, **kwargs) f.close() self.assertRaises(ValueError, f.flush) self.assertRaises(ValueError, f.fileno) @@ -4019,17 +4022,17 @@ def test_abcs(self): self.assertIsInstance(self.TextIOBase, abc.ABCMeta) def _check_abc_inheritance(self, abcmodule): - with self.open(support.TESTFN, "wb", buffering=0) as f: + with self.open(os_helper.TESTFN, "wb", buffering=0) as f: self.assertIsInstance(f, abcmodule.IOBase) self.assertIsInstance(f, abcmodule.RawIOBase) self.assertNotIsInstance(f, abcmodule.BufferedIOBase) self.assertNotIsInstance(f, abcmodule.TextIOBase) - with self.open(support.TESTFN, "wb") as f: + with self.open(os_helper.TESTFN, "wb") as f: self.assertIsInstance(f, abcmodule.IOBase) self.assertNotIsInstance(f, abcmodule.RawIOBase) self.assertIsInstance(f, abcmodule.BufferedIOBase) self.assertNotIsInstance(f, abcmodule.TextIOBase) - with self.open(support.TESTFN, "w") as f: + with self.open(os_helper.TESTFN, "w") as f: self.assertIsInstance(f, abcmodule.IOBase) self.assertNotIsInstance(f, abcmodule.RawIOBase) self.assertNotIsInstance(f, abcmodule.BufferedIOBase) @@ -4053,9 +4056,9 @@ def _check_warn_on_dealloc(self, *args, **kwargs): self.assertIn(r, str(cm.warning.args[0])) def test_warn_on_dealloc(self): - self._check_warn_on_dealloc(support.TESTFN, "wb", buffering=0) - self._check_warn_on_dealloc(support.TESTFN, "wb") - self._check_warn_on_dealloc(support.TESTFN, "w") + self._check_warn_on_dealloc(os_helper.TESTFN, "wb", buffering=0) + self._check_warn_on_dealloc(os_helper.TESTFN, "wb") + self._check_warn_on_dealloc(os_helper.TESTFN, "w") def _check_warn_on_dealloc_fd(self, *args, **kwargs): fds = [] @@ -4073,7 +4076,7 @@ def cleanup_fds(): # When using closefd=False, there's no warning r, w = os.pipe() fds += r, w - with support.check_no_resource_warning(self): + with warnings_helper.check_no_resource_warning(self): open(r, *args, closefd=False, **kwargs) def test_warn_on_dealloc_fd(self): @@ -4096,7 +4099,7 @@ def test_pickling(self): {"mode": "w+b", "buffering": 0}, ]: for protocol in range(pickle.HIGHEST_PROTOCOL + 1): - with self.open(support.TESTFN, **kwargs) as f: + with self.open(os_helper.TESTFN, **kwargs) as f: self.assertRaises(TypeError, pickle.dumps, f, protocol) def test_nonblock_pipe_write_bigbuf(self): @@ -4159,20 +4162,20 @@ def _test_nonblock_pipe_write(self, bufsize): def test_create_fail(self): # 'x' mode fails if file is existing - with self.open(support.TESTFN, 'w'): + with self.open(os_helper.TESTFN, 'w'): pass - self.assertRaises(FileExistsError, self.open, support.TESTFN, 'x') + self.assertRaises(FileExistsError, self.open, os_helper.TESTFN, 'x') def test_create_writes(self): # 'x' mode opens for writing - with self.open(support.TESTFN, 'xb') as f: + with self.open(os_helper.TESTFN, 'xb') as f: f.write(b"spam") - with self.open(support.TESTFN, 'rb') as f: + with self.open(os_helper.TESTFN, 'rb') as f: self.assertEqual(b"spam", f.read()) def test_open_allargs(self): # there used to be a buffer overflow in the parser for rawmode - self.assertRaises(ValueError, self.open, support.TESTFN, 'rwax+') + self.assertRaises(ValueError, self.open, os_helper.TESTFN, 'rwax+') def test_check_encoding_errors(self): # bpo-37388: open() and TextIOWrapper must check encoding and errors @@ -4427,7 +4430,7 @@ def check_interrupted_write_retry(self, item, **fdopen_kwargs): """Check that a buffered write, when it gets interrupted (either returning a partial result or EINTR), properly invokes the signal handler and retries if the latter returned successfully.""" - select = support.import_module("select") + select = import_helper.import_module("select") # A quantity that exceeds the buffer size of an anonymous pipe's # write end. diff --git a/Lib/test/test_json/__init__.py b/Lib/test/test_json/__init__.py index bac370dadfc830..74b64ed86a3183 100644 --- a/Lib/test/test_json/__init__.py +++ b/Lib/test/test_json/__init__.py @@ -4,10 +4,12 @@ import unittest from test import support +from test.support import import_helper + # import json with and without accelerations -cjson = support.import_fresh_module('json', fresh=['_json']) -pyjson = support.import_fresh_module('json', blocked=['_json']) +cjson = import_helper.import_fresh_module('json', fresh=['_json']) +pyjson = import_helper.import_fresh_module('json', blocked=['_json']) # JSONDecodeError is cached inside the _json module cjson.JSONDecodeError = cjson.decoder.JSONDecodeError = json.JSONDecodeError diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py index fc2a7a4fca3c5a..5411188acb6a2d 100644 --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -6,6 +6,7 @@ import subprocess from test import support +from test.support import os_helper from test.support.script_helper import assert_python_ok @@ -91,7 +92,7 @@ def test_stdin_stdout(self): self.assertEqual(process.stderr, '') def _create_infile(self, data=None): - infile = support.TESTFN + infile = os_helper.TESTFN with open(infile, "w", encoding="utf-8") as fp: self.addCleanup(os.remove, infile) fp.write(data or self.data) @@ -121,7 +122,7 @@ def test_non_ascii_infile(self): def test_infile_outfile(self): infile = self._create_infile() - outfile = support.TESTFN + '.out' + outfile = os_helper.TESTFN + '.out' rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) self.addCleanup(os.remove, outfile) with open(outfile, "r") as fp: @@ -189,7 +190,7 @@ def test_compact(self): def test_no_ensure_ascii_flag(self): infile = self._create_infile('{"key":"πŸ’©"}') - outfile = support.TESTFN + '.out' + outfile = os_helper.TESTFN + '.out' self.addCleanup(os.remove, outfile) assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile) with open(outfile, "rb") as f: @@ -200,7 +201,7 @@ def test_no_ensure_ascii_flag(self): def test_ensure_ascii_default(self): infile = self._create_infile('{"key":"πŸ’©"}') - outfile = support.TESTFN + '.out' + outfile = os_helper.TESTFN + '.out' self.addCleanup(os.remove, outfile) assert_python_ok('-m', 'json.tool', infile, outfile) with open(outfile, "rb") as f: diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 7ce7e565704f74..2bd46aa745ff23 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -1,5 +1,6 @@ import netrc, os, unittest, sys, tempfile, textwrap from test import support +from test.support import os_helper class NetrcTestCase(unittest.TestCase): @@ -108,16 +109,16 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self): def test_security(self): # This test is incomplete since we are normally not run as root and # therefore can't test the file ownership being wrong. - d = support.TESTFN + d = os_helper.TESTFN os.mkdir(d) - self.addCleanup(support.rmtree, d) + self.addCleanup(os_helper.rmtree, d) fn = os.path.join(d, '.netrc') with open(fn, 'wt') as f: f.write("""\ machine foo.domain.com login bar password pass default login foo password pass """) - with support.EnvironmentVarGuard() as environ: + with os_helper.EnvironmentVarGuard() as environ: environ.set('HOME', d) os.chmod(fn, 0o600) nrc = netrc.netrc() @@ -127,10 +128,10 @@ def test_security(self): self.assertRaises(netrc.NetrcParseError, netrc.netrc) def test_file_not_found_in_home(self): - d = support.TESTFN + d = os_helper.TESTFN os.mkdir(d) - self.addCleanup(support.rmtree, d) - with support.EnvironmentVarGuard() as environ: + self.addCleanup(os_helper.rmtree, d) + with os_helper.EnvironmentVarGuard() as environ: environ.set('HOME', d) self.assertRaises(FileNotFoundError, netrc.netrc) @@ -139,9 +140,9 @@ def test_file_not_found_explicit(self): file='unlikely_netrc') def test_home_not_set(self): - fake_home = support.TESTFN + fake_home = os_helper.TESTFN os.mkdir(fake_home) - self.addCleanup(support.rmtree, fake_home) + self.addCleanup(os_helper.rmtree, fake_home) fake_netrc_path = os.path.join(fake_home, '.netrc') with open(fake_netrc_path, 'w') as f: f.write('machine foo.domain.com login bar password pass') @@ -152,7 +153,7 @@ def test_home_not_set(self): def fake_expanduser(s): called.append(s) - with support.EnvironmentVarGuard() as environ: + with os_helper.EnvironmentVarGuard() as environ: environ.set('HOME', fake_home) environ.set('USERPROFILE', fake_home) result = orig_expanduser(s) diff --git a/Lib/test/test_nis.py b/Lib/test/test_nis.py index 21074c68106a3f..a22142f4069ba9 100644 --- a/Lib/test/test_nis.py +++ b/Lib/test/test_nis.py @@ -1,8 +1,10 @@ from test import support +from test.support import import_helper import unittest + # Skip test if nis module does not exist. -nis = support.import_module('nis') +nis = import_helper.import_module('nis') class NisTests(unittest.TestCase): diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index 2307b133dbd0d5..1f5cb103933e00 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -10,6 +10,7 @@ import unittest from test import support +from test.support import import_helper from test.pickletester import AbstractHookTests from test.pickletester import AbstractUnpickleTests @@ -499,7 +500,7 @@ def test_exceptions(self): ('builtins', name)) def test_multiprocessing_exceptions(self): - module = support.import_module('multiprocessing.context') + module = import_helper.import_module('multiprocessing.context') for name, exc in get_exceptions(module): with self.subTest(name): self.assertEqual(reverse_mapping('multiprocessing.context', name), diff --git a/Lib/test/test_sqlite.py b/Lib/test/test_sqlite.py index 9564da35193f1f..73002f228fa70b 100644 --- a/Lib/test/test_sqlite.py +++ b/Lib/test/test_sqlite.py @@ -1,7 +1,8 @@ import test.support +from test.support import import_helper # Skip test if _sqlite3 module not installed -test.support.import_module('_sqlite3') +import_helper.import_module('_sqlite3') import unittest import sqlite3 diff --git a/Lib/test/test_univnewlines.py b/Lib/test/test_univnewlines.py index fd07539fb60fde..b905491878001c 100644 --- a/Lib/test/test_univnewlines.py +++ b/Lib/test/test_univnewlines.py @@ -5,6 +5,8 @@ import os import sys from test import support +from test.support import os_helper + if not hasattr(sys.stdin, 'newlines'): raise unittest.SkipTest( @@ -46,29 +48,29 @@ def setUp(self): data = self.DATA if "b" in self.WRITEMODE: data = data.encode("ascii") - with self.open(support.TESTFN, self.WRITEMODE) as fp: + with self.open(os_helper.TESTFN, self.WRITEMODE) as fp: fp.write(data) def tearDown(self): try: - os.unlink(support.TESTFN) + os.unlink(os_helper.TESTFN) except: pass def test_read(self): - with self.open(support.TESTFN, self.READMODE) as fp: + with self.open(os_helper.TESTFN, self.READMODE) as fp: data = fp.read() self.assertEqual(data, DATA_LF) self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_readlines(self): - with self.open(support.TESTFN, self.READMODE) as fp: + with self.open(os_helper.TESTFN, self.READMODE) as fp: data = fp.readlines() self.assertEqual(data, DATA_SPLIT) self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_readline(self): - with self.open(support.TESTFN, self.READMODE) as fp: + with self.open(os_helper.TESTFN, self.READMODE) as fp: data = [] d = fp.readline() while d: @@ -78,7 +80,7 @@ def test_readline(self): self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) def test_seek(self): - with self.open(support.TESTFN, self.READMODE) as fp: + with self.open(os_helper.TESTFN, self.READMODE) as fp: fp.readline() pos = fp.tell() data = fp.readlines() @@ -105,7 +107,7 @@ class TestCRLFNewlines(TestGenericUnivNewlines): DATA = DATA_CRLF def test_tell(self): - with self.open(support.TESTFN, self.READMODE) as fp: + with self.open(os_helper.TESTFN, self.READMODE) as fp: self.assertEqual(repr(fp.newlines), repr(None)) data = fp.readline() pos = fp.tell() diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index b1c92427dd270b..718113d6e1bb2d 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import import_helper import builtins import contextlib import copy @@ -10,8 +11,8 @@ import weakref from unittest import mock -py_uuid = support.import_fresh_module('uuid', blocked=['_uuid']) -c_uuid = support.import_fresh_module('uuid', fresh=['_uuid']) +py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid']) +c_uuid = import_helper.import_fresh_module('uuid', fresh=['_uuid']) def importable(name): try: From a32eec09640c7fa44a067fca6ff6a6eb65199d98 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Mon, 6 Jul 2020 17:15:08 +0800 Subject: [PATCH 12/12] 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 | 1 + 20 files changed, 67 insertions(+), 48 deletions(-) diff --git a/Lib/ctypes/test/__init__.py b/Lib/ctypes/test/__init__.py index 26a70b76963499..6e496fa5a5201b 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 b99fdcba7b28fc..bfb6b42cbb2277 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 770e2c5592cc61..61b4b9162ccc54 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 bab152d8554563..590ffdea1122ac 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 68268992e9f98f..b0a12c97347490 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 571da973aab0ee..e02d1e16ae3da7 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 9ab68c67241f46..7e1092083269e4 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 cd642e7aaf8bb8..149767591d9eb5 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 0dc7dd8e254c31..35a62a0632e2e6 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 71a0511e53a72f..0c871afca37bdc 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 375d9c42137ba2..cfc6ba89e774c0 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 743bea7c14d0eb..e29cd4a84c5469 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 97981c882e825b..435b3e038aa394 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='