Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29906ee

Browse files
committed
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.
1 parent 2a5130e commit 29906ee

5 files changed

Lines changed: 281 additions & 116 deletions

File tree

Include/compile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
extern "C" {
88
#endif
99

10+
#define NESTED_SCOPES_DEFAULT 0
11+
#define FUTURE_NESTED_SCOPES "nested_scopes"
12+
1013
/* Bytecode object */
1114
typedef struct {
1215
PyObject_HEAD

Include/symtable.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct _symtable_entry;
2020

2121
struct symtable {
2222
int st_pass; /* pass == 1 or 2 */
23+
int st_nested_scopes; /* true if nested scopes are enabled */
2324
char *st_filename; /* name of file being compiled */
2425
struct _symtable_entry *st_cur; /* current symbol table entry */
2526
PyObject *st_symbols; /* dictionary of symbol table entries */
@@ -40,7 +41,7 @@ typedef struct _symtable_entry {
4041
PyObject *ste_children; /* list of child ids */
4142
int ste_type; /* module, class, or function */
4243
int ste_lineno; /* first line of scope */
43-
int ste_optimized; /* true if namespace is optimized */
44+
int ste_optimized; /* true if namespace can't be optimized */
4445
int ste_nested; /* true if scope is nested */
4546
int ste_child_free; /* true if a child scope has free variables,
4647
including free refs to globals */
@@ -84,6 +85,10 @@ DL_IMPORT(void) PySymtable_Free(struct symtable *);
8485
#define FREE 4
8586
#define CELL 5
8687

88+
#define OPT_IMPORT_STAR 1
89+
#define OPT_EXEC 2
90+
#define OPT_BARE_EXEC 4
91+
8792
#ifdef __cplusplus
8893
}
8994
#endif

Lib/test/test_scope.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import nested_scopes
2+
13
from test.test_support import verify, TestFailed, check_syntax
24

35
print "1. simple nesting"

0 commit comments

Comments
 (0)