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_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. */

View File

@ -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

View File

@ -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():

View File

@ -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);

View File

@ -19,8 +19,6 @@
#include "opcode.h"
#include "structmember.h"
#define REPR(O) PyString_AS_STRING(PyObject_Repr(O))
#include <ctype.h>
/* 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);