changeset: 102556:818f22f9ab02 user: Serhiy Storchaka date: Sat Aug 06 23:29:29 2016 +0300 files: 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 description: Issue #26754: Undocumented support of general bytes-like objects as path in compile() and similar functions is now deprecated. diff -r 1e01ca34a42c -r 818f22f9ab02 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Sat Aug 06 23:22:08 2016 +0300 +++ b/Doc/whatsnew/3.6.rst Sat Aug 06 23:29:29 2016 +0300 @@ -639,8 +639,9 @@ (Contributed by Serhiy Storchaka in :issue:`21708`.) * Undocumented support of general :term:`bytes-like objects ` - 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 diff -r 1e01ca34a42c -r 818f22f9ab02 Lib/test/test_compile.py --- a/Lib/test/test_compile.py Sat Aug 06 23:22:08 2016 +0300 +++ b/Lib/test/test_compile.py Sat Aug 06 23:29:29 2016 +0300 @@ -473,10 +473,13 @@ 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 diff -r 1e01ca34a42c -r 818f22f9ab02 Lib/test/test_parser.py --- a/Lib/test/test_parser.py Sat Aug 06 23:22:08 2016 +0300 +++ b/Lib/test/test_parser.py Sat Aug 06 23:29:29 2016 +0300 @@ -632,12 +632,18 @@ self.assertEqual(code.co_filename, '') code = st.compile() self.assertEqual(code.co_filename, '') - 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')) diff -r 1e01ca34a42c -r 818f22f9ab02 Lib/test/test_symtable.py --- a/Lib/test/test_symtable.py Sat Aug 06 23:22:08 2016 +0300 +++ b/Lib/test/test_symtable.py Sat Aug 06 23:29:29 2016 +0300 @@ -158,9 +158,11 @@ 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") diff -r 1e01ca34a42c -r 818f22f9ab02 Lib/test/test_zipimport.py --- a/Lib/test/test_zipimport.py Sat Aug 06 23:22:08 2016 +0300 +++ b/Lib/test/test_zipimport.py Sat Aug 06 23:29:29 2016 +0300 @@ -629,8 +629,10 @@ 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 diff -r 1e01ca34a42c -r 818f22f9ab02 Misc/NEWS --- a/Misc/NEWS Sat Aug 06 23:22:08 2016 +0300 +++ b/Misc/NEWS Sat Aug 06 23:29:29 2016 +0300 @@ -43,6 +43,9 @@ 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. diff -r 1e01ca34a42c -r 818f22f9ab02 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Aug 06 23:22:08 2016 +0300 +++ b/Objects/unicodeobject.c Sat Aug 06 23:29:29 2016 +0300 @@ -3837,7 +3837,13 @@ 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 @@ 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,