Skip to content

Commit f5197dd

Browse files
bpo-34066: Disabled interruption before SETUP_WITH and BEFORE_ASYNC_WITH. (GH-8159)
This will prevent emitting a resource warning when the execution was interrupted by Ctrl-C between calling open() and entering a 'with' block in "with open()". (cherry picked from commit 3f4d90d) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 7c43b80 commit f5197dd

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Disabled interruption by Ctrl-C between calling ``open()`` and entering a
2+
**with** block in ``with open()``.

Python/ceval.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,11 +941,18 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
941941
Py_MakePendingCalls() above. */
942942

943943
if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
944-
if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
945-
_Py_OPCODE(*next_instr) == YIELD_FROM) {
946-
/* Two cases where we skip running signal handlers and other
944+
opcode = _Py_OPCODE(*next_instr);
945+
if (opcode == SETUP_FINALLY ||
946+
opcode == SETUP_WITH ||
947+
opcode == BEFORE_ASYNC_WITH ||
948+
opcode == YIELD_FROM) {
949+
/* Few cases where we skip running signal handlers and other
947950
pending calls:
948-
- If we're about to enter the try: of a try/finally (not
951+
- If we're about to enter the 'with:'. It will prevent
952+
emitting a resource warning in the common idiom
953+
'with open(path) as file:'.
954+
- If we're about to enter the 'async with:'.
955+
- If we're about to enter the 'try:' of a try/finally (not
949956
*very* useful, but might help in some cases and it's
950957
traditional)
951958
- If we're resuming a chain of nested 'yield from' or

0 commit comments

Comments
 (0)