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

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

#countcontent
1n/a"""Fixer for operator functions.
2n/a
3n/aoperator.isCallable(obj) -> hasattr(obj, '__call__')
4n/aoperator.sequenceIncludes(obj) -> operator.contains(obj)
5n/aoperator.isSequenceType(obj) -> isinstance(obj, collections.Sequence)
6n/aoperator.isMappingType(obj) -> isinstance(obj, collections.Mapping)
7n/aoperator.isNumberType(obj) -> isinstance(obj, numbers.Number)
8n/aoperator.repeat(obj, n) -> operator.mul(obj, n)
9n/aoperator.irepeat(obj, n) -> operator.imul(obj, n)
10n/a"""
11n/a
12n/aimport collections
13n/a
14n/a# Local imports
15n/afrom lib2to3 import fixer_base
16n/afrom lib2to3.fixer_util import Call, Name, String, touch_import
17n/a
18n/a
19n/adef invocation(s):
20n/a def dec(f):
21n/a f.invocation = s
22n/a return f
23n/a return dec
24n/a
25n/a
26n/aclass FixOperator(fixer_base.BaseFix):
27n/a BM_compatible = True
28n/a order = "pre"
29n/a
30n/a methods = """
31n/a method=('isCallable'|'sequenceIncludes'
32n/a |'isSequenceType'|'isMappingType'|'isNumberType'
33n/a |'repeat'|'irepeat')
34n/a """
35n/a obj = "'(' obj=any ')'"
36n/a PATTERN = """
37n/a power< module='operator'
38n/a trailer< '.' %(methods)s > trailer< %(obj)s > >
39n/a |
40n/a power< %(methods)s trailer< %(obj)s > >
41n/a """ % dict(methods=methods, obj=obj)
42n/a
43n/a def transform(self, node, results):
44n/a method = self._check_method(node, results)
45n/a if method is not None:
46n/a return method(node, results)
47n/a
48n/a @invocation("operator.contains(%s)")
49n/a def _sequenceIncludes(self, node, results):
50n/a return self._handle_rename(node, results, "contains")
51n/a
52n/a @invocation("hasattr(%s, '__call__')")
53n/a def _isCallable(self, node, results):
54n/a obj = results["obj"]
55n/a args = [obj.clone(), String(", "), String("'__call__'")]
56n/a return Call(Name("hasattr"), args, prefix=node.prefix)
57n/a
58n/a @invocation("operator.mul(%s)")
59n/a def _repeat(self, node, results):
60n/a return self._handle_rename(node, results, "mul")
61n/a
62n/a @invocation("operator.imul(%s)")
63n/a def _irepeat(self, node, results):
64n/a return self._handle_rename(node, results, "imul")
65n/a
66n/a @invocation("isinstance(%s, collections.Sequence)")
67n/a def _isSequenceType(self, node, results):
68n/a return self._handle_type2abc(node, results, "collections", "Sequence")
69n/a
70n/a @invocation("isinstance(%s, collections.Mapping)")
71n/a def _isMappingType(self, node, results):
72n/a return self._handle_type2abc(node, results, "collections", "Mapping")
73n/a
74n/a @invocation("isinstance(%s, numbers.Number)")
75n/a def _isNumberType(self, node, results):
76n/a return self._handle_type2abc(node, results, "numbers", "Number")
77n/a
78n/a def _handle_rename(self, node, results, name):
79n/a method = results["method"][0]
80n/a method.value = name
81n/a method.changed()
82n/a
83n/a def _handle_type2abc(self, node, results, module, abc):
84n/a touch_import(None, module, node)
85n/a obj = results["obj"]
86n/a args = [obj.clone(), String(", " + ".".join([module, abc]))]
87n/a return Call(Name("isinstance"), args, prefix=node.prefix)
88n/a
89n/a def _check_method(self, node, results):
90n/a method = getattr(self, "_" + results["method"][0].value)
91n/a if isinstance(method, collections.Callable):
92n/a if "module" in results:
93n/a return method
94n/a else:
95n/a sub = (str(results["obj"]),)
96n/a invocation_str = method.invocation % sub
97n/a self.warning(node, "You should use '%s' here." % invocation_str)
98n/a return None