Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d0ad50

Browse files
committed
Bug #889500, fix line number on SyntaxWarning for global declarations.
1 parent a3bdc2c commit 5d0ad50

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 2.5 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Bug #889500, fix line number on SyntaxWarning for global declarations.
16+
1517
- Bug #1378022, UTF-8 files with a leading BOM crashed the interpreter.
1618

1719
- Support for converting hex strings to floats no longer works.

Python/symtable.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#include "symtable.h"
55
#include "structmember.h"
66

7-
/* two error strings used for warnings */
7+
/* error strings used for warnings */
88
#define GLOBAL_AFTER_ASSIGN \
99
"name '%.400s' is assigned to before global declaration"
1010

1111
#define GLOBAL_AFTER_USE \
1212
"name '%.400s' is used prior to global declaration"
1313

14+
#define IMPORT_STAR_WARNING "import * only allowed at module level"
15+
16+
1417
PySTEntryObject *
1518
PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
1619
void *key, int lineno)
@@ -152,7 +155,7 @@ PyTypeObject PySTEntry_Type = {
152155
};
153156

154157
static int symtable_analyze(struct symtable *st);
155-
static int symtable_warn(struct symtable *st, char *msg);
158+
static int symtable_warn(struct symtable *st, char *msg, int lineno);
156159
static int symtable_enter_block(struct symtable *st, identifier name,
157160
_Py_block_ty block, void *ast, int lineno);
158161
static int symtable_exit_block(struct symtable *st, void *ast);
@@ -686,10 +689,10 @@ symtable_analyze(struct symtable *st)
686689

687690

688691
static int
689-
symtable_warn(struct symtable *st, char *msg)
692+
symtable_warn(struct symtable *st, char *msg, int lineno)
690693
{
691694
if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
692-
st->st_cur->ste_lineno, NULL, NULL) < 0) {
695+
lineno, NULL, NULL) < 0) {
693696
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
694697
PyErr_SetString(PyExc_SyntaxError, msg);
695698
PyErr_SyntaxLocation(st->st_filename,
@@ -1028,7 +1031,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
10281031
PyOS_snprintf(buf, sizeof(buf),
10291032
GLOBAL_AFTER_USE,
10301033
c_name);
1031-
if (!symtable_warn(st, buf))
1034+
if (!symtable_warn(st, buf, s->lineno))
10321035
return 0;
10331036
}
10341037
if (!symtable_add_def(st, name, DEF_GLOBAL))
@@ -1277,8 +1280,8 @@ symtable_visit_alias(struct symtable *st, alias_ty a)
12771280
}
12781281
else {
12791282
if (st->st_cur->ste_type != ModuleBlock) {
1280-
if (!symtable_warn(st,
1281-
"import * only allowed at module level")) {
1283+
int lineno = st->st_cur->ste_lineno;
1284+
if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
12821285
Py_DECREF(store_name);
12831286
return 0;
12841287
}

0 commit comments

Comments
 (0)