@@ -5,12 +5,12 @@ use std::fmt::Write;
5
5
use std:: ops:: { Deref , DerefMut } ;
6
6
7
7
use crate :: pyobject:: {
8
- PyContext , PyFuncArgs , PyObject , PyObjectRef , PyResult , PyValue , TypeProtocol ,
8
+ OptionalArg , PyContext , PyFuncArgs , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol ,
9
9
} ;
10
10
11
11
use super :: objint;
12
12
13
- use super :: objtype;
13
+ use super :: objtype:: { self , PyClassRef } ;
14
14
use crate :: vm:: VirtualMachine ;
15
15
use num_traits:: ToPrimitive ;
16
16
@@ -19,6 +19,7 @@ pub struct PyByteArray {
19
19
// TODO: shouldn't be public
20
20
pub value : RefCell < Vec < u8 > > ,
21
21
}
22
+ type PyByteArrayRef = PyRef < PyByteArray > ;
22
23
23
24
impl PyByteArray {
24
25
pub fn new ( data : Vec < u8 > ) -> Self {
@@ -144,20 +145,14 @@ pub fn init(context: &PyContext) {
144
145
) ;
145
146
}
146
147
147
- fn bytearray_new ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
148
- arg_check ! (
149
- vm,
150
- args,
151
- required = [ ( cls, None ) ] ,
152
- optional = [ ( val_option, None ) ]
153
- ) ;
154
- if !objtype:: issubclass ( cls, & vm. ctx . bytearray_type ( ) ) {
155
- return Err ( vm. new_type_error ( format ! ( "{:?} is not a subtype of bytearray" , cls) ) ) ;
156
- }
157
-
148
+ fn bytearray_new (
149
+ cls : PyClassRef ,
150
+ val_option : OptionalArg < PyObjectRef > ,
151
+ vm : & mut VirtualMachine ,
152
+ ) -> PyResult < PyByteArrayRef > {
158
153
// Create bytes data:
159
- let value = if let Some ( ival) = val_option {
160
- let elements = vm. extract_elements ( ival) ?;
154
+ let value = if let OptionalArg :: Present ( ival) = val_option {
155
+ let elements = vm. extract_elements ( & ival) ?;
161
156
let mut data_bytes = vec ! [ ] ;
162
157
for elem in elements. iter ( ) {
163
158
let v = objint:: to_int ( vm, elem, 10 ) ?;
@@ -172,7 +167,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172
167
} else {
173
168
vec ! [ ]
174
169
} ;
175
- Ok ( PyObject :: new ( PyByteArray :: new ( value) , cls. clone ( ) ) )
170
+ PyByteArray :: new ( value) . into_ref_with_type ( vm , cls. clone ( ) )
176
171
}
177
172
178
173
fn bytesarray_len ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
0 commit comments