1.1 --- a/annotate.py Sun Jul 30 02:00:33 2006 +0200
1.2 +++ b/annotate.py Sun Jul 30 16:15:57 2006 +0200
1.3 @@ -93,6 +93,28 @@
1.4 def __eq__(self, other):
1.5 return hasattr(other, "type") and other.type == self.type or other == self.type
1.6
1.7 +def find_methods(structure, name):
1.8 +
1.9 + """
1.10 + Find for the given 'structure' all methods for the given 'name', visiting
1.11 + base classes where appropriate and returning the methods in order of
1.12 + descending precedence for all possible base classes.
1.13 + """
1.14 +
1.15 + try:
1.16 + return structure.namespace.load(name)
1.17 + except KeyError:
1.18 + methods = []
1.19 + if hasattr(structure, "base_refs"):
1.20 + for base_refs in structure.base_refs:
1.21 + for base_ref in base_refs:
1.22 + l = find_methods(base_ref, name)
1.23 + if l:
1.24 + for method in l:
1.25 + if method not in methods:
1.26 + methods.append(method)
1.27 + return methods
1.28 +
1.29 # Annotation.
1.30
1.31 class Annotator(Visitor):
1.32 @@ -115,12 +137,13 @@
1.33
1.34 self.visitor = self
1.35
1.36 - def process(self, node, locals=None, globals=None):
1.37 + def process(self, node, locals=None, globals=None, builtins=None):
1.38
1.39 """
1.40 - Process a subprogram or module 'node', indicating any initial 'locals'
1.41 - and 'globals' if either are defined. Return an annotated subprogram or
1.42 - module. Note that this method may mutate nodes in the original program.
1.43 + Process a subprogram or module 'node', indicating any initial 'locals',
1.44 + 'globals' and 'builtins' if any are defined. Return an annotated
1.45 + subprogram or module. Note that this method may mutate nodes in the
1.46 + original program.
1.47 """
1.48
1.49 # Obtain a namespace either based on locals or on a structure.
1.50 @@ -132,6 +155,7 @@
1.51 # Determine the global namespace.
1.52
1.53 self.global_namespace = globals or self.namespace # NOTE: Improve this.
1.54 + self.builtins_namespace = builtins or self.namespace # NOTE: Improve this.
1.55 node.namespace = self.namespace
1.56
1.57 # Remember return values.
1.58 @@ -203,7 +227,10 @@
1.59 return storename
1.60
1.61 def visitLoadGlobal(self, loadglobal):
1.62 - self.types = self.global_namespace.load(loadglobal.name)
1.63 + try:
1.64 + self.types = self.global_namespace.load(loadglobal.name)
1.65 + except KeyError:
1.66 + self.types = self.builtins_namespace.load(loadglobal.name)
1.67 self.annotate(loadglobal)
1.68 return loadglobal
1.69
2.1 --- a/test.py Sun Jul 30 02:00:33 2006 +0200
2.2 +++ b/test.py Sun Jul 30 16:15:57 2006 +0200
2.3 @@ -1,11 +1,17 @@
2.4 -import simplify, compiler, sys
2.5 +import simplify, compiler, sys, os
2.6 import fixnames
2.7 import annotate
2.8
2.9 -v = simplify.Simplifier()
2.10 -f = fixnames.Fixer()
2.11 a = annotate.Annotator()
2.12 +b = compiler.parseFile(os.path.join("lib", "builtins.py"))
2.13 m = compiler.parseFile(sys.argv[1])
2.14 -v.process(m)
2.15 -f.process_all(v)
2.16 -r = v.result
2.17 +builtins_simplifier = simplify.Simplifier()
2.18 +builtins_simplifier.process(b)
2.19 +module_simplifier = simplify.Simplifier()
2.20 +module_simplifier.process(m)
2.21 +builtins_fixer = fixnames.Fixer()
2.22 +builtins_fixer.process_all(builtins_simplifier)
2.23 +module_fixer = fixnames.Fixer()
2.24 +module_fixer.process_all(module_simplifier)
2.25 +rb = builtins_simplifier.result
2.26 +r = module_simplifier.result