~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/lib2to3/fixes/fix_operator.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Fixer for operator.{isCallable,sequenceIncludes}
 
2
 
 
3
operator.isCallable(obj) -> hasattr(obj, '__call__')
 
4
operator.sequenceIncludes(obj) -> operator.contains(obj)
 
5
"""
 
6
 
 
7
# Local imports
 
8
from .. import fixer_base
 
9
from ..fixer_util import Call, Name, String
 
10
 
 
11
class FixOperator(fixer_base.BaseFix):
 
12
 
 
13
    methods = "method=('isCallable'|'sequenceIncludes')"
 
14
    func = "'(' func=any ')'"
 
15
    PATTERN = """
 
16
              power< module='operator'
 
17
                trailer< '.' {methods} > trailer< {func} > >
 
18
              |
 
19
              power< {methods} trailer< {func} > >
 
20
              """.format(methods=methods, func=func)
 
21
 
 
22
    def transform(self, node, results):
 
23
        method = results["method"][0]
 
24
 
 
25
        if method.value == u"sequenceIncludes":
 
26
            if "module" not in results:
 
27
                # operator may not be in scope, so we can't make a change.
 
28
                self.warning(node, "You should use operator.contains here.")
 
29
            else:
 
30
                method.value = u"contains"
 
31
                method.changed()
 
32
        elif method.value == u"isCallable":
 
33
            if "module" not in results:
 
34
                self.warning(node,
 
35
                             "You should use hasattr(%s, '__call__') here." %
 
36
                             results["func"].value)
 
37
            else:
 
38
                func = results["func"]
 
39
                args = [func.clone(), String(u", "), String(u"'__call__'")]
 
40
                return Call(Name(u"hasattr"), args, prefix=node.prefix)