@@ -174,6 +174,7 @@ static int compiler_addop(struct compiler *, int);
174174static int compiler_addop_i (struct compiler * , int , Py_ssize_t );
175175static int compiler_addop_j (struct compiler * , int , basicblock * , int );
176176static int compiler_error (struct compiler * , const char * );
177+ static int compiler_warn (struct compiler * , const char * );
177178static int compiler_nameop (struct compiler * , identifier , expr_context_ty );
178179
179180static PyCodeObject * compiler_mod (struct compiler * , mod_ty );
@@ -2971,7 +2972,6 @@ compiler_assert(struct compiler *c, stmt_ty s)
29712972{
29722973 static PyObject * assertion_error = NULL ;
29732974 basicblock * end ;
2974- PyObject * msg ;
29752975
29762976 if (c -> c_optimize )
29772977 return 1 ;
@@ -2981,18 +2981,13 @@ compiler_assert(struct compiler *c, stmt_ty s)
29812981 return 0 ;
29822982 }
29832983 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+ {
29932989 return 0 ;
29942990 }
2995- Py_DECREF (msg );
29962991 }
29972992 end = compiler_new_block (c );
29982993 if (end == NULL )
@@ -4793,6 +4788,31 @@ compiler_error(struct compiler *c, const char *errstr)
47934788 return 0 ;
47944789}
47954790
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+
47964816static int
47974817compiler_handle_subscr (struct compiler * c , const char * kind ,
47984818 expr_context_ty ctx )
0 commit comments