diff -r 56ca8eb5207a Modules/posixmodule.c --- a/Modules/posixmodule.c Fri May 03 10:57:08 2013 -0400 +++ b/Modules/posixmodule.c Fri May 03 17:17:10 2013 -0700 @@ -3409,7 +3409,9 @@ static PyObject * _posix_listdir(path_t *path, PyObject *list) { +#ifdef HAVE_FDOPENDIR int fd = -1; +#endif PyObject *v; DIR *dirp = NULL; @@ -3455,6 +3457,10 @@ } if (dirp == NULL) { +#ifdef HAVE_FDOPENDIR + if (fd != -1) + close(fd); +#endif list = path_error(path); goto exit; } @@ -3498,8 +3504,10 @@ exit: if (dirp != NULL) { Py_BEGIN_ALLOW_THREADS +#ifdef HAVE_FDOPENDIR if (fd > -1) rewinddir(dirp); +#endif closedir(dirp); Py_END_ALLOW_THREADS }