paulb@31 | 1 | #!/usr/bin/env python |
paulb@31 | 2 | |
paulb@31 | 3 | """ |
paulb@47 | 4 | Fix name-related operations. The code in this module operates upon simplified |
paulb@47 | 5 | program node trees. |
paulb@31 | 6 | |
paulb@31 | 7 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@31 | 8 | |
paulb@31 | 9 | This software is free software; you can redistribute it and/or |
paulb@31 | 10 | modify it under the terms of the GNU General Public License as |
paulb@31 | 11 | published by the Free Software Foundation; either version 2 of |
paulb@31 | 12 | the License, or (at your option) any later version. |
paulb@31 | 13 | |
paulb@31 | 14 | This software is distributed in the hope that it will be useful, |
paulb@31 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@31 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@31 | 17 | GNU General Public License for more details. |
paulb@31 | 18 | |
paulb@31 | 19 | You should have received a copy of the GNU General Public |
paulb@31 | 20 | License along with this library; see the file LICENCE.txt |
paulb@31 | 21 | If not, write to the Free Software Foundation, Inc., |
paulb@31 | 22 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 23 | |
paulb@46 | 24 | -------- |
paulb@46 | 25 | |
paulb@57 | 26 | To use this module, the easiest approach is to use the fix function: |
paulb@57 | 27 | |
paulb@57 | 28 | fix(module) |
paulb@57 | 29 | |
paulb@57 | 30 | The more complicated approach involves instantiating a Fixer object: |
paulb@46 | 31 | |
paulb@46 | 32 | fixer = Fixer() |
paulb@46 | 33 | |
paulb@57 | 34 | Then, applying the fixer to an existing module: |
paulb@46 | 35 | |
paulb@57 | 36 | fixer.process(module) |
paulb@46 | 37 | |
paulb@57 | 38 | If a module containing built-in classes and functions exists, apply the fixer as |
paulb@57 | 39 | follows: |
paulb@46 | 40 | |
paulb@57 | 41 | fixer.process(module, builtins) |
paulb@31 | 42 | """ |
paulb@31 | 43 | |
paulb@31 | 44 | from simplified import * |
paulb@31 | 45 | |
paulb@31 | 46 | # Fixing of name-related operations. |
paulb@31 | 47 | |
paulb@31 | 48 | class Fixer(Visitor): |
paulb@31 | 49 | |
paulb@31 | 50 | """ |
paulb@47 | 51 | The name fixer which traverses the program nodes in a module, typically |
paulb@47 | 52 | depth-first, and maintains a record of name usage in the different |
paulb@47 | 53 | namespaces. As a consequence of various observations, some parts of the |
paulb@47 | 54 | program node tree are modified with different operations employed to those |
paulb@47 | 55 | originally defined. |
paulb@47 | 56 | |
paulb@47 | 57 | There are two kinds of subprograms in modules: functions/methods and |
paulb@47 | 58 | internal subprograms which support things like loops. The latter kind of |
paulb@47 | 59 | subprogram may acquire the locals from their callers and must therefore be |
paulb@47 | 60 | traversed with information from such callers. Thus, we choose the top-level |
paulb@47 | 61 | code and all functions/methods as roots for processing, following |
paulb@47 | 62 | invocations of internal subprograms in order to reach all subprograms that |
paulb@47 | 63 | are defined in each module. |
paulb@47 | 64 | |
paulb@47 | 65 | top-level |
paulb@47 | 66 | ... |
paulb@47 | 67 | invoke function |
paulb@47 | 68 | ... |
paulb@47 | 69 | invoke loop -> subprogram (internal) |
paulb@47 | 70 | ... |
paulb@47 | 71 | |
paulb@47 | 72 | subprogram (function) |
paulb@47 | 73 | ... |
paulb@47 | 74 | invoke loop -> subprogram (internal) |
paulb@47 | 75 | ... |
paulb@47 | 76 | |
paulb@47 | 77 | ... |
paulb@47 | 78 | |
paulb@47 | 79 | The above approach should guarantee that all subprograms are traversed and |
paulb@47 | 80 | that all name lookups are correctly categorised. |
paulb@31 | 81 | """ |
paulb@31 | 82 | |
paulb@31 | 83 | def __init__(self): |
paulb@45 | 84 | |
paulb@45 | 85 | "Initialise the name fixer." |
paulb@45 | 86 | |
paulb@31 | 87 | Visitor.__init__(self) |
paulb@31 | 88 | |
paulb@31 | 89 | # Satisfy visitor issues. |
paulb@31 | 90 | |
paulb@31 | 91 | self.visitor = self |
paulb@31 | 92 | |
paulb@57 | 93 | def process(self, module, builtins=None): |
paulb@45 | 94 | |
paulb@46 | 95 | """ |
paulb@57 | 96 | Process the given 'module' optionally using some 'builtins' to reference |
paulb@57 | 97 | built-in objects. |
paulb@46 | 98 | """ |
paulb@45 | 99 | |
paulb@47 | 100 | # The fixer maintains a list of transformed subprograms (added for each |
paulb@47 | 101 | # of the processing "roots" and also for each invoked internal |
paulb@47 | 102 | # subprogram), along with a list of current subprograms (used to avoid |
paulb@47 | 103 | # recursion issues) and a list of current namespaces (used to recall |
paulb@47 | 104 | # namespaces upon invoking internal subprograms). |
paulb@47 | 105 | |
paulb@45 | 106 | self.subprograms = [] |
paulb@45 | 107 | self.current_subprograms = [] |
paulb@45 | 108 | self.current_namespaces = [] |
paulb@45 | 109 | |
paulb@45 | 110 | # First, process the top-level code, finding out which names are |
paulb@45 | 111 | # defined at that level. |
paulb@45 | 112 | |
paulb@45 | 113 | self.global_namespace = None |
paulb@57 | 114 | self.module = module |
paulb@165 | 115 | self.builtins = builtins or module |
paulb@47 | 116 | |
paulb@57 | 117 | self.process_node(self.module) |
paulb@45 | 118 | |
paulb@45 | 119 | # Then, process all functions and methods, providing a global namespace. |
paulb@47 | 120 | # By setting a global namespace, we influence the resolution of names: |
paulb@47 | 121 | # those which are global to the top-level module (processed above) are |
paulb@47 | 122 | # considered as built-in names, whereas those which are global to a |
paulb@47 | 123 | # function or method are searched for in the global namespace. |
paulb@45 | 124 | |
paulb@45 | 125 | self.global_namespace = self.namespace |
paulb@45 | 126 | |
paulb@57 | 127 | for subprogram in self.module.simplifier.subprograms: |
paulb@45 | 128 | |
paulb@45 | 129 | # Internal subprograms are skipped here and processed specially via |
paulb@45 | 130 | # Invoke nodes. |
paulb@45 | 131 | |
paulb@112 | 132 | if not getattr(subprogram, "internal", 0): |
paulb@45 | 133 | self.subprograms.append(self.process_node(subprogram)) |
paulb@45 | 134 | |
paulb@47 | 135 | # Ultimately, we redefine the list of subprograms on the visitor. |
paulb@47 | 136 | |
paulb@57 | 137 | self.module.simplifier.subprograms = self.subprograms |
paulb@57 | 138 | return self.module |
paulb@31 | 139 | |
paulb@45 | 140 | def process_node(self, node, namespace=None): |
paulb@31 | 141 | |
paulb@31 | 142 | """ |
paulb@43 | 143 | Process a subprogram or module 'node', discovering from attributes on |
paulb@43 | 144 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@31 | 145 | """ |
paulb@31 | 146 | |
paulb@45 | 147 | # Do not process subprograms already being processed. |
paulb@45 | 148 | |
paulb@45 | 149 | if node in self.current_subprograms: |
paulb@45 | 150 | return None |
paulb@45 | 151 | |
paulb@31 | 152 | # Obtain a namespace either based on locals or on a structure. |
paulb@31 | 153 | |
paulb@45 | 154 | structure = structure=getattr(node, "structure", None) |
paulb@210 | 155 | |
paulb@210 | 156 | # If passed some namespace, use that as the current namespace. |
paulb@210 | 157 | |
paulb@210 | 158 | if namespace is not None: |
paulb@210 | 159 | self.namespace.merge_namespace(namespace) |
paulb@210 | 160 | else: |
paulb@210 | 161 | self.namespace = NameOrganiser(structure) |
paulb@45 | 162 | |
paulb@45 | 163 | # Record the current subprogram and namespace. |
paulb@45 | 164 | |
paulb@45 | 165 | self.current_subprograms.append(node) |
paulb@45 | 166 | self.current_namespaces.append(self.namespace) |
paulb@45 | 167 | |
paulb@169 | 168 | # NOTE: Avoid PEP 227 (nested scopes) whilst permitting references to a |
paulb@172 | 169 | # NOTE: subprogram within itself. Do not define the name of the function |
paulb@172 | 170 | # NOTE: within a method definition. |
paulb@169 | 171 | |
paulb@184 | 172 | if isinstance(node, Subprogram) and getattr(node, "name", None) is not None and not getattr(node, "is_method", 0): |
paulb@169 | 173 | self.namespace.store(node.name) |
paulb@169 | 174 | |
paulb@47 | 175 | # Register the names of parameters in the namespace. |
paulb@43 | 176 | |
paulb@43 | 177 | if hasattr(node, "params"): |
paulb@52 | 178 | new_params = [] |
paulb@43 | 179 | for param, default in node.params: |
paulb@52 | 180 | new_params.append((param, self.dispatch(default))) |
paulb@43 | 181 | self.namespace.store(param) |
paulb@52 | 182 | node.params = new_params |
paulb@45 | 183 | if getattr(node, "star", None): |
paulb@45 | 184 | param, default = node.star |
paulb@43 | 185 | self.namespace.store(param) |
paulb@52 | 186 | node.star = param, self.dispatch(default) |
paulb@45 | 187 | if getattr(node, "dstar", None): |
paulb@45 | 188 | param, default = node.dstar |
paulb@43 | 189 | self.namespace.store(param) |
paulb@52 | 190 | node.dstar = param, self.dispatch(default) |
paulb@43 | 191 | |
paulb@31 | 192 | # Add namespace details to any structure involved. |
paulb@31 | 193 | |
paulb@31 | 194 | if hasattr(node, "structure") and node.structure is not None: |
paulb@31 | 195 | |
paulb@31 | 196 | # Initialise bases where appropriate. |
paulb@31 | 197 | |
paulb@31 | 198 | if hasattr(node.structure, "bases"): |
paulb@31 | 199 | bases = [] |
paulb@31 | 200 | for base in node.structure.bases: |
paulb@31 | 201 | bases.append(self.dispatch(base)) |
paulb@31 | 202 | node.structure.bases = bases |
paulb@31 | 203 | |
paulb@31 | 204 | # Dispatch to the code itself. |
paulb@31 | 205 | |
paulb@31 | 206 | result = self.dispatch(node) |
paulb@47 | 207 | result.organiser = self.namespace |
paulb@45 | 208 | |
paulb@45 | 209 | # Restore the previous subprogram and namespace. |
paulb@45 | 210 | |
paulb@45 | 211 | self.current_namespaces.pop() |
paulb@45 | 212 | if self.current_namespaces: |
paulb@45 | 213 | self.namespace = self.current_namespaces[-1] |
paulb@45 | 214 | self.current_subprograms.pop() |
paulb@45 | 215 | |
paulb@31 | 216 | return result |
paulb@31 | 217 | |
paulb@31 | 218 | # Visitor methods. |
paulb@31 | 219 | |
paulb@31 | 220 | def default(self, node): |
paulb@31 | 221 | |
paulb@31 | 222 | """ |
paulb@31 | 223 | Process the given 'node', given that it does not have a specific |
paulb@31 | 224 | handler. |
paulb@31 | 225 | """ |
paulb@31 | 226 | |
paulb@98 | 227 | for attr in ("pos_args",): |
paulb@31 | 228 | value = getattr(node, attr, None) |
paulb@31 | 229 | if value is not None: |
paulb@31 | 230 | setattr(node, attr, self.dispatches(value)) |
paulb@98 | 231 | for attr in ("kw_args",): |
paulb@98 | 232 | value = getattr(node, attr, None) |
paulb@98 | 233 | if value is not None: |
paulb@98 | 234 | setattr(node, attr, self.dispatch_dict(value)) |
paulb@36 | 235 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@31 | 236 | value = getattr(node, attr, None) |
paulb@31 | 237 | if value is not None: |
paulb@31 | 238 | setattr(node, attr, self.dispatch(value)) |
paulb@36 | 239 | for attr in ("body", "else_", "handler", "finally_", "code", "choices"): |
paulb@31 | 240 | value = getattr(node, attr, None) |
paulb@31 | 241 | if value is not None: |
paulb@31 | 242 | setattr(node, attr, self.dispatches(value)) |
paulb@31 | 243 | return node |
paulb@31 | 244 | |
paulb@31 | 245 | def dispatch(self, node, *args): |
paulb@31 | 246 | return Visitor.dispatch(self, node, *args) |
paulb@31 | 247 | |
paulb@31 | 248 | def visitGlobal(self, global_): |
paulb@31 | 249 | for name in global_.names: |
paulb@31 | 250 | self.namespace.make_global(name) |
paulb@31 | 251 | return global_ |
paulb@31 | 252 | |
paulb@31 | 253 | def visitLoadName(self, loadname): |
paulb@45 | 254 | |
paulb@45 | 255 | "Transform the 'loadname' node to a specific, scope-sensitive node." |
paulb@45 | 256 | |
paulb@47 | 257 | scope = self.namespace.find_for_load(loadname.name) |
paulb@45 | 258 | |
paulb@45 | 259 | # For structure namespaces, load an attribute. |
paulb@45 | 260 | |
paulb@31 | 261 | if scope == "structure": |
paulb@63 | 262 | result = self.dispatch( |
paulb@79 | 263 | LoadAttr(loadname.original, loadname.defining, |
paulb@63 | 264 | expr=LoadRef(loadname.original, |
paulb@63 | 265 | ref=self.namespace.structure), |
paulb@63 | 266 | name=loadname.name, |
paulb@63 | 267 | nstype="structure") |
paulb@63 | 268 | ) |
paulb@45 | 269 | |
paulb@45 | 270 | # For global accesses (ie. those outside the local namespace)... |
paulb@45 | 271 | |
paulb@31 | 272 | elif scope == "global": |
paulb@45 | 273 | |
paulb@45 | 274 | # Where a distinct global namespace exists, examine it. |
paulb@45 | 275 | |
paulb@45 | 276 | if self.global_namespace is not None: |
paulb@47 | 277 | scope = self.global_namespace.find_for_load(loadname.name) |
paulb@45 | 278 | |
paulb@45 | 279 | # Where the name is outside the global namespace, it must be a |
paulb@45 | 280 | # built-in. |
paulb@45 | 281 | |
paulb@45 | 282 | if scope == "global": |
paulb@63 | 283 | result = self.dispatch( |
paulb@79 | 284 | LoadAttr(loadname.original, loadname.defining, |
paulb@63 | 285 | expr=LoadRef(loadname.original, |
paulb@63 | 286 | ref=self.builtins), |
paulb@63 | 287 | name=loadname.name, |
paulb@63 | 288 | nstype="module") |
paulb@63 | 289 | ) |
paulb@45 | 290 | |
paulb@45 | 291 | # Otherwise, it is within the global namespace and must be a |
paulb@45 | 292 | # global. |
paulb@45 | 293 | |
paulb@45 | 294 | else: |
paulb@63 | 295 | result = self.dispatch( |
paulb@79 | 296 | LoadAttr(loadname.original, loadname.defining, |
paulb@63 | 297 | expr=LoadRef(loadname.original, |
paulb@63 | 298 | ref=self.module), |
paulb@63 | 299 | name=loadname.name, |
paulb@63 | 300 | nstype="module") |
paulb@63 | 301 | ) |
paulb@45 | 302 | |
paulb@45 | 303 | # Where no global namespace exists, we are at the module level and |
paulb@45 | 304 | # must be accessing a built-in. |
paulb@45 | 305 | |
paulb@45 | 306 | else: |
paulb@63 | 307 | result = self.dispatch( |
paulb@79 | 308 | LoadAttr(loadname.original, loadname.defining, |
paulb@63 | 309 | expr=LoadRef(loadname.original, |
paulb@63 | 310 | ref=self.builtins), |
paulb@63 | 311 | name=loadname.name, |
paulb@63 | 312 | nstype="module") |
paulb@63 | 313 | ) |
paulb@45 | 314 | |
paulb@45 | 315 | # For local accesses... |
paulb@45 | 316 | |
paulb@31 | 317 | else: |
paulb@45 | 318 | |
paulb@45 | 319 | # Where a distinct global namespace exists, it must be a local. |
paulb@45 | 320 | |
paulb@45 | 321 | if self.global_namespace is not None: |
paulb@45 | 322 | result = loadname |
paulb@45 | 323 | |
paulb@45 | 324 | # Otherwise, we must be accessing a global (which is local at the |
paulb@45 | 325 | # module level). |
paulb@45 | 326 | |
paulb@45 | 327 | else: |
paulb@63 | 328 | result = self.dispatch( |
paulb@79 | 329 | LoadAttr(loadname.original, loadname.defining, |
paulb@63 | 330 | expr=LoadRef(loadname.original, |
paulb@63 | 331 | ref=self.module), |
paulb@63 | 332 | name=loadname.name, |
paulb@63 | 333 | nstype="module") |
paulb@63 | 334 | ) |
paulb@45 | 335 | |
paulb@31 | 336 | return result |
paulb@31 | 337 | |
paulb@31 | 338 | def visitStoreName(self, storename): |
paulb@45 | 339 | |
paulb@45 | 340 | "Transform the 'storename' node to a specific, scope-sensitive node." |
paulb@45 | 341 | |
paulb@47 | 342 | scope = self.namespace.find_for_store(storename.name) |
paulb@45 | 343 | |
paulb@45 | 344 | # For structure namespaces, store an attribute. |
paulb@45 | 345 | |
paulb@31 | 346 | if scope == "structure": |
paulb@164 | 347 | self.namespace.store(storename.name) |
paulb@164 | 348 | |
paulb@63 | 349 | return self.dispatch( |
paulb@79 | 350 | StoreAttr(storename.original, storename.defining, |
paulb@63 | 351 | lvalue=LoadRef(storename.original, |
paulb@63 | 352 | ref=self.namespace.structure), |
paulb@63 | 353 | name=storename.name, |
paulb@63 | 354 | expr=storename.expr, |
paulb@63 | 355 | nstype="structure") |
paulb@63 | 356 | ) |
paulb@45 | 357 | |
paulb@45 | 358 | # Where the name is outside the local namespace, disallow any built-in |
paulb@45 | 359 | # assignment and store the name globally. |
paulb@45 | 360 | |
paulb@31 | 361 | elif scope == "global": |
paulb@63 | 362 | return self.dispatch( |
paulb@79 | 363 | StoreAttr(storename.original, storename.defining, |
paulb@63 | 364 | lvalue=LoadRef(storename.original, |
paulb@63 | 365 | ref=self.module), |
paulb@63 | 366 | name=storename.name, |
paulb@63 | 367 | expr=storename.expr, |
paulb@63 | 368 | nstype="module") |
paulb@63 | 369 | ) |
paulb@45 | 370 | |
paulb@45 | 371 | # For local namespace accesses... |
paulb@45 | 372 | |
paulb@31 | 373 | else: |
paulb@31 | 374 | self.namespace.store(storename.name) |
paulb@45 | 375 | |
paulb@45 | 376 | # If a distinct global namespace exists, it must be a local access. |
paulb@45 | 377 | |
paulb@45 | 378 | if self.global_namespace is not None: |
paulb@45 | 379 | return storename |
paulb@45 | 380 | |
paulb@45 | 381 | # Otherwise, the name is being set at the module level and is |
paulb@45 | 382 | # considered global. |
paulb@45 | 383 | |
paulb@45 | 384 | else: |
paulb@63 | 385 | return self.dispatch( |
paulb@79 | 386 | StoreAttr(storename.original, storename.defining, |
paulb@63 | 387 | lvalue=LoadRef(storename.original, |
paulb@63 | 388 | ref=self.module), |
paulb@63 | 389 | name=storename.name, |
paulb@63 | 390 | expr=storename.expr, |
paulb@63 | 391 | nstype="module") |
paulb@63 | 392 | ) |
paulb@45 | 393 | |
paulb@57 | 394 | def visitInvokeFunction(self, invoke): |
paulb@57 | 395 | |
paulb@57 | 396 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@57 | 397 | |
paulb@57 | 398 | return self.default(invoke) |
paulb@57 | 399 | |
paulb@204 | 400 | def visitInvokeRef(self, invoke): |
paulb@45 | 401 | |
paulb@45 | 402 | "Transform the 'invoke' node, performing processing on subprograms." |
paulb@45 | 403 | |
paulb@45 | 404 | # The special case of internal subprogram invocation is addressed by |
paulb@45 | 405 | # propagating namespace information to the subprogram and processing it. |
paulb@45 | 406 | |
paulb@210 | 407 | if invoke.share_locals: |
paulb@210 | 408 | subprogram = self.process_node(invoke.ref, self.namespace) |
paulb@210 | 409 | else: |
paulb@210 | 410 | subprogram = self.process_node(invoke.ref) |
paulb@210 | 411 | |
paulb@57 | 412 | if subprogram is not None: |
paulb@57 | 413 | self.subprograms.append(subprogram) |
paulb@57 | 414 | return invoke |
paulb@31 | 415 | |
paulb@46 | 416 | class ScopeMismatch(Exception): |
paulb@46 | 417 | pass |
paulb@46 | 418 | |
paulb@34 | 419 | class NameOrganiser: |
paulb@32 | 420 | |
paulb@32 | 421 | """ |
paulb@32 | 422 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 423 | locals or the initialisation of a structure. |
paulb@32 | 424 | """ |
paulb@32 | 425 | |
paulb@32 | 426 | def __init__(self, structure=None): |
paulb@46 | 427 | |
paulb@46 | 428 | "Initialise the namespace with an optional 'structure'." |
paulb@46 | 429 | |
paulb@32 | 430 | self.structure = structure |
paulb@32 | 431 | if structure is not None: |
paulb@32 | 432 | self.local = "structure" |
paulb@32 | 433 | else: |
paulb@32 | 434 | self.local = "local" |
paulb@46 | 435 | |
paulb@46 | 436 | # Names may be self.local or "global". |
paulb@46 | 437 | |
paulb@32 | 438 | self.names = {} |
paulb@32 | 439 | |
paulb@32 | 440 | def make_global(self, name): |
paulb@46 | 441 | if not self.names.has_key(name): |
paulb@46 | 442 | self.names[name] = "global" |
paulb@46 | 443 | elif self.names[name] == self.local: |
paulb@46 | 444 | raise ScopeMismatch, "Name '%s' already considered as %s." % (name, self.local) |
paulb@32 | 445 | |
paulb@47 | 446 | def find_for_load(self, name): |
paulb@46 | 447 | return self.names.get(name, "global") |
paulb@32 | 448 | |
paulb@47 | 449 | def find_for_store(self, name): |
paulb@47 | 450 | return self.names.get(name, self.local) |
paulb@47 | 451 | |
paulb@45 | 452 | def store(self, name): |
paulb@46 | 453 | if self.names.get(name) != "global": |
paulb@46 | 454 | self.names[name] = self.local |
paulb@32 | 455 | else: |
paulb@46 | 456 | raise ScopeMismatch, "Name '%s' already considered as global." % name |
paulb@32 | 457 | |
paulb@46 | 458 | def merge(self, name, scope): |
paulb@46 | 459 | if self.names.get(name) in (None, scope): |
paulb@46 | 460 | self.names[name] = scope |
paulb@32 | 461 | else: |
paulb@46 | 462 | raise ScopeMismatch, "Name '%s' already considered as %s." % (name, self.names[name]) |
paulb@45 | 463 | |
paulb@45 | 464 | def merge_namespace(self, namespace): |
paulb@46 | 465 | self.merge_items(namespace.names.items()) |
paulb@45 | 466 | |
paulb@46 | 467 | def merge_items(self, items): |
paulb@46 | 468 | for name, scope in items: |
paulb@46 | 469 | self.merge(name, scope) |
paulb@45 | 470 | |
paulb@43 | 471 | def __repr__(self): |
paulb@43 | 472 | return repr(self.names) |
paulb@43 | 473 | |
paulb@57 | 474 | # Convenience functions. |
paulb@57 | 475 | |
paulb@57 | 476 | def fix(module, builtins=None): |
paulb@57 | 477 | |
paulb@57 | 478 | """ |
paulb@57 | 479 | Fix the names in the given 'module', also employing the optional 'builtins' |
paulb@57 | 480 | module, if specified. |
paulb@57 | 481 | """ |
paulb@57 | 482 | |
paulb@57 | 483 | fixer = Fixer() |
paulb@57 | 484 | if builtins is not None: |
paulb@57 | 485 | fixer.process(module, builtins) |
paulb@57 | 486 | else: |
paulb@57 | 487 | fixer.process(module) |
paulb@57 | 488 | |
paulb@31 | 489 | # vim: tabstop=4 expandtab shiftwidth=4 |