[Python-checkins] cpython (merge 3.2 -> default): Issue #13188: When called without an explicit traceback argument,

antoine.pitrou python-checkins at python.org
Tue Oct 18 16:47:02 CEST 2011


http://hg.python.org/cpython/rev/f4e3db1194e4
changeset:   72973:f4e3db1194e4
parent:      72971:e08397a5537a
parent:      72972:dcf5cc88d5c9
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue Oct 18 16:42:55 2011 +0200
summary:
  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.

files:
  Lib/test/test_generators.py |  26 +++++++++++++++++++++++++
  Misc/NEWS                   |   4 +++
  Objects/genobject.c         |   5 ++++
  3 files changed, 35 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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 #13146: Writing a pyc file is now atomic under POSIX.
 
 - Issue #7833: Extension modules built using distutils on Windows will no
diff --git a/Objects/genobject.c b/Objects/genobject.c
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -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 {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list