diff options
author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2012-01-13 21:06:12 +0100 |
---|---|---|
committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2012-01-13 21:06:12 +0100 |
commit | e557da804aae6ae6478809243c349659f5c3e722 (patch) | |
tree | 8bd67c70fc72c5cf33734d9a9f78c70e9f6ac7a2 | |
parent | 7e447c8224c0ce7748dfd502c746139965d2d1e4 (diff) | |
download | cpython-git-e557da804aae6ae6478809243c349659f5c3e722.tar.gz |
Fix a crash when the return value of a subgenerator is a temporary
object (with a refcount of 1)
-rw-r--r-- | Lib/test/test_pep380.py | 11 | ||||
-rw-r--r-- | 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 index 6554b0fa5c..3a1db2915c 100644 --- a/Lib/test/test_pep380.py +++ b/Lib/test/test_pep380.py @@ -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 diff --git a/Objects/genobject.c b/Objects/genobject.c index 20c926b79f..b32d9b68fc 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -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; } |