[docs] Remove support of bytes paths in os.scandir() (issue 27998)
storchaka+cpython at gmail.com
storchaka+cpython at gmail.com
Sat Oct 8 12:10:50 EDT 2016
Reviewers: eryksun,
http://bugs.python.org/review/27998/diff/18802/Modules/posixmodule.c
File Modules/posixmodule.c (right):
http://bugs.python.org/review/27998/diff/18802/Modules/posixmodule.c#newcode11159
Modules/posixmodule.c:11159: PyObject *unicode = self->path;
On 2016/10/08 16:08:04, eryksun wrote:
> How about consolidating this function as follows?
>
> static PyObject *
> path_object_error(PyObject *path)
> {
> #ifdef MS_WINDOWS
> return PyErr_SetExcFromWindowsErrWithFilenameObject(
> PyExc_OSError, 0, path);
> #else
> return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
path);
> #endif
> }
>
> static PyObject *
> DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
> {
> int result;
> STRUCT_STAT st;
> PyObject *ub;
>
> #ifdef MS_WINDOWS
> if (PyUnicode_FSDecoder(self->path, &ub)) {
> const wchar_t *path = PyUnicode_AsUnicode(ub);
> #else /* POSIX */
> if (PyUnicode_FSConverter(self->path, &ub)) {
> const char *path = PyBytes_AS_STRING(ub);
> #endif
> if (follow_symlinks)
> result = STAT(path, &st);
> else
> result = LSTAT(path, &st);
> Py_DECREF(ub);
> } else
> return NULL;
>
> if (result != 0)
> return path_object_error(self->path);
>
> return _pystat_fromstructstat(&st);
> }
This looks interesting. Could you provide a patch?
Please review this at http://bugs.python.org/review/27998/
Affected files:
Modules/posixmodule.c
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;
More information about the docs
mailing list