cleanup bytearray

This commit is contained in:
Jeong YunWon
2020-01-10 21:30:50 +09:00
parent 86c098b2a8
commit c6e148efcf
6 changed files with 89 additions and 97 deletions

View File

@@ -38,7 +38,7 @@ use std::str::FromStr;
#[pyclass(name = "bytearray")]
#[derive(Clone, Debug)]
pub struct PyByteArray {
pub inner: RefCell<PyByteInner>,
inner: RefCell<PyByteInner>,
}
pub type PyByteArrayRef = PyRef<PyByteArray>;
@@ -49,19 +49,19 @@ impl PyByteArray {
}
}
pub fn from_inner(inner: PyByteInner) -> Self {
fn from_inner(inner: PyByteInner) -> Self {
PyByteArray {
inner: RefCell::new(inner),
}
}
// pub fn get_value(&self) -> Vec<u8> {
// self.inner.borrow().clone().elements
// }
pub fn borrow_value(&self) -> std::cell::Ref<'_, PyByteInner> {
self.inner.borrow()
}
// pub fn get_value_mut(&self) -> Vec<u8> {
// self.inner.borrow_mut().clone().elements
// }
pub fn borrow_value_mut(&self) -> std::cell::RefMut<'_, PyByteInner> {
self.inner.borrow_mut()
}
}
impl PyValue for PyByteArray {
@@ -70,28 +70,19 @@ impl PyValue for PyByteArray {
}
}
// pub fn get_value(obj: &PyObjectRef) -> Vec<u8> {
// obj.payload::<PyByteArray>().unwrap().get_value()
// }
// pub fn get_value_mut(obj: &PyObjectRef) -> Vec<u8> {
// obj.payload::<PyByteArray>().unwrap().get_value_mut()
// }
/// Fill bytearray class methods dictionary.
pub fn init(context: &PyContext) {
PyByteArrayRef::extend_class(context, &context.types.bytearray_type);
pub(crate) fn init(context: &PyContext) {
PyByteArray::extend_class(context, &context.types.bytearray_type);
let bytearray_type = &context.types.bytearray_type;
extend_class!(context, bytearray_type, {
"fromhex" => context.new_method(PyByteArrayRef::fromhex),
"maketrans" => context.new_method(PyByteInner::maketrans),
"maketrans" => context.new_method(PyByteInner::maketrans),
});
PyByteArrayIterator::extend_class(context, &context.types.bytearrayiterator_type);
}
#[pyimpl]
impl PyByteArrayRef {
impl PyByteArray {
#[pyslot(new)]
fn tp_new(
cls: PyClassRef,
@@ -102,60 +93,60 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__repr__")]
fn repr(self, _vm: &VirtualMachine) -> PyResult<String> {
fn repr(&self, _vm: &VirtualMachine) -> PyResult<String> {
Ok(format!("bytearray(b'{}')", self.inner.borrow().repr()?))
}
#[pymethod(name = "__len__")]
fn len(self, _vm: &VirtualMachine) -> usize {
fn len(&self, _vm: &VirtualMachine) -> usize {
self.inner.borrow().len()
}
#[pymethod(name = "__sizeof__")]
fn sizeof(self, _vm: &VirtualMachine) -> usize {
fn sizeof(&self, _vm: &VirtualMachine) -> usize {
size_of::<Self>() + self.inner.borrow().len() * size_of::<u8>()
}
#[pymethod(name = "__eq__")]
fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().eq(other, vm)
}
#[pymethod(name = "__ge__")]
fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().ge(other, vm)
}
#[pymethod(name = "__le__")]
fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().le(other, vm)
}
#[pymethod(name = "__gt__")]
fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().gt(other, vm)
}
#[pymethod(name = "__lt__")]
fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().lt(other, vm)
}
#[pymethod(name = "__hash__")]
fn hash(self, vm: &VirtualMachine) -> PyResult<()> {
fn hash(&self, vm: &VirtualMachine) -> PyResult<()> {
Err(vm.new_type_error("unhashable type: bytearray".to_string()))
}
#[pymethod(name = "__iter__")]
fn iter(self, _vm: &VirtualMachine) -> PyByteArrayIterator {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyByteArrayIterator {
PyByteArrayIterator {
position: Cell::new(0),
bytearray: self,
bytearray: zelf,
}
}
#[pymethod(name = "__add__")]
fn add(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Ok(other) = PyByteInner::try_from_object(vm, other) {
Ok(vm.ctx.new_bytearray(self.inner.borrow().add(other)))
} else {
@@ -165,7 +156,7 @@ impl PyByteArrayRef {
#[pymethod(name = "__contains__")]
fn contains(
self,
&self,
needle: Either<PyByteInner, PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<bool> {
@@ -173,13 +164,13 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__getitem__")]
fn getitem(self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult {
fn getitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().getitem(needle, vm)
}
#[pymethod(name = "__setitem__")]
fn setitem(
self,
&self,
needle: Either<i32, PySliceRef>,
value: PyObjectRef,
vm: &VirtualMachine,
@@ -188,75 +179,76 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__delitem__")]
fn delitem(self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult<()> {
fn delitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult<()> {
self.inner.borrow_mut().delitem(needle, vm)
}
#[pymethod(name = "isalnum")]
fn isalnum(self, vm: &VirtualMachine) -> bool {
fn isalnum(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isalnum(vm)
}
#[pymethod(name = "isalpha")]
fn isalpha(self, vm: &VirtualMachine) -> bool {
fn isalpha(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isalpha(vm)
}
#[pymethod(name = "isascii")]
fn isascii(self, vm: &VirtualMachine) -> bool {
fn isascii(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isascii(vm)
}
#[pymethod(name = "isdigit")]
fn isdigit(self, vm: &VirtualMachine) -> bool {
fn isdigit(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isdigit(vm)
}
#[pymethod(name = "islower")]
fn islower(self, vm: &VirtualMachine) -> bool {
fn islower(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().islower(vm)
}
#[pymethod(name = "isspace")]
fn isspace(self, vm: &VirtualMachine) -> bool {
fn isspace(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isspace(vm)
}
#[pymethod(name = "isupper")]
fn isupper(self, vm: &VirtualMachine) -> bool {
fn isupper(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().isupper(vm)
}
#[pymethod(name = "istitle")]
fn istitle(self, vm: &VirtualMachine) -> bool {
fn istitle(&self, vm: &VirtualMachine) -> bool {
self.inner.borrow().istitle(vm)
}
#[pymethod(name = "lower")]
fn lower(self, vm: &VirtualMachine) -> PyResult {
fn lower(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().lower(vm)))
}
#[pymethod(name = "upper")]
fn upper(self, vm: &VirtualMachine) -> PyResult {
fn upper(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().upper(vm)))
}
#[pymethod(name = "capitalize")]
fn capitalize(self, vm: &VirtualMachine) -> PyResult {
fn capitalize(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().capitalize(vm)))
}
#[pymethod(name = "swapcase")]
fn swapcase(self, vm: &VirtualMachine) -> PyResult {
fn swapcase(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().swapcase(vm)))
}
#[pymethod(name = "hex")]
fn hex(self, vm: &VirtualMachine) -> String {
fn hex(&self, vm: &VirtualMachine) -> String {
self.inner.borrow().hex(vm)
}
#[pymethod]
fn fromhex(string: PyStringRef, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
@@ -264,39 +256,39 @@ impl PyByteArrayRef {
}
#[pymethod(name = "center")]
fn center(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
fn center(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bytearray(self.inner.borrow().center(options, vm)?))
}
#[pymethod(name = "ljust")]
fn ljust(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
fn ljust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bytearray(self.inner.borrow().ljust(options, vm)?))
}
#[pymethod(name = "rjust")]
fn rjust(self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
fn rjust(&self, options: ByteInnerPaddingOptions, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bytearray(self.inner.borrow().rjust(options, vm)?))
}
#[pymethod(name = "count")]
fn count(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<usize> {
fn count(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<usize> {
self.inner.borrow().count(options, vm)
}
#[pymethod(name = "join")]
fn join(self, iter: PyIterable<PyByteInner>, vm: &VirtualMachine) -> PyResult {
fn join(&self, iter: PyIterable<PyByteInner>, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().join(iter, vm)
}
#[pymethod(name = "endswith")]
fn endswith(
self,
&self,
suffix: Either<PyByteInner, PyTupleRef>,
start: OptionalArg<PyObjectRef>,
end: OptionalArg<PyObjectRef>,
@@ -309,7 +301,7 @@ impl PyByteArrayRef {
#[pymethod(name = "startswith")]
fn startswith(
self,
&self,
prefix: Either<PyByteInner, PyTupleRef>,
start: OptionalArg<PyObjectRef>,
end: OptionalArg<PyObjectRef>,
@@ -321,12 +313,12 @@ impl PyByteArrayRef {
}
#[pymethod(name = "find")]
fn find(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
fn find(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
self.inner.borrow().find(options, false, vm)
}
#[pymethod(name = "index")]
fn index(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
fn index(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
let res = self.inner.borrow().find(options, false, vm)?;
if res == -1 {
return Err(vm.new_value_error("substring not found".to_string()));
@@ -335,12 +327,12 @@ impl PyByteArrayRef {
}
#[pymethod(name = "rfind")]
fn rfind(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
fn rfind(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
self.inner.borrow().find(options, true, vm)
}
#[pymethod(name = "rindex")]
fn rindex(self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
fn rindex(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<isize> {
let res = self.inner.borrow().find(options, true, vm)?;
if res == -1 {
return Err(vm.new_value_error("substring not found".to_string()));
@@ -349,7 +341,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "remove")]
fn remove(self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
fn remove(&self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
let x = x.as_bigint().byte_or(vm)?;
let bytes = &mut self.inner.borrow_mut().elements;
@@ -364,12 +356,12 @@ impl PyByteArrayRef {
}
#[pymethod(name = "translate")]
fn translate(self, options: ByteInnerTranslateOptions, vm: &VirtualMachine) -> PyResult {
fn translate(&self, options: ByteInnerTranslateOptions, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().translate(options, vm)
}
#[pymethod(name = "strip")]
fn strip(self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
fn strip(&self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(
self.inner
.borrow()
@@ -378,7 +370,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "lstrip")]
fn lstrip(self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
fn lstrip(&self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(
self.inner
.borrow()
@@ -387,7 +379,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "rstrip")]
fn rstrip(self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
fn rstrip(&self, chars: OptionalArg<PyByteInner>, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(
self.inner
.borrow()
@@ -396,7 +388,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "split")]
fn split(self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.inner
.borrow()
@@ -408,7 +400,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "rsplit")]
fn rsplit(self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.inner
.borrow()
@@ -420,7 +412,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "partition")]
fn partition(self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult {
fn partition(&self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult {
// sep ALWAYS converted to bytearray even it's bytes or memoryview
// so its ok to accept PyByteInner
let (left, right) = self.inner.borrow().partition(&sep, false)?;
@@ -432,7 +424,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "rpartition")]
fn rpartition(self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult {
fn rpartition(&self, sep: PyByteInner, vm: &VirtualMachine) -> PyResult {
let (left, right) = self.inner.borrow().partition(&sep, true)?;
Ok(vm.ctx.new_tuple(vec![
vm.ctx.new_bytearray(left),
@@ -442,14 +434,14 @@ impl PyByteArrayRef {
}
#[pymethod(name = "expandtabs")]
fn expandtabs(self, options: ByteInnerExpandtabsOptions, vm: &VirtualMachine) -> PyResult {
fn expandtabs(&self, options: ByteInnerExpandtabsOptions, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bytearray(self.inner.borrow().expandtabs(options)))
}
#[pymethod(name = "splitlines")]
fn splitlines(self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
let as_bytes = self
.inner
.borrow()
@@ -461,13 +453,13 @@ impl PyByteArrayRef {
}
#[pymethod(name = "zfill")]
fn zfill(self, width: PyIntRef, vm: &VirtualMachine) -> PyResult {
fn zfill(&self, width: PyIntRef, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().zfill(width)))
}
#[pymethod(name = "replace")]
fn replace(
self,
&self,
old: PyByteInner,
new: PyByteInner,
count: OptionalArg<PyIntRef>,
@@ -479,17 +471,17 @@ impl PyByteArrayRef {
}
#[pymethod(name = "clear")]
fn clear(self, _vm: &VirtualMachine) {
fn clear(&self, _vm: &VirtualMachine) {
self.inner.borrow_mut().elements.clear();
}
#[pymethod(name = "copy")]
fn copy(self, vm: &VirtualMachine) -> PyResult {
fn copy(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().elements.clone()))
}
#[pymethod(name = "append")]
fn append(self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
fn append(&self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
self.inner
.borrow_mut()
.elements
@@ -498,7 +490,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "extend")]
fn extend(self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> PyResult<()> {
fn extend(&self, iterable_of_ints: PyIterable, vm: &VirtualMachine) -> PyResult<()> {
let mut inner = self.inner.borrow_mut();
for x in iterable_of_ints.iter(vm)? {
@@ -512,7 +504,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "insert")]
fn insert(self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
fn insert(&self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
let bytes = &mut self.inner.borrow_mut().elements;
let len = isize::try_from(bytes.len())
.map_err(|_e| vm.new_overflow_error("bytearray too big".to_string()))?;
@@ -538,7 +530,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "pop")]
fn pop(self, vm: &VirtualMachine) -> PyResult<u8> {
fn pop(&self, vm: &VirtualMachine) -> PyResult<u8> {
let bytes = &mut self.inner.borrow_mut().elements;
bytes
.pop()
@@ -546,22 +538,22 @@ impl PyByteArrayRef {
}
#[pymethod(name = "title")]
fn title(self, vm: &VirtualMachine) -> PyResult {
fn title(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().title()))
}
#[pymethod(name = "__mul__")]
fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
fn repeat(&self, n: isize, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytearray(self.inner.borrow().repeat(n, vm)?))
}
#[pymethod(name = "__rmul__")]
fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult {
fn rmul(&self, n: isize, vm: &VirtualMachine) -> PyResult {
self.repeat(n, vm)
}
#[pymethod(name = "__imul__")]
fn irepeat(self, n: isize, vm: &VirtualMachine) -> PyResult<()> {
fn irepeat(&self, n: isize, vm: &VirtualMachine) -> PyResult<()> {
self.inner.borrow_mut().irepeat(n, vm)
}
@@ -578,7 +570,7 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__mod__")]
fn modulo(self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let format_string =
CFormatString::from_str(std::str::from_utf8(&self.inner.borrow().elements).unwrap())
.map_err(|err| vm.new_value_error(err.to_string()))?;
@@ -586,12 +578,12 @@ impl PyByteArrayRef {
}
#[pymethod(name = "__rmod__")]
fn rmod(self, _values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
fn rmod(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.not_implemented())
}
#[pymethod(name = "reverse")]
fn reverse(self, _vm: &VirtualMachine) -> PyResult<()> {
fn reverse(&self, _vm: &VirtualMachine) -> PyResult<()> {
self.inner.borrow_mut().elements.reverse();
Ok(())
}

View File

@@ -32,7 +32,7 @@ impl TryFromObject for PyByteInner {
elements: i.get_value().to_vec()
}),
j @ PyByteArray => Ok(PyByteInner {
elements: j.inner.borrow().elements.to_vec()
elements: j.borrow_value().elements.to_vec()
}),
k @ PyMemoryView => Ok(PyByteInner {
elements: k.get_obj_value().unwrap()
@@ -86,7 +86,7 @@ impl ByteInnerNewOptions {
);
}
i @ PyBytes => Ok(i.get_value().to_vec()),
j @ PyByteArray => Ok(j.inner.borrow().elements.to_vec()),
j @ PyByteArray => Ok(j.borrow_value().elements.to_vec()),
obj => {
// TODO: only support this method in the bytes() constructor
if let Some(bytes_method) = vm.get_method(obj.clone(), "__bytes__") {
@@ -1214,7 +1214,7 @@ impl PyByteInner {
pub fn try_as_byte(obj: &PyObjectRef) -> Option<Vec<u8>> {
match_class!(match obj.clone() {
i @ PyBytes => Some(i.get_value().to_vec()),
j @ PyByteArray => Some(j.inner.borrow().elements.to_vec()),
j @ PyByteArray => Some(j.borrow_value().elements.to_vec()),
_ => None,
})
}
@@ -1439,7 +1439,7 @@ impl PyBytesLike {
pub fn to_cow(&self) -> std::borrow::Cow<[u8]> {
match self {
PyBytesLike::Bytes(b) => b.get_value().into(),
PyBytesLike::Bytearray(b) => b.inner.borrow().elements.clone().into(),
PyBytesLike::Bytearray(b) => b.borrow_value().elements.clone().into(),
}
}
@@ -1447,7 +1447,7 @@ impl PyBytesLike {
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
match self {
PyBytesLike::Bytes(b) => f(b.get_value()),
PyBytesLike::Bytearray(b) => f(&b.inner.borrow().elements),
PyBytesLike::Bytearray(b) => f(&b.borrow_value().elements),
}
}
}

View File

@@ -42,7 +42,7 @@ impl SerializedData {
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
match self {
SerializedData::Bytes(b) => f(b.get_value()),
SerializedData::Buffer(b) => f(&b.inner.borrow().elements),
SerializedData::Buffer(b) => f(&b.borrow_value().elements),
SerializedData::Ascii(a) => f(a.as_str().as_bytes()),
}
}

View File

@@ -465,7 +465,7 @@ fn raw_io_base_read(
vm.call_method(&instance, "readinto", vec![b.as_object().clone()])?,
)?;
if let Some(n) = n {
let bytes = &mut b.inner.borrow_mut().elements;
let bytes = &mut b.borrow_value_mut().elements;
bytes.truncate(n);
Ok(vm.ctx.new_bytes(bytes.clone()))
} else {
@@ -628,7 +628,7 @@ mod fileio {
if let Some(bytes) = obj.payload::<PyByteArray>() {
//TODO: Implement for MemoryView
let value_mut = &mut bytes.inner.borrow_mut().elements;
let value_mut = &mut bytes.borrow_value_mut().elements;
value_mut.clear();
match f.read_to_end(value_mut) {
Ok(_) => {}

View File

@@ -28,7 +28,7 @@ pub fn json_loads(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
b @ PyBytes => py_serde::deserialize(vm, &mut serde_json::Deserializer::from_slice(&b)),
ba @ PyByteArray => py_serde::deserialize(
vm,
&mut serde_json::Deserializer::from_slice(&ba.inner.borrow().elements)
&mut serde_json::Deserializer::from_slice(&ba.borrow_value().elements)
),
obj => {
let msg = format!(

View File

@@ -190,7 +190,7 @@ impl PySocket {
#[pymethod]
fn recv_into(&self, buf: PyByteArrayRef, vm: &VirtualMachine) -> PyResult<usize> {
let mut buffer = buf.inner.borrow_mut();
let mut buffer = buf.borrow_value_mut();
self.sock()
.recv(&mut buffer.elements)
.map_err(|err| convert_sock_error(vm, err))