[New-bugs-announce] [issue13739] os.fdlistdir() is not idempotent

Charles-François Natali report at bugs.python.org
Sun Jan 8 17:26:26 CET 2012

New submission from Charles-François Natali <neologix at free.fr>:

After a call to fdlistdir(), another call to fdlistdir() on the same file handle (but using a different FD, since the FD passed to fdlistdir() is closed) will return an empty list:

$ cat ~/test_fdlistdir.py 
import os
import sys

fd = os.open(sys.argv[1], os.O_RDONLY)


$ ./python ~/test_fdlistdir.py /tmp/
['pulse-B1FebW397VI5', 'ksocket-kdm', 'etc', 'kde-cf', 'ksocket-cf', 'test_posix.py', '.X0-lock', 'kde-kdm', 'akonadi-cf.k6y52j', 'ssh-iSFleEAS1243', '.ICE-unix', '.X11-unix']

That's because fdopendir()/readdir doesn't reset the FD offset.
It's documented by POSIX:
The file offset associated with the file descriptor at the time of the call determines which entries are returned.

That's rather suprising (I got bitten while trying to write a test for #13734).
I see two options:
1. rewind the directory stream in fdlistdir()
2. document this

Here's a patch for option 1.

components: None
files: fdlistdir.diff
keywords: patch
messages: 150877
nosy: neologix, pitrou, rosslagerwall
priority: normal
severity: normal
status: open
title: os.fdlistdir() is not idempotent
type: behavior
versions: Python 3.3
Added file: http://bugs.python.org/file24178/fdlistdir.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list