cpython: d54ee39b061f (original) (raw)
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -66,6 +66,7 @@ except ImportError: 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 @@ else:
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 + +@contextlib.contextmanager +def ignore_deprecation_warnings(msg_regex, quiet=False):
+ + +@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):[](#l1.26)
yield[](#l1.27)
- else:
yield[](#l1.29)
Tests creating TESTFN
class FileTests(unittest.TestCase): def setUp(self): @@ -305,8 +323,7 @@ class StatAttributeTests(unittest.TestCa fname = self.fname.encode(sys.getfilesystemencoding()) except UnicodeEncodeError: self.skipTest("cannot encode %a for the filesystem" % self.fname)
with warnings.catch_warnings():[](#l1.39)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.40)
with bytes_filename_warn(True):[](#l1.41) self.check_stat_attributes(fname)[](#l1.42)
def test_stat_result_pickle(self): @@ -443,15 +460,11 @@ class UtimeTests(unittest.TestCase): fp.write(b"ABC") def restore_float_times(state):
with warnings.catch_warnings():[](#l1.49)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.50)
with ignore_deprecation_warnings('stat_float_times'):[](#l1.52) os.stat_float_times(state)[](#l1.53)
# ensure that st_atime and st_mtime are float
with warnings.catch_warnings():[](#l1.56)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.57)
with ignore_deprecation_warnings('stat_float_times'):[](#l1.59) old_float_times = os.stat_float_times(-1)[](#l1.60) self.addCleanup(restore_float_times, old_float_times)[](#l1.61)
@@ -1024,8 +1037,7 @@ class BytesWalkTests(WalkTests): super().setUp() self.stack = contextlib.ExitStack() if os.name == 'nt':
self.stack.enter_context(warnings.catch_warnings())[](#l1.67)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.68)
self.stack.enter_context(bytes_filename_warn(False))[](#l1.69)
def tearDown(self): self.stack.close() @@ -1580,8 +1592,7 @@ class LinkTests(unittest.TestCase): with open(file1, "w") as f1: f1.write("test")
with warnings.catch_warnings():[](#l1.77)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.78)
with bytes_filename_warn(False):[](#l1.79) os.link(file1, file2)[](#l1.80) with open(file1, "r") as f1, open(file2, "r") as f2:[](#l1.81) self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))[](#l1.82)
@@ -1873,10 +1884,12 @@ class Win32ListdirTests(unittest.TestCas self.assertEqual( sorted(os.listdir(support.TESTFN)), self.created_paths) + # bytes
self.assertEqual([](#l1.89)
sorted(os.listdir(os.fsencode(support.TESTFN))),[](#l1.90)
[os.fsencode(path) for path in self.created_paths])[](#l1.91)
with bytes_filename_warn(False):[](#l1.92)
self.assertEqual([](#l1.93)
sorted(os.listdir(os.fsencode(support.TESTFN))),[](#l1.94)
[os.fsencode(path) for path in self.created_paths])[](#l1.95)
def test_listdir_extended_path(self): """Test when the path starts with '\\?\'.""" @@ -1886,11 +1899,13 @@ class Win32ListdirTests(unittest.TestCas self.assertEqual( sorted(os.listdir(path)), self.created_paths) + # bytes
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))[](#l1.105)
self.assertEqual([](#l1.106)
sorted(os.listdir(path)),[](#l1.107)
[os.fsencode(path) for path in self.created_paths])[](#l1.108)
with bytes_filename_warn(False):[](#l1.109)
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))[](#l1.110)
self.assertEqual([](#l1.111)
sorted(os.listdir(path)),[](#l1.112)
[os.fsencode(path) for path in self.created_paths])[](#l1.113)
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") @@ -1965,9 +1980,9 @@ class Win32SymlinkTests(unittest.TestCas self.assertNotEqual(os.lstat(link), os.stat(link)) bytes_link = os.fsencode(link)
with warnings.catch_warnings():[](#l1.121)
warnings.simplefilter("ignore", DeprecationWarning)[](#l1.122)
with bytes_filename_warn(True):[](#l1.123) self.assertEqual(os.stat(bytes_link), os.stat(target))[](#l1.124)
with bytes_filename_warn(True):[](#l1.125) self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))[](#l1.126)
def test_12084(self): @@ -2529,36 +2544,37 @@ class Win32DeprecatedBytesAPI(unittest.T def test_deprecated(self): import nt filename = os.fsencode(support.TESTFN)
with warnings.catch_warnings():[](#l1.133)
warnings.simplefilter("error", DeprecationWarning)[](#l1.134)
for func, *args in ([](#l1.135)
(nt._getfullpathname, filename),[](#l1.136)
(nt._isdir, filename),[](#l1.137)
(os.access, filename, os.R_OK),[](#l1.138)
(os.chdir, filename),[](#l1.139)
(os.chmod, filename, 0o777),[](#l1.140)
(os.getcwdb,),[](#l1.141)
(os.link, filename, filename),[](#l1.142)
(os.listdir, filename),[](#l1.143)
(os.lstat, filename),[](#l1.144)
(os.mkdir, filename),[](#l1.145)
(os.open, filename, os.O_RDONLY),[](#l1.146)
(os.rename, filename, filename),[](#l1.147)
(os.rmdir, filename),[](#l1.148)
(os.startfile, filename),[](#l1.149)
(os.stat, filename),[](#l1.150)
(os.unlink, filename),[](#l1.151)
(os.utime, filename),[](#l1.152)
):[](#l1.153)
self.assertRaises(DeprecationWarning, func, *args)[](#l1.154)
for func, *args in ([](#l1.155)
(nt._getfullpathname, filename),[](#l1.156)
(nt._isdir, filename),[](#l1.157)
(os.access, filename, os.R_OK),[](#l1.158)
(os.chdir, filename),[](#l1.159)
(os.chmod, filename, 0o777),[](#l1.160)
(os.getcwdb,),[](#l1.161)
(os.link, filename, filename),[](#l1.162)
(os.listdir, filename),[](#l1.163)
(os.lstat, filename),[](#l1.164)
(os.mkdir, filename),[](#l1.165)
(os.open, filename, os.O_RDONLY),[](#l1.166)
(os.rename, filename, filename),[](#l1.167)
(os.rmdir, filename),[](#l1.168)
(os.startfile, filename),[](#l1.169)
(os.stat, filename),[](#l1.170)
(os.unlink, filename),[](#l1.171)
(os.utime, filename),[](#l1.172)
):[](#l1.173)
with bytes_filename_warn(True):[](#l1.174)
try:[](#l1.175)
func(*args)[](#l1.176)
except OSError:[](#l1.177)
# ignore OSError, we only care about DeprecationWarning[](#l1.178)
pass[](#l1.179)
@support.skip_unless_symlink def test_symlink(self): filename = os.fsencode(support.TESTFN)
with warnings.catch_warnings():[](#l1.184)
warnings.simplefilter("error", DeprecationWarning)[](#l1.185)
self.assertRaises(DeprecationWarning,[](#l1.186)
os.symlink, filename, filename)[](#l1.187)
with bytes_filename_warn(True):[](#l1.188)
os.symlink(filename, filename)[](#l1.189)
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size") @@ -2696,7 +2712,8 @@ class OSErrorTests(unittest.TestCase): for filenames, func, *func_args in funcs: for name in filenames: try:
func(name, *func_args)[](#l1.197)
with bytes_filename_warn(False):[](#l1.198)
func(name, *func_args)[](#l1.199) except OSError as err:[](#l1.200) self.assertIs(err.filename, name)[](#l1.201) else:[](#l1.202)
@@ -3011,7 +3028,8 @@ class TestScandir(unittest.TestCase): def test_bytes(self): if os.name == "nt": # On Windows, os.scandir(bytes) must raise an exception
self.assertRaises(TypeError, os.scandir, b'.')[](#l1.207)
with bytes_filename_warn(True):[](#l1.208)
self.assertRaises(TypeError, os.scandir, b'.')[](#l1.209) return[](#l1.210)