Skip to content

Commit 662e26b

Browse files
Add more abstract functions to c-api
1 parent 33c0cf7 commit 662e26b

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

crates/capi/src/abstract_.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{PyObject, pystate::with_vm};
22
use alloc::slice;
3-
use core::ffi::c_int;
3+
use core::ffi::{CStr, c_char, c_int};
44
pub use iter::*;
55
pub use mapping::*;
66
pub use number::*;
@@ -57,6 +57,21 @@ pub unsafe extern "C" fn PyObject_CallNoArgs(callable: *mut PyObject) -> *mut Py
5757
with_vm(|vm| unsafe { &*callable }.call((), vm))
5858
}
5959

60+
#[unsafe(no_mangle)]
61+
pub unsafe extern "C" fn PyObject_CallObject(
62+
callable: *mut PyObject,
63+
args: *mut PyObject,
64+
) -> *mut PyObject {
65+
with_vm(|vm| {
66+
let callable = unsafe { &*callable };
67+
if let Some(args) = unsafe { args.as_ref() } {
68+
callable.call(tuple_to_args(args.try_downcast_ref::<PyTuple>(vm)?), vm)
69+
} else {
70+
callable.call((), vm)
71+
}
72+
})
73+
}
74+
6075
#[unsafe(no_mangle)]
6176
pub unsafe extern "C" fn PyObject_Vectorcall(
6277
callable: *mut PyObject,
@@ -121,6 +136,15 @@ pub unsafe extern "C" fn PyObject_VectorcallMethod(
121136
})
122137
}
123138

139+
#[unsafe(no_mangle)]
140+
pub unsafe extern "C" fn PyVectorcall_Call(
141+
callable: *mut PyObject,
142+
tuple: *mut PyObject,
143+
kwargs: *mut PyObject,
144+
) -> *mut PyObject {
145+
unsafe { PyObject_Call(callable, tuple, kwargs) }
146+
}
147+
124148
#[unsafe(no_mangle)]
125149
pub unsafe extern "C" fn PyObject_GetItem(obj: *mut PyObject, key: *mut PyObject) -> *mut PyObject {
126150
with_vm(|vm| {
@@ -153,6 +177,29 @@ pub unsafe extern "C" fn PyObject_DelItem(obj: *mut PyObject, key: *mut PyObject
153177
})
154178
}
155179

180+
#[unsafe(no_mangle)]
181+
pub unsafe extern "C" fn PyObject_DelItemString(obj: *mut PyObject, key: *const c_char) -> c_int {
182+
with_vm(|vm| {
183+
let obj = unsafe { &*obj };
184+
let key = unsafe { CStr::from_ptr(key) }
185+
.to_str()
186+
.map_err(|_| vm.new_value_error("mapping key must be valid UTF-8"))?;
187+
obj.del_item(key, vm)
188+
})
189+
}
190+
191+
#[unsafe(no_mangle)]
192+
pub unsafe extern "C" fn PyObject_Format(
193+
obj: *mut PyObject,
194+
format_spec: *mut PyObject,
195+
) -> *mut PyObject {
196+
with_vm(|vm| {
197+
let obj = unsafe { &*obj };
198+
let format_spec = unsafe { &*format_spec }.try_downcast_ref::<PyStr>(vm)?;
199+
vm.format(obj, format_spec.to_owned())
200+
})
201+
}
202+
156203
#[unsafe(no_mangle)]
157204
pub unsafe extern "C" fn PyObject_IsSubclass(derived: *mut PyObject, cls: *mut PyObject) -> c_int {
158205
with_vm(|vm| {
@@ -178,3 +225,13 @@ pub unsafe extern "C" fn PyObject_Size(obj: *mut PyObject) -> isize {
178225
obj.length(vm)
179226
})
180227
}
228+
229+
#[unsafe(no_mangle)]
230+
pub unsafe extern "C" fn PyObject_Length(obj: *mut PyObject) -> isize {
231+
unsafe { PyObject_Size(obj) }
232+
}
233+
234+
#[unsafe(no_mangle)]
235+
pub unsafe extern "C" fn PyObject_Type(obj: *mut PyObject) -> *mut PyObject {
236+
with_vm(|_vm| unsafe { &*obj }.obj_type())
237+
}

0 commit comments

Comments
 (0)