Skip to content

Commit 9530575

Browse files
Move conversion impl to c-api
1 parent 6d47176 commit 9530575

2 files changed

Lines changed: 24 additions & 35 deletions

File tree

crates/capi/src/abstract_.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@ use crate::{PyObject, pystate::with_vm};
22
use alloc::slice;
33
use core::ffi::c_int;
44
use rustpython_vm::builtins::{PyDict, PyStr, PyTuple};
5-
use rustpython_vm::function::{FuncArgs, KwArgs};
6-
use rustpython_vm::{AsObject, PyObjectRef};
5+
use rustpython_vm::function::{FuncArgs, KwArgs, PosArgs};
6+
use rustpython_vm::{AsObject, Py, PyObjectRef, PyResult, VirtualMachine};
77

88
const PY_VECTORCALL_ARGUMENTS_OFFSET: usize = 1usize << (usize::BITS as usize - 1);
99

10+
fn tuple_to_args(tuple: &Py<PyTuple>) -> PosArgs {
11+
tuple.iter().cloned().collect::<Vec<_>>().into()
12+
}
13+
14+
fn dict_to_kwargs(vm: &VirtualMachine, dict: &Py<PyDict>) -> PyResult<KwArgs> {
15+
dict.items_vec()
16+
.into_iter()
17+
.map(|(key, value)| {
18+
let key = key
19+
.downcast_ref::<PyStr>()
20+
.map(|s| s.to_string())
21+
.ok_or_else(|| vm.new_type_error("keywords must be strings"))?;
22+
Ok((key, value))
23+
})
24+
.collect::<PyResult<_>>()
25+
.map(KwArgs::new)
26+
}
27+
1028
#[unsafe(no_mangle)]
1129
pub unsafe extern "C" fn PyObject_Call(
1230
callable: *mut PyObject,
@@ -15,10 +33,10 @@ pub unsafe extern "C" fn PyObject_Call(
1533
) -> *mut PyObject {
1634
with_vm(|vm| {
1735
let callable = unsafe { &*callable };
18-
let args = unsafe { &*args }.try_downcast_ref::<PyTuple>(vm)?;
36+
let args = tuple_to_args(unsafe { &*args }.try_downcast_ref::<PyTuple>(vm)?);
1937

2038
let kwargs: Option<KwArgs> = unsafe { kwargs.as_ref() }
21-
.map(|kwargs| kwargs.try_downcast_ref::<PyDict>(vm)?.try_into())
39+
.map(|kwargs| dict_to_kwargs(vm, kwargs.try_downcast_ref::<PyDict>(vm)?))
2240
.transpose()?;
2341

2442
callable.call_with_args(FuncArgs::new(args, kwargs.unwrap_or_default()), vm)

crates/vm/src/function/argument.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
3-
builtins::{PyBaseExceptionRef, PyDict, PyStr, PyTuple, PyTupleRef, PyTypeRef},
2+
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
3+
builtins::{PyBaseExceptionRef, PyTupleRef, PyTypeRef},
44
convert::ToPyObject,
55
object::{Traverse, TraverseFn},
66
};
@@ -438,29 +438,6 @@ impl<T> FromIterator<(String, T)> for KwArgs<T> {
438438
}
439439
}
440440

441-
impl TryFrom<&Py<PyDict>> for KwArgs<PyObjectRef> {
442-
type Error = PyBaseExceptionRef;
443-
444-
fn try_from(kwargs: &Py<PyDict>) -> Result<Self, Self::Error> {
445-
kwargs
446-
.items_vec()
447-
.into_iter()
448-
.map(|(key, value)| {
449-
let key = key
450-
.downcast_ref::<PyStr>()
451-
.map(|s| s.to_string())
452-
.ok_or_else(|| {
453-
crate::vm::thread::with_current_vm(|vm| {
454-
vm.new_type_error("keywords must be strings")
455-
})
456-
})?;
457-
Ok((key, value))
458-
})
459-
.collect::<Result<IndexMap<_, _>, _>>()
460-
.map(Self)
461-
}
462-
}
463-
464441
impl<T> Default for KwArgs<T> {
465442
fn default() -> Self {
466443
Self(IndexMap::new())
@@ -531,12 +508,6 @@ impl<T> From<Vec<T>> for PosArgs<T> {
531508
}
532509
}
533510

534-
impl From<&Py<PyTuple>> for PosArgs<PyObjectRef> {
535-
fn from(args: &Py<PyTuple>) -> Self {
536-
Self(args.iter().cloned().collect())
537-
}
538-
}
539-
540511
impl From<()> for PosArgs<PyObjectRef> {
541512
fn from(_args: ()) -> Self {
542513
Self(Vec::new())

0 commit comments

Comments
 (0)