Skip to content

Commit

Permalink
fix wrong trait bound IntoPyObject impls for Hash collections (#4478
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Icxolu committed Aug 23, 2024
1 parent ec0853d commit e068a30
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
impl<'a, 'py, K, H> IntoPyObject<'py> for &'a hashbrown::HashSet<K, H>
where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a H: hash::BuildHasher,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
{
type Target = PySet;
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a, 'py, K, V, H> IntoPyObject<'py> for &'a collections::HashMap<K, V, H>
where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a V: IntoPyObject<'py>,
&'a H: hash::BuildHasher,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
{
type Target = PyDict;
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
impl<'a, 'py, K, H> IntoPyObject<'py> for &'a collections::HashSet<K, H>
where
&'a K: IntoPyObject<'py> + Eq + hash::Hash,
&'a H: hash::BuildHasher,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
{
type Target = PySet;
Expand Down
39 changes: 20 additions & 19 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,20 @@ mod tests {
use crate::{exceptions::PyKeyError, types::PyTuple};

use super::*;
use crate::conversion::IntoPyObject;

#[test]
fn test_len() {
Python::with_gil(|py| {
let mut v = HashMap::new();
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let mut v = HashMap::<i32, i32>::new();
let ob = (&v).into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
assert_eq!(0, mapping.len().unwrap());
assert!(mapping.is_empty().unwrap());

v.insert(7, 32);
let ob = v.to_object(py);
let mapping2 = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping2 = ob.downcast::<PyMapping>().unwrap();
assert_eq!(1, mapping2.len().unwrap());
assert!(!mapping2.is_empty().unwrap());
});
Expand All @@ -212,8 +213,8 @@ mod tests {
Python::with_gil(|py| {
let mut v = HashMap::new();
v.insert("key0", 1234);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
mapping.set_item("key1", "foo").unwrap();

assert!(mapping.contains("key0").unwrap());
Expand All @@ -227,8 +228,8 @@ mod tests {
Python::with_gil(|py| {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
assert_eq!(
32,
mapping.get_item(7i32).unwrap().extract::<i32>().unwrap()
Expand All @@ -245,8 +246,8 @@ mod tests {
Python::with_gil(|py| {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
assert!(mapping.set_item(7i32, 42i32).is_ok()); // change
assert!(mapping.set_item(8i32, 123i32).is_ok()); // insert
assert_eq!(
Expand All @@ -265,8 +266,8 @@ mod tests {
Python::with_gil(|py| {
let mut v = HashMap::new();
v.insert(7, 32);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
assert!(mapping.del_item(7i32).is_ok());
assert_eq!(0, mapping.len().unwrap());
assert!(mapping
Expand All @@ -283,8 +284,8 @@ mod tests {
v.insert(7, 32);
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut key_sum = 0;
let mut value_sum = 0;
Expand All @@ -305,8 +306,8 @@ mod tests {
v.insert(7, 32);
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut key_sum = 0;
for el in mapping.keys().unwrap().iter().unwrap() {
Expand All @@ -323,8 +324,8 @@ mod tests {
v.insert(7, 32);
v.insert(8, 42);
v.insert(9, 123);
let ob = v.to_object(py);
let mapping = ob.downcast_bound::<PyMapping>(py).unwrap();
let ob = v.into_pyobject(py).unwrap();
let mapping = ob.downcast::<PyMapping>().unwrap();
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
let mut values_sum = 0;
for el in mapping.values().unwrap().iter().unwrap() {
Expand Down

0 comments on commit e068a30

Please sign in to comment.