1 """Parser for future statements
5 from compiler
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",
19 "absolute_import", "with_statement", "print_function",
25 def visitModule(self
, node
):
28 if not self
.check_stmt(s
):
31 def check_stmt(self
, stmt
):
33 for name
, asname
in stmt
.names
:
34 if name
in self
.features
:
38 "future feature %s is not defined" % name
43 def get_features(self
):
44 """Return list of features enabled by future statements"""
45 return self
.found
.keys()
47 class BadFutureParser
:
48 """Check for invalid future statements"""
50 def visitFrom(self
, node
):
51 if hasattr(node
, 'valid_future'):
53 if node
.modname
!= "__future__":
55 raise SyntaxError, "invalid future statement " + repr(node
)
57 def find_futures(node
):
59 p2
= BadFutureParser()
62 return p1
.get_features()
64 if __name__
== "__main__":
66 from compiler
import parseFile
, walk
68 for file in sys
.argv
[1:]:
70 tree
= parseFile(file)