GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [3.14] gh-140373: Correctly emit `PY_UNWIND` event when generator is … · python/cpython@52731c4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Include/internal
    • pycore_ceval.h
  • Lib/test
    • test_monitoring.py
    • test_sys_setprofile.py
  • Misc/NEWS.d/next/Core_and_Builtins
    • 2025-10-29-20-59-10.gh-issue-140373.-uoaPP.rst
  • Objects
    • genobject.c
  • Python
    • ceval.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Include/internal/pycore_ceval.h‎

Copy file name to clipboardExpand all lines: Include/internal/pycore_ceval.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *
296296
PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
297297
PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
298298
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
299+
PyAPI_FUNC(bool) _PyEval_NoToolsForUnwind(PyThreadState *tstate);
299300
PyAPI_FUNC(int) _PyEval_UnpackIterableStackRef(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, _PyStackRef *sp);
300301
PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
301302
PyAPI_FUNC(PyObject **) _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch);
Collapse file

‎Lib/test/test_monitoring.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_monitoring.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,25 @@ def f():
10791079

10801080
self.assertEqual(events, expected)
10811081

1082+
# gh-140373
1083+
def test_gen_unwind(self):
1084+
def gen():
1085+
yield 1
1086+
1087+
def f():
1088+
g = gen()
1089+
next(g)
1090+
g.close()
1091+
1092+
recorders = (
1093+
UnwindRecorder,
1094+
)
1095+
events = self.get_events(f, TEST_TOOL, recorders)
1096+
expected = [
1097+
("unwind", GeneratorExit, "gen"),
1098+
]
1099+
self.assertEqual(events, expected)
1100+
10821101
class LineRecorder:
10831102

10841103
event_type = E.LINE
Collapse file

‎Lib/test/test_sys_setprofile.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_sys_setprofile.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ def g(p):
272272
self.check_events(g, [(1, 'call', g_ident, None),
273273
(2, 'call', f_ident, None),
274274
(2, 'return', f_ident, 0),
275+
(2, 'call', f_ident, None),
276+
(2, 'return', f_ident, None),
275277
(1, 'return', g_ident, None),
276278
], check_args=True)
277279

Collapse file

‎Misc/NEWS.d/next/Core_and_Builtins/2025-10-29-20-59-10.gh-issue-140373.-uoaPP.rst‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly emit ``PY_UNWIND`` event when generator object is closed. Patch by
2+
Mikhail Efimov.
Collapse file

‎Objects/genobject.c‎

Copy file name to clipboardExpand all lines: Objects/genobject.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ gen_close(PyObject *self, PyObject *args)
407407
}
408408
_PyInterpreterFrame *frame = &gen->gi_iframe;
409409
if (is_resume(frame->instr_ptr)) {
410+
bool no_unwind_tools = _PyEval_NoToolsForUnwind(_PyThreadState_GET());
410411
/* We can safely ignore the outermost try block
411412
* as it is automatically generated to handle
412413
* StopIteration. */
413414
int oparg = frame->instr_ptr->op.arg;
414-
if (oparg & RESUME_OPARG_DEPTH1_MASK) {
415+
if (oparg & RESUME_OPARG_DEPTH1_MASK && no_unwind_tools) {
415416
// RESUME after YIELD_VALUE and exception depth is 1
416417
assert((oparg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_FUNC_START);
417418
gen->gi_frame_state = FRAME_COMPLETED;
Collapse file

‎Python/ceval.c‎

Copy file name to clipboardExpand all lines: Python/ceval.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,10 @@ monitor_unwind(PyThreadState *tstate,
25452545
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
25462546
}
25472547

2548+
bool
2549+
_PyEval_NoToolsForUnwind(PyThreadState *tstate) {
2550+
return no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND);
2551+
}
25482552

25492553
static int
25502554
monitor_handled(PyThreadState *tstate,

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.