Fix #197 - Remove tuple to dict conversions
This commit is contained in:
parent
27633da3b1
commit
d9d1650fc4
|
@ -61,11 +61,3 @@ impl IntoPyDictPointer for NoArgs {
|
||||||
std::ptr::null_mut()
|
std::ptr::null_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts `()` to an null pointer.
|
|
||||||
impl IntoPyDictPointer for () {
|
|
||||||
|
|
||||||
fn into_dict_ptr(self, _: Python) -> *mut ffi::PyObject {
|
|
||||||
std::ptr::null_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -468,6 +468,7 @@ mod test {
|
||||||
use conversion::{ToPyObject, PyTryFrom};
|
use conversion::{ToPyObject, PyTryFrom};
|
||||||
use objects::PyString;
|
use objects::PyString;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use noargs::NoArgs;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug_string() {
|
fn test_debug_string() {
|
||||||
|
@ -493,7 +494,7 @@ mod test {
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let a = py.eval("42", None, None).unwrap();
|
let a = py.eval("42", None, None).unwrap();
|
||||||
a.call_method0("__str__").unwrap(); // ok
|
a.call_method0("__str__").unwrap(); // ok
|
||||||
assert!(a.call_method("nonexistent_method", (1,), ()).is_err());
|
assert!(a.call_method("nonexistent_method", (1,), NoArgs).is_err());
|
||||||
assert!(a.call_method0("nonexistent_method").is_err());
|
assert!(a.call_method0("nonexistent_method").is_err());
|
||||||
assert!(a.call_method1("nonexistent_method", (1,)).is_err());
|
assert!(a.call_method1("nonexistent_method", (1,)).is_err());
|
||||||
}
|
}
|
||||||
|
|
|
@ -243,43 +243,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: ToPyObject, V: ToPyObject> IntoPyDictPointer for (K, V) {
|
|
||||||
default fn into_dict_ptr(self, py: Python) -> *mut ffi::PyObject {
|
|
||||||
let dict = PyDict::new(py);
|
|
||||||
dict.set_item(self.0, self.1).expect("Failed to set_item on dict");
|
|
||||||
dict.into_ptr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! dict_conversion ({$length:expr,$(($refN:ident, $n:tt, $T1:ident, $T2:ident)),+} => {
|
|
||||||
impl<$($T1: ToPyObject, $T2: ToPyObject),+> IntoPyDictPointer for ($(($T1,$T2),)+) {
|
|
||||||
fn into_dict_ptr(self, py: Python) -> *mut ffi::PyObject {
|
|
||||||
let dict = PyDict::new(py);
|
|
||||||
$(dict.set_item(self.$n.0, self.$n.1).expect("Failed to set_item on dict");)+;
|
|
||||||
dict.into_ptr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dict_conversion!(1, (ref0, 0, A1, A2));
|
|
||||||
dict_conversion!(2, (ref0, 0, A1, A2), (ref1, 1, B1, B2));
|
|
||||||
dict_conversion!(3, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2));
|
|
||||||
dict_conversion!(4, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2));
|
|
||||||
dict_conversion!(5, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2), (ref4, 4, E1, E2));
|
|
||||||
dict_conversion!(6, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2), (ref4, 4, E1, E2), (ref5, 5, F1, F2));
|
|
||||||
dict_conversion!(7, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2), (ref4, 4, E1, E2), (ref5, 5, F1, F2), (ref6, 6, G1, G2));
|
|
||||||
dict_conversion!(8, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2), (ref4, 4, E1, E2), (ref5, 5, F1, F2), (ref6, 6, G1, G2),
|
|
||||||
(ref7, 7, H1, H2));
|
|
||||||
dict_conversion!(9, (ref0, 0, A1, A2), (ref1, 1, B1, B2), (ref2, 2, C1, C2),
|
|
||||||
(ref3, 3, D1, D2), (ref4, 4, E1, E2), (ref5, 5, F1, F2), (ref6, 6, G1, G2),
|
|
||||||
(ref7, 7, H1, H2), (ref8, 8, I1, I2));
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
@ -627,38 +590,4 @@ mod test {
|
||||||
assert!(py_map.len() == 3);
|
assert!(py_map.len() == 3);
|
||||||
assert!(py_map.get_item("b").unwrap().extract::<i32>().unwrap() == 2);
|
assert!(py_map.get_item("b").unwrap().extract::<i32>().unwrap() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_tuple_into_dict() {
|
|
||||||
let gil = Python::acquire_gil();
|
|
||||||
let py = gil.python();
|
|
||||||
|
|
||||||
let m = ((1, 1),).into_dict_ptr(py);
|
|
||||||
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
|
|
||||||
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
|
|
||||||
|
|
||||||
assert!(py_map.len() == 1);
|
|
||||||
assert!( py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
|
||||||
|
|
||||||
let m = ((1, 1), (2, 3)).into_dict_ptr(py);
|
|
||||||
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
|
|
||||||
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
|
|
||||||
|
|
||||||
assert!(py_map.len() == 2);
|
|
||||||
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
|
||||||
assert!(py_map.get_item(2).unwrap().extract::<i32>().unwrap() == 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_simple_tuple_into_dict() {
|
|
||||||
let gil = Python::acquire_gil();
|
|
||||||
let py = gil.python();
|
|
||||||
|
|
||||||
let m = (1, 1).into_dict_ptr(py);
|
|
||||||
let ob = unsafe{PyObject::from_owned_ptr(py, m)};
|
|
||||||
let py_map = <PyDict as PyTryFrom>::try_from(ob.as_ref(py)).unwrap();
|
|
||||||
|
|
||||||
assert!(py_map.len() == 1);
|
|
||||||
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue