Skip to content

Commit 6b3b46d

Browse files
committed
Refactor to share code and eliminate duplication between Windows & POSIX platforms
1 parent d374264 commit 6b3b46d

File tree

1 file changed

+42
-62
lines changed

1 file changed

+42
-62
lines changed

Modules/posixmodule.c

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4430,6 +4430,25 @@ os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
44304430
}
44314431
#endif
44324432

4433+
#ifdef MS_WINDOWS
4434+
4435+
static int
4436+
_is_dot_filename(WIN32_FIND_DATAW *entry)
4437+
{
4438+
return wcscmp(entry->cFileName, L".") == 0 || wcscmp(entry->cFileName, L"..") == 0;
4439+
}
4440+
4441+
#else
4442+
4443+
static int
4444+
_is_dot_filename(struct dirent *entry)
4445+
{
4446+
Py_ssize_t name_len = NAMLEN(entry);
4447+
return entry->d_name[0] == '.' &&
4448+
(name_len == 1 || (entry->d_name[1] == '.' && name_len == 2));
4449+
}
4450+
4451+
#endif
44334452

44344453
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
44354454
static PyObject *
@@ -4484,8 +4503,7 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
44844503
}
44854504
do {
44864505
/* Skip over . and .. */
4487-
if (wcscmp(wFileData.cFileName, L".") != 0 &&
4488-
wcscmp(wFileData.cFileName, L"..") != 0) {
4506+
if (!_is_dot_filename(&wFileData)) {
44894507
v = PyUnicode_FromWideChar(wFileData.cFileName,
44904508
wcslen(wFileData.cFileName));
44914509
if (return_bytes && v) {
@@ -16020,7 +16038,7 @@ join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
1602016038
}
1602116039

1602216040
static PyObject *
16023-
DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
16041+
DirEntry_from_os(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
1602416042
{
1602516043
DirEntry *entry;
1602616044
BY_HANDLE_FILE_INFORMATION file_info;
@@ -16110,15 +16128,12 @@ join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t fil
1611016128
}
1611116129

1611216130
static PyObject *
16113-
DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
16114-
Py_ssize_t name_len, ino_t d_ino
16115-
#ifdef HAVE_DIRENT_D_TYPE
16116-
, unsigned char d_type
16117-
#endif
16118-
)
16131+
DirEntry_from_os(PyObject *module, path_t *path, struct dirent *direntp)
1611916132
{
1612016133
DirEntry *entry;
1612116134
char *joined_path;
16135+
const char *name = direntp->d_name;
16136+
Py_ssize_t name_len = NAMLEN(direntp);
1612216137

1612316138
PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
1612416139
entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
@@ -16161,9 +16176,9 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
1616116176
goto error;
1616216177

1616316178
#ifdef HAVE_DIRENT_D_TYPE
16164-
entry->d_type = d_type;
16179+
entry->d_type = direntp->d_type;
1616516180
#endif
16166-
entry->d_ino = d_ino;
16181+
entry->d_ino = direntp->d_ino;
1616716182

1616816183
return (PyObject *)entry;
1616916184

@@ -16245,33 +16260,6 @@ ScandirIterator_nextdirentry(ScandirIterator *iterator, WIN32_FIND_DATAW *file_d
1624516260
return has_result;
1624616261
}
1624716262

16248-
static PyObject *
16249-
ScandirIterator_iternext(PyObject *op)
16250-
{
16251-
ScandirIterator *iterator = ScandirIterator_CAST(op);
16252-
WIN32_FIND_DATAW file_data;
16253-
PyObject *entry;
16254-
16255-
while (ScandirIterator_nextdirentry(iterator, &file_data)) {
16256-
/* Skip over . and .. */
16257-
if (wcscmp(file_data.cFileName, L".") != 0 &&
16258-
wcscmp(file_data.cFileName, L"..") != 0)
16259-
{
16260-
PyObject *module = PyType_GetModule(Py_TYPE(iterator));
16261-
entry = DirEntry_from_find_data(module, &iterator->path, &file_data);
16262-
if (!entry)
16263-
break;
16264-
return entry;
16265-
}
16266-
16267-
/* Loop till we get a non-dot directory or finish iterating */
16268-
}
16269-
16270-
/* Already closed, error, or no more files */
16271-
ScandirIterator_closedir(iterator);
16272-
return NULL;
16273-
}
16274-
1627516263
#else /* POSIX */
1627616264

1627716265
static int
@@ -16327,44 +16315,36 @@ ScandirIterator_nextdirentry(ScandirIterator *iterator, struct dirent *direntp)
1632716315
return result != NULL;
1632816316
}
1632916317

16318+
#endif
16319+
1633016320
static PyObject *
1633116321
ScandirIterator_iternext(PyObject *op)
1633216322
{
16323+
#ifdef MS_WINDOWS
16324+
WIN32_FIND_DATAW dirent;
16325+
#else
16326+
struct dirent dirent;
16327+
#endif
1633316328
ScandirIterator *iterator = ScandirIterator_CAST(op);
16334-
struct dirent direntp;
16335-
Py_ssize_t name_len;
16336-
int is_dot;
16337-
PyObject *entry;
1633816329

16339-
while ((ScandirIterator_nextdirentry(iterator, &direntp))) {
16330+
while ((ScandirIterator_nextdirentry(iterator, &dirent))) {
16331+
1634016332
/* Skip over . and .. */
16341-
name_len = NAMLEN(&direntp);
16342-
is_dot = direntp.d_name[0] == '.' &&
16343-
(name_len == 1 || (direntp.d_name[1] == '.' && name_len == 2));
16344-
if (!is_dot) {
16345-
PyObject *module = PyType_GetModule(Py_TYPE(iterator));
16346-
entry = DirEntry_from_posix_info(module,
16347-
&iterator->path, direntp.d_name,
16348-
name_len, direntp.d_ino
16349-
#ifdef HAVE_DIRENT_D_TYPE
16350-
, direntp.d_type
16351-
#endif
16352-
);
16353-
if (!entry)
16354-
break;
16355-
return entry;
16356-
}
16333+
if (_is_dot_filename(&dirent))
16334+
continue;
1635716335

16358-
/* Loop till we get a non-dot directory or finish iterating */
16336+
PyObject *module = PyType_GetModule(Py_TYPE(iterator));
16337+
PyObject *entry = DirEntry_from_os(module, &iterator->path, &dirent);
16338+
if (!entry)
16339+
break;
16340+
return entry;
1635916341
}
1636016342

1636116343
/* Already closed, error, or no more files */
1636216344
ScandirIterator_closedir(iterator);
1636316345
return NULL;
1636416346
}
1636516347

16366-
#endif
16367-
1636816348
static PyObject *
1636916349
ScandirIterator_close(PyObject *op, PyObject *Py_UNUSED(dummy))
1637016350
{

0 commit comments

Comments
 (0)