'generator ignored GeneratorExit''
cs at zip.com.au
Sun Oct 21 02:11:52 CEST 2012
On 20Oct2012 16:41, Charles Hixson <charleshixsn at earthlink.net> wrote:
| On 10/20/2012 04:28 PM, Ian Kelly wrote:
| > On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
| >> try:
| >> fil = open (path, encoding = "utf-8-sig")
| >> yield fil
| >> except:
| > The bare except is probably catching the GeneratorExit exception and
| > swallowing it. Try catching a more specific exception like OSError or
| > even just Exception instead. [...]
| Thank you. That was, indeed the problem. Removing all the try ...
| excepts made it work on my test case. Now I've got to figure out what
| to catch in case it's not a utf8 file. I guess that I'll hope that
| IOError will work, as nothing else sounds reasonable. It's general
| enough that it ought to work.
No no no!
Feed it a bad file and _watch_ what exception it throws.
IIRC, it should throw a very specific UnicodeDecodingError; catch that
and only that.
Always try to catch the most specific exception possible or you're back in
the same zone your bare except was in - catching things other than the
specific problem you're handling.
"General enough that it ought to work" is the wrong approach; "Exception"
is general enough, and clearly the wrong approach. Catch exactly what
you have to catch. Catching the wrong thing (another exception type) and
_mishandling_ it (because you're treating it like a decoding error) is
not "working", it is "not working".
If you don't know the exception, let it bubble up and escape - Python
will report it (and abort, of course).
Performing random acts of moral ambiguity.
- Jeff Miller <jxmill2 at gonix.com>
More information about the Python-list