[Python-Dev] try/except in io.py

Nick Coghlan ncoghlan at gmail.com
Fri Dec 19 14:50:37 CET 2008


Kristján Valur Jónsson wrote:
> Ah, but that is not what the intent is to guard agains, according the
> comments. During exit, modules have been deleted and all sorts of
> things have gone away. It is therefore likely that code that executes
> during exit will encounter NameErrors (when a module is being cleaned
> up and its globals removed) And AttributeErrors. ImportErrors too, in
> fact.
> 
> It would be good to see the actual repro case that caused this to be
> added in the first place, so that we could selectively catch those
> errors.

Generally speaking, close() and __delete__() methods that can be invoked
during interpreter shutdown should avoid referencing module globals at
all. Necessary globals (including members of other modules) should
either be cached on the relevant class or captured in a closure.

Now, it may be that the relevant close() method in io.py touches too
much code for that to be practical, but it certainly isn't the case in
general that encountering Name/Attribute/ImportError during shutdown is
inevitable.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list