(original) (raw)
changeset: 72972:dcf5cc88d5c9 branch: 3.2 parent: 72970:2c50343d0500 user: Antoine Pitrou solipsis@pitrou.net date: Tue Oct 18 16:40:50 2011 +0200 files: Lib/test/test_generators.py Misc/NEWS Objects/genobject.c description: Issue #13188: When called without an explicit traceback argument, generator.throw() now gets the traceback from the passed exception's `__traceback__` attribute. Patch by Petri Lehtinen. diff -r 2c50343d0500 -r dcf5cc88d5c9 Lib/test/test_generators.py --- a/Lib/test/test_generators.py Tue Oct 18 13:20:07 2011 +0300 +++ b/Lib/test/test_generators.py Tue Oct 18 16:40:50 2011 +0200 @@ -1673,6 +1673,32 @@ ... ValueError: 7 +Plain "raise" inside a generator should preserve the traceback (#13188). +The traceback should have 3 levels: +- g.throw() +- f() +- 1/0 + +>>> def f(): +... try: +... yield +... except: +... raise +>>> g = f() +>>> try: +... 1/0 +... except ZeroDivisionError as v: +... try: +... g.throw(v) +... except Exception as w: +... tb = w.__traceback__ +>>> levels = 0 +>>> while tb: +... levels += 1 +... tb = tb.tb_next +>>> levels +3 + Now let's try closing a generator: >>> def f(): diff -r 2c50343d0500 -r dcf5cc88d5c9 Misc/NEWS --- a/Misc/NEWS Tue Oct 18 13:20:07 2011 +0300 +++ b/Misc/NEWS Tue Oct 18 16:40:50 2011 +0200 @@ -10,6 +10,10 @@ Core and Builtins ----------------- +- Issue #13188: When called without an explicit traceback argument, + generator.throw() now gets the traceback from the passed exception's + ``__traceback__`` attribute. Patch by Petri Lehtinen. + - Issue #7833: Extension modules built using distutils on Windows will no longer include a "manifest" to prevent them failing at import time in some embedded situations. diff -r 2c50343d0500 -r dcf5cc88d5c9 Objects/genobject.c --- a/Objects/genobject.c Tue Oct 18 13:20:07 2011 +0300 +++ b/Objects/genobject.c Tue Oct 18 16:40:50 2011 +0200 @@ -261,6 +261,11 @@ val = typ; typ = PyExceptionInstance_Class(typ); Py_INCREF(typ); + + if (tb == NULL) { + /* Returns NULL if there's no traceback */ + tb = PyException_GetTraceback(val); + } } } else { /solipsis@pitrou.net