Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix mul
  • Loading branch information
youknowone committed Nov 28, 2025
commit 71410b60e4b10017ef74b886e1f7760653fae2fe
14 changes: 12 additions & 2 deletions crates/vm/src/stdlib/ctypes/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,18 @@ impl Constructor for PyCSimple {
let attributes = cls.get_attributes();
let _type_ = attributes
.iter()
.find(|(k, _)| k.to_object().str(vm).unwrap().to_string() == *"_type_")
.unwrap()
.find(|(k, _)| {
k.to_object()
.str(vm)
.map(|s| s.to_string() == "_type_")
.unwrap_or(false)
})
.ok_or_else(|| {
vm.new_type_error(format!(
"cannot create '{}' instances: no _type_ attribute",
cls.name()
))
})?
.1
.str(vm)?
.to_string();
Expand Down
49 changes: 45 additions & 4 deletions crates/vm/src/stdlib/ctypes/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crossbeam_utils::atomic::AtomicCell;
use num_traits::ToPrimitive;
use rustpython_common::lock::PyRwLock;

use crate::builtins::PyType;
use crate::builtins::{PyType, PyTypeRef};
use crate::convert::ToPyObject;
use crate::protocol::PyNumberMethods;
use crate::stdlib::ctypes::PyCData;
use crate::{PyObjectRef, PyResult};
use crate::types::AsNumber;
use crate::{PyObjectRef, PyResult, VirtualMachine};

#[pyclass(name = "PyCPointerType", base = PyType, module = "_ctypes")]
#[derive(PyPayload, Debug)]
Expand All @@ -11,8 +16,44 @@ pub struct PyCPointerType {
pub(crate) inner: PyCPointer,
}

#[pyclass]
impl PyCPointerType {}
#[pyclass(flags(IMMUTABLETYPE), with(AsNumber))]
impl PyCPointerType {
#[pymethod]
fn __mul__(cls: PyTypeRef, n: isize, vm: &VirtualMachine) -> PyResult {
use super::array::{PyCArray, PyCArrayType};
if n < 0 {
return Err(vm.new_value_error(format!("Array length must be >= 0, not {n}")));
}
Ok(PyCArrayType {
inner: PyCArray {
typ: PyRwLock::new(cls),
length: AtomicCell::new(n as usize),
value: PyRwLock::new(vm.ctx.none()),
},
}
.to_pyobject(vm))
}
}

impl AsNumber for PyCPointerType {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
multiply: Some(|a, b, vm| {
let cls = a
.downcast_ref::<PyType>()
.ok_or_else(|| vm.new_type_error("expected type".to_owned()))?;
let n = b
.try_index(vm)?
.as_bigint()
.to_isize()
.ok_or_else(|| vm.new_overflow_error("array size too large".to_owned()))?;
PyCPointerType::__mul__(cls.to_owned(), n, vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
}
}

#[pyclass(
name = "_Pointer",
Expand Down
Loading