From e63abd631f3b30a3f499df866af8ba8987da6082 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 29 Aug 2009 15:46:50 -0500 Subject: [PATCH] add py.builtin.callable --HG-- branch : trunk --- py/__init__.py | 1 + py/builtin/builtin31.py | 4 ++++ py/builtin/testing/test_builtin.py | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/py/__init__.py b/py/__init__.py index ffcf0e2e6..cccc4b9ef 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -151,6 +151,7 @@ initpkg(__name__, 'builtin._totext' : ('./builtin/builtin31.py', '_totext'), 'builtin.builtins' : ('./builtin/builtin31.py', 'builtins'), 'builtin.execfile' : ('./builtin/builtin31.py', 'execfile'), + 'builtin.callable' : ('./builtin/builtin31.py', 'callable'), # gateways into remote contexts 'execnet.__doc__' : ('./execnet/__init__.py', '__doc__'), diff --git a/py/builtin/builtin31.py b/py/builtin/builtin31.py index 015ddb6c1..156b44dfe 100644 --- a/py/builtin/builtin31.py +++ b/py/builtin/builtin31.py @@ -26,10 +26,14 @@ if sys.version_info >= (3, 0): fp.close() exec_(source, globs, locs) + def callable(obj): + return hasattr(obj, "__call__") + else: _totext = unicode _basestring = basestring execfile = execfile + callable = callable import __builtin__ as builtins def print_(*args, **kwargs): diff --git a/py/builtin/testing/test_builtin.py b/py/builtin/testing/test_builtin.py index 05454792e..ac8b6f7aa 100644 --- a/py/builtin/testing/test_builtin.py +++ b/py/builtin/testing/test_builtin.py @@ -96,6 +96,15 @@ def test_execfile(tmpdir): py.builtin.execfile(str(test_file)) assert A.x == 3 +def test_callable(): + class A: pass + assert py.builtin.callable(test_callable) + assert py.builtin.callable(A) + assert py.builtin.callable(list) + assert py.builtin.callable(id) + assert not py.builtin.callable(4) + assert not py.builtin.callable("hi") + def test_totext(): py.builtin._totext("hello", "UTF-8")