[Python-checkins] CVS: python/dist/src/Python compile.c,2.171,2.172 symtable.c,2.2,2.3
Jeremy Hylton
jhylton@users.sourceforge.net
Mon, 26 Feb 2001 20:23:36 -0800
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv28404/Python
Modified Files:
compile.c symtable.c
Log Message:
Preliminary support for future nested scopes
compile.h: #define NESTED_SCOPES_DEFAULT 0 for Python 2.1
__future__ feature name: "nested_scopes"
symtable.h: Add st_nested_scopes slot. Define flags to track exec and
import star.
Lib/test/test_scope.py: requires nested scopes
compile.c: Fiddle with error messages.
Reverse the sense of ste_optimized flag on
PySymtableEntryObjects. If it is true, there is an optimization
conflict.
Modify get_ref_type to respect st_nested_scopes flags.
Refactor symtable_load_symbols() into several smaller functions,
which use struct symbol_info to share variables. In new function
symtable_update_flags(), raise an error or warning for import * or
bare exec that conflicts with nested scopes. Also, modify handle
for free variables to respect st_nested_scopes flag.
In symtable_init() assign st_nested_scopes flag to
NESTED_SCOPES_DEFAULT (defined in compile.h).
Add preliminary and often incorrect implementation of
symtable_check_future().
Add symtable_lookup() helper for future use.
Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.171
retrieving revision 2.172
diff -C2 -r2.171 -r2.172
*** compile.c 2001/02/23 22:23:53 2.171
--- compile.c 2001/02/27 04:23:34 2.172
***************
*** 56,59 ****
--- 56,71 ----
"%.100s: exec or 'import *' makes names ambiguous in nested scope"
+ #define UNDEFINED_FUTURE_FEATURE \
+ "future feature %.100s is not defined"
+
+ #define GLOBAL_AFTER_ASSIGN \
+ "name '%.400s' is assigned to before global declaration"
+
+ #define GLOBAL_AFTER_USE \
+ "name '%.400s' is used prior to global declaration"
+
+ #define LOCAL_GLOBAL \
+ "name '%.400s' is a function paramter and declared global"
+
#define MANGLE_LEN 256
***************
*** 425,429 ****
}
-
/* Interface to the block stack */
--- 437,440 ----
***************
*** 450,454 ****
}
-
/* Prototype forward declarations */
--- 461,464 ----
***************
*** 2107,2110 ****
--- 2117,2123 ----
{
int i, free = PyTuple_GET_SIZE(co->co_freevars);
+ /* If the code is compiled with st->st_nested_scopes == 0,
+ then no variable will ever be added to co_freevars.
+ */
if (free == 0)
return 0;
***************
*** 2296,2300 ****
if (assigning > OP_APPLY) {
com_error(c, PyExc_SyntaxError,
! "augmented assign to tuple not possible");
return;
}
--- 2309,2313 ----
if (assigning > OP_APPLY) {
com_error(c, PyExc_SyntaxError,
! "augmented assign to tuple not possible");
return;
}
***************
*** 3940,3956 ****
{
PyObject *v;
! if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
! return CELL;
! if (PyDict_GetItemString(c->c_locals, name) != NULL)
! return LOCAL;
! if (PyDict_GetItemString(c->c_freevars, name) != NULL)
! return FREE;
! v = PyDict_GetItemString(c->c_globals, name);
! if (v) {
! if (v == Py_None)
! return GLOBAL_EXPLICIT;
! else {
! return GLOBAL_IMPLICIT;
}
}
{
--- 3953,3982 ----
{
PyObject *v;
! if (c->c_symtable->st_nested_scopes) {
! if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
! return CELL;
! if (PyDict_GetItemString(c->c_locals, name) != NULL)
! return LOCAL;
! if (PyDict_GetItemString(c->c_freevars, name) != NULL)
! return FREE;
! v = PyDict_GetItemString(c->c_globals, name);
! if (v) {
! if (v == Py_None)
! return GLOBAL_EXPLICIT;
! else {
! return GLOBAL_IMPLICIT;
! }
}
+ } else {
+ if (PyDict_GetItemString(c->c_locals, name) != NULL)
+ return LOCAL;
+ v = PyDict_GetItemString(c->c_globals, name);
+ if (v) {
+ if (v == Py_None)
+ return GLOBAL_EXPLICIT;
+ else {
+ return GLOBAL_IMPLICIT;
+ }
+ }
}
{
***************
*** 3985,4010 ****
static int
! symtable_load_symbols(struct compiling *c)
{
! static PyObject *implicit = NULL;
! PyObject *name, *varnames, *v;
! int i, flags, pos;
! int nlocals, nfrees, ncells, nimplicit;
! struct symtable *st = c->c_symtable;
! PySymtableEntryObject *ste = st->st_cur;
!
! if (implicit == NULL) {
! implicit = PyInt_FromLong(1);
! if (implicit == NULL)
! return -1;
! }
! v = NULL;
! varnames = st->st_cur->ste_varnames;
if (varnames == NULL) {
varnames = PyList_New(0);
if (varnames == NULL)
return -1;
! ste->ste_varnames = varnames;
Py_INCREF(varnames);
} else
--- 4011,4024 ----
static int
! symtable_init_compiling_symbols(struct compiling *c)
{
! PyObject *varnames;
! varnames = c->c_symtable->st_cur->ste_varnames;
if (varnames == NULL) {
varnames = PyList_New(0);
if (varnames == NULL)
return -1;
! c->c_symtable->st_cur->ste_varnames = varnames;
Py_INCREF(varnames);
} else
***************
*** 4014,4031 ****
c->c_globals = PyDict_New();
if (c->c_globals == NULL)
! goto fail;
c->c_freevars = PyDict_New();
if (c->c_freevars == NULL)
! goto fail;
c->c_cellvars = PyDict_New();
if (c->c_cellvars == NULL)
goto fail;
! nlocals = PyList_GET_SIZE(varnames);
! c->c_argcount = nlocals;
! nfrees = 0;
! ncells = 0;
! nimplicit = 0;
! for (i = 0; i < nlocals; ++i) {
v = PyInt_FromLong(i);
if (PyDict_SetItem(c->c_locals,
--- 4028,4169 ----
c->c_globals = PyDict_New();
if (c->c_globals == NULL)
! return -1;
c->c_freevars = PyDict_New();
if (c->c_freevars == NULL)
! return -1;
c->c_cellvars = PyDict_New();
if (c->c_cellvars == NULL)
+ return -1;
+ return 0;
+ }
+
+ struct symbol_info {
+ int si_nlocals;
+ int si_ncells;
+ int si_nfrees;
+ int si_nimplicit;
+ };
+
+ static void
+ symtable_init_info(struct symbol_info *si)
+ {
+ si->si_nlocals = 0;
+ si->si_ncells = 0;
+ si->si_nfrees = 0;
+ si->si_nimplicit = 0;
+ }
+
+ static int
+ symtable_resolve_free(struct compiling *c, PyObject *name,
+ struct symbol_info *si)
+ {
+ PyObject *dict, *v;
+
+ /* Seperate logic for DEF_FREE. If it occurs in a function,
+ it indicates a local that we must allocate storage for (a
+ cell var). If it occurs in a class, then the class has a
+ method and a free variable with the same name.
+ */
+
+ if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
+ v = PyInt_FromLong(si->si_ncells++);
+ dict = c->c_cellvars;
+ } else {
+ v = PyInt_FromLong(si->si_nfrees++);
+ dict = c->c_freevars;
+ }
+ if (v == NULL)
+ return -1;
+ if (PyDict_SetItem(dict, name, v) < 0) {
+ Py_DECREF(v);
+ return -1;
+ }
+ Py_DECREF(v);
+ return 0;
+ }
+
+ static int
+ symtable_freevar_offsets(PyObject *freevars, int offset)
+ {
+ PyObject *name, *v;
+ int pos;
+
+ /* The cell vars are the first elements of the closure,
+ followed by the free vars. Update the offsets in
+ c_freevars to account for number of cellvars. */
+ pos = 0;
+ while (PyDict_Next(freevars, &pos, &name, &v)) {
+ int i = PyInt_AS_LONG(v) + offset;
+ PyObject *o = PyInt_FromLong(i);
+ if (o == NULL)
+ return -1;
+ if (PyDict_SetItem(freevars, name, o) < 0) {
+ Py_DECREF(o);
+ return -1;
+ }
+ Py_DECREF(o);
+ }
+ return 0;
+ }
+
+ static int
+ symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
+ struct symbol_info *si)
+ {
+ if (ste->ste_type != TYPE_MODULE)
+ c->c_flags |= CO_NEWLOCALS;
+ if (ste->ste_type == TYPE_FUNCTION) {
+ c->c_nlocals = si->si_nlocals;
+ if (ste->ste_optimized == 0)
+ c->c_flags |= CO_OPTIMIZED;
+ else if (si->si_ncells || si->si_nfrees
+ || (ste->ste_nested && si->si_nimplicit)
+ || ste->ste_child_free) {
+ if (c->c_symtable->st_nested_scopes) {
+ PyErr_Format(PyExc_SyntaxError,
+ ILLEGAL_DYNAMIC_SCOPE,
+ PyString_AS_STRING(ste->ste_name));
+ set_error_location(c->c_symtable->st_filename,
+ ste->ste_lineno);
+ return -1;
+ } else {
+ char buf[200];
+ sprintf(buf, ILLEGAL_DYNAMIC_SCOPE,
+ PyString_AS_STRING(ste->ste_name));
+ if (PyErr_Warn(PyExc_SyntaxWarning,
+ buf) < 0) {
+ return -1;
+ }
+ }
+ }
+ }
+ return 0;
+ }
+
+ static int
+ symtable_load_symbols(struct compiling *c)
+ {
+ static PyObject *implicit = NULL;
+ struct symtable *st = c->c_symtable;
+ PySymtableEntryObject *ste = st->st_cur;
+ PyObject *name, *varnames, *v;
+ int i, flags, pos;
+ struct symbol_info si;
+
+ if (implicit == NULL) {
+ implicit = PyInt_FromLong(1);
+ if (implicit == NULL)
+ return -1;
+ }
+ v = NULL;
+
+ if (symtable_init_compiling_symbols(c) < 0)
goto fail;
+ symtable_init_info(&si);
+ varnames = st->st_cur->ste_varnames;
+ si.si_nlocals = PyList_GET_SIZE(varnames);
+ c->c_argcount = si.si_nlocals;
! for (i = 0; i < si.si_nlocals; ++i) {
v = PyInt_FromLong(i);
if (PyDict_SetItem(c->c_locals,
***************
*** 4042,4071 ****
if (flags & DEF_FREE_GLOBAL)
! /* undo the original DEF_FREE */
! flags &= ~(DEF_FREE | DEF_FREE_CLASS);
- /* Seperate logic for DEF_FREE. If it occurs in a
- function, it indicates a local that we must
- allocate storage for (a cell var). If it occurs in
- a class, then the class has a method and a free
- variable with the same name.
- */
-
if ((flags & (DEF_FREE | DEF_FREE_CLASS))
! && (flags & (DEF_LOCAL | DEF_PARAM))) {
! PyObject *dict;
! if (ste->ste_type == TYPE_FUNCTION) {
! v = PyInt_FromLong(ncells++);
! dict = c->c_cellvars;
! } else {
! v = PyInt_FromLong(nfrees++);
! dict = c->c_freevars;
! }
! if (v == NULL)
! return -1;
! if (PyDict_SetItem(dict, name, v) < 0)
! goto fail;
! Py_DECREF(v);
! }
if (flags & DEF_STAR) {
--- 4180,4189 ----
if (flags & DEF_FREE_GLOBAL)
! /* undo the original DEF_FREE */
! flags &= ~(DEF_FREE | DEF_FREE_CLASS);
if ((flags & (DEF_FREE | DEF_FREE_CLASS))
! && (flags & (DEF_LOCAL | DEF_PARAM)))
! symtable_resolve_free(c, name, &si);
if (flags & DEF_STAR) {
***************
*** 4078,4088 ****
c->c_argcount--;
else if (flags & DEF_GLOBAL) {
! if ((flags & DEF_PARAM)
! && (PyString_AS_STRING(name)[0] != '.')){
! PyErr_Format(PyExc_SyntaxError,
! "name '%.400s' is local and global",
PyString_AS_STRING(name));
! set_error_location(st->st_filename,
ste->ste_lineno);
goto fail;
}
--- 4196,4205 ----
c->c_argcount--;
else if (flags & DEF_GLOBAL) {
! if (flags & DEF_PARAM) {
! PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
PyString_AS_STRING(name));
! set_error_location(st->st_filename,
ste->ste_lineno);
+ st->st_errors++;
goto fail;
}
***************
*** 4090,4098 ****
goto fail;
} else if (flags & DEF_FREE_GLOBAL) {
! nimplicit++;
if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
goto fail;
} else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
! v = PyInt_FromLong(nlocals++);
if (v == NULL)
goto fail;
--- 4207,4215 ----
goto fail;
} else if (flags & DEF_FREE_GLOBAL) {
! si.si_nimplicit++;
if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
goto fail;
} else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
! v = PyInt_FromLong(si.si_nlocals++);
if (v == NULL)
goto fail;
***************
*** 4104,4109 ****
goto fail;
} else if (is_free(flags)) {
! if (ste->ste_nested) {
! v = PyInt_FromLong(nfrees++);
if (v == NULL)
goto fail;
--- 4221,4226 ----
goto fail;
} else if (is_free(flags)) {
! if (ste->ste_nested && st->st_nested_scopes) {
! v = PyInt_FromLong(si.si_nfrees++);
if (v == NULL)
goto fail;
***************
*** 4112,4116 ****
Py_DECREF(v);
} else {
! nimplicit++;
if (PyDict_SetItem(c->c_globals, name,
implicit) < 0)
--- 4229,4233 ----
Py_DECREF(v);
} else {
! si.si_nimplicit++;
if (PyDict_SetItem(c->c_globals, name,
implicit) < 0)
***************
*** 4120,4157 ****
}
! /* The cell vars are the first elements of the closure,
! followed by the free vars. Update the offsets in
! c_freevars to account for number of cellvars. */
! pos = 0;
! while (PyDict_Next(c->c_freevars, &pos, &name, &v)) {
! int i = PyInt_AS_LONG(v) + ncells;
! PyObject *o = PyInt_FromLong(i);
! if (PyDict_SetItem(c->c_freevars, name, o) < 0) {
! Py_DECREF(o);
! return -1;
! }
! Py_DECREF(o);
! }
!
! if (ste->ste_type == TYPE_FUNCTION)
! c->c_nlocals = nlocals;
!
! if (ste->ste_type != TYPE_MODULE)
! c->c_flags |= CO_NEWLOCALS;
! if (ste->ste_type == TYPE_FUNCTION) {
! if (ste->ste_optimized)
! c->c_flags |= CO_OPTIMIZED;
! else if (ncells || nfrees
! || (ste->ste_nested && nimplicit)
! || ste->ste_child_free) {
! PyErr_Format(PyExc_SyntaxError, ILLEGAL_DYNAMIC_SCOPE,
! PyString_AS_STRING(ste->ste_name));
! set_error_location(st->st_filename, ste->ste_lineno);
! return -1;
! }
! }
!
! return 0;
!
fail:
/* is this always the right thing to do? */
--- 4237,4243 ----
}
! if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
! return -1;
! return symtable_update_flags(c, ste, &si);
fail:
/* is this always the right thing to do? */
***************
*** 4169,4172 ****
--- 4255,4259 ----
return NULL;
st->st_pass = 1;
+ st->st_nested_scopes = NESTED_SCOPES_DEFAULT;
st->st_filename = NULL;
if ((st->st_stack = PyList_New(0)) == NULL)
***************
*** 4194,4197 ****
--- 4281,4322 ----
}
+ /* XXX this code is a placeholder for correct code.
+ from __future__ import name set language options */
+
+ static int
+ symtable_check_future(struct symtable *st, node *n)
+ {
+ int i;
+ node *name = CHILD(n, 1);
+
+ if (strcmp(STR(CHILD(name, 0)), "__future__") != 0)
+ return 0;
+ /* It is only legal to define __future__ features at the top
+ of a module. If the current scope is not the module level
+ or if there are any symbols defined, it is too late. */
+ if (st->st_cur->ste_symbols != st->st_global
+ || PyDict_Size(st->st_cur->ste_symbols) != 0) {
+ PyErr_SetString(PyExc_SyntaxError,
+ "imports from __future__ are only legal at the beginning of a module");
+ return -1;
+ }
+ for (i = 3; i < NCH(n); ++i) {
+ char *feature = STR(CHILD(CHILD(n, i), 0));
+ /* Do a linear search through the defined features,
+ assuming there aren't very many of them. */
+ if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
+ st->st_nested_scopes = 1;
+ } else {
+ PyErr_Format(PyExc_SyntaxError,
+ UNDEFINED_FUTURE_FEATURE, feature);
+ set_error_location(st->st_filename,
+ st->st_cur->ste_lineno);
+ st->st_errors++;
+ return -1;
+ }
+ }
+ return 1;
+ }
+
/* When the compiler exits a scope, it must should update the scope's
free variable information with the list of free variables in its
***************
*** 4323,4326 ****
--- 4448,4456 ----
}
+ /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
+ This reference is released when the scope is exited, via the DECREF
+ in symtable_exit_scope().
+ */
+
static int
symtable_exit_scope(struct symtable *st)
***************
*** 4328,4332 ****
int end;
! if (st->st_pass == 1)
symtable_update_free_vars(st);
Py_DECREF(st->st_cur);
--- 4458,4462 ----
int end;
! if (st->st_pass == 1 && st->st_nested_scopes)
symtable_update_free_vars(st);
Py_DECREF(st->st_cur);
***************
*** 4365,4368 ****
--- 4495,4519 ----
static int
+ symtable_lookup(struct symtable *st, char *name)
+ {
+ char buffer[MANGLE_LEN];
+ PyObject *v;
+ int flags;
+
+ if (mangle(st->st_private, name, buffer, sizeof(buffer)))
+ name = buffer;
+ v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
+ if (v == NULL) {
+ if (PyErr_Occurred())
+ return -1;
+ else
+ return 0;
+ }
+
+ flags = PyInt_AS_LONG(v);
+ return flags;
+ }
+
+ static int
symtable_add_def(struct symtable *st, char *name, int flag)
{
***************
*** 4486,4493 ****
break;
case exec_stmt: {
! st->st_cur->ste_optimized = 0;
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));
--- 4637,4646 ----
break;
case exec_stmt: {
! st->st_cur->ste_optimized |= OPT_EXEC;
symtable_node(st, CHILD(n, 1));
if (NCH(n) > 2)
symtable_node(st, CHILD(n, 3));
+ else
+ st->st_cur->ste_optimized |= OPT_BARE_EXEC;
if (NCH(n) > 4)
symtable_node(st, CHILD(n, 5));
***************
*** 4699,4704 ****
int i;
! for (i = 1; i < NCH(n); i += 2)
! symtable_add_def(st, STR(CHILD(n, i)), DEF_GLOBAL);
}
--- 4852,4859 ----
int i;
! for (i = 1; i < NCH(n); i += 2) {
! char *name = STR(CHILD(n, i));
! symtable_add_def(st, name, DEF_GLOBAL);
! }
}
***************
*** 4726,4733 ****
import_as_name: NAME [NAME NAME]
*/
-
if (STR(CHILD(n, 0))[0] == 'f') { /* from */
if (TYPE(CHILD(n, 3)) == STAR) {
! st->st_cur->ste_optimized = 0;
} else {
for (i = 3; i < NCH(n); i += 2) {
--- 4881,4888 ----
import_as_name: NAME [NAME NAME]
*/
if (STR(CHILD(n, 0))[0] == 'f') { /* from */
+ symtable_check_future(st, n);
if (TYPE(CHILD(n, 3)) == STAR) {
! st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
} else {
for (i = 3; i < NCH(n); i += 2) {
***************
*** 4741,4745 ****
}
}
! } else {
for (i = 1; i < NCH(n); i += 2) {
symtable_assign(st, CHILD(n, i), DEF_IMPORT);
--- 4896,4900 ----
}
}
! } else {
for (i = 1; i < NCH(n); i += 2) {
symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Index: symtable.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/symtable.c,v
retrieving revision 2.2
retrieving revision 2.3
diff -C2 -r2.2 -r2.3
*** symtable.c 2001/02/23 17:55:27 2.2
--- symtable.c 2001/02/27 04:23:34 2.3
***************
*** 44,48 ****
ste->ste_children = v;
! ste->ste_optimized = 1;
ste->ste_lineno = lineno;
switch (type) {
--- 44,48 ----
ste->ste_children = v;
! ste->ste_optimized = 0;
ste->ste_lineno = lineno;
switch (type) {