@@ -314,9 +314,9 @@ static int compiler_warn(struct compiler *, location loc, const char *, ...);
314314static int compiler_nameop (struct compiler * , location , identifier , expr_context_ty );
315315
316316static PyCodeObject * compiler_mod (struct compiler * , mod_ty );
317- static int compiler_visit_stmt (struct compiler * , stmt_ty );
318- static int compiler_visit_keyword (struct compiler * , keyword_ty );
319- static int compiler_visit_expr (struct compiler * , expr_ty );
317+ static int codegen_visit_stmt (struct compiler * , stmt_ty );
318+ static int codegen_visit_keyword (struct compiler * , keyword_ty );
319+ static int codegen_visit_expr (struct compiler * , expr_ty );
320320static int codegen_augassign (struct compiler * , stmt_ty );
321321static int codegen_annassign (struct compiler * , stmt_ty );
322322static int codegen_subscript (struct compiler * , expr_ty );
@@ -1037,17 +1037,17 @@ codegen_addop_j(instr_sequence *seq, location loc,
10371037*/
10381038
10391039#define VISIT (C , TYPE , V ) \
1040- RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V)));
1040+ RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), (V)));
10411041
10421042#define VISIT_IN_SCOPE (C , TYPE , V ) \
1043- RETURN_IF_ERROR_IN_SCOPE((C), compiler_visit_ ## TYPE((C), (V)))
1043+ RETURN_IF_ERROR_IN_SCOPE((C), codegen_visit_ ## TYPE((C), (V)))
10441044
10451045#define VISIT_SEQ (C , TYPE , SEQ ) { \
10461046 int _i; \
10471047 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
10481048 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
10491049 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1050- RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), elt)); \
1050+ RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), elt)); \
10511051 } \
10521052}
10531053
@@ -1056,7 +1056,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
10561056 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
10571057 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
10581058 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1059- if (compiler_visit_ ## TYPE((C), elt) < 0) { \
1059+ if (codegen_visit_ ## TYPE((C), elt) < 0) { \
10601060 compiler_exit_scope(C); \
10611061 return ERROR; \
10621062 } \
@@ -1837,22 +1837,18 @@ codegen_kwonlydefaults(struct compiler *c, location loc,
18371837
18381838 Return -1 on error, 0 if no dict pushed, 1 if a dict is pushed.
18391839 */
1840- int i ;
1841- PyObject * keys = NULL ;
18421840 int default_count = 0 ;
1843- for (i = 0 ; i < asdl_seq_LEN (kwonlyargs ); i ++ ) {
1841+ for (int i = 0 ; i < asdl_seq_LEN (kwonlyargs ); i ++ ) {
18441842 arg_ty arg = asdl_seq_GET (kwonlyargs , i );
18451843 expr_ty default_ = asdl_seq_GET (kw_defaults , i );
18461844 if (default_ ) {
18471845 default_count ++ ;
18481846 PyObject * mangled = compiler_maybe_mangle (c , arg -> arg );
18491847 if (!mangled ) {
1850- goto error ;
1848+ return ERROR ;
18511849 }
18521850 ADDOP_LOAD_CONST_NEW (c , loc , mangled );
1853- if (compiler_visit_expr (c , default_ ) < 0 ) {
1854- goto error ;
1855- }
1851+ VISIT (c , expr , default_ );
18561852 }
18571853 }
18581854 if (default_count ) {
@@ -1862,14 +1858,10 @@ codegen_kwonlydefaults(struct compiler *c, location loc,
18621858 else {
18631859 return 0 ;
18641860 }
1865-
1866- error :
1867- Py_XDECREF (keys );
1868- return ERROR ;
18691861}
18701862
18711863static int
1872- compiler_visit_annexpr (struct compiler * c , expr_ty annotation )
1864+ codegen_visit_annexpr (struct compiler * c , expr_ty annotation )
18731865{
18741866 location loc = LOC (annotation );
18751867 ADDOP_LOAD_CONST_NEW (c , loc , _PyAST_ExprAsUnicode (annotation ));
@@ -3854,7 +3846,7 @@ codegen_stmt_expr(struct compiler *c, location loc, expr_ty value)
38543846}
38553847
38563848static int
3857- compiler_visit_stmt (struct compiler * c , stmt_ty s )
3849+ codegen_visit_stmt (struct compiler * c , stmt_ty s )
38583850{
38593851
38603852 switch (s -> kind ) {
@@ -5807,7 +5799,7 @@ codegen_dictcomp(struct compiler *c, expr_ty e)
58075799
58085800
58095801static int
5810- compiler_visit_keyword (struct compiler * c , keyword_ty k )
5802+ codegen_visit_keyword (struct compiler * c , keyword_ty k )
58115803{
58125804 VISIT (c , expr , k -> value );
58135805 return SUCCESS ;
@@ -6035,7 +6027,7 @@ codegen_with(struct compiler *c, stmt_ty s, int pos)
60356027}
60366028
60376029static int
6038- compiler_visit_expr (struct compiler * c , expr_ty e )
6030+ codegen_visit_expr (struct compiler * c , expr_ty e )
60396031{
60406032 location loc = LOC (e );
60416033 switch (e -> kind ) {
@@ -6406,7 +6398,7 @@ codegen_annassign(struct compiler *c, stmt_ty s)
64066398 return SUCCESS ;
64076399}
64086400
6409- /* Raises a SyntaxError and returns 0 .
6401+ /* Raises a SyntaxError and returns ERROR .
64106402 If something goes wrong, a different exception may be raised.
64116403*/
64126404
@@ -6877,6 +6869,36 @@ codegen_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
68776869 return SUCCESS ;
68786870}
68796871
6872+ static int
6873+ codegen_pattern_mapping_key (struct compiler * c , PyObject * seen , pattern_ty p , Py_ssize_t i )
6874+ {
6875+ asdl_expr_seq * keys = p -> v .MatchMapping .keys ;
6876+ asdl_pattern_seq * patterns = p -> v .MatchMapping .patterns ;
6877+ expr_ty key = asdl_seq_GET (keys , i );
6878+ if (key == NULL ) {
6879+ const char * e = "can't use NULL keys in MatchMapping "
6880+ "(set 'rest' parameter instead)" ;
6881+ location loc = LOC ((pattern_ty ) asdl_seq_GET (patterns , i ));
6882+ return compiler_error (c , loc , e );
6883+ }
6884+
6885+ if (key -> kind == Constant_kind ) {
6886+ int in_seen = PySet_Contains (seen , key -> v .Constant .value );
6887+ RETURN_IF_ERROR (in_seen );
6888+ if (in_seen ) {
6889+ const char * e = "mapping pattern checks duplicate key (%R)" ;
6890+ return compiler_error (c , LOC (p ), e , key -> v .Constant .value );
6891+ }
6892+ RETURN_IF_ERROR (PySet_Add (seen , key -> v .Constant .value ));
6893+ }
6894+ else if (key -> kind != Attribute_kind ) {
6895+ const char * e = "mapping pattern keys may only match literals and attribute lookups" ;
6896+ return compiler_error (c , LOC (p ), e );
6897+ }
6898+ VISIT (c , expr , key );
6899+ return SUCCESS ;
6900+ }
6901+
68806902static int
68816903codegen_pattern_mapping (struct compiler * c , pattern_ty p ,
68826904 pattern_context * pc )
@@ -6923,45 +6945,15 @@ codegen_pattern_mapping(struct compiler *c, pattern_ty p,
69236945 if (seen == NULL ) {
69246946 return ERROR ;
69256947 }
6926-
6927- // NOTE: goto error on failure in the loop below to avoid leaking `seen`
69286948 for (Py_ssize_t i = 0 ; i < size ; i ++ ) {
6929- expr_ty key = asdl_seq_GET (keys , i );
6930- if (key == NULL ) {
6931- const char * e = "can't use NULL keys in MatchMapping "
6932- "(set 'rest' parameter instead)" ;
6933- location loc = LOC ((pattern_ty ) asdl_seq_GET (patterns , i ));
6934- compiler_error (c , loc , e );
6935- goto error ;
6936- }
6937-
6938- if (key -> kind == Constant_kind ) {
6939- int in_seen = PySet_Contains (seen , key -> v .Constant .value );
6940- if (in_seen < 0 ) {
6941- goto error ;
6942- }
6943- if (in_seen ) {
6944- const char * e = "mapping pattern checks duplicate key (%R)" ;
6945- compiler_error (c , LOC (p ), e , key -> v .Constant .value );
6946- goto error ;
6947- }
6948- if (PySet_Add (seen , key -> v .Constant .value )) {
6949- goto error ;
6950- }
6951- }
6952-
6953- else if (key -> kind != Attribute_kind ) {
6954- const char * e = "mapping pattern keys may only match literals and attribute lookups" ;
6955- compiler_error (c , LOC (p ), e );
6956- goto error ;
6957- }
6958- if (compiler_visit_expr (c , key ) < 0 ) {
6959- goto error ;
6949+ if (codegen_pattern_mapping_key (c , seen , p , i ) < 0 ) {
6950+ Py_DECREF (seen );
6951+ return ERROR ;
69606952 }
69616953 }
6954+ Py_DECREF (seen );
69626955
69636956 // all keys have been checked; there are no duplicates
6964- Py_DECREF (seen );
69656957
69666958 ADDOP_I (c , LOC (p ), BUILD_TUPLE , size );
69676959 ADDOP (c , LOC (p ), MATCH_KEYS );
@@ -7006,10 +6998,6 @@ codegen_pattern_mapping(struct compiler *c, pattern_ty p,
70066998 ADDOP (c , LOC (p ), POP_TOP ); // Subject.
70076999 }
70087000 return SUCCESS ;
7009-
7010- error :
7011- Py_DECREF (seen );
7012- return ERROR ;
70137001}
70147002
70157003static int
0 commit comments