2020-12-13 22:51:19 +00:00
|
|
|
use pyo3::exceptions::PyValueError;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
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() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<EmptyClassWithNew>();
|
2018-10-31 10:38:45 +00:00
|
|
|
assert!(typeobj
|
2019-02-23 17:01:22 +00:00
|
|
|
.call((), None)
|
2018-10-31 10:38:45 +00:00
|
|
|
.unwrap()
|
2020-02-15 04:42:25 +00:00
|
|
|
.cast_as::<PyCell<EmptyClassWithNew>>()
|
2018-10-31 10:38:45 +00:00
|
|
|
.is_ok());
|
2018-05-02 18:49:40 +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 {
|
|
|
|
_data: i32,
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
NewWithOneArg { _data: arg }
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_with_one_arg() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<NewWithOneArg>();
|
2018-08-25 18:47:30 +00:00
|
|
|
let wrp = typeobj.call((42,), None).unwrap();
|
2020-02-15 04:42:25 +00:00
|
|
|
let obj = wrp.cast_as::<PyCell<NewWithOneArg>>().unwrap();
|
|
|
|
let obj_ref = obj.borrow();
|
|
|
|
assert_eq!(obj_ref._data, 42);
|
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 {
|
|
|
|
_data1: i32,
|
|
|
|
_data2: i32,
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-15 19:21:12 +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() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<NewWithTwoArgs>();
|
2018-06-15 19:21:12 +00:00
|
|
|
let wrp = typeobj
|
2018-08-25 18:47:30 +00:00
|
|
|
.call((10, 20), None)
|
2018-06-15 19:21:12 +00:00
|
|
|
.map_err(|e| e.print(py))
|
|
|
|
.unwrap();
|
2020-02-15 04:42:25 +00:00
|
|
|
let obj = wrp.cast_as::<PyCell<NewWithTwoArgs>>().unwrap();
|
|
|
|
let obj_ref = obj.borrow();
|
|
|
|
assert_eq!(obj_ref._data1, 10);
|
|
|
|
assert_eq!(obj_ref._data2, 20);
|
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() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let super_cls = py.get_type::<SuperClass>();
|
|
|
|
let source = pyo3::indoc::indoc!(
|
|
|
|
r#"
|
|
|
|
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
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
let globals = PyModule::import(py, "__main__").unwrap().dict();
|
|
|
|
globals.set_item("SuperClass", super_cls).unwrap();
|
|
|
|
py.run(source, Some(globals), None)
|
|
|
|
.map_err(|e| e.print(py))
|
|
|
|
.unwrap();
|
|
|
|
}
|
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() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<NewWithCustomError>();
|
|
|
|
let err = typeobj.call0().unwrap_err();
|
|
|
|
assert_eq!(err.to_string(), "ValueError: custom error");
|
|
|
|
}
|