mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Add vm.is_callable
This commit is contained in:
@@ -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 {
|
||||
|
||||
15
vm/src/vm.rs
15
vm/src/vm.rs
@@ -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, "-"))
|
||||
|
||||
Reference in New Issue
Block a user