Should I close after popen??

François Pinard pinard at iro.umontreal.ca
Fri Mar 30 15:29:14 EST 2001


[David Bolen]

> pinard at iro.umontreal.ca (=?iso-8859-1?q?Fran=E7ois?= Pinard) writes:

> > I would completely agree with the above for someone who wants to write
> > Jython, or write Python that could be quickly turned into Jython.
> > But if you stick with Python, you can safely rely on the fact that a
> > file is automagically closed as soon as you do not refer to it anymore.

> I'd probably change Python in that sentence to CPython, since it's
> an implementation issue and not a language one (the language makes no
> such guarantees).

As it was pointed on this list quite a while ago, Python has no language
definition per itself.  It is rather defined by the documentation of its
main implementation.  It is true that the documentation of (C)Python does
not commit it on the particular issue above, and this is why I checked
with this list before starting to firmly rely on this feature, in real code.

About Jython being Python or not, or JPython being CPython (:-), the
context usually makes it clear what Python means.  I remember that I was
surprised to read that JPython, on its Web pages, had strong opinions about
what Python did rightly or wrongly, or what it should have done otherwise.
I got from there that there is definite distance.  I did not check these
pages recently, at least not since JPython has been renamed Jython.

In the meantime, I think we might continue to safely define Python as per
(C)Python, rather than by the intersection of Jython and Python.  I do know
that if I ever move from Python to Jython, I'll have to revise many things
in my code, but until then, I prefer to be as clean and legible as possible.

> > Code like:
> >     open(logfile, 'a').write('Done!\n')
> > or maybe:
> >     for line in open(logfile).readlines():
> >         process(line)
> > is very legible, and would rather be uselessly encumbered with explicit
> > closes and intermediate variables.

> I [...] would caution that [...] you have no guarantees that your data
> actually got to the logfile without doing the close and possibly handling
> any errors there.

The close *is* properly done.  This is guaranteed by the fact I do not
keep any reference to the file.  On the first example, the close is done
before proceeding with the next Python instruction.  On the second example,
the close is done even before the first "process(line)" is executed.

About errors, shouldn't we just rely on the fact that they raise exceptions,
automatically?  One of the beauties of Python, compared to C or Perl, is that
errors are always trapped and handled by the system, when not by the user.

So, we have all guarantees that the data got written to the logfile, given
the script does not fail with an I/O error.  Errors on implicit close will
also be properly detected.  So, there is no need to feel uncomfortable! :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list