Skip to content

Commit d5b624b

Browse files
committed
compiler_error returns ERROR
1 parent 45d26ef commit d5b624b

File tree

1 file changed

+37
-71
lines changed

1 file changed

+37
-71
lines changed

Python/compile.c

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,8 +1913,7 @@ compiler_push_fblock(struct compiler *c, location loc,
19131913
{
19141914
struct fblockinfo *f;
19151915
if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1916-
compiler_error(c, loc, "too many statically nested blocks");
1917-
return ERROR;
1916+
return compiler_error(c, loc, "too many statically nested blocks");
19181917
}
19191918
f = &c->u->u_fblock[c->u->u_nfblocks++];
19201919
f->fb_type = t;
@@ -2102,9 +2101,8 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
21022101
}
21032102
struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
21042103
if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
2105-
compiler_error(c, *ploc,
2104+
return compiler_error(c, *ploc,
21062105
"'break', 'continue' and 'return' cannot appear in an except* block");
2107-
return ERROR;
21082106
}
21092107
if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
21102108
*loop = top;
@@ -3133,8 +3131,7 @@ compiler_async_for(struct compiler *c, stmt_ty s)
31333131
if (IS_TOP_LEVEL_AWAIT(c)){
31343132
c->u->u_ste->ste_coroutine = 1;
31353133
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
3136-
compiler_error(c, loc, "'async for' outside async function");
3137-
return ERROR;
3134+
return compiler_error(c, loc, "'async for' outside async function");
31383135
}
31393136

31403137
NEW_JUMP_TARGET_LABEL(c, start);
@@ -3212,14 +3209,12 @@ compiler_return(struct compiler *c, stmt_ty s)
32123209
int preserve_tos = ((s->v.Return.value != NULL) &&
32133210
(s->v.Return.value->kind != Constant_kind));
32143211
if (c->u->u_ste->ste_type != FunctionBlock) {
3215-
compiler_error(c, loc, "'return' outside function");
3216-
return ERROR;
3212+
return compiler_error(c, loc, "'return' outside function");
32173213
}
32183214
if (s->v.Return.value != NULL &&
32193215
c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
32203216
{
3221-
compiler_error(c, loc, "'return' with value in async generator");
3222-
return ERROR;
3217+
return compiler_error(c, loc, "'return' with value in async generator");
32233218
}
32243219

32253220
if (preserve_tos) {
@@ -3256,8 +3251,7 @@ compiler_break(struct compiler *c, location loc)
32563251
ADDOP(c, loc, NOP);
32573252
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
32583253
if (loop == NULL) {
3259-
compiler_error(c, loc, "'break' outside loop");
3260-
return ERROR;
3254+
return compiler_error(c, loc, "'break' outside loop");
32613255
}
32623256
RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
32633257
ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
@@ -3272,8 +3266,7 @@ compiler_continue(struct compiler *c, location loc)
32723266
ADDOP(c, loc, NOP);
32733267
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
32743268
if (loop == NULL) {
3275-
compiler_error(c, loc, "'continue' not properly in loop");
3276-
return ERROR;
3269+
return compiler_error(c, loc, "'continue' not properly in loop");
32773270
}
32783271
ADDOP_JUMP(c, loc, JUMP, loop->fb_block);
32793272
return SUCCESS;
@@ -3491,8 +3484,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
34913484
s->v.Try.handlers, i);
34923485
location loc = LOC(handler);
34933486
if (!handler->v.ExceptHandler.type && i < n-1) {
3494-
compiler_error(c, loc, "default 'except:' must be last");
3495-
return ERROR;
3487+
return compiler_error(c, loc, "default 'except:' must be last");
34963488
}
34973489
NEW_JUMP_TARGET_LABEL(c, next_except);
34983490
except = next_except;
@@ -3931,9 +3923,8 @@ compiler_from_import(struct compiler *c, stmt_ty s)
39313923
_PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__"))
39323924
{
39333925
Py_DECREF(names);
3934-
compiler_error(c, LOC(s), "from __future__ imports must occur "
3935-
"at the beginning of the file");
3936-
return ERROR;
3926+
return compiler_error(c, LOC(s), "from __future__ imports must occur "
3927+
"at the beginning of the file");
39373928
}
39383929
ADDOP_LOAD_CONST_NEW(c, LOC(s), names);
39393930

@@ -4425,18 +4416,16 @@ unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts)
44254416
if (elt->kind == Starred_kind && !seen_star) {
44264417
if ((i >= (1 << 8)) ||
44274418
(n-i-1 >= (INT_MAX >> 8))) {
4428-
compiler_error(c, loc,
4419+
return compiler_error(c, loc,
44294420
"too many expressions in "
44304421
"star-unpacking assignment");
4431-
return ERROR;
44324422
}
44334423
ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8)));
44344424
seen_star = 1;
44354425
}
44364426
else if (elt->kind == Starred_kind) {
4437-
compiler_error(c, loc,
4427+
return compiler_error(c, loc,
44384428
"multiple starred expressions in assignment");
4439-
return ERROR;
44404429
}
44414430
}
44424431
if (!seen_star) {
@@ -4884,8 +4873,7 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
48844873
for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
48854874
keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
48864875
if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4887-
compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg);
4888-
return ERROR;
4876+
return compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg);
48894877
}
48904878
}
48914879
}
@@ -5614,8 +5602,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
56145602
if (IS_TOP_LEVEL_AWAIT(c)){
56155603
c->u->u_ste->ste_coroutine = 1;
56165604
} else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
5617-
compiler_error(c, loc, "'async with' outside async function");
5618-
return ERROR;
5605+
return compiler_error(c, loc, "'async with' outside async function");
56195606
}
56205607

56215608
NEW_JUMP_TARGET_LABEL(c, block);
@@ -5812,8 +5799,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
58125799
return compiler_dictcomp(c, e);
58135800
case Yield_kind:
58145801
if (c->u->u_ste->ste_type != FunctionBlock) {
5815-
compiler_error(c, loc, "'yield' outside function");
5816-
return ERROR;
5802+
return compiler_error(c, loc, "'yield' outside function");
58175803
}
58185804
if (e->v.Yield.value) {
58195805
VISIT(c, expr, e->v.Yield.value);
@@ -5825,12 +5811,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
58255811
break;
58265812
case YieldFrom_kind:
58275813
if (c->u->u_ste->ste_type != FunctionBlock) {
5828-
compiler_error(c, loc, "'yield' outside function");
5829-
return ERROR;
5814+
return compiler_error(c, loc, "'yield' outside function");
58305815
}
58315816
if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
5832-
compiler_error(c, loc, "'yield from' inside async function");
5833-
return ERROR;
5817+
return compiler_error(c, loc, "'yield from' inside async function");
58345818
}
58355819
VISIT(c, expr, e->v.YieldFrom.value);
58365820
ADDOP(c, loc, GET_YIELD_FROM_ITER);
@@ -5840,14 +5824,12 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
58405824
case Await_kind:
58415825
if (!IS_TOP_LEVEL_AWAIT(c)){
58425826
if (c->u->u_ste->ste_type != FunctionBlock){
5843-
compiler_error(c, loc, "'await' outside function");
5844-
return ERROR;
5827+
return compiler_error(c, loc, "'await' outside function");
58455828
}
58465829

58475830
if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
58485831
c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) {
5849-
compiler_error(c, loc, "'await' outside async function");
5850-
return ERROR;
5832+
return compiler_error(c, loc, "'await' outside async function");
58515833
}
58525834
}
58535835

@@ -5894,13 +5876,11 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
58945876
case Store:
58955877
/* In all legitimate cases, the Starred node was already replaced
58965878
* by compiler_list/compiler_tuple. XXX: is that okay? */
5897-
compiler_error(c, loc,
5879+
return compiler_error(c, loc,
58985880
"starred assignment target must be in a list or tuple");
5899-
return ERROR;
59005881
default:
5901-
compiler_error(c, loc,
5882+
return compiler_error(c, loc,
59025883
"can't use starred expression here");
5903-
return ERROR;
59045884
}
59055885
break;
59065886
case Slice_kind:
@@ -6142,7 +6122,7 @@ compiler_error(struct compiler *c, location loc,
61426122
PyObject *msg = PyUnicode_FromFormatV(format, vargs);
61436123
va_end(vargs);
61446124
if (msg == NULL) {
6145-
return 0;
6125+
return ERROR;
61466126
}
61476127
PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno);
61486128
if (loc_obj == NULL) {
@@ -6159,7 +6139,7 @@ compiler_error(struct compiler *c, location loc,
61596139
exit:
61606140
Py_DECREF(loc_obj);
61616141
Py_XDECREF(args);
6162-
return 0;
6142+
return ERROR;
61636143
}
61646144

61656145
/* Emits a SyntaxWarning and returns 1 on success.
@@ -6345,9 +6325,8 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
63456325
static int
63466326
compiler_error_duplicate_store(struct compiler *c, location loc, identifier n)
63476327
{
6348-
compiler_error(c, loc,
6328+
return compiler_error(c, loc,
63496329
"multiple assignments to name %R in pattern", n);
6350-
return ERROR;
63516330
}
63526331

63536332
// Duplicate the effect of 3.10's ROT_* instructions using SWAPs.
@@ -6377,8 +6356,7 @@ pattern_helper_store_name(struct compiler *c, location loc,
63776356
return ERROR;
63786357
}
63796358
if (duplicate) {
6380-
compiler_error_duplicate_store(c, loc, n);
6381-
return ERROR;
6359+
return compiler_error_duplicate_store(c, loc, n);
63826360
}
63836361
// Rotate this object underneath any items we need to preserve:
63846362
Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
@@ -6403,18 +6381,16 @@ pattern_unpack_helper(struct compiler *c, location loc,
64036381
if (elt->kind == MatchStar_kind && !seen_star) {
64046382
if ((i >= (1 << 8)) ||
64056383
(n-i-1 >= (INT_MAX >> 8))) {
6406-
compiler_error(c, loc,
6384+
return compiler_error(c, loc,
64076385
"too many expressions in "
64086386
"star-unpacking sequence pattern");
6409-
return ERROR;
64106387
}
64116388
ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8)));
64126389
seen_star = 1;
64136390
}
64146391
else if (elt->kind == MatchStar_kind) {
6415-
compiler_error(c, loc,
6392+
return compiler_error(c, loc,
64166393
"multiple starred expressions in sequence pattern");
6417-
return ERROR;
64186394
}
64196395
}
64206396
if (!seen_star) {
@@ -6505,12 +6481,10 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
65056481
if (!pc->allow_irrefutable) {
65066482
if (p->v.MatchAs.name) {
65076483
const char *e = "name capture %R makes remaining patterns unreachable";
6508-
compiler_error(c, LOC(p), e, p->v.MatchAs.name);
6509-
return ERROR;
6484+
return compiler_error(c, LOC(p), e, p->v.MatchAs.name);
65106485
}
65116486
const char *e = "wildcard makes remaining patterns unreachable";
6512-
compiler_error(c, LOC(p), e);
6513-
return ERROR;
6487+
return compiler_error(c, LOC(p), e);
65146488
}
65156489
return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc);
65166490
}
@@ -6549,8 +6523,7 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
65496523
identifier other = ((identifier)asdl_seq_GET(attrs, j));
65506524
if (!PyUnicode_Compare(attr, other)) {
65516525
location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j));
6552-
compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr);
6553-
return ERROR;
6526+
return compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr);
65546527
}
65556528
}
65566529
}
@@ -6571,13 +6544,11 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
65716544
// AST validator shouldn't let this happen, but if it does,
65726545
// just fail, don't crash out of the interpreter
65736546
const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
6574-
compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
6575-
return ERROR;
6547+
return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
65766548
}
65776549
if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
65786550
const char *e = "too many sub-patterns in class pattern %R";
6579-
compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
6580-
return ERROR;
6551+
return compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
65816552
}
65826553
if (nattrs) {
65836554
RETURN_IF_ERROR(validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
@@ -6636,8 +6607,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p,
66366607
// AST validator shouldn't let this happen, but if it does,
66376608
// just fail, don't crash out of the interpreter
66386609
const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
6639-
compiler_error(c, LOC(p), e, size, npatterns);
6640-
return ERROR;
6610+
return compiler_error(c, LOC(p), e, size, npatterns);
66416611
}
66426612
// We have a double-star target if "rest" is set
66436613
PyObject *star_target = p->v.MatchMapping.rest;
@@ -6659,8 +6629,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p,
66596629
RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
66606630
}
66616631
if (INT_MAX < size - 1) {
6662-
compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern");
6663-
return ERROR;
6632+
return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern");
66646633
}
66656634
// Collect all of the keys into a tuple for MATCH_KEYS and
66666635
// **rest. They can either be dotted names or literals:
@@ -6926,8 +6895,7 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p,
69266895
if (pattern->kind == MatchStar_kind) {
69276896
if (star >= 0) {
69286897
const char *e = "multiple starred names in sequence pattern";
6929-
compiler_error(c, LOC(p), e);
6930-
return ERROR;
6898+
return compiler_error(c, LOC(p), e);
69316899
}
69326900
star_wildcard = WILDCARD_STAR_CHECK(pattern);
69336901
only_wildcard &= star_wildcard;
@@ -6978,8 +6946,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
69786946
expr_ty value = p->v.MatchValue.value;
69796947
if (!MATCH_VALUE_EXPR(value)) {
69806948
const char *e = "patterns may only match literals and attribute lookups";
6981-
compiler_error(c, LOC(p), e);
6982-
return ERROR;
6949+
return compiler_error(c, LOC(p), e);
69836950
}
69846951
VISIT(c, expr, value);
69856952
ADDOP_COMPARE(c, LOC(p), Eq);
@@ -7021,8 +6988,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
70216988
// AST validator shouldn't let this happen, but if it does,
70226989
// just fail, don't crash out of the interpreter
70236990
const char *e = "invalid match pattern node in AST (kind=%d)";
7024-
compiler_error(c, LOC(p), e, p->kind);
7025-
return ERROR;
6991+
return compiler_error(c, LOC(p), e, p->kind);
70266992
}
70276993

70286994
static int

0 commit comments

Comments
 (0)