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

Doc/whatsnew/3.6.rst
Lib/test/test_compile.py
Lib/test/test_parser.py
Lib/test/test_symtable.py
Lib/test/test_zipimport.py
Misc/NEWS
Objects/unicodeobject.c

index 7603fa122039f29d54b2adcd72e597b22ee743d9..ca5b3e5e91d42a01def8d926c77663320b9253af 100644 (file)
@@ -639,8 +639,9 @@ Deprecated features
   (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`.)
+  as paths in :mod:`os` functions, :func:`compile` and similar functions is
+  now deprecated.
+  (Contributed by Serhiy Storchaka in :issue:`25791` and :issue:`26754`.)
 
 
 Deprecated Python behavior
index 824e843914c1231a5c2ae23f1337b217bab8e3ad..9638e6975a222e18961cd7b037ec8cb65ce6b7e5 100644 (file)
@@ -473,10 +473,13 @@ if 1:
         self.assertEqual(d, {1: 2, 3: 4})
 
     def test_compile_filename(self):
-        for filename in ('file.py', b'file.py',
-                         bytearray(b'file.py'), memoryview(b'file.py')):
+        for filename in 'file.py', b'file.py':
             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')
         self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
 
     @support.cpython_only
index 1e7d331bc0b4705bda1f9e9cfb3e6fedd360e068..e2a42f9715200c8561c8aa82875ef51525899b32 100644 (file)
@@ -632,12 +632,18 @@ class CompileTestCase(unittest.TestCase):
         self.assertEqual(code.co_filename, '<syntax-tree>')
         code = st.compile()
         self.assertEqual(code.co_filename, '<syntax-tree>')
-        for filename in ('file.py', b'file.py',
-                         bytearray(b'file.py'), memoryview(b'file.py')):
+        for filename in 'file.py', b'file.py':
             code = parser.compilest(st, filename)
             self.assertEqual(code.co_filename, 'file.py')
             code = st.compile(filename)
             self.assertEqual(code.co_filename, 'file.py')
+        for filename in bytearray(b'file.py'), memoryview(b'file.py'):
+            with self.assertWarns(DeprecationWarning):
+                code = parser.compilest(st, filename)
+            self.assertEqual(code.co_filename, 'file.py')
+            with self.assertWarns(DeprecationWarning):
+                code = st.compile(filename)
+            self.assertEqual(code.co_filename, 'file.py')
         self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
         self.assertRaises(TypeError, st.compile, list(b'file.py'))
 
index c5d7facfdbd1849ef41d6d702c5222268711855c..bf995056234af133d83a1c96ba1833983f36265c 100644 (file)
@@ -158,9 +158,11 @@ class SymtableTest(unittest.TestCase):
         checkfilename("def f(x): foo)(")  # parse-time
         checkfilename("def f(x): global x")  # symtable-build-time
         symtable.symtable("pass", b"spam", "exec")
-        with self.assertRaises(TypeError):
+        with self.assertWarns(DeprecationWarning), \
+             self.assertRaises(TypeError):
             symtable.symtable("pass", bytearray(b"spam"), "exec")
-        symtable.symtable("pass", memoryview(b"spam"), "exec")
+        with self.assertWarns(DeprecationWarning):
+            symtable.symtable("pass", memoryview(b"spam"), "exec")
         with self.assertRaises(TypeError):
             symtable.symtable("pass", list(b"spam"), "exec")
 
index 20491cde7a79ce8bc591d4229b465f9ccb67925f..a2482d45a747a5f343753375932c99fd66290a6b 100644 (file)
@@ -629,8 +629,10 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
 
         zipimport.zipimporter(filename)
         zipimport.zipimporter(os.fsencode(filename))
-        zipimport.zipimporter(bytearray(os.fsencode(filename)))
-        zipimport.zipimporter(memoryview(os.fsencode(filename)))
+        with self.assertWarns(DeprecationWarning):
+            zipimport.zipimporter(bytearray(os.fsencode(filename)))
+        with self.assertWarns(DeprecationWarning):
+            zipimport.zipimporter(memoryview(os.fsencode(filename)))
 
 
 @support.requires_zlib
index 03a8104a34b2f018b800e96c08ee8e4f4afe2e84..56c21cb8bee37d207185815edb973802d639a1d6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #26754: Undocumented support of general bytes-like objects
+  as path in compile() and similar functions is now deprecated.
+
 - Issue #26800: Undocumented support of general bytes-like objects
   as paths in os functions is now deprecated.
 
index 0932f35b7668c3634f838f80aa137d4f11822283..2d31c700def0a7824676501619bc69277233f30e 100644 (file)
@@ -3837,7 +3837,13 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
         output = arg;
         Py_INCREF(output);
     }
-    else if (PyObject_CheckBuffer(arg)) {
+    else if (PyBytes_Check(arg) || PyObject_CheckBuffer(arg)) {
+        if (!PyBytes_Check(arg) &&
+            PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+            "path should be string or bytes, not %.200s",
+            Py_TYPE(arg)->tp_name)) {
+            return 0;
+        }
         arg = PyBytes_FromObject(arg);
         if (!arg)
             return 0;
@@ -3846,11 +3852,6 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
         Py_DECREF(arg);
         if (!output)
             return 0;
-        if (!PyUnicode_Check(output)) {
-            Py_DECREF(output);
-            PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode");
-            return 0;
-        }
     }
     else {
         PyErr_Format(PyExc_TypeError,