[issue18644] Got ResourceWarning: unclosed file when using test function from formatter module
Eric V. Smith
report at bugs.python.org
Mon Jan 5 09:29:14 CET 2015
Eric V. Smith added the comment:
Not that I think it's worth changing for this case, but I find code like this better written as:
if some_test:
fl = contextlib.closing(open(sys.argv[1]))
else:
fl = sys.stdin
with fl as fl:
do_stuff(fl)
This way you don't need another test, the close isn't far away from the open, and you save some lines of boilerplate.
Or, if "with fl as fl" bothers you:
with sys.stdout if some_test else contextlib.closing(open(sys.argv[1])) as fl:
do_stuff(fl)
I don't recommend that, though.
In any event, thanks for the fix!
----------
nosy: +eric.smith
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18644>
_______________________________________
More information about the Python-bugs-list
mailing list