diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index 842bb233f..aad4e6827 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -1,6 +1,9 @@ +use std::borrow::Borrow; + use crate::obj::objbyteinner::try_as_byte; -use crate::obj::objtype::PyClassRef; +use crate::obj::objtype::{issubclass, PyClassRef}; use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue}; +use crate::stdlib::array::PyArray; use crate::vm::VirtualMachine; #[pyclass(name = "memoryview")] @@ -23,10 +26,23 @@ impl PyMemoryView { bytes_object: PyObjectRef, vm: &VirtualMachine, ) -> PyResult { - PyMemoryView { - obj_ref: bytes_object.clone(), + let object_type = bytes_object.typ.borrow(); + + if issubclass(object_type, &vm.ctx.types.memoryview_type) + || issubclass(object_type, &vm.ctx.types.bytes_type) + || issubclass(object_type, &vm.ctx.types.bytearray_type) + || issubclass(object_type, &PyArray::class(vm)) + { + PyMemoryView { + obj_ref: bytes_object.clone(), + } + .into_ref_with_type(vm, cls) + } else { + Err(vm.new_type_error(format!( + "memoryview: a bytes-like object is required, not '{}'", + object_type.name + ))) } - .into_ref_with_type(vm, cls) } #[pyproperty] diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index a65bb62b2..1d3b0afce 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -1,4 +1,4 @@ -mod array; +pub mod array; #[cfg(feature = "rustpython-parser")] mod ast; mod binascii;