8000 compiler_pattern_class, validate_kwd_attrs return SUCCESS/ERROR · python/cpython@77d8b27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77d8b27

Browse files
committed
compiler_pattern_class, validate_kwd_attrs return SUCCESS/ERROR
1 parent 44d5452 commit 77d8b27

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Python/compile.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6740,18 +6740,18 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
67406740
identifier attr = ((identifier)asdl_seq_GET(attrs, i));
67416741
location loc = LOC((pattern_ty) asdl_seq_GET(patterns, i));
67426742
if (forbidden_name(c, loc, attr, Store)) {
6743-
return -1;
6743+
return ERROR;
67446744
}
67456745
for (Py_ssize_t j = i + 1; j < nattrs; j++) {
67466746
identifier other = ((identifier)asdl_seq_GET(attrs, j));
67476747
if (!PyUnicode_Compare(attr, other)) {
67486748
location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j));
67496749
compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr);
6750-
return -1;
6750+
return ERROR;
67516751
}
67526752
}
67536753
}
6754-
return 0;
6754+
return SUCCESS;
67556755
}
67566756

67576757
static int
@@ -6768,34 +6768,36 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
67686768
// AST validator shouldn't let this happen, but if it does,
67696769
// just fail, don't crash out of the interpreter
67706770
const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
6771-
return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
6771+
compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
6772+
return ERROR;
67726773
}
67736774
if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
67746775
const char *e = "too many sub-patterns in class pattern %R";
6775-
return compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
6776+
compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
6777+
return ERROR;
67766778
}
67776779
if (nattrs) {
6778-
RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
6780+
RETURN_IF_ERROR(validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
6781+
}
6782+
VISIT(c, expr, p->v.MatchClass.cls);
6783+
PyObject *attr_names = PyTuple_New(nattrs);
6784+
if (!attr_names) {
6785+
return ERROR;
67796786
}
6780-
_VISIT(c, expr, p->v.MatchClass.cls);
6781-
PyObject *attr_names;
6782-
RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
67836787
Py_ssize_t i;
67846788
for (i = 0; i < nattrs; i++) {
67856789
PyObject *name = asdl_seq_GET(kwd_attrs, i);
67866790
PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name));
67876791
}
6788-
_ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names);
6789-
_ADDOP_I(c, LOC(p), MATCH_CLASS, nargs);
6790-
_ADDOP_I(c, LOC(p), COPY, 1);
6791-
_ADDOP_LOAD_CONST(c, LOC(p), Py_None);
6792-
_ADDOP_I(c, LOC(p), IS_OP, 1);
6792+
ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names);
6793+
ADDOP_I(c, LOC(p), MATCH_CLASS, nargs);
6794+
ADDOP_I(c, LOC(p), COPY, 1);
6795+
ADDOP_LOAD_CONST(c, LOC(p), Py_None);
6796+
ADDOP_I(c, LOC(p), IS_OP, 1);
67936797
// TOS is now a tuple of (nargs + nattrs) attributes (or None):
67946798
pc->on_top++;
6795-
if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) {
6796-
return 0;
6797-
}
6798- _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs);
6799+
RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
6800+
ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs);
67996801
pc->on_top += nargs + nattrs - 1;
68006802
for (i = 0; i < nargs + nattrs; i++) {
68016803
pc->on_top--;
@@ -6809,15 +6811,13 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
68096811
pattern = asdl_seq_GET(kwd_patterns, i - nargs);
68106812
}
68116813
if (WILDCARD_CHECK(pattern)) {
6812-
_ADDOP(c, LOC(p), POP_TOP);
6814+
ADDOP(c, LOC(p), POP_TOP);
68136815
continue;
68146816
}
6815-
if (compiler_pattern_subpattern(c, pattern, pc) < 0) {
6816-
return 0;
6817-
}
6817+
RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc));
68186818
}
68196819
// Success! Pop the tuple of attributes:
6820-
return 1;
6820+
return SUCCESS;
68216821
}
68226822

68236823
static int
@@ -7227,7 +7227,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
72277227
case MatchMapping_kind:
72287228
return compiler_pattern_mapping(c, p, pc);
72297229
case MatchClass_kind:
7230-
return compiler_pattern_class(c, p, pc);
7230+
return compiler_pattern_class(c, p, pc) == SUCCESS ? 1 : 0;
72317231
case MatchStar_kind:
72327232
return compiler_pattern_star(c, p, pc) == SUCCESS ? 1 : 0;
72337233
case MatchAs_kind:

0 commit comments

Comments
 (0)
0