]> granicus.if.org Git - python/commitdiff
Undo recent change that banned using import to bind a global, as per
authorJeremy Hylton <jeremy@alum.mit.edu>
Thu, 1 Feb 2001 20:20:45 +0000 (20:20 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Thu, 1 Feb 2001 20:20:45 +0000 (20:20 +0000)
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
Lib/test/output/test_grammar
Lib/test/test_grammar.py
Python/ceval.c
Python/compile.c

index 1eaa900235ae4a7c6665e3a0fa3e8ef8097e21ec..972e05b1e7f7329a51c21543602776c7cbbb881e 100644 (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. */
 
index a262e794185b444038a674a460fe702fb18c661e..172a597467f93f750a28dfccd4c8c4556c570be4 100644 (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
index 022398d10a1e4caa3fb39c5a1555153fdd19f21d..6e0fe91b66dd9bc0740ebb4729c5e7a2d4a419be 100644 (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():
index 15e0b3afb62f16434a80d4622ec2e3700d31500c..8eaa8bdbf5050f32f00fcb8c063b751aa8bb865a 100644 (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);
index d95287cf262b45a41ef74d021c0603aea998ed53..68f9e7f98c16fdf5e1795cc9bb3ec45a1d212124 100644 (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);