Skip to content

Commit 3fd8610

Browse files
authored
GH-89914: Make the oparg of the YIELD_VALUE instruction equal the stack depth. (GH-92960)
1 parent 70aa1b9 commit 3fd8610

File tree

9 files changed

+35
-26
lines changed

9 files changed

+35
-26
lines changed

Doc/library/dis.rst

+2
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ iterations of the loop.
575575

576576
Pops TOS and yields it from a :term:`generator`.
577577

578+
.. versionchanged:: 3.11
579+
oparg set to be the stack depth, for efficient handling on frames.
578580

579581
.. opcode:: YIELD_FROM
580582

Include/internal/pycore_opcode.h

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode.h

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/importlib/_bootstrap_external.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def _write_atomic(path, data, mode=0o666):
405405
# Python 3.11a7 3494 (New location info table)
406406

407407
# Python 3.12a1 3500 (Remove PRECALL opcode)
408+
# Python 3.12a1 3501 (YIELD_VALUE oparg == stack_depth)
408409

409410
# Python 3.13 will start with 3550
410411

@@ -418,7 +419,7 @@ def _write_atomic(path, data, mode=0o666):
418419
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
419420
# in PC/launcher.c must also be updated.
420421

421-
MAGIC_NUMBER = (3500).to_bytes(2, 'little') + b'\r\n'
422+
MAGIC_NUMBER = (3501).to_bytes(2, 'little') + b'\r\n'
422423

423424
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
424425

Lib/opcode.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def jabs_op(name, op):
9898
def_op('RETURN_VALUE', 83)
9999
def_op('IMPORT_STAR', 84)
100100
def_op('SETUP_ANNOTATIONS', 85)
101-
def_op('YIELD_VALUE', 86)
101+
102102
def_op('ASYNC_GEN_WRAP', 87)
103103
def_op('PREP_RERAISE_STAR', 88)
104104
def_op('POP_EXCEPT', 89)
@@ -174,7 +174,7 @@ def jabs_op(name, op):
174174
def_op('LOAD_CLASSDEREF', 148)
175175
hasfree.append(148)
176176
def_op('COPY_FREE_VARS', 149)
177-
177+
def_op('YIELD_VALUE', 150)
178178
def_op('RESUME', 151)
179179
def_op('MATCH_CLASS', 152)
180180

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The operand of the ``YIELD_VALUE`` instruction is set to the stack depth.
2+
This is done to help frame handling on ``yield`` and may assist debuggers.

Python/ceval.c

+1
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
26982698
}
26992699

27002700
TARGET(YIELD_VALUE) {
2701+
assert(oparg == STACK_LEVEL());
27012702
assert(frame->is_entry);
27022703
PyObject *retval = POP();
27032704
_PyFrame_GetGenerator(frame)->gi_frame_state = FRAME_SUSPENDED;

Python/compile.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ compiler_add_yield_from(struct compiler *c, int await)
19621962
compiler_use_next_block(c, start);
19631963
ADDOP_JUMP(c, SEND, exit);
19641964
compiler_use_next_block(c, resume);
1965-
ADDOP(c, YIELD_VALUE);
1965+
ADDOP_I(c, YIELD_VALUE, 0);
19661966
ADDOP_I(c, RESUME, await ? 3 : 2);
19671967
ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start);
19681968
compiler_use_next_block(c, exit);
@@ -4193,7 +4193,7 @@ addop_yield(struct compiler *c) {
41934193
if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
41944194
ADDOP(c, ASYNC_GEN_WRAP);
41954195
}
4196-
ADDOP(c, YIELD_VALUE);
4196+
ADDOP_I(c, YIELD_VALUE, 0);
41974197
ADDOP_I(c, RESUME, 1);
41984198
return 1;
41994199
}
@@ -7152,6 +7152,9 @@ stackdepth(struct compiler *c, basicblock *entry)
71527152
next = NULL;
71537153
break;
71547154
}
7155+
if (instr->i_opcode == YIELD_VALUE) {
7156+
instr->i_oparg = depth;
7157+
}
71557158
}
71567159
if (next != NULL) {
71577160
assert(b->b_nofallthrough == 0);

Python/opcode_targets.h

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)