mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
gh-121404: split compiler_nameop into a codegen part and a compiler part (#123398)
This commit is contained in:
parent
08c0166ce4
commit
d24d1c986d
1 changed files with 101 additions and 89 deletions
190
Python/compile.c
190
Python/compile.c
|
@ -107,6 +107,7 @@ static int compiler_optimization_level(struct compiler *c);
|
||||||
static int compiler_is_interactive(struct compiler *c);
|
static int compiler_is_interactive(struct compiler *c);
|
||||||
static int compiler_is_nested_scope(struct compiler *c);
|
static int compiler_is_nested_scope(struct compiler *c);
|
||||||
static int compiler_scope_type(struct compiler *c);
|
static int compiler_scope_type(struct compiler *c);
|
||||||
|
static int compiler_is_in_inlined_comp(struct compiler *c);
|
||||||
static PyObject *compiler_qualname(struct compiler *c);
|
static PyObject *compiler_qualname(struct compiler *c);
|
||||||
|
|
||||||
#define LOCATION(LNO, END_LNO, COL, END_COL) \
|
#define LOCATION(LNO, END_LNO, COL, END_COL) \
|
||||||
|
@ -311,7 +312,7 @@ typedef struct {
|
||||||
static void compiler_free(struct compiler *);
|
static void compiler_free(struct compiler *);
|
||||||
static int compiler_error(struct compiler *, location loc, const char *, ...);
|
static int compiler_error(struct compiler *, location loc, const char *, ...);
|
||||||
static int compiler_warn(struct compiler *, location loc, const char *, ...);
|
static int compiler_warn(struct compiler *, location loc, const char *, ...);
|
||||||
static int compiler_nameop(struct compiler *, location, identifier, expr_context_ty);
|
static int codegen_nameop(struct compiler *, location, identifier, expr_context_ty);
|
||||||
|
|
||||||
static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
|
static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
|
||||||
static int codegen_visit_stmt(struct compiler *, stmt_ty);
|
static int codegen_visit_stmt(struct compiler *, stmt_ty);
|
||||||
|
@ -1419,8 +1420,8 @@ codegen_unwind_fblock(struct compiler *c, location *ploc,
|
||||||
ADDOP(c, *ploc, POP_EXCEPT);
|
ADDOP(c, *ploc, POP_EXCEPT);
|
||||||
if (info->fb_datum) {
|
if (info->fb_datum) {
|
||||||
ADDOP_LOAD_CONST(c, *ploc, Py_None);
|
ADDOP_LOAD_CONST(c, *ploc, Py_None);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, *ploc, info->fb_datum, Store));
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Del));
|
RETURN_IF_ERROR(codegen_nameop(c, *ploc, info->fb_datum, Del));
|
||||||
}
|
}
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -1553,12 +1554,8 @@ codegen_process_deferred_annotations(struct compiler *c, location loc)
|
||||||
}
|
}
|
||||||
Py_DECREF(deferred_anno);
|
Py_DECREF(deferred_anno);
|
||||||
|
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(codegen_leave_annotations_scope(c, loc, annotations_len));
|
||||||
codegen_leave_annotations_scope(c, loc, annotations_len)
|
RETURN_IF_ERROR(codegen_nameop(c, loc, &_Py_ID(__annotate__), Store));
|
||||||
);
|
|
||||||
RETURN_IF_ERROR(
|
|
||||||
compiler_nameop(c, loc, &_Py_ID(__annotate__), Store)
|
|
||||||
);
|
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -1594,7 +1591,7 @@ codegen_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
|
||||||
location loc = LOC(st->v.Expr.value);
|
location loc = LOC(st->v.Expr.value);
|
||||||
ADDOP_LOAD_CONST(c, loc, cleandoc);
|
ADDOP_LOAD_CONST(c, loc, cleandoc);
|
||||||
Py_DECREF(cleandoc);
|
Py_DECREF(cleandoc);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
|
RETURN_IF_ERROR(codegen_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2109,7 +2106,7 @@ codegen_type_params(struct compiler *c, asdl_type_param_seq *type_params)
|
||||||
typeparam->v.TypeVar.name);
|
typeparam->v.TypeVar.name);
|
||||||
}
|
}
|
||||||
ADDOP_I(c, loc, COPY, 1);
|
ADDOP_I(c, loc, COPY, 1);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, typeparam->v.TypeVar.name, Store));
|
||||||
break;
|
break;
|
||||||
case TypeVarTuple_kind:
|
case TypeVarTuple_kind:
|
||||||
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVarTuple.name);
|
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVarTuple.name);
|
||||||
|
@ -2128,7 +2125,7 @@ codegen_type_params(struct compiler *c, asdl_type_param_seq *type_params)
|
||||||
typeparam->v.TypeVarTuple.name);
|
typeparam->v.TypeVarTuple.name);
|
||||||
}
|
}
|
||||||
ADDOP_I(c, loc, COPY, 1);
|
ADDOP_I(c, loc, COPY, 1);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
|
||||||
break;
|
break;
|
||||||
case ParamSpec_kind:
|
case ParamSpec_kind:
|
||||||
ADDOP_LOAD_CONST(c, loc, typeparam->v.ParamSpec.name);
|
ADDOP_LOAD_CONST(c, loc, typeparam->v.ParamSpec.name);
|
||||||
|
@ -2147,7 +2144,7 @@ codegen_type_params(struct compiler *c, asdl_type_param_seq *type_params)
|
||||||
typeparam->v.ParamSpec.name);
|
typeparam->v.ParamSpec.name);
|
||||||
}
|
}
|
||||||
ADDOP_I(c, loc, COPY, 1);
|
ADDOP_I(c, loc, COPY, 1);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, typeparam->v.ParamSpec.name, Store));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2356,15 +2353,15 @@ codegen_function(struct compiler *c, stmt_ty s, int is_async)
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
|
RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
|
||||||
return compiler_nameop(c, loc, name, Store);
|
return codegen_nameop(c, loc, name, Store);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
codegen_set_type_params_in_class(struct compiler *c, location loc)
|
codegen_set_type_params_in_class(struct compiler *c, location loc)
|
||||||
{
|
{
|
||||||
_Py_DECLARE_STR(type_params, ".type_params");
|
_Py_DECLARE_STR(type_params, ".type_params");
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Load));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, &_Py_STR(type_params), Load));
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_ID(__type_params__), Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, &_Py_ID(__type_params__), Store));
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2396,13 +2393,13 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
|
||||||
|
|
||||||
location loc = LOCATION(firstlineno, firstlineno, 0, 0);
|
location loc = LOCATION(firstlineno, firstlineno, 0, 0);
|
||||||
/* load (global) __name__ ... */
|
/* load (global) __name__ ... */
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__name__), Load));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_ID(__name__), Load));
|
||||||
/* ... and store it as __module__ */
|
/* ... and store it as __module__ */
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__module__), Store));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_ID(__module__), Store));
|
||||||
ADDOP_LOAD_CONST(c, loc, QUALNAME(c));
|
ADDOP_LOAD_CONST(c, loc, QUALNAME(c));
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__qualname__), Store));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_ID(__qualname__), Store));
|
||||||
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(c->u->u_metadata.u_firstlineno));
|
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(c->u->u_metadata.u_firstlineno));
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__firstlineno__), Store));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_ID(__firstlineno__), Store));
|
||||||
asdl_type_param_seq *type_params = s->v.ClassDef.type_params;
|
asdl_type_param_seq *type_params = s->v.ClassDef.type_params;
|
||||||
if (asdl_seq_LEN(type_params) > 0) {
|
if (asdl_seq_LEN(type_params) > 0) {
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, codegen_set_type_params_in_class(c, loc));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_set_type_params_in_class(c, loc));
|
||||||
|
@ -2410,8 +2407,8 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
|
||||||
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
|
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
|
||||||
ADDOP(c, loc, LOAD_LOCALS);
|
ADDOP(c, loc, LOAD_LOCALS);
|
||||||
|
|
||||||
// We can't use compiler_nameop here because we need to generate a
|
// We can't use codegen_nameop here because we need to generate a
|
||||||
// STORE_DEREF in a class namespace, and compiler_nameop() won't do
|
// STORE_DEREF in a class namespace, and codegen_nameop() won't do
|
||||||
// that by default.
|
// that by default.
|
||||||
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
|
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
|
||||||
}
|
}
|
||||||
|
@ -2425,7 +2422,7 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
|
||||||
ADDOP_LOAD_CONST(c, NO_LOCATION, static_attributes);
|
ADDOP_LOAD_CONST(c, NO_LOCATION, static_attributes);
|
||||||
Py_CLEAR(static_attributes);
|
Py_CLEAR(static_attributes);
|
||||||
RETURN_IF_ERROR_IN_SCOPE(
|
RETURN_IF_ERROR_IN_SCOPE(
|
||||||
c, compiler_nameop(c, NO_LOCATION, &_Py_ID(__static_attributes__), Store));
|
c, codegen_nameop(c, NO_LOCATION, &_Py_ID(__static_attributes__), Store));
|
||||||
/* The following code is artificial */
|
/* The following code is artificial */
|
||||||
/* Set __classdictcell__ if necessary */
|
/* Set __classdictcell__ if necessary */
|
||||||
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
|
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
|
||||||
|
@ -2434,7 +2431,7 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, i);
|
RETURN_IF_ERROR_IN_SCOPE(c, i);
|
||||||
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
|
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
|
||||||
RETURN_IF_ERROR_IN_SCOPE(
|
RETURN_IF_ERROR_IN_SCOPE(
|
||||||
c, compiler_nameop(c, NO_LOCATION, &_Py_ID(__classdictcell__), Store));
|
c, codegen_nameop(c, NO_LOCATION, &_Py_ID(__classdictcell__), Store));
|
||||||
}
|
}
|
||||||
/* Return __classcell__ if it is referenced, otherwise return None */
|
/* Return __classcell__ if it is referenced, otherwise return None */
|
||||||
if (SYMTABLE_ENTRY(c)->ste_needs_class_closure) {
|
if (SYMTABLE_ENTRY(c)->ste_needs_class_closure) {
|
||||||
|
@ -2444,7 +2441,7 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
|
||||||
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
|
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
|
||||||
ADDOP_I(c, NO_LOCATION, COPY, 1);
|
ADDOP_I(c, NO_LOCATION, COPY, 1);
|
||||||
RETURN_IF_ERROR_IN_SCOPE(
|
RETURN_IF_ERROR_IN_SCOPE(
|
||||||
c, compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store));
|
c, codegen_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* No methods referenced __class__, so just return None */
|
/* No methods referenced __class__, so just return None */
|
||||||
|
@ -2506,7 +2503,7 @@ codegen_class(struct compiler *c, stmt_ty s)
|
||||||
RETURN_IF_ERROR(ret);
|
RETURN_IF_ERROR(ret);
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, codegen_type_params(c, type_params));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_type_params(c, type_params));
|
||||||
_Py_DECLARE_STR(type_params, ".type_params");
|
_Py_DECLARE_STR(type_params, ".type_params");
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Store));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_STR(type_params), Store));
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = codegen_class_body(c, s, firstlineno);
|
int ret = codegen_class_body(c, s, firstlineno);
|
||||||
|
@ -2522,9 +2519,9 @@ codegen_class(struct compiler *c, stmt_ty s)
|
||||||
if (is_generic) {
|
if (is_generic) {
|
||||||
_Py_DECLARE_STR(type_params, ".type_params");
|
_Py_DECLARE_STR(type_params, ".type_params");
|
||||||
_Py_DECLARE_STR(generic_base, ".generic_base");
|
_Py_DECLARE_STR(generic_base, ".generic_base");
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Load));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_STR(type_params), Load));
|
||||||
ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
|
ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
|
||||||
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(generic_base), Store));
|
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_STR(generic_base), Store));
|
||||||
|
|
||||||
Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
|
Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
|
||||||
asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
|
asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
|
||||||
|
@ -2570,7 +2567,7 @@ codegen_class(struct compiler *c, stmt_ty s)
|
||||||
RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
|
RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
|
||||||
|
|
||||||
/* 7. store into <name> */
|
/* 7. store into <name> */
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, s->v.ClassDef.name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, s->v.ClassDef.name, Store));
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2647,7 +2644,7 @@ codegen_typealias(struct compiler *c, stmt_ty s)
|
||||||
ADDOP(c, loc, PUSH_NULL);
|
ADDOP(c, loc, PUSH_NULL);
|
||||||
ADDOP_I(c, loc, CALL, 0);
|
ADDOP_I(c, loc, CALL, 0);
|
||||||
}
|
}
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, name, Store));
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3340,7 +3337,7 @@ codegen_try_except(struct compiler *c, stmt_ty s)
|
||||||
NEW_JUMP_TARGET_LABEL(c, cleanup_body);
|
NEW_JUMP_TARGET_LABEL(c, cleanup_body);
|
||||||
|
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, loc, handler->v.ExceptHandler.name, Store));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
try:
|
try:
|
||||||
|
@ -3370,9 +3367,9 @@ codegen_try_except(struct compiler *c, stmt_ty s)
|
||||||
ADDOP(c, NO_LOCATION, POP_EXCEPT);
|
ADDOP(c, NO_LOCATION, POP_EXCEPT);
|
||||||
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
||||||
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
|
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
|
||||||
|
|
||||||
/* except: */
|
/* except: */
|
||||||
|
@ -3381,9 +3378,9 @@ codegen_try_except(struct compiler *c, stmt_ty s)
|
||||||
/* name = None; del name; # artificial */
|
/* name = None; del name; # artificial */
|
||||||
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
||||||
|
|
||||||
ADDOP_I(c, NO_LOCATION, RERAISE, 1);
|
ADDOP_I(c, NO_LOCATION, RERAISE, 1);
|
||||||
}
|
}
|
||||||
|
@ -3535,7 +3532,7 @@ codegen_try_star_except(struct compiler *c, stmt_ty s)
|
||||||
|
|
||||||
if (handler->v.ExceptHandler.name) {
|
if (handler->v.ExceptHandler.name) {
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, loc, handler->v.ExceptHandler.name, Store));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ADDOP(c, loc, POP_TOP); // match
|
ADDOP(c, loc, POP_TOP); // match
|
||||||
|
@ -3567,9 +3564,9 @@ codegen_try_star_except(struct compiler *c, stmt_ty s)
|
||||||
if (handler->v.ExceptHandler.name) {
|
if (handler->v.ExceptHandler.name) {
|
||||||
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
||||||
}
|
}
|
||||||
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);
|
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);
|
||||||
|
|
||||||
|
@ -3580,9 +3577,9 @@ codegen_try_star_except(struct compiler *c, stmt_ty s)
|
||||||
if (handler->v.ExceptHandler.name) {
|
if (handler->v.ExceptHandler.name) {
|
||||||
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
|
||||||
RETURN_IF_ERROR(
|
RETURN_IF_ERROR(
|
||||||
compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
codegen_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* add exception raised to the res list */
|
/* add exception raised to the res list */
|
||||||
|
@ -3690,11 +3687,11 @@ codegen_import_as(struct compiler *c, location loc,
|
||||||
ADDOP_I(c, loc, SWAP, 2);
|
ADDOP_I(c, loc, SWAP, 2);
|
||||||
ADDOP(c, loc, POP_TOP);
|
ADDOP(c, loc, POP_TOP);
|
||||||
}
|
}
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, asname, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, asname, Store));
|
||||||
ADDOP(c, loc, POP_TOP);
|
ADDOP(c, loc, POP_TOP);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
return compiler_nameop(c, loc, asname, Store);
|
return codegen_nameop(c, loc, asname, Store);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -3733,7 +3730,7 @@ codegen_import(struct compiler *c, stmt_ty s)
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r = compiler_nameop(c, loc, tmp, Store);
|
r = codegen_nameop(c, loc, tmp, Store);
|
||||||
if (dot != -1) {
|
if (dot != -1) {
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
}
|
}
|
||||||
|
@ -3787,7 +3784,7 @@ codegen_from_import(struct compiler *c, stmt_ty s)
|
||||||
store_name = alias->asname;
|
store_name = alias->asname;
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, LOC(s), store_name, Store));
|
RETURN_IF_ERROR(codegen_nameop(c, LOC(s), store_name, Store));
|
||||||
}
|
}
|
||||||
/* remove imported module */
|
/* remove imported module */
|
||||||
ADDOP(c, LOC(s), POP_TOP);
|
ADDOP(c, LOC(s), POP_TOP);
|
||||||
|
@ -4031,76 +4028,89 @@ codegen_load_classdict_freevar(struct compiler *c, location loc)
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } compiler_optype;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
compiler_nameop(struct compiler *c, location loc,
|
compiler_resolve_nameop(struct compiler *c, PyObject *mangled, int scope,
|
||||||
identifier name, expr_context_ty ctx)
|
compiler_optype *optype, Py_ssize_t *arg)
|
||||||
{
|
{
|
||||||
int op, scope;
|
|
||||||
Py_ssize_t arg;
|
|
||||||
enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
|
|
||||||
|
|
||||||
PyObject *dict = c->u->u_metadata.u_names;
|
PyObject *dict = c->u->u_metadata.u_names;
|
||||||
PyObject *mangled;
|
*optype = OP_NAME;
|
||||||
|
|
||||||
assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
|
assert(scope >= 0);
|
||||||
!_PyUnicode_EqualToASCIIString(name, "True") &&
|
|
||||||
!_PyUnicode_EqualToASCIIString(name, "False"));
|
|
||||||
|
|
||||||
mangled = compiler_maybe_mangle(c, name);
|
|
||||||
if (!mangled) {
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
op = 0;
|
|
||||||
optype = OP_NAME;
|
|
||||||
scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
|
|
||||||
switch (scope) {
|
switch (scope) {
|
||||||
case FREE:
|
case FREE:
|
||||||
dict = c->u->u_metadata.u_freevars;
|
dict = c->u->u_metadata.u_freevars;
|
||||||
optype = OP_DEREF;
|
*optype = OP_DEREF;
|
||||||
break;
|
break;
|
||||||
case CELL:
|
case CELL:
|
||||||
dict = c->u->u_metadata.u_cellvars;
|
dict = c->u->u_metadata.u_cellvars;
|
||||||
optype = OP_DEREF;
|
*optype = OP_DEREF;
|
||||||
break;
|
break;
|
||||||
case LOCAL:
|
case LOCAL:
|
||||||
if (_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
|
if (_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
|
||||||
optype = OP_FAST;
|
*optype = OP_FAST;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyObject *item;
|
PyObject *item;
|
||||||
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
|
RETURN_IF_ERROR(PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
|
||||||
&item) < 0) {
|
&item));
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (item == Py_True) {
|
if (item == Py_True) {
|
||||||
optype = OP_FAST;
|
*optype = OP_FAST;
|
||||||
}
|
}
|
||||||
Py_XDECREF(item);
|
Py_XDECREF(item);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLOBAL_IMPLICIT:
|
case GLOBAL_IMPLICIT:
|
||||||
if (_PyST_IsFunctionLike(SYMTABLE_ENTRY(c)))
|
if (_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
|
||||||
optype = OP_GLOBAL;
|
*optype = OP_GLOBAL;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case GLOBAL_EXPLICIT:
|
case GLOBAL_EXPLICIT:
|
||||||
optype = OP_GLOBAL;
|
*optype = OP_GLOBAL;
|
||||||
break;
|
break;
|
||||||
case -1:
|
|
||||||
goto error;
|
|
||||||
default:
|
default:
|
||||||
/* scope can be 0 */
|
/* scope can be 0 */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (*optype != OP_FAST) {
|
||||||
|
*arg = dict_add_o(dict, mangled);
|
||||||
|
RETURN_IF_ERROR(*arg);
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
codegen_nameop(struct compiler *c, location loc,
|
||||||
|
identifier name, expr_context_ty ctx)
|
||||||
|
{
|
||||||
|
assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
|
||||||
|
!_PyUnicode_EqualToASCIIString(name, "True") &&
|
||||||
|
!_PyUnicode_EqualToASCIIString(name, "False"));
|
||||||
|
|
||||||
|
PyObject *mangled = compiler_maybe_mangle(c, name);
|
||||||
|
if (!mangled) {
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
|
||||||
|
RETURN_IF_ERROR(scope);
|
||||||
|
compiler_optype optype;
|
||||||
|
Py_ssize_t arg = 0;
|
||||||
|
if (compiler_resolve_nameop(c, mangled, scope, &optype, &arg) < 0) {
|
||||||
|
Py_DECREF(mangled);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
/* XXX Leave assert here, but handle __doc__ and the like better */
|
/* XXX Leave assert here, but handle __doc__ and the like better */
|
||||||
assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
|
assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
|
||||||
|
|
||||||
|
int op = 0;
|
||||||
switch (optype) {
|
switch (optype) {
|
||||||
case OP_DEREF:
|
case OP_DEREF:
|
||||||
switch (ctx) {
|
switch (ctx) {
|
||||||
case Load:
|
case Load:
|
||||||
if (SYMTABLE_ENTRY(c)->ste_type == ClassBlock && !c->u->u_in_inlined_comp) {
|
if (SYMTABLE_ENTRY(c)->ste_type == ClassBlock && !compiler_is_in_inlined_comp(c)) {
|
||||||
op = LOAD_FROM_DICT_OR_DEREF;
|
op = LOAD_FROM_DICT_OR_DEREF;
|
||||||
// First load the locals
|
// First load the locals
|
||||||
if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) {
|
if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) {
|
||||||
|
@ -4151,7 +4161,7 @@ compiler_nameop(struct compiler *c, location loc,
|
||||||
switch (ctx) {
|
switch (ctx) {
|
||||||
case Load:
|
case Load:
|
||||||
op = (SYMTABLE_ENTRY(c)->ste_type == ClassBlock
|
op = (SYMTABLE_ENTRY(c)->ste_type == ClassBlock
|
||||||
&& c->u->u_in_inlined_comp)
|
&& compiler_is_in_inlined_comp(c))
|
||||||
? LOAD_GLOBAL
|
? LOAD_GLOBAL
|
||||||
: LOAD_NAME;
|
: LOAD_NAME;
|
||||||
break;
|
break;
|
||||||
|
@ -4162,11 +4172,7 @@ compiler_nameop(struct compiler *c, location loc,
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(op);
|
assert(op);
|
||||||
arg = dict_add_o(dict, mangled);
|
|
||||||
Py_DECREF(mangled);
|
Py_DECREF(mangled);
|
||||||
if (arg < 0) {
|
|
||||||
return ERROR;
|
|
||||||
}
|
|
||||||
if (op == LOAD_GLOBAL) {
|
if (op == LOAD_GLOBAL) {
|
||||||
arg <<= 1;
|
arg <<= 1;
|
||||||
}
|
}
|
||||||
|
@ -4705,7 +4711,7 @@ load_args_for_super(struct compiler *c, expr_ty e) {
|
||||||
|
|
||||||
// load super() global
|
// load super() global
|
||||||
PyObject *super_name = e->v.Call.func->v.Name.id;
|
PyObject *super_name = e->v.Call.func->v.Name.id;
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, LOC(e->v.Call.func), super_name, Load));
|
RETURN_IF_ERROR(codegen_nameop(c, LOC(e->v.Call.func), super_name, Load));
|
||||||
|
|
||||||
if (asdl_seq_LEN(e->v.Call.args) == 2) {
|
if (asdl_seq_LEN(e->v.Call.args) == 2) {
|
||||||
VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 0));
|
VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 0));
|
||||||
|
@ -4716,7 +4722,7 @@ load_args_for_super(struct compiler *c, expr_ty e) {
|
||||||
// load __class__ cell
|
// load __class__ cell
|
||||||
PyObject *name = &_Py_ID(__class__);
|
PyObject *name = &_Py_ID(__class__);
|
||||||
assert(compiler_get_ref_type(c, name) == FREE);
|
assert(compiler_get_ref_type(c, name) == FREE);
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, name, Load));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, name, Load));
|
||||||
|
|
||||||
// load self (first argument)
|
// load self (first argument)
|
||||||
Py_ssize_t i = 0;
|
Py_ssize_t i = 0;
|
||||||
|
@ -4724,7 +4730,7 @@ load_args_for_super(struct compiler *c, expr_ty e) {
|
||||||
if (!PyDict_Next(c->u->u_metadata.u_varnames, &i, &key, &value)) {
|
if (!PyDict_Next(c->u->u_metadata.u_varnames, &i, &key, &value)) {
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, key, Load));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, key, Load));
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -6171,7 +6177,7 @@ codegen_visit_expr(struct compiler *c, expr_ty e)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Name_kind:
|
case Name_kind:
|
||||||
return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx);
|
return codegen_nameop(c, loc, e->v.Name.id, e->v.Name.ctx);
|
||||||
/* child nodes of List and Tuple will have expr_context set */
|
/* child nodes of List and Tuple will have expr_context set */
|
||||||
case List_kind:
|
case List_kind:
|
||||||
return codegen_list(c, e);
|
return codegen_list(c, e);
|
||||||
|
@ -6220,7 +6226,7 @@ codegen_augassign(struct compiler *c, stmt_ty s)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Name_kind:
|
case Name_kind:
|
||||||
RETURN_IF_ERROR(compiler_nameop(c, loc, e->v.Name.id, Load));
|
RETURN_IF_ERROR(codegen_nameop(c, loc, e->v.Name.id, Load));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PyErr_Format(PyExc_SystemError,
|
PyErr_Format(PyExc_SystemError,
|
||||||
|
@ -6256,7 +6262,7 @@ codegen_augassign(struct compiler *c, stmt_ty s)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Name_kind:
|
case Name_kind:
|
||||||
return compiler_nameop(c, loc, e->v.Name.id, Store);
|
return codegen_nameop(c, loc, e->v.Name.id, Store);
|
||||||
default:
|
default:
|
||||||
Py_UNREACHABLE();
|
Py_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
@ -7297,7 +7303,7 @@ codegen_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||||
Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
|
Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
|
||||||
for (Py_ssize_t n = 0; n < nstores; n++) {
|
for (Py_ssize_t n = 0; n < nstores; n++) {
|
||||||
PyObject *name = PyList_GET_ITEM(pc->stores, n);
|
PyObject *name = PyList_GET_ITEM(pc->stores, n);
|
||||||
if (compiler_nameop(c, LOC(m->pattern), name, Store) < 0) {
|
if (codegen_nameop(c, LOC(m->pattern), name, Store) < 0) {
|
||||||
Py_DECREF(pc->stores);
|
Py_DECREF(pc->stores);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
@ -7440,6 +7446,12 @@ compiler_scope_type(struct compiler *c)
|
||||||
return c->u->u_scope_type;
|
return c->u->u_scope_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
compiler_is_in_inlined_comp(struct compiler *c)
|
||||||
|
{
|
||||||
|
return c->u->u_in_inlined_comp;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
compiler_qualname(struct compiler *c)
|
compiler_qualname(struct compiler *c)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue