Skip to content

Commit f7ba40b

Browse files
authored
bpo-40275: Use new test.support helper submodules in tests (GH-20849)
1 parent 5f190d2 commit f7ba40b

17 files changed

+78
-60
lines changed

Lib/test/libregrtest/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from test.libregrtest.pgo import setup_pgo_tests
2121
from test.libregrtest.utils import removepy, count, format_duration, printlist
2222
from test import support
23+
from test.support import os_helper
2324

2425

2526
# bpo-38203: Maximum delay in seconds to exit Python (call Py_Finalize()).
@@ -628,7 +629,7 @@ def main(self, tests=None, **kwargs):
628629
# to a temporary and writable directory. If it's not possible to
629630
# create or change the CWD, the original CWD will be used.
630631
# The original CWD is available from support.SAVEDCWD.
631-
with support.temp_cwd(test_cwd, quiet=True):
632+
with os_helper.temp_cwd(test_cwd, quiet=True):
632633
# When using multiprocessing, worker processes will use test_cwd
633634
# as their parent temporary directory. So when the main process
634635
# exit, it removes also subdirectories of worker processes.

Lib/test/libregrtest/runtest.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import unittest
1212

1313
from test import support
14+
from test.support import import_helper
15+
from test.support import os_helper
1416
from test.libregrtest.refleak import dash_R, clear_caches
1517
from test.libregrtest.save_env import saved_test_environment
1618
from test.libregrtest.utils import format_duration, print_warning
@@ -216,7 +218,7 @@ def _runtest_inner2(ns, test_name):
216218
abstest = get_abs_module(ns, test_name)
217219

218220
# remove the module from sys.module to reload it if it was already imported
219-
support.unload(abstest)
221+
import_helper.unload(abstest)
220222

221223
the_module = importlib.import_module(abstest)
222224

@@ -313,7 +315,7 @@ def cleanup_test_droppings(test_name, verbose):
313315
# since if a test leaves a file open, it cannot be deleted by name (while
314316
# there's nothing we can do about that here either, we can display the
315317
# name of the offending test, which is a real help).
316-
for name in (support.TESTFN,):
318+
for name in (os_helper.TESTFN,):
317319
if not os.path.exists(name):
318320
continue
319321

Lib/test/libregrtest/save_env.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import urllib.request
1111
import warnings
1212
from test import support
13+
from test.support import os_helper
1314
from test.libregrtest.utils import print_warning
1415
try:
1516
import _multiprocessing, multiprocessing.process
@@ -241,7 +242,7 @@ def get_files(self):
241242
return sorted(fn + ('/' if os.path.isdir(fn) else '')
242243
for fn in os.listdir())
243244
def restore_files(self, saved_value):
244-
fn = support.TESTFN
245+
fn = os_helper.TESTFN
245246
if fn not in saved_value and (fn + '/') not in saved_value:
246247
if os.path.isfile(fn):
247248
support.unlink(fn)

Lib/test/support/script_helper.py

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

1313
from importlib.util import source_from_cache
14-
from test.support import make_legacy_pyc
14+
from test.support.import_helper import make_legacy_pyc
1515

1616

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

Lib/test/test__xxsubinterpreters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import unittest
1111

1212
from test import support
13+
from test.support import import_helper
1314
from test.support import script_helper
1415

1516

16-
interpreters = support.import_module('_xxsubinterpreters')
17+
interpreters = import_helper.import_module('_xxsubinterpreters')
1718

1819

1920
##################################

Lib/test/test_array.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import unittest
66
from test import support
7+
from test.support import os_helper
78
from test.support import _2G
89
import weakref
910
import pickle
@@ -366,13 +367,13 @@ def test_insert(self):
366367
def test_tofromfile(self):
367368
a = array.array(self.typecode, 2*self.example)
368369
self.assertRaises(TypeError, a.tofile)
369-
support.unlink(support.TESTFN)
370-
f = open(support.TESTFN, 'wb')
370+
os_helper.unlink(os_helper.TESTFN)
371+
f = open(os_helper.TESTFN, 'wb')
371372
try:
372373
a.tofile(f)
373374
f.close()
374375
b = array.array(self.typecode)
375-
f = open(support.TESTFN, 'rb')
376+
f = open(os_helper.TESTFN, 'rb')
376377
self.assertRaises(TypeError, b.fromfile)
377378
b.fromfile(f, len(self.example))
378379
self.assertEqual(b, array.array(self.typecode, self.example))
@@ -383,27 +384,27 @@ def test_tofromfile(self):
383384
finally:
384385
if not f.closed:
385386
f.close()
386-
support.unlink(support.TESTFN)
387+
os_helper.unlink(os_helper.TESTFN)
387388

388389
def test_fromfile_ioerror(self):
389390
# Issue #5395: Check if fromfile raises a proper OSError
390391
# instead of EOFError.
391392
a = array.array(self.typecode)
392-
f = open(support.TESTFN, 'wb')
393+
f = open(os_helper.TESTFN, 'wb')
393394
try:
394395
self.assertRaises(OSError, a.fromfile, f, len(self.example))
395396
finally:
396397
f.close()
397-
support.unlink(support.TESTFN)
398+
os_helper.unlink(os_helper.TESTFN)
398399

399400
def test_filewrite(self):
400401
a = array.array(self.typecode, 2*self.example)
401-
f = open(support.TESTFN, 'wb')
402+
f = open(os_helper.TESTFN, 'wb')
402403
try:
403404
f.write(a)
404405
f.close()
405406
b = array.array(self.typecode)
406-
f = open(support.TESTFN, 'rb')
407+
f = open(os_helper.TESTFN, 'rb')
407408
b.fromfile(f, len(self.example))
408409
self.assertEqual(b, array.array(self.typecode, self.example))
409410
self.assertNotEqual(a, b)
@@ -413,7 +414,7 @@ def test_filewrite(self):
413414
finally:
414415
if not f.closed:
415416
f.close()
416-
support.unlink(support.TESTFN)
417+
os_helper.unlink(os_helper.TESTFN)
417418

418419
def test_tofromlist(self):
419420
a = array.array(self.typecode, 2*self.example)

Lib/test/test_cmd_line.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import textwrap
1010
import unittest
1111
from test import support
12+
from test.support import os_helper
1213
from test.support.script_helper import (
1314
spawn_python, kill_python, assert_python_ok, assert_python_failure,
1415
interpreter_requires_environment
@@ -141,11 +142,11 @@ def test_run_code(self):
141142
# All good if execution is successful
142143
assert_python_ok('-c', 'pass')
143144

144-
@unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
145+
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
145146
def test_non_ascii(self):
146147
# Test handling of non-ascii data
147148
command = ("assert(ord(%r) == %s)"
148-
% (support.FS_NONASCII, ord(support.FS_NONASCII)))
149+
% (os_helper.FS_NONASCII, ord(os_helper.FS_NONASCII)))
149150
assert_python_ok('-c', command)
150151

151152
# On Windows, pass bytes to subprocess doesn't test how Python decodes the
@@ -463,8 +464,8 @@ def test_del___main__(self):
463464
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
464465
# borrowed reference to the dict of __main__ module and later modify
465466
# the dict whereas the module was destroyed
466-
filename = support.TESTFN
467-
self.addCleanup(support.unlink, filename)
467+
filename = os_helper.TESTFN
468+
self.addCleanup(os_helper.unlink, filename)
468469
with open(filename, "w") as script:
469470
print("import sys", file=script)
470471
print("del sys.modules['__main__']", file=script)
@@ -499,7 +500,7 @@ def test_isolatedmode(self):
499500
# dummyvar to prevent extraneous -E
500501
dummyvar="")
501502
self.assertEqual(out.strip(), b'1 1 1')
502-
with support.temp_cwd() as tmpdir:
503+
with os_helper.temp_cwd() as tmpdir:
503504
fake = os.path.join(tmpdir, "uuid.py")
504505
main = os.path.join(tmpdir, "main.py")
505506
with open(fake, "w") as f:
@@ -561,7 +562,7 @@ def test_set_pycache_prefix(self):
561562
elif opt is not None:
562563
args[:0] = ['-X', f'pycache_prefix={opt}']
563564
with self.subTest(envval=envval, opt=opt):
564-
with support.temp_cwd():
565+
with os_helper.temp_cwd():
565566
assert_python_ok(*args, **env)
566567

567568
def run_xdev(self, *args, check_exitcode=True, xdev=True):
@@ -644,7 +645,8 @@ def test_xdev(self):
644645

645646
def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False):
646647
if use_pywarning:
647-
code = ("import sys; from test.support import import_fresh_module; "
648+
code = ("import sys; from test.support.import_helper import "
649+
"import_fresh_module; "
648650
"warnings = import_fresh_module('warnings', blocked=['_warnings']); ")
649651
else:
650652
code = "import sys, warnings; "

Lib/test/test_dbm_dumb.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import unittest
1111
import dbm.dumb as dumbdbm
1212
from test import support
13+
from test.support import os_helper
1314
from functools import partial
1415

15-
_fname = support.TESTFN
16+
_fname = os_helper.TESTFN
17+
1618

1719
def _delete_files():
1820
for ext in [".dir", ".dat", ".bak"]:
@@ -264,7 +266,7 @@ def test_invalid_flag(self):
264266
dumbdbm.open(_fname, flag)
265267

266268
def test_readonly_files(self):
267-
with support.temp_dir() as dir:
269+
with os_helper.temp_dir() as dir:
268270
fname = os.path.join(dir, 'db')
269271
with dumbdbm.open(fname, 'n') as f:
270272
self.assertEqual(list(f.keys()), [])
@@ -277,12 +279,12 @@ def test_readonly_files(self):
277279
self.assertEqual(sorted(f.keys()), sorted(self._dict))
278280
f.close() # don't write
279281

280-
@unittest.skipUnless(support.TESTFN_NONASCII,
282+
@unittest.skipUnless(os_helper.TESTFN_NONASCII,
281283
'requires OS support of non-ASCII encodings')
282284
def test_nonascii_filename(self):
283-
filename = support.TESTFN_NONASCII
285+
filename = os_helper.TESTFN_NONASCII
284286
for suffix in ['.dir', '.dat', '.bak']:
285-
self.addCleanup(support.unlink, filename + suffix)
287+
self.addCleanup(os_helper.unlink, filename + suffix)
286288
with dumbdbm.open(filename, 'c') as db:
287289
db[b'key'] = b'value'
288290
self.assertTrue(os.path.exists(filename + '.dat'))

Lib/test/test_decimal.py

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

Lib/test/test_global.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Verify that warnings are issued for global statements following use."""
22

3-
from test.support import run_unittest, check_syntax_error, check_warnings
3+
from test.support import run_unittest, check_syntax_error
4+
from test.support.warnings_helper import check_warnings
45
import unittest
56
import warnings
67

Lib/test/test_imp.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import py_compile
66
import sys
77
from test import support
8+
from test.support import import_helper
9+
from test.support import os_helper
810
from test.support import script_helper
911
import unittest
1012
import warnings
@@ -107,8 +109,8 @@ def test_issue3594(self):
107109
self.assertEqual(file.encoding, 'cp1252')
108110
finally:
109111
del sys.path[0]
110-
support.unlink(temp_mod_name + '.py')
111-
support.unlink(temp_mod_name + '.pyc')
112+
os_helper.unlink(temp_mod_name + '.py')
113+
os_helper.unlink(temp_mod_name + '.pyc')
112114

113115
def test_issue5604(self):
114116
# Test cannot cover imp.load_compiled function.
@@ -192,10 +194,10 @@ def test_issue5604(self):
192194
finally:
193195
del sys.path[0]
194196
for ext in ('.py', '.pyc'):
195-
support.unlink(temp_mod_name + ext)
196-
support.unlink(init_file_name + ext)
197-
support.rmtree(test_package_name)
198-
support.rmtree('__pycache__')
197+
os_helper.unlink(temp_mod_name + ext)
198+
os_helper.unlink(init_file_name + ext)
199+
os_helper.rmtree(test_package_name)
200+
os_helper.rmtree('__pycache__')
199201

200202
def test_issue9319(self):
201203
path = os.path.dirname(__file__)
@@ -204,7 +206,7 @@ def test_issue9319(self):
204206

205207
def test_load_from_source(self):
206208
# Verify that the imp module can correctly load and find .py files
207-
# XXX (ncoghlan): It would be nice to use support.CleanImport
209+
# XXX (ncoghlan): It would be nice to use import_helper.CleanImport
208210
# here, but that breaks because the os module registers some
209211
# handlers in copy_reg on import. Since CleanImport doesn't
210212
# revert that registration, the module is left in a broken
@@ -213,7 +215,7 @@ def test_load_from_source(self):
213215
# workaround
214216
orig_path = os.path
215217
orig_getenv = os.getenv
216-
with support.EnvironmentVarGuard():
218+
with os_helper.EnvironmentVarGuard():
217219
x = imp.find_module("os")
218220
self.addCleanup(x[0].close)
219221
new_os = imp.load_module("os", *x)
@@ -299,11 +301,11 @@ def test_issue24748_load_module_skips_sys_modules_check(self):
299301
@unittest.skipIf(sys.dont_write_bytecode,
300302
"test meaningful only when writing bytecode")
301303
def test_bug7732(self):
302-
with support.temp_cwd():
303-
source = support.TESTFN + '.py'
304+
with os_helper.temp_cwd():
305+
source = os_helper.TESTFN + '.py'
304306
os.mkdir(source)
305307
self.assertRaisesRegex(ImportError, '^No module',
306-
imp.find_module, support.TESTFN, ["."])
308+
imp.find_module, os_helper.TESTFN, ["."])
307309

308310
def test_multiple_calls_to_get_data(self):
309311
# Issue #18755: make sure multiple calls to get_data() can succeed.
@@ -364,7 +366,7 @@ def test_pyc_invalidation_mode_from_cmdline(self):
364366

365367
def test_find_and_load_checked_pyc(self):
366368
# issue 34056
367-
with support.temp_cwd():
369+
with os_helper.temp_cwd():
368370
with open('mymod.py', 'wb') as fp:
369371
fp.write(b'x = 42\n')
370372
py_compile.compile(
@@ -383,24 +385,24 @@ class ReloadTests(unittest.TestCase):
383385
reload()."""
384386

385387
def test_source(self):
386-
# XXX (ncoghlan): It would be nice to use test.support.CleanImport
388+
# XXX (ncoghlan): It would be nice to use test.import_helper.CleanImport
387389
# here, but that breaks because the os module registers some
388390
# handlers in copy_reg on import. Since CleanImport doesn't
389391
# revert that registration, the module is left in a broken
390392
# state after reversion. Reinitialising the module contents
391393
# and just reverting os.environ to its previous state is an OK
392394
# workaround
393-
with support.EnvironmentVarGuard():
395+
with os_helper.EnvironmentVarGuard():
394396
import os
395397
imp.reload(os)
396398

397399
def test_extension(self):
398-
with support.CleanImport('time'):
400+
with import_helper.CleanImport('time'):
399401
import time
400402
imp.reload(time)
401403

402404
def test_builtin(self):
403-
with support.CleanImport('marshal'):
405+
with import_helper.CleanImport('marshal'):
404406
import marshal
405407
imp.reload(marshal)
406408

@@ -443,10 +445,10 @@ def test_source_from_cache(self):
443445

444446

445447
class NullImporterTests(unittest.TestCase):
446-
@unittest.skipIf(support.TESTFN_UNENCODABLE is None,
448+
@unittest.skipIf(os_helper.TESTFN_UNENCODABLE is None,
447449
"Need an undecodeable filename")
448450
def test_unencodeable(self):
449-
name = support.TESTFN_UNENCODABLE
451+
name = os_helper.TESTFN_UNENCODABLE
450452
os.mkdir(name)
451453
try:
452454
self.assertRaises(ImportError, imp.NullImporter, name)

Lib/test/test_ioctl.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import array
22
import unittest
3-
from test.support import import_module, get_attribute
3+
from test.support import get_attribute
4+
from test.support.import_helper import import_module
45
import os, struct
56
fcntl = import_module('fcntl')
67
termios = import_module('termios')

0 commit comments

Comments
 (0)