1.1 --- a/fixnames.py Sat Sep 30 18:54:06 2006 +0200
1.2 +++ b/fixnames.py Tue Oct 03 00:50:03 2006 +0200
1.3 @@ -23,19 +23,22 @@
1.4
1.5 --------
1.6
1.7 -To use this module, first instantiate a Fixer object:
1.8 +To use this module, the easiest approach is to use the fix function:
1.9 +
1.10 +fix(module)
1.11 +
1.12 +The more complicated approach involves instantiating a Fixer object:
1.13
1.14 fixer = Fixer()
1.15
1.16 -Then, apply the fixer to an existing Simplifier object:
1.17 +Then, applying the fixer to an existing module:
1.18
1.19 -simplifier = ...
1.20 -fixer.process(simplifier)
1.21 +fixer.process(module)
1.22
1.23 -If an existing simplifier has been used to process a module containing built-in
1.24 -classes and functions, apply the fixer as follows:
1.25 +If a module containing built-in classes and functions exists, apply the fixer as
1.26 +follows:
1.27
1.28 -fixer.process(simplifier, builtins_simplifier)
1.29 +fixer.process(module, builtins)
1.30 """
1.31
1.32 from simplified import *
1.33 @@ -87,11 +90,11 @@
1.34
1.35 self.visitor = self
1.36
1.37 - def process(self, visitor, builtins_visitor=None):
1.38 + def process(self, module, builtins=None):
1.39
1.40 """
1.41 - Process the resources of the given 'visitor' optionally using a
1.42 - 'builtins_visitor' to reference built-in objects.
1.43 + Process the given 'module' optionally using some 'builtins' to reference
1.44 + built-in objects.
1.45 """
1.46
1.47 # The fixer maintains a list of transformed subprograms (added for each
1.48 @@ -108,14 +111,10 @@
1.49 # defined at that level.
1.50
1.51 self.global_namespace = None
1.52 - self.module = visitor.result
1.53 + self.module = module
1.54 + self.builtins = builtins
1.55
1.56 - if builtins_visitor is not None:
1.57 - self.builtins_module = builtins_visitor.result
1.58 - else:
1.59 - self.builtins_module = None
1.60 -
1.61 - self.process_node(visitor.result)
1.62 + self.process_node(self.module)
1.63
1.64 # Then, process all functions and methods, providing a global namespace.
1.65 # By setting a global namespace, we influence the resolution of names:
1.66 @@ -125,7 +124,7 @@
1.67
1.68 self.global_namespace = self.namespace
1.69
1.70 - for subprogram in visitor.subprograms:
1.71 + for subprogram in self.module.simplifier.subprograms:
1.72
1.73 # Internal subprograms are skipped here and processed specially via
1.74 # Invoke nodes.
1.75 @@ -135,8 +134,8 @@
1.76
1.77 # Ultimately, we redefine the list of subprograms on the visitor.
1.78
1.79 - visitor.subprograms = self.subprograms
1.80 - return visitor
1.81 + self.module.simplifier.subprograms = self.subprograms
1.82 + return self.module
1.83
1.84 def process_node(self, node, namespace=None):
1.85
1.86 @@ -263,7 +262,7 @@
1.87 # built-in.
1.88
1.89 if scope == "global":
1.90 - result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins_module), name=loadname.name, nstype="module"))
1.91 + result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins), name=loadname.name, nstype="module"))
1.92
1.93 # Otherwise, it is within the global namespace and must be a
1.94 # global.
1.95 @@ -275,7 +274,7 @@
1.96 # must be accessing a built-in.
1.97
1.98 else:
1.99 - result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins_module), name=loadname.name, nstype="module"))
1.100 + result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.builtins), name=loadname.name, nstype="module"))
1.101
1.102 # For local accesses...
1.103
1.104 @@ -327,20 +326,23 @@
1.105 else:
1.106 return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.module), name=storename.name, expr=storename.expr, nstype="module"))
1.107
1.108 - def visitInvoke(self, invoke):
1.109 + def visitInvokeFunction(self, invoke):
1.110 +
1.111 + "Transform the 'invoke' node, performing processing on subprograms."
1.112 +
1.113 + return self.default(invoke)
1.114 +
1.115 + def visitInvokeBlock(self, invoke):
1.116
1.117 "Transform the 'invoke' node, performing processing on subprograms."
1.118
1.119 # The special case of internal subprogram invocation is addressed by
1.120 # propagating namespace information to the subprogram and processing it.
1.121
1.122 - if getattr(invoke, "same_frame", 0):
1.123 - subprogram = self.process_node(invoke.expr.ref, self.namespace)
1.124 - if subprogram is not None:
1.125 - self.subprograms.append(subprogram)
1.126 - return invoke
1.127 - else:
1.128 - return self.default(invoke)
1.129 + subprogram = self.process_node(invoke.expr.ref, self.namespace)
1.130 + if subprogram is not None:
1.131 + self.subprograms.append(subprogram)
1.132 + return invoke
1.133
1.134 class ScopeMismatch(Exception):
1.135 pass
1.136 @@ -400,4 +402,19 @@
1.137 def __repr__(self):
1.138 return repr(self.names)
1.139
1.140 +# Convenience functions.
1.141 +
1.142 +def fix(module, builtins=None):
1.143 +
1.144 + """
1.145 + Fix the names in the given 'module', also employing the optional 'builtins'
1.146 + module, if specified.
1.147 + """
1.148 +
1.149 + fixer = Fixer()
1.150 + if builtins is not None:
1.151 + fixer.process(module, builtins)
1.152 + else:
1.153 + fixer.process(module)
1.154 +
1.155 # vim: tabstop=4 expandtab shiftwidth=4