FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-90997: Wrap `yield from`/`await` in a virtual `try`/`except StopIteration` by brandtbucher · Pull Request #96010 · python/cpython · GitHub

/ cpython Public
Prev Previous commit
Next Next commit
Don't load StopIteration as a constant
  • Loading branch information
brandtbucher committed May 6, 2022
commit d557e418c3b63427bf2ca090bc339ffc61b17d4a
10 changes: 6 additions & 4 deletions Doc/library/dis.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,14 @@ iterations of the loop.
Exception representation on the stack now consist of one, not three, items.


.. opcode:: LOAD_ASSERTION_ERROR
.. opcode:: LOAD_EXCEPTION_TYPE (type)
Comment thread
brandtbucher marked this conversation as resolved.
Outdated

Pushes :exc:`AssertionError` onto the stack. Used by the :keyword:`assert`
statement.
Pushes an exception type onto the stack, depending on the value of *type*:

.. versionadded:: 3.9
* ``0``: :exc:`AssertionError`
* ``1``: :exc:`StopIteration`

.. versionadded:: 3.11


.. opcode:: LOAD_BUILD_CLASS
Expand Down
16 changes: 8 additions & 8 deletions Include/internal/pycore_opcode.h

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

22 changes: 11 additions & 11 deletions Include/opcode.h

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

3 changes: 3 additions & 0 deletions Lib/dis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
LOAD_GLOBAL = opmap['LOAD_GLOBAL']
BINARY_OP = opmap['BINARY_OP']
JUMP_BACKWARD = opmap['JUMP_BACKWARD']
LOAD_EXCEPTION_TYPE = opmap['LOAD_EXCEPTION_TYPE']

CACHE = opmap["CACHE"]

Expand Down Expand Up @@ -491,6 +492,8 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
if arg & (1<<i))
elif deop == BINARY_OP:
_, argrepr = _nb_ops[arg]
elif deop == LOAD_EXCEPTION_TYPE:
argrepr = "StopIteration" if arg else "AssertionError"
yield Instruction(_all_opname[op], op,
arg, argval, argrepr,
offset, starts_line, is_jump_target, positions)
Expand Down
8 changes: 5 additions & 3 deletions Lib/importlib/_bootstrap_external.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,11 @@ def _write_atomic(path, data, mode=0o666):
# add JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual)
# Python 3.11a7 3492 (make POP_JUMP_IF_NONE/NOT_NONE/TRUE/FALSE relative)
# Python 3.11a7 3493 (Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative)
# Python 3.11a7 3494 (New location info table)
# Python 3.12 will start with magic number 3500
# Python 3.11a7 3494 (New location info table)
# Python 3.11a7 3495 (Replace LOAD_ASSERTION_ERROR with LOAD_EXCEPTION_TYPE)


# Python 3.12 will start with magic number 3500

#
# MAGIC must change whenever the bytecode emitted by the compiler may no
Expand All @@ -416,7 +418,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 = (3494).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3495).to_bytes(2, 'little') + b'\r\n'

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

Expand Down
3 changes: 1 addition & 2 deletions Lib/opcode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def jabs_op(name, op, entries=0):
def_op('PRINT_EXPR', 70)
def_op('LOAD_BUILD_CLASS', 71)

def_op('LOAD_ASSERTION_ERROR', 74)
def_op('RETURN_GENERATOR', 75)

def_op('LIST_TO_TUPLE', 82)
Expand Down Expand Up @@ -166,7 +165,7 @@ def jabs_op(name, op, entries=0):
def_op('DELETE_DEREF', 139)
hasfree.append(139)
jrel_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)

def_op('LOAD_EXCEPTION_TYPE', 141)
def_op('CALL_FUNCTION_EX', 142) # Flags

def_op('EXTENDED_ARG', 144)
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_dis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def bug1333982(x=[]):
dis_bug1333982 = """\
%3d RESUME 0

%3d LOAD_ASSERTION_ERROR
%3d LOAD_EXCEPTION_TYPE 0 (AssertionError)
LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
MAKE_FUNCTION 0
LOAD_FAST 0 (x)
Expand Down Expand Up @@ -1171,7 +1171,6 @@ async def async_def():
Constants:
0: None
1: 1
2: <class 'StopIteration'>
Names:
0: value
1: b
Expand Down
4 changes: 2 additions & 2 deletions Objects/codeobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ PyCode_New(int argcount, int kwonlyargcount,
}

static const char assert0[4] = {
LOAD_ASSERTION_ERROR,
0,
LOAD_EXCEPTION_TYPE,
0, // AssertionError
RAISE_VARARGS,
1
};
Expand Down
28 changes: 11 additions & 17 deletions Objects/exceptions.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -600,23 +600,17 @@ StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
}

// Don't use ComplexExtendsException for this, since deepfreeze doesn't work if
// the type is static:
PyTypeObject _PyExc_StopIteration = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "StopIteration",
.tp_basicsize = sizeof(PyStopIterationObject),
.tp_dealloc = (destructor)StopIteration_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.tp_doc = PyDoc_STR("Signal the end from iterator.__next__()."),
.tp_traverse = (traverseproc)StopIteration_traverse,
.tp_clear = (inquiry)StopIteration_clear,
.tp_members = StopIteration_members,
.tp_base = &_PyExc_Exception,
.tp_dictoffset = offsetof(PyStopIterationObject, dict),
.tp_init = (initproc)StopIteration_init,
};
PyObject *PyExc_StopIteration = (PyObject *)&_PyExc_StopIteration;
ComplexExtendsException(
PyExc_Exception, /* base */
StopIteration, /* name */
StopIteration, /* prefix for *_init, etc */
0, /* new */
0, /* methods */
StopIteration_members, /* members */
0, /* getset */
0, /* str */
"Signal the end from iterator.__next__()."
);


/*
Expand Down
5 changes: 3 additions & 2 deletions Python/ceval.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2778,8 +2778,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}
}

TARGET(LOAD_ASSERTION_ERROR) {
PyObject *value = PyExc_AssertionError;
TARGET(LOAD_EXCEPTION_TYPE) {
assert(oparg < 2);
PyObject *value = oparg ? PyExc_StopIteration: PyExc_AssertionError;
Py_INCREF(value);
PUSH(value);
DISPATCH();
Expand Down
7 changes: 3 additions & 4 deletions Python/compile.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ stack_effect(int opcode, int oparg, int jump)
return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
case LOAD_METHOD:
return 1;
case LOAD_ASSERTION_ERROR:
case LOAD_EXCEPTION_TYPE:
return 1;
case LIST_TO_TUPLE:
return 0;
Expand Down Expand Up @@ -1961,7 +1961,6 @@ compiler_add_yield_from(struct compiler *c, int await)
RETURN_IF_FALSE(stopiter = compiler_new_block(c));
RETURN_IF_FALSE(error = compiler_new_block(c));
RETURN_IF_FALSE(exit = compiler_new_block(c));

compiler_use_next_block(c, start);
ADDOP_JUMP(c, SEND, exit);
compiler_use_next_block(c, resume);
Expand All @@ -1976,7 +1975,7 @@ compiler_add_yield_from(struct compiler *c, int await)
ADDOP_I(c, RESUME, await ? 3 : 2);
ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start);
compiler_use_next_block(c, stopiter);
ADDOP_LOAD_CONST(c, PyExc_StopIteration); // StopIteration is marshallable!
ADDOP_I(c, LOAD_EXCEPTION_TYPE, 1); // StopIteration
ADDOP(c, CHECK_EXC_MATCH);
ADDOP_JUMP(c, POP_JUMP_IF_FALSE, error);
// StopIteration was raised. Push the return value and continue execution:
Expand Down Expand Up @@ -4018,7 +4017,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
return 0;
if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
return 0;
ADDOP(c, LOAD_ASSERTION_ERROR);
ADDOP_I(c, LOAD_EXCEPTION_TYPE, 0); // AssertionError
if (s->v.Assert.msg) {
VISIT(c, expr, s->v.Assert.msg);
ADDOP_I(c, PRECALL, 0);
Expand Down
12 changes: 6 additions & 6 deletions Python/opcode_targets.h

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

4 changes: 0 additions & 4 deletions Tools/scripts/deepfreeze.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ def __init__(self, file: TextIO) -> None:
self.write('#include "internal/pycore_code.h"')
self.write('#include "internal/pycore_long.h"')
self.write("")
self.write("extern PyTypeObject _PyExc_StopIteration;")
self.write("")

@contextlib.contextmanager
def indent(self) -> None:
Expand Down Expand Up @@ -404,8 +402,6 @@ def generate(self, name: str, obj: object) -> str:
return "Py_Ellipsis"
elif obj is None:
return "Py_None"
elif obj is StopIteration:
return "(PyObject *)&_PyExc_StopIteration"
else:
raise TypeError(
f"Cannot generate code for {type(obj).__name__} object")
Expand Down
2 changes: 0 additions & 2 deletions Tools/scripts/umarshal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ def R_REF(obj: Any) -> Any:
retval = self.refs[n]
assert retval is not None
return retval
elif type == Type.STOPITER:
return StopIteration
else:
breakpoint()
raise AssertionError(f"Unknown type {type} {chr(type)!r}")
Expand Down

Back | FazBrowse Home | New Git URL