2016-03-16 20:49:45 +00:00
|
|
|
#![allow(dead_code, unused_variables)]
|
2016-03-12 19:32:19 +00:00
|
|
|
|
2016-03-09 00:07:50 +00:00
|
|
|
#[macro_use] extern crate cpython;
|
|
|
|
|
2016-03-16 20:36:18 +00:00
|
|
|
use cpython::{PyResult, Python, NoArgs, ObjectProtocol, PyDict};
|
2016-03-09 00:07:50 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
py_class!(class EmptyClass |py| { });
|
|
|
|
|
2016-03-09 00:07:50 +00:00
|
|
|
#[test]
|
|
|
|
fn empty_class() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2016-03-12 19:32:19 +00:00
|
|
|
let typeobj = py.get_type::<EmptyClass>();
|
2016-03-09 00:07:50 +00:00
|
|
|
// By default, don't allow creating instances from python.
|
|
|
|
assert!(typeobj.call(py, NoArgs, None).is_err());
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
py_class!(class EmptyClassWithNew |py| {
|
|
|
|
def __new__(_cls) -> PyResult<EmptyClassWithNew> {
|
|
|
|
EmptyClassWithNew::create_instance(py)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-09 00:07:50 +00:00
|
|
|
#[test]
|
|
|
|
fn empty_class_with_new() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2016-03-12 19:32:19 +00:00
|
|
|
let typeobj = py.get_type::<EmptyClassWithNew>();
|
|
|
|
assert!(typeobj.call(py, NoArgs, None).unwrap().cast_into::<EmptyClassWithNew>(py).is_ok());
|
2016-03-09 00:07:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
py_class!(class NewWithOneArg |py| {
|
|
|
|
data _data: i32;
|
|
|
|
def __new__(_cls, arg: i32) -> PyResult<NewWithOneArg> {
|
|
|
|
NewWithOneArg::create_instance(py, arg)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-12 00:31:06 +00:00
|
|
|
#[test]
|
|
|
|
fn new_with_one_arg() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2016-03-12 19:32:19 +00:00
|
|
|
let typeobj = py.get_type::<NewWithOneArg>();
|
|
|
|
let obj = typeobj.call(py, (42,), None).unwrap().cast_into::<NewWithOneArg>(py).unwrap();
|
2016-03-12 00:31:06 +00:00
|
|
|
assert_eq!(*obj._data(py), 42);
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
py_class!(class NewWithTwoArgs |py| {
|
|
|
|
data _data1: i32;
|
|
|
|
data _data2: i32;
|
|
|
|
def __new__(_cls, arg1: i32, arg2: i32) -> PyResult<NewWithTwoArgs> {
|
|
|
|
NewWithTwoArgs::create_instance(py, arg1, arg2)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-12 00:31:06 +00:00
|
|
|
#[test]
|
|
|
|
fn new_with_two_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2016-03-12 19:32:19 +00:00
|
|
|
let typeobj = py.get_type::<NewWithTwoArgs>();
|
|
|
|
let obj = typeobj.call(py, (10, 20), None).unwrap().cast_into::<NewWithTwoArgs>(py).unwrap();
|
2016-03-12 00:31:06 +00:00
|
|
|
assert_eq!(*obj._data1(py), 10);
|
|
|
|
assert_eq!(*obj._data2(py), 20);
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
struct TestDropCall {
|
|
|
|
drop_called: Arc<AtomicBool>
|
|
|
|
}
|
|
|
|
impl Drop for TestDropCall {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.drop_called.store(true, Ordering::Relaxed);
|
2016-03-09 00:07:50 +00:00
|
|
|
}
|
2016-03-12 19:32:19 +00:00
|
|
|
}
|
2016-03-09 00:07:50 +00:00
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
py_class!(class DataIsDropped |py| {
|
|
|
|
data member1: TestDropCall;
|
|
|
|
data member2: TestDropCall;
|
|
|
|
});
|
2016-03-09 00:07:50 +00:00
|
|
|
|
2016-03-12 19:32:19 +00:00
|
|
|
#[test]
|
|
|
|
fn data_is_dropped() {
|
2016-03-09 00:07:50 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let drop_called1 = Arc::new(AtomicBool::new(false));
|
|
|
|
let drop_called2 = Arc::new(AtomicBool::new(false));
|
2016-03-12 19:32:19 +00:00
|
|
|
let inst = DataIsDropped::create_instance(py,
|
|
|
|
TestDropCall { drop_called: drop_called1.clone() },
|
|
|
|
TestDropCall { drop_called: drop_called2.clone() });
|
2016-03-09 00:07:50 +00:00
|
|
|
assert!(drop_called1.load(Ordering::Relaxed) == false);
|
|
|
|
assert!(drop_called2.load(Ordering::Relaxed) == false);
|
|
|
|
drop(inst);
|
|
|
|
assert!(drop_called1.load(Ordering::Relaxed) == true);
|
|
|
|
assert!(drop_called2.load(Ordering::Relaxed) == true);
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:36:18 +00:00
|
|
|
py_class!(class InstanceMethod |py| {
|
|
|
|
data member: i32;
|
|
|
|
|
|
|
|
def method(&self) -> PyResult<i32> {
|
|
|
|
Ok(*self.member(py))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instance_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let obj = InstanceMethod::create_instance(py, 42).unwrap();
|
|
|
|
assert!(obj.method(py).unwrap() == 42);
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "obj", obj).unwrap();
|
|
|
|
py.run("assert obj.method() == 42", None, Some(&d)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
py_class!(class InstanceMethodWithArgs |py| {
|
|
|
|
data member: i32;
|
|
|
|
|
|
|
|
def method(&self, multiplier: i32) -> PyResult<i32> {
|
|
|
|
Ok(*self.member(py) * multiplier)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instance_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let obj = InstanceMethodWithArgs::create_instance(py, 7).unwrap();
|
|
|
|
assert!(obj.method(py, 6).unwrap() == 42);
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "obj", obj).unwrap();
|
|
|
|
py.run("assert obj.method(3) == 21", None, Some(&d)).unwrap();
|
|
|
|
py.run("assert obj.method(multiplier=6) == 42", None, Some(&d)).unwrap();
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:49:45 +00:00
|
|
|
py_class!(class StaticMethod |py| {
|
|
|
|
@staticmethod
|
|
|
|
def method() -> PyResult<&'static str> {
|
|
|
|
Ok("StaticMethod.method()!")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
assert_eq!(StaticMethod::method(py).unwrap(), "StaticMethod.method()!");
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "C", py.get_type::<StaticMethod>()).unwrap();
|
|
|
|
py.run("assert C.method() == 'StaticMethod.method()!'", None, Some(&d)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
py_class!(class StaticMethodWithArgs |py| {
|
|
|
|
@staticmethod
|
|
|
|
def method(input: i32) -> PyResult<String> {
|
|
|
|
Ok(format!("0x{:x}", input))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
assert_eq!(StaticMethodWithArgs::method(py, 1234).unwrap(), "0x4d2");
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "C", py.get_type::<StaticMethodWithArgs>()).unwrap();
|
|
|
|
py.run("assert C.method(1337) == '0x539'", None, Some(&d)).unwrap();
|
|
|
|
}
|
|
|
|
|