Commit e3334e82 authored by Jeremy Hylton's avatar Jeremy Hylton

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.
parent 0934bae8
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
extern "C" { extern "C" {
#endif #endif
#define NESTED_SCOPES_DEFAULT 0
#define FUTURE_NESTED_SCOPES "nested_scopes"
/* Bytecode object */ /* Bytecode object */
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
......
...@@ -20,6 +20,7 @@ struct _symtable_entry; ...@@ -20,6 +20,7 @@ struct _symtable_entry;
struct symtable { struct symtable {
int st_pass; /* pass == 1 or 2 */ int st_pass; /* pass == 1 or 2 */
int st_nested_scopes; /* true if nested scopes are enabled */
char *st_filename; /* name of file being compiled */ char *st_filename; /* name of file being compiled */
struct _symtable_entry *st_cur; /* current symbol table entry */ struct _symtable_entry *st_cur; /* current symbol table entry */
PyObject *st_symbols; /* dictionary of symbol table entries */ PyObject *st_symbols; /* dictionary of symbol table entries */
...@@ -40,7 +41,7 @@ typedef struct _symtable_entry { ...@@ -40,7 +41,7 @@ typedef struct _symtable_entry {
PyObject *ste_children; /* list of child ids */ PyObject *ste_children; /* list of child ids */
int ste_type; /* module, class, or function */ int ste_type; /* module, class, or function */
int ste_lineno; /* first line of scope */ int ste_lineno; /* first line of scope */
int ste_optimized; /* true if namespace is optimized */ int ste_optimized; /* true if namespace can't be optimized */
int ste_nested; /* true if scope is nested */ int ste_nested; /* true if scope is nested */
int ste_child_free; /* true if a child scope has free variables, int ste_child_free; /* true if a child scope has free variables,
including free refs to globals */ including free refs to globals */
...@@ -84,6 +85,10 @@ DL_IMPORT(void) PySymtable_Free(struct symtable *); ...@@ -84,6 +85,10 @@ DL_IMPORT(void) PySymtable_Free(struct symtable *);
#define FREE 4 #define FREE 4
#define CELL 5 #define CELL 5
#define OPT_IMPORT_STAR 1
#define OPT_EXEC 2
#define OPT_BARE_EXEC 4
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
from __future__ import nested_scopes
from test.test_support import verify, TestFailed, check_syntax from test.test_support import verify, TestFailed, check_syntax
print "1. simple nesting" print "1. simple nesting"
......
This diff is collapsed.
...@@ -43,7 +43,7 @@ PySymtableEntry_New(struct symtable *st, char *name, int type, int lineno) ...@@ -43,7 +43,7 @@ PySymtableEntry_New(struct symtable *st, char *name, int type, int lineno)
goto fail; goto fail;
ste->ste_children = v; ste->ste_children = v;
ste->ste_optimized = 1; ste->ste_optimized = 0;
ste->ste_lineno = lineno; ste->ste_lineno = lineno;
switch (type) { switch (type) {
case funcdef: case funcdef:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment