Skip to content

Commit a31a862

Browse files
committed
properly handle mismatch symmetry
1 parent 5cc7b11 commit a31a862

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

Python/codegen.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6272,6 +6272,7 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
62726272
assert(size > 1);
62736273
PyObject *mismatched_names = NULL;
62746274
Py_ssize_t mismatch_index = 0;
6275+
PyObject *str_nothing = NULL; // the string 'nothing'
62756276
// We're going to be messing with pc. Keep the original info handy:
62766277
pattern_context old_pc = *pc;
62776278
Py_INCREF(pc->stores);
@@ -6412,25 +6413,30 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
64126413
ADDOP(c, LOC(p), POP_TOP);
64136414
return SUCCESS;
64146415
diff:;
6415-
PyObject *no_names = NULL;
6416-
if (PyList_GET_SIZE(control) == 0 || !mismatched_names) {
6417-
no_names = PyUnicode_FromString("no names");
6418-
if (no_names == NULL) {
6416+
int control_is_empty = PyList_GET_SIZE(control) == 0;
6417+
int pattern_is_empty = (
6418+
mismatched_names == NULL
6419+
|| PyList_GET_SIZE(mismatched_names) == 0
6420+
);
6421+
6422+
if (control_is_empty || pattern_is_empty) {
6423+
str_nothing = PyUnicode_FromString("nothing");
6424+
if (str_nothing == NULL) {
64196425
goto error;
64206426
}
64216427
}
64226428
_PyCompile_Error(
64236429
c, LOC(p),
64246430
"alternative patterns bind different names "
6425-
"(first pattern binds %S, pattern %zd binds %S)",
6426-
PyList_GET_SIZE(control) == 0 ? no_names : control,
6431+
"(pattern 1 binds %S, pattern %zd binds %S)",
6432+
control_is_empty ? str_nothing : control,
64276433
mismatch_index + 1,
6428-
mismatched_names == NULL ? no_names : mismatched_names
6434+
pattern_is_empty ? str_nothing : mismatched_names
64296435
);
6430-
Py_XDECREF(no_names);
64316436
error:
64326437
PyMem_Free(old_pc.fail_pop);
64336438
Py_XDECREF(mismatched_names);
6439+
Py_XDECREF(str_nothing);
64346440
Py_DECREF(old_pc.stores);
64356441
Py_XDECREF(control);
64366442
return ERROR;

0 commit comments

Comments
 (0)