[issue13824] argparse.FileType opens a file and never closes it
paul j3
report at bugs.python.org
Wed Sep 25 09:15:01 CEST 2013
paul j3 added the comment:
An alternative way to delay the file opening, is to return an instance that has a `filename` attribute, and has an `open` method. This can be compactly added to the `FileContext` that I defined in the previous patch. The `FileContext` provides the `_ostest` function (testing using os.access), and wrapper for stdin/out.
class FileOpener(argparse.FileContext):
# delayed FileType; alt to use of partial()
# sample use:
# with args.input.open() as f: f.read()
def __call__(self, string):
string = self._ostest(string)
self.filename = string
return self
def open(self):
return self.__delay_call__(self.filename)()
file = property(open, None, None, 'open file property')
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13824>
_______________________________________
More information about the Python-bugs-list
mailing list