mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Merge pull request #429 from adrian17/master
Fix callable(), add tests.
This commit is contained in:
3 files changed
+28
-2
No files matched your search
@@ -0,0 +1,26 @@
|
||||
assert not callable(1)
|
||||
def f(): pass
|
||||
# TODO uncomment when callable types get unified __call__ (or equivalent)
|
||||
#assert callable(f)
|
||||
#assert callable(len)
|
||||
#assert callable(lambda: 1)
|
||||
assert callable(int)
|
||||
|
||||
class C:
|
||||
def __init__(self):
|
||||
# must be defined on class
|
||||
self.__call__ = lambda self: 1
|
||||
def f(self): pass
|
||||
assert callable(C)
|
||||
assert not callable(C())
|
||||
#assert callable(C().f)
|
||||
|
||||
class C:
|
||||
def __call__(self): pass
|
||||
assert callable(C())
|
||||
class C1(C): pass
|
||||
assert callable(C1())
|
||||
class C:
|
||||
__call__ = 1
|
||||
# CPython returns true here, but fails when actually calling it
|
||||
assert callable(C())
|
||||
@@ -8,5 +8,6 @@ class X:
|
||||
a = X()
|
||||
b = ref(a)
|
||||
|
||||
assert callable(b)
|
||||
assert b() is a
|
||||
|
||||
+1
-2
@@ -101,8 +101,7 @@ fn builtin_bin(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
fn builtin_callable(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(obj, None)]);
|
||||
// TODO: is this a sufficiently thorough check?
|
||||
let is_callable = obj.has_attr("__call__");
|
||||
let is_callable = obj.typ().has_attr("__call__");
|
||||
Ok(vm.new_bool(is_callable))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user