@@ -174,6 +174,7 @@ static int compiler_addop(struct compiler *, int);
174
174
static int compiler_addop_i (struct compiler * , int , Py_ssize_t );
175
175
static int compiler_addop_j (struct compiler * , int , basicblock * , int );
176
176
static int compiler_error (struct compiler * , const char * );
177
+ static int compiler_warn (struct compiler * , const char * );
177
178
static int compiler_nameop (struct compiler * , identifier , expr_context_ty );
178
179
179
180
static PyCodeObject * compiler_mod (struct compiler * , mod_ty );
@@ -2971,7 +2972,6 @@ compiler_assert(struct compiler *c, stmt_ty s)
2971
2972
{
2972
2973
static PyObject * assertion_error = NULL ;
2973
2974
basicblock * end ;
2974
- PyObject * msg ;
2975
2975
2976
2976
if (c -> c_optimize )
2977
2977
return 1 ;
@@ -2981,18 +2981,13 @@ compiler_assert(struct compiler *c, stmt_ty s)
2981
2981
return 0 ;
2982
2982
}
2983
2983
if (s -> v .Assert .test -> kind == Tuple_kind &&
2984
- asdl_seq_LEN (s -> v .Assert .test -> v .Tuple .elts ) > 0 ) {
2985
- msg = PyUnicode_FromString ("assertion is always true, "
2986
- "perhaps remove parentheses?" );
2987
- if (msg == NULL )
2988
- return 0 ;
2989
- if (PyErr_WarnExplicitObject (PyExc_SyntaxWarning , msg ,
2990
- c -> c_filename , c -> u -> u_lineno ,
2991
- NULL , NULL ) == -1 ) {
2992
- Py_DECREF (msg );
2984
+ asdl_seq_LEN (s -> v .Assert .test -> v .Tuple .elts ) > 0 )
2985
+ {
2986
+ if (!compiler_warn (c , "assertion is always true, "
2987
+ "perhaps remove parentheses?" ))
2988
+ {
2993
2989
return 0 ;
2994
2990
}
2995
- Py_DECREF (msg );
2996
2991
}
2997
2992
end = compiler_new_block (c );
2998
2993
if (end == NULL )
@@ -4793,6 +4788,31 @@ compiler_error(struct compiler *c, const char *errstr)
4793
4788
return 0 ;
4794
4789
}
4795
4790
4791
+ /* Emits a SyntaxWarning and returns 1 on success.
4792
+ If a SyntaxWarning raised as error, replaces it with a SyntaxError
4793
+ and returns 0.
4794
+ */
4795
+ static int
4796
+ compiler_warn (struct compiler * c , const char * errstr )
4797
+ {
4798
+ PyObject * msg = PyUnicode_FromString (errstr );
4799
+ if (msg == NULL ) {
4800
+ return 0 ;
4801
+ }
4802
+ if (PyErr_WarnExplicitObject (PyExc_SyntaxWarning , msg , c -> c_filename ,
4803
+ c -> u -> u_lineno , NULL , NULL ) < 0 )
4804
+ {
4805
+ Py_DECREF (msg );
4806
+ if (PyErr_ExceptionMatches (PyExc_SyntaxWarning )) {
4807
+ PyErr_Clear ();
4808
+ return compiler_error (c , errstr );
4809
+ }
4810
+ return 0 ;
4811
+ }
4812
+ Py_DECREF (msg );
4813
+ return 1 ;
4814
+ }
4815
+
4796
4816
static int
4797
4817
compiler_handle_subscr (struct compiler * c , const char * kind ,
4798
4818
expr_context_ty ctx )
0 commit comments