Skip to content

Commit

Permalink
Revert #2500, i.e. impl FromPyObject for Vec<T> will accept str.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Sep 21, 2022
1 parent 914ab09 commit 4f7a811
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
3 changes: 1 addition & 2 deletions pytests/tests/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def test_vec_from_bytes():


def test_vec_from_str():
with pytest.raises(ValueError):
sequence.vec_to_vec_pystring("123")
assert sequence.vec_to_vec_pystring("123") == ["1", "2", "3"]


@pytest.mark.skipif(
Expand Down
23 changes: 14 additions & 9 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions::PyValueError;
use crate::inspect::types::TypeInfo;
use crate::internal_tricks::get_ssize_index;
use crate::once_cell::GILOnceCell;
use crate::type_object::PyTypeInfo;
use crate::types::{PyAny, PyList, PyString, PyTuple, PyType};
use crate::types::{PyAny, PyList, PyTuple, PyType};
use crate::{ffi, PyNativeType, ToPyObject};
use crate::{AsPyPointer, IntoPy, IntoPyPointer, Py, Python};
use crate::{FromPyObject, PyTryFrom};
Expand Down Expand Up @@ -284,9 +283,6 @@ where
T: FromPyObject<'a>,
{
fn extract(obj: &'a PyAny) -> PyResult<Self> {
if let Ok(true) = obj.is_instance_of::<PyString>() {
return Err(PyValueError::new_err("Can't extract `str` to `Vec`"));
}
extract_sequence(obj)
}

Expand Down Expand Up @@ -405,14 +401,23 @@ mod tests {
}

#[test]
fn test_strings_cannot_be_extracted_to_vec() {
fn test_strings_can_be_extracted_to_vec() {
Python::with_gil(|py| {
let v = "London Calling";
let ob = v.to_object(py);

assert!(ob.extract::<Vec<&str>>(py).is_err());
assert!(ob.extract::<Vec<String>>(py).is_err());
assert!(ob.extract::<Vec<char>>(py).is_err());
assert_eq!(
ob.extract::<Vec<&str>>(py).unwrap(),
["L", "o", "n", "d", "o", "n", " ", "C", "a", "l", "l", "i", "n", "g"]
);
assert_eq!(
ob.extract::<Vec<String>>(py).unwrap(),
["L", "o", "n", "d", "o", "n", " ", "C", "a", "l", "l", "i", "n", "g"]
);
assert_eq!(
ob.extract::<Vec<char>>(py).unwrap(),
['L', 'o', 'n', 'd', 'o', 'n', ' ', 'C', 'a', 'l', 'l', 'i', 'n', 'g']
);
});
}

Expand Down

0 comments on commit 4f7a811

Please sign in to comment.