Skip to content
Merged
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 newsfragments/5667.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`FromPyObject` on `Cow<[u8]>` allow any `Sequence[int]` and change the error type to `PyErr`
4 changes: 2 additions & 2 deletions pytests/stubs/buf_and_str.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BytesExtractor:
@staticmethod
def from_str_lossy(string: str) -> int: ...

def map_byte_cow(bytes: bytes | bytearray) -> bytes: ...
def map_byte_cow(bytes: Sequence[int]) -> bytes: ...
def map_byte_slice(bytes: bytes) -> bytes: ...
def map_byte_vec(bytes: bytes | bytearray | Sequence[int]) -> bytes: ...
def map_byte_vec(bytes: Sequence[int]) -> bytes: ...
def return_memoryview() -> memoryview: ...
10 changes: 2 additions & 8 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::inspect::types::TypeInfo;
use crate::inspect::TypeHint;
use crate::pyclass::boolean_struct::False;
use crate::pyclass::{PyClassGuardError, PyClassGuardMutError};
use crate::types::PyTuple;
#[cfg(feature = "experimental-inspect")]
use crate::types::{PyList, PySequence};
use crate::types::PyList;
use crate::types::PyTuple;
use crate::{
Borrowed, Bound, BoundObject, Py, PyAny, PyClass, PyClassGuard, PyErr, PyRef, PyRefMut,
PyTypeCheck, Python,
Expand Down Expand Up @@ -463,12 +463,6 @@ pub trait FromPyObject<'a, 'py>: Sized {
Option::<NeverASequence<Self>>::None
}

/// The union of Sequence[Self::INPUT_TYPE] and the input sequence extraction function [`FromPyObject::sequence_extractor`] if it's defined
#[cfg(feature = "experimental-inspect")]
#[doc(hidden)]
const SEQUENCE_INPUT_TYPE: TypeHint =
TypeHint::subscript(&PySequence::TYPE_HINT, &[Self::INPUT_TYPE]);

/// Helper used to make a specialized path in extracting `DateTime<Tz>` where `Tz` is
/// `chrono::Local`, which will accept "naive" datetime objects as being in the local timezone.
#[cfg(feature = "chrono-local")]
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
type Error = PyErr;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = T::SEQUENCE_INPUT_TYPE;
const INPUT_TYPE: TypeHint = TypeHint::subscript(&PySequence::TYPE_HINT, &[T::INPUT_TYPE]);

fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
if let Some(extractor) = T::sequence_extractor(obj, crate::conversion::private::Token) {
Expand Down
9 changes: 0 additions & 9 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::inspect::TypeHint;
use crate::py_result_ext::PyResultExt;
#[cfg(feature = "experimental-inspect")]
use crate::type_object::PyTypeInfo;
#[cfg(feature = "experimental-inspect")]
use crate::types::PySequence;
use crate::types::{PyByteArray, PyByteArrayMethods, PyBytes, PyInt};
use crate::{exceptions, ffi, Borrowed, Bound, FromPyObject, PyAny, PyErr, PyResult, Python};
use std::convert::Infallible;
Expand Down Expand Up @@ -322,13 +320,6 @@ impl<'py> FromPyObject<'_, 'py> for u8 {
None
}
}

#[cfg(feature = "experimental-inspect")]
const SEQUENCE_INPUT_TYPE: TypeHint = TypeHint::union(&[
PyBytes::TYPE_HINT,
PyByteArray::TYPE_HINT,
TypeHint::subscript(&PySequence::TYPE_HINT, &[Self::INPUT_TYPE]),
]);
}

pub(crate) enum BytesSequenceExtractor<'a, 'py> {
Expand Down
23 changes: 10 additions & 13 deletions src/conversions/std/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::inspect::TypeHint;
#[cfg(feature = "experimental-inspect")]
use crate::type_object::PyTypeInfo;
use crate::{
conversion::IntoPyObject,
types::{PyByteArray, PyByteArrayMethods, PyBytes},
Bound, CastError, PyAny, PyErr, Python,
conversion::IntoPyObject, types::PyBytes, Bound, CastError, PyAny, PyErr, PyResult, Python,
};

impl<'a, 'py, T> IntoPyObject<'py> for &'a [T]
Expand Down Expand Up @@ -63,18 +61,17 @@ impl<'a, 'py> crate::conversion::FromPyObject<'a, 'py> for &'a [u8] {
/// pointing into the source object, and no copying or heap allocations will happen.
/// If it is a `bytearray`, its contents will be copied to an owned `Cow`.
impl<'a, 'py> crate::conversion::FromPyObject<'a, 'py> for Cow<'a, [u8]> {
type Error = CastError<'a, 'py>;
type Error = PyErr;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed because conversion from arbitrary sequences can raise Python exceptions


#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = TypeHint::union(&[PyBytes::TYPE_HINT, PyByteArray::TYPE_HINT]);

fn extract(ob: crate::Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
if let Ok(bytes) = ob.cast::<PyBytes>() {
return Ok(Cow::Borrowed(bytes.as_bytes()));
}

let byte_array = ob.cast::<PyByteArray>()?;
Ok(Cow::Owned(byte_array.to_vec()))
const INPUT_TYPE: TypeHint = Vec::<u8>::INPUT_TYPE;

fn extract(ob: crate::Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
Ok(if let Ok(bytes) = ob.cast::<PyBytes>() {
Cow::Borrowed(bytes.as_bytes()) // It's immutable, we can take a slice
} else {
Cow::Owned(Vec::extract(ob)?) // Not possible to take a slice, we have to build a Vec<u8>
})
}

#[cfg(feature = "experimental-inspect")]
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
type Error = PyErr;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = T::SEQUENCE_INPUT_TYPE;
const INPUT_TYPE: TypeHint = TypeHint::subscript(&PySequence::TYPE_HINT, &[T::INPUT_TYPE]);

fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
if let Some(extractor) = T::sequence_extractor(obj, crate::conversion::private::Token) {
Expand Down
Loading