PageRenderTime 344ms CodeModel.GetById 141ms app.highlight 2ms RepoModel.GetById 200ms app.codeStats 0ms

/Lib/lib2to3/fixes/fix_callable.py

http://unladen-swallow.googlecode.com/
Python | 31 lines | 26 code | 1 blank | 4 comment | 0 complexity | 37990663703ff5ea2fabb3095a9ad189 MD5 | raw file
 1# Copyright 2007 Google, Inc. All Rights Reserved.
 2# Licensed to PSF under a Contributor Agreement.
 3
 4"""Fixer for callable().
 5
 6This converts callable(obj) into hasattr(obj, '__call__')."""
 7
 8# Local imports
 9from .. import pytree
10from .. import fixer_base
11from ..fixer_util import Call, Name, String
12
13class FixCallable(fixer_base.BaseFix):
14
15    # Ignore callable(*args) or use of keywords.
16    # Either could be a hint that the builtin callable() is not being used.
17    PATTERN = """
18    power< 'callable'
19           trailer< lpar='('
20                    ( not(arglist | argument<any '=' any>) func=any
21                      | func=arglist<(not argument<any '=' any>) any ','> )
22                    rpar=')' >
23           after=any*
24    >
25    """
26
27    def transform(self, node, results):
28        func = results["func"]
29
30        args = [func.clone(), String(', '), String("'__call__'")]
31        return Call(Name("hasattr"), args, prefix=node.get_prefix())