mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
The memoryview skeleton did not correctly assign the "obj" attribute; The "obj" attribute was assigned to the memoryview class and not the memryview object, leading to a funny bug that can be reproduced as follows: ```python m1 = memoryview(b'1234') m2 = memoryview(b'abcd') print(m1.obj) ``` the result would be the value inside `m2` instead that of `m1`
47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use crate::obj::objbyteinner::try_as_byte;
|
|
use crate::obj::objtype::PyClassRef;
|
|
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
|
|
use crate::vm::VirtualMachine;
|
|
|
|
#[pyclass(name = "memoryview")]
|
|
#[derive(Debug)]
|
|
pub struct PyMemoryView {
|
|
obj_ref: PyObjectRef,
|
|
}
|
|
|
|
pub type PyMemoryViewRef = PyRef<PyMemoryView>;
|
|
|
|
#[pyimpl]
|
|
impl PyMemoryView {
|
|
pub fn get_obj_value(&self) -> Option<Vec<u8>> {
|
|
try_as_byte(&self.obj_ref)
|
|
}
|
|
|
|
#[pymethod(name = "__new__")]
|
|
fn new(
|
|
cls: PyClassRef,
|
|
bytes_object: PyObjectRef,
|
|
vm: &VirtualMachine,
|
|
) -> PyResult<PyMemoryViewRef> {
|
|
PyMemoryView {
|
|
obj_ref: bytes_object.clone(),
|
|
}
|
|
.into_ref_with_type(vm, cls)
|
|
}
|
|
|
|
#[pyproperty]
|
|
fn obj(&self, __vm: &VirtualMachine) -> PyObjectRef {
|
|
self.obj_ref.clone()
|
|
}
|
|
}
|
|
|
|
impl PyValue for PyMemoryView {
|
|
fn class(vm: &VirtualMachine) -> PyClassRef {
|
|
vm.ctx.memoryview_type()
|
|
}
|
|
}
|
|
|
|
pub fn init(ctx: &PyContext) {
|
|
PyMemoryView::extend_class(ctx, &ctx.memoryview_type)
|
|
}
|