[Python-checkins] cpython: Fix DeprecationWarning on Windows

victor.stinner python-checkins at python.org
Thu Mar 24 04:48:18 EDT 2016


https://hg.python.org/cpython/rev/d54ee39b061f
changeset:   100710:d54ee39b061f
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Thu Mar 24 09:11:48 2016 +0100
summary:
  Fix DeprecationWarning on Windows

Issue #25911: Use support.check_warnings() to expect or ignore
DeprecationWarning in test_os.

files:
  Lib/test/test_os.py |  116 ++++++++++++++++++-------------
  1 files changed, 67 insertions(+), 49 deletions(-)


diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -66,6 +66,7 @@
 
 from test.support.script_helper import assert_python_ok
 
+
 root_in_posix = False
 if hasattr(os, 'geteuid'):
     root_in_posix = (os.geteuid() == 0)
@@ -82,6 +83,23 @@
 # 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
 
+
+ at contextlib.contextmanager
+def ignore_deprecation_warnings(msg_regex, quiet=False):
+    with support.check_warnings((msg_regex, DeprecationWarning), quiet=quiet):
+        yield
+
+
+ at 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
 class FileTests(unittest.TestCase):
     def setUp(self):
@@ -305,8 +323,7 @@
             fname = self.fname.encode(sys.getfilesystemencoding())
         except UnicodeEncodeError:
             self.skipTest("cannot encode %a for the filesystem" % self.fname)
-        with warnings.catch_warnings():
-            warnings.simplefilter("ignore", DeprecationWarning)
+        with bytes_filename_warn(True):
             self.check_stat_attributes(fname)
 
     def test_stat_result_pickle(self):
@@ -443,15 +460,11 @@
             fp.write(b"ABC")
 
         def restore_float_times(state):
-            with warnings.catch_warnings():
-                warnings.simplefilter("ignore", DeprecationWarning)
-
+            with ignore_deprecation_warnings('stat_float_times'):
                 os.stat_float_times(state)
 
         # ensure that st_atime and st_mtime are float
-        with warnings.catch_warnings():
-            warnings.simplefilter("ignore", DeprecationWarning)
-
+        with ignore_deprecation_warnings('stat_float_times'):
             old_float_times = os.stat_float_times(-1)
             self.addCleanup(restore_float_times, old_float_times)
 
@@ -1024,8 +1037,7 @@
         super().setUp()
         self.stack = contextlib.ExitStack()
         if os.name == 'nt':
-            self.stack.enter_context(warnings.catch_warnings())
-            warnings.simplefilter("ignore", DeprecationWarning)
+            self.stack.enter_context(bytes_filename_warn(False))
 
     def tearDown(self):
         self.stack.close()
@@ -1580,8 +1592,7 @@
         with open(file1, "w") as f1:
             f1.write("test")
 
-        with warnings.catch_warnings():
-            warnings.simplefilter("ignore", DeprecationWarning)
+        with bytes_filename_warn(False):
             os.link(file1, file2)
         with open(file1, "r") as f1, open(file2, "r") as f2:
             self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
@@ -1873,10 +1884,12 @@
         self.assertEqual(
                 sorted(os.listdir(support.TESTFN)),
                 self.created_paths)
+
         # bytes
-        self.assertEqual(
-                sorted(os.listdir(os.fsencode(support.TESTFN))),
-                [os.fsencode(path) for path in self.created_paths])
+        with bytes_filename_warn(False):
+            self.assertEqual(
+                    sorted(os.listdir(os.fsencode(support.TESTFN))),
+                    [os.fsencode(path) for path in self.created_paths])
 
     def test_listdir_extended_path(self):
         """Test when the path starts with '\\\\?\\'."""
@@ -1886,11 +1899,13 @@
         self.assertEqual(
                 sorted(os.listdir(path)),
                 self.created_paths)
+
         # bytes
-        path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
-        self.assertEqual(
-                sorted(os.listdir(path)),
-                [os.fsencode(path) for path in self.created_paths])
+        with bytes_filename_warn(False):
+            path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
+            self.assertEqual(
+                    sorted(os.listdir(path)),
+                    [os.fsencode(path) for path in self.created_paths])
 
 
 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
@@ -1965,9 +1980,9 @@
         self.assertNotEqual(os.lstat(link), os.stat(link))
 
         bytes_link = os.fsencode(link)
-        with warnings.catch_warnings():
-            warnings.simplefilter("ignore", DeprecationWarning)
+        with bytes_filename_warn(True):
             self.assertEqual(os.stat(bytes_link), os.stat(target))
+        with bytes_filename_warn(True):
             self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
 
     def test_12084(self):
@@ -2529,36 +2544,37 @@
     def test_deprecated(self):
         import nt
         filename = os.fsencode(support.TESTFN)
-        with warnings.catch_warnings():
-            warnings.simplefilter("error", DeprecationWarning)
-            for func, *args in (
-                (nt._getfullpathname, filename),
-                (nt._isdir, filename),
-                (os.access, filename, os.R_OK),
-                (os.chdir, filename),
-                (os.chmod, filename, 0o777),
-                (os.getcwdb,),
-                (os.link, filename, filename),
-                (os.listdir, filename),
-                (os.lstat, filename),
-                (os.mkdir, filename),
-                (os.open, filename, os.O_RDONLY),
-                (os.rename, filename, filename),
-                (os.rmdir, filename),
-                (os.startfile, filename),
-                (os.stat, filename),
-                (os.unlink, filename),
-                (os.utime, filename),
-            ):
-                self.assertRaises(DeprecationWarning, func, *args)
+        for func, *args in (
+            (nt._getfullpathname, filename),
+            (nt._isdir, filename),
+            (os.access, filename, os.R_OK),
+            (os.chdir, filename),
+            (os.chmod, filename, 0o777),
+            (os.getcwdb,),
+            (os.link, filename, filename),
+            (os.listdir, filename),
+            (os.lstat, filename),
+            (os.mkdir, filename),
+            (os.open, filename, os.O_RDONLY),
+            (os.rename, filename, filename),
+            (os.rmdir, filename),
+            (os.startfile, filename),
+            (os.stat, filename),
+            (os.unlink, filename),
+            (os.utime, filename),
+        ):
+            with bytes_filename_warn(True):
+                try:
+                    func(*args)
+                except OSError:
+                    # ignore OSError, we only care about DeprecationWarning
+                    pass
 
     @support.skip_unless_symlink
     def test_symlink(self):
         filename = os.fsencode(support.TESTFN)
-        with warnings.catch_warnings():
-            warnings.simplefilter("error", DeprecationWarning)
-            self.assertRaises(DeprecationWarning,
-                              os.symlink, filename, filename)
+        with bytes_filename_warn(True):
+            os.symlink(filename, filename)
 
 
 @unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
@@ -2696,7 +2712,8 @@
         for filenames, func, *func_args in funcs:
             for name in filenames:
                 try:
-                    func(name, *func_args)
+                    with bytes_filename_warn(False):
+                        func(name, *func_args)
                 except OSError as err:
                     self.assertIs(err.filename, name)
                 else:
@@ -3011,7 +3028,8 @@
     def test_bytes(self):
         if os.name == "nt":
             # On Windows, os.scandir(bytes) must raise an exception
-            self.assertRaises(TypeError, os.scandir, b'.')
+            with bytes_filename_warn(True):
+                self.assertRaises(TypeError, os.scandir, b'.')
             return
 
         self.create_file("file.txt")

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list