ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_renames.py

Python code coverage for Lib/lib2to3/fixes/fix_renames.py

#countcontent
1n/a"""Fix incompatible renames
2n/a
3n/aFixes:
4n/a * sys.maxint -> sys.maxsize
5n/a"""
6n/a# Author: Christian Heimes
7n/a# based on Collin Winter's fix_import
8n/a
9n/a# Local imports
10n/afrom .. import fixer_base
11n/afrom ..fixer_util import Name, attr_chain
12n/a
13n/aMAPPING = {"sys": {"maxint" : "maxsize"},
14n/a }
15n/aLOOKUP = {}
16n/a
17n/adef alternates(members):
18n/a return "(" + "|".join(map(repr, members)) + ")"
19n/a
20n/a
21n/adef build_pattern():
22n/a #bare = set()
23n/a for module, replace in list(MAPPING.items()):
24n/a for old_attr, new_attr in list(replace.items()):
25n/a LOOKUP[(module, old_attr)] = new_attr
26n/a #bare.add(module)
27n/a #bare.add(old_attr)
28n/a #yield """
29n/a # import_name< 'import' (module=%r
30n/a # | dotted_as_names< any* module=%r any* >) >
31n/a # """ % (module, module)
32n/a yield """
33n/a import_from< 'from' module_name=%r 'import'
34n/a ( attr_name=%r | import_as_name< attr_name=%r 'as' any >) >
35n/a """ % (module, old_attr, old_attr)
36n/a yield """
37n/a power< module_name=%r trailer< '.' attr_name=%r > any* >
38n/a """ % (module, old_attr)
39n/a #yield """bare_name=%s""" % alternates(bare)
40n/a
41n/a
42n/aclass FixRenames(fixer_base.BaseFix):
43n/a BM_compatible = True
44n/a PATTERN = "|".join(build_pattern())
45n/a
46n/a order = "pre" # Pre-order tree traversal
47n/a
48n/a # Don't match the node if it's within another match
49n/a def match(self, node):
50n/a match = super(FixRenames, self).match
51n/a results = match(node)
52n/a if results:
53n/a if any(match(obj) for obj in attr_chain(node, "parent")):
54n/a return False
55n/a return results
56n/a return False
57n/a
58n/a #def start_tree(self, tree, filename):
59n/a # super(FixRenames, self).start_tree(tree, filename)
60n/a # self.replace = {}
61n/a
62n/a def transform(self, node, results):
63n/a mod_name = results.get("module_name")
64n/a attr_name = results.get("attr_name")
65n/a #bare_name = results.get("bare_name")
66n/a #import_mod = results.get("module")
67n/a
68n/a if mod_name and attr_name:
69n/a new_attr = LOOKUP[(mod_name.value, attr_name.value)]
70n/a attr_name.replace(Name(new_attr, prefix=attr_name.prefix))