Skip to content
Closed
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
Next Next commit
fix more errors
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Jan 23, 2025
commit 112765bf5a1605a8a3daa1fa4781a07a666d6092
11 changes: 6 additions & 5 deletions vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::{
primitive::{new_simple_type, PyCSimple},
};
use crate::builtins::{
self,
slice::PySlice,
PyBytes, PyInt, PyList, PyRange, PyStr, PyType, PyTypeRef,
};
Expand All @@ -14,7 +13,7 @@ use crate::stdlib::ctypes::basics::{
default_from_param, generic_get_buffer, get_size, BorrowValue as BorrowValueCData,
BorrowValueMut, PyCData, PyCDataFunctions, PyCDataMethods, PyCDataSequenceMethods, RawBuffer,
};
use crate::{AsObject, Context, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine};
use crate::{AsObject, Py, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine};
use rustpython_vm::object::PyPayload;
use num_traits::Signed;
use std::convert::TryInto;
Expand All @@ -23,6 +22,7 @@ use widestring::WideCString;
use crate::class::StaticType;
use crate::convert::IntoObject;
use crate::protocol::{PyBuffer, PyIter};
use crate::types::AsBuffer;

// TODO: make sure that this is correct wrt windows and unix wstr
fn slice_to_obj(ty: &str, b: &[u8], vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -256,7 +256,7 @@ fn array_slice_setitem(
//Right now I'm setting one
let size = length.map_or(Ok(1), |v| usize::try_from_object(vm, v))?;

for (i, curr) in PyIterable::try_from_object(vm,_range.into_object(vm))?.iter(vm)?.enumerate() {
for (i, curr) in PyIter::try_from_object(vm,_range.into_object(vm))?.iter(vm)?.enumerate() {
let idx = fix_index(isize::try_from_object(vm, curr?)?, size, vm)? as usize;
let offset = idx * size;
let item = obj.get_item(i, vm)?;
Expand Down Expand Up @@ -286,6 +286,7 @@ fn fix_index(index: isize, length: usize, vm: &VirtualMachine) -> PyResult<isize
#[pyclass(module = "_ctypes", name = "PyCArrayType", base = "PyType")]
pub struct PyCArrayMeta {}

#[derive(PyPayload)]
#[pyclass(
module = "_ctypes",
name = "Array",
Expand Down Expand Up @@ -327,8 +328,8 @@ impl<'a> BorrowValueMut<'a> for PyCArray {
}
}

impl BufferProtocol for PyCArray {
fn get_buffer(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>> {
impl AsBuffer for PyCArray {
fn as_buffer(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyBuffer> {
generic_get_buffer::<Self>(zelf, vm)
}
}
Expand Down
8 changes: 5 additions & 3 deletions vm/src/stdlib/ctypes/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::function::Either;
use crate::builtins::PyTypeRef;
use crate::protocol::PyBuffer;
use crossbeam_utils::atomic::AtomicCell;
use crate::types::AsBuffer;

pub fn get_size(ty: &str) -> usize {
match ty {
Expand Down Expand Up @@ -270,7 +271,7 @@ pub trait PyCDataSequenceMethods: PyPayload {
}
}

pub fn generic_get_buffer<T>(zelf: &PyRef<T>, vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>>
pub fn generic_get_buffer<T>(zelf: &Py<T>, vm: &VirtualMachine) -> PyResult<PyBuffer>
where
for<'a> T: PyPayload + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
{
Expand Down Expand Up @@ -312,8 +313,8 @@ impl<'a> BorrowValueMut<'a> for PyCData {
}
}

impl BufferProtocol for PyCData {
fn get_buffer(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>> {
impl AsBuffer for PyCData {
fn as_buffer(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyBuffer> {
generic_get_buffer::<Self>(zelf, vm)
}
}
Expand Down Expand Up @@ -371,6 +372,7 @@ unsafe impl Sync for RawBuffer {}
// This Trait is the equivalent of PyCData_Type on tp_base for
// Struct_Type, Union_Type, PyCPointer_Type
// PyCArray_Type, PyCSimple_Type, PyCFuncPtr_Type
#[derive(PyPayload)]
#[pyclass(module = "_ctypes", name = "_CData")]
pub struct PyCData {
_objects: AtomicCell<Vec<PyObjectRef>>,
Expand Down
6 changes: 4 additions & 2 deletions vm/src/stdlib/ctypes/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::stdlib::ctypes::basics::{
use crate::stdlib::ctypes::function::PyCFuncPtr;
use crate::stdlib::ctypes::pointer::PyCPointer;
use crate::function::Either;
use crate::{PyObjectRef, PyRef, PyResult, VirtualMachine};
use crate::{PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine};
use crate::protocol::PyBuffer;

const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfguzZPqQ?";
Expand Down Expand Up @@ -259,20 +259,22 @@ pub fn new_simple_type(
}
}

#[derive(PyPayload)]
#[pyclass(module = "_ctypes", name = "PyCSimpleType", base = "PyType")]
pub struct PySimpleMeta {}

#[pyclass(with(PyCDataMethods), flags(BASETYPE))]
impl PySimpleMeta {
#[pyslot]
fn tp_new(cls: PyTypeRef, _: OptionalArg, vm: &VirtualMachine) -> PyResult {
fn new(cls: PyTypeRef, _: OptionalArg, vm: &VirtualMachine) -> PyResult {
Ok(new_simple_type(Either::B(&cls), vm)?
.into_ref_with_type(vm, cls)?
.as_object()
.clone())
}
}

#[derive(PyPayload)]
#[pyclass(
module = "_ctypes",
name = "_SimpleCData",
Expand Down