Skip to content

Commit 792c19f

Browse files
committed
Fix array
1 parent e5aec9d commit 792c19f

4 files changed

Lines changed: 10 additions & 16 deletions

File tree

crates/vm/src/stdlib/ctypes/array.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Constructor for PyCArrayType {
7575
#[pyclass(flags(IMMUTABLETYPE), with(Callable, Constructor, AsNumber))]
7676
impl PyCArrayType {
7777
#[pygetset(name = "_type_")]
78-
fn typ(&self) -> PyTypeRef {
78+
fn typ(&self) -> PyObjectRef {
7979
self.inner.typ.read().clone()
8080
}
8181

@@ -96,10 +96,7 @@ impl PyCArrayType {
9696
let inner_element_size = zelf.inner.element_size.load();
9797

9898
// The element type of the new array is the current array type itself
99-
let obj_ref: PyObjectRef = zelf.to_owned().into();
100-
let current_array_type = obj_ref
101-
.downcast::<PyType>()
102-
.expect("PyCArrayType should be a PyType");
99+
let current_array_type: PyObjectRef = zelf.as_object().to_owned();
103100

104101
// Element size is the total size of the inner array
105102
let new_element_size = inner_length * inner_element_size;
@@ -144,7 +141,8 @@ impl AsNumber for PyCArrayType {
144141
)]
145142
#[derive(PyPayload)]
146143
pub struct PyCArray {
147-
pub(super) typ: PyRwLock<PyTypeRef>,
144+
/// Element type - can be a simple type (c_int) or an array type (c_int * 5)
145+
pub(super) typ: PyRwLock<PyObjectRef>,
148146
pub(super) length: AtomicCell<usize>,
149147
pub(super) element_size: AtomicCell<usize>,
150148
pub(super) buffer: PyRwLock<Vec<u8>>,
@@ -207,12 +205,8 @@ impl Constructor for PyCArray {
207205
}
208206
}
209207

210-
let element_type_ref = element_type
211-
.downcast::<PyType>()
212-
.unwrap_or_else(|_| vm.ctx.types.object_type.to_owned());
213-
214208
PyCArray {
215-
typ: PyRwLock::new(element_type_ref),
209+
typ: PyRwLock::new(element_type),
216210
length: AtomicCell::new(length),
217211
element_size: AtomicCell::new(element_size),
218212
buffer: PyRwLock::new(buffer),
@@ -354,7 +348,7 @@ impl PyCArray {
354348
}
355349

356350
#[pygetset(name = "_type_")]
357-
fn typ(&self) -> PyTypeRef {
351+
fn typ(&self) -> PyObjectRef {
358352
self.typ.read().clone()
359353
}
360354

crates/vm/src/stdlib/ctypes/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl PyCSimple {
324324
};
325325
Ok(PyCArrayType {
326326
inner: PyCArray {
327-
typ: PyRwLock::new(cls),
327+
typ: PyRwLock::new(cls.clone().into()),
328328
length: AtomicCell::new(n as usize),
329329
element_size: AtomicCell::new(element_size),
330330
buffer: PyRwLock::new(vec![]),

crates/vm/src/stdlib/ctypes/pointer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::convert::ToPyObject;
77
use crate::protocol::PyNumberMethods;
88
use crate::stdlib::ctypes::PyCData;
99
use crate::types::AsNumber;
10-
use crate::{PyObjectRef, PyResult, VirtualMachine};
10+
use crate::{AsObject, PyObjectRef, PyResult, VirtualMachine};
1111

1212
#[pyclass(name = "PyCPointerType", base = PyType, module = "_ctypes")]
1313
#[derive(PyPayload, Debug)]
@@ -28,7 +28,7 @@ impl PyCPointerType {
2828
let element_size = std::mem::size_of::<usize>();
2929
Ok(PyCArrayType {
3030
inner: PyCArray {
31-
typ: PyRwLock::new(cls),
31+
typ: PyRwLock::new(cls.as_object().to_owned()),
3232
length: AtomicCell::new(n as usize),
3333
element_size: AtomicCell::new(element_size),
3434
buffer: PyRwLock::new(vec![]),

crates/vm/src/stdlib/ctypes/structure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl PyCStructType {
144144
let element_size = std::mem::size_of::<usize>(); // Default, should calculate from fields
145145
Ok(PyCArrayType {
146146
inner: PyCArray {
147-
typ: PyRwLock::new(cls),
147+
typ: PyRwLock::new(cls.clone().into()),
148148
length: AtomicCell::new(n as usize),
149149
element_size: AtomicCell::new(element_size),
150150
buffer: PyRwLock::new(vec![]),

0 commit comments

Comments
 (0)