[Python-checkins] cpython: Issue #26800: Undocumented support of general bytes-like objects

serhiy.storchaka python-checkins at python.org
Sat Aug 6 16:22:50 EDT 2016


https://hg.python.org/cpython/rev/1e01ca34a42c
changeset:   102555:1e01ca34a42c
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Aug 06 23:22:08 2016 +0300
summary:
  Issue #26800: Undocumented support of general bytes-like objects
as paths in os functions is now deprecated.

files:
  Doc/whatsnew/3.6.rst   |   5 ++++
  Lib/test/test_os.py    |   9 +++++++-
  Lib/test/test_posix.py |   4 ++-
  Misc/NEWS              |   3 ++
  Modules/posixmodule.c  |  31 ++++++++++++++++++++++++++++-
  5 files changed, 48 insertions(+), 4 deletions(-)


diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -638,6 +638,11 @@
   and will be removed in 3.8.
   (Contributed by Serhiy Storchaka in :issue:`21708`.)
 
+* Undocumented support of general :term:`bytes-like objects <bytes-like object>`
+  as paths in :mod:`os` functions is now deprecated.
+  (Contributed by Serhiy Storchaka in :issue:`25791`.)
+
+
 Deprecated Python behavior
 --------------------------
 
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
@@ -2626,6 +2626,7 @@
         else:
             encoded = os.fsencode(support.TESTFN)
         self.bytes_filenames.append(encoded)
+        self.bytes_filenames.append(bytearray(encoded))
         self.bytes_filenames.append(memoryview(encoded))
 
         self.filenames = self.bytes_filenames + self.unicode_filenames
@@ -2699,8 +2700,14 @@
         for filenames, func, *func_args in funcs:
             for name in filenames:
                 try:
-                    with bytes_filename_warn(False):
+                    if isinstance(name, str):
                         func(name, *func_args)
+                    elif isinstance(name, bytes):
+                        with bytes_filename_warn(False):
+                            func(name, *func_args)
+                    else:
+                        with self.assertWarnsRegex(DeprecationWarning, 'should be'):
+                            func(name, *func_args)
                 except OSError as err:
                     self.assertIs(err.filename, name)
                 else:
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -407,8 +407,10 @@
     def test_stat(self):
         self.assertTrue(posix.stat(support.TESTFN))
         self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
-        self.assertTrue(posix.stat(bytearray(os.fsencode(support.TESTFN))))
 
+        self.assertWarnsRegex(DeprecationWarning,
+                'should be string, bytes or integer, not',
+                posix.stat, bytearray(os.fsencode(support.TESTFN)))
         self.assertRaisesRegex(TypeError,
                 'should be string, bytes or integer, not',
                 posix.stat, None)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@
 Library
 -------
 
+- Issue #26800: Undocumented support of general bytes-like objects
+  as paths in os functions is now deprecated.
+
 - Issue #27661: Added tzinfo keyword argument to datetime.combine.
 
 - Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -891,7 +891,28 @@
         }
 #endif
     }
+    else if (PyBytes_Check(o)) {
+#ifdef MS_WINDOWS
+        if (win32_warn_bytes_api()) {
+            return 0;
+        }
+#endif
+        bytes = o;
+        Py_INCREF(bytes);
+    }
     else if (PyObject_CheckBuffer(o)) {
+        if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+            "%s%s%s should be %s, not %.200s",
+            path->function_name ? path->function_name : "",
+            path->function_name ? ": "                : "",
+            path->argument_name ? path->argument_name : "path",
+            path->allow_fd && path->nullable ? "string, bytes, integer or None" :
+            path->allow_fd ? "string, bytes or integer" :
+            path->nullable ? "string, bytes or None" :
+                             "string or bytes",
+            Py_TYPE(o)->tp_name)) {
+            return 0;
+        }
 #ifdef MS_WINDOWS
         if (win32_warn_bytes_api()) {
             return 0;
@@ -946,8 +967,14 @@
     path->length = length;
     path->object = o;
     path->fd = -1;
-    path->cleanup = bytes;
-    return Py_CLEANUP_SUPPORTED;
+    if (bytes == o) {
+        Py_DECREF(bytes);
+        return 1;
+    }
+    else {
+        path->cleanup = bytes;
+        return Py_CLEANUP_SUPPORTED;
+    }
 }
 
 static void

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


More information about the Python-checkins mailing list