Skip to content

Commit d35c87a

Browse files
committed
fix ctypes
1 parent b75893a commit d35c87a

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
},
1010
class::StaticType,
1111
function::{ArgBytesLike, FuncArgs, PySetterValue},
12-
protocol::{BufferDescriptor, PyBuffer, PyNumberMethods, PySequenceMethods},
13-
types::{AsBuffer, AsNumber, AsSequence, Constructor, Initializer},
12+
protocol::{BufferDescriptor, PyBuffer, PyMappingMethods, PyNumberMethods, PySequenceMethods},
13+
types::{AsBuffer, AsMapping, AsNumber, AsSequence, Constructor, Initializer},
1414
};
1515
use alloc::borrow::Cow;
1616
use num_traits::{Signed, ToPrimitive};
@@ -468,9 +468,33 @@ impl AsSequence for PyCArray {
468468
}
469469
}
470470

471+
impl AsMapping for PyCArray {
472+
fn as_mapping() -> &'static PyMappingMethods {
473+
use std::sync::LazyLock;
474+
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
475+
length: atomic_func!(|mapping, _vm| {
476+
let zelf = PyCArray::mapping_downcast(mapping);
477+
Ok(zelf.class().stg_info_opt().map_or(0, |i| i.length))
478+
}),
479+
subscript: atomic_func!(|mapping, needle, vm| {
480+
let zelf = PyCArray::mapping_downcast(mapping);
481+
PyCArray::__getitem__(zelf, needle.to_owned(), vm)
482+
}),
483+
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
484+
let zelf = PyCArray::mapping_downcast(mapping);
485+
match value {
486+
Some(value) => PyCArray::__setitem__(zelf, needle.to_owned(), value, vm),
487+
None => PyCArray::__delitem__(zelf, needle.to_owned(), vm),
488+
}
489+
}),
490+
});
491+
&AS_MAPPING
492+
}
493+
}
494+
471495
#[pyclass(
472496
flags(BASETYPE, IMMUTABLETYPE),
473-
with(Constructor, Initializer, AsSequence, AsBuffer)
497+
with(Constructor, Initializer, AsSequence, AsMapping, AsBuffer)
474498
)]
475499
impl PyCArray {
476500
#[pyclassmethod]

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::base::CDATA_BUFFER_METHODS;
22
use super::{PyCArray, PyCData, PyCSimple, PyCStructure, StgInfo, StgInfoFlags};
3-
use crate::protocol::{BufferDescriptor, PyBuffer, PyNumberMethods};
4-
use crate::types::{AsBuffer, AsNumber, Constructor, Initializer};
3+
use crate::atomic_func;
4+
use crate::protocol::{BufferDescriptor, PyBuffer, PyMappingMethods, PyNumberMethods};
5+
use crate::types::{AsBuffer, AsMapping, AsNumber, Constructor, Initializer};
56
use crate::{
67
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
78
builtins::{PyBytes, PyInt, PyList, PySlice, PyStr, PyType, PyTypeRef},
@@ -260,7 +261,7 @@ impl Initializer for PyCPointer {
260261

261262
#[pyclass(
262263
flags(BASETYPE, IMMUTABLETYPE),
263-
with(Constructor, Initializer, AsNumber, AsBuffer)
264+
with(Constructor, Initializer, AsNumber, AsBuffer, AsMapping)
264265
)]
265266
impl PyCPointer {
266267
/// Get the pointer value stored in buffer as usize
@@ -785,6 +786,27 @@ impl AsNumber for PyCPointer {
785786
}
786787
}
787788

789+
impl AsMapping for PyCPointer {
790+
fn as_mapping() -> &'static PyMappingMethods {
791+
use std::sync::LazyLock;
792+
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
793+
subscript: atomic_func!(|mapping, needle, vm| {
794+
let zelf = PyCPointer::mapping_downcast(mapping);
795+
PyCPointer::__getitem__(zelf, needle.to_owned(), vm)
796+
}),
797+
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
798+
let zelf = PyCPointer::mapping_downcast(mapping);
799+
match value {
800+
Some(value) => PyCPointer::__setitem__(zelf, needle.to_owned(), value, vm),
801+
None => Err(vm.new_type_error("Pointer does not support item deletion")),
802+
}
803+
}),
804+
..PyMappingMethods::NOT_IMPLEMENTED
805+
});
806+
&AS_MAPPING
807+
}
808+
}
809+
788810
impl AsBuffer for PyCPointer {
789811
fn as_buffer(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
790812
let stg_info = zelf

0 commit comments

Comments
 (0)