return _PyFrame_MakeAndSetFrameObject(frame);
}
-void
-_PyFrame_ClearLocals(_PyInterpreterFrame *frame);
-
/* Clears all references in the frame.
* If take is non-zero, then the _PyInterpreterFrame frame
* may be transferred to the frame object it references
for func, (cc, nc, _, _, _) in pr.stats.items():
if func[2] == "<genexpr>":
- self.assertEqual(cc, 1)
- self.assertEqual(nc, 1)
+ self.assertEqual(cc, 2)
+ self.assertEqual(nc, 2)
class TestCommandLine(unittest.TestCase):
import unittest
import weakref
import inspect
+import types
from test import support
self.assertEqual(gc.garbage, old_garbage)
def test_lambda_generator(self):
- # Issue #23192: Test that a lambda returning a generator behaves
+ # bpo-23192, gh-119897: Test that a lambda returning a generator behaves
# like the equivalent function
f = lambda: (yield 1)
+ self.assertIsInstance(f(), types.GeneratorType)
+ self.assertEqual(next(f()), 1)
+
def g(): return (yield 1)
# test 'yield from'
self.assertIsInstance(cm.exception.value, StopIteration)
self.assertEqual(cm.exception.value.value, 2)
- def test_close_releases_frame_locals(self):
- # See gh-118272
-
- class Foo:
- pass
-
- f = Foo()
- f_wr = weakref.ref(f)
-
- def genfn():
- a = f
- yield
-
- g = genfn()
- next(g)
- del f
- g.close()
- support.gc_collect()
- self.assertIsNone(f_wr())
-
class GeneratorThrowTest(unittest.TestCase):
f_ident = ident(f)
g_ident = ident(g)
self.check_events(g, [(1, 'call', g_ident),
+ (2, 'call', f_ident),
+ (2, 'return', f_ident),
+ # once more; the generator is being garbage collected
+ # and it will do a PY_THROW
(2, 'call', f_ident),
(2, 'return', f_ident),
(1, 'return', g_ident),
gen_close(PyGenObject *gen, PyObject *args)
{
PyObject *retval;
+ PyObject *yf = _PyGen_yf(gen);
int err = 0;
if (gen->gi_frame_state == FRAME_CREATED) {
if (gen->gi_frame_state >= FRAME_COMPLETED) {
Py_RETURN_NONE;
}
- PyObject *yf = _PyGen_yf(gen);
if (yf) {
PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING;
* YIELD_VALUE if the debugger has changed the lineno. */
if (err == 0 && is_yield(frame->prev_instr)) {
assert(is_resume(frame->prev_instr + 1));
- int exception_handler_depth = frame->prev_instr[0].op.arg;
+ int exception_handler_depth = frame->prev_instr[0].op.code;
assert(exception_handler_depth > 0);
/* We can safely ignore the outermost try block
* as it automatically generated to handle
* StopIteration. */
if (exception_handler_depth == 1) {
- gen->gi_frame_state = FRAME_COMPLETED;
- _PyFrame_ClearLocals((_PyInterpreterFrame *)gen->gi_iframe);
Py_RETURN_NONE;
}
}
}
}
-void
-_PyFrame_ClearLocals(_PyInterpreterFrame *frame)
-{
- assert(frame->stacktop >= 0);
- int stacktop = frame->stacktop;
- frame->stacktop = 0;
- for (int i = 0; i < stacktop; i++) {
- Py_XDECREF(frame->localsplus[i]);
- }
- Py_CLEAR(frame->f_locals);
-}
-
void
_PyFrame_ClearExceptCode(_PyInterpreterFrame *frame)
{
}
Py_DECREF(f);
}
- _PyFrame_ClearLocals(frame);
+ assert(frame->stacktop >= 0);
+ for (int i = 0; i < frame->stacktop; i++) {
+ Py_XDECREF(frame->localsplus[i]);
+ }
Py_XDECREF(frame->frame_obj);
+ Py_XDECREF(frame->f_locals);
Py_DECREF(frame->f_funcobj);
}