Skip to content

Commit

Permalink
amend code for PyDict::get_item change
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jul 19, 2023
1 parent 9345cd0 commit 8924da6
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 47 deletions.
2 changes: 1 addition & 1 deletion benches/bench_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ py_dec = decimal.Decimal("0.0")
Some(locals),
)
.unwrap();
let py_dec = locals.get_item("py_dec").unwrap();
let py_dec = locals.get_item("py_dec").unwrap().unwrap();

b.iter(|| {
let _: Decimal = black_box(py_dec).extract().unwrap();
Expand Down
7 changes: 6 additions & 1 deletion benches/bench_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ fn dict_get_item(b: &mut Bencher<'_>) {
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
sum += dict
.get_item(i)
.unwrap()
.unwrap()
.extract::<usize>()
.unwrap();
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ mod tests {
Some(locals),
)
.unwrap();
let result: PyResult<FixedOffset> = locals.get_item("zi").unwrap().extract();
let result: PyResult<FixedOffset> = locals.get_item("zi").unwrap().unwrap().extract();
assert!(result.is_err());
let res = result.err().unwrap();
// Also check the error message is what we expect
Expand Down
30 changes: 27 additions & 3 deletions src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
assert_eq!(map, py_map.extract().unwrap());
});
}
Expand All @@ -126,7 +134,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
});
}

Expand All @@ -139,7 +155,15 @@ mod tests {
let py_map = map.into_py_dict(py);

assert_eq!(py_map.len(), 1);
assert_eq!(py_map.get_item(1).unwrap().extract::<i32>().unwrap(), 1);
assert_eq!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap(),
1
);
});
}

Expand Down
30 changes: 27 additions & 3 deletions src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ mod test_indexmap {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
assert_eq!(
map,
py_map.extract::<indexmap::IndexMap::<i32, i32>>().unwrap()
Expand All @@ -166,7 +174,15 @@ mod test_indexmap {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
});
}

Expand All @@ -179,7 +195,15 @@ mod test_indexmap {
let py_map = map.into_py_dict(py);

assert_eq!(py_map.len(), 1);
assert_eq!(py_map.get_item(1).unwrap().extract::<i32>().unwrap(), 1);
assert_eq!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap(),
1
);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/conversions/rust_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod test_rust_decimal {
)
.unwrap();
// Checks if Python Decimal -> Rust Decimal conversion is correct
let py_dec = locals.get_item("py_dec").unwrap();
let py_dec = locals.get_item("py_dec").unwrap().unwrap();
let py_result: Decimal = FromPyObject::extract(py_dec).unwrap();
assert_eq!(rs_orig, py_result);
})
Expand Down Expand Up @@ -192,7 +192,7 @@ mod test_rust_decimal {
Some(locals),
)
.unwrap();
let py_dec = locals.get_item("py_dec").unwrap();
let py_dec = locals.get_item("py_dec").unwrap().unwrap();
let roundtripped: Result<Decimal, PyErr> = FromPyObject::extract(py_dec);
assert!(roundtripped.is_err());
})
Expand All @@ -208,7 +208,7 @@ mod test_rust_decimal {
Some(locals),
)
.unwrap();
let py_dec = locals.get_item("py_dec").unwrap();
let py_dec = locals.get_item("py_dec").unwrap().unwrap();
let roundtripped: Result<Decimal, PyErr> = FromPyObject::extract(py_dec);
assert!(roundtripped.is_err());
})
Expand Down
40 changes: 36 additions & 4 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
assert_eq!(map, py_map.extract().unwrap());
});
}
Expand All @@ -137,7 +145,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
assert_eq!(map, py_map.extract().unwrap());
});
}
Expand All @@ -152,7 +168,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
});
}

Expand All @@ -166,7 +190,15 @@ mod tests {
let py_map: &PyDict = m.downcast(py).unwrap();

assert!(py_map.len() == 1);
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
assert!(
py_map
.get_item(1)
.unwrap()
.unwrap()
.extract::<i32>()
.unwrap()
== 1
);
});
}
}
2 changes: 1 addition & 1 deletion src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ impl<'py> Python<'py> {
/// Some(locals),
/// )
/// .unwrap();
/// let ret = locals.get_item("ret").unwrap();
/// let ret = locals.get_item("ret").unwrap().unwrap();
/// let b64: &PyBytes = ret.downcast().unwrap();
/// assert_eq!(b64.as_bytes(), b"SGVsbG8gUnVzdCE=");
/// });
Expand Down
9 changes: 8 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,14 @@ mod tests {
let dict = PyDict::new(py);
dict.set_item(foo1, 42_usize).unwrap();
assert!(dict.contains(foo2).unwrap());
assert_eq!(dict.get_item(foo3).unwrap().extract::<usize>().unwrap(), 42);
assert_eq!(
dict.get_item(foo3)
.unwrap()
.unwrap()
.extract::<usize>()
.unwrap(),
42
);
});
}

Expand Down
Loading

0 comments on commit 8924da6

Please sign in to comment.