[Python-ideas] Context helper for new os.*at functions

Christian Heimes lists at cheimes.de
Sun Jun 17 03:34:13 CEST 2012


Am 17.06.2012 02:46, schrieb Guido van Rossum:
> Hmm... Isn't Larry Hastings working on replacing the separate
> functions with an api where you pass an 'fd=...' argument to the
> non-at function?

Oh, is he? I didn't know that. Indeed, it sounds like a good approach.

Users must still handle the fd correctly and make sure they open a
directory. Linux's man(2) open warns about possibility of
denial-of-service attempts for wrong fds. Linux has O_DIRECTORY for this
purpose. On other OSes users should do a stat() call in front, which is
open for race conditions but still better than getting stuck in a FIFO.

I could modify the wrapper a bit to make the wrapper useful for the new API:

class atcontext:
   def fileno(self):
       # for PyObject_AsFileDescriptor()
       return self.dirfd


with atcontext("/etc") as at:
    os.open("fstab", os.O_RDONLY, fd=at)

Christian



More information about the Python-ideas mailing list