Skip to content

Commit 883bc63

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-21314)
1 parent 9ce8132 commit 883bc63

19 files changed

+225
-194
lines changed

‎Lib/test/pickletester.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
_testbuffer = None
2323

2424
from test import support
25+
from test.support import os_helper
2526
from test.support import (
26-
TestFailed, TESTFN, run_with_locale, no_tracing,
27-
_2G, _4G, bigmemtest, forget,
27+
TestFailed, run_with_locale, no_tracing,
28+
_2G, _4G, bigmemtest
2829
)
30+
from test.support.import_helper import forget
31+
from test.support.os_helper import TESTFN
2932
from test.support import threading_helper
3033
from test.support.warnings_helper import save_restore_warnings_filters
3134

@@ -3100,15 +3103,15 @@ def test_dump_closed_file(self):
31003103
f.close()
31013104
self.assertRaises(ValueError, self.dump, 123, f)
31023105
finally:
3103-
support.unlink(TESTFN)
3106+
os_helper.unlink(TESTFN)
31043107

31053108
def test_load_closed_file(self):
31063109
f = open(TESTFN, "wb")
31073110
try:
31083111
f.close()
31093112
self.assertRaises(ValueError, self.dump, 123, f)
31103113
finally:
3111-
support.unlink(TESTFN)
3114+
os_helper.unlink(TESTFN)
31123115

31133116
def test_load_from_and_dump_to_file(self):
31143117
stream = io.BytesIO()
@@ -3139,7 +3142,7 @@ def test_dump_text_file(self):
31393142
self.assertRaises(TypeError, self.dump, 123, f, proto)
31403143
finally:
31413144
f.close()
3142-
support.unlink(TESTFN)
3145+
os_helper.unlink(TESTFN)
31433146

31443147
def test_incomplete_input(self):
31453148
s = io.BytesIO(b"X''.")

‎Lib/test/test_binhex.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
"""
66
import unittest
77
from test import support
8+
from test.support import import_helper
9+
from test.support import os_helper
10+
from test.support import warnings_helper
811

9-
with support.check_warnings(('', DeprecationWarning)):
10-
binhex = support.import_fresh_module('binhex')
12+
13+
with warnings_helper.check_warnings(('', DeprecationWarning)):
14+
binhex = import_helper.import_fresh_module('binhex')
1115

1216

1317
class BinHexTestCase(unittest.TestCase):
1418

1519
def setUp(self):
1620
# binhex supports only file names encodable to Latin1
17-
self.fname1 = support.TESTFN_ASCII + "1"
18-
self.fname2 = support.TESTFN_ASCII + "2"
19-
self.fname3 = support.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
21+
self.fname1 = os_helper.TESTFN_ASCII + "1"
22+
self.fname2 = os_helper.TESTFN_ASCII + "2"
23+
self.fname3 = os_helper.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
2024

2125
def tearDown(self):
22-
support.unlink(self.fname1)
23-
support.unlink(self.fname2)
24-
support.unlink(self.fname3)
26+
os_helper.unlink(self.fname1)
27+
os_helper.unlink(self.fname2)
28+
os_helper.unlink(self.fname3)
2529

2630
DATA = b'Jack is my hero'
2731

‎Lib/test/test_bufio.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from test import support
3+
from test.support import os_helper
34

45
import io # C implementation.
56
import _pyio as pyio # Python implementation.
@@ -17,18 +18,18 @@ def try_one(self, s):
1718
# .readline()s deliver what we wrote.
1819

1920
# Ensure we can open TESTFN for writing.
20-
support.unlink(support.TESTFN)
21+
os_helper.unlink(os_helper.TESTFN)
2122

2223
# Since C doesn't guarantee we can write/read arbitrary bytes in text
2324
# files, use binary mode.
24-
f = self.open(support.TESTFN, "wb")
25+
f = self.open(os_helper.TESTFN, "wb")
2526
try:
2627
# write once with \n and once without
2728
f.write(s)
2829
f.write(b"\n")
2930
f.write(s)
3031
f.close()
31-
f = open(support.TESTFN, "rb")
32+
f = open(os_helper.TESTFN, "rb")
3233
line = f.readline()
3334
self.assertEqual(line, s + b"\n")
3435
line = f.readline()
@@ -37,7 +38,7 @@ def try_one(self, s):
3738
self.assertFalse(line) # Must be at EOF
3839
f.close()
3940
finally:
40-
support.unlink(support.TESTFN)
41+
os_helper.unlink(os_helper.TESTFN)
4142

4243
def drive_one(self, pattern):
4344
for length in lengths:

‎Lib/test/test_capi.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import importlib.util
1818
from test import support
1919
from test.support import MISSING_C_DOCSTRINGS
20+
from test.support import import_helper
2021
from test.support import threading_helper
2122
from test.support.script_helper import assert_python_failure, assert_python_ok
2223
try:
@@ -25,7 +26,7 @@
2526
_posixsubprocess = None
2627

2728
# Skip this test if the _testcapi module isn't available.
28-
_testcapi = support.import_module('_testcapi')
29+
_testcapi = import_helper.import_module('_testcapi')
2930

3031
import _testinternalcapi
3132

‎Lib/test/test_compileall.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
_have_multiprocessing = False
2424

2525
from test import support
26+
from test.support import os_helper
2627
from test.support import script_helper
2728

2829
from .test_py_compile import without_source_date_epoch
@@ -356,7 +357,7 @@ def test_multiple_optimization_levels(self):
356357
except Exception:
357358
pass
358359

359-
@support.skip_unless_symlink
360+
@os_helper.skip_unless_symlink
360361
def test_ignore_symlink_destination(self):
361362
# Create folders for allowed files, symlinks and prohibited area
362363
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
@@ -438,7 +439,7 @@ def setUpClass(cls):
438439
sys_path_writable = False
439440
break
440441
finally:
441-
support.unlink(str(path))
442+
os_helper.unlink(str(path))
442443
if directory_created:
443444
directory.rmdir()
444445
else:
@@ -477,7 +478,7 @@ def assertNotCompiled(self, fn):
477478

478479
def setUp(self):
479480
self.directory = tempfile.mkdtemp()
480-
self.addCleanup(support.rmtree, self.directory)
481+
self.addCleanup(os_helper.rmtree, self.directory)
481482
self.pkgdir = os.path.join(self.directory, 'foo')
482483
os.mkdir(self.pkgdir)
483484
self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
@@ -625,7 +626,7 @@ def test_recursion_limit(self):
625626
self.assertCompiled(spamfn)
626627
self.assertCompiled(eggfn)
627628

628-
@support.skip_unless_symlink
629+
@os_helper.skip_unless_symlink
629630
def test_symlink_loop(self):
630631
# Currently, compileall ignores symlinks to directories.
631632
# If that limitation is ever lifted, it should protect against
@@ -823,7 +824,7 @@ def test_multiple_optimization_levels(self):
823824
except Exception:
824825
pass
825826

826-
@support.skip_unless_symlink
827+
@os_helper.skip_unless_symlink
827828
def test_ignore_symlink_destination(self):
828829
# Create folders for allowed files, symlinks and prohibited area
829830
allowed_path = os.path.join(self.directory, "test", "dir", "allowed")

‎Lib/test/test_fileio.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from weakref import proxy
1010
from functools import wraps
1111

12-
from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest,
13-
make_bad_fd, cpython_only, swap_attr)
12+
from test.support import (run_unittest, cpython_only, swap_attr)
13+
from test.support.os_helper import (TESTFN, TESTFN_UNICODE, make_bad_fd)
14+
from test.support.warnings_helper import check_warnings
1415
from collections import UserList
1516

1617
import _io # C implementation of io

‎Lib/test/test_heapq.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import doctest
66

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

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

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

‎Lib/test/test_httplib.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
TestCase = unittest.TestCase
1414

1515
from test import support
16+
from test.support import os_helper
1617
from test.support import socket_helper
18+
from test.support import warnings_helper
19+
1720

1821
here = os.path.dirname(__file__)
1922
# Self-signed cert file for 'localhost'
@@ -1766,14 +1769,14 @@ def test_local_bad_hostname(self):
17661769
with self.assertRaises(ssl.CertificateError):
17671770
h.request('GET', '/')
17681771
# Same with explicit check_hostname=True
1769-
with support.check_warnings(('', DeprecationWarning)):
1772+
with warnings_helper.check_warnings(('', DeprecationWarning)):
17701773
h = client.HTTPSConnection('localhost', server.port,
17711774
context=context, check_hostname=True)
17721775
with self.assertRaises(ssl.CertificateError):
17731776
h.request('GET', '/')
17741777
# With check_hostname=False, the mismatching is ignored
17751778
context.check_hostname = False
1776-
with support.check_warnings(('', DeprecationWarning)):
1779+
with warnings_helper.check_warnings(('', DeprecationWarning)):
17771780
h = client.HTTPSConnection('localhost', server.port,
17781781
context=context, check_hostname=False)
17791782
h.request('GET', '/nonexistent')
@@ -1792,7 +1795,7 @@ def test_local_bad_hostname(self):
17921795
h.close()
17931796
# Passing check_hostname to HTTPSConnection should override the
17941797
# context's setting.
1795-
with support.check_warnings(('', DeprecationWarning)):
1798+
with warnings_helper.check_warnings(('', DeprecationWarning)):
17961799
h = client.HTTPSConnection('localhost', server.port,
17971800
context=context, check_hostname=True)
17981801
with self.assertRaises(ssl.CertificateError):
@@ -1908,10 +1911,10 @@ def test_bytes_body(self):
19081911
self.assertEqual(b'body\xc1', f.read())
19091912

19101913
def test_text_file_body(self):
1911-
self.addCleanup(support.unlink, support.TESTFN)
1912-
with open(support.TESTFN, "w") as f:
1914+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
1915+
with open(os_helper.TESTFN, "w") as f:
19131916
f.write("body")
1914-
with open(support.TESTFN) as f:
1917+
with open(os_helper.TESTFN) as f:
19151918
self.conn.request("PUT", "/url", f)
19161919
message, f = self.get_headers_and_fp()
19171920
self.assertEqual("text/plain", message.get_content_type())
@@ -1923,10 +1926,10 @@ def test_text_file_body(self):
19231926
self.assertEqual(b'4\r\nbody\r\n0\r\n\r\n', f.read())
19241927

19251928
def test_binary_file_body(self):
1926-
self.addCleanup(support.unlink, support.TESTFN)
1927-
with open(support.TESTFN, "wb") as f:
1929+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
1930+
with open(os_helper.TESTFN, "wb") as f:
19281931
f.write(b"body\xc1")
1929-
with open(support.TESTFN, "rb") as f:
1932+
with open(os_helper.TESTFN, "rb") as f:
19301933
self.conn.request("PUT", "/url", f)
19311934
message, f = self.get_headers_and_fp()
19321935
self.assertEqual("text/plain", message.get_content_type())

‎Lib/test/test_importlib/util.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os.path
1313
from pathlib import Path, PurePath
1414
from test import support
15+
from test.support import import_helper
1516
import unittest
1617
import sys
1718
import tempfile
@@ -55,8 +56,8 @@ def _extension_details():
5556
def import_importlib(module_name):
5657
"""Import a module from importlib both w/ and w/o _frozen_importlib."""
5758
fresh = ('importlib',) if '.' in module_name else ()
58-
frozen = support.import_fresh_module(module_name)
59-
source = support.import_fresh_module(module_name, fresh=fresh,
59+
frozen = import_helper.import_fresh_module(module_name)
60+
source = import_helper.import_fresh_module(module_name, fresh=fresh,
6061
blocked=('_frozen_importlib', '_frozen_importlib_external'))
6162
return {'Frozen': frozen, 'Source': source}
6263

0 commit comments

Comments
 (0)