diff -r 4646b64139c9 Modules/posixmodule.c --- a/Modules/posixmodule.c Fri Oct 07 23:46:22 2016 +0300 +++ b/Modules/posixmodule.c Sat Oct 08 10:51:28 2016 +0300 @@ -11156,15 +11156,29 @@ DirEntry_fetch_stat(DirEntry *self, int #ifdef MS_WINDOWS const wchar_t *path; - - path = PyUnicode_AsUnicode(self->path); - if (!path) - return NULL; + PyObject *unicode = self->path; + + if (PyBytes_Check(unicode)) { + unicode = PyUnicode_DecodeFSDefaultAndSize( + PyBytes_AS_STRING(unicode), + PyBytes_GET_SIZE(unicode)); + if (unicode == NULL) + return NULL; + } + else { + Py_INCREF(unicode); + } + path = PyUnicode_AsUnicode(unicode) + if (!path) { + Py_DECREF(unicode); + return NULL; + } if (follow_symlinks) result = win32_stat(path, &st); else result = win32_lstat(path, &st); + Py_DECREF(unicode); if (result != 0) { return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, @@ -11358,15 +11372,30 @@ DirEntry_inode(DirEntry *self) if (!self->got_file_index) { const wchar_t *path; struct _Py_stat_struct stat; - - path = PyUnicode_AsUnicode(self->path); - if (!path) + PyObject *unicode = self->path; + + if (PyBytes_Check(unicode)) { + unicode = PyUnicode_DecodeFSDefaultAndSize( + PyBytes_AS_STRING(unicode), + PyBytes_GET_SIZE(unicode)); + if (unicode == NULL) + return NULL; + } + else { + Py_INCREF(unicode); + } + path = PyUnicode_AsUnicode(unicode) + if (!path) { + Py_DECREF(unicode); return NULL; + } if (win32_lstat(path, &stat) != 0) { + Py_DECREF(unicode); return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, self->path); } + Py_DECREF(unicode); self->win32_file_index = stat.st_ino; self->got_file_index = 1;