Issue #13739: In os.listdir(), rewind the directory stream (so that listdir()

can be called again on the same open file).
This commit is contained in:
Charles-François Natali 2012-01-08 18:34:06 +01:00
parent 94f6fa62bf
commit 7546ad327d
2 changed files with 12 additions and 2 deletions

View File

@ -454,14 +454,22 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'fdlistdir'), "test needs posix.fdlistdir()")
def test_fdlistdir(self):
f = posix.open(posix.getcwd(), posix.O_RDONLY)
self.addCleanup(posix.close, f)
f1 = posix.dup(f)
self.assertEqual(
sorted(posix.listdir('.')),
sorted(posix.fdlistdir(f))
sorted(posix.fdlistdir(f1))
)
# Check the fd was closed by fdlistdir
with self.assertRaises(OSError) as ctx:
posix.close(f)
posix.close(f1)
self.assertEqual(ctx.exception.errno, errno.EBADF)
# Check that the fd offset was reset (issue #13739)
f2 = posix.dup(f)
self.assertEqual(
sorted(posix.listdir('.')),
sorted(posix.fdlistdir(f2))
)
def test_access(self):
if hasattr(posix, 'access'):

View File

@ -2906,6 +2906,7 @@ posix_fdlistdir(PyObject *self, PyObject *args)
break;
} else {
Py_BEGIN_ALLOW_THREADS
rewinddir(dirp);
closedir(dirp);
Py_END_ALLOW_THREADS
Py_DECREF(d);
@ -2929,6 +2930,7 @@ posix_fdlistdir(PyObject *self, PyObject *args)
Py_DECREF(v);
}
Py_BEGIN_ALLOW_THREADS
rewinddir(dirp);
closedir(dirp);
Py_END_ALLOW_THREADS