@@ -314,9 +314,9 @@ static int compiler_warn(struct compiler *, location loc, const char *, ...);
314
314
static int compiler_nameop (struct compiler * , location , identifier , expr_context_ty );
315
315
316
316
static 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 );
320
320
static int codegen_augassign (struct compiler * , stmt_ty );
321
321
static int codegen_annassign (struct compiler * , stmt_ty );
322
322
static int codegen_subscript (struct compiler * , expr_ty );
@@ -1037,17 +1037,17 @@ codegen_addop_j(instr_sequence *seq, location loc,
1037
1037
*/
1038
1038
1039
1039
#define VISIT (C , TYPE , V ) \
1040
- RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V)));
1040
+ RETURN_IF_ERROR(codegen_visit_ ## TYPE((C), (V)));
1041
1041
1042
1042
#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)))
1044
1044
1045
1045
#define VISIT_SEQ (C , TYPE , SEQ ) { \
1046
1046
int _i; \
1047
1047
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1048
1048
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1049
1049
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)); \
1051
1051
} \
1052
1052
}
1053
1053
@@ -1056,7 +1056,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
1056
1056
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1057
1057
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1058
1058
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) { \
1060
1060
compiler_exit_scope(C); \
1061
1061
return ERROR; \
1062
1062
} \
@@ -1837,22 +1837,18 @@ codegen_kwonlydefaults(struct compiler *c, location loc,
1837
1837
1838
1838
Return -1 on error, 0 if no dict pushed, 1 if a dict is pushed.
1839
1839
*/
1840
- int i ;
1841
- PyObject * keys = NULL ;
1842
1840
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 ++ ) {
1844
1842
arg_ty arg = asdl_seq_GET (kwonlyargs , i );
1845
1843
expr_ty default_ = asdl_seq_GET (kw_defaults , i );
1846
1844
if (default_ ) {
1847
1845
default_count ++ ;
1848
1846
PyObject * mangled = compiler_maybe_mangle (c , arg -> arg );
1849
1847
if (!mangled ) {
1850
- goto error ;
1848
+ return ERROR ;
1851
1849
}
1852
1850
ADDOP_LOAD_CONST_NEW (c , loc , mangled );
1853
- if (compiler_visit_expr (c , default_ ) < 0 ) {
1854
- goto error ;
1855
- }
1851
+ VISIT (c , expr , default_ );
1856
1852
}
1857
1853
}
1858
1854
if (default_count ) {
@@ -1862,14 +1858,10 @@ codegen_kwonlydefaults(struct compiler *c, location loc,
1862
1858
else {
1863
1859
return 0 ;
1864
1860
}
1865
-
1866
- error :
1867
- Py_XDECREF (keys );
1868
- return ERROR ;
1869
1861
}
1870
1862
1871
1863
static int
1872
- compiler_visit_annexpr (struct compiler * c , expr_ty annotation )
1864
+ codegen_visit_annexpr (struct compiler * c , expr_ty annotation )
1873
1865
{
1874
1866
location loc = LOC (annotation );
1875
1867
ADDOP_LOAD_CONST_NEW (c , loc , _PyAST_ExprAsUnicode (annotation ));
@@ -3854,7 +3846,7 @@ codegen_stmt_expr(struct compiler *c, location loc, expr_ty value)
3854
3846
}
3855
3847
3856
3848
static int
3857
- compiler_visit_stmt (struct compiler * c , stmt_ty s )
3849
+ codegen_visit_stmt (struct compiler * c , stmt_ty s )
3858
3850
{
3859
3851
3860
3852
switch (s -> kind ) {
@@ -5807,7 +5799,7 @@ codegen_dictcomp(struct compiler *c, expr_ty e)
5807
5799
5808
5800
5809
5801
static int
5810
- compiler_visit_keyword (struct compiler * c , keyword_ty k )
5802
+ codegen_visit_keyword (struct compiler * c , keyword_ty k )
5811
5803
{
5812
5804
VISIT (c , expr , k -> value );
5813
5805
return SUCCESS ;
@@ -6035,7 +6027,7 @@ codegen_with(struct compiler *c, stmt_ty s, int pos)
6035
6027
}
6036
6028
6037
6029
static int
6038
- compiler_visit_expr (struct compiler * c , expr_ty e )
6030
+ codegen_visit_expr (struct compiler * c , expr_ty e )
6039
6031
{
6040
6032
location loc = LOC (e );
6041
6033
switch (e -> kind ) {
@@ -6406,7 +6398,7 @@ codegen_annassign(struct compiler *c, stmt_ty s)
6406
6398
return SUCCESS ;
6407
6399
}
6408
6400
6409
- /* Raises a SyntaxError and returns 0 .
6401
+ /* Raises a SyntaxError and returns ERROR .
6410
6402
If something goes wrong, a different exception may be raised.
6411
6403
*/
6412
6404
@@ -6877,6 +6869,36 @@ codegen_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
6877
6869
return SUCCESS ;
6878
6870
}
6879
6871
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
+
6880
6902
static int
6881
6903
codegen_pattern_mapping (struct compiler * c , pattern_ty p ,
6882
6904
pattern_context * pc )
@@ -6923,45 +6945,15 @@ codegen_pattern_mapping(struct compiler *c, pattern_ty p,
6923
6945
if (seen == NULL ) {
6924
6946
return ERROR ;
6925
6947
}
6926
-
6927
- // NOTE: goto error on failure in the loop below to avoid leaking `seen`
6928
6948
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 ;
6960
6952
}
6961
6953
}
6954
+ Py_DECREF (seen );
6962
6955
6963
6956
// all keys have been checked; there are no duplicates
6964
- Py_DECREF (seen );
6965
6957
6966
6958
ADDOP_I (c , LOC (p ), BUILD_TUPLE , size );
6967
6959
ADDOP (c , LOC (p ), MATCH_KEYS );
@@ -7006,10 +6998,6 @@ codegen_pattern_mapping(struct compiler *c, pattern_ty p,
7006
6998
ADDOP (c , LOC (p ), POP_TOP ); // Subject.
7007
6999
}
7008
7000
return SUCCESS ;
7009
-
7010
- error :
7011
- Py_DECREF (seen );
7012
- return ERROR ;
7013
7001
}
7014
7002
7015
7003
static int
0 commit comments