@@ -55,9 +55,6 @@ int Py_OptimizeFlag = 0;
5555#define ILLEGAL_DYNAMIC_SCOPE \
5656"%.100s: exec or 'import *' makes names ambiguous in nested scope"
5757
58- #define UNDEFINED_FUTURE_FEATURE \
59- "future feature %.100s is not defined"
60-
6158#define GLOBAL_AFTER_ASSIGN \
6259"name '%.400s' is assigned to before global declaration"
6360
@@ -368,6 +365,7 @@ struct compiling {
368365 int c_nested ; /* Is block nested funcdef or lamdef? */
369366 int c_closure ; /* Is nested w/freevars? */
370367 struct symtable * c_symtable ; /* pointer to module symbol table */
368+ PyFutureFeatures * c_future ; /* pointer to module's __future__ */
371369};
372370
373371int is_free (int v )
@@ -3864,7 +3862,8 @@ jcompile(node *n, char *filename, struct compiling *base)
38643862 sc .c_nested = 1 ;
38653863 } else {
38663864 sc .c_private = NULL ;
3867- if (symtable_build (& sc , n ) < 0 ) {
3865+ sc .c_future = PyNode_Future (n , filename );
3866+ if (sc .c_future == NULL || symtable_build (& sc , n ) < 0 ) {
38683867 com_free (& sc );
38693868 return NULL ;
38703869 }
@@ -3996,6 +3995,8 @@ symtable_build(struct compiling *c, node *n)
39963995{
39973996 if ((c -> c_symtable = symtable_init ()) == NULL )
39983997 return -1 ;
3998+ if (c -> c_future -> ff_nested_scopes )
3999+ c -> c_symtable -> st_nested_scopes = 1 ;
39994000 c -> c_symtable -> st_filename = c -> c_filename ;
40004001 symtable_enter_scope (c -> c_symtable , TOP , TYPE (n ), n -> n_lineno );
40014002 if (c -> c_symtable -> st_errors > 0 )
@@ -4280,44 +4281,6 @@ PySymtable_Free(struct symtable *st)
42804281 PyMem_Free ((void * )st );
42814282}
42824283
4283- /* XXX this code is a placeholder for correct code.
4284- from __future__ import name set language options */
4285-
4286- static int
4287- symtable_check_future (struct symtable * st , node * n )
4288- {
4289- int i ;
4290- node * name = CHILD (n , 1 );
4291-
4292- if (strcmp (STR (CHILD (name , 0 )), "__future__" ) != 0 )
4293- return 0 ;
4294- /* It is only legal to define __future__ features at the top
4295- of a module. If the current scope is not the module level
4296- or if there are any symbols defined, it is too late. */
4297- if (st -> st_cur -> ste_symbols != st -> st_global
4298- || PyDict_Size (st -> st_cur -> ste_symbols ) != 0 ) {
4299- PyErr_SetString (PyExc_SyntaxError ,
4300- "imports from __future__ are only legal at the beginning of a module" );
4301- return -1 ;
4302- }
4303- for (i = 3 ; i < NCH (n ); ++ i ) {
4304- char * feature = STR (CHILD (CHILD (n , i ), 0 ));
4305- /* Do a linear search through the defined features,
4306- assuming there aren't very many of them. */
4307- if (strcmp (feature , FUTURE_NESTED_SCOPES ) == 0 ) {
4308- st -> st_nested_scopes = 1 ;
4309- } else {
4310- PyErr_Format (PyExc_SyntaxError ,
4311- UNDEFINED_FUTURE_FEATURE , feature );
4312- set_error_location (st -> st_filename ,
4313- st -> st_cur -> ste_lineno );
4314- st -> st_errors ++ ;
4315- return -1 ;
4316- }
4317- }
4318- return 1 ;
4319- }
4320-
43214284/* When the compiler exits a scope, it must should update the scope's
43224285 free variable information with the list of free variables in its
43234286 children.
@@ -4910,7 +4873,6 @@ symtable_import(struct symtable *st, node *n)
49104873 import_as_name: NAME [NAME NAME]
49114874 */
49124875 if (STR (CHILD (n , 0 ))[0 ] == 'f' ) { /* from */
4913- symtable_check_future (st , n );
49144876 if (TYPE (CHILD (n , 3 )) == STAR ) {
49154877 st -> st_cur -> ste_optimized |= OPT_IMPORT_STAR ;
49164878 } else {
0 commit comments