From 551ba20e8ea9a4a97cf63f28b47175e084eb63cd Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 18 Oct 2011 16:40:50 +0200 Subject: [PATCH] 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. --- Lib/test/test_generators.py | 26 ++++++++++++++++++++++++++ Misc/NEWS | 4 ++++ Objects/genobject.c | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 3ec209ba871..5f47b3eae03 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -1673,6 +1673,32 @@ Traceback (most recent call last): ... 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 --git a/Misc/NEWS b/Misc/NEWS index d610ff45f21..dd31b4bb59f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,10 @@ What's New in Python 3.2.3? 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 --git a/Objects/genobject.c b/Objects/genobject.c index 01cd44ae595..49e2adeaa06 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -261,6 +261,11 @@ gen_throw(PyGenObject *gen, PyObject *args) val = typ; typ = PyExceptionInstance_Class(typ); Py_INCREF(typ); + + if (tb == NULL) { + /* Returns NULL if there's no traceback */ + tb = PyException_GetTraceback(val); + } } } else {