]> granicus.if.org Git - python/commitdiff
Issue #26800: Undocumented support of general bytes-like objects
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 6 Aug 2016 20:22:08 +0000 (23:22 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 6 Aug 2016 20:22:08 +0000 (23:22 +0300)
as paths in os functions is now deprecated.

Doc/whatsnew/3.6.rst
Lib/test/test_os.py
Lib/test/test_posix.py
Misc/NEWS
Modules/posixmodule.c

index 7a318e4ca12ed68bbf75d567801107826a872792..7603fa122039f29d54b2adcd72e597b22ee743d9 100644 (file)
@@ -638,6 +638,11 @@ Deprecated features
   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
 --------------------------
 
index aa9b53874849d5d262c8c5d7770f1bfe995f735d..d8920d99c514a0d05a9a9af0a58cc25b4268efe5 100644 (file)
@@ -2626,6 +2626,7 @@ class OSErrorTests(unittest.TestCase):
         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 @@ class OSErrorTests(unittest.TestCase):
         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:
index 6a1c82917a9a35632afbdee37daa8c6b37ecafa7..de22513e34e782021f000e4a17e551ebd3a5c163 100644 (file)
@@ -407,8 +407,10 @@ class PosixTester(unittest.TestCase):
     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)
index 9c60247f302bc9844110c2cf5e5784e1b9a1f49c..03a8104a34b2f018b800e96c08ee8e4f4afe2e84 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@ Core and Builtins
 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
index 6adc7f44dbf42654976f52407b3578c6e5ed26e7..52e465f1ffd0800467a50f2eb3dbc94f68310768 100644 (file)
@@ -891,7 +891,28 @@ path_converter(PyObject *o, void *p)
         }
 #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_converter(PyObject *o, void *p)
     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