Add vm.is_callable

This commit is contained in:
coolreader18
2019-04-19 10:10:56 -05:00
parent 19be5c9cf5
commit 2a74e48d98
2 changed files with 17 additions and 2 deletions

View File

@@ -67,8 +67,8 @@ fn builtin_bin(x: PyIntRef, _vm: &VirtualMachine) -> String {
// builtin_breakpoint
fn builtin_callable(obj: PyObjectRef, _vm: &VirtualMachine) -> bool {
objtype::class_has_attr(&obj.class(), "__call__")
fn builtin_callable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
vm.is_callable(&obj)
}
fn builtin_chr(i: u32, _vm: &VirtualMachine) -> String {

View File

@@ -666,6 +666,21 @@ impl VirtualMachine {
crate::stdlib::json::de_pyobject(self, s)
}
pub fn is_callable(&self, obj: &PyObjectRef) -> bool {
match_class!(obj,
PyFunction => true,
PyMethod => true,
PyBuiltinFunction => true,
obj => {
if let Some(dict) = &obj.dict {
dict.contains_key("__call__", self)
} else {
false
}
},
)
}
pub fn _sub(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_reflection(a, b, "__sub__", "__rsub__", |vm, a, b| {
Err(vm.new_unsupported_operand_error(a, b, "-"))