Skip to content

Commit

Permalink
Fix conversion of non-mapping sequences into (i.e can be cast to ) du…
Browse files Browse the repository at this point in the history
…e to returning true for sequences.
  • Loading branch information
aviramha committed Jan 13, 2022
1 parent edf03c1 commit 6a5cd29
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'v> PyTryFrom<'v> for PyMapping {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v PyMapping, PyDowncastError<'v>> {
let value = value.into();
unsafe {
if ffi::PyMapping_Check(value.as_ptr()) != 0 {
if ffi::PyMapping_Check(value.as_ptr()) != 0 && ffi::PySequence_Check(value.as_ptr()) == 0 {
Ok(<PyMapping as PyTryFrom>::try_from_unchecked(value))
} else {
Err(PyDowncastError::new(value, "Mapping"))
Expand Down Expand Up @@ -142,8 +142,8 @@ mod tests {
use std::collections::HashMap;

use crate::{
exceptions::PyKeyError,
types::{PyDict, PyTuple},
exceptions::{PyKeyError, PyTypeError},
types::{PyDict, PyTuple, PyString},
Python,
};

Expand Down Expand Up @@ -299,4 +299,12 @@ mod tests {
assert_eq!(mapping_ref.get_refcnt(), 2);
})
}

#[test]
fn test_not_mapping() {
Python::with_gil(|py| {
let str = PyString::new(py, "hello, world");
str.downcast::<PyMapping>().unwrap_err();
});
}
}

0 comments on commit 6a5cd29

Please sign in to comment.