2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2020-12-13 22:51:19 +00:00
|
|
|
use pyo3::exceptions::PyValueError;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
2023-07-02 21:26:31 +00:00
|
|
|
use pyo3::sync::GILOnceCell;
|
2022-11-19 06:33:43 +00:00
|
|
|
use pyo3::types::IntoPyDict;
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-09-02 21:33:45 +00:00
|
|
|
struct EmptyClassWithNew {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl EmptyClassWithNew {
|
2019-02-23 17:38:00 +00:00
|
|
|
#[new]
|
2019-12-14 14:16:39 +00:00
|
|
|
fn new() -> EmptyClassWithNew {
|
|
|
|
EmptyClassWithNew {}
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_class_with_new() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<EmptyClassWithNew>();
|
2022-07-19 17:34:23 +00:00
|
|
|
assert!(typeobj
|
|
|
|
.call((), None)
|
|
|
|
.unwrap()
|
2024-03-03 14:47:25 +00:00
|
|
|
.downcast::<EmptyClassWithNew>()
|
2022-07-19 17:34:23 +00:00
|
|
|
.is_ok());
|
2022-11-19 06:33:43 +00:00
|
|
|
|
|
|
|
// Calling with arbitrary args or kwargs is not ok
|
|
|
|
assert!(typeobj.call(("some", "args"), None).is_err());
|
|
|
|
assert!(typeobj
|
2024-02-18 18:27:19 +00:00
|
|
|
.call((), Some(&[("some", "kwarg")].into_py_dict_bound(py)))
|
2022-11-19 06:33:43 +00:00
|
|
|
.is_err());
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 16:04:25 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct UnitClassWithNew;
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl UnitClassWithNew {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unit_class_with_new() {
|
2021-03-18 13:09:46 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<UnitClassWithNew>();
|
2021-03-18 13:09:46 +00:00
|
|
|
assert!(typeobj
|
|
|
|
.call((), None)
|
|
|
|
.unwrap()
|
2024-03-03 14:47:25 +00:00
|
|
|
.downcast::<UnitClassWithNew>()
|
2021-03-18 13:09:46 +00:00
|
|
|
.is_ok());
|
|
|
|
});
|
2021-03-17 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct TupleClassWithNew(i32);
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl TupleClassWithNew {
|
|
|
|
#[new]
|
|
|
|
fn new(arg: i32) -> Self {
|
|
|
|
Self(arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tuple_class_with_new() {
|
2021-03-18 13:09:46 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<TupleClassWithNew>();
|
2021-03-18 13:09:46 +00:00
|
|
|
let wrp = typeobj.call((42,), None).unwrap();
|
2024-02-18 18:27:19 +00:00
|
|
|
let obj = wrp.downcast::<TupleClassWithNew>().unwrap();
|
2021-03-18 13:09:46 +00:00
|
|
|
let obj_ref = obj.borrow();
|
|
|
|
assert_eq!(obj_ref.0, 42);
|
|
|
|
});
|
2021-03-17 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2019-12-14 14:16:39 +00:00
|
|
|
#[derive(Debug)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct NewWithOneArg {
|
2023-07-14 12:42:35 +00:00
|
|
|
data: i32,
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl NewWithOneArg {
|
|
|
|
#[new]
|
2019-12-14 14:16:39 +00:00
|
|
|
fn new(arg: i32) -> NewWithOneArg {
|
2023-07-14 12:42:35 +00:00
|
|
|
NewWithOneArg { data: arg }
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_with_one_arg() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<NewWithOneArg>();
|
2022-07-19 17:34:23 +00:00
|
|
|
let wrp = typeobj.call((42,), None).unwrap();
|
2024-02-18 18:27:19 +00:00
|
|
|
let obj = wrp.downcast::<NewWithOneArg>().unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
let obj_ref = obj.borrow();
|
2023-07-14 12:42:35 +00:00
|
|
|
assert_eq!(obj_ref.data, 42);
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct NewWithTwoArgs {
|
2023-07-14 12:42:35 +00:00
|
|
|
data1: i32,
|
|
|
|
data2: i32,
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl NewWithTwoArgs {
|
|
|
|
#[new]
|
2019-12-14 14:16:39 +00:00
|
|
|
fn new(arg1: i32, arg2: i32) -> Self {
|
|
|
|
NewWithTwoArgs {
|
2023-07-14 12:42:35 +00:00
|
|
|
data1: arg1,
|
|
|
|
data2: arg2,
|
2019-12-14 14:16:39 +00:00
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_with_two_args() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<NewWithTwoArgs>();
|
2022-07-19 17:34:23 +00:00
|
|
|
let wrp = typeobj
|
|
|
|
.call((10, 20), None)
|
2023-07-21 13:30:02 +00:00
|
|
|
.map_err(|e| e.display(py))
|
2022-07-19 17:34:23 +00:00
|
|
|
.unwrap();
|
2024-02-18 18:27:19 +00:00
|
|
|
let obj = wrp.downcast::<NewWithTwoArgs>().unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
let obj_ref = obj.borrow();
|
2023-07-14 12:42:35 +00:00
|
|
|
assert_eq!(obj_ref.data1, 10);
|
|
|
|
assert_eq!(obj_ref.data2, 20);
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2018-06-15 19:21:12 +00:00
|
|
|
}
|
2020-06-21 14:38:26 +00:00
|
|
|
|
|
|
|
#[pyclass(subclass)]
|
|
|
|
struct SuperClass {
|
|
|
|
#[pyo3(get)]
|
|
|
|
from_rust: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl SuperClass {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
SuperClass { from_rust: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that `subclass.__new__` works correctly.
|
|
|
|
/// See https://github.com/PyO3/pyo3/issues/947 for the corresponding bug.
|
|
|
|
#[test]
|
|
|
|
fn subclass_new() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let super_cls = py.get_type_bound::<SuperClass>();
|
2022-07-19 17:34:23 +00:00
|
|
|
let source = pyo3::indoc::indoc!(
|
|
|
|
r#"
|
2020-06-21 14:38:26 +00:00
|
|
|
class Class(SuperClass):
|
|
|
|
def __new__(cls):
|
|
|
|
return super().__new__(cls) # This should return an instance of Class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def from_rust(self):
|
|
|
|
return False
|
|
|
|
c = Class()
|
|
|
|
assert c.from_rust is False
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
2024-02-22 09:35:47 +00:00
|
|
|
let globals = PyModule::import_bound(py, "__main__").unwrap().dict();
|
2022-07-19 17:34:23 +00:00
|
|
|
globals.set_item("SuperClass", super_cls).unwrap();
|
2024-02-05 18:44:00 +00:00
|
|
|
py.run_bound(source, Some(&globals), None)
|
2023-07-21 13:30:02 +00:00
|
|
|
.map_err(|e| e.display(py))
|
2022-07-19 17:34:23 +00:00
|
|
|
.unwrap();
|
|
|
|
});
|
2020-06-21 14:38:26 +00:00
|
|
|
}
|
2020-12-13 22:51:19 +00:00
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct NewWithCustomError {}
|
|
|
|
|
|
|
|
struct CustomError;
|
|
|
|
|
|
|
|
impl From<CustomError> for PyErr {
|
|
|
|
fn from(_error: CustomError) -> PyErr {
|
|
|
|
PyValueError::new_err("custom error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl NewWithCustomError {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Result<NewWithCustomError, CustomError> {
|
|
|
|
Err(CustomError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_with_custom_error() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<NewWithCustomError>();
|
2022-07-19 17:34:23 +00:00
|
|
|
let err = typeobj.call0().unwrap_err();
|
|
|
|
assert_eq!(err.to_string(), "ValueError: custom error");
|
|
|
|
});
|
2020-12-13 22:51:19 +00:00
|
|
|
}
|
2023-07-02 21:26:31 +00:00
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct NewExisting {
|
|
|
|
#[pyo3(get)]
|
|
|
|
num: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl NewExisting {
|
|
|
|
#[new]
|
|
|
|
fn new(py: pyo3::Python<'_>, val: usize) -> pyo3::Py<NewExisting> {
|
|
|
|
static PRE_BUILT: GILOnceCell<[pyo3::Py<NewExisting>; 2]> = GILOnceCell::new();
|
|
|
|
let existing = PRE_BUILT.get_or_init(py, || {
|
|
|
|
[
|
2024-02-20 07:45:47 +00:00
|
|
|
pyo3::Py::new(py, NewExisting { num: 0 }).unwrap(),
|
|
|
|
pyo3::Py::new(py, NewExisting { num: 1 }).unwrap(),
|
2023-07-02 21:26:31 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
if val < existing.len() {
|
|
|
|
return existing[val].clone_ref(py);
|
|
|
|
}
|
|
|
|
|
2024-02-20 07:45:47 +00:00
|
|
|
pyo3::Py::new(py, NewExisting { num: val }).unwrap()
|
2023-07-02 21:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_existing() {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let typeobj = py.get_type_bound::<NewExisting>();
|
2023-07-02 21:26:31 +00:00
|
|
|
|
|
|
|
let obj1 = typeobj.call1((0,)).unwrap();
|
|
|
|
let obj2 = typeobj.call1((0,)).unwrap();
|
|
|
|
let obj3 = typeobj.call1((1,)).unwrap();
|
|
|
|
let obj4 = typeobj.call1((1,)).unwrap();
|
|
|
|
let obj5 = typeobj.call1((2,)).unwrap();
|
|
|
|
let obj6 = typeobj.call1((2,)).unwrap();
|
|
|
|
|
|
|
|
assert!(obj1.getattr("num").unwrap().extract::<u32>().unwrap() == 0);
|
|
|
|
assert!(obj2.getattr("num").unwrap().extract::<u32>().unwrap() == 0);
|
|
|
|
assert!(obj3.getattr("num").unwrap().extract::<u32>().unwrap() == 1);
|
|
|
|
assert!(obj4.getattr("num").unwrap().extract::<u32>().unwrap() == 1);
|
|
|
|
assert!(obj5.getattr("num").unwrap().extract::<u32>().unwrap() == 2);
|
|
|
|
assert!(obj6.getattr("num").unwrap().extract::<u32>().unwrap() == 2);
|
|
|
|
|
2024-02-18 18:27:19 +00:00
|
|
|
assert!(obj1.is(&obj2));
|
|
|
|
assert!(obj3.is(&obj4));
|
|
|
|
assert!(!obj1.is(&obj3));
|
|
|
|
assert!(!obj1.is(&obj5));
|
|
|
|
assert!(!obj5.is(&obj6));
|
2023-07-02 21:26:31 +00:00
|
|
|
});
|
|
|
|
}
|
2024-05-28 01:49:52 +00:00
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct NewReturnsPy;
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl NewReturnsPy {
|
|
|
|
#[new]
|
|
|
|
fn new(py: Python<'_>) -> PyResult<Py<NewReturnsPy>> {
|
|
|
|
Py::new(py, NewReturnsPy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_returns_py() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let type_ = py.get_type_bound::<NewReturnsPy>();
|
|
|
|
let obj = type_.call0().unwrap();
|
|
|
|
assert!(obj.is_exact_instance_of::<NewReturnsPy>());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct NewReturnsBound;
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl NewReturnsBound {
|
|
|
|
#[new]
|
|
|
|
fn new(py: Python<'_>) -> PyResult<Bound<'_, NewReturnsBound>> {
|
|
|
|
Bound::new(py, NewReturnsBound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_returns_bound() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let type_ = py.get_type_bound::<NewReturnsBound>();
|
|
|
|
let obj = type_.call0().unwrap();
|
|
|
|
assert!(obj.is_exact_instance_of::<NewReturnsBound>());
|
|
|
|
})
|
|
|
|
}
|