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

Kristján Valur Jónsson kristjan at ccpgames.com
Fri Dec 19 11:56:46 CET 2008



> > try:
> >                 self.close()
> > except:
> >                 try:
> >                                if not sys.exiting(): raise
> >                 except:
> >                                pass
> >
> >
> > Or better yet, do as we have done often here, just catch the particular
> > problem that occurs during shutdown, most often name error:
> >
> > try:
> >                 self.close()
> > except (AttributeError, NameError):
> >                 pass
>
> From: Amaury Forgeot d'Arc [mailto:amauryfa at gmail.com]
> I suggest "except Exception": SystemExit and KeyboardInterrupt inherit
> from BaseException, not from Exceptions
> And close() is likely to raise IOErrors.

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.

Kristján


More information about the Python-Dev mailing list