Skip to content

Commit 7aa9b94

Browse files
committed
Avoid use of PyObject in objbytearray.
1 parent a0acc7a commit 7aa9b94

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

vm/src/obj/objbytearray.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::fmt::Write;
55
use std::ops::{Deref, DerefMut};
66

77
use crate::pyobject::{
8-
PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
8+
OptionalArg, PyContext, PyFuncArgs, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
99
};
1010

1111
use super::objint;
1212

13-
use super::objtype;
13+
use super::objtype::{self, PyClassRef};
1414
use crate::vm::VirtualMachine;
1515
use num_traits::ToPrimitive;
1616

@@ -19,6 +19,7 @@ pub struct PyByteArray {
1919
// TODO: shouldn't be public
2020
pub value: RefCell<Vec<u8>>,
2121
}
22+
type PyByteArrayRef = PyRef<PyByteArray>;
2223

2324
impl PyByteArray {
2425
pub fn new(data: Vec<u8>) -> Self {
@@ -144,20 +145,14 @@ pub fn init(context: &PyContext) {
144145
);
145146
}
146147

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> {
158153
// 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)?;
161156
let mut data_bytes = vec![];
162157
for elem in elements.iter() {
163158
let v = objint::to_int(vm, elem, 10)?;
@@ -172,7 +167,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172167
} else {
173168
vec![]
174169
};
175-
Ok(PyObject::new(PyByteArray::new(value), cls.clone()))
170+
PyByteArray::new(value).into_ref_with_type(vm, cls.clone())
176171
}
177172

178173
fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)