@@ -55,6 +55,10 @@ pub struct PyByteArray {
55
55
pub type PyByteArrayRef = PyRef < PyByteArray > ;
56
56
57
57
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
+
58
62
fn from_inner ( inner : PyBytesInner ) -> Self {
59
63
PyByteArray {
60
64
inner : PyRwLock :: new ( inner) ,
@@ -146,8 +150,8 @@ impl PyByteArray {
146
150
}
147
151
148
152
#[ 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 ( )
151
155
}
152
156
153
157
#[ pymethod( magic) ]
@@ -524,14 +528,20 @@ impl PyByteArray {
524
528
525
529
#[ pymethod]
526
530
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
+ )
529
536
}
530
537
531
538
#[ pymethod]
532
539
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
+ )
535
545
}
536
546
537
547
#[ pymethod]
@@ -541,10 +551,9 @@ impl PyByteArray {
541
551
let value = self . inner ( ) ;
542
552
let ( front, has_mid, back) = value. partition ( & sep, vm) ?;
543
553
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 ) ,
548
557
) ) )
549
558
}
550
559
@@ -553,10 +562,9 @@ impl PyByteArray {
553
562
let value = self . inner ( ) ;
554
563
let ( back, has_mid, front) = value. rpartition ( & sep, vm) ?;
555
564
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 ) ,
560
568
) ) )
561
569
}
562
570
@@ -569,7 +577,7 @@ impl PyByteArray {
569
577
fn splitlines ( & self , options : anystr:: SplitLinesArgs , vm : & VirtualMachine ) -> PyObjectRef {
570
578
let lines = self
571
579
. 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 ( ) ) ;
573
581
vm. ctx . new_list ( lines)
574
582
}
575
583
0 commit comments