diff -r 69c0aa8a8185 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Thu Jun 09 16:30:29 2016 +0300 +++ b/Doc/whatsnew/3.6.rst Fri Jun 10 23:59:06 2016 +0300 @@ -550,6 +550,10 @@ Deprecated features ``__package__`` are not defined now raises an :exc:`ImportWarning`. (Contributed by Rose Ames in :issue:`25791`.) +* Undocumented support of general :term:`bytes-like objects ` + as paths in :mod:`os` functions is now deprecated. + (Contributed by Serhiy Storchaka in :issue:`25791`.) + Deprecated Python behavior -------------------------- diff -r 69c0aa8a8185 Lib/test/test_os.py --- a/Lib/test/test_os.py Thu Jun 09 16:30:29 2016 +0300 +++ b/Lib/test/test_os.py Fri Jun 10 23:59:06 2016 +0300 @@ -2623,6 +2623,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 @@ -2696,8 +2697,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: diff -r 69c0aa8a8185 Lib/test/test_posix.py --- a/Lib/test/test_posix.py Thu Jun 09 16:30:29 2016 +0300 +++ b/Lib/test/test_posix.py Fri Jun 10 23:59:06 2016 +0300 @@ -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) diff -r 69c0aa8a8185 Modules/posixmodule.c --- a/Modules/posixmodule.c Thu Jun 09 16:30:29 2016 +0300 +++ b/Modules/posixmodule.c Fri Jun 10 23:59:06 2016 +0300 @@ -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