Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sequences that can be cast into PyMapping #2098

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix undefined behavior in `PySlice::indices`. [#2061](https://github.com/PyO3/pyo3/pull/2061)
- Use the Rust function path for `wrap_pymodule!` of a `#[pymodule]` with a `#[pyo3(name = "..")]` attribute, not the Python name. [#2081](https://github.com/PyO3/pyo3/pull/2081)
- Fix panic in `#[pyfunction]` generated code when a required argument following an `Option` was not provided. [#2093](https://github.com/PyO3/pyo3/pull/2093)
- Fix conversion of non-mapping sequences into `PyMapping` (i.e `PyStr` can be cast to `PyMapping`) due to `PyMapping_Check` returning true for sequences. [#2098](https://github.com/PyO3/pyo3/pull/2098)

## [0.15.1] - 2021-11-19

Expand Down
14 changes: 12 additions & 2 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ 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 @@ -143,7 +145,7 @@ mod tests {

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

Expand Down Expand Up @@ -299,4 +301,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();
});
}
}