forked from Rust-related/RustPython
Rename PyBytesLike, PyRwBytesLike, BufOrStr to Arg-prefixed proper names
This commit is contained in:
@@ -12,7 +12,7 @@ use crate::bytesinner::{
|
||||
ByteInnerPaddingOptions, ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs,
|
||||
PyBytesInner,
|
||||
};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
|
||||
use crate::common::lock::{
|
||||
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard,
|
||||
@@ -142,7 +142,7 @@ impl PyByteArray {
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
fn add(&self, other: ArgBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.new_bytearray(self.inner().add(&*other.borrow_buf()))
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ impl PyByteArray {
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn iadd(zelf: PyRef<Self>, other: PyBytesLike, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
fn iadd(zelf: PyRef<Self>, other: ArgBytesLike, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
zelf.try_resizable(vm)?
|
||||
.elements
|
||||
.extend(&*other.borrow_buf());
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::bytesinner::{
|
||||
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
|
||||
ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
|
||||
};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::hash::PyHash;
|
||||
use crate::function::{OptionalArg, OptionalOption};
|
||||
use crate::slots::{AsBuffer, Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
|
||||
@@ -134,7 +134,7 @@ impl PyBytes {
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
fn add(&self, other: ArgBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.new_bytes(self.inner.add(&*other.borrow_buf()))
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ mod decl {
|
||||
use crate::builtins::pystr::{PyStr, PyStrRef};
|
||||
use crate::builtins::pytype::PyTypeRef;
|
||||
use crate::builtins::{PyByteArray, PyBytes};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::{hash::PyHash, str::to_ascii};
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
use crate::compile;
|
||||
@@ -563,7 +563,7 @@ mod decl {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn ord(string: Either<PyBytesLike, PyStrRef>, vm: &VirtualMachine) -> PyResult<u32> {
|
||||
fn ord(string: Either<ArgBytesLike, PyStrRef>, vm: &VirtualMachine) -> PyResult<u32> {
|
||||
match string {
|
||||
Either::A(bytes) => bytes.with_ref(|bytes| {
|
||||
let bytes_len = bytes.len();
|
||||
|
||||
@@ -4,13 +4,17 @@ use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PyBytesLike(PyBufferRef);
|
||||
// Python/getargs.c
|
||||
|
||||
/// any bytes-like object. Like the `y*` format code for `PyArg_Parse` in CPython.
|
||||
#[derive(Debug)]
|
||||
pub struct PyRwBytesLike(PyBufferRef);
|
||||
pub struct ArgBytesLike(PyBufferRef);
|
||||
|
||||
impl PyBytesLike {
|
||||
/// A memory buffer, read-write access. Like the `w*` format code for `PyArg_Parse` in CPython.
|
||||
#[derive(Debug)]
|
||||
pub struct ArgMemoryBuffer(PyBufferRef);
|
||||
|
||||
impl ArgBytesLike {
|
||||
pub fn with_ref<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&[u8]) -> R,
|
||||
@@ -31,7 +35,7 @@ impl PyBytesLike {
|
||||
}
|
||||
}
|
||||
|
||||
impl PyRwBytesLike {
|
||||
impl ArgMemoryBuffer {
|
||||
pub fn with_ref<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut [u8]) -> R,
|
||||
@@ -48,7 +52,7 @@ impl PyRwBytesLike {
|
||||
}
|
||||
}
|
||||
|
||||
impl PyBytesLike {
|
||||
impl ArgBytesLike {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
if buffer.get_options().contiguous {
|
||||
@@ -67,7 +71,7 @@ impl PyBytesLike {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromObject for PyBytesLike {
|
||||
impl TryFromObject for ArgBytesLike {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
Self::new(vm, &obj)
|
||||
}
|
||||
@@ -96,7 +100,7 @@ pub fn try_rw_bytes_like<R>(
|
||||
.ok_or_else(|| vm.new_type_error("buffer is not a read-write bytes-like object".to_owned()))
|
||||
}
|
||||
|
||||
impl PyRwBytesLike {
|
||||
impl ArgMemoryBuffer {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = PyBufferRef::try_from_borrowed_object(vm, obj)?;
|
||||
let options = buffer.get_options();
|
||||
@@ -118,27 +122,27 @@ impl PyRwBytesLike {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromObject for PyRwBytesLike {
|
||||
impl TryFromObject for ArgMemoryBuffer {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
Self::new(vm, &obj)
|
||||
}
|
||||
}
|
||||
|
||||
/// A buffer or utf8 string. Like the `s*` format code for `PyArg_Parse` in CPython.
|
||||
pub enum BufOrStr {
|
||||
Buf(PyBytesLike),
|
||||
/// A text string or bytes-like object. Like the `s*` format code for `PyArg_Parse` in CPython.
|
||||
pub enum ArgStrOrBytesLike {
|
||||
Buf(ArgBytesLike),
|
||||
Str(PyStrRef),
|
||||
}
|
||||
|
||||
impl TryFromObject for BufOrStr {
|
||||
impl TryFromObject for ArgStrOrBytesLike {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
obj.downcast()
|
||||
.map(Self::Str)
|
||||
.or_else(|obj| PyBytesLike::try_from_object(vm, obj).map(Self::Buf))
|
||||
.or_else(|obj| ArgBytesLike::try_from_object(vm, obj).map(Self::Buf))
|
||||
}
|
||||
}
|
||||
|
||||
impl BufOrStr {
|
||||
impl ArgStrOrBytesLike {
|
||||
pub fn borrow_bytes(&self) -> BorrowedValue<'_, [u8]> {
|
||||
match self {
|
||||
Self::Buf(b) => b.borrow_buf(),
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::builtins::pystr::PyStrRef;
|
||||
use crate::builtins::pytype::PyTypeRef;
|
||||
use crate::builtins::slice::PySliceRef;
|
||||
use crate::builtins::{PyByteArray, PyBytes};
|
||||
use crate::byteslike::{try_bytes_like, PyBytesLike};
|
||||
use crate::byteslike::{try_bytes_like, ArgBytesLike};
|
||||
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
|
||||
use crate::common::lock::{
|
||||
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard,
|
||||
@@ -585,7 +585,7 @@ impl PyArray {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn frombytes(zelf: PyRef<Self>, b: PyBytesLike, vm: &VirtualMachine) -> PyResult<()> {
|
||||
fn frombytes(zelf: PyRef<Self>, b: ArgBytesLike, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let b = b.borrow_buf();
|
||||
let itemsize = zelf.read().itemsize();
|
||||
if b.len() % itemsize != 0 {
|
||||
|
||||
@@ -5,7 +5,7 @@ mod decl {
|
||||
use crate::builtins::bytearray::{PyByteArray, PyByteArrayRef};
|
||||
use crate::builtins::bytes::{PyBytes, PyBytesRef};
|
||||
use crate::builtins::pystr::{PyStr, PyStrRef};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::function::OptionalArg;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, TryFromObject, TypeProtocol};
|
||||
@@ -61,7 +61,7 @@ mod decl {
|
||||
|
||||
#[pyfunction(name = "b2a_hex")]
|
||||
#[pyfunction]
|
||||
fn hexlify(data: PyBytesLike) -> Vec<u8> {
|
||||
fn hexlify(data: ArgBytesLike) -> Vec<u8> {
|
||||
data.with_ref(|bytes| {
|
||||
let mut hex = Vec::<u8>::with_capacity(bytes.len() * 2);
|
||||
for b in bytes.iter() {
|
||||
@@ -135,7 +135,7 @@ mod decl {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn b2a_base64(data: PyBytesLike, NewlineArg { newline }: NewlineArg) -> Vec<u8> {
|
||||
fn b2a_base64(data: ArgBytesLike, NewlineArg { newline }: NewlineArg) -> Vec<u8> {
|
||||
#[allow(clippy::redundant_closure)] // https://stackoverflow.com/questions/63916821
|
||||
let mut encoded = data.with_ref(|b| base64::encode(b)).into_bytes();
|
||||
if newline {
|
||||
|
||||
@@ -5,7 +5,7 @@ mod _codecs {
|
||||
use std::ops::Range;
|
||||
|
||||
use crate::builtins::{PyBytesRef, PyStr, PyStrRef, PyTuple};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::codecs;
|
||||
use crate::common::encodings;
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
@@ -202,7 +202,7 @@ mod _codecs {
|
||||
#[derive(FromArgs)]
|
||||
struct DecodeArgs {
|
||||
#[pyarg(positional)]
|
||||
data: PyBytesLike,
|
||||
data: ArgBytesLike,
|
||||
#[pyarg(positional, optional)]
|
||||
errors: Option<PyStrRef>,
|
||||
#[pyarg(positional, default = "false")]
|
||||
|
||||
@@ -4,7 +4,7 @@ pub(crate) use fcntl::make_module;
|
||||
mod fcntl {
|
||||
use super::super::{io, os};
|
||||
use crate::builtins::int;
|
||||
use crate::byteslike::{BufOrStr, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgMemoryBuffer, ArgStrOrBytesLike};
|
||||
use crate::function::OptionalArg;
|
||||
use crate::utils::Either;
|
||||
use crate::PyResult;
|
||||
@@ -25,7 +25,7 @@ mod fcntl {
|
||||
fn fcntl(
|
||||
io::Fildes(fd): io::Fildes,
|
||||
cmd: i32,
|
||||
arg: OptionalArg<Either<BufOrStr, int::PyIntRef>>,
|
||||
arg: OptionalArg<Either<ArgStrOrBytesLike, int::PyIntRef>>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let int = match arg {
|
||||
@@ -59,7 +59,7 @@ mod fcntl {
|
||||
fn ioctl(
|
||||
fd: i32,
|
||||
request: i32,
|
||||
arg: OptionalArg<Either<Either<PyRwBytesLike, BufOrStr>, i32>>,
|
||||
arg: OptionalArg<Either<Either<ArgMemoryBuffer, ArgStrOrBytesLike>, i32>>,
|
||||
mutate_flag: OptionalArg<bool>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
|
||||
@@ -83,7 +83,7 @@ mod _io {
|
||||
bytes::{PyBytes, PyBytesRef},
|
||||
pybool, pytype, PyByteArray, PyStr, PyStrRef, PyTypeRef,
|
||||
};
|
||||
use crate::byteslike::{PyBytesLike, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgBytesLike, ArgMemoryBuffer};
|
||||
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
|
||||
use crate::common::lock::{
|
||||
PyMappedThreadMutexGuard, PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard,
|
||||
@@ -439,7 +439,7 @@ mod _io {
|
||||
let read = vm.get_attribute(instance, "read")?;
|
||||
let mut res = Vec::new();
|
||||
while size.map_or(true, |s| res.len() < s) {
|
||||
let read_res = PyBytesLike::try_from_object(vm, vm.invoke(&read, (1,))?)?;
|
||||
let read_res = ArgBytesLike::try_from_object(vm, vm.invoke(&read, (1,))?)?;
|
||||
if read_res.with_ref(|b| b.is_empty()) {
|
||||
break;
|
||||
}
|
||||
@@ -618,14 +618,14 @@ mod _io {
|
||||
method: &str,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<usize> {
|
||||
let b = PyRwBytesLike::new(vm, &bufobj)?;
|
||||
let b = ArgMemoryBuffer::new(vm, &bufobj)?;
|
||||
let l = b.len();
|
||||
let data = vm.call_method(&zelf, method, (l,))?;
|
||||
if data.is(&bufobj) {
|
||||
return Ok(l);
|
||||
}
|
||||
let mut buf = b.borrow_buf_mut();
|
||||
let data = PyBytesLike::try_from_object(vm, data)?;
|
||||
let data = ArgBytesLike::try_from_object(vm, data)?;
|
||||
let data = data.borrow_buf();
|
||||
match buf.get_mut(..data.len()) {
|
||||
Some(slice) => {
|
||||
@@ -912,7 +912,7 @@ mod _io {
|
||||
Ok(Some(n as usize))
|
||||
}
|
||||
|
||||
fn write(&mut self, obj: PyBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn write(&mut self, obj: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
if !self.valid_read() && !self.valid_write() {
|
||||
self.pos = 0;
|
||||
self.raw_pos = 0;
|
||||
@@ -1624,14 +1624,14 @@ mod _io {
|
||||
Ok(v)
|
||||
}
|
||||
#[pymethod]
|
||||
fn readinto(&self, buf: PyRwBytesLike, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
fn readinto(&self, buf: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
let mut data = self.reader().lock(vm)?;
|
||||
let raw = data.check_init(vm)?;
|
||||
ensure_unclosed(raw, "readinto of closed file", vm)?;
|
||||
data.readinto_generic(buf.into_buffer(), false, vm)
|
||||
}
|
||||
#[pymethod]
|
||||
fn readinto1(&self, buf: PyRwBytesLike, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
fn readinto1(&self, buf: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
let mut data = self.reader().lock(vm)?;
|
||||
let raw = data.check_init(vm)?;
|
||||
ensure_unclosed(raw, "readinto of closed file", vm)?;
|
||||
@@ -1677,7 +1677,7 @@ mod _io {
|
||||
type Writer: BufferedMixin;
|
||||
fn writer(&self) -> &Self::Writer;
|
||||
#[pymethod]
|
||||
fn write(&self, obj: PyBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn write(&self, obj: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
let mut data = self.writer().lock(vm)?;
|
||||
let raw = data.check_init(vm)?;
|
||||
ensure_unclosed(raw, "write to closed file", vm)?;
|
||||
@@ -2946,7 +2946,7 @@ mod _io {
|
||||
let chunk_size = std::cmp::max(self.chunk_size, size_hint);
|
||||
let input_chunk = vm.call_method(&self.buffer, method, (chunk_size,))?;
|
||||
|
||||
let buf = PyBytesLike::new(vm, &input_chunk).map_err(|_| {
|
||||
let buf = ArgBytesLike::new(vm, &input_chunk).map_err(|_| {
|
||||
vm.new_type_error(format!(
|
||||
"underlying {}() should have returned a bytes-like object, not '{}'",
|
||||
method,
|
||||
@@ -3256,7 +3256,7 @@ mod _io {
|
||||
#[pyimpl]
|
||||
impl BytesIORef {
|
||||
#[pymethod]
|
||||
fn write(self, data: PyBytesLike, vm: &VirtualMachine) -> PyResult<u64> {
|
||||
fn write(self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<u64> {
|
||||
let mut buffer = self.try_resizable(vm)?;
|
||||
match data.with_ref(|b| buffer.write(b)) {
|
||||
Some(value) => Ok(value),
|
||||
@@ -3284,7 +3284,7 @@ mod _io {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn readinto(self, obj: PyRwBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn readinto(self, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
let mut buf = self.buffer(vm)?;
|
||||
let ret = buf
|
||||
.cursor
|
||||
@@ -3724,7 +3724,7 @@ mod fileio {
|
||||
use super::Offset;
|
||||
use super::_io::*;
|
||||
use crate::builtins::{PyStr, PyStrRef, PyTypeRef};
|
||||
use crate::byteslike::{PyBytesLike, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgBytesLike, ArgMemoryBuffer};
|
||||
use crate::crt_fd::Fd;
|
||||
use crate::exceptions::IntoPyException;
|
||||
use crate::function::OptionalOption;
|
||||
@@ -4025,7 +4025,7 @@ mod fileio {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn readinto(&self, obj: PyRwBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn readinto(&self, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
if !self.mode.load().contains(Mode::READABLE) {
|
||||
return Err(new_unsupported_operation(
|
||||
vm,
|
||||
@@ -4043,7 +4043,7 @@ mod fileio {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn write(&self, obj: PyBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn write(&self, obj: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
if !self.mode.load().contains(Mode::WRITABLE) {
|
||||
return Err(new_unsupported_operation(
|
||||
vm,
|
||||
|
||||
@@ -5,7 +5,7 @@ mod decl {
|
||||
use crate::builtins::bytes::PyBytes;
|
||||
use crate::builtins::code::{PyCode, PyCodeRef};
|
||||
use crate::bytecode;
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{PyObjectRef, PyResult, TryFromObject};
|
||||
|
||||
@@ -21,7 +21,7 @@ mod decl {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn loads(code_bytes: PyBytesLike, vm: &VirtualMachine) -> PyResult<PyCode> {
|
||||
fn loads(code_bytes: ArgBytesLike, vm: &VirtualMachine) -> PyResult<PyCode> {
|
||||
let code =
|
||||
bytecode::CodeObject::from_bytes(&*code_bytes.borrow_buf()).map_err(|e| match e {
|
||||
bytecode::CodeDeserializeError::Eof => vm.new_exception_msg(
|
||||
@@ -38,7 +38,7 @@ mod decl {
|
||||
#[pyfunction]
|
||||
fn load(f: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyCode> {
|
||||
let read_res = vm.call_method(&f, "read", ())?;
|
||||
let bytes = PyBytesLike::try_from_object(vm, read_res)?;
|
||||
let bytes = ArgBytesLike::try_from_object(vm, read_res)?;
|
||||
loads(bytes, vm)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ pub(crate) use _multiprocessing::make_module;
|
||||
#[pymodule]
|
||||
mod _multiprocessing {
|
||||
use super::super::os;
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::PyResult;
|
||||
use crate::VirtualMachine;
|
||||
use winapi::um::winsock2::{self, SOCKET};
|
||||
@@ -32,7 +32,7 @@ mod _multiprocessing {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn send(socket: usize, buf: PyBytesLike, vm: &VirtualMachine) -> PyResult<libc::c_int> {
|
||||
fn send(socket: usize, buf: ArgBytesLike, vm: &VirtualMachine) -> PyResult<libc::c_int> {
|
||||
let ret = buf.with_ref(|b| unsafe {
|
||||
winsock2::send(socket as SOCKET, b.as_ptr() as *const _, b.len() as i32, 0)
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@ mod _operator {
|
||||
use crate::builtins::pystr::PyStrRef;
|
||||
use crate::builtins::PyInt;
|
||||
use crate::builtins::PyTypeRef;
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::cmp;
|
||||
use crate::function::FuncArgs;
|
||||
use crate::function::KwArgs;
|
||||
@@ -402,8 +402,8 @@ mod _operator {
|
||||
/// types and lengths of a and b--but not their values.
|
||||
#[pyfunction]
|
||||
fn _compare_digest(
|
||||
a: Either<PyStrRef, PyBytesLike>,
|
||||
b: Either<PyStrRef, PyBytesLike>,
|
||||
a: Either<PyStrRef, ArgBytesLike>,
|
||||
b: Either<PyStrRef, ArgBytesLike>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<bool> {
|
||||
let res = match (a, b) {
|
||||
|
||||
@@ -21,7 +21,7 @@ use crate::builtins::pystr::{PyStr, PyStrRef};
|
||||
use crate::builtins::pytype::PyTypeRef;
|
||||
use crate::builtins::set::PySet;
|
||||
use crate::builtins::tuple::{PyTuple, PyTupleRef};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::lock::PyRwLock;
|
||||
use crate::exceptions::{IntoPyException, PyBaseExceptionRef};
|
||||
use crate::function::{ArgumentError, FromArgs, FuncArgs, OptionalArg};
|
||||
@@ -541,10 +541,10 @@ mod _os {
|
||||
fn _extract_vec_bytes(
|
||||
x: OptionalArg,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<Option<Vec<PyBytesLike>>> {
|
||||
) -> PyResult<Option<Vec<ArgBytesLike>>> {
|
||||
let inner = match x.into_option() {
|
||||
Some(v) => {
|
||||
let v = vm.extract_elements::<PyBytesLike>(&v)?;
|
||||
let v = vm.extract_elements::<ArgBytesLike>(&v)?;
|
||||
if v.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -575,7 +575,6 @@ mod _os {
|
||||
let headers = headers.as_deref();
|
||||
|
||||
let trailers = _extract_vec_bytes(args.trailers, vm)?;
|
||||
|
||||
let trailers = trailers
|
||||
.as_ref()
|
||||
.map(|v| v.iter().map(|b| b.borrow_buf()).collect::<Vec<_>>());
|
||||
@@ -614,7 +613,7 @@ mod _os {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn write(fd: i32, data: PyBytesLike, vm: &VirtualMachine) -> PyResult {
|
||||
fn write(fd: i32, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult {
|
||||
let mut file = Fd(fd);
|
||||
let written = data
|
||||
.with_ref(|b| file.write(b))
|
||||
|
||||
@@ -33,7 +33,7 @@ macro_rules! create_property {
|
||||
#[pymodule(name = "pyexpat")]
|
||||
mod _pyexpat {
|
||||
use crate::builtins::{PyStr, PyStrRef, PyTypeRef};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::function::{IntoFuncArgs, OptionalArg};
|
||||
use crate::pyobject::StaticType;
|
||||
use crate::{
|
||||
@@ -173,7 +173,7 @@ mod _pyexpat {
|
||||
fn parse_file(&self, file: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
// todo: read chunks at a time
|
||||
let read_res = vm.call_method(&file, "read", ())?;
|
||||
let bytes_like = PyBytesLike::try_from_object(vm, read_res)?;
|
||||
let bytes_like = ArgBytesLike::try_from_object(vm, read_res)?;
|
||||
let buf = bytes_like.borrow_buf().to_vec();
|
||||
let reader = Cursor::new(buf);
|
||||
let parser = self.create_config().create_reader(reader);
|
||||
|
||||
@@ -23,7 +23,7 @@ pub(crate) mod _struct {
|
||||
bytes::PyBytesRef, float, int::try_to_primitive, pybool::IntoPyBool, pystr::PyStr,
|
||||
pystr::PyStrRef, pytype::PyTypeRef, tuple::PyTupleRef,
|
||||
};
|
||||
use crate::byteslike::{PyBytesLike, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgBytesLike, ArgMemoryBuffer};
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
use crate::function::Args;
|
||||
use crate::slots::PyIter;
|
||||
@@ -676,7 +676,7 @@ pub(crate) mod _struct {
|
||||
}
|
||||
|
||||
fn pack_string(vm: &VirtualMachine, arg: PyObjectRef, buf: &mut [u8]) -> PyResult<()> {
|
||||
let b = PyBytesLike::try_from_object(vm, arg)?;
|
||||
let b = ArgBytesLike::try_from_object(vm, arg)?;
|
||||
b.with_ref(|data| write_string(buf, data));
|
||||
Ok(())
|
||||
}
|
||||
@@ -685,7 +685,7 @@ pub(crate) mod _struct {
|
||||
if buf.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let b = PyBytesLike::try_from_object(vm, arg)?;
|
||||
let b = ArgBytesLike::try_from_object(vm, arg)?;
|
||||
b.with_ref(|data| {
|
||||
let string_length = std::cmp::min(std::cmp::min(data.len(), 255), buf.len() - 1);
|
||||
buf[0] = string_length as u8;
|
||||
@@ -727,7 +727,7 @@ pub(crate) mod _struct {
|
||||
#[pyfunction]
|
||||
fn pack_into(
|
||||
fmt: Either<PyStrRef, PyBytesRef>,
|
||||
buffer: PyRwBytesLike,
|
||||
buffer: ArgMemoryBuffer,
|
||||
offset: isize,
|
||||
args: Args,
|
||||
vm: &VirtualMachine,
|
||||
@@ -756,7 +756,7 @@ pub(crate) mod _struct {
|
||||
#[pyfunction]
|
||||
fn unpack(
|
||||
fmt: Either<PyStrRef, PyBytesRef>,
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyTupleRef> {
|
||||
let format_spec = FormatSpec::decode_and_parse(vm, &fmt)?;
|
||||
@@ -765,7 +765,7 @@ pub(crate) mod _struct {
|
||||
|
||||
#[derive(FromArgs)]
|
||||
struct UpdateFromArgs {
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
#[pyarg(any, default = "0")]
|
||||
offset: isize,
|
||||
}
|
||||
@@ -788,7 +788,7 @@ pub(crate) mod _struct {
|
||||
#[derive(Debug)]
|
||||
struct UnpackIterator {
|
||||
format_spec: FormatSpec,
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
offset: AtomicCell<usize>,
|
||||
}
|
||||
|
||||
@@ -796,7 +796,7 @@ pub(crate) mod _struct {
|
||||
fn new(
|
||||
vm: &VirtualMachine,
|
||||
format_spec: FormatSpec,
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
) -> PyResult<UnpackIterator> {
|
||||
if format_spec.size == 0 {
|
||||
Err(new_struct_error(
|
||||
@@ -851,7 +851,7 @@ pub(crate) mod _struct {
|
||||
#[pyfunction]
|
||||
fn iter_unpack(
|
||||
fmt: Either<PyStrRef, PyBytesRef>,
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<UnpackIterator> {
|
||||
let format_spec = FormatSpec::decode_and_parse(vm, &fmt)?;
|
||||
@@ -914,7 +914,7 @@ pub(crate) mod _struct {
|
||||
#[pymethod]
|
||||
fn pack_into(
|
||||
&self,
|
||||
buffer: PyRwBytesLike,
|
||||
buffer: ArgMemoryBuffer,
|
||||
offset: isize,
|
||||
args: Args,
|
||||
vm: &VirtualMachine,
|
||||
@@ -927,7 +927,7 @@ pub(crate) mod _struct {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn unpack(&self, data: PyBytesLike, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
|
||||
fn unpack(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
|
||||
data.with_ref(|buf| self.spec.unpack(buf, vm))
|
||||
}
|
||||
|
||||
@@ -941,7 +941,7 @@ pub(crate) mod _struct {
|
||||
#[pymethod]
|
||||
fn iter_unpack(
|
||||
&self,
|
||||
buffer: PyBytesLike,
|
||||
buffer: ArgBytesLike,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<UnpackIterator> {
|
||||
UnpackIterator::new(vm, self.spec.clone(), buffer)
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::builtins::int;
|
||||
use crate::builtins::pystr::PyStrRef;
|
||||
use crate::builtins::pytype::PyTypeRef;
|
||||
use crate::builtins::tuple::PyTupleRef;
|
||||
use crate::byteslike::{PyBytesLike, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgBytesLike, ArgMemoryBuffer};
|
||||
use crate::common::lock::{PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard};
|
||||
use crate::exceptions::{IntoPyException, PyBaseExceptionRef};
|
||||
use crate::function::{FuncArgs, OptionalArg, OptionalOption};
|
||||
@@ -384,7 +384,7 @@ impl PySocket {
|
||||
#[cfg(unix)]
|
||||
c::AF_UNIX => {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
let buf = crate::byteslike::BufOrStr::try_from_object(vm, addr)?;
|
||||
let buf = crate::byteslike::ArgStrOrBytesLike::try_from_object(vm, addr)?;
|
||||
let path = &*buf.borrow_bytes();
|
||||
if cfg!(any(target_os = "linux", target_os = "android")) && path.first() == Some(&0)
|
||||
{
|
||||
@@ -593,7 +593,7 @@ impl PySocket {
|
||||
#[pymethod]
|
||||
fn recv_into(
|
||||
&self,
|
||||
buf: PyRwBytesLike,
|
||||
buf: ArgMemoryBuffer,
|
||||
flags: OptionalArg<i32>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<usize> {
|
||||
@@ -629,7 +629,7 @@ impl PySocket {
|
||||
#[pymethod]
|
||||
fn recvfrom_into(
|
||||
&self,
|
||||
buf: PyRwBytesLike,
|
||||
buf: ArgMemoryBuffer,
|
||||
nbytes: OptionalArg<isize>,
|
||||
flags: OptionalArg<i32>,
|
||||
vm: &VirtualMachine,
|
||||
@@ -658,7 +658,7 @@ impl PySocket {
|
||||
#[pymethod]
|
||||
fn send(
|
||||
&self,
|
||||
bytes: PyBytesLike,
|
||||
bytes: ArgBytesLike,
|
||||
flags: OptionalArg<i32>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<usize> {
|
||||
@@ -673,7 +673,7 @@ impl PySocket {
|
||||
#[pymethod]
|
||||
fn sendall(
|
||||
&self,
|
||||
bytes: PyBytesLike,
|
||||
bytes: ArgBytesLike,
|
||||
flags: OptionalArg<i32>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
@@ -706,7 +706,7 @@ impl PySocket {
|
||||
#[pymethod]
|
||||
fn sendto(
|
||||
&self,
|
||||
bytes: PyBytesLike,
|
||||
bytes: ArgBytesLike,
|
||||
arg2: PyObjectRef,
|
||||
arg3: OptionalArg<PyObjectRef>,
|
||||
vm: &VirtualMachine,
|
||||
@@ -853,7 +853,7 @@ impl PySocket {
|
||||
&self,
|
||||
level: i32,
|
||||
name: i32,
|
||||
value: Option<Either<PyBytesLike, i32>>,
|
||||
value: Option<Either<ArgBytesLike, i32>>,
|
||||
optlen: OptionalArg<u32>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
@@ -1058,7 +1058,7 @@ fn _socket_inet_aton(ip_string: PyStrRef, vm: &VirtualMachine) -> PyResult<Vec<u
|
||||
.map_err(|_| vm.new_os_error("illegal IP address string passed to inet_aton".to_owned()))
|
||||
}
|
||||
|
||||
fn _socket_inet_ntoa(packed_ip: PyBytesLike, vm: &VirtualMachine) -> PyResult {
|
||||
fn _socket_inet_ntoa(packed_ip: ArgBytesLike, vm: &VirtualMachine) -> PyResult {
|
||||
let packed_ip = packed_ip.borrow_buf();
|
||||
let packed_ip = <&[u8; 4]>::try_from(&*packed_ip)
|
||||
.map_err(|_| vm.new_os_error("packed IP wrong length for inet_ntoa".to_owned()))?;
|
||||
@@ -1325,7 +1325,7 @@ fn _socket_inet_pton(af_inet: i32, ip_string: PyStrRef, vm: &VirtualMachine) ->
|
||||
|
||||
fn _socket_inet_ntop(
|
||||
af_inet: i32,
|
||||
packed_ip: PyBytesLike,
|
||||
packed_ip: ArgBytesLike,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<String> {
|
||||
let packed_ip = packed_ip.borrow_buf();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::os::PyPathLike;
|
||||
use super::socket::{self, PySocketRef};
|
||||
use crate::builtins::{pytype, weakref::PyWeak, PyStrRef, PyTypeRef};
|
||||
use crate::byteslike::{PyBytesLike, PyRwBytesLike};
|
||||
use crate::byteslike::{ArgBytesLike, ArgMemoryBuffer};
|
||||
use crate::common::lock::{PyRwLock, PyRwLockWriteGuard};
|
||||
use crate::exceptions::{create_exception_type, IntoPyException, PyBaseExceptionRef};
|
||||
use crate::function::OptionalArg;
|
||||
@@ -216,7 +216,7 @@ fn _ssl_rand_status() -> i32 {
|
||||
unsafe { sys::RAND_status() }
|
||||
}
|
||||
|
||||
fn _ssl_rand_add(string: Either<PyStrRef, PyBytesLike>, entropy: f64) {
|
||||
fn _ssl_rand_add(string: Either<PyStrRef, ArgBytesLike>, entropy: f64) {
|
||||
let f = |b: &[u8]| {
|
||||
for buf in b.chunks(libc::c_int::max_value() as usize) {
|
||||
unsafe { sys::RAND_add(buf.as_ptr() as *const _, buf.len() as _, entropy) }
|
||||
@@ -419,7 +419,7 @@ impl PySslContext {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn _set_alpn_protocols(&self, protos: PyBytesLike, vm: &VirtualMachine) -> PyResult<()> {
|
||||
fn _set_alpn_protocols(&self, protos: ArgBytesLike, vm: &VirtualMachine) -> PyResult<()> {
|
||||
#[cfg(ossl102)]
|
||||
{
|
||||
let mut ctx = self.builder();
|
||||
@@ -632,7 +632,7 @@ struct LoadVerifyLocationsArgs {
|
||||
#[pyarg(any, default)]
|
||||
capath: Option<PyStrRef>,
|
||||
#[pyarg(any, default)]
|
||||
cadata: Option<Either<PyStrRef, PyBytesLike>>,
|
||||
cadata: Option<Either<PyStrRef, ArgBytesLike>>,
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
@@ -848,7 +848,7 @@ impl PySslSocket {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn write(&self, data: PyBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
fn write(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
let mut stream = self.stream.write();
|
||||
let data = data.borrow_buf();
|
||||
let data = &*data;
|
||||
@@ -890,7 +890,12 @@ impl PySslSocket {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn read(&self, n: usize, buffer: OptionalArg<PyRwBytesLike>, vm: &VirtualMachine) -> PyResult {
|
||||
fn read(
|
||||
&self,
|
||||
n: usize,
|
||||
buffer: OptionalArg<ArgMemoryBuffer>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let mut stream = self.stream.write();
|
||||
let mut inner_buffer = if let OptionalArg::Present(buffer) = &buffer {
|
||||
Either::A(buffer.borrow_buf_mut())
|
||||
|
||||
@@ -5,7 +5,7 @@ mod decl {
|
||||
use crate::builtins::bytes::{PyBytes, PyBytesRef};
|
||||
use crate::builtins::int::{self, PyIntRef};
|
||||
use crate::builtins::pytype::PyTypeRef;
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::byteslike::ArgBytesLike;
|
||||
use crate::common::lock::PyMutex;
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
use crate::function::OptionalArg;
|
||||
@@ -66,7 +66,7 @@ mod decl {
|
||||
|
||||
/// Compute an Adler-32 checksum of data.
|
||||
#[pyfunction]
|
||||
fn adler32(data: PyBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
|
||||
fn adler32(data: ArgBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
|
||||
data.with_ref(|data| {
|
||||
let begin_state = begin_state.map_or(1, |i| int::bigint_unsigned_mask(i.as_bigint()));
|
||||
|
||||
@@ -78,7 +78,7 @@ mod decl {
|
||||
|
||||
/// Compute a CRC-32 checksum of data.
|
||||
#[pyfunction]
|
||||
fn crc32(data: PyBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
|
||||
fn crc32(data: ArgBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
|
||||
data.with_ref(|data| {
|
||||
let begin_state = begin_state.map_or(0, |i| int::bigint_unsigned_mask(i.as_bigint()));
|
||||
|
||||
@@ -100,7 +100,7 @@ mod decl {
|
||||
|
||||
/// Returns a bytes object containing compressed data.
|
||||
#[pyfunction]
|
||||
fn compress(data: PyBytesLike, level: OptionalArg<i32>, vm: &VirtualMachine) -> PyResult {
|
||||
fn compress(data: ArgBytesLike, level: OptionalArg<i32>, vm: &VirtualMachine) -> PyResult {
|
||||
let compression = compression_from_int(level.into_option())
|
||||
.ok_or_else(|| new_zlib_error("Bad compression level", vm))?;
|
||||
|
||||
@@ -230,7 +230,7 @@ mod decl {
|
||||
/// Returns a bytes object containing the uncompressed data.
|
||||
#[pyfunction]
|
||||
fn decompress(
|
||||
data: PyBytesLike,
|
||||
data: ArgBytesLike,
|
||||
wbits: OptionalArg<i8>,
|
||||
bufsize: OptionalArg<usize>,
|
||||
vm: &VirtualMachine,
|
||||
@@ -396,7 +396,7 @@ mod decl {
|
||||
#[derive(FromArgs)]
|
||||
struct DecompressArgs {
|
||||
#[pyarg(positional)]
|
||||
data: PyBytesLike,
|
||||
data: ArgBytesLike,
|
||||
#[pyarg(any, default = "0")]
|
||||
max_length: usize,
|
||||
}
|
||||
@@ -407,7 +407,7 @@ mod decl {
|
||||
wbits: OptionalArg<i8>,
|
||||
#[cfg(feature = "zlib")]
|
||||
#[pyarg(any, optional)]
|
||||
zdict: OptionalArg<PyBytesLike>,
|
||||
zdict: OptionalArg<ArgBytesLike>,
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -419,7 +419,7 @@ mod decl {
|
||||
// these aren't used.
|
||||
_mem_level: OptionalArg<i32>, // this is memLevel in CPython
|
||||
_strategy: OptionalArg<i32>,
|
||||
_zdict: OptionalArg<PyBytesLike>,
|
||||
_zdict: OptionalArg<ArgBytesLike>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyCompress> {
|
||||
let level = compression_from_int(level.into_option())
|
||||
@@ -455,7 +455,7 @@ mod decl {
|
||||
#[pyimpl]
|
||||
impl PyCompress {
|
||||
#[pymethod]
|
||||
fn compress(&self, data: PyBytesLike, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
|
||||
fn compress(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
|
||||
let mut inner = self.inner.lock();
|
||||
data.with_ref(|b| inner.compress(b, vm))
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use js_sys::{Array, ArrayBuffer, Object, Promise, Reflect, SyntaxError, Uint8Arr
|
||||
use wasm_bindgen::{closure::Closure, prelude::*, JsCast};
|
||||
|
||||
use rustpython_parser::error::ParseErrorType;
|
||||
use rustpython_vm::byteslike::PyBytesLike;
|
||||
use rustpython_vm::byteslike::ArgBytesLike;
|
||||
use rustpython_vm::compile::{CompileError, CompileErrorType};
|
||||
use rustpython_vm::exceptions::PyBaseExceptionRef;
|
||||
use rustpython_vm::function::FuncArgs;
|
||||
@@ -139,7 +139,7 @@ pub fn py_to_js(vm: &VirtualMachine, py_obj: PyObjectRef) -> JsValue {
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(bytes) = PyBytesLike::try_from_object(vm, py_obj.clone()) {
|
||||
if let Ok(bytes) = ArgBytesLike::try_from_object(vm, py_obj.clone()) {
|
||||
bytes.with_ref(|bytes| unsafe {
|
||||
// `Uint8Array::view` is an `unsafe fn` because it provides
|
||||
// a direct view into the WASM linear memory; if you were to allocate
|
||||
|
||||
Reference in New Issue
Block a user