From 4e1e4a377e5f35fb861507443759b9921d2af470 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 12:30:06 +0000 Subject: [PATCH 01/13] Add CALL_INSTRINSIC_1 instruction, but don't use it yet. --- Include/internal/pycore_intrinsics.h | 11 ++ Include/internal/pycore_opcode.h | 26 ++-- Include/opcode.h | 57 ++++---- Lib/opcode.py | 1 + Makefile.pre.in | 2 + PCbuild/_freeze_module.vcxproj | 1 + PCbuild/_freeze_module.vcxproj.filters | 3 + PCbuild/pythoncore.vcxproj | 2 + PCbuild/pythoncore.vcxproj.filters | 6 + Python/bytecodes.c | 7 + Python/ceval.c | 1 + Python/compile.c | 3 +- Python/generated_cases.c.h | 10 ++ Python/intrinsics.c | 189 +++++++++++++++++++++++++ Python/opcode_targets.h | 24 ++-- 15 files changed, 289 insertions(+), 54 deletions(-) create mode 100644 Include/internal/pycore_intrinsics.h create mode 100644 Python/intrinsics.c diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h new file mode 100644 index 00000000000000..0175b2923820da --- /dev/null +++ b/Include/internal/pycore_intrinsics.h @@ -0,0 +1,11 @@ + + +typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value); + + +#define INTRINSIC_PRINT 1 +#define INRINSIC_IMPORT_STAR 2 +#define INTRINSIC_STOPITERATION_ERROR 3 + +extern instrinsic_func1 +_PyIntrinsics_UnaryFunctions[]; diff --git a/Include/internal/pycore_opcode.h b/Include/internal/pycore_opcode.h index da8a272f2fa2d0..58d7201d199337 100644 --- a/Include/internal/pycore_opcode.h +++ b/Include/internal/pycore_opcode.h @@ -85,6 +85,7 @@ const uint8_t _PyOpcode_Deopt[256] = { [CALL_BUILTIN_CLASS] = CALL, [CALL_BUILTIN_FAST_WITH_KEYWORDS] = CALL, [CALL_FUNCTION_EX] = CALL_FUNCTION_EX, + [CALL_INTRINSIC_1] = CALL_INTRINSIC_1, [CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = CALL, [CALL_NO_KW_BUILTIN_FAST] = CALL, [CALL_NO_KW_BUILTIN_O] = CALL, @@ -296,29 +297,29 @@ static const char *const _PyOpcode_OpName[263] = { [DELETE_SUBSCR] = "DELETE_SUBSCR", [FOR_ITER_TUPLE] = "FOR_ITER_TUPLE", [STOPITERATION_ERROR] = "STOPITERATION_ERROR", + [CALL_INTRINSIC_1] = "CALL_INTRINSIC_1", [FOR_ITER_RANGE] = "FOR_ITER_RANGE", [FOR_ITER_GEN] = "FOR_ITER_GEN", [LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS", - [LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN", [GET_ITER] = "GET_ITER", [GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER", [PRINT_EXPR] = "PRINT_EXPR", [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", + [LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN", [LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE", - [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR", [RETURN_GENERATOR] = "RETURN_GENERATOR", + [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY", [LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT", [LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT", [LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT", [LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT", - [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", [LIST_TO_TUPLE] = "LIST_TO_TUPLE", [RETURN_VALUE] = "RETURN_VALUE", [IMPORT_STAR] = "IMPORT_STAR", [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", - [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", + [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", [ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP", [PREP_RERAISE_STAR] = "PREP_RERAISE_STAR", [POP_EXCEPT] = "POP_EXCEPT", @@ -345,7 +346,7 @@ static const char *const _PyOpcode_OpName[263] = { [JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP", [JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP", - [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", + [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", [POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE", [POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE", [LOAD_GLOBAL] = "LOAD_GLOBAL", @@ -353,7 +354,7 @@ static const char *const _PyOpcode_OpName[263] = { [CONTAINS_OP] = "CONTAINS_OP", [RERAISE] = "RERAISE", [COPY] = "COPY", - [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", + [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", [BINARY_OP] = "BINARY_OP", [SEND] = "SEND", [LOAD_FAST] = "LOAD_FAST", @@ -373,9 +374,9 @@ static const char *const _PyOpcode_OpName[263] = { [STORE_DEREF] = "STORE_DEREF", [DELETE_DEREF] = "DELETE_DEREF", [JUMP_BACKWARD] = "JUMP_BACKWARD", - [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", + [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", - [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", + [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", [EXTENDED_ARG] = "EXTENDED_ARG", [LIST_APPEND] = "LIST_APPEND", [SET_ADD] = "SET_ADD", @@ -385,27 +386,27 @@ static const char *const _PyOpcode_OpName[263] = { [YIELD_VALUE] = "YIELD_VALUE", [RESUME] = "RESUME", [MATCH_CLASS] = "MATCH_CLASS", + [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", - [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [FORMAT_VALUE] = "FORMAT_VALUE", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_STRING] = "BUILD_STRING", + [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", - [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", [LIST_EXTEND] = "LIST_EXTEND", [SET_UPDATE] = "SET_UPDATE", [DICT_MERGE] = "DICT_MERGE", [DICT_UPDATE] = "DICT_UPDATE", + [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", - [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", [CALL] = "CALL", [KW_NAMES] = "KW_NAMES", - [173] = "<173>", + [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", [174] = "<174>", [175] = "<175>", [176] = "<176>", @@ -499,7 +500,6 @@ static const char *const _PyOpcode_OpName[263] = { #endif #define EXTRA_CASES \ - case 173: \ case 174: \ case 175: \ case 176: \ diff --git a/Include/opcode.h b/Include/opcode.h index 888250ed37e8cb..1bda01772332ea 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -38,6 +38,7 @@ extern "C" { #define STORE_SUBSCR 60 #define DELETE_SUBSCR 61 #define STOPITERATION_ERROR 63 +#define CALL_INTRINSIC_1 64 #define GET_ITER 68 #define GET_YIELD_FROM_ITER 69 #define PRINT_EXPR 70 @@ -163,34 +164,34 @@ extern "C" { #define COMPARE_OP_STR_JUMP 58 #define FOR_ITER_LIST 59 #define FOR_ITER_TUPLE 62 -#define FOR_ITER_RANGE 64 -#define FOR_ITER_GEN 65 -#define LOAD_ATTR_CLASS 66 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 67 -#define LOAD_ATTR_INSTANCE_VALUE 72 -#define LOAD_ATTR_MODULE 73 -#define LOAD_ATTR_PROPERTY 76 -#define LOAD_ATTR_SLOT 77 -#define LOAD_ATTR_WITH_HINT 78 -#define LOAD_ATTR_METHOD_LAZY_DICT 79 -#define LOAD_ATTR_METHOD_NO_DICT 80 -#define LOAD_ATTR_METHOD_WITH_DICT 81 -#define LOAD_ATTR_METHOD_WITH_VALUES 86 -#define LOAD_CONST__LOAD_FAST 113 -#define LOAD_FAST__LOAD_CONST 121 -#define LOAD_FAST__LOAD_FAST 141 -#define LOAD_GLOBAL_BUILTIN 143 -#define LOAD_GLOBAL_MODULE 153 -#define STORE_ATTR_INSTANCE_VALUE 154 -#define STORE_ATTR_SLOT 158 -#define STORE_ATTR_WITH_HINT 159 -#define STORE_FAST__LOAD_FAST 160 -#define STORE_FAST__STORE_FAST 161 -#define STORE_SUBSCR_DICT 166 -#define STORE_SUBSCR_LIST_INT 167 -#define UNPACK_SEQUENCE_LIST 168 -#define UNPACK_SEQUENCE_TUPLE 169 -#define UNPACK_SEQUENCE_TWO_TUPLE 170 +#define FOR_ITER_RANGE 65 +#define FOR_ITER_GEN 66 +#define LOAD_ATTR_CLASS 67 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 72 +#define LOAD_ATTR_INSTANCE_VALUE 73 +#define LOAD_ATTR_MODULE 76 +#define LOAD_ATTR_PROPERTY 77 +#define LOAD_ATTR_SLOT 78 +#define LOAD_ATTR_WITH_HINT 79 +#define LOAD_ATTR_METHOD_LAZY_DICT 80 +#define LOAD_ATTR_METHOD_NO_DICT 81 +#define LOAD_ATTR_METHOD_WITH_DICT 86 +#define LOAD_ATTR_METHOD_WITH_VALUES 113 +#define LOAD_CONST__LOAD_FAST 121 +#define LOAD_FAST__LOAD_CONST 141 +#define LOAD_FAST__LOAD_FAST 143 +#define LOAD_GLOBAL_BUILTIN 153 +#define LOAD_GLOBAL_MODULE 154 +#define STORE_ATTR_INSTANCE_VALUE 158 +#define STORE_ATTR_SLOT 159 +#define STORE_ATTR_WITH_HINT 160 +#define STORE_FAST__LOAD_FAST 161 +#define STORE_FAST__STORE_FAST 166 +#define STORE_SUBSCR_DICT 167 +#define STORE_SUBSCR_LIST_INT 168 +#define UNPACK_SEQUENCE_LIST 169 +#define UNPACK_SEQUENCE_TUPLE 170 +#define UNPACK_SEQUENCE_TWO_TUPLE 173 #define DO_TRACING 255 #define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\ diff --git a/Lib/opcode.py b/Lib/opcode.py index fc57affbac5814..b1d082ed401351 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -113,6 +113,7 @@ def pseudo_op(name, op, real_ops): def_op('DELETE_SUBSCR', 61) def_op('STOPITERATION_ERROR', 63) +def_op('CALL_INTRINSIC_1', 64) def_op('GET_ITER', 68) def_op('GET_YIELD_FROM_ITER', 69) diff --git a/Makefile.pre.in b/Makefile.pre.in index 1f8bd561f61dcd..397fc996192d21 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -395,6 +395,7 @@ PYTHON_OBJS= \ Python/import.o \ Python/importdl.o \ Python/initconfig.o \ + Python/intrinsics.o \ Python/marshal.o \ Python/modsupport.o \ Python/mysnprintf.o \ @@ -1650,6 +1651,7 @@ PYTHON_HEADERS= \ $(srcdir)/Include/internal/pycore_initconfig.h \ $(srcdir)/Include/internal/pycore_interp.h \ $(srcdir)/Include/internal/pycore_interpreteridobject.h \ + $(srcdir)/Include/internal/pycore_intrinsics.h \ $(srcdir)/Include/internal/pycore_list.h \ $(srcdir)/Include/internal/pycore_long.h \ $(srcdir)/Include/internal/pycore_moduleobject.h \ diff --git a/PCbuild/_freeze_module.vcxproj b/PCbuild/_freeze_module.vcxproj index fce1f670510001..4f39756019e692 100644 --- a/PCbuild/_freeze_module.vcxproj +++ b/PCbuild/_freeze_module.vcxproj @@ -206,6 +206,7 @@ + diff --git a/PCbuild/_freeze_module.vcxproj.filters b/PCbuild/_freeze_module.vcxproj.filters index dce6278987c5df..7d7c4587b9a3f3 100644 --- a/PCbuild/_freeze_module.vcxproj.filters +++ b/PCbuild/_freeze_module.vcxproj.filters @@ -205,6 +205,9 @@ Source Files + + Source Files + Source Files diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index bb2aaae3317b02..53ed5f959779f3 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -231,6 +231,7 @@ + @@ -520,6 +521,7 @@ + diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index 339e7cc4937a3d..c1b531fd818a97 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -600,6 +600,9 @@ Include\cpython + + Include\cpython + Include\internal @@ -1151,6 +1154,9 @@ Python + + Source Files + Python diff --git a/Python/bytecodes.c b/Python/bytecodes.c index dec122a78223fe..b755576d393bf3 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -12,6 +12,7 @@ #include "pycore_ceval.h" // _PyEval_SignalAsyncExc() #include "pycore_code.h" #include "pycore_function.h" +#include "pycore_intrinsics.h" #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_object.h" // _PyObject_GC_TRACK() #include "pycore_moduleobject.h" // PyModuleObject @@ -554,6 +555,12 @@ dummy_func( Py_DECREF(res); } + inst(CALL_INTRINSIC_1, (value -- res)) { + res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value); + Py_DECREF(value); + ERROR_IF(res == NULL, error); + } + // stack effect: (__array[oparg] -- ) inst(RAISE_VARARGS) { PyObject *cause = NULL, *exc = NULL; diff --git a/Python/ceval.c b/Python/ceval.c index 54df1c512502eb..eb9d37f23883c8 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -13,6 +13,7 @@ #include "pycore_ceval.h" // _PyEval_SignalAsyncExc() #include "pycore_code.h" #include "pycore_function.h" +#include "pycore_intrinsics.h" #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_object.h" // _PyObject_GC_TRACK() #include "pycore_moduleobject.h" // PyModuleObject diff --git a/Python/compile.c b/Python/compile.c index 2527023d18e852..97bc07e5057f98 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1249,7 +1249,8 @@ stack_effect(int opcode, int oparg, int jump) return 0; case CALL: return -1-oparg; - + case CALL_INTRINSIC_1: + return 0; case CALL_FUNCTION_EX: return -2 - ((oparg & 0x01) != 0); case MAKE_FUNCTION: diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3218bd091433f9..1b25e69c4d9ad4 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -703,6 +703,16 @@ DISPATCH(); } + TARGET(CALL_INTRINSIC_1) { + PyObject *value = PEEK(1); + PyObject *res; + res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value); + Py_DECREF(value); + if (res == NULL) goto pop_1_error; + POKE(1, res); + DISPATCH(); + } + TARGET(RAISE_VARARGS) { PyObject *cause = NULL, *exc = NULL; switch (oparg) { diff --git a/Python/intrinsics.c b/Python/intrinsics.c new file mode 100644 index 00000000000000..de3ab528c95543 --- /dev/null +++ b/Python/intrinsics.c @@ -0,0 +1,189 @@ + +#define _PY_INTERPRETER + +#include "Python.h" +#include "pycore_frame.h" +#include "pycore_runtime.h" +#include "pycore_global_objects.h" +#include "pycore_intrinsics.h" +#include "pycore_pyerrors.h" + + + +static PyObject * +print_expr(PyThreadState* tstate, PyObject *value) +{ + PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(displayhook)); + // Can't use ERROR_IF here. + if (hook == NULL) { + _PyErr_SetString(tstate, PyExc_RuntimeError, + "lost sys.displayhook"); + return NULL; + } + return PyObject_CallOneArg(hook, value); +} + +static int +import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) +{ + PyObject *all, *dict, *name, *value; + int skip_leading_underscores = 0; + int pos, err; + + if (_PyObject_LookupAttr(v, &_Py_ID(__all__), &all) < 0) { + return -1; /* Unexpected error */ + } + if (all == NULL) { + if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &dict) < 0) { + return -1; + } + if (dict == NULL) { + _PyErr_SetString(tstate, PyExc_ImportError, + "from-import-* object has no __dict__ and no __all__"); + return -1; + } + all = PyMapping_Keys(dict); + Py_DECREF(dict); + if (all == NULL) + return -1; + skip_leading_underscores = 1; + } + + for (pos = 0, err = 0; ; pos++) { + name = PySequence_GetItem(all, pos); + if (name == NULL) { + if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { + err = -1; + } + else { + _PyErr_Clear(tstate); + } + break; + } + if (!PyUnicode_Check(name)) { + PyObject *modname = PyObject_GetAttr(v, &_Py_ID(__name__)); + if (modname == NULL) { + Py_DECREF(name); + err = -1; + break; + } + if (!PyUnicode_Check(modname)) { + _PyErr_Format(tstate, PyExc_TypeError, + "module __name__ must be a string, not %.100s", + Py_TYPE(modname)->tp_name); + } + else { + _PyErr_Format(tstate, PyExc_TypeError, + "%s in %U.%s must be str, not %.100s", + skip_leading_underscores ? "Key" : "Item", + modname, + skip_leading_underscores ? "__dict__" : "__all__", + Py_TYPE(name)->tp_name); + } + Py_DECREF(modname); + Py_DECREF(name); + err = -1; + break; + } + if (skip_leading_underscores) { + if (PyUnicode_READY(name) == -1) { + Py_DECREF(name); + err = -1; + break; + } + if (PyUnicode_READ_CHAR(name, 0) == '_') { + Py_DECREF(name); + continue; + } + } + value = PyObject_GetAttr(v, name); + if (value == NULL) + err = -1; + else if (PyDict_CheckExact(locals)) + err = PyDict_SetItem(locals, name, value); + else + err = PyObject_SetItem(locals, name, value); + Py_DECREF(name); + Py_XDECREF(value); + if (err != 0) + break; + } + Py_DECREF(all); + return err; +} + +static PyObject * +import_star(PyThreadState* tstate, PyObject *from) +{ + _PyInterpreterFrame *frame = tstate->cframe->current_frame; + if (_PyFrame_FastToLocalsWithError(frame) < 0) { + return NULL; + } + + PyObject *locals = frame->f_locals; + if (locals == NULL) { + _PyErr_SetString(tstate, PyExc_SystemError, + "no locals found during 'import *'"); + return NULL; + } + int err = import_all_from(tstate, locals, from); + _PyFrame_LocalsToFast(frame, 0); + if (err) { + return NULL; + } + Py_RETURN_NONE; +} + +static PyObject * +stopiteration_error(PyThreadState* tstate, PyObject *exc) +{ + _PyInterpreterFrame *frame = tstate->cframe->current_frame; + assert(frame->owner == FRAME_OWNED_BY_GENERATOR); + assert(PyExceptionInstance_Check(exc)); + const char *msg = NULL; + if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) { + msg = "generator raised StopIteration"; + if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) { + msg = "async generator raised StopIteration"; + } + else if (frame->f_code->co_flags & CO_COROUTINE) { + msg = "coroutine raised StopIteration"; + } + } + else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) && + PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) + { + /* code in `gen` raised a StopAsyncIteration error: + raise a RuntimeError. + */ + msg = "async generator raised StopAsyncIteration"; + } + if (msg != NULL) { + PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg)); + if (message == NULL) { + return NULL; + } + PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message); + if (error == NULL) { + Py_DECREF(message); + return NULL; + } + assert(PyExceptionInstance_Check(error)); + PyException_SetCause(error, Py_NewRef(exc)); + // Steal exc reference, rather than Py_NewRef+Py_DECREF + PyException_SetContext(error, exc); + Py_DECREF(message); + return error; + } + return exc; +} + +instrinsic_func1 +_PyIntrinsics_UnaryFunctions[] = { + + [INTRINSIC_PRINT] = print_expr, + [INRINSIC_IMPORT_STAR] = import_star, + [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error, +}; + + diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index be3ad01c151c04..4e2cb03a71f298 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -63,29 +63,29 @@ static void *opcode_targets[256] = { &&TARGET_DELETE_SUBSCR, &&TARGET_FOR_ITER_TUPLE, &&TARGET_STOPITERATION_ERROR, + &&TARGET_CALL_INTRINSIC_1, &&TARGET_FOR_ITER_RANGE, &&TARGET_FOR_ITER_GEN, &&TARGET_LOAD_ATTR_CLASS, - &&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, &&TARGET_GET_ITER, &&TARGET_GET_YIELD_FROM_ITER, &&TARGET_PRINT_EXPR, &&TARGET_LOAD_BUILD_CLASS, + &&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, &&TARGET_LOAD_ATTR_INSTANCE_VALUE, - &&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ASSERTION_ERROR, &&TARGET_RETURN_GENERATOR, + &&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ATTR_PROPERTY, &&TARGET_LOAD_ATTR_SLOT, &&TARGET_LOAD_ATTR_WITH_HINT, &&TARGET_LOAD_ATTR_METHOD_LAZY_DICT, &&TARGET_LOAD_ATTR_METHOD_NO_DICT, - &&TARGET_LOAD_ATTR_METHOD_WITH_DICT, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, &&TARGET_IMPORT_STAR, &&TARGET_SETUP_ANNOTATIONS, - &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, + &&TARGET_LOAD_ATTR_METHOD_WITH_DICT, &&TARGET_ASYNC_GEN_WRAP, &&TARGET_PREP_RERAISE_STAR, &&TARGET_POP_EXCEPT, @@ -112,7 +112,7 @@ static void *opcode_targets[256] = { &&TARGET_JUMP_FORWARD, &&TARGET_JUMP_IF_FALSE_OR_POP, &&TARGET_JUMP_IF_TRUE_OR_POP, - &&TARGET_LOAD_CONST__LOAD_FAST, + &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, &&TARGET_POP_JUMP_IF_FALSE, &&TARGET_POP_JUMP_IF_TRUE, &&TARGET_LOAD_GLOBAL, @@ -120,7 +120,7 @@ static void *opcode_targets[256] = { &&TARGET_CONTAINS_OP, &&TARGET_RERAISE, &&TARGET_COPY, - &&TARGET_LOAD_FAST__LOAD_CONST, + &&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_BINARY_OP, &&TARGET_SEND, &&TARGET_LOAD_FAST, @@ -140,9 +140,9 @@ static void *opcode_targets[256] = { &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, &&TARGET_JUMP_BACKWARD, - &&TARGET_LOAD_FAST__LOAD_FAST, + &&TARGET_LOAD_FAST__LOAD_CONST, &&TARGET_CALL_FUNCTION_EX, - &&TARGET_LOAD_GLOBAL_BUILTIN, + &&TARGET_LOAD_FAST__LOAD_FAST, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, @@ -152,27 +152,27 @@ static void *opcode_targets[256] = { &&TARGET_YIELD_VALUE, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, + &&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_LOAD_GLOBAL_MODULE, - &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, + &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_STORE_ATTR_SLOT, &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_FAST__LOAD_FAST, - &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_LIST_EXTEND, &&TARGET_SET_UPDATE, &&TARGET_DICT_MERGE, &&TARGET_DICT_UPDATE, + &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_STORE_SUBSCR_DICT, &&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_UNPACK_SEQUENCE_TUPLE, - &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&TARGET_CALL, &&TARGET_KW_NAMES, - &&_unknown_opcode, + &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, From f0eafa915ca3e96e9f6e1a464360ead6bfc72c9c Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 12:39:12 +0000 Subject: [PATCH 02/13] Remove PRINT_EXPR instruction, replacing it with CALL_INTRINSIC_1. --- Include/internal/pycore_opcode.h | 26 ++++++++-------- Include/opcode.h | 51 ++++++++++++++++---------------- Lib/opcode.py | 2 +- Python/bytecodes.c | 16 ---------- Python/compile.c | 6 ++-- Python/generated_cases.c.h | 19 ------------ Python/opcode_targets.h | 24 +++++++-------- 7 files changed, 54 insertions(+), 90 deletions(-) diff --git a/Include/internal/pycore_opcode.h b/Include/internal/pycore_opcode.h index 58d7201d199337..8726af7a746072 100644 --- a/Include/internal/pycore_opcode.h +++ b/Include/internal/pycore_opcode.h @@ -189,7 +189,6 @@ const uint8_t _PyOpcode_Deopt[256] = { [POP_JUMP_IF_TRUE] = POP_JUMP_IF_TRUE, [POP_TOP] = POP_TOP, [PREP_RERAISE_STAR] = PREP_RERAISE_STAR, - [PRINT_EXPR] = PRINT_EXPR, [PUSH_EXC_INFO] = PUSH_EXC_INFO, [PUSH_NULL] = PUSH_NULL, [RAISE_VARARGS] = RAISE_VARARGS, @@ -303,23 +302,23 @@ static const char *const _PyOpcode_OpName[263] = { [LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS", [GET_ITER] = "GET_ITER", [GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER", - [PRINT_EXPR] = "PRINT_EXPR", - [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", [LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN", + [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", [LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE", + [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR", [RETURN_GENERATOR] = "RETURN_GENERATOR", - [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY", [LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT", [LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT", [LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT", [LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT", + [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", [LIST_TO_TUPLE] = "LIST_TO_TUPLE", [RETURN_VALUE] = "RETURN_VALUE", [IMPORT_STAR] = "IMPORT_STAR", [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", - [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", + [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", [ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP", [PREP_RERAISE_STAR] = "PREP_RERAISE_STAR", [POP_EXCEPT] = "POP_EXCEPT", @@ -346,7 +345,7 @@ static const char *const _PyOpcode_OpName[263] = { [JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP", [JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP", - [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", + [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", [POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE", [POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE", [LOAD_GLOBAL] = "LOAD_GLOBAL", @@ -354,7 +353,7 @@ static const char *const _PyOpcode_OpName[263] = { [CONTAINS_OP] = "CONTAINS_OP", [RERAISE] = "RERAISE", [COPY] = "COPY", - [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", + [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", [BINARY_OP] = "BINARY_OP", [SEND] = "SEND", [LOAD_FAST] = "LOAD_FAST", @@ -374,9 +373,9 @@ static const char *const _PyOpcode_OpName[263] = { [STORE_DEREF] = "STORE_DEREF", [DELETE_DEREF] = "DELETE_DEREF", [JUMP_BACKWARD] = "JUMP_BACKWARD", - [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", - [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", + [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", + [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [EXTENDED_ARG] = "EXTENDED_ARG", [LIST_APPEND] = "LIST_APPEND", [SET_ADD] = "SET_ADD", @@ -386,27 +385,27 @@ static const char *const _PyOpcode_OpName[263] = { [YIELD_VALUE] = "YIELD_VALUE", [RESUME] = "RESUME", [MATCH_CLASS] = "MATCH_CLASS", - [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", + [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [FORMAT_VALUE] = "FORMAT_VALUE", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_STRING] = "BUILD_STRING", - [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", + [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", [LIST_EXTEND] = "LIST_EXTEND", [SET_UPDATE] = "SET_UPDATE", [DICT_MERGE] = "DICT_MERGE", [DICT_UPDATE] = "DICT_UPDATE", - [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", + [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", [CALL] = "CALL", [KW_NAMES] = "KW_NAMES", - [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", + [173] = "<173>", [174] = "<174>", [175] = "<175>", [176] = "<176>", @@ -500,6 +499,7 @@ static const char *const _PyOpcode_OpName[263] = { #endif #define EXTRA_CASES \ + case 173: \ case 174: \ case 175: \ case 176: \ diff --git a/Include/opcode.h b/Include/opcode.h index 1bda01772332ea..ea09ddad753375 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -41,7 +41,6 @@ extern "C" { #define CALL_INTRINSIC_1 64 #define GET_ITER 68 #define GET_YIELD_FROM_ITER 69 -#define PRINT_EXPR 70 #define LOAD_BUILD_CLASS 71 #define LOAD_ASSERTION_ERROR 74 #define RETURN_GENERATOR 75 @@ -167,31 +166,31 @@ extern "C" { #define FOR_ITER_RANGE 65 #define FOR_ITER_GEN 66 #define LOAD_ATTR_CLASS 67 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 72 -#define LOAD_ATTR_INSTANCE_VALUE 73 -#define LOAD_ATTR_MODULE 76 -#define LOAD_ATTR_PROPERTY 77 -#define LOAD_ATTR_SLOT 78 -#define LOAD_ATTR_WITH_HINT 79 -#define LOAD_ATTR_METHOD_LAZY_DICT 80 -#define LOAD_ATTR_METHOD_NO_DICT 81 -#define LOAD_ATTR_METHOD_WITH_DICT 86 -#define LOAD_ATTR_METHOD_WITH_VALUES 113 -#define LOAD_CONST__LOAD_FAST 121 -#define LOAD_FAST__LOAD_CONST 141 -#define LOAD_FAST__LOAD_FAST 143 -#define LOAD_GLOBAL_BUILTIN 153 -#define LOAD_GLOBAL_MODULE 154 -#define STORE_ATTR_INSTANCE_VALUE 158 -#define STORE_ATTR_SLOT 159 -#define STORE_ATTR_WITH_HINT 160 -#define STORE_FAST__LOAD_FAST 161 -#define STORE_FAST__STORE_FAST 166 -#define STORE_SUBSCR_DICT 167 -#define STORE_SUBSCR_LIST_INT 168 -#define UNPACK_SEQUENCE_LIST 169 -#define UNPACK_SEQUENCE_TUPLE 170 -#define UNPACK_SEQUENCE_TWO_TUPLE 173 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 70 +#define LOAD_ATTR_INSTANCE_VALUE 72 +#define LOAD_ATTR_MODULE 73 +#define LOAD_ATTR_PROPERTY 76 +#define LOAD_ATTR_SLOT 77 +#define LOAD_ATTR_WITH_HINT 78 +#define LOAD_ATTR_METHOD_LAZY_DICT 79 +#define LOAD_ATTR_METHOD_NO_DICT 80 +#define LOAD_ATTR_METHOD_WITH_DICT 81 +#define LOAD_ATTR_METHOD_WITH_VALUES 86 +#define LOAD_CONST__LOAD_FAST 113 +#define LOAD_FAST__LOAD_CONST 121 +#define LOAD_FAST__LOAD_FAST 141 +#define LOAD_GLOBAL_BUILTIN 143 +#define LOAD_GLOBAL_MODULE 153 +#define STORE_ATTR_INSTANCE_VALUE 154 +#define STORE_ATTR_SLOT 158 +#define STORE_ATTR_WITH_HINT 159 +#define STORE_FAST__LOAD_FAST 160 +#define STORE_FAST__STORE_FAST 161 +#define STORE_SUBSCR_DICT 166 +#define STORE_SUBSCR_LIST_INT 167 +#define UNPACK_SEQUENCE_LIST 168 +#define UNPACK_SEQUENCE_TUPLE 169 +#define UNPACK_SEQUENCE_TWO_TUPLE 170 #define DO_TRACING 255 #define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\ diff --git a/Lib/opcode.py b/Lib/opcode.py index b1d082ed401351..7fcd3402162194 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -117,7 +117,7 @@ def pseudo_op(name, op, real_ops): def_op('GET_ITER', 68) def_op('GET_YIELD_FROM_ITER', 69) -def_op('PRINT_EXPR', 70) + def_op('LOAD_BUILD_CLASS', 71) def_op('LOAD_ASSERTION_ERROR', 74) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index b755576d393bf3..ff703d4cd5f624 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -539,22 +539,6 @@ dummy_func( ERROR_IF(err, error); } - inst(PRINT_EXPR, (value --)) { - PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(displayhook)); - PyObject *res; - // Can't use ERROR_IF here. - if (hook == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "lost sys.displayhook"); - DECREF_INPUTS(); - ERROR_IF(true, error); - } - res = PyObject_CallOneArg(hook, value); - DECREF_INPUTS(); - ERROR_IF(res == NULL, error); - Py_DECREF(res); - } - inst(CALL_INTRINSIC_1, (value -- res)) { res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value); Py_DECREF(value); diff --git a/Python/compile.c b/Python/compile.c index 97bc07e5057f98..25a73394254dfd 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -30,6 +30,7 @@ #include "pycore_ast.h" // _PyAST_GetDocString() #include "pycore_code.h" // _PyCode_New() #include "pycore_compile.h" // _PyFuture_FromAST() +#include "pycore_intrinsics.h" #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_opcode.h" // _PyOpcode_Caches #include "pycore_pymem.h" // _PyMem_IsPtrFreed() @@ -1113,8 +1114,6 @@ stack_effect(int opcode, int oparg, int jump) case GET_ITER: return 0; - case PRINT_EXPR: - return -1; case LOAD_BUILD_CLASS: return 1; @@ -4006,7 +4005,8 @@ compiler_stmt_expr(struct compiler *c, location loc, expr_ty value) { if (c->c_interactive && c->c_nestlevel <= 1) { VISIT(c, expr, value); - ADDOP(c, loc, PRINT_EXPR); + ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PRINT); + ADDOP(c, NO_LOCATION, POP_TOP); return SUCCESS; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 1b25e69c4d9ad4..411d23fb4e259a 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -684,25 +684,6 @@ DISPATCH(); } - TARGET(PRINT_EXPR) { - PyObject *value = PEEK(1); - PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(displayhook)); - PyObject *res; - // Can't use ERROR_IF here. - if (hook == NULL) { - _PyErr_SetString(tstate, PyExc_RuntimeError, - "lost sys.displayhook"); - Py_DECREF(value); - if (true) goto pop_1_error; - } - res = PyObject_CallOneArg(hook, value); - Py_DECREF(value); - if (res == NULL) goto pop_1_error; - Py_DECREF(res); - STACK_SHRINK(1); - DISPATCH(); - } - TARGET(CALL_INTRINSIC_1) { PyObject *value = PEEK(1); PyObject *res; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 4e2cb03a71f298..235edd6b7786dc 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -69,23 +69,23 @@ static void *opcode_targets[256] = { &&TARGET_LOAD_ATTR_CLASS, &&TARGET_GET_ITER, &&TARGET_GET_YIELD_FROM_ITER, - &&TARGET_PRINT_EXPR, - &&TARGET_LOAD_BUILD_CLASS, &&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, + &&TARGET_LOAD_BUILD_CLASS, &&TARGET_LOAD_ATTR_INSTANCE_VALUE, + &&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ASSERTION_ERROR, &&TARGET_RETURN_GENERATOR, - &&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ATTR_PROPERTY, &&TARGET_LOAD_ATTR_SLOT, &&TARGET_LOAD_ATTR_WITH_HINT, &&TARGET_LOAD_ATTR_METHOD_LAZY_DICT, &&TARGET_LOAD_ATTR_METHOD_NO_DICT, + &&TARGET_LOAD_ATTR_METHOD_WITH_DICT, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, &&TARGET_IMPORT_STAR, &&TARGET_SETUP_ANNOTATIONS, - &&TARGET_LOAD_ATTR_METHOD_WITH_DICT, + &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, &&TARGET_ASYNC_GEN_WRAP, &&TARGET_PREP_RERAISE_STAR, &&TARGET_POP_EXCEPT, @@ -112,7 +112,7 @@ static void *opcode_targets[256] = { &&TARGET_JUMP_FORWARD, &&TARGET_JUMP_IF_FALSE_OR_POP, &&TARGET_JUMP_IF_TRUE_OR_POP, - &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, + &&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_POP_JUMP_IF_FALSE, &&TARGET_POP_JUMP_IF_TRUE, &&TARGET_LOAD_GLOBAL, @@ -120,7 +120,7 @@ static void *opcode_targets[256] = { &&TARGET_CONTAINS_OP, &&TARGET_RERAISE, &&TARGET_COPY, - &&TARGET_LOAD_CONST__LOAD_FAST, + &&TARGET_LOAD_FAST__LOAD_CONST, &&TARGET_BINARY_OP, &&TARGET_SEND, &&TARGET_LOAD_FAST, @@ -140,9 +140,9 @@ static void *opcode_targets[256] = { &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, &&TARGET_JUMP_BACKWARD, - &&TARGET_LOAD_FAST__LOAD_CONST, - &&TARGET_CALL_FUNCTION_EX, &&TARGET_LOAD_FAST__LOAD_FAST, + &&TARGET_CALL_FUNCTION_EX, + &&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, @@ -152,27 +152,27 @@ static void *opcode_targets[256] = { &&TARGET_YIELD_VALUE, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, - &&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_LOAD_GLOBAL_MODULE, + &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, - &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_STORE_ATTR_SLOT, &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_FAST__LOAD_FAST, + &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_LIST_EXTEND, &&TARGET_SET_UPDATE, &&TARGET_DICT_MERGE, &&TARGET_DICT_UPDATE, - &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_STORE_SUBSCR_DICT, &&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_UNPACK_SEQUENCE_TUPLE, + &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&TARGET_CALL, &&TARGET_KW_NAMES, - &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, + &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, From 85108611f961f537063ecd9754b137ae7950eb14 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 13:08:30 +0000 Subject: [PATCH 03/13] Remove STOPITERATION_ERROR instruction, replacing it with CALL_INTRINSIC_1 --- Include/internal/pycore_opcode.h | 42 ++++++++++---------- Include/opcode.h | 59 ++++++++++++++-------------- Lib/importlib/_bootstrap_external.py | 3 +- Lib/opcode.py | 5 +-- Lib/test/test_dis.py | 2 +- Python/bytecodes.c | 41 ------------------- Python/compile.c | 6 +-- Python/generated_cases.c.h | 42 -------------------- Python/intrinsics.c | 4 +- Python/opcode_targets.h | 38 +++++++++--------- 10 files changed, 76 insertions(+), 166 deletions(-) diff --git a/Include/internal/pycore_opcode.h b/Include/internal/pycore_opcode.h index 8726af7a746072..34bb1e52fb558b 100644 --- a/Include/internal/pycore_opcode.h +++ b/Include/internal/pycore_opcode.h @@ -200,7 +200,6 @@ const uint8_t _PyOpcode_Deopt[256] = { [SETUP_ANNOTATIONS] = SETUP_ANNOTATIONS, [SET_ADD] = SET_ADD, [SET_UPDATE] = SET_UPDATE, - [STOPITERATION_ERROR] = STOPITERATION_ERROR, [STORE_ATTR] = STORE_ATTR, [STORE_ATTR_INSTANCE_VALUE] = STORE_ATTR, [STORE_ATTR_SLOT] = STORE_ATTR, @@ -295,30 +294,30 @@ static const char *const _PyOpcode_OpName[263] = { [STORE_SUBSCR] = "STORE_SUBSCR", [DELETE_SUBSCR] = "DELETE_SUBSCR", [FOR_ITER_TUPLE] = "FOR_ITER_TUPLE", - [STOPITERATION_ERROR] = "STOPITERATION_ERROR", - [CALL_INTRINSIC_1] = "CALL_INTRINSIC_1", [FOR_ITER_RANGE] = "FOR_ITER_RANGE", [FOR_ITER_GEN] = "FOR_ITER_GEN", [LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS", - [GET_ITER] = "GET_ITER", - [GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER", [LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN", - [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", [LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE", + [GET_ITER] = "GET_ITER", + [GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER", [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", - [LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR", - [RETURN_GENERATOR] = "RETURN_GENERATOR", + [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", [LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY", [LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT", + [LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR", + [RETURN_GENERATOR] = "RETURN_GENERATOR", [LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT", [LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT", [LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT", [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", + [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", + [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", [LIST_TO_TUPLE] = "LIST_TO_TUPLE", [RETURN_VALUE] = "RETURN_VALUE", [IMPORT_STAR] = "IMPORT_STAR", [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", - [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", + [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", [ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP", [PREP_RERAISE_STAR] = "PREP_RERAISE_STAR", [POP_EXCEPT] = "POP_EXCEPT", @@ -345,7 +344,7 @@ static const char *const _PyOpcode_OpName[263] = { [JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP", [JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP", - [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", + [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", [POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE", [POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE", [LOAD_GLOBAL] = "LOAD_GLOBAL", @@ -353,7 +352,7 @@ static const char *const _PyOpcode_OpName[263] = { [CONTAINS_OP] = "CONTAINS_OP", [RERAISE] = "RERAISE", [COPY] = "COPY", - [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", + [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [BINARY_OP] = "BINARY_OP", [SEND] = "SEND", [LOAD_FAST] = "LOAD_FAST", @@ -373,9 +372,9 @@ static const char *const _PyOpcode_OpName[263] = { [STORE_DEREF] = "STORE_DEREF", [DELETE_DEREF] = "DELETE_DEREF", [JUMP_BACKWARD] = "JUMP_BACKWARD", - [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", + [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", - [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", + [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [EXTENDED_ARG] = "EXTENDED_ARG", [LIST_APPEND] = "LIST_APPEND", [SET_ADD] = "SET_ADD", @@ -385,27 +384,27 @@ static const char *const _PyOpcode_OpName[263] = { [YIELD_VALUE] = "YIELD_VALUE", [RESUME] = "RESUME", [MATCH_CLASS] = "MATCH_CLASS", - [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", - [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", + [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", + [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [FORMAT_VALUE] = "FORMAT_VALUE", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_STRING] = "BUILD_STRING", - [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", - [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", + [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", + [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [LIST_EXTEND] = "LIST_EXTEND", [SET_UPDATE] = "SET_UPDATE", [DICT_MERGE] = "DICT_MERGE", [DICT_UPDATE] = "DICT_UPDATE", - [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", - [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", + [169] = "<169>", + [170] = "<170>", [CALL] = "CALL", [KW_NAMES] = "KW_NAMES", - [173] = "<173>", + [CALL_INTRINSIC_1] = "CALL_INTRINSIC_1", [174] = "<174>", [175] = "<175>", [176] = "<176>", @@ -499,7 +498,8 @@ static const char *const _PyOpcode_OpName[263] = { #endif #define EXTRA_CASES \ - case 173: \ + case 169: \ + case 170: \ case 174: \ case 175: \ case 176: \ diff --git a/Include/opcode.h b/Include/opcode.h index ea09ddad753375..18fbc0114c9b25 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -37,8 +37,6 @@ extern "C" { #define CLEANUP_THROW 55 #define STORE_SUBSCR 60 #define DELETE_SUBSCR 61 -#define STOPITERATION_ERROR 63 -#define CALL_INTRINSIC_1 64 #define GET_ITER 68 #define GET_YIELD_FROM_ITER 69 #define LOAD_BUILD_CLASS 71 @@ -120,6 +118,7 @@ extern "C" { #define DICT_UPDATE 165 #define CALL 171 #define KW_NAMES 172 +#define CALL_INTRINSIC_1 173 #define MIN_PSEUDO_OPCODE 256 #define SETUP_FINALLY 256 #define SETUP_CLEANUP 257 @@ -163,34 +162,34 @@ extern "C" { #define COMPARE_OP_STR_JUMP 58 #define FOR_ITER_LIST 59 #define FOR_ITER_TUPLE 62 -#define FOR_ITER_RANGE 65 -#define FOR_ITER_GEN 66 -#define LOAD_ATTR_CLASS 67 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 70 -#define LOAD_ATTR_INSTANCE_VALUE 72 -#define LOAD_ATTR_MODULE 73 -#define LOAD_ATTR_PROPERTY 76 -#define LOAD_ATTR_SLOT 77 -#define LOAD_ATTR_WITH_HINT 78 -#define LOAD_ATTR_METHOD_LAZY_DICT 79 -#define LOAD_ATTR_METHOD_NO_DICT 80 -#define LOAD_ATTR_METHOD_WITH_DICT 81 -#define LOAD_ATTR_METHOD_WITH_VALUES 86 -#define LOAD_CONST__LOAD_FAST 113 -#define LOAD_FAST__LOAD_CONST 121 -#define LOAD_FAST__LOAD_FAST 141 -#define LOAD_GLOBAL_BUILTIN 143 -#define LOAD_GLOBAL_MODULE 153 -#define STORE_ATTR_INSTANCE_VALUE 154 -#define STORE_ATTR_SLOT 158 -#define STORE_ATTR_WITH_HINT 159 -#define STORE_FAST__LOAD_FAST 160 -#define STORE_FAST__STORE_FAST 161 -#define STORE_SUBSCR_DICT 166 -#define STORE_SUBSCR_LIST_INT 167 -#define UNPACK_SEQUENCE_LIST 168 -#define UNPACK_SEQUENCE_TUPLE 169 -#define UNPACK_SEQUENCE_TWO_TUPLE 170 +#define FOR_ITER_RANGE 63 +#define FOR_ITER_GEN 64 +#define LOAD_ATTR_CLASS 65 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 66 +#define LOAD_ATTR_INSTANCE_VALUE 67 +#define LOAD_ATTR_MODULE 70 +#define LOAD_ATTR_PROPERTY 72 +#define LOAD_ATTR_SLOT 73 +#define LOAD_ATTR_WITH_HINT 76 +#define LOAD_ATTR_METHOD_LAZY_DICT 77 +#define LOAD_ATTR_METHOD_NO_DICT 78 +#define LOAD_ATTR_METHOD_WITH_DICT 79 +#define LOAD_ATTR_METHOD_WITH_VALUES 80 +#define LOAD_CONST__LOAD_FAST 81 +#define LOAD_FAST__LOAD_CONST 86 +#define LOAD_FAST__LOAD_FAST 113 +#define LOAD_GLOBAL_BUILTIN 121 +#define LOAD_GLOBAL_MODULE 141 +#define STORE_ATTR_INSTANCE_VALUE 143 +#define STORE_ATTR_SLOT 153 +#define STORE_ATTR_WITH_HINT 154 +#define STORE_FAST__LOAD_FAST 158 +#define STORE_FAST__STORE_FAST 159 +#define STORE_SUBSCR_DICT 160 +#define STORE_SUBSCR_LIST_INT 161 +#define UNPACK_SEQUENCE_LIST 166 +#define UNPACK_SEQUENCE_TUPLE 167 +#define UNPACK_SEQUENCE_TWO_TUPLE 168 #define DO_TRACING 255 #define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\ diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 71a16064b8ec0a..d258d8693db710 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -426,6 +426,7 @@ def _write_atomic(path, data, mode=0o666): # Python 3.12a1 3510 (FOR_ITER leaves iterator on the stack) # Python 3.12a1 3511 (Add STOPITERATION_ERROR instruction) # Python 3.12a1 3512 (Remove all unused consts from code objects) +# Python 3.12a1 3513 (Add CALL_INTRINSIC_1 instruction) # Python 3.13 will start with 3550 @@ -438,7 +439,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 = (3512).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3514).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c diff --git a/Lib/opcode.py b/Lib/opcode.py index 7fcd3402162194..c8312d74d19c76 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -112,9 +112,6 @@ def pseudo_op(name, op, real_ops): def_op('STORE_SUBSCR', 60) def_op('DELETE_SUBSCR', 61) -def_op('STOPITERATION_ERROR', 63) -def_op('CALL_INTRINSIC_1', 64) - def_op('GET_ITER', 68) def_op('GET_YIELD_FROM_ITER', 69) @@ -221,7 +218,7 @@ def pseudo_op(name, op, real_ops): def_op('CALL', 171) def_op('KW_NAMES', 172) hasconst.append(172) - +def_op('CALL_INTRINSIC_1', 173) hasarg.extend([op for op in opmap.values() if op >= HAVE_ARGUMENT]) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 950af3ceb24fea..c2a339ac7cbae4 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -543,7 +543,7 @@ async def _asyncwith(c): >> COPY 3 POP_EXCEPT RERAISE 1 - >> STOPITERATION_ERROR + >> CALL_INTRINSIC_1 3 RERAISE 1 ExceptionTable: 12 rows diff --git a/Python/bytecodes.c b/Python/bytecodes.c index ff703d4cd5f624..2347ae55ab5f4d 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -858,47 +858,6 @@ dummy_func( } } - inst(STOPITERATION_ERROR) { - assert(frame->owner == FRAME_OWNED_BY_GENERATOR); - PyObject *exc = TOP(); - assert(PyExceptionInstance_Check(exc)); - const char *msg = NULL; - if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) { - msg = "generator raised StopIteration"; - if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) { - msg = "async generator raised StopIteration"; - } - else if (frame->f_code->co_flags & CO_COROUTINE) { - msg = "coroutine raised StopIteration"; - } - } - else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) && - PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) - { - /* code in `gen` raised a StopAsyncIteration error: - raise a RuntimeError. - */ - msg = "async generator raised StopAsyncIteration"; - } - if (msg != NULL) { - PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg)); - if (message == NULL) { - goto error; - } - PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message); - if (error == NULL) { - Py_DECREF(message); - goto error; - } - assert(PyExceptionInstance_Check(error)); - SET_TOP(error); - PyException_SetCause(error, Py_NewRef(exc)); - // Steal exc reference, rather than Py_NewRef+Py_DECREF - PyException_SetContext(error, exc); - Py_DECREF(message); - } - } - inst(LOAD_ASSERTION_ERROR, ( -- value)) { value = Py_NewRef(PyExc_AssertionError); } diff --git a/Python/compile.c b/Python/compile.c index 25a73394254dfd..f62b6e0741a278 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1215,10 +1215,6 @@ stack_effect(int opcode, int oparg, int jump) * of __(a)enter__ and push 2 values before jumping to the handler * if an exception be raised. */ return jump ? 1 : 0; - - case STOPITERATION_ERROR: - return 0; - case PREP_RERAISE_STAR: return -1; case RERAISE: @@ -2604,7 +2600,7 @@ wrap_in_stopiteration_handler(struct compiler *c) ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); ADDOP(c, NO_LOCATION, RETURN_VALUE); USE_LABEL(c, handler); - ADDOP(c, NO_LOCATION, STOPITERATION_ERROR); + ADDOP_I(c, NO_LOCATION, CALL_INTRINSIC_1, INTRINSIC_STOPITERATION_ERROR); ADDOP_I(c, NO_LOCATION, RERAISE, 1); return SUCCESS; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 411d23fb4e259a..490c66b983ed47 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1036,48 +1036,6 @@ DISPATCH(); } - TARGET(STOPITERATION_ERROR) { - assert(frame->owner == FRAME_OWNED_BY_GENERATOR); - PyObject *exc = TOP(); - assert(PyExceptionInstance_Check(exc)); - const char *msg = NULL; - if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) { - msg = "generator raised StopIteration"; - if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) { - msg = "async generator raised StopIteration"; - } - else if (frame->f_code->co_flags & CO_COROUTINE) { - msg = "coroutine raised StopIteration"; - } - } - else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) && - PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) - { - /* code in `gen` raised a StopAsyncIteration error: - raise a RuntimeError. - */ - msg = "async generator raised StopAsyncIteration"; - } - if (msg != NULL) { - PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg)); - if (message == NULL) { - goto error; - } - PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message); - if (error == NULL) { - Py_DECREF(message); - goto error; - } - assert(PyExceptionInstance_Check(error)); - SET_TOP(error); - PyException_SetCause(error, Py_NewRef(exc)); - // Steal exc reference, rather than Py_NewRef+Py_DECREF - PyException_SetContext(error, exc); - Py_DECREF(message); - } - DISPATCH(); - } - TARGET(LOAD_ASSERTION_ERROR) { PyObject *value; value = Py_NewRef(PyExc_AssertionError); diff --git a/Python/intrinsics.c b/Python/intrinsics.c index de3ab528c95543..0c14b810fe04f4 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -171,11 +171,11 @@ stopiteration_error(PyThreadState* tstate, PyObject *exc) assert(PyExceptionInstance_Check(error)); PyException_SetCause(error, Py_NewRef(exc)); // Steal exc reference, rather than Py_NewRef+Py_DECREF - PyException_SetContext(error, exc); + PyException_SetContext(error, Py_NewRef(exc)); Py_DECREF(message); return error; } - return exc; + return Py_NewRef(exc); } instrinsic_func1 diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 235edd6b7786dc..e96a6102bbad83 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -62,30 +62,30 @@ static void *opcode_targets[256] = { &&TARGET_STORE_SUBSCR, &&TARGET_DELETE_SUBSCR, &&TARGET_FOR_ITER_TUPLE, - &&TARGET_STOPITERATION_ERROR, - &&TARGET_CALL_INTRINSIC_1, &&TARGET_FOR_ITER_RANGE, &&TARGET_FOR_ITER_GEN, &&TARGET_LOAD_ATTR_CLASS, - &&TARGET_GET_ITER, - &&TARGET_GET_YIELD_FROM_ITER, &&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, - &&TARGET_LOAD_BUILD_CLASS, &&TARGET_LOAD_ATTR_INSTANCE_VALUE, + &&TARGET_GET_ITER, + &&TARGET_GET_YIELD_FROM_ITER, &&TARGET_LOAD_ATTR_MODULE, - &&TARGET_LOAD_ASSERTION_ERROR, - &&TARGET_RETURN_GENERATOR, + &&TARGET_LOAD_BUILD_CLASS, &&TARGET_LOAD_ATTR_PROPERTY, &&TARGET_LOAD_ATTR_SLOT, + &&TARGET_LOAD_ASSERTION_ERROR, + &&TARGET_RETURN_GENERATOR, &&TARGET_LOAD_ATTR_WITH_HINT, &&TARGET_LOAD_ATTR_METHOD_LAZY_DICT, &&TARGET_LOAD_ATTR_METHOD_NO_DICT, &&TARGET_LOAD_ATTR_METHOD_WITH_DICT, + &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, + &&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, &&TARGET_IMPORT_STAR, &&TARGET_SETUP_ANNOTATIONS, - &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, + &&TARGET_LOAD_FAST__LOAD_CONST, &&TARGET_ASYNC_GEN_WRAP, &&TARGET_PREP_RERAISE_STAR, &&TARGET_POP_EXCEPT, @@ -112,7 +112,7 @@ static void *opcode_targets[256] = { &&TARGET_JUMP_FORWARD, &&TARGET_JUMP_IF_FALSE_OR_POP, &&TARGET_JUMP_IF_TRUE_OR_POP, - &&TARGET_LOAD_CONST__LOAD_FAST, + &&TARGET_LOAD_FAST__LOAD_FAST, &&TARGET_POP_JUMP_IF_FALSE, &&TARGET_POP_JUMP_IF_TRUE, &&TARGET_LOAD_GLOBAL, @@ -120,7 +120,7 @@ static void *opcode_targets[256] = { &&TARGET_CONTAINS_OP, &&TARGET_RERAISE, &&TARGET_COPY, - &&TARGET_LOAD_FAST__LOAD_CONST, + &&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_BINARY_OP, &&TARGET_SEND, &&TARGET_LOAD_FAST, @@ -140,9 +140,9 @@ static void *opcode_targets[256] = { &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, &&TARGET_JUMP_BACKWARD, - &&TARGET_LOAD_FAST__LOAD_FAST, + &&TARGET_LOAD_GLOBAL_MODULE, &&TARGET_CALL_FUNCTION_EX, - &&TARGET_LOAD_GLOBAL_BUILTIN, + &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, @@ -152,27 +152,27 @@ static void *opcode_targets[256] = { &&TARGET_YIELD_VALUE, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, - &&TARGET_LOAD_GLOBAL_MODULE, - &&TARGET_STORE_ATTR_INSTANCE_VALUE, + &&TARGET_STORE_ATTR_SLOT, + &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, - &&TARGET_STORE_ATTR_SLOT, - &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_STORE_FAST__STORE_FAST, + &&TARGET_STORE_SUBSCR_DICT, + &&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_LIST_EXTEND, &&TARGET_SET_UPDATE, &&TARGET_DICT_MERGE, &&TARGET_DICT_UPDATE, - &&TARGET_STORE_SUBSCR_DICT, - &&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_UNPACK_SEQUENCE_TUPLE, &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, + &&_unknown_opcode, + &&_unknown_opcode, &&TARGET_CALL, &&TARGET_KW_NAMES, - &&_unknown_opcode, + &&TARGET_CALL_INTRINSIC_1, &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, From 6e99cc71db5ea32fc10f9ff67f962609cc83f6af Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 13:45:58 +0000 Subject: [PATCH 04/13] Remove IMPORT_STAR instruction, replacing it with CALL_INTRINSIC_1 --- Include/internal/pycore_intrinsics.h | 2 +- Include/internal/pycore_opcode.h | 20 +++---- Include/opcode.h | 29 +++++---- Lib/importlib/_bootstrap_external.py | 2 +- Lib/opcode.py | 2 +- Python/bytecodes.c | 21 ------- Python/ceval.c | 90 ---------------------------- Python/compile.c | 5 +- Python/generated_cases.c.h | 24 -------- Python/intrinsics.c | 2 +- Python/opcode_targets.h | 18 +++--- 11 files changed, 39 insertions(+), 176 deletions(-) diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h index 0175b2923820da..3a4bee412e2344 100644 --- a/Include/internal/pycore_intrinsics.h +++ b/Include/internal/pycore_intrinsics.h @@ -4,7 +4,7 @@ typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value); #define INTRINSIC_PRINT 1 -#define INRINSIC_IMPORT_STAR 2 +#define INTRINSIC_IMPORT_STAR 2 #define INTRINSIC_STOPITERATION_ERROR 3 extern instrinsic_func1 diff --git a/Include/internal/pycore_opcode.h b/Include/internal/pycore_opcode.h index 34bb1e52fb558b..b364fdfe771268 100644 --- a/Include/internal/pycore_opcode.h +++ b/Include/internal/pycore_opcode.h @@ -135,7 +135,6 @@ const uint8_t _PyOpcode_Deopt[256] = { [GET_YIELD_FROM_ITER] = GET_YIELD_FROM_ITER, [IMPORT_FROM] = IMPORT_FROM, [IMPORT_NAME] = IMPORT_NAME, - [IMPORT_STAR] = IMPORT_STAR, [INTERPRETER_EXIT] = INTERPRETER_EXIT, [IS_OP] = IS_OP, [JUMP_BACKWARD] = JUMP_BACKWARD, @@ -315,9 +314,9 @@ static const char *const _PyOpcode_OpName[263] = { [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", [LIST_TO_TUPLE] = "LIST_TO_TUPLE", [RETURN_VALUE] = "RETURN_VALUE", - [IMPORT_STAR] = "IMPORT_STAR", - [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", [LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", + [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", + [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", [ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP", [PREP_RERAISE_STAR] = "PREP_RERAISE_STAR", [POP_EXCEPT] = "POP_EXCEPT", @@ -344,7 +343,7 @@ static const char *const _PyOpcode_OpName[263] = { [JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP", [JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP", - [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", + [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE", [POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE", [LOAD_GLOBAL] = "LOAD_GLOBAL", @@ -352,7 +351,7 @@ static const char *const _PyOpcode_OpName[263] = { [CONTAINS_OP] = "CONTAINS_OP", [RERAISE] = "RERAISE", [COPY] = "COPY", - [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", + [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", [BINARY_OP] = "BINARY_OP", [SEND] = "SEND", [LOAD_FAST] = "LOAD_FAST", @@ -372,9 +371,9 @@ static const char *const _PyOpcode_OpName[263] = { [STORE_DEREF] = "STORE_DEREF", [DELETE_DEREF] = "DELETE_DEREF", [JUMP_BACKWARD] = "JUMP_BACKWARD", - [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", - [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", + [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", + [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [EXTENDED_ARG] = "EXTENDED_ARG", [LIST_APPEND] = "LIST_APPEND", [SET_ADD] = "SET_ADD", @@ -384,22 +383,22 @@ static const char *const _PyOpcode_OpName[263] = { [YIELD_VALUE] = "YIELD_VALUE", [RESUME] = "RESUME", [MATCH_CLASS] = "MATCH_CLASS", - [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", + [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", [FORMAT_VALUE] = "FORMAT_VALUE", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_STRING] = "BUILD_STRING", - [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", [STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST", [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", + [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [LIST_EXTEND] = "LIST_EXTEND", [SET_UPDATE] = "SET_UPDATE", [DICT_MERGE] = "DICT_MERGE", [DICT_UPDATE] = "DICT_UPDATE", - [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", + [168] = "<168>", [169] = "<169>", [170] = "<170>", [CALL] = "CALL", @@ -498,6 +497,7 @@ static const char *const _PyOpcode_OpName[263] = { #endif #define EXTRA_CASES \ + case 168: \ case 169: \ case 170: \ case 174: \ diff --git a/Include/opcode.h b/Include/opcode.h index 18fbc0114c9b25..93d549d0f5e6c9 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -44,7 +44,6 @@ extern "C" { #define RETURN_GENERATOR 75 #define LIST_TO_TUPLE 82 #define RETURN_VALUE 83 -#define IMPORT_STAR 84 #define SETUP_ANNOTATIONS 85 #define ASYNC_GEN_WRAP 87 #define PREP_RERAISE_STAR 88 @@ -176,20 +175,20 @@ extern "C" { #define LOAD_ATTR_METHOD_WITH_DICT 79 #define LOAD_ATTR_METHOD_WITH_VALUES 80 #define LOAD_CONST__LOAD_FAST 81 -#define LOAD_FAST__LOAD_CONST 86 -#define LOAD_FAST__LOAD_FAST 113 -#define LOAD_GLOBAL_BUILTIN 121 -#define LOAD_GLOBAL_MODULE 141 -#define STORE_ATTR_INSTANCE_VALUE 143 -#define STORE_ATTR_SLOT 153 -#define STORE_ATTR_WITH_HINT 154 -#define STORE_FAST__LOAD_FAST 158 -#define STORE_FAST__STORE_FAST 159 -#define STORE_SUBSCR_DICT 160 -#define STORE_SUBSCR_LIST_INT 161 -#define UNPACK_SEQUENCE_LIST 166 -#define UNPACK_SEQUENCE_TUPLE 167 -#define UNPACK_SEQUENCE_TWO_TUPLE 168 +#define LOAD_FAST__LOAD_CONST 84 +#define LOAD_FAST__LOAD_FAST 86 +#define LOAD_GLOBAL_BUILTIN 113 +#define LOAD_GLOBAL_MODULE 121 +#define STORE_ATTR_INSTANCE_VALUE 141 +#define STORE_ATTR_SLOT 143 +#define STORE_ATTR_WITH_HINT 153 +#define STORE_FAST__LOAD_FAST 154 +#define STORE_FAST__STORE_FAST 158 +#define STORE_SUBSCR_DICT 159 +#define STORE_SUBSCR_LIST_INT 160 +#define UNPACK_SEQUENCE_LIST 161 +#define UNPACK_SEQUENCE_TUPLE 166 +#define UNPACK_SEQUENCE_TWO_TUPLE 167 #define DO_TRACING 255 #define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\ diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index d258d8693db710..afc82df8586d51 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -439,7 +439,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 = (3514).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3513).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c diff --git a/Lib/opcode.py b/Lib/opcode.py index c8312d74d19c76..84a6a4e7157485 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -122,7 +122,7 @@ def pseudo_op(name, op, real_ops): def_op('LIST_TO_TUPLE', 82) def_op('RETURN_VALUE', 83) -def_op('IMPORT_STAR', 84) + def_op('SETUP_ANNOTATIONS', 85) def_op('ASYNC_GEN_WRAP', 87) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 2347ae55ab5f4d..6d023d639ba6d1 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2015,27 +2015,6 @@ dummy_func( ERROR_IF(res == NULL, error); } - inst(IMPORT_STAR, (from --)) { - PyObject *locals; - int err; - if (_PyFrame_FastToLocalsWithError(frame) < 0) { - DECREF_INPUTS(); - ERROR_IF(true, error); - } - - locals = LOCALS(); - if (locals == NULL) { - _PyErr_SetString(tstate, PyExc_SystemError, - "no locals found during 'import *'"); - DECREF_INPUTS(); - ERROR_IF(true, error); - } - err = import_all_from(tstate, locals, from); - _PyFrame_LocalsToFast(frame, 0); - DECREF_INPUTS(); - ERROR_IF(err, error); - } - inst(IMPORT_FROM, (from -- from, res)) { PyObject *name = GETITEM(names, oparg); res = import_from(tstate, from, name); diff --git a/Python/ceval.c b/Python/ceval.c index eb9d37f23883c8..149b88e1545d0a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -205,7 +205,6 @@ static void dtrace_function_return(_PyInterpreterFrame *); static PyObject * import_name(PyThreadState *, _PyInterpreterFrame *, PyObject *, PyObject *, PyObject *); static PyObject * import_from(PyThreadState *, PyObject *, PyObject *); -static int import_all_from(PyThreadState *, PyObject *, PyObject *); static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *); static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg); static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg); @@ -3157,95 +3156,6 @@ import_from(PyThreadState *tstate, PyObject *v, PyObject *name) return NULL; } -static int -import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) -{ - PyObject *all, *dict, *name, *value; - int skip_leading_underscores = 0; - int pos, err; - - if (_PyObject_LookupAttr(v, &_Py_ID(__all__), &all) < 0) { - return -1; /* Unexpected error */ - } - if (all == NULL) { - if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &dict) < 0) { - return -1; - } - if (dict == NULL) { - _PyErr_SetString(tstate, PyExc_ImportError, - "from-import-* object has no __dict__ and no __all__"); - return -1; - } - all = PyMapping_Keys(dict); - Py_DECREF(dict); - if (all == NULL) - return -1; - skip_leading_underscores = 1; - } - - for (pos = 0, err = 0; ; pos++) { - name = PySequence_GetItem(all, pos); - if (name == NULL) { - if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { - err = -1; - } - else { - _PyErr_Clear(tstate); - } - break; - } - if (!PyUnicode_Check(name)) { - PyObject *modname = PyObject_GetAttr(v, &_Py_ID(__name__)); - if (modname == NULL) { - Py_DECREF(name); - err = -1; - break; - } - if (!PyUnicode_Check(modname)) { - _PyErr_Format(tstate, PyExc_TypeError, - "module __name__ must be a string, not %.100s", - Py_TYPE(modname)->tp_name); - } - else { - _PyErr_Format(tstate, PyExc_TypeError, - "%s in %U.%s must be str, not %.100s", - skip_leading_underscores ? "Key" : "Item", - modname, - skip_leading_underscores ? "__dict__" : "__all__", - Py_TYPE(name)->tp_name); - } - Py_DECREF(modname); - Py_DECREF(name); - err = -1; - break; - } - if (skip_leading_underscores) { - if (PyUnicode_READY(name) == -1) { - Py_DECREF(name); - err = -1; - break; - } - if (PyUnicode_READ_CHAR(name, 0) == '_') { - Py_DECREF(name); - continue; - } - } - value = PyObject_GetAttr(v, name); - if (value == NULL) - err = -1; - else if (PyDict_CheckExact(locals)) - err = PyDict_SetItem(locals, name, value); - else - err = PyObject_SetItem(locals, name, value); - Py_DECREF(name); - Py_XDECREF(value); - if (err != 0) - break; - } - Py_DECREF(all); - return err; -} - #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ "BaseException is not allowed" diff --git a/Python/compile.c b/Python/compile.c index f62b6e0741a278..ff29fb42c5d108 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1119,8 +1119,6 @@ stack_effect(int opcode, int oparg, int jump) case RETURN_VALUE: return -1; - case IMPORT_STAR: - return -1; case SETUP_ANNOTATIONS: return 0; case ASYNC_GEN_WRAP: @@ -3949,7 +3947,8 @@ compiler_from_import(struct compiler *c, stmt_ty s) if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { assert(n == 1); - ADDOP(c, LOC(s), IMPORT_STAR); + ADDOP_I(c, LOC(s), CALL_INTRINSIC_1, INTRINSIC_IMPORT_STAR); + ADDOP(c, NO_LOCATION, POP_TOP); return SUCCESS; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 490c66b983ed47..95d55e0e58b34c 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2350,30 +2350,6 @@ DISPATCH(); } - TARGET(IMPORT_STAR) { - PyObject *from = PEEK(1); - PyObject *locals; - int err; - if (_PyFrame_FastToLocalsWithError(frame) < 0) { - Py_DECREF(from); - if (true) goto pop_1_error; - } - - locals = LOCALS(); - if (locals == NULL) { - _PyErr_SetString(tstate, PyExc_SystemError, - "no locals found during 'import *'"); - Py_DECREF(from); - if (true) goto pop_1_error; - } - err = import_all_from(tstate, locals, from); - _PyFrame_LocalsToFast(frame, 0); - Py_DECREF(from); - if (err) goto pop_1_error; - STACK_SHRINK(1); - DISPATCH(); - } - TARGET(IMPORT_FROM) { PyObject *from = PEEK(1); PyObject *res; diff --git a/Python/intrinsics.c b/Python/intrinsics.c index 0c14b810fe04f4..a8561aab6628ec 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -182,7 +182,7 @@ instrinsic_func1 _PyIntrinsics_UnaryFunctions[] = { [INTRINSIC_PRINT] = print_expr, - [INRINSIC_IMPORT_STAR] = import_star, + [INTRINSIC_IMPORT_STAR] = import_star, [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error, }; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index e96a6102bbad83..f06e37be39c1e6 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -83,9 +83,9 @@ static void *opcode_targets[256] = { &&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, - &&TARGET_IMPORT_STAR, - &&TARGET_SETUP_ANNOTATIONS, &&TARGET_LOAD_FAST__LOAD_CONST, + &&TARGET_SETUP_ANNOTATIONS, + &&TARGET_LOAD_FAST__LOAD_FAST, &&TARGET_ASYNC_GEN_WRAP, &&TARGET_PREP_RERAISE_STAR, &&TARGET_POP_EXCEPT, @@ -112,7 +112,7 @@ static void *opcode_targets[256] = { &&TARGET_JUMP_FORWARD, &&TARGET_JUMP_IF_FALSE_OR_POP, &&TARGET_JUMP_IF_TRUE_OR_POP, - &&TARGET_LOAD_FAST__LOAD_FAST, + &&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_POP_JUMP_IF_FALSE, &&TARGET_POP_JUMP_IF_TRUE, &&TARGET_LOAD_GLOBAL, @@ -120,7 +120,7 @@ static void *opcode_targets[256] = { &&TARGET_CONTAINS_OP, &&TARGET_RERAISE, &&TARGET_COPY, - &&TARGET_LOAD_GLOBAL_BUILTIN, + &&TARGET_LOAD_GLOBAL_MODULE, &&TARGET_BINARY_OP, &&TARGET_SEND, &&TARGET_LOAD_FAST, @@ -140,9 +140,9 @@ static void *opcode_targets[256] = { &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, &&TARGET_JUMP_BACKWARD, - &&TARGET_LOAD_GLOBAL_MODULE, - &&TARGET_CALL_FUNCTION_EX, &&TARGET_STORE_ATTR_INSTANCE_VALUE, + &&TARGET_CALL_FUNCTION_EX, + &&TARGET_STORE_ATTR_SLOT, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, @@ -152,24 +152,24 @@ static void *opcode_targets[256] = { &&TARGET_YIELD_VALUE, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, - &&TARGET_STORE_ATTR_SLOT, &&TARGET_STORE_ATTR_WITH_HINT, + &&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, - &&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_STORE_SUBSCR_DICT, &&TARGET_STORE_SUBSCR_LIST_INT, + &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_LIST_EXTEND, &&TARGET_SET_UPDATE, &&TARGET_DICT_MERGE, &&TARGET_DICT_UPDATE, - &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_UNPACK_SEQUENCE_TUPLE, &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&_unknown_opcode, &&_unknown_opcode, + &&_unknown_opcode, &&TARGET_CALL, &&TARGET_KW_NAMES, &&TARGET_CALL_INTRINSIC_1, From 66d3ea41d503d8c3e96ba90c853c1153f6f35a7c Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 13:58:21 +0000 Subject: [PATCH 05/13] Add news and update dis docs. --- Doc/library/dis.rst | 32 ++++++------------- ...-01-05-13-54-00.gh-issue-100770.D7H6j4.rst | 4 +++ 2 files changed, 13 insertions(+), 23 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 30bbf95be63417..e541341938de89 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -607,15 +607,6 @@ the original TOS1. .. versionadded:: 3.12 -.. opcode:: STOPITERATION_ERROR - - Handles a StopIteration raised in a generator or coroutine. - If TOS is an instance of :exc:`StopIteration`, or :exc:`StopAsyncIteration` - replace it with a :exc:`RuntimeError`. - - .. versionadded:: 3.12 - - .. opcode:: BEFORE_ASYNC_WITH Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the @@ -627,13 +618,6 @@ the original TOS1. **Miscellaneous opcodes** -.. opcode:: PRINT_EXPR - - Implements the expression statement for the interactive mode. TOS is removed - from the stack and printed. In non-interactive mode, an expression statement - is terminated with :opcode:`POP_TOP`. - - .. opcode:: SET_ADD (i) Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions. @@ -682,13 +666,6 @@ iterations of the loop. .. versionadded:: 3.6 -.. opcode:: IMPORT_STAR - - Loads all symbols not starting with ``'_'`` directly from the module TOS to - the local namespace. The module is popped after loading all names. This - opcode implements ``from module import *``. - - .. opcode:: POP_EXCEPT Pops a value from the stack, which is used to restore the exception state. @@ -1422,6 +1399,15 @@ iterations of the loop. they use their arg. +.. opcode:: CALL_INTRINSIC_1 + + Calls an intrinsic function with one argument. Passes the TOS as the argument + and sets TOS to the result. Used to implement functionality that is necessary + but not performance critical. + + .. versionadded:: 3.12 + + **Pseudo-instructions** These opcodes do not appear in python bytecode, they are used by the compiler diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst new file mode 100644 index 00000000000000..bd81cde45bc90b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst @@ -0,0 +1,4 @@ +Add new :opcode:`CALL_INSTRINSIC_1` instruction. Remove +:opcode:`IMPORT_STAR`, :opcode:`PRINT_EXPR` and +:opcode:`STOPITERATION_ERROR`, replacing them with the +:opcode:`CALL_INSTRINSIC_1` instruction. From fcf2806d26f61d49fcae900efc0ba5baeef1cc05 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 14:03:40 +0000 Subject: [PATCH 06/13] Fix issue number. --- ...0.D7H6j4.rst => 2023-01-05-13-54-00.gh-issue-99005.D7H6j4.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Core and Builtins/{2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst => 2023-01-05-13-54-00.gh-issue-99005.D7H6j4.rst} (100%) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-99005.D7H6j4.rst similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-100770.D7H6j4.rst rename to Misc/NEWS.d/next/Core and Builtins/2023-01-05-13-54-00.gh-issue-99005.D7H6j4.rst From 9c6cbbc100c11e2074bc8e30a573b0e17bece32d Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 14:19:18 +0000 Subject: [PATCH 07/13] Fix typo in project file. --- PCbuild/pythoncore.vcxproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index 53ed5f959779f3..78f5234c63420b 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -231,7 +231,7 @@ - From c5ecb5f2c4a4da8277d4d04d7525f0d3caeb13d2 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 14:23:24 +0000 Subject: [PATCH 08/13] Tidy up formatting. --- Include/internal/pycore_intrinsics.h | 9 +++------ Python/intrinsics.c | 3 --- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h index 3a4bee412e2344..8ed900bf5c7663 100644 --- a/Include/internal/pycore_intrinsics.h +++ b/Include/internal/pycore_intrinsics.h @@ -1,11 +1,8 @@ - -typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value); - - #define INTRINSIC_PRINT 1 #define INTRINSIC_IMPORT_STAR 2 #define INTRINSIC_STOPITERATION_ERROR 3 -extern instrinsic_func1 -_PyIntrinsics_UnaryFunctions[]; +typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value); + +extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[]; diff --git a/Python/intrinsics.c b/Python/intrinsics.c index a8561aab6628ec..e9e1a89ea12384 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -180,10 +180,7 @@ stopiteration_error(PyThreadState* tstate, PyObject *exc) instrinsic_func1 _PyIntrinsics_UnaryFunctions[] = { - [INTRINSIC_PRINT] = print_expr, [INTRINSIC_IMPORT_STAR] = import_star, [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error, }; - - From 8bad358c12a5555d9c684bd9426d1008a65052ee Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 15:01:01 +0000 Subject: [PATCH 09/13] Make error code checking consistent. --- Python/intrinsics.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/intrinsics.c b/Python/intrinsics.c index e9e1a89ea12384..a03ff880f24c27 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -105,7 +105,7 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) err = PyObject_SetItem(locals, name, value); Py_DECREF(name); Py_XDECREF(value); - if (err != 0) + if (err < 0) break; } Py_DECREF(all); @@ -128,7 +128,7 @@ import_star(PyThreadState* tstate, PyObject *from) } int err = import_all_from(tstate, locals, from); _PyFrame_LocalsToFast(frame, 0); - if (err) { + if (err < 0) { return NULL; } Py_RETURN_NONE; From a39ac2be6d8f800f063b0e40bea7b89575140df8 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 15:07:27 +0000 Subject: [PATCH 10/13] Make CALL_INTRINSIC_1 a bit more robust --- Include/internal/pycore_intrinsics.h | 2 ++ Python/bytecodes.c | 1 + Python/intrinsics.c | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h index 8ed900bf5c7663..92f06c73626794 100644 --- a/Include/internal/pycore_intrinsics.h +++ b/Include/internal/pycore_intrinsics.h @@ -3,6 +3,8 @@ #define INTRINSIC_IMPORT_STAR 2 #define INTRINSIC_STOPITERATION_ERROR 3 +#define MAX_INTRINSIC_1 3 + typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value); extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[]; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index b30d23f00578c4..9283f590582a03 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -540,6 +540,7 @@ dummy_func( } inst(CALL_INTRINSIC_1, (value -- res)) { + assert(oparg <= MAX_INTRINSIC_1); res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value); Py_DECREF(value); ERROR_IF(res == NULL, error); diff --git a/Python/intrinsics.c b/Python/intrinsics.c index a03ff880f24c27..07b9c6a97c6e56 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -10,6 +10,13 @@ +static PyObject * +no_intrinsic(PyThreadState* tstate, PyObject *unused) +{ + _PyErr_SetString(tstate, PyExc_SystemError, "invalid intrinsic function"); + return NULL; +} + static PyObject * print_expr(PyThreadState* tstate, PyObject *value) { @@ -180,6 +187,7 @@ stopiteration_error(PyThreadState* tstate, PyObject *exc) instrinsic_func1 _PyIntrinsics_UnaryFunctions[] = { + [0] = no_intrinsic, [INTRINSIC_PRINT] = print_expr, [INTRINSIC_IMPORT_STAR] = import_star, [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error, From a0a4ba67ed6975122135c109da386ca7585f73b6 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 15:11:39 +0000 Subject: [PATCH 11/13] Document the operand of CALL_INTRINSIC_1 --- Doc/library/dis.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index e541341938de89..33ef6d7ed495ca 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -1405,6 +1405,13 @@ iterations of the loop. and sets TOS to the result. Used to implement functionality that is necessary but not performance critical. + The operand determines which intrinsic function is called: + + * ``0`` Not valid + * ``1`` Prints the argument to standard out. Used in the REPL. + * ``2`` Performs ``import *`` for the named module. + * ``3`` Extracts the return value from a ``StopIteration`` exception. + .. versionadded:: 3.12 From e42c99dd1031e859e4de066ab19e287c13f34ec2 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 15:12:59 +0000 Subject: [PATCH 12/13] Update Lib/importlib/_bootstrap_external.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/importlib/_bootstrap_external.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index afc82df8586d51..e700ce634acae7 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -426,7 +426,7 @@ def _write_atomic(path, data, mode=0o666): # Python 3.12a1 3510 (FOR_ITER leaves iterator on the stack) # Python 3.12a1 3511 (Add STOPITERATION_ERROR instruction) # Python 3.12a1 3512 (Remove all unused consts from code objects) -# Python 3.12a1 3513 (Add CALL_INTRINSIC_1 instruction) +# Python 3.12a1 3513 (Add CALL_INTRINSIC_1 instruction, removed STOPITERATION_ERROR, PRINT_EXPR, IMPORT_STAR) # Python 3.13 will start with 3550 From abb24b9517fe3fd04b29765129e00a7fc1d3a191 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 5 Jan 2023 15:38:34 +0000 Subject: [PATCH 13/13] Update generated_cases.c.h --- Python/generated_cases.c.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 2b6e5f3528f6e3..0d4dad40ea546b 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -687,6 +687,7 @@ TARGET(CALL_INTRINSIC_1) { PyObject *value = PEEK(1); PyObject *res; + assert(oparg <= MAX_INTRINSIC_1); res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value); Py_DECREF(value); if (res == NULL) goto pop_1_error; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy