Repository navigation
Expand file tree
/
Copy pathcallable.rs
More file actions
143 lines (129 loc) · 4.51 KB
/
Copy pathcallable.rs
File metadata and controls
143 lines (129 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::{
builtins::{PyBoundMethod, PyFunction},
function::{FuncArgs, IntoFuncArgs},
types::GenericMethod,
{PyObject, PyObjectRef, 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()
}
/// PyObject_Call*Arg* series
#[inline]
pub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
let args = args.into_args(vm);
self.call_with_args(args, vm)
}
/// PyObject_Call
pub fn call_with_args(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let Some(callable) = self.to_callable() else {
return Err(
vm.new_type_error(format!("'{}' object is not callable", self.class().name()))
);
};
vm_trace!("Invoke: {:?} {:?}", callable, args);
callable.invoke(args, vm)
}
}
#[derive(Debug)]
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().slots.call.load()?;
Some(PyCallable { obj, call })
}
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
let args = args.into_args(vm);
// Python functions get 'call'/'return' events from with_frame().
// Bound methods delegate to the inner callable, which fires its own events.
// All other callables (built-in functions, etc.) get 'c_call'/'c_return'/'c_exception'.
let is_python_callable = self.obj.downcast_ref::<PyFunction>().is_some()
|| self.obj.downcast_ref::<PyBoundMethod>().is_some();
if is_python_callable {
(self.call)(self.obj, args, vm)
} else {
let callable = self.obj.to_owned();
vm.trace_event(TraceEvent::CCall, Some(callable.clone()))?;
let result = (self.call)(self.obj, args, vm);
if result.is_ok() {
vm.trace_event(TraceEvent::CReturn, Some(callable))?;
} else {
let _ = vm.trace_event(TraceEvent::CException, Some(callable));
}
result
}
}
}
/// Trace events for sys.settrace and sys.setprofile.
pub(crate) enum TraceEvent {
Call,
Return,
Line,
CCall,
CReturn,
CException,
}
impl core::fmt::Display for TraceEvent {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use TraceEvent::*;
match self {
Call => write!(f, "call"),
Return => write!(f, "return"),
Line => write!(f, "line"),
CCall => write!(f, "c_call"),
CReturn => write!(f, "c_return"),
CException => write!(f, "c_exception"),
}
}
}
impl VirtualMachine {
/// Call registered trace function.
#[inline]
pub(crate) fn trace_event(&self, event: TraceEvent, arg: Option<PyObjectRef>) -> PyResult<()> {
if self.use_tracing.get() {
self._trace_event_inner(event, arg)
} else {
Ok(())
}
}
fn _trace_event_inner(&self, event: TraceEvent, arg: Option<PyObjectRef>) -> PyResult<()> {
let trace_func = self.trace_func.borrow().to_owned();
let profile_func = self.profile_func.borrow().to_owned();
if self.is_none(&trace_func) && self.is_none(&profile_func) {
return Ok(());
}
let Some(frame_ref) = self.current_frame() else {
return Ok(());
};
let frame: PyObjectRef = frame_ref.into();
let event = self.ctx.new_str(event.to_string()).into();
let args = vec![frame, event, arg.unwrap_or_else(|| self.ctx.none())];
// temporarily disable tracing, during the call to the
// tracing function itself.
if !self.is_none(&trace_func) {
self.use_tracing.set(false);
let res = trace_func.call(args.clone(), self);
self.use_tracing.set(true);
if res.is_err() {
*self.trace_func.borrow_mut() = self.ctx.none();
}
}
if !self.is_none(&profile_func) {
self.use_tracing.set(false);
let res = profile_func.call(args, self);
self.use_tracing.set(true);
if res.is_err() {
*self.profile_func.borrow_mut() = self.ctx.none();
}
}
Ok(())
}
}