Skip to content

Commit 2674bc7

Browse files
Issue #27998: Fixed bytes path support in os.scandir() on Windows.
Patch by Eryk Sun.
1 parent 3e49264 commit 2674bc7

2 files changed

Lines changed: 46 additions & 46 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Core and Builtins
6262
Library
6363
-------
6464

65+
- Issue #27998: Fixed bytes path support in os.scandir() on Windows.
66+
Patch by Eryk Sun.
67+
6568
- Issue #28317: The disassembler now decodes FORMAT_VALUE argument.
6669

6770
- Issue #26293: Fixed writing ZIP files that starts not from the start of the

Modules/posixmodule.c

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,29 +1337,39 @@ win32_error_object(const char* function, PyObject* filename)
13371337
#endif /* MS_WINDOWS */
13381338

13391339
static PyObject *
1340-
path_error(path_t *path)
1340+
path_object_error(PyObject *path)
13411341
{
13421342
#ifdef MS_WINDOWS
1343-
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
1344-
0, path->object);
1343+
return PyErr_SetExcFromWindowsErrWithFilenameObject(
1344+
PyExc_OSError, 0, path);
13451345
#else
1346-
return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
1346+
return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
13471347
#endif
13481348
}
13491349

1350-
13511350
static PyObject *
1352-
path_error2(path_t *path, path_t *path2)
1351+
path_object_error2(PyObject *path, PyObject *path2)
13531352
{
13541353
#ifdef MS_WINDOWS
1355-
return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError,
1356-
0, path->object, path2->object);
1354+
return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1355+
PyExc_OSError, 0, path, path2);
13571356
#else
1358-
return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError,
1359-
path->object, path2->object);
1357+
return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
13601358
#endif
13611359
}
13621360

1361+
static PyObject *
1362+
path_error(path_t *path)
1363+
{
1364+
return path_object_error(path->object);
1365+
}
1366+
1367+
static PyObject *
1368+
path_error2(path_t *path, path_t *path2)
1369+
{
1370+
return path_object_error2(path->object, path2->object);
1371+
}
1372+
13631373

13641374
/* POSIX generic methods */
13651375

@@ -11152,41 +11162,26 @@ static PyObject *
1115211162
DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
1115311163
{
1115411164
int result;
11155-
struct _Py_stat_struct st;
11165+
STRUCT_STAT st;
11166+
PyObject *ub;
1115611167

1115711168
#ifdef MS_WINDOWS
11158-
const wchar_t *path;
11159-
11160-
path = PyUnicode_AsUnicode(self->path);
11161-
if (!path)
11162-
return NULL;
11163-
11164-
if (follow_symlinks)
11165-
result = win32_stat(path, &st);
11166-
else
11167-
result = win32_lstat(path, &st);
11168-
11169-
if (result != 0) {
11170-
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11171-
0, self->path);
11172-
}
11169+
if (PyUnicode_FSDecoder(self->path, &ub)) {
11170+
const wchar_t *path = PyUnicode_AsUnicode(ub);
1117311171
#else /* POSIX */
11174-
PyObject *bytes;
11175-
const char *path;
11176-
11177-
if (!PyUnicode_FSConverter(self->path, &bytes))
11172+
if (PyUnicode_FSConverter(self->path, &ub)) {
11173+
const char *path = PyBytes_AS_STRING(ub);
11174+
#endif
11175+
if (follow_symlinks)
11176+
result = STAT(path, &st);
11177+
else
11178+
result = LSTAT(path, &st);
11179+
Py_DECREF(ub);
11180+
} else
1117811181
return NULL;
11179-
path = PyBytes_AS_STRING(bytes);
11180-
11181-
if (follow_symlinks)
11182-
result = STAT(path, &st);
11183-
else
11184-
result = LSTAT(path, &st);
11185-
Py_DECREF(bytes);
1118611182

1118711183
if (result != 0)
11188-
return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path);
11189-
#endif
11184+
return path_object_error(self->path);
1119011185

1119111186
return _pystat_fromstructstat(&st);
1119211187
}
@@ -11356,17 +11351,19 @@ DirEntry_inode(DirEntry *self)
1135611351
{
1135711352
#ifdef MS_WINDOWS
1135811353
if (!self->got_file_index) {
11354+
PyObject *unicode;
1135911355
const wchar_t *path;
11360-
struct _Py_stat_struct stat;
11356+
STRUCT_STAT stat;
11357+
int result;
1136111358

11362-
path = PyUnicode_AsUnicode(self->path);
11363-
if (!path)
11359+
if (!PyUnicode_FSDecoder(self->path, &unicode))
1136411360
return NULL;
11361+
path = PyUnicode_AsUnicode(unicode);
11362+
result = LSTAT(path, &stat);
11363+
Py_DECREF(unicode);
1136511364

11366-
if (win32_lstat(path, &stat) != 0) {
11367-
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11368-
0, self->path);
11369-
}
11365+
if (result != 0)
11366+
return path_object_error(self->path);
1137011367

1137111368
self->win32_file_index = stat.st_ino;
1137211369
self->got_file_index = 1;

0 commit comments

Comments
 (0)