<div dir="ltr"><div><div><div>From: Peter Otten <__<a href="mailto:peter__@web.de">peter__@web.de</a>><br>To: <a href="mailto:python-list@python.org">python-list@python.org</a><br>Cc: <br>Date: Thu, 14 Feb 2013 09:00:58 +0100<br>
Subject: Re: "Exception ... in <generator object ...> ignored" Messages<br>Ami Tavory wrote:<br>
<br>
>   Hi,<br>
><br>
>   Running the unit tests for some generator code, prints, as a side<br>
>   effect,<br>
> numerous messages of the form:<br>
><br>
> ...<br>
> Exception NameError: "global name 'l' is not defined" in <generator object<br>
> _dagpype_internal_fn_act at 0x9d4c500> ignored<br>
> Exception AttributeError: "'NoneType' object has no attribute 'close'" in<br>
> <generator object split at 0x7601640> ignored<br>
> Exception AttributeError: "'NoneType' object has no attribute 'close'" in<br>
> <generator object split at 0x7601690> ignored<br>
> ...<br>
><br>
> The tests otherwise run fine.<br>
><br>
>   Is there any way to catch the point where such a message originates, and<br>
> print a traceback? I find it difficult to debug otherwise. I've tried<br>
> running Python with -W error, catching warnings with context managers, and<br>
> so forth, but without any success.<br>
<br>
>>> def g():<br>
...     try:<br>
...             yield 42<br>
...     finally:<br>
...             1/0<br>
...<br>
>>> for item in g():<br>
...     break<br>
...<br>
Exception ZeroDivisionError: 'integer division or modulo by zero' in<br>
<generator object g at 0x7f990243b0f0> ignored<br>
<br>
Can you exhaust the generator?<br>
<br>
>>> for item in g():<br>
...     pass<br>
...<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
  File "<stdin>", line 5, in g<br>
ZeroDivisionError: integer division or modulo by zero<br>
<br>
Explicitly closing the generator seems to work, too:<br>
<br>
>>> x = g()<br>
>>> next(x)<br>
42<br>
>>> x.close()<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
  File "<stdin>", line 5, in g<br>
ZeroDivisionError: integer division or modulo by zero<br><br>-------------------------------------------------------------------------------------------------<br>-------------------------------------------------------------------------------------------------<br>
-------------------------------------------------------------------------------------------------<br><br></div>  Hi Peter,<br><br></div>  Thanks for your answer! <br><br>  So basically the thing is that, as you've shown, if you exhaust a generator or force-close it, an exception is indeed thrown. However, in this were the case then unittest would identify it as a problem, and I could relatively easily get a traceback and see from where it's originating. However, the unittests are passing fine (except for the pesky messages), so it's something else.<br>
</div><div>  I'm starting to think that what's happening is something like this: I have a cascade of generator objects (some hold on to others), and the unittests check both the exhaustion case and the non-exhaustion case (both cases are valid use cases). *Later on*, after the tests finish, the garbage collector starts tearing down the generator cascade, but in a different order. Some generators, while closing down, are attempting to close down other generators that no longer exist. <br>
</div><div>  So, notwithstanding your correct answer - I'm still stumped about finding out how to get rid of these annoying messages.<br><br></div><div>  Thanks,<br><br></div><div>  Ami<br> </div></div>