2017-05-20 17:40:33 +00:00
|
|
|
#![feature(proc_macro, specialization)]
|
2016-03-16 20:49:45 +00:00
|
|
|
#![allow(dead_code, unused_variables)]
|
2016-03-12 19:32:19 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
extern crate pyo3;
|
2016-03-09 00:07:50 +00:00
|
|
|
|
2017-05-13 05:43:17 +00:00
|
|
|
use pyo3::*;
|
2017-06-08 20:43:24 +00:00
|
|
|
use std::{isize, iter};
|
2017-05-20 17:40:33 +00:00
|
|
|
use std::cell::RefCell;
|
2016-03-09 00:07:50 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2017-05-13 05:43:17 +00:00
|
|
|
use pyo3::ffi;
|
2017-05-27 17:49:38 +00:00
|
|
|
|
2016-04-17 21:26:33 +00:00
|
|
|
|
2016-05-07 21:01:45 +00:00
|
|
|
macro_rules! py_run {
|
|
|
|
($py:expr, $val:ident, $code:expr) => {{
|
2017-05-29 09:47:27 +00:00
|
|
|
let d = PyDict::new($py);
|
2017-06-03 01:58:16 +00:00
|
|
|
d.set_item($py, stringify!($val), &$val).unwrap();
|
2016-05-07 21:01:45 +00:00
|
|
|
$py.run($code, None, Some(&d)).expect($code);
|
2016-04-17 21:26:33 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2016-05-07 21:01:45 +00:00
|
|
|
macro_rules! py_assert {
|
|
|
|
($py:expr, $val:ident, $assertion:expr) => { py_run!($py, $val, concat!("assert ", $assertion)) };
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:26:33 +00:00
|
|
|
macro_rules! py_expect_exception {
|
|
|
|
($py:expr, $val:ident, $code:expr, $err:ident) => {{
|
2017-05-29 09:47:27 +00:00
|
|
|
let d = PyDict::new($py);
|
2017-06-03 01:58:16 +00:00
|
|
|
d.set_item($py, stringify!($val), &$val).unwrap();
|
2016-05-07 21:01:45 +00:00
|
|
|
let res = $py.run($code, None, Some(&d));
|
2016-04-17 21:26:33 +00:00
|
|
|
let err = res.unwrap_err();
|
2017-05-29 09:47:27 +00:00
|
|
|
if !err.matches($py, $py.get_type::<exc::$err>()) {
|
2016-05-07 21:01:45 +00:00
|
|
|
panic!(format!("Expected {} but got {:?}", stringify!($err), err))
|
|
|
|
}
|
2016-04-17 21:26:33 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct EmptyClass { }
|
2016-03-12 19:32:19 +00:00
|
|
|
|
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.
|
2017-06-03 01:58:16 +00:00
|
|
|
assert!(typeobj.call(py, NoArgs, None).is_err());
|
2017-02-19 15:15:33 +00:00
|
|
|
|
|
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'EmptyClass'");
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:15:26 +00:00
|
|
|
/// Line1
|
2017-06-13 04:15:44 +00:00
|
|
|
///Line2
|
|
|
|
/// Line3
|
|
|
|
// this is not doc string
|
|
|
|
#[py::class]
|
2017-06-13 00:15:26 +00:00
|
|
|
struct ClassWithDocs { }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_docstr() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<ClassWithDocs>();
|
2017-06-13 04:15:44 +00:00
|
|
|
py_run!(py, typeobj, "assert typeobj.__doc__ == 'Line1\\nLine2\\n Line3'");
|
2017-06-13 00:15:26 +00:00
|
|
|
}
|
2017-06-09 19:35:29 +00:00
|
|
|
|
|
|
|
#[py::class(name=CustomName)]
|
|
|
|
struct EmptyClass2 { }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_class_name() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<EmptyClass2>();
|
|
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'");
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct EmptyClassInModule { }
|
2017-02-19 15:15:33 +00:00
|
|
|
|
2017-05-30 05:59:03 +00:00
|
|
|
#[test]
|
2017-02-19 15:15:33 +00:00
|
|
|
fn empty_class_in_module() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2017-05-29 09:47:27 +00:00
|
|
|
let module = PyModule::new(py, "test_module.nested").unwrap();
|
2017-06-03 01:58:16 +00:00
|
|
|
module.add_class::<EmptyClassInModule>(py).unwrap();
|
2017-02-19 15:15:33 +00:00
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
let ty = module.getattr(py, "EmptyClassInModule").unwrap();
|
|
|
|
assert_eq!(ty.getattr(py, "__name__").unwrap().extract::<String>(py).unwrap(), "EmptyClassInModule");
|
|
|
|
assert_eq!(ty.getattr(py, "__module__").unwrap().extract::<String>(py).unwrap(), "test_module.nested");
|
2016-03-09 00:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
2017-06-01 03:50:40 +00:00
|
|
|
struct EmptyClassWithNew {
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-06-01 03:50:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl EmptyClassWithNew {
|
|
|
|
#[__new__]
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<EmptyClassWithNew>> {
|
|
|
|
Py::new(py, |t| EmptyClassWithNew{token: t})
|
2016-03-12 19:32:19 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-12 19:32:19 +00:00
|
|
|
|
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>();
|
2017-06-03 01:58:16 +00:00
|
|
|
assert!(typeobj.call(py, NoArgs, None).unwrap().cast_as::<EmptyClassWithNew>(py).is_ok());
|
2016-03-09 00:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct NewWithOneArg {
|
|
|
|
_data: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2017-06-01 03:50:40 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl NewWithOneArg {
|
|
|
|
#[new]
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __new__(_cls: &PyType, py: Python, arg: i32) -> PyResult<Py<NewWithOneArg>> {
|
|
|
|
Py::new(py, |t| NewWithOneArg{_data: arg, token: t})
|
2016-03-12 19:32:19 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-12 19:32:19 +00:00
|
|
|
|
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>();
|
2017-06-03 01:58:16 +00:00
|
|
|
let wrp = typeobj.call(py, (42,), None).unwrap();
|
|
|
|
let obj = wrp.cast_as::<NewWithOneArg>(py).unwrap();
|
2017-05-25 05:43:07 +00:00
|
|
|
assert_eq!(obj._data, 42);
|
2016-03-12 00:31:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct NewWithTwoArgs {
|
|
|
|
_data1: i32,
|
|
|
|
_data2: i32,
|
2017-06-01 03:50:40 +00:00
|
|
|
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[py::methods]
|
|
|
|
impl NewWithTwoArgs {
|
|
|
|
#[new]
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __new__(_cls: &PyType, py: Python, arg1: i32, arg2: i32) -> PyResult<Py<NewWithTwoArgs>>
|
2017-06-20 03:55:07 +00:00
|
|
|
{
|
2017-06-20 06:57:34 +00:00
|
|
|
Py::new(py, |t| NewWithTwoArgs{_data1: arg1, _data2: arg2, token: t})
|
2016-03-12 19:32:19 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-12 19:32:19 +00:00
|
|
|
|
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>();
|
2017-06-03 01:58:16 +00:00
|
|
|
let wrp = typeobj.call(py, (10, 20), None).unwrap();
|
|
|
|
let obj = wrp.cast_as::<NewWithTwoArgs>(py).unwrap();
|
2017-05-25 05:43:07 +00:00
|
|
|
assert_eq!(obj._data1, 10);
|
|
|
|
assert_eq!(obj._data2, 20);
|
2016-03-12 00:31:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 21:27:37 +00:00
|
|
|
#[py::class(freelist=10)]
|
|
|
|
struct ClassWithFreelist{token: PyToken}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_freelist() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
|
|
|
let inst2 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
2017-06-09 21:27:37 +00:00
|
|
|
let ptr = inst.as_ptr();
|
|
|
|
drop(inst);
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst3 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap();
|
2017-06-09 21:27:37 +00:00
|
|
|
assert_eq!(ptr, inst3.as_ptr());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct DataIsDropped {
|
|
|
|
member1: TestDropCall,
|
|
|
|
member2: TestDropCall,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-09 00:07:50 +00:00
|
|
|
|
2017-05-30 05:59:03 +00:00
|
|
|
#[test]
|
2016-03-12 19:32:19 +00:00
|
|
|
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));
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| DataIsDropped{
|
2017-05-25 05:43:07 +00:00
|
|
|
member1: TestDropCall { drop_called: drop_called1.clone() },
|
2017-05-27 18:43:45 +00:00
|
|
|
member2: TestDropCall { drop_called: drop_called2.clone() },
|
|
|
|
token: t
|
2017-06-03 01:58:16 +00:00
|
|
|
}).unwrap();
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-25 05:43:07 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct InstanceMethod {
|
|
|
|
member: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-16 20:36:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl InstanceMethod {
|
2017-06-13 04:08:59 +00:00
|
|
|
/// Test method
|
2017-05-27 17:49:38 +00:00
|
|
|
fn method(&self, py: Python) -> PyResult<i32> {
|
2017-05-25 05:43:07 +00:00
|
|
|
Ok(self.member)
|
2016-03-16 20:36:18 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-16 20:36:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instance_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let obj = Py::new(py, |t| InstanceMethod{member: 42, token: t}).unwrap();
|
2017-06-04 00:27:26 +00:00
|
|
|
assert!(obj.as_ref(py).method(py).unwrap() == 42);
|
2016-03-16 20:36:18 +00:00
|
|
|
let d = PyDict::new(py);
|
2017-06-03 01:58:16 +00:00
|
|
|
d.set_item(py, "obj", obj).unwrap();
|
2016-03-16 20:36:18 +00:00
|
|
|
py.run("assert obj.method() == 42", None, Some(&d)).unwrap();
|
2017-06-13 04:15:44 +00:00
|
|
|
py.run("assert obj.method.__doc__ == 'Test method'", None, Some(&d)).unwrap();
|
2016-03-16 20:36:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct InstanceMethodWithArgs {
|
2017-05-27 18:43:45 +00:00
|
|
|
member: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2017-06-04 00:27:26 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl InstanceMethodWithArgs {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn method(&self, py: Python, multiplier: i32) -> PyResult<i32> {
|
2017-05-25 05:43:07 +00:00
|
|
|
Ok(self.member * multiplier)
|
2016-03-16 20:36:18 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-16 20:36:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instance_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let obj = Py::new(py, |t| InstanceMethodWithArgs{member: 7, token: t}).unwrap();
|
2017-06-04 00:27:26 +00:00
|
|
|
assert!(obj.as_ref(py).method(py, 6).unwrap() == 42);
|
2016-03-16 20:36:18 +00:00
|
|
|
let d = PyDict::new(py);
|
2017-06-03 01:58:16 +00:00
|
|
|
d.set_item(py, "obj", obj).unwrap();
|
2016-03-16 20:36:18 +00:00
|
|
|
py.run("assert obj.method(3) == 21", None, Some(&d)).unwrap();
|
|
|
|
py.run("assert obj.method(multiplier=6) == 42", None, Some(&d)).unwrap();
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
2017-06-08 18:29:40 +00:00
|
|
|
struct ClassMethod {token: PyToken}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl ClassMethod {
|
|
|
|
#[new]
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<ClassMethod>> {
|
|
|
|
Py::new(py, |t| ClassMethod{token: t})
|
2016-03-16 21:18:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
#[classmethod]
|
|
|
|
fn method(cls: &PyType, py: Python) -> PyResult<String> {
|
|
|
|
Ok(format!("{}.method()!", cls.name(py)))
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-16 21:18:48 +00:00
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
#[test]
|
2016-03-16 21:18:48 +00:00
|
|
|
fn class_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "C", py.get_type::<ClassMethod>()).unwrap();
|
|
|
|
py.run("assert C.method() == 'ClassMethod.method()!'", None, Some(&d)).unwrap();
|
|
|
|
py.run("assert C().method() == 'ClassMethod.method()!'", None, Some(&d)).unwrap();
|
2017-06-08 18:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[py::class]
|
|
|
|
struct ClassMethodWithArgs{token: PyToken}
|
2016-03-16 21:18:48 +00:00
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl ClassMethodWithArgs {
|
|
|
|
#[classmethod]
|
|
|
|
fn method(cls: &PyType, py: Python, input: PyString) -> PyResult<String> {
|
|
|
|
Ok(format!("{}.method({})", cls.name(py), input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_method_with_args() {
|
2016-03-16 21:18:48 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item(py, "C", py.get_type::<ClassMethodWithArgs>()).unwrap();
|
|
|
|
py.run("assert C.method('abc') == 'ClassMethodWithArgs.method(abc)'", None, Some(&d)).unwrap();
|
2017-06-08 18:29:40 +00:00
|
|
|
}
|
2016-03-16 21:18:48 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
2017-06-01 03:50:40 +00:00
|
|
|
struct StaticMethod {
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-06-01 03:50:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl StaticMethod {
|
|
|
|
#[new]
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __new__(cls: &PyType, py: Python) -> PyResult<Py<StaticMethod>> {
|
|
|
|
Py::new(py, |t| StaticMethod{token: t})
|
2016-03-16 21:18:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
#[staticmethod]
|
|
|
|
fn method(py: Python) -> PyResult<&'static str> {
|
|
|
|
Ok("StaticMethod.method()!")
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-03-16 20:49:45 +00:00
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
#[test]
|
|
|
|
fn static_method() {
|
2016-03-16 20:49:45 +00:00
|
|
|
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();
|
2016-03-16 21:18:48 +00:00
|
|
|
py.run("assert C().method() == 'StaticMethod.method()!'", None, Some(&d)).unwrap();
|
2017-06-08 18:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[py::class]
|
|
|
|
struct StaticMethodWithArgs{token: PyToken}
|
|
|
|
|
|
|
|
#[py::methods]
|
|
|
|
impl StaticMethodWithArgs {
|
|
|
|
|
|
|
|
#[staticmethod]
|
|
|
|
fn method(py: Python, 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");
|
2017-05-20 17:40:33 +00:00
|
|
|
|
2017-06-08 18:29:40 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-05-25 05:43:07 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct GCIntegration {
|
2017-06-03 01:58:16 +00:00
|
|
|
self_ref: RefCell<PyObject>,
|
2017-05-20 17:40:33 +00:00
|
|
|
dropped: TestDropCall,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyGCProtocol for GCIntegration {
|
|
|
|
fn __traverse__(&self, py: Python, visit: PyVisit) -> Result<(), PyTraverseError> {
|
2017-05-27 17:49:38 +00:00
|
|
|
visit.call(&*self.self_ref.borrow())
|
2016-04-14 14:24:23 +00:00
|
|
|
}
|
2016-04-14 16:36:34 +00:00
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __clear__(&mut self, py: Python) {
|
2017-06-03 01:58:16 +00:00
|
|
|
*self.self_ref.borrow_mut() = py.None();
|
2016-04-14 16:36:34 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-04-14 14:24:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gc_integration() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let drop_called = Arc::new(AtomicBool::new(false));
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| GCIntegration{
|
2017-06-03 01:58:16 +00:00
|
|
|
self_ref: RefCell::new(py.None()),
|
2017-05-27 18:43:45 +00:00
|
|
|
dropped: TestDropCall { drop_called: drop_called.clone() },
|
2017-06-03 01:58:16 +00:00
|
|
|
token: t}).unwrap();
|
2017-05-27 17:49:38 +00:00
|
|
|
|
2017-06-04 00:42:12 +00:00
|
|
|
*inst.as_mut(py).self_ref.borrow_mut() = inst.clone_ref(py).into();
|
2017-05-27 17:49:38 +00:00
|
|
|
drop(inst);
|
2016-04-14 14:24:23 +00:00
|
|
|
|
|
|
|
py.run("import gc; gc.collect()", None, None).unwrap();
|
|
|
|
assert!(drop_called.load(Ordering::Relaxed));
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
pub struct Len {
|
2017-05-27 18:43:45 +00:00
|
|
|
l: usize,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-04-17 21:26:33 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyMappingProtocol for Len {
|
|
|
|
fn __len__(&self, py: Python) -> PyResult<usize> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(self.l)
|
2016-04-17 21:26:33 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-04-17 21:26:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn len() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| Len{l: 10, token: t}).unwrap();
|
2016-04-17 21:26:33 +00:00
|
|
|
py_assert!(py, inst, "len(inst) == 10");
|
2016-05-06 21:16:06 +00:00
|
|
|
unsafe {
|
2017-05-27 17:49:38 +00:00
|
|
|
assert_eq!(ffi::PyObject_Size(inst.as_ptr()), 10);
|
|
|
|
assert_eq!(ffi::PyMapping_Size(inst.as_ptr()), 10);
|
2016-05-06 21:16:06 +00:00
|
|
|
}
|
2016-04-17 21:26:33 +00:00
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| Len{l: (isize::MAX as usize) + 1, token: t}).unwrap();
|
2016-04-17 21:26:33 +00:00
|
|
|
py_expect_exception!(py, inst, "len(inst)", OverflowError);
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:43:24 +00:00
|
|
|
#[py::class]
|
|
|
|
struct Iterator{
|
|
|
|
iter: Box<iter::Iterator<Item=i32> + Send>,
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyIterProtocol for Iterator {
|
2017-06-20 06:57:34 +00:00
|
|
|
fn __iter__(&mut self, py: Python) -> PyResult<Py<Iterator>> {
|
2017-06-20 03:55:07 +00:00
|
|
|
Ok(self.into())
|
2016-04-30 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 20:43:24 +00:00
|
|
|
fn __next__(&mut self, py: Python) -> PyResult<Option<i32>> {
|
|
|
|
Ok(self.iter.next())
|
2016-04-30 21:41:18 +00:00
|
|
|
}
|
2017-06-08 20:43:24 +00:00
|
|
|
}
|
2016-04-30 21:41:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iterator() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let inst = Py::new(py, |t| Iterator{iter: Box::new(5..8), token: t}).unwrap();
|
2016-04-30 21:41:18 +00:00
|
|
|
py_assert!(py, inst, "iter(inst) is inst");
|
|
|
|
py_assert!(py, inst, "list(inst) == [5, 6, 7]");
|
2017-06-08 20:43:24 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
|
|
|
|
#[py::class]
|
2017-06-01 22:06:48 +00:00
|
|
|
struct StringMethods {token: PyToken}
|
2016-04-30 21:41:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
2017-05-27 17:49:38 +00:00
|
|
|
impl<'p> PyObjectProtocol<'p> for StringMethods {
|
|
|
|
fn __str__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-05 05:31:14 +00:00
|
|
|
Ok("str")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __repr__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-05 05:31:14 +00:00
|
|
|
Ok("repr")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __format__(&self, py: Python, format_spec: String) -> PyResult<String> {
|
2016-05-06 19:24:32 +00:00
|
|
|
Ok(format!("format({})", format_spec))
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:48:57 +00:00
|
|
|
fn __unicode__(&self, py: Python) -> PyResult<PyString> {
|
|
|
|
Ok(PyString::new(py, "unicode"))
|
|
|
|
}
|
2016-05-06 19:24:32 +00:00
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __bytes__(&self, py: Python) -> PyResult<PyBytes> {
|
2016-05-06 19:24:32 +00:00
|
|
|
Ok(PyBytes::new(py, b"bytes"))
|
2016-05-05 05:31:14 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-05 05:31:14 +00:00
|
|
|
|
2017-06-12 05:23:49 +00:00
|
|
|
#[cfg(Py_3)]
|
2016-05-05 05:31:14 +00:00
|
|
|
#[test]
|
|
|
|
fn string_methods() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let obj = Py::new(py, |t| StringMethods{token: t}).unwrap();
|
2016-05-05 05:31:14 +00:00
|
|
|
py_assert!(py, obj, "str(obj) == 'str'");
|
|
|
|
py_assert!(py, obj, "repr(obj) == 'repr'");
|
|
|
|
py_assert!(py, obj, "'{0:x}'.format(obj) == 'format(x)'");
|
2016-05-06 19:24:32 +00:00
|
|
|
py_assert!(py, obj, "bytes(obj) == b'bytes'");
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
2016-05-06 19:24:32 +00:00
|
|
|
|
2017-06-12 05:23:49 +00:00
|
|
|
#[cfg(not(Py_3))]
|
|
|
|
#[test]
|
|
|
|
fn string_methods() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let obj = Py::new(py, |t| StringMethods{token: t}).unwrap();
|
2017-06-12 05:23:49 +00:00
|
|
|
py_assert!(py, obj, "str(obj) == 'str'");
|
|
|
|
py_assert!(py, obj, "repr(obj) == 'repr'");
|
2017-06-15 14:48:57 +00:00
|
|
|
py_assert!(py, obj, "unicode(obj) == 'unicode'");
|
2017-06-12 05:23:49 +00:00
|
|
|
py_assert!(py, obj, "'{0:x}'.format(obj) == 'format(x)'");
|
|
|
|
}
|
|
|
|
|
2016-05-06 19:24:32 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct Comparisons {
|
|
|
|
val: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-06 19:41:05 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyObjectProtocol for Comparisons {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __hash__(&self, py: Python) -> PyResult<usize> {
|
|
|
|
Ok(self.val as usize)
|
2016-05-06 19:41:05 +00:00
|
|
|
}
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __bool__(&self, py: Python) -> PyResult<bool> {
|
|
|
|
Ok(self.val != 0)
|
2016-05-06 20:05:12 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-06 19:41:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comparisons() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-20 06:57:34 +00:00
|
|
|
let zero = Py::new(py, |t| Comparisons{val: 0, token: t}).unwrap();
|
|
|
|
let one = Py::new(py, |t| Comparisons{val: 1, token: t}).unwrap();
|
|
|
|
let ten = Py::new(py, |t| Comparisons{val: 10, token: t}).unwrap();
|
|
|
|
let minus_one = Py::new(py, |t| Comparisons{val: -1, token: t}).unwrap();
|
2016-05-06 19:41:05 +00:00
|
|
|
py_assert!(py, one, "hash(one) == 1");
|
|
|
|
py_assert!(py, ten, "hash(ten) == 10");
|
|
|
|
py_assert!(py, minus_one, "hash(minus_one) == -2");
|
2016-05-06 20:05:12 +00:00
|
|
|
|
|
|
|
py_assert!(py, one, "bool(one) is True");
|
|
|
|
py_assert!(py, zero, "not zero");
|
2016-05-06 19:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 19:15:28 +00:00
|
|
|
#[py::class]
|
2017-05-29 22:08:19 +00:00
|
|
|
struct Sequence {
|
2017-06-04 00:27:26 +00:00
|
|
|
token: PyToken
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PySequenceProtocol for Sequence {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __len__(&self, py: Python) -> PyResult<usize> {
|
2016-05-06 22:04:18 +00:00
|
|
|
Ok(5)
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __getitem__(&self, py: Python, key: isize) -> PyResult<isize> {
|
2017-05-20 17:40:33 +00:00
|
|
|
if key == 5 {
|
|
|
|
return Err(PyErr::new::<exc::IndexError, NoArgs>(py, NoArgs));
|
2016-05-06 22:04:18 +00:00
|
|
|
}
|
|
|
|
Ok(key)
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-06 22:04:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sequence() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| Sequence{token: t}).unwrap();
|
2016-05-06 22:04:18 +00:00
|
|
|
py_assert!(py, c, "list(c) == [0, 1, 2, 3, 4]");
|
2017-05-20 19:15:28 +00:00
|
|
|
py_expect_exception!(py, c, "c['abc']", TypeError);
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
|
2016-05-06 22:04:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
2017-06-01 22:06:48 +00:00
|
|
|
struct Callable {token: PyToken}
|
2016-05-06 22:04:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::methods]
|
|
|
|
impl Callable {
|
|
|
|
|
|
|
|
#[__call__]
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __call__(&self, py: Python, arg: i32) -> PyResult<i32> {
|
2016-05-06 20:32:46 +00:00
|
|
|
Ok(arg * 6)
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-06 20:32:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn callable() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| Callable{token: t}).unwrap();
|
2016-05-06 20:32:46 +00:00
|
|
|
py_assert!(py, c, "callable(c)");
|
|
|
|
py_assert!(py, c, "c(7) == 42");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let nc = py.init(|t| Comparisons{val: 0, token: t}).unwrap();
|
2016-05-06 20:32:46 +00:00
|
|
|
py_assert!(py, nc, "not callable(nc)");
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct SetItem {
|
|
|
|
key: i32,
|
|
|
|
val: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
2017-05-25 05:43:07 +00:00
|
|
|
impl PyMappingProtocol<'a> for SetItem {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __setitem__(&mut self, py: Python, key: i32, val: i32) -> PyResult<()> {
|
|
|
|
self.key = key;
|
|
|
|
self.val = val;
|
2016-05-07 21:01:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn setitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| SetItem{key: 0, val: 0, token: t}).unwrap();
|
2016-05-07 21:01:45 +00:00
|
|
|
py_run!(py, c, "c[1] = 2");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert_eq!(c.as_ref(py).key, 1);
|
|
|
|
assert_eq!(c.as_ref(py).val, 2);
|
2016-05-07 21:01:45 +00:00
|
|
|
py_expect_exception!(py, c, "del c[1]", NotImplementedError);
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct DelItem {
|
|
|
|
key: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
2017-05-25 05:43:07 +00:00
|
|
|
impl PyMappingProtocol<'a> for DelItem {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __delitem__(&mut self, py: Python, key: i32) -> PyResult<()> {
|
|
|
|
self.key = key;
|
2016-05-07 21:01:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| DelItem{key:0, token:t}).unwrap();
|
2016-05-07 21:01:45 +00:00
|
|
|
py_run!(py, c, "del c[1]");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert_eq!(c.as_ref(py).key, 1);
|
2016-05-07 21:01:45 +00:00
|
|
|
py_expect_exception!(py, c, "c[1] = 2", NotImplementedError);
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct SetDelItem {
|
|
|
|
val: Option<i32>,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyMappingProtocol for SetDelItem {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __setitem__(&mut self, py: Python, key: i32, val: i32) -> PyResult<()> {
|
|
|
|
self.val = Some(val);
|
2016-05-07 21:01:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __delitem__(&mut self, py: Python, key: i32) -> PyResult<()> {
|
|
|
|
self.val = None;
|
2016-05-07 21:01:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:01:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn setdelitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| SetDelItem{val: None, token: t}).unwrap();
|
2016-05-07 21:01:45 +00:00
|
|
|
py_run!(py, c, "c[1] = 2");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert_eq!(c.as_ref(py).val, Some(2));
|
2016-05-07 21:01:45 +00:00
|
|
|
py_run!(py, c, "del c[1]");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert_eq!(c.as_ref(py).val, None);
|
2016-05-07 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
2017-06-01 22:06:48 +00:00
|
|
|
struct Reversed {token: PyToken}
|
2017-05-20 17:40:33 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyMappingProtocol for Reversed{
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __reversed__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 21:19:26 +00:00
|
|
|
Ok("I am reversed")
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:19:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reversed() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| Reversed{token: t}).unwrap();
|
2016-05-07 21:19:26 +00:00
|
|
|
py_run!(py, c, "assert reversed(c) == 'I am reversed'");
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:15:28 +00:00
|
|
|
#[py::class]
|
2017-06-01 22:06:48 +00:00
|
|
|
struct Contains {token: PyToken}
|
2017-05-20 17:40:33 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
2017-05-20 19:15:28 +00:00
|
|
|
impl PySequenceProtocol for Contains {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __contains__(&self, py: Python, item: i32) -> PyResult<bool> {
|
2016-05-07 21:19:26 +00:00
|
|
|
Ok(item >= 0)
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 21:19:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn contains() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| Contains{token: t}).unwrap();
|
2016-05-07 21:19:26 +00:00
|
|
|
py_run!(py, c, "assert 1 in c");
|
|
|
|
py_run!(py, c, "assert -1 not in c");
|
2017-05-20 19:15:28 +00:00
|
|
|
py_expect_exception!(py, c, "assert 'wrong type' not in c", TypeError);
|
|
|
|
}
|
2016-05-07 21:19:26 +00:00
|
|
|
|
2017-05-21 05:18:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[py::class]
|
2017-06-01 22:06:48 +00:00
|
|
|
struct UnaryArithmetic {token: PyToken}
|
2017-05-21 05:18:31 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyNumberProtocol for UnaryArithmetic {
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __neg__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 23:01:18 +00:00
|
|
|
Ok("neg")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __pos__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 23:01:18 +00:00
|
|
|
Ok("pos")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __abs__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 23:01:18 +00:00
|
|
|
Ok("abs")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __invert__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 23:01:18 +00:00
|
|
|
Ok("invert")
|
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
2016-05-07 23:01:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unary_arithmetic() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| UnaryArithmetic{token: t}).unwrap();
|
2016-05-07 23:01:18 +00:00
|
|
|
py_run!(py, c, "assert -c == 'neg'");
|
|
|
|
py_run!(py, c, "assert +c == 'pos'");
|
|
|
|
py_run!(py, c, "assert abs(c) == 'abs'");
|
|
|
|
py_run!(py, c, "assert ~c == 'invert'");
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
|
|
|
|
#[py::class]
|
2017-05-28 15:57:34 +00:00
|
|
|
struct BinaryArithmetic {
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-28 15:57:34 +00:00
|
|
|
}
|
2016-05-07 23:01:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyObjectProtocol for BinaryArithmetic {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __repr__(&self, py: Python) -> PyResult<&'static str> {
|
2016-05-07 23:01:18 +00:00
|
|
|
Ok("BA")
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 23:01:18 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyNumberProtocol for BinaryArithmetic {
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __add__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} + {:?}", self, rhs))
|
2016-05-07 23:01:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __sub__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} - {:?}", self, rhs))
|
2016-05-07 23:01:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __mul__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} * {:?}", self, rhs))
|
2016-05-07 23:01:18 +00:00
|
|
|
}
|
2016-05-07 23:17:22 +00:00
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __lshift__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} << {:?}", self, rhs))
|
2016-05-07 23:17:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __rshift__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} >> {:?}", self, rhs))
|
2016-05-07 23:17:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __and__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} & {:?}", self, rhs))
|
2016-05-07 23:17:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __xor__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} ^ {:?}", self, rhs))
|
2016-05-07 23:17:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __or__(&self, py: Python, rhs: &PyObject) -> PyResult<String> {
|
2017-05-27 17:49:38 +00:00
|
|
|
Ok(format!("{:?} | {:?}", self, rhs))
|
2016-05-07 23:17:22 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-05-07 23:01:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binary_arithmetic() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| BinaryArithmetic{token: t}).unwrap();
|
2016-05-07 23:01:18 +00:00
|
|
|
py_run!(py, c, "assert c + c == 'BA + BA'");
|
|
|
|
py_run!(py, c, "assert c + 1 == 'BA + 1'");
|
|
|
|
py_run!(py, c, "assert 1 + c == '1 + BA'");
|
|
|
|
py_run!(py, c, "assert c - 1 == 'BA - 1'");
|
|
|
|
py_run!(py, c, "assert 1 - c == '1 - BA'");
|
|
|
|
py_run!(py, c, "assert c * 1 == 'BA * 1'");
|
|
|
|
py_run!(py, c, "assert 1 * c == '1 * BA'");
|
2016-05-07 23:17:22 +00:00
|
|
|
|
|
|
|
py_run!(py, c, "assert c << 1 == 'BA << 1'");
|
|
|
|
py_run!(py, c, "assert 1 << c == '1 << BA'");
|
|
|
|
py_run!(py, c, "assert c >> 1 == 'BA >> 1'");
|
|
|
|
py_run!(py, c, "assert 1 >> c == '1 >> BA'");
|
|
|
|
py_run!(py, c, "assert c & 1 == 'BA & 1'");
|
|
|
|
py_run!(py, c, "assert 1 & c == '1 & BA'");
|
|
|
|
py_run!(py, c, "assert c ^ 1 == 'BA ^ 1'");
|
|
|
|
py_run!(py, c, "assert 1 ^ c == '1 ^ BA'");
|
|
|
|
py_run!(py, c, "assert c | 1 == 'BA | 1'");
|
|
|
|
py_run!(py, c, "assert 1 | c == '1 | BA'");
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 23:01:18 +00:00
|
|
|
|
2017-05-21 05:18:31 +00:00
|
|
|
#[py::class]
|
2017-05-27 18:43:45 +00:00
|
|
|
struct RichComparisons {
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken
|
2017-05-27 18:43:45 +00:00
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyObjectProtocol for RichComparisons {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __repr__(&self, py: Python) -> PyResult<&'static str> {
|
2016-06-07 00:43:16 +00:00
|
|
|
Ok("RC")
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:58:16 +00:00
|
|
|
fn __richcmp__(&self, py: Python, other: &PyObject, op: CompareOp) -> PyResult<String> {
|
2016-06-07 00:43:16 +00:00
|
|
|
match op {
|
2017-05-29 23:16:48 +00:00
|
|
|
CompareOp::Lt => Ok(format!("{} < {:?}", self.__repr__(py).unwrap(), other)),
|
|
|
|
CompareOp::Le => Ok(format!("{} <= {:?}", self.__repr__(py).unwrap(), other)),
|
|
|
|
CompareOp::Eq => Ok(format!("{} == {:?}", self.__repr__(py).unwrap(), other)),
|
|
|
|
CompareOp::Ne => Ok(format!("{} != {:?}", self.__repr__(py).unwrap(), other)),
|
|
|
|
CompareOp::Gt => Ok(format!("{} > {:?}", self.__repr__(py).unwrap(), other)),
|
|
|
|
CompareOp::Ge => Ok(format!("{} >= {:?}", self.__repr__(py).unwrap(), other))
|
2016-06-07 00:43:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
2016-06-07 00:43:16 +00:00
|
|
|
|
2017-05-21 05:18:31 +00:00
|
|
|
#[py::class]
|
2017-05-27 17:49:38 +00:00
|
|
|
struct RichComparisons2 {
|
2017-06-01 22:06:48 +00:00
|
|
|
py: PyToken
|
2017-05-27 17:49:38 +00:00
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
|
|
|
impl PyObjectProtocol for RichComparisons2 {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __repr__(&self, py: Python) -> PyResult<&'static str> {
|
2016-06-07 00:43:16 +00:00
|
|
|
Ok("RC2")
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __richcmp__(&self, py: Python,
|
2017-06-03 01:58:16 +00:00
|
|
|
other: &'p PyObject, op: CompareOp) -> PyResult<PyObject> {
|
2016-06-07 00:43:16 +00:00
|
|
|
match op {
|
2017-06-03 01:58:16 +00:00
|
|
|
CompareOp::Eq => Ok(true.to_object(py)),
|
|
|
|
CompareOp::Ne => Ok(false.to_object(py)),
|
2017-05-29 09:47:27 +00:00
|
|
|
_ => Ok(py.NotImplemented())
|
2016-06-07 00:43:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
}
|
2016-06-07 00:43:16 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rich_comparisons() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| RichComparisons{token: t}).unwrap();
|
2016-06-07 00:43:16 +00:00
|
|
|
py_run!(py, c, "assert (c < c) == 'RC < RC'");
|
|
|
|
py_run!(py, c, "assert (c < 1) == 'RC < 1'");
|
|
|
|
py_run!(py, c, "assert (1 < c) == 'RC > 1'");
|
|
|
|
py_run!(py, c, "assert (c <= c) == 'RC <= RC'");
|
|
|
|
py_run!(py, c, "assert (c <= 1) == 'RC <= 1'");
|
|
|
|
py_run!(py, c, "assert (1 <= c) == 'RC >= 1'");
|
|
|
|
py_run!(py, c, "assert (c == c) == 'RC == RC'");
|
|
|
|
py_run!(py, c, "assert (c == 1) == 'RC == 1'");
|
|
|
|
py_run!(py, c, "assert (1 == c) == 'RC == 1'");
|
|
|
|
py_run!(py, c, "assert (c != c) == 'RC != RC'");
|
|
|
|
py_run!(py, c, "assert (c != 1) == 'RC != 1'");
|
|
|
|
py_run!(py, c, "assert (1 != c) == 'RC != 1'");
|
|
|
|
py_run!(py, c, "assert (c > c) == 'RC > RC'");
|
|
|
|
py_run!(py, c, "assert (c > 1) == 'RC > 1'");
|
|
|
|
py_run!(py, c, "assert (1 > c) == 'RC < 1'");
|
|
|
|
py_run!(py, c, "assert (c >= c) == 'RC >= RC'");
|
|
|
|
py_run!(py, c, "assert (c >= 1) == 'RC >= 1'");
|
|
|
|
py_run!(py, c, "assert (1 >= c) == 'RC <= 1'");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-06-12 05:23:49 +00:00
|
|
|
#[cfg(Py_3)]
|
2016-06-07 00:43:16 +00:00
|
|
|
fn rich_comparisons_python_3_type_error() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c2 = py.init(|t| RichComparisons2{py: t}).unwrap();
|
2016-06-07 00:43:16 +00:00
|
|
|
py_expect_exception!(py, c2, "c2 < c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 < 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 < c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 <= c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 <= 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 <= c2", TypeError);
|
|
|
|
py_run!(py, c2, "assert (c2 == c2) == True");
|
|
|
|
py_run!(py, c2, "assert (c2 == 1) == True");
|
|
|
|
py_run!(py, c2, "assert (1 == c2) == True");
|
|
|
|
py_run!(py, c2, "assert (c2 != c2) == False");
|
|
|
|
py_run!(py, c2, "assert (c2 != 1) == False");
|
|
|
|
py_run!(py, c2, "assert (1 != c2) == False");
|
|
|
|
py_expect_exception!(py, c2, "c2 > c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 > 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 > c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 >= c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 >= 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 >= c2", TypeError);
|
|
|
|
}
|
2017-05-21 05:18:31 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct InPlaceOperations {
|
2017-05-27 18:43:45 +00:00
|
|
|
value: u32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-06-07 00:43:16 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyObjectProtocol for InPlaceOperations {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __repr__(&self, py: Python) -> PyResult<String> {
|
|
|
|
Ok(format!("IPO({:?})", self.value))
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-06-14 19:53:43 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
|
|
|
impl PyNumberProtocol for InPlaceOperations {
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __iadd__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value + other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __isub__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value - other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __imul__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value * other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __ilshift__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value << other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __irshift__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value >> other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __iand__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value & other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __ixor__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value ^ other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __ior__(&mut self, py: Python, other: u32) -> PyResult<()> {
|
|
|
|
self.value = self.value | other;
|
|
|
|
Ok(())
|
2016-06-14 19:53:43 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-06-14 19:53:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inplace_operations() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 0, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c += 1; assert repr(c) == repr(d) == 'IPO(1)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value:10, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c -= 1; assert repr(c) == repr(d) == 'IPO(9)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 3, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c *= 3; assert repr(c) == repr(d) == 'IPO(9)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 3, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c <<= 2; assert repr(c) == repr(d) == 'IPO(12)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 12, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c >>= 2; assert repr(c) == repr(d) == 'IPO(3)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 12, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c &= 10; assert repr(c) == repr(d) == 'IPO(8)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 12, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c |= 3; assert repr(c) == repr(d) == 'IPO(15)'");
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| InPlaceOperations{value: 12, token: t}).unwrap();
|
2016-06-14 19:53:43 +00:00
|
|
|
py_run!(py, c, "d = c; c ^= 5; assert repr(c) == repr(d) == 'IPO(9)'");
|
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
|
|
|
|
#[py::class]
|
|
|
|
struct ContextManager {
|
2017-05-27 18:43:45 +00:00
|
|
|
exit_called: bool,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
2016-06-14 19:53:43 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::proto]
|
2017-05-25 05:43:07 +00:00
|
|
|
impl<'p> PyContextProtocol<'p> for ContextManager {
|
2016-05-08 15:28:26 +00:00
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __enter__(&mut self, py: Python) -> PyResult<i32> {
|
2016-05-08 15:28:26 +00:00
|
|
|
Ok(42)
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn __exit__(&mut self, py: Python,
|
2017-06-03 01:58:16 +00:00
|
|
|
ty: Option<PyType>,
|
|
|
|
value: Option<PyObject>,
|
|
|
|
traceback: Option<PyObject>) -> PyResult<bool> {
|
2017-05-25 05:43:07 +00:00
|
|
|
self.exit_called = true;
|
2016-05-08 15:51:16 +00:00
|
|
|
if ty == Some(py.get_type::<exc::ValueError>()) {
|
2016-05-08 15:28:26 +00:00
|
|
|
Ok(true)
|
|
|
|
} else {
|
2017-05-29 09:47:27 +00:00
|
|
|
Ok(false)
|
2016-05-08 15:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-29 09:47:27 +00:00
|
|
|
}
|
2016-05-08 15:28:26 +00:00
|
|
|
|
2017-05-29 09:47:27 +00:00
|
|
|
#[test]
|
2016-05-08 15:28:26 +00:00
|
|
|
fn context_manager() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let c = py.init(|t| ContextManager{exit_called: false, token: t}).unwrap();
|
2016-05-08 15:28:26 +00:00
|
|
|
py_run!(py, c, "with c as x:\n assert x == 42");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert!(c.as_ref(py).exit_called);
|
2016-05-08 15:28:26 +00:00
|
|
|
|
2017-06-04 00:27:26 +00:00
|
|
|
c.as_mut(py).exit_called = false;
|
2016-05-08 15:28:26 +00:00
|
|
|
py_run!(py, c, "with c as x:\n raise ValueError");
|
2017-06-04 00:27:26 +00:00
|
|
|
assert!(c.as_ref(py).exit_called);
|
2016-05-08 15:28:26 +00:00
|
|
|
|
2017-06-04 00:27:26 +00:00
|
|
|
c.as_mut(py).exit_called = false;
|
2017-05-30 01:36:44 +00:00
|
|
|
py_expect_exception!(
|
|
|
|
py, c, "with c as x:\n raise NotImplementedError",
|
|
|
|
NotImplementedError);
|
2017-06-04 00:27:26 +00:00
|
|
|
assert!(c.as_ref(py).exit_called);
|
2017-05-29 09:47:27 +00:00
|
|
|
}
|
2016-05-08 15:28:26 +00:00
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[py::class]
|
|
|
|
struct ClassWithProperties {
|
2017-05-27 18:43:45 +00:00
|
|
|
num: i32,
|
2017-06-01 22:06:48 +00:00
|
|
|
token: PyToken,
|
2017-05-20 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[py::methods]
|
|
|
|
impl ClassWithProperties {
|
2017-04-06 17:03:03 +00:00
|
|
|
|
2017-05-27 17:49:38 +00:00
|
|
|
fn get_num(&self, py: Python) -> PyResult<i32> {
|
|
|
|
Ok(self.num)
|
2017-04-06 17:03:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:40:33 +00:00
|
|
|
#[getter(DATA)]
|
2017-05-27 17:49:38 +00:00
|
|
|
fn get_data(&self, py: Python) -> PyResult<i32> {
|
|
|
|
Ok(self.num)
|
2017-04-06 17:03:03 +00:00
|
|
|
}
|
2017-05-20 17:40:33 +00:00
|
|
|
#[setter(DATA)]
|
2017-06-04 00:27:26 +00:00
|
|
|
fn set_data(&mut self, py: Python, value: i32) -> PyResult<()> {
|
2017-05-27 17:49:38 +00:00
|
|
|
self.num = value;
|
2017-05-20 17:40:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 17:03:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_properties() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let inst = py.init(|t| ClassWithProperties{num: 10, token: t}).unwrap();
|
2017-04-06 17:03:03 +00:00
|
|
|
|
|
|
|
py_run!(py, inst, "assert inst.get_num() == 10");
|
|
|
|
py_run!(py, inst, "assert inst.get_num() == inst.DATA");
|
|
|
|
py_run!(py, inst, "inst.DATA = 20");
|
|
|
|
py_run!(py, inst, "assert inst.get_num() == 20");
|
|
|
|
py_run!(py, inst, "assert inst.get_num() == inst.DATA");
|
2017-05-27 17:49:38 +00:00
|
|
|
}
|