]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #13188: When called without an explicit traceback argument,
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 18 Oct 2011 14:40:50 +0000 (16:40 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 18 Oct 2011 14:40:50 +0000 (16:40 +0200)
generator.throw() now gets the traceback from the passed exception's
`__traceback__` attribute.  Patch by Petri Lehtinen.

Lib/test/test_generators.py
Misc/NEWS
Objects/genobject.c

index 3ec209ba871248d9a668202e43f68adf798b9e44..5f47b3eae033678734ad066a8e47641112f4851e 100644 (file)
@@ -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():
index d610ff45f219aaa7bc63fb71c08b570f749b728e..dd31b4bb59f7133410dec8e4551bb2d2934b93aa 100644 (file)
--- 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.
index 01cd44ae59552955eb0b4be180d51bf97f7281d1..49e2adeaa060f3daf8a6ad9697354d1ad4ac2371 100644 (file)
@@ -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 {