diff options
author | Victor Stinner <vstinner@python.org> | 2022-10-18 15:52:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-18 15:52:31 (GMT) |
commit | db03c8066a6b12fa23618f9add0eb795936726b4 (patch) | |
tree | 71d35cac37736188bbe8b046aab657b0892be1de /Lib | |
parent | 9da5215000920870eeddd78af92da4c98099706c (diff) | |
download | cpython-db03c8066a6b12fa23618f9add0eb795936726b4.zip cpython-db03c8066a6b12fa23618f9add0eb795936726b4.tar.gz cpython-db03c8066a6b12fa23618f9add0eb795936726b4.tar.bz2 |
gh-98393: os module reject bytes-like, only accept bytes (#98394)
The os module and the PyUnicode_FSDecoder() function no longer accept
bytes-like paths, like bytearray and memoryview types: only the exact
bytes type is accepted for bytes strings.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_compile.py | 5 | ||||
-rw-r--r-- | Lib/test/test_os.py | 32 | ||||
-rw-r--r-- | Lib/test/test_posix.py | 9 | ||||
-rw-r--r-- | Lib/test/test_symtable.py | 5 |
4 files changed, 19 insertions, 32 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 8bf8470..f5c0c76 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -494,9 +494,8 @@ if 1: code = compile('pass', filename, 'exec') self.assertEqual(code.co_filename, 'file.py') for filename in bytearray(b'file.py'), memoryview(b'file.py'): - with self.assertWarns(DeprecationWarning): - code = compile('pass', filename, 'exec') - self.assertEqual(code.co_filename, 'file.py') + with self.assertRaises(TypeError): + compile('pass', filename, 'exec') self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec') @support.cpython_only diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 2dafb78..10dbb2b 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3860,18 +3860,18 @@ class OSErrorTests(unittest.TestCase): for filenames, func, *func_args in funcs: for name in filenames: - try: - if isinstance(name, (str, bytes)): + if not isinstance(name, (str, bytes)): + with self.assertRaises(TypeError): func(name, *func_args) - else: - with self.assertWarnsRegex(DeprecationWarning, 'should be'): - func(name, *func_args) - except OSError as err: - self.assertIs(err.filename, name, str(func)) - except UnicodeDecodeError: - pass else: - self.fail("No exception thrown by {}".format(func)) + try: + func(name, *func_args) + except OSError as err: + self.assertIs(err.filename, name, str(func)) + except UnicodeDecodeError: + pass + else: + self.fail("No exception thrown by {}".format(func)) class CPUCountTests(unittest.TestCase): def test_cpu_count(self): @@ -4350,16 +4350,8 @@ class TestScandir(unittest.TestCase): for cls in bytearray, memoryview: path_bytes = cls(os.fsencode(self.path)) - with self.assertWarns(DeprecationWarning): - entries = list(os.scandir(path_bytes)) - self.assertEqual(len(entries), 1, entries) - entry = entries[0] - - self.assertEqual(entry.name, b'file.txt') - self.assertEqual(entry.path, - os.fsencode(os.path.join(self.path, 'file.txt'))) - self.assertIs(type(entry.name), bytes) - self.assertIs(type(entry.path), bytes) + with self.assertRaises(TypeError): + list(os.scandir(path_bytes)) @unittest.skipUnless(os.listdir in os.supports_fd, 'fd support for listdir required for this test.') diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index e643d8e..be561af 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -634,7 +634,7 @@ class PosixTester(unittest.TestCase): self.assertTrue(posix.stat(os_helper.TESTFN)) self.assertTrue(posix.stat(os.fsencode(os_helper.TESTFN))) - self.assertWarnsRegex(DeprecationWarning, + self.assertRaisesRegex(TypeError, 'should be string, bytes, os.PathLike or integer, not', posix.stat, bytearray(os.fsencode(os_helper.TESTFN))) self.assertRaisesRegex(TypeError, @@ -841,11 +841,8 @@ class PosixTester(unittest.TestCase): def test_listdir_bytes_like(self): for cls in bytearray, memoryview: - with self.assertWarns(DeprecationWarning): - names = posix.listdir(cls(b'.')) - self.assertIn(os.fsencode(os_helper.TESTFN), names) - for name in names: - self.assertIs(type(name), bytes) + with self.assertRaises(TypeError): + posix.listdir(cls(b'.')) @unittest.skipUnless(posix.listdir in os.supports_fd, "test needs fd support for posix.listdir()") diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index 819354e..25714ae 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -222,10 +222,9 @@ class SymtableTest(unittest.TestCase): checkfilename("def f(x): foo)(", 14) # parse-time checkfilename("def f(x): global x", 11) # symtable-build-time symtable.symtable("pass", b"spam", "exec") - with self.assertWarns(DeprecationWarning), \ - self.assertRaises(TypeError): + with self.assertRaises(TypeError): symtable.symtable("pass", bytearray(b"spam"), "exec") - with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): symtable.symtable("pass", memoryview(b"spam"), "exec") with self.assertRaises(TypeError): symtable.symtable("pass", list(b"spam"), "exec") |