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

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


Greetings!

Yesterday, I committed revision r67843 to py3k.
Re-enablign the windows CRT runtime checks showed me that close() was beeing called with an invalid file descriptor.
Now, the problem was was in tokenizer.c, but the reason this wasn't caught earlier was,

1)      Incorrect error checking for close() in _fileio.c, which I fixed, and

2)      Line 384 in io.py, where all exceptions are caught for self.close().

Fixing 1 and patching 2 would bring the problem to light when running the test_imp.py part of the testsuite and, indeed, applying the fix to tokenizer.c would then remove it again.

I am a bit worried about 2) thoug.  I didn't modify that, but having a catch all clause just to be clean on system exit seems shaky to me.   I wonder, is there a way to make such behaviour, if it is indeed necessary, just to be active when exit is in progress?

Something like:
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


What do you think?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20081219/d4167657/attachment-0001.htm>


More information about the Python-Dev mailing list