2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2019-10-25 10:12:05 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-07-04 15:55:26 +00:00
|
|
|
use pyo3::exceptions::PyKeyError;
|
2019-10-25 10:12:05 +00:00
|
|
|
use pyo3::prelude::*;
|
2021-03-14 07:34:05 +00:00
|
|
|
use pyo3::py_run;
|
2019-10-25 10:12:05 +00:00
|
|
|
use pyo3::types::IntoPyDict;
|
|
|
|
use pyo3::types::PyList;
|
2022-03-21 23:34:50 +00:00
|
|
|
use pyo3::types::PyMapping;
|
|
|
|
use pyo3::types::PySequence;
|
2019-10-25 10:12:05 +00:00
|
|
|
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2021-03-14 07:34:05 +00:00
|
|
|
mod common;
|
|
|
|
|
2022-03-21 23:34:50 +00:00
|
|
|
#[pyclass(mapping)]
|
2019-10-25 10:12:05 +00:00
|
|
|
struct Mapping {
|
|
|
|
index: HashMap<String, usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl Mapping {
|
|
|
|
#[new]
|
2019-12-14 14:16:39 +00:00
|
|
|
fn new(elements: Option<&PyList>) -> PyResult<Self> {
|
2019-10-25 10:12:05 +00:00
|
|
|
if let Some(pylist) = elements {
|
|
|
|
let mut elems = HashMap::with_capacity(pylist.len());
|
|
|
|
for (i, pyelem) in pylist.into_iter().enumerate() {
|
|
|
|
let elem = String::extract(pyelem)?;
|
|
|
|
elems.insert(elem, i);
|
|
|
|
}
|
2019-12-14 14:16:39 +00:00
|
|
|
Ok(Self { index: elems })
|
2019-10-25 10:12:05 +00:00
|
|
|
} else {
|
2019-12-14 14:16:39 +00:00
|
|
|
Ok(Self {
|
2019-10-25 10:12:05 +00:00
|
|
|
index: HashMap::new(),
|
2019-12-14 14:16:39 +00:00
|
|
|
})
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:40:17 +00:00
|
|
|
fn __len__(&self) -> usize {
|
|
|
|
self.index.len()
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn __getitem__(&self, query: String) -> PyResult<usize> {
|
|
|
|
self.index
|
|
|
|
.get(&query)
|
|
|
|
.copied()
|
2020-08-25 19:33:36 +00:00
|
|
|
.ok_or_else(|| PyKeyError::new_err("unknown key"))
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:40:17 +00:00
|
|
|
fn __setitem__(&mut self, key: String, value: usize) {
|
2019-10-25 10:12:05 +00:00
|
|
|
self.index.insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __delitem__(&mut self, key: String) -> PyResult<()> {
|
|
|
|
if self.index.remove(&key).is_none() {
|
2020-08-25 19:33:36 +00:00
|
|
|
Err(PyKeyError::new_err("unknown key"))
|
2019-10-25 10:12:05 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 23:34:50 +00:00
|
|
|
|
|
|
|
fn get(&self, py: Python<'_>, key: &str, default: Option<PyObject>) -> Option<PyObject> {
|
|
|
|
self.index
|
|
|
|
.get(key)
|
|
|
|
.map(|value| value.into_py(py))
|
|
|
|
.or(default)
|
|
|
|
}
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 07:34:05 +00:00
|
|
|
/// Return a dict with `m = Mapping(['1', '2', '3'])`.
|
2024-02-10 12:59:55 +00:00
|
|
|
fn map_dict(py: Python<'_>) -> Bound<'_, pyo3::types::PyDict> {
|
2024-02-18 18:27:19 +00:00
|
|
|
let d = [("Mapping", py.get_type_bound::<Mapping>())].into_py_dict_bound(py);
|
2021-03-14 07:34:05 +00:00
|
|
|
py_run!(py, *d, "m = Mapping(['1', '2', '3'])");
|
|
|
|
d
|
|
|
|
}
|
|
|
|
|
2019-10-25 10:12:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_getitem() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let d = map_dict(py);
|
|
|
|
|
|
|
|
py_assert!(py, *d, "m['1'] == 0");
|
|
|
|
py_assert!(py, *d, "m['2'] == 1");
|
|
|
|
py_assert!(py, *d, "m['3'] == 2");
|
|
|
|
py_expect_exception!(py, *d, "print(m['4'])", PyKeyError);
|
|
|
|
});
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setitem() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let d = map_dict(py);
|
|
|
|
|
|
|
|
py_run!(py, *d, "m['1'] = 4; assert m['1'] == 4");
|
|
|
|
py_run!(py, *d, "m['0'] = 0; assert m['0'] == 0");
|
|
|
|
py_assert!(py, *d, "len(m) == 4");
|
|
|
|
py_expect_exception!(py, *d, "m[0] = 'hello'", PyTypeError);
|
|
|
|
py_expect_exception!(py, *d, "m[0] = -1", PyTypeError);
|
|
|
|
});
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delitem() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let d = map_dict(py);
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
*d,
|
|
|
|
"del m['1']; assert len(m) == 2 and m['2'] == 1 and m['3'] == 2"
|
|
|
|
);
|
|
|
|
py_expect_exception!(py, *d, "del m[-1]", PyTypeError);
|
|
|
|
py_expect_exception!(py, *d, "del m['4']", PyKeyError);
|
|
|
|
});
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
2022-03-21 23:34:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mapping_is_not_sequence() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let mut index = HashMap::new();
|
|
|
|
index.insert("Foo".into(), 1);
|
|
|
|
index.insert("Bar".into(), 2);
|
|
|
|
let m = Py::new(py, Mapping { index }).unwrap();
|
2022-08-10 20:03:18 +00:00
|
|
|
|
|
|
|
PyMapping::register::<Mapping>(py).unwrap();
|
|
|
|
|
2024-02-22 22:38:42 +00:00
|
|
|
assert!(m.bind(py).downcast::<PyMapping>().is_ok());
|
|
|
|
assert!(m.bind(py).downcast::<PySequence>().is_err());
|
2022-03-21 23:34:50 +00:00
|
|
|
});
|
|
|
|
}
|