[Python-ideas] Context helper for new os.*at functions (original) (raw)

Guido van Rossum guido at python.org
Sun Jun 17 02:46:15 CEST 2012


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?

On Sat, Jun 16, 2012 at 5:13 PM, Christian Heimes <lists at cheimes.de> wrote:

Hello,

Python 3.3 has got new wrappers for the 'at' variants of low level functions, for example os.openat(). The 'at' variants work like their brothers and sisters with one exception. The first argument must be a file descriptor of a directory. The fd is used to calculate the absolute path instead of the current working directory. File descriptors are harder to manage than files because a fd isnt't automatically closed when it gets out of scope. I've written a small wrapper that takes care of the details. It also ensures that only directories are opened. Example: with atcontext("/etc") as at:  print(at.open)  # functools.partial(, 3)  f = at.open("fstab", os.ORDONLY)  print(os.read(f, 50))  os.close(f) Code: http://pastebin.com/J4SLjB6k The code calculates the name and creates dynamic wrapper with functool.partial. This may not be desired if the wrapper is added to the os module. I could add explicit methods and generate the doc strings from the methods' doc strings. def docfix(func):  name = func.name  nameat = name + "at"  doc = getattr(os, nameat).doc  func.doc = doc.replace("{}(dirfd, ".format(nameat), "{}(".format(name))  return func class atcontext:  ...  @docfix  def open(self, *args):  return self.openat(self.dirf, *args)

How do you like my proposal? Christian


Python-ideas mailing list Python-ideas at python.org http://mail.python.org/mailman/listinfo/python-ideas

-- --Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list