@@ -6270,6 +6270,8 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
62706270 NEW_JUMP_TARGET_LABEL (c , end );
62716271 Py_ssize_t size = asdl_seq_LEN (p -> v .MatchOr .patterns );
62726272 assert (size > 1 );
6273+ PyObject * mismatched_names = NULL ;
6274+ Py_ssize_t mismatch_index = 0 ;
62736275 // We're going to be messing with pc. Keep the original info handy:
62746276 pattern_context old_pc = * pc ;
62756277 Py_INCREF (pc -> stores );
@@ -6304,6 +6306,8 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
63046306 control = Py_NewRef (pc -> stores );
63056307 }
63066308 else if (nstores != PyList_GET_SIZE (control )) {
6309+ mismatch_index = i ;
6310+ mismatched_names = Py_NewRef (pc -> stores );
63076311 goto diff ;
63086312 }
63096313 else if (nstores ) {
@@ -6314,6 +6318,8 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
63146318 Py_ssize_t istores = PySequence_Index (pc -> stores , name );
63156319 if (istores < 0 ) {
63166320 PyErr_Clear ();
6321+ mismatch_index = i ;
6322+ mismatched_names = Py_NewRef (pc -> stores );
63176323 goto diff ;
63186324 }
63196325 if (icontrol != istores ) {
@@ -6405,10 +6411,26 @@ codegen_pattern_or(compiler *c, pattern_ty p, pattern_context *pc)
64056411 // Pop the copy of the subject:
64066412 ADDOP (c , LOC (p ), POP_TOP );
64076413 return SUCCESS ;
6408- diff :
6409- _PyCompile_Error (c , LOC (p ), "alternative patterns bind different names" );
6414+ 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 ) {
6419+ goto error ;
6420+ }
6421+ }
6422+ _PyCompile_Error (
6423+ c , LOC (p ),
6424+ "alternative patterns bind different names "
6425+ "(first pattern binds %S, pattern %d binds %S)" ,
6426+ PyList_GET_SIZE (control ) == 0 ? no_names : control ,
6427+ mismatch_index + 1 ,
6428+ mismatched_names == NULL ? no_names : mismatched_names
6429+ );
6430+ Py_XDECREF (no_names );
64106431error :
64116432 PyMem_Free (old_pc .fail_pop );
6433+ Py_XDECREF (mismatched_names );
64126434 Py_DECREF (old_pc .stores );
64136435 Py_XDECREF (control );
64146436 return ERROR ;
0 commit comments