Should I close after popen??

François Pinard pinard at iro.umontreal.ca
Mon Apr 2 10:00:05 EDT 2001


[David Bolen]

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

> > [...] the documentation of (C)Python does not commit it on the
> > particular issue above [automatic closing as soon as the object becomes
> > non-referenced] [...]

> [...] Python (the language) does have a Language Reference [...]  section
> 3.1 talks about finalization and garbage collection as implementation
> specific, and even "strongly" recommends using methods such as close()
> on objects that involved external resources.

Yes, indeed, yet indirectly circumstantiated by "but since garbage
collection is not guaranteed to happen [...]".  In C-Python, currently,
such a guarantee exists.

> > About errors, shouldn't we just rely on the fact that they raise
> > exceptions, automatically?

> Except that if the finalization was postponed, that exception might
> crop up in the strangest place (assuming it was propagated up at all).

Of course.  Yet, in many cases, we know that the finalization is not going to
be postponed, and in such cases, I think we might avoid making it explicit.

> But aren't exceptions that occur as part of finalization suppressed?
> Assuming that the internal file type works similarly to Python
> code objects, where exceptions are definitely suppressed in __del__
> processing (see language reference section 3.3.1, under the Warning:
> note for __del__).

You are right.  Thanks for getting me straight on that one!

According to section 3.3.1, at __del__ time, either explicit or implicit,
exceptions will then not be processed as such, but nevertheless, a warning
is going to be printed to sys.stderr.  This would be enough for me.  My need,
here, is that an error on implicit close should not merely go un-noticed.

> The close that is done during object deallocation ignores any error
> codes, unlike that done in response to a close() method call.  This is
> true in at least 1.5.2 and 2.x which I had source around for.  So errors
> on implicit closes will be silently ignored.

This is much more serious.  So __del__ would not behave the same depending
whether it was called explicitely or implicitly?  I would like if this was
considered as a bug?  I would be happy with an error on implicit close being
reported to sys.stderr.  Just ignoring an error is not really Pythonesque...

> I think it's worth pointing out the risks in such dependence (note your
> assumption about exceptions), since it's up to each developer to make
> that risk/ease tradeoff.

Agreed.  Thanks for all your comments, David.  Accept my regards,

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




More information about the Python-list mailing list