Commits

Anonymous committed 6d395ed

Issue #13757: Change os.fdlistdir() so that it duplicates the passed file
descriptor (instead of closing it).

  • Participants
  • Parent commits eb028b3

Comments (0)

Files changed (3)

File Doc/library/os.rst

 .. 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.
 

File Lib/test/test_posix.py

     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):

File Modules/posixmodule.c

 #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)
     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