mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
TryFromBorrowedObjcet for PyBuffer
This commit is contained in:
@@ -19,7 +19,7 @@ use crate::{
|
||||
IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
|
||||
PyResult, PyThreadingConstraint, PyValue, TypeProtocol,
|
||||
};
|
||||
use crate::{TryFromObject, VirtualMachine};
|
||||
use crate::{TryFromBorrowedObject, TryFromObject, VirtualMachine};
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
use itertools::Itertools;
|
||||
use num_bigint::BigInt;
|
||||
@@ -28,9 +28,8 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
|
||||
#[derive(Debug)]
|
||||
pub struct PyBufferRef(Box<dyn PyBuffer>);
|
||||
|
||||
impl TryFromObject for PyBufferRef {
|
||||
// FIXME: `obj: &PyObjectRef` is enough
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
impl TryFromBorrowedObject for PyBufferRef {
|
||||
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let obj_cls = obj.class();
|
||||
for cls in obj_cls.iter_mro() {
|
||||
if let Some(f) = cls.slots.as_buffer.as_ref() {
|
||||
@@ -277,7 +276,7 @@ impl PyMemoryView {
|
||||
args: PyMemoryViewNewArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
let buffer = PyBufferRef::try_from_object(vm, args.object.clone())?;
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, &args.object)?;
|
||||
let zelf = PyMemoryView::from_buffer(args.object, buffer, vm)?;
|
||||
zelf.into_ref_with_type(vm, cls)
|
||||
}
|
||||
@@ -782,7 +781,7 @@ impl PyMemoryView {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let other = match PyBufferRef::try_from_object(vm, other.clone()) {
|
||||
let other = match PyBufferRef::try_from_borrowed_object(vm, other) {
|
||||
Ok(buf) => buf,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::builtins::memory::PyBufferRef;
|
||||
use crate::builtins::PyStrRef;
|
||||
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, TryFromObject};
|
||||
use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PyBytesLike(PyBufferRef);
|
||||
@@ -50,7 +50,7 @@ impl PyRwBytesLike {
|
||||
|
||||
impl PyBytesLike {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = PyBufferRef::try_from_object(vm, obj.clone())?;
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
if buffer.get_options().contiguous {
|
||||
Ok(Self(buffer))
|
||||
} else {
|
||||
@@ -78,7 +78,7 @@ pub fn try_bytes_like<R>(
|
||||
obj: &PyObjectRef,
|
||||
f: impl FnOnce(&[u8]) -> R,
|
||||
) -> PyResult<R> {
|
||||
let buffer = PyBufferRef::try_from_object(vm, obj.clone())?;
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
buffer.as_contiguous().map(|x| f(&*x)).ok_or_else(|| {
|
||||
vm.new_type_error("non-contiguous buffer is not a bytes-like object".to_owned())
|
||||
})
|
||||
@@ -89,7 +89,7 @@ pub fn try_rw_bytes_like<R>(
|
||||
obj: &PyObjectRef,
|
||||
f: impl FnOnce(&mut [u8]) -> R,
|
||||
) -> PyResult<R> {
|
||||
let buffer = PyBufferRef::try_from_object(vm, obj.clone())?;
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
buffer
|
||||
.as_contiguous_mut()
|
||||
.map(|mut x| f(&mut *x))
|
||||
@@ -98,7 +98,7 @@ pub fn try_rw_bytes_like<R>(
|
||||
|
||||
impl PyRwBytesLike {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = PyBufferRef::try_from_object(vm, obj.clone())?;
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
let options = buffer.get_options();
|
||||
if !options.contiguous {
|
||||
Err(vm.new_type_error("non-contiguous buffer is not a bytes-like object".to_owned()))
|
||||
|
||||
@@ -7,7 +7,9 @@ use crate::builtins::pystr::PyStr;
|
||||
use crate::builtins::{tuple, PyBytes};
|
||||
use crate::common::float_ops;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{ItemProtocol, PyObjectRef, PyResult, TryFromObject, TypeProtocol};
|
||||
use crate::{
|
||||
ItemProtocol, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use num_bigint::{BigInt, Sign};
|
||||
use num_traits::cast::ToPrimitive;
|
||||
@@ -364,7 +366,7 @@ impl CFormatSpec {
|
||||
Ok(s.into_bytes())
|
||||
}
|
||||
CFormatPreconversor::Str | CFormatPreconversor::Bytes => {
|
||||
if let Ok(buffer) = PyBufferRef::try_from_object(vm, obj.clone()) {
|
||||
if let Ok(buffer) = PyBufferRef::try_from_borrowed_object(vm, &obj) {
|
||||
let guard;
|
||||
let vec;
|
||||
let bytes = match buffer.as_contiguous() {
|
||||
|
||||
@@ -18,7 +18,7 @@ mod _sre {
|
||||
use crate::VirtualMachine;
|
||||
use crate::{
|
||||
IntoPyObject, ItemProtocol, PyCallable, PyComparisonValue, PyObjectRef, PyRef, PyResult,
|
||||
PyValue, StaticType, TryFromObject,
|
||||
PyValue, StaticType, TryFromBorrowedObject, TryFromObject,
|
||||
};
|
||||
use core::str;
|
||||
use sre_engine::constants::SreFlag;
|
||||
@@ -153,7 +153,7 @@ mod _sre {
|
||||
let vec;
|
||||
let s;
|
||||
let str_drive = if self.isbytes {
|
||||
buffer = PyBufferRef::try_from_object(vm, string.clone())?;
|
||||
buffer = PyBufferRef::try_from_borrowed_object(vm, &string)?;
|
||||
let bytes = match buffer.as_contiguous() {
|
||||
Some(bytes) => {
|
||||
guard = bytes;
|
||||
|
||||
Reference in New Issue
Block a user