home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Fixer that changes xrange(...) into range(...).'''
- from import fixer_base
- from fixer_util import Name, Call, consuming_calls
- from import patcomp
-
- class FixXrange(fixer_base.BaseFix):
- PATTERN = "\n power<\n (name='range'|name='xrange') trailer< '(' args=any ')' >\n rest=any* >\n "
-
- def transform(self, node, results):
- name = results['name']
- if name.value == 'xrange':
- return self.transform_xrange(node, results)
- if name.value == 'range':
- return self.transform_range(node, results)
- raise ValueError(repr(name))
-
-
- def transform_xrange(self, node, results):
- name = results['name']
- name.replace(Name('range', prefix = name.get_prefix()))
-
-
- def transform_range(self, node, results):
- if not self.in_special_context(node):
- range_call = Call(Name('range'), [
- results['args'].clone()])
- list_call = Call(Name('list'), [
- range_call], prefix = node.get_prefix())
- for n in results['rest']:
- list_call.append_child(n)
-
- return list_call
- return node
-
- P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
- p1 = patcomp.compile_pattern(P1)
- P2 = "for_stmt< 'for' any 'in' node=any ':' any* >\n | comp_for< 'for' any 'in' node=any any* >\n | comparison< any 'in' node=any any*>\n "
- p2 = patcomp.compile_pattern(P2)
-
- def in_special_context(self, node):
- if node.parent is None:
- return False
- results = { }
- if node.parent.parent is not None and self.p1.match(node.parent.parent, results) and results['node'] is node:
- return results['func'].value in consuming_calls
- if self.p2.match(node.parent, results):
- pass
- return results['node'] is node
-
-
-