Issue #13739: In os.listdir(), rewind the directory stream (so that listdir()
authorCharles-François Natali <neologix@free.fr>
Sun, 8 Jan 2012 17:34:06 +0000 (18:34 +0100)
committerCharles-François Natali <neologix@free.fr>
Sun, 8 Jan 2012 17:34:06 +0000 (18:34 +0100)
can be called again on the same open file).

Lib/test/test_posix.py
Modules/posixmodule.c

index 3c863a7c837bc17b6865735e09675db6981fe868..07755b983acad52f7ede95b58ddd64b954d462dd 100644 (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'):
index ea3665d7e6ad9853534868184bb955f34f5af2c6..3c723cf28837f0a0026de7a472fd3419606bd3c1 100644 (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