Skip to content

Commit d2fc805

Browse files
committed
Fix DeprecationWarning on Windows
Issue #25911: Use support.check_warnings() to expect or ignore DeprecationWarning in test_os.
1 parent abf7f0c commit d2fc805

File tree

1 file changed

+67
-49
lines changed

1 file changed

+67
-49
lines changed

Lib/test/test_os.py

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
from test.support.script_helper import assert_python_ok
6868

69+
6970
root_in_posix = False
7071
if hasattr(os, 'geteuid'):
7172
root_in_posix = (os.geteuid() == 0)
@@ -82,6 +83,23 @@
8283
# Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
8384
HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
8485

86+
87+
@contextlib.contextmanager
88+
def ignore_deprecation_warnings(msg_regex, quiet=False):
89+
with support.check_warnings((msg_regex, DeprecationWarning), quiet=quiet):
90+
yield
91+
92+
93+
@contextlib.contextmanager
94+
def bytes_filename_warn(expected):
95+
msg = 'The Windows bytes API has been deprecated'
96+
if os.name == 'nt':
97+
with ignore_deprecation_warnings(msg, quiet=not expected):
98+
yield
99+
else:
100+
yield
101+
102+
85103
# Tests creating TESTFN
86104
class FileTests(unittest.TestCase):
87105
def setUp(self):
@@ -305,8 +323,7 @@ def test_stat_attributes_bytes(self):
305323
fname = self.fname.encode(sys.getfilesystemencoding())
306324
except UnicodeEncodeError:
307325
self.skipTest("cannot encode %a for the filesystem" % self.fname)
308-
with warnings.catch_warnings():
309-
warnings.simplefilter("ignore", DeprecationWarning)
326+
with bytes_filename_warn(True):
310327
self.check_stat_attributes(fname)
311328

312329
def test_stat_result_pickle(self):
@@ -443,15 +460,11 @@ def setUp(self):
443460
fp.write(b"ABC")
444461

445462
def restore_float_times(state):
446-
with warnings.catch_warnings():
447-
warnings.simplefilter("ignore", DeprecationWarning)
448-
463+
with ignore_deprecation_warnings('stat_float_times'):
449464
os.stat_float_times(state)
450465

451466
# ensure that st_atime and st_mtime are float
452-
with warnings.catch_warnings():
453-
warnings.simplefilter("ignore", DeprecationWarning)
454-
467+
with ignore_deprecation_warnings('stat_float_times'):
455468
old_float_times = os.stat_float_times(-1)
456469
self.addCleanup(restore_float_times, old_float_times)
457470

@@ -1024,8 +1037,7 @@ def setUp(self):
10241037
super().setUp()
10251038
self.stack = contextlib.ExitStack()
10261039
if os.name == 'nt':
1027-
self.stack.enter_context(warnings.catch_warnings())
1028-
warnings.simplefilter("ignore", DeprecationWarning)
1040+
self.stack.enter_context(bytes_filename_warn(False))
10291041

10301042
def tearDown(self):
10311043
self.stack.close()
@@ -1580,8 +1592,7 @@ def _test_link(self, file1, file2):
15801592
with open(file1, "w") as f1:
15811593
f1.write("test")
15821594

1583-
with warnings.catch_warnings():
1584-
warnings.simplefilter("ignore", DeprecationWarning)
1595+
with bytes_filename_warn(False):
15851596
os.link(file1, file2)
15861597
with open(file1, "r") as f1, open(file2, "r") as f2:
15871598
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
@@ -1873,10 +1884,12 @@ def test_listdir_no_extended_path(self):
18731884
self.assertEqual(
18741885
sorted(os.listdir(support.TESTFN)),
18751886
self.created_paths)
1887+
18761888
# bytes
1877-
self.assertEqual(
1878-
sorted(os.listdir(os.fsencode(support.TESTFN))),
1879-
[os.fsencode(path) for path in self.created_paths])
1889+
with bytes_filename_warn(False):
1890+
self.assertEqual(
1891+
sorted(os.listdir(os.fsencode(support.TESTFN))),
1892+
[os.fsencode(path) for path in self.created_paths])
18801893

18811894
def test_listdir_extended_path(self):
18821895
"""Test when the path starts with '\\\\?\\'."""
@@ -1886,11 +1899,13 @@ def test_listdir_extended_path(self):
18861899
self.assertEqual(
18871900
sorted(os.listdir(path)),
18881901
self.created_paths)
1902+
18891903
# bytes
1890-
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
1891-
self.assertEqual(
1892-
sorted(os.listdir(path)),
1893-
[os.fsencode(path) for path in self.created_paths])
1904+
with bytes_filename_warn(False):
1905+
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
1906+
self.assertEqual(
1907+
sorted(os.listdir(path)),
1908+
[os.fsencode(path) for path in self.created_paths])
18941909

18951910

18961911
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
@@ -1965,9 +1980,9 @@ def check_stat(self, link, target):
19651980
self.assertNotEqual(os.lstat(link), os.stat(link))
19661981

19671982
bytes_link = os.fsencode(link)
1968-
with warnings.catch_warnings():
1969-
warnings.simplefilter("ignore", DeprecationWarning)
1983+
with bytes_filename_warn(True):
19701984
self.assertEqual(os.stat(bytes_link), os.stat(target))
1985+
with bytes_filename_warn(True):
19711986
self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
19721987

19731988
def test_12084(self):
@@ -2529,36 +2544,37 @@ class Win32DeprecatedBytesAPI(unittest.TestCase):
25292544
def test_deprecated(self):
25302545
import nt
25312546
filename = os.fsencode(support.TESTFN)
2532-
with warnings.catch_warnings():
2533-
warnings.simplefilter("error", DeprecationWarning)
2534-
for func, *args in (
2535-
(nt._getfullpathname, filename),
2536-
(nt._isdir, filename),
2537-
(os.access, filename, os.R_OK),
2538-
(os.chdir, filename),
2539-
(os.chmod, filename, 0o777),
2540-
(os.getcwdb,),
2541-
(os.link, filename, filename),
2542-
(os.listdir, filename),
2543-
(os.lstat, filename),
2544-
(os.mkdir, filename),
2545-
(os.open, filename, os.O_RDONLY),
2546-
(os.rename, filename, filename),
2547-
(os.rmdir, filename),
2548-
(os.startfile, filename),
2549-
(os.stat, filename),
2550-
(os.unlink, filename),
2551-
(os.utime, filename),
2552-
):
2553-
self.assertRaises(DeprecationWarning, func, *args)
2547+
for func, *args in (
2548+
(nt._getfullpathname, filename),
2549+
(nt._isdir, filename),
2550+
(os.access, filename, os.R_OK),
2551+
(os.chdir, filename),
2552+
(os.chmod, filename, 0o777),
2553+
(os.getcwdb,),
2554+
(os.link, filename, filename),
2555+
(os.listdir, filename),
2556+
(os.lstat, filename),
2557+
(os.mkdir, filename),
2558+
(os.open, filename, os.O_RDONLY),
2559+
(os.rename, filename, filename),
2560+
(os.rmdir, filename),
2561+
(os.startfile, filename),
2562+
(os.stat, filename),
2563+
(os.unlink, filename),
2564+
(os.utime, filename),
2565+
):
2566+
with bytes_filename_warn(True):
2567+
try:
2568+
func(*args)
2569+
except OSError:
2570+
# ignore OSError, we only care about DeprecationWarning
2571+
pass
25542572

25552573
@support.skip_unless_symlink
25562574
def test_symlink(self):
25572575
filename = os.fsencode(support.TESTFN)
2558-
with warnings.catch_warnings():
2559-
warnings.simplefilter("error", DeprecationWarning)
2560-
self.assertRaises(DeprecationWarning,
2561-
os.symlink, filename, filename)
2576+
with bytes_filename_warn(True):
2577+
os.symlink(filename, filename)
25622578

25632579

25642580
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
@@ -2696,7 +2712,8 @@ def test_oserror_filename(self):
26962712
for filenames, func, *func_args in funcs:
26972713
for name in filenames:
26982714
try:
2699-
func(name, *func_args)
2715+
with bytes_filename_warn(False):
2716+
func(name, *func_args)
27002717
except OSError as err:
27012718
self.assertIs(err.filename, name)
27022719
else:
@@ -3011,7 +3028,8 @@ def test_broken_symlink(self):
30113028
def test_bytes(self):
30123029
if os.name == "nt":
30133030
# On Windows, os.scandir(bytes) must raise an exception
3014-
self.assertRaises(TypeError, os.scandir, b'.')
3031+
with bytes_filename_warn(True):
3032+
self.assertRaises(TypeError, os.scandir, b'.')
30153033
return
30163034

30173035
self.create_file("file.txt")

0 commit comments

Comments
 (0)