[Python-Dev] IO module improvements

Christian Heimes lists at cheimes.de
Sat Feb 6 00:47:15 CET 2010


Guido van Rossum wrote:
> You're right, I didn't see the OP's comma. :-)
> 
> I don't think this can be helped though -- I really don't want open()
> to be slowed down or complicated by an attempt to do path
> manipulation. If this matters to the app author they should use
> os.path.abspath() or os.path.realpath() or whatever before calling
> open().

I had the idea to add a property that returns the file name based on the
file descriptor. However there isn't a plain way to lookup the file
based on the fd on POSIX OSes. fstat() returns only the inode and
device. The combination of inode + device references 0 to n files due to
anonymous files and hard links. On POSIX OSes with a /proc file systems
it's possible to do a reverse lookup by (ab)using /proc/self/fd/, but
that's a hack.

>>> import os
>>> f = open("/etc/passwd")
>>> fd = f.fileno()
>>> os.readlink("/proc/self/fds/%i" % fd)
'/etc/passwd'

On Windows it's possible to get the file name from the handle with
GetFileInformationByHandleEx().

This doesn't strike me as a feasible options ...

Christian


More information about the Python-Dev mailing list