Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pkcs8 = "0.11"
proc-macro2 = "1.0.105"
psm = "0.1"
pymath = { git = "https://github.com/RustPython/pymath.git", rev = "dd1e9a4cf26d68caf3b549c09476f52e356e16a3", features = ["mul_add", "malachite-bigint", "complex"] }
pyo3 = "0.29"
pyo3 = "0.30"
quote = "1.0.45"
radium = "1.1.1"
rand = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion crates/capi/src/abstract_/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub unsafe extern "C" fn PyIter_Send(
#[cfg(test)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::{PyAnyMethods, PyIterator, PyList, PySendResult};
use pyo3::types::{PyAnyMethods, PyIterator, PyList, iterator::PySendResult};

#[test]
fn next_item() {
Expand Down
41 changes: 27 additions & 14 deletions crates/capi/src/methodobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,25 @@ pub(crate) fn build_method_def(
},
PyMethodFlags::VARARGS => {
let callable = move |args: PosArgs, vm: &VirtualMachine| unsafe {
call_function(vm, method, flags, Some(args))
call_function(vm, method, has_self, 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)
call_function_with_keywords(vm, method, has_self, 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)
call_fast_function_with_keywords(vm, method, has_self, 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)
call_fast_function(vm, method, has_self, args)
};
Ok(vm.ctx.new_method_def(name, callable, flags, doc))
},
Expand Down Expand Up @@ -160,12 +160,12 @@ pub(crate) fn build_method_def(
unsafe fn call_function<A: Into<FuncArgs>>(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
has_self: bool,
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 slf = take_self_arg(&mut args, has_self);
let arg_tuple = vm.ctx.new_tuple(args.args);
(slf, Some(arg_tuple))
} else {
Expand All @@ -188,11 +188,11 @@ unsafe fn call_function<A: Into<FuncArgs>>(
unsafe fn call_function_with_keywords(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
has_self: bool,
mut args: FuncArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionWithKeywords };
let slf = take_self_arg(&mut args, flags);
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
Expand All @@ -215,11 +215,11 @@ unsafe fn call_function_with_keywords(
unsafe fn call_fast_function_with_keywords(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
has_self: bool,
mut args: FuncArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionFastWithKeywords };
let slf = take_self_arg(&mut args, flags);
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
Expand Down Expand Up @@ -250,12 +250,12 @@ unsafe fn call_fast_function_with_keywords(
unsafe fn call_fast_function(
vm: &VirtualMachine,
method: PyMethodPointer,
flags: PyMethodFlags,
has_self: bool,
args: PosArgs,
) -> PyResult {
let f = unsafe { method.PyCFunctionFast };
let mut args: FuncArgs = args.into();
let slf = take_self_arg(&mut args, flags);
let slf = take_self_arg(&mut args, has_self);
let slf_ptr = slf
.as_ref()
.map(|obj| obj.as_object().as_raw().cast_mut())
Expand All @@ -267,8 +267,8 @@ unsafe fn call_fast_function(
unsafe { f(slf_ptr, fastcall_arg_ptrs, args.args.len() as isize).assume_owned_or_err(vm) }
}

fn take_self_arg(args: &mut FuncArgs, flags: PyMethodFlags) -> Option<PyObjectRef> {
if flags.contains(PyMethodFlags::STATIC) {
fn take_self_arg(args: &mut FuncArgs, has_self: bool) -> Option<PyObjectRef> {
if !has_self {
None
} else {
args.take_positional()
Expand Down Expand Up @@ -386,6 +386,19 @@ mod tests {
});
}

#[test]
fn wrap_one_arg_function_with_no_module() {
#[pyfunction]
fn one_arg(value: i64) -> i64 {
value
}

Python::attach(|py| {
let func = wrap_pyfunction!(one_arg, py).unwrap();
func.call1((7,)).unwrap();
});
}

#[test]
fn no_keywords_before_arity() {
Python::attach(|py| {
Expand Down
3 changes: 2 additions & 1 deletion crates/capi/src/pybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ mod tests {
let buffer = PyBuffer::<u8>::get(&bytearray).unwrap();
assert_eq!(buffer.dimensions(), 1);
assert_eq!(buffer.item_count(), 5);
buffer.as_mut_slice(py).unwrap()[0].replace(b'H');
// SAFETY: There are no other references to the buffer.
unsafe { buffer.as_slice_ptr().unwrap().as_mut()[0] = b'H' };
drop(buffer);
assert_eq!(bytearray.to_vec(), b"Hello");
});
Expand Down
Loading