change `PyDict::from_sequence` to take just `&PyAny`

This commit is contained in:
David Hewitt 2023-10-20 11:59:26 +01:00
parent d8a6f37fec
commit d895734499
2 changed files with 5 additions and 3 deletions

View File

@ -0,0 +1 @@
- `PyDict::from_sequence` now takes a single argument of type `&PyAny` (previously took two arguments `Python` and `PyObject`).

View File

@ -65,7 +65,8 @@ impl PyDict {
/// Returns an error on invalid input. In the case of key collisions, /// Returns an error on invalid input. In the case of key collisions,
/// this keeps the last entry seen. /// this keeps the last entry seen.
#[cfg(not(PyPy))] #[cfg(not(PyPy))]
pub fn from_sequence(py: Python<'_>, seq: PyObject) -> PyResult<&PyDict> { pub fn from_sequence(seq: &PyAny) -> PyResult<&PyDict> {
let py = seq.py();
let dict = Self::new(py); let dict = Self::new(py);
err::error_on_minusone(py, unsafe { err::error_on_minusone(py, unsafe {
ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1) ffi::PyDict_MergeFromSeq2(dict.into_ptr(), seq.into_ptr(), 1)
@ -505,7 +506,7 @@ mod tests {
fn test_from_sequence() { fn test_from_sequence() {
Python::with_gil(|py| { Python::with_gil(|py| {
let items = PyList::new(py, &vec![("a", 1), ("b", 2)]); let items = PyList::new(py, &vec![("a", 1), ("b", 2)]);
let dict = PyDict::from_sequence(py, items.to_object(py)).unwrap(); let dict = PyDict::from_sequence(items).unwrap();
assert_eq!( assert_eq!(
1, 1,
dict.get_item("a") dict.get_item("a")
@ -534,7 +535,7 @@ mod tests {
fn test_from_sequence_err() { fn test_from_sequence_err() {
Python::with_gil(|py| { Python::with_gil(|py| {
let items = PyList::new(py, &vec!["a", "b"]); let items = PyList::new(py, &vec!["a", "b"]);
assert!(PyDict::from_sequence(py, items.to_object(py)).is_err()); assert!(PyDict::from_sequence(items).is_err());
}); });
} }