Commit e557da80 authored by Amaury Forgeot d'Arc's avatar Amaury Forgeot d'Arc

Fix a crash when the return value of a subgenerator is a temporary

object (with a refcount of 1)
parent 7e447c82
...@@ -364,6 +364,17 @@ class TestPEP380Operation(unittest.TestCase): ...@@ -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): def test_generator_return_value(self):
""" """
Test generator return value Test generator return value
......
...@@ -475,6 +475,7 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) { ...@@ -475,6 +475,7 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) {
Py_XDECREF(tb); Py_XDECREF(tb);
if (ev) { if (ev) {
value = ((PyStopIterationObject *)ev)->value; value = ((PyStopIterationObject *)ev)->value;
Py_INCREF(value);
Py_DECREF(ev); Py_DECREF(ev);
} }
} else if (PyErr_Occurred()) { } else if (PyErr_Occurred()) {
...@@ -482,8 +483,8 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) { ...@@ -482,8 +483,8 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) {
} }
if (value == NULL) { if (value == NULL) {
value = Py_None; value = Py_None;
Py_INCREF(value);
} }
Py_INCREF(value);
*pvalue = value; *pvalue = value;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment