From 483638c9a865d504b1131c098f010590103415dd Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Thu, 1 Feb 2001 20:20:45 +0000 Subject: 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 --- Include/object.h | 3 +++ Lib/test/output/test_grammar | 1 - Lib/test/test_grammar.py | 1 - Python/ceval.c | 8 +++----- Python/compile.c | 47 +++++++++++++++++++++++--------------------- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Include/object.h b/Include/object.h index 1eaa900..972e05b 100644 --- a/Include/object.h +++ b/Include/object.h @@ -294,6 +294,9 @@ extern DL_IMPORT(void) Py_ReprLeave(PyObject *); extern DL_IMPORT(long) _Py_HashDouble(double); extern DL_IMPORT(long) _Py_HashPointer(void*); +/* Helper for passing objects to printf and the like */ +#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj)) + /* Flag bits for printing: */ #define Py_PRINT_RAW 1 /* No string quotes etc. */ diff --git a/Lib/test/output/test_grammar b/Lib/test/output/test_grammar index a262e79..172a597 100644 --- a/Lib/test/output/test_grammar +++ b/Lib/test/output/test_grammar @@ -37,7 +37,6 @@ return_stmt raise_stmt import_stmt SyntaxError expected for "def f(): from sys import *" -SyntaxError expected for "def f(): global time; import " global_stmt exec_stmt if_stmt diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 022398d..6e0fe91 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -368,7 +368,6 @@ from time import time from sys import * from sys import path, argv check_syntax("def f(): from sys import *") -check_syntax("def f(): global time; import ") print 'global_stmt' # 'global' NAME (',' NAME)* def f(): diff --git a/Python/ceval.c b/Python/ceval.c index 15e0b3a..8eaa8bd 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -31,8 +31,6 @@ typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); -#define REPR(ob) PyString_AS_STRING(PyObject_Repr(ob)) - /* Forward declarations */ static PyObject *eval_code2(PyCodeObject *, @@ -1456,7 +1454,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals, if ((x = f->f_locals) == NULL) { PyErr_Format(PyExc_SystemError, "no locals found when storing %s", - REPR(w)); + PyObject_REPR(w)); break; } err = PyDict_SetItem(x, w, v); @@ -1468,7 +1466,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals, if ((x = f->f_locals) == NULL) { PyErr_Format(PyExc_SystemError, "no locals when deleting %s", - REPR(w)); + PyObject_REPR(w)); break; } if ((err = PyDict_DelItem(x, w)) != 0) @@ -1563,7 +1561,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals, if ((x = f->f_locals) == NULL) { PyErr_Format(PyExc_SystemError, "no locals when loading %s", - REPR(w)); + PyObject_REPR(w)); break; } x = PyDict_GetItem(x, w); diff --git a/Python/compile.c b/Python/compile.c index d95287c..68f9e7f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -19,8 +19,6 @@ #include "opcode.h" #include "structmember.h" -#define REPR(O) PyString_AS_STRING(PyObject_Repr(O)) - #include /* Three symbols from graminit.h are also defined in Python.h, with @@ -66,9 +64,6 @@ int Py_OptimizeFlag = 0; #define ILLEGAL_IMPORT_STAR \ "'from ... import *' may only occur in a module scope" -#define ILLEGAL_IMPORT_GLOBAL \ -"may not import name '%.400s' because it is declared global" - #define MANGLE_LEN 256 #define OFF(x) offsetof(PyCodeObject, x) @@ -2204,7 +2199,7 @@ com_make_closure(struct compiling *c, PyCodeObject *co) arg = com_lookup_arg(c->c_freevars, name); if (arg == -1) { fprintf(stderr, "lookup %s in %s %d %d\n", - REPR(name), c->c_name, reftype, arg); + PyObject_REPR(name), c->c_name, reftype, arg); Py_FatalError("com_make_closure()"); } com_addoparg(c, LOAD_CLOSURE, arg); @@ -3995,7 +3990,8 @@ get_ref_type(struct compiling *c, char *name) { char buf[250]; sprintf(buf, "unknown scope for %.100s in %.100s (%s)", - name, c->c_name, REPR(c->c_symtable->st_cur_id)); + name, c->c_name, + PyObject_REPR(c->c_symtable->st_cur_id)); Py_FatalError(buf); } return -1; /* can't get here */ @@ -4117,13 +4113,6 @@ symtable_load_symbols(struct compiling *c) com_error(c, PyExc_SyntaxError, buf); goto fail; } - if (info & DEF_IMPORT) { - char buf[500]; - sprintf(buf, ILLEGAL_IMPORT_GLOBAL, - PyString_AS_STRING(name)); - com_error(c, PyExc_SyntaxError, buf); - goto fail; - } if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) goto fail; } else if (info & DEF_FREE_GLOBAL) { @@ -4529,7 +4518,7 @@ symtable_add_def_o(struct symtable *st, PyObject *dict, val = PyInt_AS_LONG(o); if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, - name); + PyString_AsString(name)); return -1; } val |= flag; @@ -4626,15 +4615,24 @@ symtable_node(struct symtable *st, node *n) case import_stmt: symtable_import(st, n); break; - case exec_stmt: - if (PyDict_SetItemString(st->st_cur, NOOPT, Py_None) < 0) + case exec_stmt: { + PyObject *zero = PyInt_FromLong(0); + if (zero == NULL) st->st_errors++; + else { + if (PyDict_SetItemString(st->st_cur, NOOPT, + zero) < 0) + st->st_errors++; + Py_DECREF(zero); + } symtable_node(st, CHILD(n, 1)); if (NCH(n) > 2) symtable_node(st, CHILD(n, 3)); if (NCH(n) > 4) symtable_node(st, CHILD(n, 5)); break; + + } case except_clause: if (NCH(n) == 4) symtable_assign(st, CHILD(n, 3), 0); @@ -4848,24 +4846,29 @@ static void symtable_import(struct symtable *st, node *n) { int i; - /* - import_stmt: 'import' dotted_as_name (',' dotted_as_name)* + /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*) - import_as_name: NAME [NAME NAME] + import_as_name: NAME [NAME NAME] */ if (STR(CHILD(n, 0))[0] == 'f') { /* from */ if (TYPE(CHILD(n, 3)) == STAR) { + PyObject *zero = PyInt_FromLong(0); if (st->st_cur_type != TYPE_MODULE) { PyErr_SetString(PyExc_SyntaxError, ILLEGAL_IMPORT_STAR); st->st_errors++; return; } - if (PyDict_SetItemString(st->st_cur, NOOPT, - Py_None) < 0) + if (zero == NULL) st->st_errors++; + else { + if (PyDict_SetItemString(st->st_cur, NOOPT, + zero) < 0) + st->st_errors++; + Py_DECREF(zero); + } } else { for (i = 3; i < NCH(n); i += 2) { node *c = CHILD(n, i); -- cgit v0.12