]> granicus.if.org Git - python/commitdiff
Fix a crash when the return value of a subgenerator is a temporary
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 13 Jan 2012 20:06:12 +0000 (21:06 +0100)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 13 Jan 2012 20:06:12 +0000 (21:06 +0100)
object (with a refcount of 1)

Lib/test/test_pep380.py
Objects/genobject.c

index 6554b0fa5c6d0e597906479644521023a696208c..3a1db2915c6e3f7f2b1eb10db7e3e6835fcd42a8 100644 (file)
@@ -364,6 +364,17 @@ class TestPEP380Operation(unittest.TestCase):
         ])
 
 
+    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
index 20c926b79f48b1e2d45513f585496492145b926f..b32d9b68fc3cd939f916d4333a2d5515335f8d5a 100644 (file)
@@ -475,6 +475,7 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) {
         Py_XDECREF(tb);
         if (ev) {
             value = ((PyStopIterationObject *)ev)->value;
+            Py_INCREF(value);
             Py_DECREF(ev);
         }
     } else if (PyErr_Occurred()) {
@@ -482,8 +483,8 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) {
     }
     if (value == NULL) {
         value = Py_None;
+        Py_INCREF(value);
     }
-    Py_INCREF(value);
     *pvalue = value;
     return 0;
 }