forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodobject.rs
More file actions
369 lines (338 loc) · 12.3 KB
/
Copy pathmethodobject.rs
File metadata and controls
369 lines (338 loc) · 12.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use crate::PyObject;
use crate::object::PyTypeObject;
use crate::object::define_py_check;
use crate::pystate::with_vm;
use core::ffi::{CStr, c_char, c_int};
use core::ptr::NonNull;
use rustpython_vm::function::{FuncArgs, HeapMethodDef, PosArgs, PyMethodFlags};
use rustpython_vm::{AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine};
define_py_check!(fn PyCFunction_Check, types.builtin_function_or_method_type);
define_py_check!(exact fn PyCFunction_CheckExact, types.builtin_function_or_method_type);
#[repr(C)]
pub struct PyMethodDef {
pub ml_name: *const c_char,
pub ml_meth: PyMethodPointer,
pub ml_flags: c_int,
pub ml_doc: *const c_char,
}
#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub union PyMethodPointer {
pub PyCFunction: unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject,
pub PyCFunctionWithKeywords: unsafe extern "C" fn(
slf: *mut PyObject,
args: *mut PyObject,
kwargs: *mut PyObject,
) -> *mut PyObject,
pub PyCFunctionFast: unsafe extern "C" fn(
slf: *mut PyObject,
args: *const *mut PyObject,
nargs: isize,
) -> *mut PyObject,
pub PyCFunctionFastWithKeywords: unsafe extern "C" fn(
slf: *mut PyObject,
args: *const *mut PyObject,
nargs: isize,
kwnames: *mut PyObject,
) -> *mut PyObject,
}
pub(crate) fn build_method_def(
vm: &VirtualMachine,
ml: &PyMethodDef,
has_self: bool,
) -> PyResult<PyRef<HeapMethodDef>> {
let name = unsafe { CStr::from_ptr(ml.ml_name) }
.to_str()
.map_err(|_| vm.new_system_error("Method name was not valid UTF-8"))?;
let doc = NonNull::new(ml.ml_doc.cast_mut())
.map(|doc| {
unsafe { CStr::from_ptr(doc.as_ptr()) }
.to_str()
.map_err(|_| vm.new_system_error("Method doc was not valid UTF-8"))
})
.transpose()?;
let flags = PyMethodFlags::from_bits(ml.ml_flags as u32)
.ok_or_else(|| vm.new_system_error("PyMethodDef contains unknown flags"))?;
let method = ml.ml_meth;
if flags.contains(PyMethodFlags::METHOD) {
return Err(vm.new_system_error("METH_METHOD is not supported on abi3"));
}
let call_flags = flags
& (PyMethodFlags::VARARGS
| PyMethodFlags::KEYWORDS
| PyMethodFlags::NOARGS
| PyMethodFlags::O
| PyMethodFlags::FASTCALL);
bitflags::bitflags_match!(call_flags, {
PyMethodFlags::NOARGS => {
if has_self {
let callable = move |zelf: PyObjectRef, vm: &VirtualMachine| unsafe {
let f = method.PyCFunction;
let ret_ptr = f(zelf.as_raw().cast_mut(), core::ptr::null_mut());
ret_ptr_to_pyresult(vm, ret_ptr)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
} else {
let callable = move |vm: &VirtualMachine| unsafe {
let f = method.PyCFunction;
let ret_ptr = f(core::ptr::null_mut(), core::ptr::null_mut());
ret_ptr_to_pyresult(vm, ret_ptr)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
}
},
PyMethodFlags::VARARGS => {
let callable = move |args: PosArgs, vm: &VirtualMachine| unsafe {
call_function(vm, method, flags, Some(args))
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
},
PyMethodFlags::VARARGS | PyMethodFlags::KEYWORDS => {
let callable = move | args: FuncArgs, vm: &VirtualMachine| unsafe {
call_function_with_keywords(vm, method, flags, args)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
},
PyMethodFlags::FASTCALL | PyMethodFlags::KEYWORDS => {
let callable = move |args: FuncArgs, vm: &VirtualMachine| unsafe {
call_fast_function_with_keywords(vm, method, flags, args)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
},
PyMethodFlags::FASTCALL => {
let callable = move |args: PosArgs, vm: &VirtualMachine| unsafe {
call_fast_function(vm, method, flags, args)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
},
PyMethodFlags::O => {
let f = unsafe { method.PyCFunction };
if has_self {
let callable = move |zelf: PyObjectRef, arg: PyObjectRef, vm: &VirtualMachine| -> PyResult {
let ret_ptr = unsafe { f(zelf.as_raw().cast_mut(), arg.as_raw().cast_mut()) };
ret_ptr_to_pyresult(vm, ret_ptr)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
} else {
let callable = move |arg: PyObjectRef, vm: &VirtualMachine| -> PyResult {
let ret_ptr = unsafe { f(core::ptr::null_mut(), arg.as_raw().cast_mut()) };
ret_ptr_to_pyresult(vm, ret_ptr)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
}
},
_ => {
Err(vm.new_system_error(format!(
"function {name} has unsupported or invalid calling-convention flags: {flags:?}"
)))
},
})
}
unsafe fn call_function<A: Into<FuncArgs>>(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
args: Option<A>,
) -> PyResult {
let f = unsafe { method.PyCFunction };
let (slf, arg_tuple) = if let Some(mut args) = args.map(Into::into) {
let slf = take_self_arg(&mut args, flags);
let arg_tuple = vm.ctx.new_tuple(args.args);
(slf, Some(arg_tuple))
} else {
(None, None)
};
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
let arg_ptr = arg_tuple
.as_ref()
.map(|tuple| tuple.as_object().as_raw().cast_mut())
.unwrap_or_default();
let ret_ptr = unsafe { f(slf_ptr, arg_ptr) };
ret_ptr_to_pyresult(vm, ret_ptr)
}
unsafe fn call_function_with_keywords(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
mut args: FuncArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionWithKeywords };
let slf = take_self_arg(&mut args, flags);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
let arg_tuple = vm.ctx.new_tuple(args.args);
let kwargs = vm.ctx.new_dict();
for (k, v) in args.kwargs {
kwargs.set_item(&*k, v, vm)?;
}
let ret_ptr = unsafe {
f(
slf_ptr,
arg_tuple.as_object().as_raw().cast_mut(),
kwargs.as_object().as_raw().cast_mut(),
)
};
ret_ptr_to_pyresult(vm, ret_ptr)
}
unsafe fn call_fast_function_with_keywords(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
mut args: FuncArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionFastWithKeywords };
let slf = take_self_arg(&mut args, flags);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
let nargs = args.args.len();
let mut fastcall_args = args.args;
let kwnames_tuple = if !args.kwargs.is_empty() {
let mut kwnames = Vec::with_capacity(args.kwargs.len());
for (k, v) in args.kwargs {
kwnames.push(vm.ctx.new_str(k).into());
fastcall_args.push(v);
}
Some(vm.ctx.new_tuple(kwnames))
} else {
None
};
let kwnames_ptr = kwnames_tuple
.as_ref()
.map(|tuple| tuple.as_object().as_raw().cast_mut())
.unwrap_or_default();
// SAFETY: PyObjectRef is repr(transparent) over a pointer to PyObject, so a
// Vec<PyObjectRef> has a layout-compatible contiguous backing buffer. The
// vector is kept alive for the duration of the call.
let fastcall_arg_ptrs = fastcall_args.as_ptr().cast::<*mut PyObject>();
let ret_ptr = unsafe { f(slf_ptr, fastcall_arg_ptrs, nargs as isize, kwnames_ptr) };
ret_ptr_to_pyresult(vm, ret_ptr)
}
unsafe fn call_fast_function(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
args: PosArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionFast };
let mut args: FuncArgs = args.into();
let slf = take_self_arg(&mut args, flags);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
.unwrap_or_default();
// SAFETY: PyObjectRef is repr(transparent) over a pointer to PyObject, so a
// Vec<PyObjectRef> has a layout-compatible contiguous backing buffer. The
// vector is kept alive for the duration of the call.
let fastcall_arg_ptrs = args.args.as_mut_ptr().cast::<*mut PyObject>();
let ret_ptr = unsafe { f(slf_ptr, fastcall_arg_ptrs, args.args.len() as isize) };
ret_ptr_to_pyresult(vm, ret_ptr)
}
fn ret_ptr_to_pyresult(vm: &VirtualMachine, ret_ptr: *mut PyObject) -> PyResult {
let ret_ptr = NonNull::new(ret_ptr).ok_or_else(|| {
vm.take_raised_exception()
.expect("Native function returned NULL, but there was no exception set")
})?;
Ok(unsafe { PyObjectRef::from_raw(ret_ptr) })
}
fn take_self_arg(args: &mut FuncArgs, flags: PyMethodFlags) -> Option<PyObjectRef> {
if flags.contains(PyMethodFlags::STATIC) {
None
} else {
args.take_positional()
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyCMethod_New(
ml: *mut PyMethodDef,
slf: *mut PyObject,
_module: *mut PyObject,
_cls: *mut PyTypeObject,
) -> *mut PyObject {
with_vm(|vm| -> PyResult {
assert!(
_cls.is_null(),
"PyCMethod_New does not support METH_METHOD on abi3"
);
let ml = unsafe { &*ml };
let zelf = unsafe { slf.as_ref().map(|obj| obj.to_owned()) };
Ok(build_method_def(vm, ml, zelf.is_some())?
.build_function(vm, zelf)
.into())
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyCFunction_New(
ml: *mut PyMethodDef,
slf: *mut PyObject,
) -> *mut PyObject {
unsafe { PyCMethod_New(ml, slf, core::ptr::null_mut(), core::ptr::null_mut()) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyCFunction_NewEx(
ml: *mut PyMethodDef,
slf: *mut PyObject,
module: *mut PyObject,
) -> *mut PyObject {
unsafe { PyCMethod_New(ml, slf, module, core::ptr::null_mut()) }
}
#[cfg(false)]
mod tests {
use pyo3::exceptions::PyException;
use pyo3::ffi::{PyLong_FromLong, PyObject};
use pyo3::prelude::*;
use pyo3::types::{PyCFunction, PyInt, PyString};
#[test]
fn test_closure_function() {
Python::attach(|py| {
let f = PyCFunction::new_closure(py, None, None, |_args, _kwargs| "Hello from Rust!")
.unwrap();
assert_eq!(
f.call0().unwrap().cast::<PyString>().unwrap(),
"Hello from Rust!"
);
})
}
#[test]
fn test_function_no_args() {
Python::attach(|py| {
unsafe extern "C" fn c_fn(_self: *mut PyObject, _args: *mut PyObject) -> *mut PyObject {
assert!(_self.is_null());
assert!(_args.is_null());
unsafe { PyLong_FromLong(4200) }
}
let py_fn = PyCFunction::new(py, c_fn, c"py_fn", c"", None).unwrap();
let result = py_fn
.call0()
.unwrap()
.cast::<PyInt>()
.unwrap()
.extract::<u32>()
.unwrap();
assert_eq!(result, 4200);
assert!(py_fn.call((1,), None).is_err());
assert!(py_fn.call((1, 2), None).is_err());
})
}
#[test]
fn test_closure_function_error() {
Python::attach(|py| {
let f = PyCFunction::new_closure(py, None, None, |_args, _kwargs| {
Err::<(), _>(PyException::new_err("Something went wrong"))
})
.unwrap();
let err = f.call0().unwrap_err();
assert_eq!(
err.value(py).repr().unwrap(),
"Exception('Something went wrong')"
);
})
}
}