Add type constraints to memoryview constructor

This commit is contained in:
lazylife7157
2019-10-07 02:27:12 +09:00
parent 597453ec45
commit 725d153937
2 changed files with 21 additions and 5 deletions

View File

@@ -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<PyMemoryViewRef> {
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]

View File

@@ -1,4 +1,4 @@
mod array;
pub mod array;
#[cfg(feature = "rustpython-parser")]
mod ast;
mod binascii;