Skip to content

Commit

Permalink
Refactor to only raise an error based on isinstance
Browse files Browse the repository at this point in the history
  • Loading branch information
jeertmans committed Jul 11, 2022
1 parent d9f90f5 commit 6b01925
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![cfg_attr(
feature = "nightly",
feature(auto_traits, negative_impls, min_specialization)
)]
#![cfg_attr(feature = "nightly", feature(auto_traits, negative_impls))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(
docsrs, // rustdoc:: is not supported on msrv
Expand Down
43 changes: 5 additions & 38 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions::PyValueError;
use crate::internal_tricks::get_ssize_index;
#[cfg(feature = "nightly")]
use crate::types::PyString;
use crate::types::{PyAny, PyList, PyTuple};
use crate::types::{PyAny, PyList, PyString, PyTuple};
use crate::{ffi, PyNativeType, ToPyObject};
use crate::{AsPyPointer, IntoPyPointer, Py, Python};
use crate::{FromPyObject, PyTryFrom};
Expand Down Expand Up @@ -266,50 +265,18 @@ fn sequence_slice(seq: &PySequence, start: usize, end: usize) -> &PySequence {

index_impls!(PySequence, "sequence", sequence_len, sequence_slice);

#[cfg(not(feature = "nightly"))]
impl<'a, T> FromPyObject<'a> for Vec<T>
where
T: FromPyObject<'a>,
{
fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj)
}
}

#[cfg(feature = "nightly")]
impl<'a, T> FromPyObject<'a> for Vec<T>
where
T: FromPyObject<'a>,
{
default fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj)
}
}

#[cfg(feature = "nightly")]
impl<'a> FromPyObject<'a> for Vec<String> {
fn extract(obj: &'a PyAny) -> PyResult<Self> {
if obj.is_instance_of::<PyString>()? {
Ok(vec![FromPyObject::extract(obj)?])
} else {
extract_sequence(obj)
if let Ok(true) = obj.is_instance_of::<PyString>() {
return Err(PyValueError::new_err("Can't extract `str` to `Vec`"));
}
extract_sequence(obj)
}
}

/*
#[cfg(feature = "nightly")]
impl<'a> FromPyObject<'a> for Vec<&'a str>
{
fn extract(obj: &'a PyAny) -> PyResult<Self> {
if obj.is_instance_of::<PyString>()? {
Ok(vec![FromPyObject::extract(obj)?])
} else {
extract_sequence(obj)
}
}
}*/

fn extract_sequence<'s, T>(obj: &'s PyAny) -> PyResult<Vec<T>>
where
T: FromPyObject<'s>,
Expand Down

0 comments on commit 6b01925

Please sign in to comment.