[Python-projects] astng: generator ignored GeneratorExit

Maarten ter Huurne maarten at treewalker.org
Mon May 12 12:23:31 CEST 2008


Hi,

When I run pylint on the following program:
===
print {}[str(0) + ''].a
===

I see this message:
===
Exception exceptions.RuntimeError: 'generator ignored GeneratorExit' in 
<generator object at 0x835096c> ignored
===

Versions used:
===
pylint 0.14.0,
astng 0.17.2, common 0.31.0
Python 2.5.1 (r251:54863, Jan 10 2008, 18:01:57)
===

Python 2.5 is essential to trigger this issue, since GeneratorExit was 
introduced in that release.

The problem seems to be that _infer_operator() uses an "except:" clause that 
catches all exceptions and then yields YES. Since GeneratorExit is an 
exception, this means the generator will yield another value after the 
Python VM told it to stop, which is the reason the Python VM prints that 
message.

There is a patch at the end of this mail.

In general, it seems to be unsafe to have a "yield" in a "try" body and in a 
following "except:" or "except Exception:" clause, unless GeneratorExit is 
explicitly dealt with in an earlier "except" clause. Maybe this could 
become a new pylint rule?

Bye,
		Maarten

--- inference.py        2008-01-14 14:03:14.000000000 +0100
+++ /usr/local/lib/python2.5/site-packages/logilab/astng/inference.py   
2008-05-12 12:13:14.000000000 +0200
@@ -435,6 +435,8 @@
                 # will be the same
                 lhs.getattr(meth)
                 yield lhs
+            except GeneratorExit:
+                raise
             except:
                 yield YES
             continue
@@ -447,6 +449,8 @@
                     # will be the same
                     rhs.getattr(meth)
                     yield rhs
+                except GeneratorExit:
+                    raise
                 except:
                     yield YES
                 continue

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part.
Url : http://lists.logilab.org/pipermail/python-projects/attachments/20080512/359adf21/attachment.pgp 


More information about the Python-Projects mailing list