Skip to content

Commit ef6c066

Browse files
committed
Remove new_bytearray
1 parent 0b56213 commit ef6c066

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

vm/src/builtins/bytearray.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ pub struct PyByteArray {
5555
pub type PyByteArrayRef = PyRef<PyByteArray>;
5656

5757
impl PyByteArray {
58+
pub fn new_ref(data: Vec<u8>, ctx: &PyContext) -> PyRef<Self> {
59+
PyRef::new_ref(Self::from(data), ctx.types.bytearray_type.clone(), None)
60+
}
61+
5862
fn from_inner(inner: PyBytesInner) -> Self {
5963
PyByteArray {
6064
inner: PyRwLock::new(inner),
@@ -146,8 +150,8 @@ impl PyByteArray {
146150
}
147151

148152
#[pymethod(magic)]
149-
fn add(&self, other: ArgBytesLike, vm: &VirtualMachine) -> PyObjectRef {
150-
vm.ctx.new_bytearray(self.inner().add(&*other.borrow_buf()))
153+
fn add(&self, other: ArgBytesLike) -> Self {
154+
self.inner().add(&*other.borrow_buf()).into()
151155
}
152156

153157
#[pymethod(magic)]
@@ -524,14 +528,20 @@ impl PyByteArray {
524528

525529
#[pymethod]
526530
fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
527-
self.inner()
528-
.split(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
531+
self.inner().split(
532+
options,
533+
|s, vm| Self::new_ref(s.to_vec(), &vm.ctx).into(),
534+
vm,
535+
)
529536
}
530537

531538
#[pymethod]
532539
fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
533-
self.inner()
534-
.rsplit(options, |s, vm| vm.ctx.new_bytearray(s.to_vec()), vm)
540+
self.inner().rsplit(
541+
options,
542+
|s, vm| Self::new_ref(s.to_vec(), &vm.ctx).into(),
543+
vm,
544+
)
535545
}
536546

537547
#[pymethod]
@@ -541,10 +551,9 @@ impl PyByteArray {
541551
let value = self.inner();
542552
let (front, has_mid, back) = value.partition(&sep, vm)?;
543553
Ok(vm.new_tuple((
544-
vm.ctx.new_bytearray(front.to_vec()),
545-
vm.ctx
546-
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
547-
vm.ctx.new_bytearray(back.to_vec()),
554+
Self::new_ref(front.to_vec(), &vm.ctx),
555+
Self::new_ref(if has_mid { sep.elements } else { Vec::new() }, &vm.ctx),
556+
Self::new_ref(back.to_vec(), &vm.ctx),
548557
)))
549558
}
550559

@@ -553,10 +562,9 @@ impl PyByteArray {
553562
let value = self.inner();
554563
let (back, has_mid, front) = value.rpartition(&sep, vm)?;
555564
Ok(vm.new_tuple((
556-
vm.ctx.new_bytearray(front.to_vec()),
557-
vm.ctx
558-
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
559-
vm.ctx.new_bytearray(back.to_vec()),
565+
Self::new_ref(front.to_vec(), &vm.ctx),
566+
Self::new_ref(if has_mid { sep.elements } else { Vec::new() }, &vm.ctx),
567+
Self::new_ref(back.to_vec(), &vm.ctx),
560568
)))
561569
}
562570

@@ -569,7 +577,7 @@ impl PyByteArray {
569577
fn splitlines(&self, options: anystr::SplitLinesArgs, vm: &VirtualMachine) -> PyObjectRef {
570578
let lines = self
571579
.inner()
572-
.splitlines(options, |x| vm.ctx.new_bytearray(x.to_vec()));
580+
.splitlines(options, |x| Self::new_ref(x.to_vec(), &vm.ctx).into());
573581
vm.ctx.new_list(lines)
574582
}
575583

vm/src/pyobject.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use crate::pyobjectrc::{PyObject, PyObjectRef, PyObjectWeak, PyObjectWrap, P
77
use crate::{
88
builtins::{
99
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
10-
bytearray, bytes,
10+
bytes,
1111
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
1212
object, pystr, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat,
1313
PyFrozenSet, PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef,
@@ -209,14 +209,6 @@ impl PyContext {
209209
)
210210
}
211211

212-
pub fn new_bytearray(&self, data: Vec<u8>) -> PyObjectRef {
213-
PyObject::new(
214-
bytearray::PyByteArray::from(data),
215-
self.types.bytearray_type.clone(),
216-
None,
217-
)
218-
}
219-
220212
#[inline]
221213
pub fn new_bool(&self, b: bool) -> PyIntRef {
222214
let value = if b {

0 commit comments

Comments
 (0)