diff -r 27f55ff12f41 Modules/posixmodule.c --- a/Modules/posixmodule.c Sun Jun 09 21:08:05 2013 +0300 +++ b/Modules/posixmodule.c Sun Jun 09 19:30:50 2013 -0700 @@ -3443,7 +3443,9 @@ 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; @@ -3731,6 +3733,13 @@ } if (dirp == NULL) { +#ifdef HAVE_FDOPENDIR + if (fd != -1) { + Py_BEGIN_ALLOW_THREADS + close(fd); + Py_END_ALLOW_THREADS + } +#endif /* HAVE_FDOPENDIR */ list = path_error("listdir", &path); goto exit; } @@ -3774,8 +3783,10 @@ 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 }