'generator ignored GeneratorExit''
Charles Hixson
charleshixsn at earthlink.net
Sun Oct 21 01:41:37 CEST 2012
On 10/20/2012 04:28 PM, Ian Kelly wrote:
> On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
> <charleshixsn at earthlink.net> wrote:
>> If I run the following code in the same module, it works correctly, but if I
>> import it I get the message:
>> Exception RuntimeError: 'generator ignored GeneratorExit' in<generator
>> object getNxtFile at 0x7f932f884f50> ignored
>>
>> def getNxtFile (startDir, exts = ["txt", "utf8"]):
>> try:
>> for path in getNxtPath (startDir, exts):
>> try:
>> fil = open (path, encoding = "utf-8-sig")
>> yield fil
>> except:
>> print ("Could not read: ", path)
>> except GeneratorExit:
>> raise StopIteration
>>
>> The message appears to be purely informational, but I *would* like to fix
>> whatever problem it's reporting, and none of the changes that I've tried
>> have worked. What *should* I be doing?
> 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.
>
> Also, you don't need to explicitly catch GeneratorExit just to raise
> StopIteration. The generator will normally stop on GeneratorExit,
> provided the exception is actually able to propagate up.
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.
--
Charles Hixson
More information about the Python-list
mailing list