Skip to content

Commit 2a74e48

Browse files
committed
Add vm.is_callable
1 parent 19be5c9 commit 2a74e48

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

vm/src/builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ fn builtin_bin(x: PyIntRef, _vm: &VirtualMachine) -> String {
6767

6868
// builtin_breakpoint
6969

70-
fn builtin_callable(obj: PyObjectRef, _vm: &VirtualMachine) -> bool {
71-
objtype::class_has_attr(&obj.class(), "__call__")
70+
fn builtin_callable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
71+
vm.is_callable(&obj)
7272
}
7373

7474
fn builtin_chr(i: u32, _vm: &VirtualMachine) -> String {

vm/src/vm.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,21 @@ impl VirtualMachine {
666666
crate::stdlib::json::de_pyobject(self, s)
667667
}
668668

669+
pub fn is_callable(&self, obj: &PyObjectRef) -> bool {
670+
match_class!(obj,
671+
PyFunction => true,
672+
PyMethod => true,
673+
PyBuiltinFunction => true,
674+
obj => {
675+
if let Some(dict) = &obj.dict {
676+
dict.contains_key("__call__", self)
677+
} else {
678+
false
679+
}
680+
},
681+
)
682+
}
683+
669684
pub fn _sub(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
670685
self.call_or_reflection(a, b, "__sub__", "__rsub__", |vm, a, b| {
671686
Err(vm.new_unsupported_operand_error(a, b, "-"))

0 commit comments

Comments
 (0)