1
"""Parser for future statements
5
from pypy.interpreter.stablecompiler import ast, walk
8
"""Return true if statement is a well-formed future statement"""
9
if not isinstance(stmt, ast.From):
11
if stmt.modname == "__future__":
18
features = ("nested_scopes", "generators", "division")
23
def visitModule(self, node):
26
if not self.check_stmt(s):
29
def check_stmt(self, stmt):
31
for name, asname in stmt.names:
32
if name in self.features:
36
"future statement does not support import *",
37
( stmt.filename, stmt.lineno, 0, "" ) )
40
"future feature %s is not defined" % name,
41
( stmt.filename, stmt.lineno, 0, "" ) )
46
def get_features(self):
47
"""Return list of features enabled by future statements"""
48
return self.found.keys()
50
class BadFutureParser:
51
"""Check for invalid future statements
52
Those not marked valid are appearing after other statements
55
def visitFrom(self, node):
56
if hasattr(node, 'valid_future'):
58
if node.modname != "__future__":
60
raise SyntaxError( "from __future__ imports must occur at the beginning of the file",
61
( node.filename, node.lineno, 0, "" ) )
63
def find_futures(node):
65
p2 = BadFutureParser()
68
return p1.get_features()
70
if __name__ == "__main__":
72
from pypy.interpreter.stablecompiler import parseFile, walk
74
for file in sys.argv[1:]:
76
tree = parseFile(file)