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
This commit is contained in:
Jeremy Hylton 2001-02-01 20:20:45 +00:00
parent fb9d712721
commit 483638c9a8
5 changed files with 31 additions and 29 deletions

View file

@ -294,6 +294,9 @@ extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
extern DL_IMPORT(long) _Py_HashDouble(double); extern DL_IMPORT(long) _Py_HashDouble(double);
extern DL_IMPORT(long) _Py_HashPointer(void*); 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: */ /* Flag bits for printing: */
#define Py_PRINT_RAW 1 /* No string quotes etc. */ #define Py_PRINT_RAW 1 /* No string quotes etc. */

View file

@ -37,7 +37,6 @@ return_stmt
raise_stmt raise_stmt
import_stmt import_stmt
SyntaxError expected for "def f(): from sys import *" SyntaxError expected for "def f(): from sys import *"
SyntaxError expected for "def f(): global time; import "
global_stmt global_stmt
exec_stmt exec_stmt
if_stmt if_stmt

View file

@ -368,7 +368,6 @@ from time import time
from sys import * from sys import *
from sys import path, argv from sys import path, argv
check_syntax("def f(): from sys import *") check_syntax("def f(): from sys import *")
check_syntax("def f(): global time; import ")
print 'global_stmt' # 'global' NAME (',' NAME)* print 'global_stmt' # 'global' NAME (',' NAME)*
def f(): def f():

View file

@ -31,8 +31,6 @@
typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
#define REPR(ob) PyString_AS_STRING(PyObject_Repr(ob))
/* Forward declarations */ /* Forward declarations */
static PyObject *eval_code2(PyCodeObject *, static PyObject *eval_code2(PyCodeObject *,
@ -1456,7 +1454,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
if ((x = f->f_locals) == NULL) { if ((x = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals found when storing %s", "no locals found when storing %s",
REPR(w)); PyObject_REPR(w));
break; break;
} }
err = PyDict_SetItem(x, w, v); err = PyDict_SetItem(x, w, v);
@ -1468,7 +1466,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
if ((x = f->f_locals) == NULL) { if ((x = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals when deleting %s", "no locals when deleting %s",
REPR(w)); PyObject_REPR(w));
break; break;
} }
if ((err = PyDict_DelItem(x, w)) != 0) 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) { if ((x = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals when loading %s", "no locals when loading %s",
REPR(w)); PyObject_REPR(w));
break; break;
} }
x = PyDict_GetItem(x, w); x = PyDict_GetItem(x, w);

View file

@ -19,8 +19,6 @@
#include "opcode.h" #include "opcode.h"
#include "structmember.h" #include "structmember.h"
#define REPR(O) PyString_AS_STRING(PyObject_Repr(O))
#include <ctype.h> #include <ctype.h>
/* Three symbols from graminit.h are also defined in Python.h, with /* Three symbols from graminit.h are also defined in Python.h, with
@ -66,9 +64,6 @@ int Py_OptimizeFlag = 0;
#define ILLEGAL_IMPORT_STAR \ #define ILLEGAL_IMPORT_STAR \
"'from ... import *' may only occur in a module scope" "'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 MANGLE_LEN 256
#define OFF(x) offsetof(PyCodeObject, x) #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); arg = com_lookup_arg(c->c_freevars, name);
if (arg == -1) { if (arg == -1) {
fprintf(stderr, "lookup %s in %s %d %d\n", 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()"); Py_FatalError("com_make_closure()");
} }
com_addoparg(c, LOAD_CLOSURE, arg); com_addoparg(c, LOAD_CLOSURE, arg);
@ -3995,7 +3990,8 @@ get_ref_type(struct compiling *c, char *name)
{ {
char buf[250]; char buf[250];
sprintf(buf, "unknown scope for %.100s in %.100s (%s)", 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); Py_FatalError(buf);
} }
return -1; /* can't get here */ return -1; /* can't get here */
@ -4117,13 +4113,6 @@ symtable_load_symbols(struct compiling *c)
com_error(c, PyExc_SyntaxError, buf); com_error(c, PyExc_SyntaxError, buf);
goto fail; 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) if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
goto fail; goto fail;
} else if (info & DEF_FREE_GLOBAL) { } else if (info & DEF_FREE_GLOBAL) {
@ -4529,7 +4518,7 @@ symtable_add_def_o(struct symtable *st, PyObject *dict,
val = PyInt_AS_LONG(o); val = PyInt_AS_LONG(o);
if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
name); PyString_AsString(name));
return -1; return -1;
} }
val |= flag; val |= flag;
@ -4626,15 +4615,24 @@ symtable_node(struct symtable *st, node *n)
case import_stmt: case import_stmt:
symtable_import(st, n); symtable_import(st, n);
break; break;
case exec_stmt: case exec_stmt: {
if (PyDict_SetItemString(st->st_cur, NOOPT, Py_None) < 0) PyObject *zero = PyInt_FromLong(0);
if (zero == NULL)
st->st_errors++; 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)); symtable_node(st, CHILD(n, 1));
if (NCH(n) > 2) if (NCH(n) > 2)
symtable_node(st, CHILD(n, 3)); symtable_node(st, CHILD(n, 3));
if (NCH(n) > 4) if (NCH(n) > 4)
symtable_node(st, CHILD(n, 5)); symtable_node(st, CHILD(n, 5));
break; break;
}
case except_clause: case except_clause:
if (NCH(n) == 4) if (NCH(n) == 4)
symtable_assign(st, CHILD(n, 3), 0); symtable_assign(st, CHILD(n, 3), 0);
@ -4848,8 +4846,7 @@ static void
symtable_import(struct symtable *st, node *n) symtable_import(struct symtable *st, node *n)
{ {
int i; int i;
/* /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
| 'from' dotted_name 'import' | 'from' dotted_name 'import'
('*' | import_as_name (',' import_as_name)*) ('*' | import_as_name (',' import_as_name)*)
import_as_name: NAME [NAME NAME] import_as_name: NAME [NAME NAME]
@ -4857,15 +4854,21 @@ symtable_import(struct symtable *st, node *n)
if (STR(CHILD(n, 0))[0] == 'f') { /* from */ if (STR(CHILD(n, 0))[0] == 'f') { /* from */
if (TYPE(CHILD(n, 3)) == STAR) { if (TYPE(CHILD(n, 3)) == STAR) {
PyObject *zero = PyInt_FromLong(0);
if (st->st_cur_type != TYPE_MODULE) { if (st->st_cur_type != TYPE_MODULE) {
PyErr_SetString(PyExc_SyntaxError, PyErr_SetString(PyExc_SyntaxError,
ILLEGAL_IMPORT_STAR); ILLEGAL_IMPORT_STAR);
st->st_errors++; st->st_errors++;
return; return;
} }
if (PyDict_SetItemString(st->st_cur, NOOPT, if (zero == NULL)
Py_None) < 0)
st->st_errors++; st->st_errors++;
else {
if (PyDict_SetItemString(st->st_cur, NOOPT,
zero) < 0)
st->st_errors++;
Py_DECREF(zero);
}
} else { } else {
for (i = 3; i < NCH(n); i += 2) { for (i = 3; i < NCH(n); i += 2) {
node *c = CHILD(n, i); node *c = CHILD(n, i);