diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-03-17 21:41:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-17 21:41:08 (GMT) |
commit | 6b97598fb66a08d0f36e4d73bffea5c1b17740d4 (patch) | |
tree | 5fb1922e733a71ad26788b1d5f3f9575ca015600 /Python | |
parent | dab8423d220243efabbbcafafc12d90145539b50 (diff) | |
download | cpython-6b97598fb66a08d0f36e4d73bffea5c1b17740d4.zip cpython-6b97598fb66a08d0f36e4d73bffea5c1b17740d4.tar.gz cpython-6b97598fb66a08d0f36e4d73bffea5c1b17740d4.tar.bz2 |
bpo-39988: Remove ast.AugLoad and ast.AugStore node classes. (GH-19038)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/Python-ast.c | 62 | ||||
-rw-r--r-- | Python/ast.c | 13 | ||||
-rw-r--r-- | Python/compile.c | 143 |
3 files changed, 45 insertions, 173 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 96ecc31..aba83fb 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -21,10 +21,6 @@ typedef struct { PyObject *AsyncWith_type; PyObject *Attribute_type; PyObject *AugAssign_type; - PyObject *AugLoad_singleton; - PyObject *AugLoad_type; - PyObject *AugStore_singleton; - PyObject *AugStore_type; PyObject *Await_type; PyObject *BinOp_type; PyObject *BitAnd_singleton; @@ -245,10 +241,6 @@ static int astmodule_clear(PyObject *module) Py_CLEAR(astmodulestate(module)->AsyncWith_type); Py_CLEAR(astmodulestate(module)->Attribute_type); Py_CLEAR(astmodulestate(module)->AugAssign_type); - Py_CLEAR(astmodulestate(module)->AugLoad_singleton); - Py_CLEAR(astmodulestate(module)->AugLoad_type); - Py_CLEAR(astmodulestate(module)->AugStore_singleton); - Py_CLEAR(astmodulestate(module)->AugStore_type); Py_CLEAR(astmodulestate(module)->Await_type); Py_CLEAR(astmodulestate(module)->BinOp_type); Py_CLEAR(astmodulestate(module)->BitAnd_singleton); @@ -468,10 +460,6 @@ static int astmodule_traverse(PyObject *module, visitproc visit, void* arg) Py_VISIT(astmodulestate(module)->AsyncWith_type); Py_VISIT(astmodulestate(module)->Attribute_type); Py_VISIT(astmodulestate(module)->AugAssign_type); - Py_VISIT(astmodulestate(module)->AugLoad_singleton); - Py_VISIT(astmodulestate(module)->AugLoad_type); - Py_VISIT(astmodulestate(module)->AugStore_singleton); - Py_VISIT(astmodulestate(module)->AugStore_type); Py_VISIT(astmodulestate(module)->Await_type); Py_VISIT(astmodulestate(module)->BinOp_type); Py_VISIT(astmodulestate(module)->BitAnd_singleton); @@ -1728,7 +1716,7 @@ static int init_types(void) return 0; state->expr_context_type = make_type("expr_context", state->AST_type, NULL, 0, - "expr_context = Load | Store | Del | AugLoad | AugStore"); + "expr_context = Load | Store | Del"); if (!state->expr_context_type) return 0; if (!add_attributes(state->expr_context_type, NULL, 0)) return 0; state->Load_type = make_type("Load", state->expr_context_type, NULL, 0, @@ -1749,22 +1737,6 @@ static int init_types(void) state->Del_singleton = PyType_GenericNew((PyTypeObject *)state->Del_type, NULL, NULL); if (!state->Del_singleton) return 0; - state->AugLoad_type = make_type("AugLoad", state->expr_context_type, NULL, - 0, - "AugLoad"); - if (!state->AugLoad_type) return 0; - state->AugLoad_singleton = PyType_GenericNew((PyTypeObject - *)state->AugLoad_type, NULL, - NULL); - if (!state->AugLoad_singleton) return 0; - state->AugStore_type = make_type("AugStore", state->expr_context_type, - NULL, 0, - "AugStore"); - if (!state->AugStore_type) return 0; - state->AugStore_singleton = PyType_GenericNew((PyTypeObject - *)state->AugStore_type, NULL, - NULL); - if (!state->AugStore_singleton) return 0; state->boolop_type = make_type("boolop", state->AST_type, NULL, 0, "boolop = And | Or"); if (!state->boolop_type) return 0; @@ -4614,12 +4586,6 @@ PyObject* ast2obj_expr_context(expr_context_ty o) case Del: Py_INCREF(astmodulestate_global->Del_singleton); return astmodulestate_global->Del_singleton; - case AugLoad: - Py_INCREF(astmodulestate_global->AugLoad_singleton); - return astmodulestate_global->AugLoad_singleton; - case AugStore: - Py_INCREF(astmodulestate_global->AugStore_singleton); - return astmodulestate_global->AugStore_singleton; default: /* should never happen, but just in case ... */ PyErr_Format(PyExc_SystemError, "unknown expr_context found"); @@ -8809,22 +8775,6 @@ obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena) *out = Del; return 0; } - isinstance = PyObject_IsInstance(obj, astmodulestate_global->AugLoad_type); - if (isinstance == -1) { - return 1; - } - if (isinstance) { - *out = AugLoad; - return 0; - } - isinstance = PyObject_IsInstance(obj, astmodulestate_global->AugStore_type); - if (isinstance == -1) { - return 1; - } - if (isinstance) { - *out = AugStore; - return 0; - } PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj); return 1; @@ -10163,16 +10113,6 @@ PyInit__ast(void) goto error; } Py_INCREF(astmodulestate(m)->Del_type); - if (PyModule_AddObject(m, "AugLoad", astmodulestate_global->AugLoad_type) < - 0) { - goto error; - } - Py_INCREF(astmodulestate(m)->AugLoad_type); - if (PyModule_AddObject(m, "AugStore", astmodulestate_global->AugStore_type) - < 0) { - goto error; - } - Py_INCREF(astmodulestate(m)->AugStore_type); if (PyModule_AddObject(m, "boolop", astmodulestate_global->boolop_type) < 0) { goto error; diff --git a/Python/ast.c b/Python/ast.c index 1c1395f..2e9a8d0 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -71,10 +71,6 @@ expr_context_name(expr_context_ty ctx) return "Store"; case Del: return "Del"; - case AugLoad: - return "AugLoad"; - case AugStore: - return "AugStore"; default: Py_UNREACHABLE(); } @@ -1099,14 +1095,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) { asdl_seq *s = NULL; - /* The ast defines augmented store and load contexts, but the - implementation here doesn't actually use them. The code may be - a little more complex than necessary as a result. It also means - that expressions in an augmented assignment have a Store context. - Consider restructuring so that augmented assignment uses - set_context(), too. - */ - assert(ctx != AugStore && ctx != AugLoad); + /* Expressions in an augmented assignment have a Store context. */ switch (e->kind) { case Attribute_kind: diff --git a/Python/compile.c b/Python/compile.c index d98caba..486b7bb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3506,7 +3506,6 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) PyObject *dict = c->u->u_names; PyObject *mangled; - /* XXX AugStore isn't used anywhere! */ assert(!_PyUnicode_EqualToASCIIString(name, "None") && !_PyUnicode_EqualToASCIIString(name, "True") && @@ -3553,70 +3552,30 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) case Load: op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; break; - case Store: - op = STORE_DEREF; - break; - case AugLoad: - case AugStore: - break; + case Store: op = STORE_DEREF; break; case Del: op = DELETE_DEREF; break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - ctx); - return 0; } break; case OP_FAST: switch (ctx) { case Load: op = LOAD_FAST; break; - case Store: - op = STORE_FAST; - break; + case Store: op = STORE_FAST; break; case Del: op = DELETE_FAST; break; - case AugLoad: - case AugStore: - break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - ctx); - return 0; } ADDOP_N(c, op, mangled, varnames); return 1; case OP_GLOBAL: switch (ctx) { case Load: op = LOAD_GLOBAL; break; - case Store: - op = STORE_GLOBAL; - break; + case Store: op = STORE_GLOBAL; break; case Del: op = DELETE_GLOBAL; break; - case AugLoad: - case AugStore: - break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - ctx); - return 0; } break; case OP_NAME: switch (ctx) { case Load: op = LOAD_NAME; break; - case Store: - op = STORE_NAME; - break; + case Store: op = STORE_NAME; break; case Del: op = DELETE_NAME; break; - case AugLoad: - case AugStore: - break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - ctx); - return 0; } break; } @@ -5021,29 +4980,17 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) return compiler_formatted_value(c, e); /* The following exprs can be assignment targets. */ case Attribute_kind: - if (e->v.Attribute.ctx != AugStore) - VISIT(c, expr, e->v.Attribute.value); + VISIT(c, expr, e->v.Attribute.value); switch (e->v.Attribute.ctx) { - case AugLoad: - ADDOP(c, DUP_TOP); - /* Fall through */ case Load: ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); break; - case AugStore: - ADDOP(c, ROT_TWO); - /* Fall through */ case Store: ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); break; case Del: ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - e->v.Attribute.ctx); - return 0; } break; case Subscript_kind: @@ -5088,48 +5035,58 @@ compiler_visit_expr(struct compiler *c, expr_ty e) static int compiler_augassign(struct compiler *c, stmt_ty s) { + assert(s->kind == AugAssign_kind); expr_ty e = s->v.AugAssign.target; - expr_ty auge; - assert(s->kind == AugAssign_kind); + int old_lineno = c->u->u_lineno; + int old_col_offset = c->u->u_col_offset; + SET_LOC(c, e); switch (e->kind) { case Attribute_kind: - auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, - AugLoad, e->lineno, e->col_offset, - e->end_lineno, e->end_col_offset, c->c_arena); - if (auge == NULL) - return 0; - VISIT(c, expr, auge); - VISIT(c, expr, s->v.AugAssign.value); - ADDOP(c, inplace_binop(s->v.AugAssign.op)); - auge->v.Attribute.ctx = AugStore; - VISIT(c, expr, auge); + VISIT(c, expr, e->v.Attribute.value); + ADDOP(c, DUP_TOP); + ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); break; case Subscript_kind: - auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, - AugLoad, e->lineno, e->col_offset, - e->end_lineno, e->end_col_offset, c->c_arena); - if (auge == NULL) - return 0; - VISIT(c, expr, auge); - VISIT(c, expr, s->v.AugAssign.value); - ADDOP(c, inplace_binop(s->v.AugAssign.op)); - auge->v.Subscript.ctx = AugStore; - VISIT(c, expr, auge); + VISIT(c, expr, e->v.Subscript.value); + VISIT(c, expr, e->v.Subscript.slice); + ADDOP(c, DUP_TOP_TWO); + ADDOP(c, BINARY_SUBSCR); break; case Name_kind: if (!compiler_nameop(c, e->v.Name.id, Load)) return 0; - VISIT(c, expr, s->v.AugAssign.value); - ADDOP(c, inplace_binop(s->v.AugAssign.op)); - return compiler_nameop(c, e->v.Name.id, Store); + break; default: PyErr_Format(PyExc_SystemError, "invalid node type (%d) for augmented assignment", e->kind); return 0; } + + c->u->u_lineno = old_lineno; + c->u->u_col_offset = old_col_offset; + + VISIT(c, expr, s->v.AugAssign.value); + ADDOP(c, inplace_binop(s->v.AugAssign.op)); + + SET_LOC(c, e); + + switch (e->kind) { + case Attribute_kind: + ADDOP(c, ROT_TWO); + ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); + break; + case Subscript_kind: + ADDOP(c, ROT_THREE); + ADDOP(c, STORE_SUBSCR); + break; + case Name_kind: + return compiler_nameop(c, e->v.Name.id, Store); + default: + Py_UNREACHABLE(); + } return 1; } @@ -5322,27 +5279,13 @@ compiler_subscript(struct compiler *c, expr_ty e) } switch (ctx) { - case AugLoad: /* fall through to Load */ case Load: op = BINARY_SUBSCR; break; - case AugStore:/* fall through to Store */ case Store: op = STORE_SUBSCR; break; case Del: op = DELETE_SUBSCR; break; - default: - PyErr_Format(PyExc_SystemError, - "expr_context kind %d should not be possible", - ctx); - return 0; - } - if (ctx == AugStore) { - ADDOP(c, ROT_THREE); - } - else { - VISIT(c, expr, e->v.Subscript.value); - VISIT(c, expr, e->v.Subscript.slice); - if (ctx == AugLoad) { - ADDOP(c, DUP_TOP_TWO); - } } + assert(op); + VISIT(c, expr, e->v.Subscript.value); + VISIT(c, expr, e->v.Subscript.slice); ADDOP(c, op); return 1; } |