summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCharles-François Natali <neologix@free.fr>2012-01-10 19:25:09 (GMT)
committerCharles-François Natali <neologix@free.fr>2012-01-10 19:25:09 (GMT)
commit76961faaa0323580caac8068848c33b7aeec13ee (patch)
tree3ee6487ceb4e3c4674aabd0f92d7e06a1a09c09e
parentbda7a80194849eb2797c8bbffdbbaccad36d4583 (diff)
downloadcpython-76961faaa0323580caac8068848c33b7aeec13ee.zip
cpython-76961faaa0323580caac8068848c33b7aeec13ee.tar.gz
cpython-76961faaa0323580caac8068848c33b7aeec13ee.tar.bz2
Issue #13757: Change os.fdlistdir() so that it duplicates the passed file
descriptor (instead of closing it).
-rw-r--r--Doc/library/os.rst2
-rw-r--r--Lib/test/test_posix.py10
-rw-r--r--Modules/posixmodule.c7
3 files changed, 8 insertions, 11 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 53f5025..0c0d681 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -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.
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 07755b9..f8c6baa 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -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):
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 3c723cf..a71d2e6 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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