2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2023-05-06 10:42:05 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-09-15 12:21:49 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2020-06-04 13:00:47 +00:00
|
|
|
use pyo3::buffer::PyBuffer;
|
2020-05-17 18:17:24 +00:00
|
|
|
use pyo3::prelude::*;
|
2020-09-23 12:50:40 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2022-08-13 16:23:34 +00:00
|
|
|
use pyo3::types::PyDateTime;
|
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
|
|
|
use pyo3::types::PyFunction;
|
|
|
|
use pyo3::types::{self, PyCFunction};
|
2020-05-17 18:17:24 +00:00
|
|
|
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2020-05-17 18:17:24 +00:00
|
|
|
mod common;
|
|
|
|
|
2022-10-25 06:23:21 +00:00
|
|
|
#[pyfunction(signature = (arg = true))]
|
2020-05-17 18:17:24 +00:00
|
|
|
fn optional_bool(arg: Option<bool>) -> String {
|
|
|
|
format!("{:?}", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optional_bool() {
|
|
|
|
// Regression test for issue #932
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(optional_bool)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
py_assert!(py, f, "f() == 'Some(true)'");
|
|
|
|
py_assert!(py, f, "f(True) == 'Some(true)'");
|
|
|
|
py_assert!(py, f, "f(False) == 'Some(false)'");
|
|
|
|
py_assert!(py, f, "f(None) == 'None'");
|
|
|
|
});
|
2020-05-17 18:17:24 +00:00
|
|
|
}
|
2020-06-04 13:00:47 +00:00
|
|
|
|
2020-09-15 12:21:49 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2020-06-04 13:00:47 +00:00
|
|
|
#[pyfunction]
|
2022-03-23 07:07:28 +00:00
|
|
|
fn buffer_inplace_add(py: Python<'_>, x: PyBuffer<i32>, y: PyBuffer<i32>) {
|
2020-06-04 13:00:47 +00:00
|
|
|
let x = x.as_mut_slice(py).unwrap();
|
|
|
|
let y = y.as_slice(py).unwrap();
|
|
|
|
for (xi, yi) in x.iter().zip(y) {
|
|
|
|
let xi_plus_yi = xi.get() + yi.get();
|
|
|
|
xi.set(xi_plus_yi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:21:49 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
2020-06-04 13:00:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buffer_add() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(buffer_inplace_add)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
f,
|
|
|
|
r#"
|
2020-06-04 13:00:47 +00:00
|
|
|
import array
|
|
|
|
a = array.array("i", [0, 1, 2, 3])
|
|
|
|
b = array.array("I", [0, 1, 2, 3])
|
|
|
|
f(a, b)
|
|
|
|
"#,
|
2022-07-19 17:34:23 +00:00
|
|
|
PyBufferError
|
|
|
|
);
|
2020-06-04 13:00:47 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
pyo3::py_run!(
|
|
|
|
py,
|
|
|
|
f,
|
|
|
|
r#"
|
2020-06-04 13:00:47 +00:00
|
|
|
import array
|
|
|
|
a = array.array("i", [0, 1, 2, 3])
|
|
|
|
b = array.array("i", [2, 3, 4, 5])
|
|
|
|
f(a, b)
|
|
|
|
assert a, array.array("i", [2, 4, 6, 8])
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
|
|
|
});
|
2020-06-04 13:00:47 +00:00
|
|
|
}
|
2020-09-05 13:54:03 +00:00
|
|
|
|
2022-08-13 16:23:34 +00:00
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
2020-09-05 13:54:03 +00:00
|
|
|
#[pyfunction]
|
|
|
|
fn function_with_pyfunction_arg(fun: &PyFunction) -> PyResult<&PyAny> {
|
|
|
|
fun.call((), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn function_with_pycfunction_arg(fun: &PyCFunction) -> PyResult<&PyAny> {
|
|
|
|
fun.call((), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_functions_with_function_args() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let py_cfunc_arg = wrap_pyfunction_bound!(function_with_pycfunction_arg)(py).unwrap();
|
|
|
|
let bool_to_string = wrap_pyfunction_bound!(optional_bool)(py).unwrap();
|
2020-09-23 12:50:40 +00:00
|
|
|
|
|
|
|
pyo3::py_run!(
|
|
|
|
py,
|
2022-07-19 17:34:23 +00:00
|
|
|
py_cfunc_arg
|
|
|
|
bool_to_string,
|
2020-09-23 12:50:40 +00:00
|
|
|
r#"
|
2022-07-19 17:34:23 +00:00
|
|
|
assert py_cfunc_arg(bool_to_string) == "Some(true)"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-08-13 16:23:34 +00:00
|
|
|
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
|
2022-07-19 17:34:23 +00:00
|
|
|
{
|
2024-03-12 22:57:03 +00:00
|
|
|
let py_func_arg = wrap_pyfunction_bound!(function_with_pyfunction_arg)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
pyo3::py_run!(
|
|
|
|
py,
|
|
|
|
py_func_arg,
|
|
|
|
r#"
|
2020-09-23 12:50:40 +00:00
|
|
|
def foo(): return "bar"
|
|
|
|
assert py_func_arg(foo) == "bar"
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2020-09-05 13:54:03 +00:00
|
|
|
}
|
2020-09-08 12:23:24 +00:00
|
|
|
|
2021-02-20 15:15:20 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
|
|
|
fn datetime_to_timestamp(dt: &PyAny) -> PyResult<i64> {
|
|
|
|
let dt: &PyDateTime = dt.extract()?;
|
|
|
|
let ts: f64 = dt.call_method0("timestamp")?.extract()?;
|
|
|
|
|
|
|
|
Ok(ts as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
|
|
|
#[pyfunction]
|
|
|
|
fn function_with_custom_conversion(
|
|
|
|
#[pyo3(from_py_with = "datetime_to_timestamp")] timestamp: i64,
|
|
|
|
) -> i64 {
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
|
|
|
#[test]
|
|
|
|
fn test_function_with_custom_conversion() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let custom_conv_func = wrap_pyfunction_bound!(function_with_custom_conversion)(py).unwrap();
|
2021-02-20 15:15:20 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
pyo3::py_run!(
|
|
|
|
py,
|
|
|
|
custom_conv_func,
|
|
|
|
r#"
|
2021-02-20 15:15:20 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
dt = datetime.datetime.fromtimestamp(1612040400)
|
|
|
|
assert custom_conv_func(dt) == 1612040400
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
)
|
|
|
|
});
|
2021-02-20 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
|
|
|
#[test]
|
|
|
|
fn test_function_with_custom_conversion_error() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let custom_conv_func = wrap_pyfunction_bound!(function_with_custom_conversion)(py).unwrap();
|
2021-02-20 15:15:20 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
custom_conv_func,
|
|
|
|
"custom_conv_func(['a'])",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'timestamp': 'list' object cannot be converted to 'PyDateTime'"
|
|
|
|
);
|
|
|
|
});
|
2021-02-20 15:15:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 17:52:33 +00:00
|
|
|
#[test]
|
|
|
|
fn test_from_py_with_defaults() {
|
|
|
|
fn optional_int(x: &PyAny) -> PyResult<Option<i32>> {
|
|
|
|
if x.is_none() {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Some(x.extract()).transpose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// issue 2280 combination of from_py_with and Option<T> did not compile
|
|
|
|
#[pyfunction]
|
|
|
|
fn from_py_with_option(#[pyo3(from_py_with = "optional_int")] int: Option<i32>) -> i32 {
|
|
|
|
int.unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:23:21 +00:00
|
|
|
#[pyfunction(signature = (len=0))]
|
2022-08-25 17:52:33 +00:00
|
|
|
fn from_py_with_default(#[pyo3(from_py_with = "PyAny::len")] len: usize) -> usize {
|
|
|
|
len
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(from_py_with_option)(py).unwrap();
|
2022-08-25 17:52:33 +00:00
|
|
|
|
|
|
|
assert_eq!(f.call0().unwrap().extract::<i32>().unwrap(), 0);
|
|
|
|
assert_eq!(f.call1((123,)).unwrap().extract::<i32>().unwrap(), 123);
|
|
|
|
assert_eq!(f.call1((999,)).unwrap().extract::<i32>().unwrap(), 999);
|
|
|
|
|
2024-03-12 22:57:03 +00:00
|
|
|
let f2 = wrap_pyfunction_bound!(from_py_with_default)(py).unwrap();
|
2022-08-25 17:52:33 +00:00
|
|
|
|
|
|
|
assert_eq!(f2.call0().unwrap().extract::<usize>().unwrap(), 0);
|
|
|
|
assert_eq!(f2.call1(("123",)).unwrap().extract::<usize>().unwrap(), 3);
|
|
|
|
assert_eq!(f2.call1(("1234",)).unwrap().extract::<usize>().unwrap(), 4);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-22 08:48:41 +00:00
|
|
|
#[pyclass]
|
2022-02-21 22:01:45 +00:00
|
|
|
#[derive(Debug, FromPyObject)]
|
|
|
|
struct ValueClass {
|
2022-02-22 08:48:41 +00:00
|
|
|
#[pyo3(get)]
|
2022-02-21 22:01:45 +00:00
|
|
|
value: usize,
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:38:21 +00:00
|
|
|
#[pyfunction]
|
2022-02-22 08:48:41 +00:00
|
|
|
fn conversion_error(
|
|
|
|
str_arg: &str,
|
|
|
|
int_arg: i64,
|
2024-03-08 07:43:48 +00:00
|
|
|
tuple_arg: (String, f64),
|
2022-02-22 08:48:41 +00:00
|
|
|
option_arg: Option<i64>,
|
|
|
|
struct_arg: Option<ValueClass>,
|
2022-02-21 22:01:45 +00:00
|
|
|
) {
|
2020-10-13 21:38:21 +00:00
|
|
|
println!(
|
2022-02-21 22:01:45 +00:00
|
|
|
"{:?} {:?} {:?} {:?} {:?}",
|
|
|
|
str_arg, int_arg, tuple_arg, option_arg, struct_arg
|
2020-10-13 21:38:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conversion_error() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let conversion_error = wrap_pyfunction_bound!(conversion_error)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"conversion_error(None, None, None, None, None)",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'str_arg': 'NoneType' object cannot be converted to 'PyString'"
|
|
|
|
);
|
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"conversion_error(100, None, None, None, None)",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'str_arg': 'int' object cannot be converted to 'PyString'"
|
|
|
|
);
|
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"conversion_error('string1', 'string2', None, None, None)",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'int_arg': 'str' object cannot be interpreted as an integer"
|
|
|
|
);
|
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"conversion_error('string1', -100, 'string2', None, None)",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'tuple_arg': 'str' object cannot be converted to 'PyTuple'"
|
|
|
|
);
|
|
|
|
py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"conversion_error('string1', -100, ('string2', 10.), 'string3', None)",
|
|
|
|
PyTypeError,
|
|
|
|
"argument 'option_arg': 'str' object cannot be interpreted as an integer"
|
|
|
|
);
|
|
|
|
let exception = py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"
|
2022-02-21 22:01:45 +00:00
|
|
|
class ValueClass:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
conversion_error('string1', -100, ('string2', 10.), None, ValueClass(\"no_expected_type\"))",
|
2022-07-19 17:34:23 +00:00
|
|
|
PyTypeError
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
extract_traceback(py, exception),
|
|
|
|
"TypeError: argument 'struct_arg': failed to \
|
2022-02-22 08:48:41 +00:00
|
|
|
extract field ValueClass.value: TypeError: 'str' object cannot be interpreted as an integer"
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
2022-02-22 08:48:41 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
let exception = py_expect_exception!(
|
|
|
|
py,
|
|
|
|
conversion_error,
|
|
|
|
"
|
2022-02-21 22:01:45 +00:00
|
|
|
class ValueClass:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
conversion_error('string1', -100, ('string2', 10.), None, ValueClass(-5))",
|
2022-07-19 17:34:23 +00:00
|
|
|
PyTypeError
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
extract_traceback(py, exception),
|
|
|
|
"TypeError: argument 'struct_arg': failed to \
|
2022-02-22 08:48:41 +00:00
|
|
|
extract field ValueClass.value: OverflowError: can't convert negative int to unsigned"
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
|
|
|
});
|
2020-10-13 21:38:21 +00:00
|
|
|
}
|
2021-10-03 19:37:17 +00:00
|
|
|
|
2022-02-22 08:48:41 +00:00
|
|
|
/// Helper function that concatenates the error message from
|
|
|
|
/// each error in the traceback into a single string that can
|
|
|
|
/// be tested.
|
2022-03-23 07:07:28 +00:00
|
|
|
fn extract_traceback(py: Python<'_>, mut error: PyErr) -> String {
|
2022-02-22 08:48:41 +00:00
|
|
|
let mut error_msg = error.to_string();
|
|
|
|
while let Some(cause) = error.cause(py) {
|
|
|
|
error_msg.push_str(": ");
|
|
|
|
error_msg.push_str(&cause.to_string());
|
|
|
|
error = cause
|
|
|
|
}
|
|
|
|
error_msg
|
|
|
|
}
|
|
|
|
|
2022-06-23 20:30:22 +00:00
|
|
|
#[test]
|
|
|
|
fn test_pycfunction_new() {
|
|
|
|
use pyo3::ffi;
|
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
unsafe extern "C" fn c_fn(
|
|
|
|
_self: *mut ffi::PyObject,
|
|
|
|
_args: *mut ffi::PyObject,
|
|
|
|
) -> *mut ffi::PyObject {
|
|
|
|
ffi::PyLong_FromLong(4200)
|
|
|
|
}
|
2022-06-23 20:30:22 +00:00
|
|
|
|
2024-02-05 07:51:18 +00:00
|
|
|
let py_fn = PyCFunction::new_bound(
|
2024-02-27 19:19:52 +00:00
|
|
|
py,
|
2022-07-19 17:34:23 +00:00
|
|
|
c_fn,
|
|
|
|
"py_fn",
|
|
|
|
"py_fn for test (this is the docstring)",
|
2024-02-27 19:19:52 +00:00
|
|
|
None,
|
2022-07-19 17:34:23 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-23 20:30:22 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(py, py_fn, "py_fn() == 4200");
|
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
py_fn,
|
|
|
|
"py_fn.__doc__ == 'py_fn for test (this is the docstring)'"
|
|
|
|
);
|
|
|
|
});
|
2022-06-23 20:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pycfunction_new_with_keywords() {
|
|
|
|
use pyo3::ffi;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::raw::{c_char, c_long};
|
|
|
|
use std::ptr;
|
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
unsafe extern "C" fn c_fn(
|
|
|
|
_self: *mut ffi::PyObject,
|
|
|
|
args: *mut ffi::PyObject,
|
|
|
|
kwds: *mut ffi::PyObject,
|
|
|
|
) -> *mut ffi::PyObject {
|
|
|
|
let mut foo: c_long = 0;
|
|
|
|
let mut bar: c_long = 0;
|
|
|
|
let foo_ptr: *mut c_long = &mut foo;
|
|
|
|
let bar_ptr: *mut c_long = &mut bar;
|
|
|
|
|
|
|
|
let foo_name = CString::new("foo").unwrap();
|
|
|
|
let foo_name_raw: *mut c_char = foo_name.into_raw();
|
|
|
|
let kw_bar_name = CString::new("kw_bar").unwrap();
|
|
|
|
let kw_bar_name_raw: *mut c_char = kw_bar_name.into_raw();
|
|
|
|
|
|
|
|
let mut arglist = vec![foo_name_raw, kw_bar_name_raw, ptr::null_mut()];
|
|
|
|
let arglist_ptr: *mut *mut c_char = arglist.as_mut_ptr();
|
|
|
|
|
|
|
|
let arg_pattern: *const c_char = CString::new("l|l").unwrap().into_raw();
|
|
|
|
|
|
|
|
ffi::PyArg_ParseTupleAndKeywords(
|
|
|
|
args,
|
|
|
|
kwds,
|
|
|
|
arg_pattern,
|
|
|
|
arglist_ptr,
|
|
|
|
foo_ptr,
|
|
|
|
bar_ptr,
|
|
|
|
);
|
|
|
|
|
|
|
|
ffi::PyLong_FromLong(foo * bar)
|
|
|
|
}
|
2022-06-23 20:30:22 +00:00
|
|
|
|
2024-02-05 07:51:18 +00:00
|
|
|
let py_fn = PyCFunction::new_with_keywords_bound(
|
2024-02-27 19:19:52 +00:00
|
|
|
py,
|
2022-07-19 17:34:23 +00:00
|
|
|
c_fn,
|
|
|
|
"py_fn",
|
|
|
|
"py_fn for test (this is the docstring)",
|
2024-02-27 19:19:52 +00:00
|
|
|
None,
|
2022-07-19 17:34:23 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
py_assert!(py, py_fn, "py_fn(42, kw_bar=100) == 4200");
|
|
|
|
py_assert!(py, py_fn, "py_fn(foo=42, kw_bar=100) == 4200");
|
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
py_fn,
|
|
|
|
"py_fn.__doc__ == 'py_fn for test (this is the docstring)'"
|
|
|
|
);
|
|
|
|
});
|
2022-06-23 20:30:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 19:37:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_closure() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-23 14:07:54 +00:00
|
|
|
let f = |args: &Bound<'_, types::PyTuple>,
|
|
|
|
_kwargs: Option<&Bound<'_, types::PyDict>>|
|
|
|
|
-> PyResult<_> {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let res: Vec<_> = args
|
|
|
|
.iter()
|
|
|
|
.map(|elem| {
|
|
|
|
if let Ok(i) = elem.extract::<i64>() {
|
|
|
|
(i + 1).into_py(py)
|
|
|
|
} else if let Ok(f) = elem.extract::<f64>() {
|
|
|
|
(2. * f).into_py(py)
|
|
|
|
} else if let Ok(mut s) = elem.extract::<String>() {
|
|
|
|
s.push_str("-py");
|
|
|
|
s.into_py(py)
|
|
|
|
} else {
|
|
|
|
panic!("unexpected argument type for {:?}", elem)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(res)
|
2021-10-03 19:37:17 +00:00
|
|
|
})
|
2022-07-19 17:34:23 +00:00
|
|
|
};
|
2022-10-15 03:06:56 +00:00
|
|
|
let closure_py =
|
2024-02-05 07:51:18 +00:00
|
|
|
PyCFunction::new_closure_bound(py, Some("test_fn"), Some("test_fn doc"), f).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
py_assert!(py, closure_py, "closure_py(42) == [43]");
|
2022-10-15 03:06:56 +00:00
|
|
|
py_assert!(py, closure_py, "closure_py.__name__ == 'test_fn'");
|
|
|
|
py_assert!(py, closure_py, "closure_py.__doc__ == 'test_fn doc'");
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
closure_py,
|
|
|
|
"closure_py(42, 3.14, 'foo') == [43, 6.28, 'foo-py']"
|
|
|
|
);
|
|
|
|
});
|
2021-10-03 19:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_closure_counter() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let counter = std::cell::RefCell::new(0);
|
2024-02-23 14:07:54 +00:00
|
|
|
let counter_fn = move |_args: &Bound<'_, types::PyTuple>,
|
|
|
|
_kwargs: Option<&Bound<'_, types::PyDict>>|
|
|
|
|
-> PyResult<i32> {
|
|
|
|
let mut counter = counter.borrow_mut();
|
|
|
|
*counter += 1;
|
|
|
|
Ok(*counter)
|
|
|
|
};
|
2024-02-05 07:51:18 +00:00
|
|
|
let counter_py = PyCFunction::new_closure_bound(py, None, None, counter_fn).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
py_assert!(py, counter_py, "counter_py() == 1");
|
|
|
|
py_assert!(py, counter_py, "counter_py() == 2");
|
|
|
|
py_assert!(py, counter_py, "counter_py() == 3");
|
|
|
|
});
|
2021-10-03 19:37:17 +00:00
|
|
|
}
|
2022-01-06 23:21:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn use_pyfunction() {
|
|
|
|
mod function_in_module {
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
pub fn foo(x: i32) -> i32 {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
use function_in_module::foo;
|
|
|
|
|
|
|
|
// check imported name can be wrapped
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(foo, py).unwrap();
|
2022-01-06 23:21:06 +00:00
|
|
|
assert_eq!(f.call1((5,)).unwrap().extract::<i32>().unwrap(), 5);
|
|
|
|
assert_eq!(f.call1((42,)).unwrap().extract::<i32>().unwrap(), 42);
|
|
|
|
|
|
|
|
// check path import can be wrapped
|
2024-03-12 22:57:03 +00:00
|
|
|
let f2 = wrap_pyfunction_bound!(function_in_module::foo, py).unwrap();
|
2022-01-06 23:21:06 +00:00
|
|
|
assert_eq!(f2.call1((5,)).unwrap().extract::<i32>().unwrap(), 5);
|
|
|
|
assert_eq!(f2.call1((42,)).unwrap().extract::<i32>().unwrap(), 42);
|
|
|
|
})
|
|
|
|
}
|
2022-01-10 22:45:32 +00:00
|
|
|
|
2023-05-06 10:42:05 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct Key(String);
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct Value(i32);
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn return_value_borrows_from_arguments<'py>(
|
|
|
|
py: Python<'py>,
|
|
|
|
key: &'py Key,
|
|
|
|
value: &'py Value,
|
|
|
|
) -> HashMap<&'py str, i32> {
|
|
|
|
py.allow_threads(move || {
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(key.0.as_str(), value.0);
|
|
|
|
map
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_value_borrows_from_arguments() {
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let function = wrap_pyfunction_bound!(return_value_borrows_from_arguments, py).unwrap();
|
2023-05-06 10:42:05 +00:00
|
|
|
|
|
|
|
let key = Py::new(py, Key("key".to_owned())).unwrap();
|
|
|
|
let value = Py::new(py, Value(42)).unwrap();
|
|
|
|
|
|
|
|
py_assert!(py, function key value, "function(key, value) == { \"key\": 42 }");
|
|
|
|
});
|
|
|
|
}
|
2023-09-20 07:38:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_some_wrap_arguments() {
|
|
|
|
// https://github.com/PyO3/pyo3/issues/3460
|
|
|
|
const NONE: Option<u8> = None;
|
|
|
|
#[pyfunction(signature = (a = 1, b = Some(2), c = None, d = NONE))]
|
|
|
|
fn some_wrap_arguments(
|
|
|
|
a: Option<u8>,
|
|
|
|
b: Option<u8>,
|
|
|
|
c: Option<u8>,
|
|
|
|
d: Option<u8>,
|
|
|
|
) -> [Option<u8>; 4] {
|
|
|
|
[a, b, c, d]
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let function = wrap_pyfunction_bound!(some_wrap_arguments, py).unwrap();
|
2023-09-20 07:38:34 +00:00
|
|
|
py_assert!(py, function, "function() == [1, 2, None, None]");
|
|
|
|
})
|
|
|
|
}
|
2024-01-29 13:31:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reference_to_bound_arguments() {
|
|
|
|
#[pyfunction]
|
|
|
|
fn reference_args<'py>(
|
|
|
|
x: &Bound<'py, PyAny>,
|
|
|
|
y: Option<&Bound<'py, PyAny>>,
|
|
|
|
) -> PyResult<Bound<'py, PyAny>> {
|
|
|
|
y.map_or_else(|| Ok(x.clone()), |y| y.add(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let function = wrap_pyfunction_bound!(reference_args, py).unwrap();
|
2024-01-29 13:31:21 +00:00
|
|
|
py_assert!(py, function, "function(1) == 1");
|
|
|
|
py_assert!(py, function, "function(1, 2) == 3");
|
|
|
|
})
|
|
|
|
}
|