Skip to content

Commit fd625c3

Browse files
Issue python#26117: The os.scandir() iterator now closes file descriptor not only
when the iteration is finished, but when it was failed with error.
2 parents d2962f1 + 988b9bc commit fd625c3

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ Core and Builtins
170170
Library
171171
-------
172172

173+
- Issue #26117: The os.scandir() iterator now closes file descriptor not only
174+
when the iteration is finished, but when it was failed with error.
175+
173176
- Issue #25949: __dict__ for an OrderedDict instance is now created only when
174177
needed.
175178

‎Modules/posixmodule.c‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11954,38 +11954,40 @@ ScandirIterator_iternext(ScandirIterator *iterator)
1195411954
{
1195511955
WIN32_FIND_DATAW *file_data = &iterator->file_data;
1195611956
BOOL success;
11957+
PyObject *entry;
1195711958

1195811959
/* Happens if the iterator is iterated twice */
11959-
if (iterator->handle == INVALID_HANDLE_VALUE) {
11960-
PyErr_SetNone(PyExc_StopIteration);
11960+
if (iterator->handle == INVALID_HANDLE_VALUE)
1196111961
return NULL;
11962-
}
1196311962

1196411963
while (1) {
1196511964
if (!iterator->first_time) {
1196611965
Py_BEGIN_ALLOW_THREADS
1196711966
success = FindNextFileW(iterator->handle, file_data);
1196811967
Py_END_ALLOW_THREADS
1196911968
if (!success) {
11969+
/* Error or no more files */
1197011970
if (GetLastError() != ERROR_NO_MORE_FILES)
11971-
return path_error(&iterator->path);
11972-
/* No more files found in directory, stop iterating */
11971+
path_error(&iterator->path);
1197311972
break;
1197411973
}
1197511974
}
1197611975
iterator->first_time = 0;
1197711976

1197811977
/* Skip over . and .. */
1197911978
if (wcscmp(file_data->cFileName, L".") != 0 &&
11980-
wcscmp(file_data->cFileName, L"..") != 0)
11981-
return DirEntry_from_find_data(&iterator->path, file_data);
11979+
wcscmp(file_data->cFileName, L"..") != 0) {
11980+
entry = DirEntry_from_find_data(&iterator->path, file_data);
11981+
if (!entry)
11982+
break;
11983+
return entry;
11984+
}
1198211985

1198311986
/* Loop till we get a non-dot directory or finish iterating */
1198411987
}
1198511988

11989+
/* Error or no more files */
1198611990
ScandirIterator_close(iterator);
11987-
11988-
PyErr_SetNone(PyExc_StopIteration);
1198911991
return NULL;
1199011992
}
1199111993

@@ -12010,12 +12012,11 @@ ScandirIterator_iternext(ScandirIterator *iterator)
1201012012
struct dirent *direntp;
1201112013
Py_ssize_t name_len;
1201212014
int is_dot;
12015+
PyObject *entry;
1201312016

1201412017
/* Happens if the iterator is iterated twice */
12015-
if (!iterator->dirp) {
12016-
PyErr_SetNone(PyExc_StopIteration);
12018+
if (!iterator->dirp)
1201712019
return NULL;
12018-
}
1201912020

1202012021
while (1) {
1202112022
errno = 0;
@@ -12024,9 +12025,9 @@ ScandirIterator_iternext(ScandirIterator *iterator)
1202412025
Py_END_ALLOW_THREADS
1202512026

1202612027
if (!direntp) {
12028+
/* Error or no more files */
1202712029
if (errno != 0)
12028-
return path_error(&iterator->path);
12029-
/* No more files found in directory, stop iterating */
12030+
path_error(&iterator->path);
1203012031
break;
1203112032
}
1203212033

@@ -12035,20 +12036,22 @@ ScandirIterator_iternext(ScandirIterator *iterator)
1203512036
is_dot = direntp->d_name[0] == '.' &&
1203612037
(name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
1203712038
if (!is_dot) {
12038-
return DirEntry_from_posix_info(&iterator->path, direntp->d_name,
12039+
entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
1203912040
name_len, direntp->d_ino
1204012041
#ifdef HAVE_DIRENT_D_TYPE
1204112042
, direntp->d_type
1204212043
#endif
1204312044
);
12045+
if (!entry)
12046+
break;
12047+
return entry;
1204412048
}
1204512049

1204612050
/* Loop till we get a non-dot directory or finish iterating */
1204712051
}
1204812052

12053+
/* Error or no more files */
1204912054
ScandirIterator_close(iterator);
12050-
12051-
PyErr_SetNone(PyExc_StopIteration);
1205212055
return NULL;
1205312056
}
1205412057

0 commit comments

Comments
 (0)