Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e557da8

Browse files
committed
Fix a crash when the return value of a subgenerator is a temporary
object (with a refcount of 1)
1 parent 7e447c8 commit e557da8

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_pep380.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ def pex(e):
364364
])
365365

366366

367+
def test_exception_value_crash(self):
368+
# There used to be a refcount error when the return value
369+
# stored in the StopIteration has a refcount of 1.
370+
def g1():
371+
yield from g2()
372+
def g2():
373+
yield "g2"
374+
return [42]
375+
self.assertEqual(list(g1()), ["g2"])
376+
377+
367378
def test_generator_return_value(self):
368379
"""
369380
Test generator return value

Objects/genobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,16 @@ PyGen_FetchStopIterationValue(PyObject **pvalue) {
475475
Py_XDECREF(tb);
476476
if (ev) {
477477
value = ((PyStopIterationObject *)ev)->value;
478+
Py_INCREF(value);
478479
Py_DECREF(ev);
479480
}
480481
} else if (PyErr_Occurred()) {
481482
return -1;
482483
}
483484
if (value == NULL) {
484485
value = Py_None;
486+
Py_INCREF(value);
485487
}
486-
Py_INCREF(value);
487488
*pvalue = value;
488489
return 0;
489490
}

0 commit comments

Comments
 (0)