[Python-Dev] Exception chaining and generator finalisation

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Aug 1 09:45:31 CEST 2010


Antoine Pitrou wrote:

> It only happens if you call close() explicitly:

Well, that's only because the exception is being ignored and
you're not getting a traceback at all.

If you arrange to get a traceback, the same thing happens.

   import traceback as tb

   def g():
     try:
       try:
         yield 1
       finally:
         raise ValueError("Hovercraft contains eels")
     except Exception:
       tb.print_exc()

   gi = g()
   next(gi)
   del gi

-- 
Greg


> 
>>>>def g():
> 
> ...   try: yield 1
> ...   finally: 1/0
> ... 
> 
>>>>gi = g()
>>>>next(gi)
> 
> 1
> 
>>>>del gi
> 
> Exception ZeroDivisionError: ZeroDivisionError('division by zero',) in
> <generator object g at 0x7fe50351ddc0> ignored
> 
>>>>gi = g()
>>>>next(gi)
> 
> 1
> 
>>>>next(gi)
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 3, in g
> ZeroDivisionError: division by zero
> 
> 
> 
> Regards
> 
> Antoine.
> 
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/greg.ewing%40canterbury.ac.nz



More information about the Python-Dev mailing list