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

Skip to content

Commit 483638c

Browse files
committed
Undo recent change that banned using import to bind a global, as per
discussion on python-dev. 'from mod import *' is still banned except at the module level. Fix value for special NOOPT entry in symtable. Initialze to 0 instead of None, so that later uses of PyInt_AS_LONG() are valid. (Bug reported by Donn Cave.) replace local REPR macros with PyObject_REPR in object.h
1 parent fb9d712 commit 483638c

5 files changed

Lines changed: 31 additions & 29 deletions

File tree

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
294294
extern DL_IMPORT(long) _Py_HashDouble(double);
295295
extern DL_IMPORT(long) _Py_HashPointer(void*);
296296

297+
/* Helper for passing objects to printf and the like */
298+
#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
299+
297300
/* Flag bits for printing: */
298301
#define Py_PRINT_RAW 1 /* No string quotes etc. */
299302

Lib/test/output/test_grammar

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ return_stmt
3737
raise_stmt
3838
import_stmt
3939
SyntaxError expected for "def f(): from sys import *"
40-
SyntaxError expected for "def f(): global time; import "
4140
global_stmt
4241
exec_stmt
4342
if_stmt

Lib/test/test_grammar.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ def g2(): return 1
368368
from sys import *
369369
from sys import path, argv
370370
check_syntax("def f(): from sys import *")
371-
check_syntax("def f(): global time; import ")
372371

373372
print 'global_stmt' # 'global' NAME (',' NAME)*
374373
def f():

Python/ceval.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
3333

34-
#define REPR(ob) PyString_AS_STRING(PyObject_Repr(ob))
35-
3634
/* Forward declarations */
3735

3836
static PyObject *eval_code2(PyCodeObject *,
@@ -1456,7 +1454,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
14561454
if ((x = f->f_locals) == NULL) {
14571455
PyErr_Format(PyExc_SystemError,
14581456
"no locals found when storing %s",
1459-
REPR(w));
1457+
PyObject_REPR(w));
14601458
break;
14611459
}
14621460
err = PyDict_SetItem(x, w, v);
@@ -1468,7 +1466,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
14681466
if ((x = f->f_locals) == NULL) {
14691467
PyErr_Format(PyExc_SystemError,
14701468
"no locals when deleting %s",
1471-
REPR(w));
1469+
PyObject_REPR(w));
14721470
break;
14731471
}
14741472
if ((err = PyDict_DelItem(x, w)) != 0)
@@ -1563,7 +1561,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
15631561
if ((x = f->f_locals) == NULL) {
15641562
PyErr_Format(PyExc_SystemError,
15651563
"no locals when loading %s",
1566-
REPR(w));
1564+
PyObject_REPR(w));
15671565
break;
15681566
}
15691567
x = PyDict_GetItem(x, w);

Python/compile.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "opcode.h"
2020
#include "structmember.h"
2121

22-
#define REPR(O) PyString_AS_STRING(PyObject_Repr(O))
23-
2422
#include <ctype.h>
2523

2624
/* Three symbols from graminit.h are also defined in Python.h, with
@@ -66,9 +64,6 @@ int Py_OptimizeFlag = 0;
6664
#define ILLEGAL_IMPORT_STAR \
6765
"'from ... import *' may only occur in a module scope"
6866

69-
#define ILLEGAL_IMPORT_GLOBAL \
70-
"may not import name '%.400s' because it is declared global"
71-
7267
#define MANGLE_LEN 256
7368

7469
#define OFF(x) offsetof(PyCodeObject, x)
@@ -2204,7 +2199,7 @@ com_make_closure(struct compiling *c, PyCodeObject *co)
22042199
arg = com_lookup_arg(c->c_freevars, name);
22052200
if (arg == -1) {
22062201
fprintf(stderr, "lookup %s in %s %d %d\n",
2207-
REPR(name), c->c_name, reftype, arg);
2202+
PyObject_REPR(name), c->c_name, reftype, arg);
22082203
Py_FatalError("com_make_closure()");
22092204
}
22102205
com_addoparg(c, LOAD_CLOSURE, arg);
@@ -3995,7 +3990,8 @@ get_ref_type(struct compiling *c, char *name)
39953990
{
39963991
char buf[250];
39973992
sprintf(buf, "unknown scope for %.100s in %.100s (%s)",
3998-
name, c->c_name, REPR(c->c_symtable->st_cur_id));
3993+
name, c->c_name,
3994+
PyObject_REPR(c->c_symtable->st_cur_id));
39993995
Py_FatalError(buf);
40003996
}
40013997
return -1; /* can't get here */
@@ -4117,13 +4113,6 @@ symtable_load_symbols(struct compiling *c)
41174113
com_error(c, PyExc_SyntaxError, buf);
41184114
goto fail;
41194115
}
4120-
if (info & DEF_IMPORT) {
4121-
char buf[500];
4122-
sprintf(buf, ILLEGAL_IMPORT_GLOBAL,
4123-
PyString_AS_STRING(name));
4124-
com_error(c, PyExc_SyntaxError, buf);
4125-
goto fail;
4126-
}
41274116
if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
41284117
goto fail;
41294118
} else if (info & DEF_FREE_GLOBAL) {
@@ -4529,7 +4518,7 @@ symtable_add_def_o(struct symtable *st, PyObject *dict,
45294518
val = PyInt_AS_LONG(o);
45304519
if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
45314520
PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4532-
name);
4521+
PyString_AsString(name));
45334522
return -1;
45344523
}
45354524
val |= flag;
@@ -4626,15 +4615,24 @@ symtable_node(struct symtable *st, node *n)
46264615
case import_stmt:
46274616
symtable_import(st, n);
46284617
break;
4629-
case exec_stmt:
4630-
if (PyDict_SetItemString(st->st_cur, NOOPT, Py_None) < 0)
4618+
case exec_stmt: {
4619+
PyObject *zero = PyInt_FromLong(0);
4620+
if (zero == NULL)
46314621
st->st_errors++;
4622+
else {
4623+
if (PyDict_SetItemString(st->st_cur, NOOPT,
4624+
zero) < 0)
4625+
st->st_errors++;
4626+
Py_DECREF(zero);
4627+
}
46324628
symtable_node(st, CHILD(n, 1));
46334629
if (NCH(n) > 2)
46344630
symtable_node(st, CHILD(n, 3));
46354631
if (NCH(n) > 4)
46364632
symtable_node(st, CHILD(n, 5));
46374633
break;
4634+
4635+
}
46384636
case except_clause:
46394637
if (NCH(n) == 4)
46404638
symtable_assign(st, CHILD(n, 3), 0);
@@ -4848,24 +4846,29 @@ static void
48484846
symtable_import(struct symtable *st, node *n)
48494847
{
48504848
int i;
4851-
/*
4852-
import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
4849+
/* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
48534850
| 'from' dotted_name 'import'
48544851
('*' | import_as_name (',' import_as_name)*)
4855-
import_as_name: NAME [NAME NAME]
4852+
import_as_name: NAME [NAME NAME]
48564853
*/
48574854

48584855
if (STR(CHILD(n, 0))[0] == 'f') { /* from */
48594856
if (TYPE(CHILD(n, 3)) == STAR) {
4857+
PyObject *zero = PyInt_FromLong(0);
48604858
if (st->st_cur_type != TYPE_MODULE) {
48614859
PyErr_SetString(PyExc_SyntaxError,
48624860
ILLEGAL_IMPORT_STAR);
48634861
st->st_errors++;
48644862
return;
48654863
}
4866-
if (PyDict_SetItemString(st->st_cur, NOOPT,
4867-
Py_None) < 0)
4864+
if (zero == NULL)
48684865
st->st_errors++;
4866+
else {
4867+
if (PyDict_SetItemString(st->st_cur, NOOPT,
4868+
zero) < 0)
4869+
st->st_errors++;
4870+
Py_DECREF(zero);
4871+
}
48694872
} else {
48704873
for (i = 3; i < NCH(n); i += 2) {
48714874
node *c = CHILD(n, i);

0 commit comments

Comments
 (0)