2018-08-19 18:42:17 +00:00
|
|
|
#![feature(specialization)]
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
extern crate pyo3;
|
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::PyRawObject;
|
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 {
|
|
|
|
#[__new__]
|
|
|
|
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
2018-09-02 21:33:45 +00:00
|
|
|
obj.init(|_| 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-06-15 19:21:12 +00:00
|
|
|
assert!(
|
|
|
|
typeobj
|
2018-08-25 18:47:30 +00:00
|
|
|
.call(NoArgs, None)
|
2018-06-15 19:21:12 +00:00
|
|
|
.unwrap()
|
|
|
|
.cast_as::<EmptyClassWithNew>()
|
|
|
|
.is_ok()
|
|
|
|
);
|
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 NewWithOneArg {
|
|
|
|
_data: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl NewWithOneArg {
|
|
|
|
#[new]
|
|
|
|
fn __new__(obj: &PyRawObject, arg: i32) -> PyResult<()> {
|
2018-09-02 21:33:45 +00:00
|
|
|
obj.init(|_| 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();
|
2018-05-02 18:49:40 +00:00
|
|
|
let obj = wrp.cast_as::<NewWithOneArg>().unwrap();
|
|
|
|
assert_eq!(obj._data, 42);
|
|
|
|
}
|
|
|
|
|
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]
|
2018-06-15 19:21:12 +00:00
|
|
|
fn __new__(obj: &PyRawObject, arg1: i32, arg2: i32) -> PyResult<()> {
|
2018-09-02 21:33:45 +00:00
|
|
|
obj.init(|_| NewWithTwoArgs {
|
2018-06-15 19:21:12 +00:00
|
|
|
_data1: arg1,
|
|
|
|
_data2: arg2,
|
|
|
|
})
|
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();
|
2018-05-02 18:49:40 +00:00
|
|
|
let obj = wrp.cast_as::<NewWithTwoArgs>().unwrap();
|
|
|
|
assert_eq!(obj._data1, 10);
|
|
|
|
assert_eq!(obj._data2, 20);
|
2018-06-15 19:21:12 +00:00
|
|
|
}
|