Skip to content

Commit

Permalink
GH-89914: Make the oparg of the YIELD_VALUE instruction equal the sta…
Browse files Browse the repository at this point in the history
…ck depth. (GH-92960)
  • Loading branch information
markshannon authored May 19, 2022
1 parent 70aa1b9 commit 3fd8610
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ iterations of the loop.

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

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

.. opcode:: YIELD_FROM

Expand Down
14 changes: 7 additions & 7 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a7 3494 (New location info table)

# Python 3.12a1 3500 (Remove PRECALL opcode)
# Python 3.12a1 3501 (YIELD_VALUE oparg == stack_depth)

# Python 3.13 will start with 3550

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def jabs_op(name, op):
def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)
def_op('SETUP_ANNOTATIONS', 85)
def_op('YIELD_VALUE', 86)

def_op('ASYNC_GEN_WRAP', 87)
def_op('PREP_RERAISE_STAR', 88)
def_op('POP_EXCEPT', 89)
Expand Down Expand Up @@ -174,7 +174,7 @@ def jabs_op(name, op):
def_op('LOAD_CLASSDEREF', 148)
hasfree.append(148)
def_op('COPY_FREE_VARS', 149)

def_op('YIELD_VALUE', 150)
def_op('RESUME', 151)
def_op('MATCH_CLASS', 152)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The operand of the ``YIELD_VALUE`` instruction is set to the stack depth.
This is done to help frame handling on ``yield`` and may assist debuggers.
1 change: 1 addition & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(YIELD_VALUE) {
assert(oparg == STACK_LEVEL());
assert(frame->is_entry);
PyObject *retval = POP();
_PyFrame_GetGenerator(frame)->gi_frame_state = FRAME_SUSPENDED;
Expand Down
7 changes: 5 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ compiler_add_yield_from(struct compiler *c, int await)
compiler_use_next_block(c, start);
ADDOP_JUMP(c, SEND, exit);
compiler_use_next_block(c, resume);
ADDOP(c, YIELD_VALUE);
ADDOP_I(c, YIELD_VALUE, 0);
ADDOP_I(c, RESUME, await ? 3 : 2);
ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start);
compiler_use_next_block(c, exit);
Expand Down Expand Up @@ -4193,7 +4193,7 @@ addop_yield(struct compiler *c) {
if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
ADDOP(c, ASYNC_GEN_WRAP);
}
ADDOP(c, YIELD_VALUE);
ADDOP_I(c, YIELD_VALUE, 0);
ADDOP_I(c, RESUME, 1);
return 1;
}
Expand Down Expand Up @@ -7152,6 +7152,9 @@ stackdepth(struct compiler *c, basicblock *entry)
next = NULL;
break;
}
if (instr->i_opcode == YIELD_VALUE) {
instr->i_oparg = depth;
}
}
if (next != NULL) {
assert(b->b_nofallthrough == 0);
Expand Down
14 changes: 7 additions & 7 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fd8610

Please sign in to comment.