|
1 | 1 | use crate::{ |
2 | | - builtins::{PyBoundMethod, PyFunction}, |
| 2 | + builtins::{builtin_func::PyNativeFunction, descriptor::PyMethodDescriptor}, |
3 | 3 | function::{FuncArgs, IntoFuncArgs}, |
4 | | - types::{GenericMethod, PyTypeFlags, VectorCallFunc}, |
5 | | - {PyObject, PyObjectRef, PyResult, VirtualMachine}, |
| 4 | + types::{GenericMethod, GetDescriptor, PyTypeFlags, VectorCallFunc}, |
| 5 | + {AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine}, |
6 | 6 | }; |
7 | 7 |
|
8 | 8 | impl PyObject { |
@@ -111,22 +111,69 @@ impl PyObject { |
111 | 111 | if !vm.use_tracing.get() { |
112 | 112 | return vc(self, args, nargs, kwnames, vm); |
113 | 113 | } |
114 | | - let is_python_callable = self.downcast_ref::<PyFunction>().is_some() |
115 | | - || self.downcast_ref::<PyBoundMethod>().is_some(); |
116 | | - if is_python_callable { |
117 | | - vc(self, args, nargs, kwnames, vm) |
| 114 | + let first_arg = self.profile_first_arg(args[..nargs].first(), vm); |
| 115 | + let Some(callable) = self.profile_callable(first_arg.as_ref(), vm)? else { |
| 116 | + return vc(self, args, nargs, kwnames, vm); |
| 117 | + }; |
| 118 | + vm.trace_event(TraceEvent::CCall, Some(callable))?; |
| 119 | + let result = vc(self, args, nargs, kwnames, vm); |
| 120 | + if result.is_ok() { |
| 121 | + self.profile_event(TraceEvent::CReturn, first_arg.as_ref(), vm)?; |
118 | 122 | } else { |
119 | | - let callable = self.to_owned(); |
120 | | - vm.trace_event(TraceEvent::CCall, Some(callable.clone()))?; |
121 | | - let result = vc(self, args, nargs, kwnames, vm); |
122 | | - if result.is_ok() { |
123 | | - vm.trace_event(TraceEvent::CReturn, Some(callable))?; |
124 | | - } else { |
125 | | - let _ = vm.trace_event(TraceEvent::CException, Some(callable)); |
126 | | - } |
127 | | - result |
| 123 | + let _ = self.profile_event(TraceEvent::CException, first_arg.as_ref(), vm); |
| 124 | + } |
| 125 | + result |
| 126 | + } |
| 127 | + |
| 128 | + #[cold] |
| 129 | + fn profile_event( |
| 130 | + &self, |
| 131 | + event: TraceEvent, |
| 132 | + first_arg: Option<&PyObjectRef>, |
| 133 | + vm: &VirtualMachine, |
| 134 | + ) -> PyResult<()> { |
| 135 | + if let Some(callable) = self.profile_callable(first_arg, vm)? { |
| 136 | + vm.trace_event(event, Some(callable))?; |
| 137 | + } |
| 138 | + Ok(()) |
| 139 | + } |
| 140 | + |
| 141 | + #[cold] |
| 142 | + fn profile_first_arg( |
| 143 | + &self, |
| 144 | + first_arg: Option<&PyObjectRef>, |
| 145 | + vm: &VirtualMachine, |
| 146 | + ) -> Option<PyObjectRef> { |
| 147 | + // Only method descriptors need a receiver for legacy profile callbacks. |
| 148 | + // Keep it available for binding independently at call and return. |
| 149 | + if self.class().is(vm.ctx.types.method_descriptor_type) { |
| 150 | + first_arg.cloned() |
| 151 | + } else { |
| 152 | + None |
128 | 153 | } |
129 | 154 | } |
| 155 | + |
| 156 | + // CPython legacy_tracing.c: sys_profile_call_or_return. This filter is |
| 157 | + // specific to sys.setprofile, not sys.monitoring's general CALL events. |
| 158 | + #[cold] |
| 159 | + fn profile_callable( |
| 160 | + &self, |
| 161 | + first_arg: Option<&PyObjectRef>, |
| 162 | + vm: &VirtualMachine, |
| 163 | + ) -> PyResult<Option<PyObjectRef>> { |
| 164 | + if vm.tracing_is_suppressed() || vm.is_none(&vm.profile_func.borrow()) { |
| 165 | + return Ok(None); |
| 166 | + } |
| 167 | + if self.downcast_ref::<PyNativeFunction>().is_some() { |
| 168 | + return Ok(Some(self.to_owned())); |
| 169 | + } |
| 170 | + if self.class().is(vm.ctx.types.method_descriptor_type) |
| 171 | + && let Some(first_arg) = first_arg |
| 172 | + { |
| 173 | + return PyMethodDescriptor::descr_get(self, Some(first_arg), None, vm).map(Some); |
| 174 | + } |
| 175 | + Ok(None) |
| 176 | + } |
130 | 177 | } |
131 | 178 |
|
132 | 179 | #[derive(Debug)] |
@@ -165,24 +212,21 @@ impl<'a> PyCallable<'a> { |
165 | 212 | if !vm.use_tracing.get() { |
166 | 213 | return (self.call)(self.obj, args, vm); |
167 | 214 | } |
168 | | - // Python functions get 'call'/'return' events from with_frame(). |
169 | | - // Bound methods delegate to the inner callable, which fires its own events. |
170 | | - // All other callables (built-in functions, etc.) get 'c_call'/'c_return'/'c_exception'. |
171 | | - let is_python_callable = self.obj.downcast_ref::<PyFunction>().is_some() |
172 | | - || self.obj.downcast_ref::<PyBoundMethod>().is_some(); |
173 | | - if is_python_callable { |
174 | | - (self.call)(self.obj, args, vm) |
| 215 | + let first_arg = self.obj.profile_first_arg(args.args.first(), vm); |
| 216 | + let Some(callable) = self.obj.profile_callable(first_arg.as_ref(), vm)? else { |
| 217 | + return (self.call)(self.obj, args, vm); |
| 218 | + }; |
| 219 | + vm.trace_event(TraceEvent::CCall, Some(callable))?; |
| 220 | + let result = (self.call)(self.obj, args, vm); |
| 221 | + if result.is_ok() { |
| 222 | + self.obj |
| 223 | + .profile_event(TraceEvent::CReturn, first_arg.as_ref(), vm)?; |
175 | 224 | } else { |
176 | | - let callable = self.obj.to_owned(); |
177 | | - vm.trace_event(TraceEvent::CCall, Some(callable.clone()))?; |
178 | | - let result = (self.call)(self.obj, args, vm); |
179 | | - if result.is_ok() { |
180 | | - vm.trace_event(TraceEvent::CReturn, Some(callable))?; |
181 | | - } else { |
182 | | - let _ = vm.trace_event(TraceEvent::CException, Some(callable)); |
183 | | - } |
184 | | - result |
| 225 | + let _ = self |
| 226 | + .obj |
| 227 | + .profile_event(TraceEvent::CException, first_arg.as_ref(), vm); |
185 | 228 | } |
| 229 | + result |
186 | 230 | } |
187 | 231 |
|
188 | 232 | /// Use automatic vectorcall if available, else fall back to FuncArgs. |
|
0 commit comments