Kaydet (Commit) 1ab6c2d2 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #13374: The Windows bytes API has been deprecated in the os module. Use

Unicode filenames instead of bytes filenames to not depend on the ANSI code
page anymore and to support any filename.
üst 6166519d
...@@ -573,6 +573,10 @@ Porting Python code ...@@ -573,6 +573,10 @@ Porting Python code
with sys.platform.startswith('linux'), or directly sys.platform == 'linux' if with sys.platform.startswith('linux'), or directly sys.platform == 'linux' if
you don't need to support older Python versions. you don't need to support older Python versions.
* Issue #13374: The Windows bytes API has been deprecated in the :mod:`os`
module. Use Unicode filenames instead of bytes filenames to not depend on the
ANSI code page anymore and to support any filename.
Porting C code Porting C code
-------------- --------------
......
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
Tests common to genericpath, macpath, ntpath and posixpath Tests common to genericpath, macpath, ntpath and posixpath
""" """
import unittest
from test import support
import os
import genericpath import genericpath
import os
import sys import sys
import unittest
import warnings
from test import support
def safe_rmdir(dirname): def safe_rmdir(dirname):
...@@ -258,7 +259,9 @@ class CommonTest(GenericTest): ...@@ -258,7 +259,9 @@ class CommonTest(GenericTest):
def test_abspath(self): def test_abspath(self):
self.assertIn("foo", self.pathmodule.abspath("foo")) self.assertIn("foo", self.pathmodule.abspath("foo"))
self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
# Abspath returns bytes when the arg is bytes # Abspath returns bytes when the arg is bytes
for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'): for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
...@@ -266,7 +269,9 @@ class CommonTest(GenericTest): ...@@ -266,7 +269,9 @@ class CommonTest(GenericTest):
def test_realpath(self): def test_realpath(self):
self.assertIn("foo", self.pathmodule.realpath("foo")) self.assertIn("foo", self.pathmodule.realpath("foo"))
self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
def test_normpath_issue5827(self): def test_normpath_issue5827(self):
# Make sure normpath preserves unicode # Make sure normpath preserves unicode
...@@ -296,8 +301,10 @@ class CommonTest(GenericTest): ...@@ -296,8 +301,10 @@ class CommonTest(GenericTest):
"Mac OS X denies the creation of a directory with an invalid utf8 name") "Mac OS X denies the creation of a directory with an invalid utf8 name")
def test_nonascii_abspath(self): def test_nonascii_abspath(self):
# Test non-ASCII, non-UTF8 bytes in the path. # Test non-ASCII, non-UTF8 bytes in the path.
with support.temp_cwd(b'\xe7w\xf0'): with warnings.catch_warnings():
self.test_abspath() warnings.simplefilter("ignore", DeprecationWarning)
with support.temp_cwd(b'\xe7w\xf0'):
self.test_abspath()
def test_main(): def test_main():
......
import ntpath import ntpath
import os import os
import sys import sys
import unittest
import warnings
from test.support import TestFailed from test.support import TestFailed
from test import support, test_genericpath from test import support, test_genericpath
from tempfile import TemporaryFile from tempfile import TemporaryFile
import unittest
def tester(fn, wantResult): def tester(fn, wantResult):
...@@ -21,7 +22,9 @@ def tester(fn, wantResult): ...@@ -21,7 +22,9 @@ def tester(fn, wantResult):
fn = fn.replace('["', '[b"') fn = fn.replace('["', '[b"')
fn = fn.replace(", '", ", b'") fn = fn.replace(", '", ", b'")
fn = fn.replace(', "', ', b"') fn = fn.replace(', "', ', b"')
gotResult = eval(fn) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
gotResult = eval(fn)
if isinstance(wantResult, str): if isinstance(wantResult, str):
wantResult = wantResult.encode('ascii') wantResult = wantResult.encode('ascii')
elif isinstance(wantResult, tuple): elif isinstance(wantResult, tuple):
......
...@@ -213,7 +213,9 @@ class StatAttributeTests(unittest.TestCase): ...@@ -213,7 +213,9 @@ 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)
self.check_stat_attributes(fname) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.check_stat_attributes(fname)
def test_statvfs_attributes(self): def test_statvfs_attributes(self):
if not hasattr(os, "statvfs"): if not hasattr(os, "statvfs"):
...@@ -838,7 +840,9 @@ class LinkTests(unittest.TestCase): ...@@ -838,7 +840,9 @@ class LinkTests(unittest.TestCase):
with open(file1, "w") as f1: with open(file1, "w") as f1:
f1.write("test") f1.write("test")
os.link(file1, file2) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
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()))
...@@ -1160,8 +1164,10 @@ class Win32SymlinkTests(unittest.TestCase): ...@@ -1160,8 +1164,10 @@ 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)
self.assertEqual(os.stat(bytes_link), os.stat(target)) with warnings.catch_warnings():
self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link)) warnings.simplefilter("ignore", DeprecationWarning)
self.assertEqual(os.stat(bytes_link), os.stat(target))
self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))
def test_12084(self): def test_12084(self):
level1 = os.path.abspath(support.TESTFN) level1 = os.path.abspath(support.TESTFN)
...@@ -1619,6 +1625,35 @@ class ExtendedAttributeTests(unittest.TestCase): ...@@ -1619,6 +1625,35 @@ class ExtendedAttributeTests(unittest.TestCase):
self._check_xattrs(getxattr, setxattr, removexattr, listxattr) self._check_xattrs(getxattr, setxattr, removexattr, listxattr)
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32DeprecatedBytesAPI(unittest.TestCase):
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.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.symlink, filename, filename),
(os.unlink, filename),
(os.utime, filename),
):
self.assertRaises(DeprecationWarning, func, *args)
@support.reap_threads @support.reap_threads
def test_main(): def test_main():
support.run_unittest( support.run_unittest(
...@@ -1643,6 +1678,7 @@ def test_main(): ...@@ -1643,6 +1678,7 @@ def test_main():
TestSendfile, TestSendfile,
ProgramPriorityTests, ProgramPriorityTests,
ExtendedAttributeTests, ExtendedAttributeTests,
Win32DeprecatedBytesAPI,
) )
if __name__ == "__main__": if __name__ == "__main__":
......
# Test the Unicode versions of normal file functions # Test the Unicode versions of normal file functions
# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
import sys, os, unittest import os
import sys
import unittest
import warnings
from unicodedata import normalize from unicodedata import normalize
from test import support from test import support
...@@ -155,7 +158,9 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -155,7 +158,9 @@ class UnicodeFileTests(unittest.TestCase):
@unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X') @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
def test_listdir(self): def test_listdir(self):
sf0 = set(self.files) sf0 = set(self.files)
f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
f2 = os.listdir(support.TESTFN) f2 = os.listdir(support.TESTFN)
sf2 = set(os.path.join(support.TESTFN, f) for f in f2) sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2)) self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
......
import unittest
from test import support, test_genericpath
import posixpath
import os import os
import posixpath
import sys import sys
import unittest
import warnings
from posixpath import realpath, abspath, dirname, basename from posixpath import realpath, abspath, dirname, basename
from test import support, test_genericpath
try: try:
import posix import posix
...@@ -231,7 +231,9 @@ class PosixPathTest(unittest.TestCase): ...@@ -231,7 +231,9 @@ class PosixPathTest(unittest.TestCase):
def test_ismount(self): def test_ismount(self):
self.assertIs(posixpath.ismount("/"), True) self.assertIs(posixpath.ismount("/"), True)
self.assertIs(posixpath.ismount(b"/"), True) with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertIs(posixpath.ismount(b"/"), True)
def test_ismount_non_existent(self): def test_ismount_non_existent(self):
# Non-existent mountpoint. # Non-existent mountpoint.
......
...@@ -374,6 +374,10 @@ Core and Builtins ...@@ -374,6 +374,10 @@ Core and Builtins
Library Library
------- -------
- Issue #13374: The Windows bytes API has been deprecated in the os module. Use
Unicode filenames instead of bytes filenames to not depend on the ANSI code
page anymore and to support any filename.
- Issue #13297: Use bytes type to send and receive binary data through XMLRPC. - Issue #13297: Use bytes type to send and receive binary data through XMLRPC.
- Issue #6397: Support "/dev/poll" polling objects in select module, - Issue #6397: Support "/dev/poll" polling objects in select module,
......
This diff is collapsed.
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