[Python-checkins] cpython: Fix a crash when the return value of a subgenerator is a temporary

amaury.forgeotdarc python-checkins at python.org
Fri Jan 13 21:11:09 CET 2012


http://hg.python.org/cpython/rev/9e9c1be433d7
changeset:   74366:9e9c1be433d7
user:        Amaury Forgeot d'Arc <amauryfa at gmail.com>
date:        Fri Jan 13 21:06:12 2012 +0100
summary:
  Fix a crash when the return value of a subgenerator is a temporary
object (with a refcount of 1)

files:
  Lib/test/test_pep380.py |  11 +++++++++++
  Objects/genobject.c     |   3 ++-
  2 files changed, 13 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_pep380.py b/Lib/test/test_pep380.py
--- a/Lib/test/test_pep380.py
+++ b/Lib/test/test_pep380.py
@@ -364,6 +364,17 @@
         ])
 
 
+    def test_exception_value_crash(self):
+        # There used to be a refcount error when the return value
+        # stored in the StopIteration has a refcount of 1.
+        def g1():
+            yield from g2()
+        def g2():
+            yield "g2"
+            return [42]
+        self.assertEqual(list(g1()), ["g2"])
+
+
     def test_generator_return_value(self):
         """
         Test generator return value
diff --git a/Objects/genobject.c b/Objects/genobject.c
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -475,6 +475,7 @@
         Py_XDECREF(tb);
         if (ev) {
             value = ((PyStopIterationObject *)ev)->value;
+            Py_INCREF(value);
             Py_DECREF(ev);
         }
     } else if (PyErr_Occurred()) {
@@ -482,8 +483,8 @@
     }
     if (value == NULL) {
         value = Py_None;
+        Py_INCREF(value);
     }
-    Py_INCREF(value);
     *pvalue = value;
     return 0;
 }

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


More information about the Python-checkins mailing list