Message 150877 - Python tracker (original) (raw)

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)

print(os.fdlistdir(os.dup(fd))) print(os.fdlistdir(os.dup(fd)))

os.close(fd) $ ./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.