]> granicus.if.org Git - python/commitdiff
Issue #13757: Change os.fdlistdir() so that it duplicates the passed file
authorCharles-François Natali <neologix@free.fr>
Tue, 10 Jan 2012 19:25:09 +0000 (20:25 +0100)
committerCharles-François Natali <neologix@free.fr>
Tue, 10 Jan 2012 19:25:09 +0000 (20:25 +0100)
descriptor (instead of closing it).

Doc/library/os.rst
Lib/test/test_posix.py
Modules/posixmodule.c

index 53f5025216d4a2c9d46d3c971bec4f8260ef1b05..0c0d681a9f3ff864c0c47d7ef1b392071ff21a36 100644 (file)
@@ -772,7 +772,7 @@ as internal buffering of data.
 .. function:: fdlistdir(fd)
 
    Like :func:`listdir`, but uses a file descriptor instead and always returns
-   strings.  After execution of this function, *fd* will be closed.
+   strings.
 
    Availability: Unix.
 
index 07755b983acad52f7ede95b58ddd64b954d462dd..f8c6baa08306a7c841bca0d00c9c1c317fb96e82 100644 (file)
@@ -455,20 +455,14 @@ class PosixTester(unittest.TestCase):
     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(f1))
+            sorted(posix.fdlistdir(f))
             )
-        # Check the fd was closed by fdlistdir
-        with self.assertRaises(OSError) as ctx:
-            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))
+            sorted(posix.fdlistdir(f))
             )
 
     def test_access(self):
index 3c723cf28837f0a0026de7a472fd3419606bd3c1..a71d2e6426fac003740a325ebd8aa8bee640fabb 100644 (file)
@@ -2869,8 +2869,7 @@ posix_listdir(PyObject *self, PyObject *args)
 #ifdef HAVE_FDOPENDIR
 PyDoc_STRVAR(posix_fdlistdir__doc__,
 "fdlistdir(fd) -> list_of_strings\n\n\
-Like listdir(), but uses a file descriptor instead.\n\
-After succesful execution of this function, fd will be closed.");
+Like listdir(), but uses a file descriptor instead.");
 
 static PyObject *
 posix_fdlistdir(PyObject *self, PyObject *args)
@@ -2883,6 +2882,10 @@ posix_fdlistdir(PyObject *self, PyObject *args)
     errno = 0;
     if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
         return NULL;
+    /* closedir() closes the FD, so we duplicate it */
+    fd = dup(fd);
+    if (fd < 0)
+        return posix_error();
     Py_BEGIN_ALLOW_THREADS
     dirp = fdopendir(fd);
     Py_END_ALLOW_THREADS