mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
PyCallable
This commit is contained in:
33
vm/src/protocol/callable.rs
Normal file
33
vm/src/protocol/callable.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use crate::{
|
||||
function::IntoFuncArgs,
|
||||
types::GenericMethod,
|
||||
{PyObject, PyResult, VirtualMachine}
|
||||
};
|
||||
|
||||
impl PyObject {
|
||||
#[inline]
|
||||
pub fn to_callable(&self) -> Option<PyCallable<'_>> {
|
||||
PyCallable::new(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_callable(&self) -> bool {
|
||||
self.to_callable().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PyCallable<'a> {
|
||||
pub obj: &'a PyObject,
|
||||
pub call: GenericMethod,
|
||||
}
|
||||
|
||||
impl<'a> PyCallable<'a> {
|
||||
pub fn new(obj: &'a PyObject) -> Option<Self> {
|
||||
let call = obj.class().mro_find_map(|cls| cls.slots.call.load())?;
|
||||
Some(PyCallable { obj, call })
|
||||
}
|
||||
|
||||
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
|
||||
(self.call)(self.obj, args.into_args(vm), vm)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod buffer;
|
||||
mod callable;
|
||||
mod iter;
|
||||
mod mapping;
|
||||
mod number;
|
||||
@@ -6,6 +7,7 @@ mod object;
|
||||
mod sequence;
|
||||
|
||||
pub use buffer::{BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, VecBuffer};
|
||||
pub use callable::PyCallable;
|
||||
pub use iter::{PyIter, PyIterIter, PyIterReturn};
|
||||
pub use mapping::{PyMapping, PyMappingMethods};
|
||||
pub use number::{PyNumber, PyNumberMethods};
|
||||
|
||||
@@ -37,8 +37,8 @@ fn trigger_signals(vm: &VirtualMachine) -> PyResult<()> {
|
||||
let triggered = trigger.swap(false, Ordering::Relaxed);
|
||||
if triggered {
|
||||
if let Some(handler) = &signal_handlers[signum] {
|
||||
if vm.is_callable(handler) {
|
||||
vm.invoke(handler, (signum, vm.ctx.none()))?;
|
||||
if let Some(callable) = handler.to_callable() {
|
||||
callable.invoke((signum, vm.ctx.none()), vm)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user