8000 compiler_class returns SUCCESS/ERROR · python/cpython@1409c3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1409c3e

Browse files
committed
compiler_class returns SUCCESS/ERROR
1 parent 151e8f0 commit 1409c3e

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

Python/compile.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,9 +2862,7 @@ compiler_class(struct compiler *c, stmt_ty s)
28622862
int i, firstlineno;
28632863
asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
28642864

2865-
if (compiler_decorators(c, decos) < 0) {
2866-
return 0;
2867-
}
2865+
RETURN_IF_ERROR(compiler_decorators(c, decos));
28682866

28692867
firstlineno = s->lineno;
28702868
if (asdl_seq_LEN(decos)) {
@@ -2882,10 +2880,10 @@ compiler_class(struct compiler *c, stmt_ty s)
28822880
This borrows from compiler_call.
28832881
*/
28842882
/* 1. compile the class body into a code object */
2885-
if (compiler_enter_scope(c, s->v.ClassDef.name,
2886-
COMPILER_SCOPE_CLASS, (void *)s, firstlineno) < 0) {
2887-
return 0;
2888-
}
2883+
RETURN_IF_ERROR(
2884+
compiler_enter_scope(c, s->v.ClassDef.name,
2885+
COMPILER_SCOPE_CLASS, (void *)s, firstlineno));
2886+
28892887
/* this block represents what we do in the new scope */
28902888
{
28912889
location loc = LOCATION(firstlineno, firstlineno, 0, 0);
@@ -2894,23 +2892,23 @@ compiler_class(struct compiler *c, stmt_ty s)
28942892
/* load (global) __name__ ... */
28952893
if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) {
28962894
compiler_exit_scope(c);
2897-
return 0;
2895+
return ERROR;
28982896
}
28992897
/* ... and store it as __module__ */
29002898
if (!compiler_nameop(c, loc, &_Py_ID(__module__), Store)) {
29012899
compiler_exit_scope(c);
2902-
return 0;
2900+
return ERROR;
29032901
}
29042902
assert(c->u->u_qualname);
2905-
_ADDOP_LOAD_CONST(c, loc, c->u->u_qualname);
2903+
ADDOP_LOAD_CONST(c, loc, c->u->u_qualname);
29062904
if (!compiler_nameop(c, loc, &_Py_ID(__qualname__), Store)) {
29072905
compiler_exit_scope(c);
2908-
return 0;
2906+
return ERROR;
29092907
}
29102908
/* compile the body proper */
29112909
if (compiler_body(c, loc, s->v.ClassDef.body) < 0) {
29122910
compiler_exit_scope(c);
2913-
return 0;
2911+
return ERROR;
29142912
}
29152913
/* The following code is artificial */
29162914
/* Return __classcell__ if it is referenced, otherwise return None */
@@ -2919,59 +2917,60 @@ compiler_class(struct compiler *c, stmt_ty s)
29192917
i = compiler_lookup_arg(c->u->u_cellvars, &_Py_ID(__class__));
29202918
if (i < 0) {
29212919
compiler_exit_scope(c);
2922-
return 0;
2920+
return ERROR;
29232921
}
29242922
assert(i == 0);
2925-
_ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
2926-
_ADDOP_I(c, NO_LOCATION, COPY, 1);
2923+
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
2924+
ADDOP_I(c, NO_LOCATION, COPY, 1);
29272925
if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store)) {
29282926
compiler_exit_scope(c);
2929-
return 0;
2927+
return ERROR;
29302928
}
29312929
}
29322930
else {
29332931
/* No methods referenced __class__, so just return None */
29342932
assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
2935-
_ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2933+
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
29362934
}
2937-
_ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE);
2935+
ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE);
29382936
/* create the code object */
29392937
co = assemble(c, 1);
29402938
}
29412939
/* leave the new scope */
29422940
compiler_exit_scope(c);
2943-
if (co == NULL)
2944-
return 0;
2941+
if (co == NULL) {
2942+
return ERROR;
2943+
}
29452944

29462945
location loc = LOC(s);
29472946
/* 2. load the 'build_class' function */
2948-
_ADDOP(c, loc, PUSH_NULL);
2949-
_ADDOP(c, loc, LOAD_BUILD_CLASS);
2947+
ADDOP(c, loc, PUSH_NULL);
2948+
ADDOP(c, loc, LOAD_BUILD_CLASS);
29502949

29512950
/* 3. load a function (or closure) made from the code object */
29522951
if (compiler_make_closure(c, loc, co, 0, NULL) < 0) {
29532952
Py_DECREF(co);
2954-
return 0;
2953+
return ERROR;
29552954
}
29562955
Py_DECREF(co);
29572956

29582957
/* 4. load class name */
2959-
_ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name);
2958+
ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name);
29602959

29612960
/* 5. generate the rest of the code for the call */
29622961
if (!compiler_call_helper(c, loc, 2,
29632962
s->v.ClassDef.bases,
2964-
s->v.ClassDef.keywords))
2965-
return 0;
2966-
/* 6. apply decorators */
2967-
if (compiler_apply_decorators(c, decos) < 0) {
2968-
return 0;
2963+
s->v.ClassDef.keywords)) {
2964+
return ERROR;
29692965
}
2966+
/* 6. apply decorators */
2967+
RETURN_IF_ERROR(compiler_apply_decorators(c, decos));
29702968

29712969
/* 7. store into <name> */
2972-
if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store))
2973-
return 0;
2974-
return 1;
2970+
if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store)) {
2971+
return ERROR;
2972+
}
2973+
return SUCCESS;
29752974
}
29762975

29772976
/* Return 0 if the expression is a constant value except named singletons.
@@ -4202,7 +4201,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
42024201
case FunctionDef_kind:
42034202
return compiler_function(c, s, 0);
42044203
case ClassDef_kind:
4205-
return compiler_class(c, s);
4204+
return compiler_class(c, s) == SUCCESS ? 1 : 0;
42064205
case Return_kind:
42074206
return compiler_return(c, s);
42084207
case Delete_kind:

0 commit comments

Comments
 (0)
0