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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 10 additions & 9 deletions
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

Lines changed: 9 additions & 7 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)