2017-07-21 16:48:48 +00:00
|
|
|
#![feature(proc_macro, specialization, const_fn)]
|
2017-04-28 02:38:02 +00:00
|
|
|
#![allow(dead_code, unused_variables)]
|
|
|
|
|
2017-05-20 19:22:35 +00:00
|
|
|
extern crate pyo3;
|
2017-04-28 02:38:02 +00:00
|
|
|
|
2017-05-13 05:43:17 +00:00
|
|
|
use pyo3::*;
|
2017-04-28 02:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basics() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let v = PySlice::new(py, 1, 10, 2);
|
2017-06-29 05:23:55 +00:00
|
|
|
let indices = v.indices(100).unwrap();
|
2017-04-28 02:38:02 +00:00
|
|
|
assert_eq!(1, indices.start);
|
|
|
|
assert_eq!(10, indices.stop);
|
|
|
|
assert_eq!(2, indices.step);
|
|
|
|
assert_eq!(5, indices.slicelength);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 19:22:35 +00:00
|
|
|
#[py::class]
|
2017-06-04 00:27:26 +00:00
|
|
|
struct Test {
|
|
|
|
token: PyToken
|
|
|
|
}
|
2017-05-20 19:22:35 +00:00
|
|
|
|
|
|
|
#[py::proto]
|
2017-05-29 09:47:27 +00:00
|
|
|
impl<'p> PyMappingProtocol<'p> for Test
|
|
|
|
{
|
2017-06-24 15:28:31 +00:00
|
|
|
fn __getitem__(&self, idx: &PyObjectRef) -> PyResult<PyObject> {
|
2017-07-29 06:19:00 +00:00
|
|
|
if let Ok(slice) = idx.cast_as::<PySlice>() {
|
2017-06-20 21:10:12 +00:00
|
|
|
let indices = slice.indices(1000)?;
|
2017-04-28 02:38:02 +00:00
|
|
|
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {
|
2017-07-14 02:04:00 +00:00
|
|
|
return Ok("slice".into_object(self.py()))
|
2017-04-28 02:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 08:04:37 +00:00
|
|
|
else if let Ok(idx) = idx.extract::<isize>() {
|
2017-04-28 02:38:02 +00:00
|
|
|
if idx == 1 {
|
2017-07-14 02:04:00 +00:00
|
|
|
return Ok("int".into_object(self.py()))
|
2017-04-28 02:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:11:00 +00:00
|
|
|
Err(PyErr::new::<exc::ValueError, _>("error"))
|
2017-04-28 02:38:02 +00:00
|
|
|
}
|
2017-05-20 19:22:35 +00:00
|
|
|
}
|
2017-04-28 02:38:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cls_impl() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2017-06-06 03:25:00 +00:00
|
|
|
let ob = py.init(|t| Test{token: t}).unwrap();
|
2017-04-28 02:38:02 +00:00
|
|
|
let d = PyDict::new(py);
|
2017-06-21 21:08:16 +00:00
|
|
|
d.set_item("ob", ob).unwrap();
|
2017-04-28 02:38:02 +00:00
|
|
|
|
2017-06-21 21:08:16 +00:00
|
|
|
py.run("assert ob[1] == 'int'", None, Some(d)).unwrap();
|
|
|
|
py.run("assert ob[100:200:1] == 'slice'", None, Some(d)).unwrap();
|
2017-04-28 02:38:02 +00:00
|
|
|
}
|