(Done as separate patch from trunk as the code has diverged quite a bit.)
Core and Builtins
-----------------
+- Issue #17899: Fix rare file descriptor leak in os.listdir().
+
- Issue #18552: Check return value of PyArena_AddPyObject() in
obj2ast_object().
path_t path;
PyObject *list = NULL;
static char *keywords[] = {"path", NULL};
+#ifdef HAVE_FDOPENDIR
int fd = -1;
+#endif /* HAVE_FDOPENDIR */
#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
PyObject *v;
if (dirp == NULL) {
list = path_error("listdir", &path);
+#ifdef HAVE_FDOPENDIR
+ if (fd != -1) {
+ Py_BEGIN_ALLOW_THREADS
+ close(fd);
+ Py_END_ALLOW_THREADS
+ }
+#endif /* HAVE_FDOPENDIR */
goto exit;
}
if ((list = PyList_New(0)) == NULL) {
exit:
if (dirp != NULL) {
Py_BEGIN_ALLOW_THREADS
+#ifdef HAVE_FDOPENDIR
if (fd > -1)
rewinddir(dirp);
+#endif /* HAVE_FDOPENDIR */
closedir(dirp);
Py_END_ALLOW_THREADS
}