Use the new PyErr_WarnExplicit() API to issue better warnings for
authorGuido van Rossum <guido@python.org>
Wed, 28 Feb 2001 21:55:38 +0000 (21:55 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 28 Feb 2001 21:55:38 +0000 (21:55 +0000)
global after assign / use.

Note: I'm not updating the PyErr_Warn() call for import * / exec
combined with a function, because I can't trigger it with an example.
Jeremy, just follow the example of the call to PyErr_WarnExplicit()
that I *did* include.

Python/compile.c

index b44512271bf6a652387a25f9531be58cd643e9d5..6b5fa117057c3948f6346f89c0f197c9093fce2b 100644 (file)
@@ -4830,18 +4830,27 @@ symtable_global(struct symtable *st, node *n)
                                                   st->st_cur->ste_lineno);
                                st->st_errors++;
                                return;
-                       } else if (flags & DEF_LOCAL) {
-                               sprintf(buf, GLOBAL_AFTER_ASSIGN, name);
-                               if (PyErr_Warn(PyExc_SyntaxWarning,
-                                              buf) < 0) {
-                                       /* XXX set line number? */
-                                       st->st_errors++;
-                               }
-                       } else {
-                               sprintf(buf, GLOBAL_AFTER_USE, name);
-                               if (PyErr_Warn(PyExc_SyntaxWarning,
-                                              buf) < 0) {
-                                       /* XXX set line number? */
+                       }
+                       else {
+                               if (flags & DEF_LOCAL)
+                                       sprintf(buf, GLOBAL_AFTER_ASSIGN,
+                                               name);
+                               else
+                                       sprintf(buf, GLOBAL_AFTER_USE, name);
+                               if (PyErr_WarnExplicit(PyExc_SyntaxWarning,
+                                                      buf, st->st_filename,
+                                                      st->st_cur->ste_lineno,
+                                                      NULL, NULL) < 0)
+                               {
+                                       if (PyErr_ExceptionMatches(
+                                               PyExc_SyntaxWarning))
+                                       {
+                                               PyErr_SetString(
+                                                   PyExc_SyntaxError, buf);
+                                               PyErr_SyntaxLocation(
+                                                   st->st_filename,
+                                                   st->st_cur->ste_lineno);
+                                       }
                                        st->st_errors++;
                                }
                        }