Kaydet (Commit) 332cd5ee authored tarafından Mark Shannon's avatar Mark Shannon Kaydeden (comit) Raymond Hettinger

bpo-32550. Remove the STORE_ANNOTATION bytecode. (GH-5181)

üst b6e43af6
...@@ -993,13 +993,6 @@ All of the following opcodes use their arguments. ...@@ -993,13 +993,6 @@ All of the following opcodes use their arguments.
Deletes local ``co_varnames[var_num]``. Deletes local ``co_varnames[var_num]``.
.. opcode:: STORE_ANNOTATION (namei)
Stores TOS as ``locals()['__annotations__'][co_names[namei]] = TOS``.
.. versionadded:: 3.6
.. opcode:: LOAD_CLOSURE (i) .. opcode:: LOAD_CLOSURE (i)
Pushes a reference to the cell contained in slot *i* of the cell and free Pushes a reference to the cell contained in slot *i* of the cell and free
......
...@@ -1173,6 +1173,8 @@ CPython bytecode changes ...@@ -1173,6 +1173,8 @@ CPython bytecode changes
* Added two new opcodes: :opcode:`LOAD_METHOD` and :opcode:`CALL_METHOD`. * Added two new opcodes: :opcode:`LOAD_METHOD` and :opcode:`CALL_METHOD`.
(Contributed by Yury Selivanov and INADA Naoki in :issue:`26110`.) (Contributed by Yury Selivanov and INADA Naoki in :issue:`26110`.)
* Removed the STORE_ANNOTATION opcode.
Other CPython implementation changes Other CPython implementation changes
------------------------------------ ------------------------------------
......
...@@ -99,7 +99,6 @@ extern "C" { ...@@ -99,7 +99,6 @@ extern "C" {
#define LOAD_FAST 124 #define LOAD_FAST 124
#define STORE_FAST 125 #define STORE_FAST 125
#define DELETE_FAST 126 #define DELETE_FAST 126
#define STORE_ANNOTATION 127
#define RAISE_VARARGS 130 #define RAISE_VARARGS 130
#define CALL_FUNCTION 131 #define CALL_FUNCTION 131
#define MAKE_FUNCTION 132 #define MAKE_FUNCTION 132
......
...@@ -242,6 +242,7 @@ _code_type = type(_write_atomic.__code__) ...@@ -242,6 +242,7 @@ _code_type = type(_write_atomic.__code__)
# Python 3.7a0 3390 (add LOAD_METHOD and CALL_METHOD opcodes) # Python 3.7a0 3390 (add LOAD_METHOD and CALL_METHOD opcodes)
# Python 3.7a0 3391 (update GET_AITER #31709) # Python 3.7a0 3391 (update GET_AITER #31709)
# Python 3.7a0 3392 (PEP 552: Deterministic pycs) # Python 3.7a0 3392 (PEP 552: Deterministic pycs)
# Python 3.7a0 3393 (remove STORE_ANNOTATION opcode)
# #
# MAGIC must change whenever the bytecode emitted by the compiler may no # MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually # longer be understood by older implementations of the eval loop (usually
...@@ -250,7 +251,7 @@ _code_type = type(_write_atomic.__code__) ...@@ -250,7 +251,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated. # in PC/launcher.c must also be updated.
MAGIC_NUMBER = (3392).to_bytes(2, 'little') + b'\r\n' MAGIC_NUMBER = (3393).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__' _PYCACHE = '__pycache__'
......
...@@ -169,7 +169,6 @@ def_op('STORE_FAST', 125) # Local variable number ...@@ -169,7 +169,6 @@ def_op('STORE_FAST', 125) # Local variable number
haslocal.append(125) haslocal.append(125)
def_op('DELETE_FAST', 126) # Local variable number def_op('DELETE_FAST', 126) # Local variable number
haslocal.append(126) haslocal.append(126)
name_op('STORE_ANNOTATION', 127) # Index in name list
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
def_op('CALL_FUNCTION', 131) # #args def_op('CALL_FUNCTION', 131) # #args
......
...@@ -226,23 +226,27 @@ dis_annot_stmt_str = """\ ...@@ -226,23 +226,27 @@ dis_annot_stmt_str = """\
2 LOAD_CONST 0 (1) 2 LOAD_CONST 0 (1)
4 STORE_NAME 0 (x) 4 STORE_NAME 0 (x)
6 LOAD_NAME 1 (int) 6 LOAD_NAME 1 (int)
8 STORE_ANNOTATION 0 (x) 8 LOAD_NAME 2 (__annotations__)
10 LOAD_CONST 1 ('x')
3 10 LOAD_NAME 2 (fun) 12 STORE_SUBSCR
12 LOAD_CONST 0 (1)
14 CALL_FUNCTION 1 3 14 LOAD_NAME 3 (fun)
16 STORE_ANNOTATION 3 (y) 16 LOAD_CONST 0 (1)
18 CALL_FUNCTION 1
4 18 LOAD_CONST 0 (1) 20 LOAD_NAME 2 (__annotations__)
20 LOAD_NAME 4 (lst) 22 LOAD_CONST 2 ('y')
22 LOAD_NAME 2 (fun) 24 STORE_SUBSCR
24 LOAD_CONST 1 (0)
26 CALL_FUNCTION 1 4 26 LOAD_CONST 0 (1)
28 STORE_SUBSCR 28 LOAD_NAME 4 (lst)
30 LOAD_NAME 1 (int) 30 LOAD_NAME 3 (fun)
32 POP_TOP 32 LOAD_CONST 3 (0)
34 LOAD_CONST 2 (None) 34 CALL_FUNCTION 1
36 RETURN_VALUE 36 STORE_SUBSCR
38 LOAD_NAME 1 (int)
40 POP_TOP
42 LOAD_CONST 4 (None)
44 RETURN_VALUE
""" """
compound_stmt_str = """\ compound_stmt_str = """\
......
...@@ -1574,61 +1574,6 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) ...@@ -1574,61 +1574,6 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
DISPATCH(); DISPATCH();
} }
TARGET(STORE_ANNOTATION) {
_Py_IDENTIFIER(__annotations__);
PyObject *ann_dict;
PyObject *ann = POP();
PyObject *name = GETITEM(names, oparg);
int err;
if (f->f_locals == NULL) {
PyErr_Format(PyExc_SystemError,
"no locals found when storing annotation");
Py_DECREF(ann);
goto error;
}
/* first try to get __annotations__ from locals... */
if (PyDict_CheckExact(f->f_locals)) {
ann_dict = _PyDict_GetItemId(f->f_locals,
&PyId___annotations__);
if (ann_dict == NULL) {
PyErr_SetString(PyExc_NameError,
"__annotations__ not found");
Py_DECREF(ann);
goto error;
}
Py_INCREF(ann_dict);
}
else {
PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
if (ann_str == NULL) {
Py_DECREF(ann);
goto error;
}
ann_dict = PyObject_GetItem(f->f_locals, ann_str);
if (ann_dict == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_SetString(PyExc_NameError,
"__annotations__ not found");
}
Py_DECREF(ann);
goto error;
}
}
/* ...if succeeded, __annotations__[name] = ann */
if (PyDict_CheckExact(ann_dict)) {
err = PyDict_SetItem(ann_dict, name, ann);
}
else {
err = PyObject_SetItem(ann_dict, name, ann);
}
Py_DECREF(ann_dict);
Py_DECREF(ann);
if (err != 0) {
goto error;
}
DISPATCH();
}
TARGET(DELETE_SUBSCR) { TARGET(DELETE_SUBSCR) {
PyObject *sub = TOP(); PyObject *sub = TOP();
PyObject *container = SECOND(); PyObject *container = SECOND();
......
...@@ -213,7 +213,7 @@ static int compiler_async_comprehension_generator( ...@@ -213,7 +213,7 @@ static int compiler_async_comprehension_generator(
expr_ty elt, expr_ty val, int type); expr_ty elt, expr_ty val, int type);
static PyCodeObject *assemble(struct compiler *, int addNone); static PyCodeObject *assemble(struct compiler *, int addNone);
static PyObject *__doc__; static PyObject *__doc__, *__annotations__;
#define CAPSULE_NAME "compile.c compiler unit" #define CAPSULE_NAME "compile.c compiler unit"
...@@ -311,7 +311,11 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, ...@@ -311,7 +311,11 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
if (!__doc__) if (!__doc__)
return NULL; return NULL;
} }
if (!__annotations__) {
__annotations__ = PyUnicode_InternFromString("__annotations__");
if (!__annotations__)
return NULL;
}
if (!compiler_init(&c)) if (!compiler_init(&c))
return NULL; return NULL;
Py_INCREF(filename); Py_INCREF(filename);
...@@ -1056,8 +1060,6 @@ stack_effect(int opcode, int oparg, int jump) ...@@ -1056,8 +1060,6 @@ stack_effect(int opcode, int oparg, int jump)
return -1; return -1;
case DELETE_FAST: case DELETE_FAST:
return 0; return 0;
case STORE_ANNOTATION:
return -1;
case RAISE_VARARGS: case RAISE_VARARGS:
return -oparg; return -oparg;
...@@ -4711,8 +4713,10 @@ compiler_annassign(struct compiler *c, stmt_ty s) ...@@ -4711,8 +4713,10 @@ compiler_annassign(struct compiler *c, stmt_ty s)
else { else {
VISIT(c, expr, s->v.AnnAssign.annotation); VISIT(c, expr, s->v.AnnAssign.annotation);
} }
/* ADDOP_N decrefs its argument */ ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
ADDOP_N(c, STORE_ANNOTATION, mangled, names); ADDOP_O(c, LOAD_CONST, mangled, consts);
Py_DECREF(mangled);
ADDOP(c, STORE_SUBSCR);
} }
break; break;
case Attribute_kind: case Attribute_kind:
......
This diff is collapsed.
...@@ -126,7 +126,7 @@ static void *opcode_targets[256] = { ...@@ -126,7 +126,7 @@ static void *opcode_targets[256] = {
&&TARGET_LOAD_FAST, &&TARGET_LOAD_FAST,
&&TARGET_STORE_FAST, &&TARGET_STORE_FAST,
&&TARGET_DELETE_FAST, &&TARGET_DELETE_FAST,
&&TARGET_STORE_ANNOTATION, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,
&&TARGET_RAISE_VARARGS, &&TARGET_RAISE_VARARGS,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment