[Python-checkins] r55483 - in sandbox/trunk/2to3: fixes/fix_callable.py tests/test_fixers.py
neal.norwitz
python-checkins at python.org
Mon May 21 08:07:31 CEST 2007
Author: neal.norwitz
Date: Mon May 21 08:07:29 2007
New Revision: 55483
Added:
sandbox/trunk/2to3/fixes/fix_callable.py (contents, props changed)
Modified:
sandbox/trunk/2to3/tests/test_fixers.py
Log:
A simplistic fixer for callable.
Added: sandbox/trunk/2to3/fixes/fix_callable.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_callable.py Mon May 21 08:07:29 2007
@@ -0,0 +1,28 @@
+# Copyright 2007 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer for callable().
+
+This converts callable(obj) into hasattr(obj, '__call__')."""
+
+# Local imports
+import pytree
+from fixes import basefix
+from fixes.util import Call, Name, String
+
+class FixCallable(basefix.BaseFix):
+
+ # XXX(nnorwitz): how should this code be handled: callable(*args)
+ PATTERN = """
+ power< 'callable' trailer< '(' func=any ')' > >
+ """
+
+ def transform(self, node):
+ results = self.match(node)
+ func = results["func"]
+
+ args = [func.clone(), String(', '), String("'__call__'")]
+ new = Call(Name("hasattr"), args)
+ new.set_prefix(node.get_prefix())
+ return new
+
Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Mon May 21 08:07:29 2007
@@ -1783,6 +1783,14 @@
a = """R'''x'''"""
self.check(b, a)
+class Test_callable(FixerTestCase):
+ fixer = "callable"
+
+ def test_callable_call(self):
+ b = """callable(x)"""
+ a = """hasattr(x, '__call__')"""
+ self.check(b, a)
+
if __name__ == "__main__":
import __main__
More information about the Python-checkins
mailing list