summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/fixes/fix_callable.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-12-29 00:06:20 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-12-29 00:06:20 (GMT)
commitffa94b0f022a697dd491a0c31acd2e3b2629c048 (patch)
treed3ced4761609693558e548230f572274958d9662 /Lib/lib2to3/fixes/fix_callable.py
parent1e28a27f845a59513ad3bebbd6ae19f399071bbd (diff)
downloadcpython-ffa94b0f022a697dd491a0c31acd2e3b2629c048.zip
cpython-ffa94b0f022a697dd491a0c31acd2e3b2629c048.tar.gz
cpython-ffa94b0f022a697dd491a0c31acd2e3b2629c048.tar.bz2
Merged revisions 77102 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ................ r77102 | benjamin.peterson | 2009-12-28 17:50:41 -0600 (Mon, 28 Dec 2009) | 50 lines Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line handle unencodable diffs gracefully #5093 ........ r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line fix emacs header ........ r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines replace callable(x) with isinstance(x, collections.Callable) #7006 This is a more accurate translation than hasattr(x, '__call__') which failed in the case that somebody had put __call__ in the instance dictionary. Patch mostly by Joe Amenta. ........ r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines deuglify imports ........ r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line remove unused flag ........ r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines clean up imports and whitespace ........ r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line *** empty log message *** ........ r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line revert unintended change ........ r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line revert unintended changes ........ r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line normalize whitespace ........ ................
Diffstat (limited to 'Lib/lib2to3/fixes/fix_callable.py')
-rw-r--r--Lib/lib2to3/fixes/fix_callable.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/Lib/lib2to3/fixes/fix_callable.py b/Lib/lib2to3/fixes/fix_callable.py
index 831b913..ed1cb37 100644
--- a/Lib/lib2to3/fixes/fix_callable.py
+++ b/Lib/lib2to3/fixes/fix_callable.py
@@ -3,12 +3,12 @@
"""Fixer for callable().
-This converts callable(obj) into hasattr(obj, '__call__')."""
+This converts callable(obj) into isinstance(obj, collections.Callable), adding a
+collections import if needed."""
# Local imports
-from .. import pytree
-from .. import fixer_base
-from ..fixer_util import Call, Name, String
+from lib2to3 import fixer_base
+from lib2to3.fixer_util import Call, Name, String, Attr, touch_import
class FixCallable(fixer_base.BaseFix):
@@ -25,7 +25,10 @@ class FixCallable(fixer_base.BaseFix):
"""
def transform(self, node, results):
- func = results["func"]
+ func = results['func']
- args = [func.clone(), String(', '), String("'__call__'")]
- return Call(Name("hasattr"), args, prefix=node.prefix)
+ touch_import(None, 'collections', node=node)
+
+ args = [func.clone(), String(', ')]
+ args.extend(Attr(Name('collections'), Name('Callable')))
+ return Call(Name('isinstance'), args, prefix=node.prefix)