Commit 923590e3 authored by Victor Stinner's avatar Victor Stinner

Fix DeprecationWarning on Windows

Issue #25911: Use support.check_warnings() to expect or ignore
DeprecationWarning in test_os.
parent 5e14a38e
...@@ -66,6 +66,7 @@ except ImportError: ...@@ -66,6 +66,7 @@ except ImportError:
from test.support.script_helper import assert_python_ok from test.support.script_helper import assert_python_ok
root_in_posix = False root_in_posix = False
if hasattr(os, 'geteuid'): if hasattr(os, 'geteuid'):
root_in_posix = (os.geteuid() == 0) root_in_posix = (os.geteuid() == 0)
...@@ -82,6 +83,23 @@ else: ...@@ -82,6 +83,23 @@ else:
# Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group. # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0 HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
@contextlib.contextmanager
def ignore_deprecation_warnings(msg_regex, quiet=False):
with support.check_warnings((msg_regex, DeprecationWarning), quiet=quiet):
yield
@contextlib.contextmanager
def bytes_filename_warn(expected):
msg = 'The Windows bytes API has been deprecated'
if os.name == 'nt':
with ignore_deprecation_warnings(msg, quiet=not expected):
yield
else:
yield
# Tests creating TESTFN # Tests creating TESTFN
class FileTests(unittest.TestCase): class FileTests(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -305,8 +323,7 @@ class StatAttributeTests(unittest.TestCase): ...@@ -305,8 +323,7 @@ class StatAttributeTests(unittest.TestCase):
fname = self.fname.encode(sys.getfilesystemencoding()) fname = self.fname.encode(sys.getfilesystemencoding())
except UnicodeEncodeError: except UnicodeEncodeError:
self.skipTest("cannot encode %a for the filesystem" % self.fname) self.skipTest("cannot encode %a for the filesystem" % self.fname)
with warnings.catch_warnings(): with bytes_filename_warn(True):
warnings.simplefilter("ignore", DeprecationWarning)
self.check_stat_attributes(fname) self.check_stat_attributes(fname)
def test_stat_result_pickle(self): def test_stat_result_pickle(self):
...@@ -443,15 +460,11 @@ class UtimeTests(unittest.TestCase): ...@@ -443,15 +460,11 @@ class UtimeTests(unittest.TestCase):
fp.write(b"ABC") fp.write(b"ABC")
def restore_float_times(state): def restore_float_times(state):
with warnings.catch_warnings(): with ignore_deprecation_warnings('stat_float_times'):
warnings.simplefilter("ignore", DeprecationWarning)
os.stat_float_times(state) os.stat_float_times(state)
# ensure that st_atime and st_mtime are float # ensure that st_atime and st_mtime are float
with warnings.catch_warnings(): with ignore_deprecation_warnings('stat_float_times'):
warnings.simplefilter("ignore", DeprecationWarning)
old_float_times = os.stat_float_times(-1) old_float_times = os.stat_float_times(-1)
self.addCleanup(restore_float_times, old_float_times) self.addCleanup(restore_float_times, old_float_times)
...@@ -1024,8 +1037,7 @@ class BytesWalkTests(WalkTests): ...@@ -1024,8 +1037,7 @@ class BytesWalkTests(WalkTests):
super().setUp() super().setUp()
self.stack = contextlib.ExitStack() self.stack = contextlib.ExitStack()
if os.name == 'nt': if os.name == 'nt':
self.stack.enter_context(warnings.catch_warnings()) self.stack.enter_context(bytes_filename_warn(False))
warnings.simplefilter("ignore", DeprecationWarning)
def tearDown(self): def tearDown(self):
self.stack.close() self.stack.close()
...@@ -1580,8 +1592,7 @@ class LinkTests(unittest.TestCase): ...@@ -1580,8 +1592,7 @@ class LinkTests(unittest.TestCase):
with open(file1, "w") as f1: with open(file1, "w") as f1:
f1.write("test") f1.write("test")
with warnings.catch_warnings(): with bytes_filename_warn(False):
warnings.simplefilter("ignore", DeprecationWarning)
os.link(file1, file2) os.link(file1, file2)
with open(file1, "r") as f1, open(file2, "r") as f2: with open(file1, "r") as f1, open(file2, "r") as f2:
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno())) self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
...@@ -1873,7 +1884,9 @@ class Win32ListdirTests(unittest.TestCase): ...@@ -1873,7 +1884,9 @@ class Win32ListdirTests(unittest.TestCase):
self.assertEqual( self.assertEqual(
sorted(os.listdir(support.TESTFN)), sorted(os.listdir(support.TESTFN)),
self.created_paths) self.created_paths)
# bytes # bytes
with bytes_filename_warn(False):
self.assertEqual( self.assertEqual(
sorted(os.listdir(os.fsencode(support.TESTFN))), sorted(os.listdir(os.fsencode(support.TESTFN))),
[os.fsencode(path) for path in self.created_paths]) [os.fsencode(path) for path in self.created_paths])
...@@ -1886,7 +1899,9 @@ class Win32ListdirTests(unittest.TestCase): ...@@ -1886,7 +1899,9 @@ class Win32ListdirTests(unittest.TestCase):
self.assertEqual( self.assertEqual(
sorted(os.listdir(path)), sorted(os.listdir(path)),
self.created_paths) self.created_paths)
# bytes # bytes
with bytes_filename_warn(False):
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN)) path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
self.assertEqual( self.assertEqual(
sorted(os.listdir(path)), sorted(os.listdir(path)),
...@@ -1965,9 +1980,9 @@ class Win32SymlinkTests(unittest.TestCase): ...@@ -1965,9 +1980,9 @@ class Win32SymlinkTests(unittest.TestCase):
self.assertNotEqual(os.lstat(link), os.stat(link)) self.assertNotEqual(os.lstat(link), os.stat(link))
bytes_link = os.fsencode(link) bytes_link = os.fsencode(link)
with warnings.catch_warnings(): with bytes_filename_warn(True):
warnings.simplefilter("ignore", DeprecationWarning)
self.assertEqual(os.stat(bytes_link), os.stat(target)) self.assertEqual(os.stat(bytes_link), os.stat(target))
with bytes_filename_warn(True):
self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link)) self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
def test_12084(self): def test_12084(self):
...@@ -2529,8 +2544,6 @@ class Win32DeprecatedBytesAPI(unittest.TestCase): ...@@ -2529,8 +2544,6 @@ class Win32DeprecatedBytesAPI(unittest.TestCase):
def test_deprecated(self): def test_deprecated(self):
import nt import nt
filename = os.fsencode(support.TESTFN) filename = os.fsencode(support.TESTFN)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
for func, *args in ( for func, *args in (
(nt._getfullpathname, filename), (nt._getfullpathname, filename),
(nt._isdir, filename), (nt._isdir, filename),
...@@ -2550,15 +2563,18 @@ class Win32DeprecatedBytesAPI(unittest.TestCase): ...@@ -2550,15 +2563,18 @@ class Win32DeprecatedBytesAPI(unittest.TestCase):
(os.unlink, filename), (os.unlink, filename),
(os.utime, filename), (os.utime, filename),
): ):
self.assertRaises(DeprecationWarning, func, *args) with bytes_filename_warn(True):
try:
func(*args)
except OSError:
# ignore OSError, we only care about DeprecationWarning
pass
@support.skip_unless_symlink @support.skip_unless_symlink
def test_symlink(self): def test_symlink(self):
filename = os.fsencode(support.TESTFN) filename = os.fsencode(support.TESTFN)
with warnings.catch_warnings(): with bytes_filename_warn(True):
warnings.simplefilter("error", DeprecationWarning) os.symlink(filename, filename)
self.assertRaises(DeprecationWarning,
os.symlink, filename, filename)
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size") @unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
...@@ -2696,6 +2712,7 @@ class OSErrorTests(unittest.TestCase): ...@@ -2696,6 +2712,7 @@ class OSErrorTests(unittest.TestCase):
for filenames, func, *func_args in funcs: for filenames, func, *func_args in funcs:
for name in filenames: for name in filenames:
try: try:
with bytes_filename_warn(False):
func(name, *func_args) func(name, *func_args)
except OSError as err: except OSError as err:
self.assertIs(err.filename, name) self.assertIs(err.filename, name)
...@@ -3011,6 +3028,7 @@ class TestScandir(unittest.TestCase): ...@@ -3011,6 +3028,7 @@ class TestScandir(unittest.TestCase):
def test_bytes(self): def test_bytes(self):
if os.name == "nt": if os.name == "nt":
# On Windows, os.scandir(bytes) must raise an exception # On Windows, os.scandir(bytes) must raise an exception
with bytes_filename_warn(True):
self.assertRaises(TypeError, os.scandir, b'.') self.assertRaises(TypeError, os.scandir, b'.')
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment