Skip to content

Commit c046124

Browse files
committed
cut down the number of errors by ~75% by porting changes over from refactorings
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 8de573a commit c046124

9 files changed

Lines changed: 141 additions & 139 deletions

File tree

vm/src/stdlib/ctypes/array.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@ use super::{
44
};
55
use crate::builtins::{
66
self,
7-
memory::{try_buffer_from_object, Buffer},
87
slice::PySlice,
98
PyBytes, PyInt, PyList, PyRange, PyStr, PyType, PyTypeRef,
109
};
1110
use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
12-
use crate::function::OptionalArg;
13-
use crate::pyobject::{
14-
IdProtocol, ItemProtocol, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject,
15-
TypeProtocol,
16-
};
11+
use crate::function::{Either, OptionalArg};
1712
use crate::sliceable::SequenceIndex;
18-
use crate::slots::BufferProtocol;
1913
use crate::stdlib::ctypes::basics::{
2014
default_from_param, generic_get_buffer, get_size, BorrowValue as BorrowValueCData,
2115
BorrowValueMut, PyCData, PyCDataFunctions, PyCDataMethods, PyCDataSequenceMethods, RawBuffer,
2216
};
23-
use crate::utils::Either;
24-
use crate::VirtualMachine;
17+
use crate::{AsObject, Context, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine};
18+
use rustpython_vm::object::PyPayload;
2519
use num_traits::Signed;
2620
use std::convert::TryInto;
2721
use std::fmt;
2822
use widestring::WideCString;
23+
use crate::class::StaticType;
24+
use crate::convert::IntoObject;
25+
use crate::protocol::PyBuffer;
2926

3027
// TODO: make sure that this is correct wrt windows and unix wstr
3128
fn slice_to_obj(ty: &str, b: &[u8], vm: &VirtualMachine) -> PyResult {
@@ -127,7 +124,7 @@ fn set_array_value(
127124
}
128125

129126
if vm.isinstance(&obj, &self_cls)? {
130-
let o_buffer = try_buffer_from_object(vm, &obj)?;
127+
let o_buffer = PyBuffer::try_from_object(vm, &obj)?;
131128
let src_buffer = o_buffer.obj_bytes();
132129

133130
assert!(dst_buffer.len() == size && src_buffer.len() >= size);
@@ -318,18 +315,6 @@ impl fmt::Debug for PyCArray {
318315
}
319316
}
320317

321-
impl PyValue for PyCArrayMeta {
322-
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
323-
Self::static_type()
324-
}
325-
}
326-
327-
impl PyValue for PyCArray {
328-
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
329-
Self::static_type()
330-
}
331-
}
332-
333318
impl<'a> BorrowValueCData<'a> for PyCArray {
334319
fn borrow_value(&'a self) -> PyRwLockReadGuard<'a, RawBuffer> {
335320
self._buffer.read()
@@ -407,7 +392,7 @@ impl PyCDataMethods for PyCArrayMeta {
407392
}
408393
}
409394

410-
#[pyimpl(with(PyCDataMethods), flags(BASETYPE))]
395+
#[pyclass(with(PyCDataMethods), flags(BASETYPE))]
411396
impl PyCArrayMeta {
412397
#[pyslot]
413398
fn tp_new(cls: PyTypeRef, vm: &VirtualMachine) -> PyResult {
@@ -433,7 +418,7 @@ impl PyCArrayMeta {
433418
}
434419
}
435420

436-
#[pyimpl(flags(BASETYPE), with(BufferProtocol, PyCDataFunctions))]
421+
#[pyclass(flags(BASETYPE), with(BufferProtocol, PyCDataFunctions))]
437422
impl PyCArray {
438423
#[pymethod(magic)]
439424
pub fn init(zelf: PyRef<Self>, value: OptionalArg, vm: &VirtualMachine) -> PyResult<()> {
@@ -463,11 +448,11 @@ impl PyCArray {
463448
})
464449
}
465450

466-
#[pyproperty]
451+
#[pygetset(magic)]
467452
pub fn value(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
468453
// TODO: make sure that this is correct
469454
let obj = zelf.as_object();
470-
let buffer = try_buffer_from_object(vm, obj)?;
455+
let buffer = PyBuffer::try_from_object(vm, &obj)?;
471456

472457
let res = if zelf._type_._type_ == "u" {
473458
vm.new_pyobj(
@@ -519,10 +504,10 @@ impl PyCArray {
519504
Ok(res)
520505
}
521506

522-
#[pyproperty(setter)]
507+
#[pygetset(magic, setter)]
523508
fn set_value(zelf: PyRef<Self>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
524509
let obj = zelf.as_object();
525-
let buffer = try_buffer_from_object(vm, obj)?;
510+
let buffer = PyBuffer::try_from_object(vm, &obj)?;
526511
let my_size = buffer.get_options().len;
527512
let mut bytes = buffer.obj_bytes_mut();
528513

@@ -582,24 +567,23 @@ impl PyCArray {
582567
Ok(())
583568
}
584569

585-
#[pyproperty]
570+
#[pygetset(magic)]
586571
fn raw(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyBytes> {
587572
// zelf._type_ == "c"
588573

589574
let obj = zelf.as_object();
590-
let buffer = try_buffer_from_object(vm, obj)?;
575+
let buffer = PyBuffer::try_from_object(vm, obj)?;
591576
let buffer_vec = buffer.obj_bytes().to_vec();
592577

593578
Ok(PyBytes::from(buffer_vec))
594579
}
595580

596-
#[pyproperty(setter)]
581+
#[pygetset(magic, setter)]
597582
fn set_raw(zelf: PyRef<Self>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
598-
let obj = zelf.as_object();
599-
let my_buffer = try_buffer_from_object(vm, obj)?;
583+
let my_buffer = PyBuffer::try_from_object(vm, zelf)?;
600584
let my_size = my_buffer.get_options().len;
601585

602-
let new_value = try_buffer_from_object(vm, &value)?;
586+
let new_value = PyBuffer::try_from_object(vm, &value)?;
603587
let new_size = new_value.get_options().len;
604588

605589
// byte string zelf._type_ == "c"
@@ -620,7 +604,7 @@ impl PyCArray {
620604

621605
#[pymethod(magic)]
622606
fn getitem(zelf: PyRef<Self>, k_or_idx: SequenceIndex, vm: &VirtualMachine) -> PyResult {
623-
let buffer = try_buffer_from_object(vm, zelf.as_object())?;
607+
let buffer = PyBuffer::try_from_object(vm, zelf.as_object())?;
624608
let buffer_size = buffer.get_options().len;
625609
let buffer_bytes = buffer.obj_bytes();
626610
let size = buffer_size / zelf._length_;
@@ -649,7 +633,7 @@ impl PyCArray {
649633
obj: PyObjectRef,
650634
vm: &VirtualMachine,
651635
) -> PyResult<()> {
652-
let buffer = try_buffer_from_object(vm, zelf.as_object())?;
636+
let buffer = PyBuffer::try_from_object(vm, zelf.as_object())?;
653637
let buffer_size = buffer.get_options().len;
654638
let mut buffer_bytes = buffer.obj_bytes_mut();
655639

vm/src/stdlib/ctypes/basics.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
1-
use std::{fmt, mem, os::raw::*, ptr, slice};
1+
use std::{fmt, os::raw::*, ptr, slice};
22

33
use widestring::WideChar;
44

55
use crate::builtins::int::PyInt;
6-
use crate::builtins::memory::{try_buffer_from_object, Buffer, BufferOptions};
76
use crate::builtins::pystr::PyStrRef;
8-
use crate::builtins::pytype::PyTypeRef;
97
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
108
use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
119
use crate::function::OptionalArg;
12-
use crate::pyobject::{
13-
PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol,
14-
};
15-
use crate::slots::BufferProtocol;
16-
use crate::utils::Either;
17-
use crate::VirtualMachine;
10+
use crate::{PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine};
1811

1912
use crate::stdlib::ctypes::array::make_array_with_length;
2013
use crate::stdlib::ctypes::dll::dlsym;
2114
use crate::stdlib::ctypes::primitive::{new_simple_type, PyCSimple};
2215

16+
use crate::builtins::PyTypeRef;
17+
use crate::protocol::PyBuffer;
2318
use crossbeam_utils::atomic::AtomicCell;
2419

2520
pub fn get_size(ty: &str) -> usize {
2621
match ty {
27-
"u" => mem::size_of::<WideChar>(),
28-
"c" | "b" => mem::size_of::<c_schar>(),
29-
"h" => mem::size_of::<c_short>(),
30-
"H" => mem::size_of::<c_short>(),
31-
"i" => mem::size_of::<c_int>(),
32-
"I" => mem::size_of::<c_uint>(),
33-
"l" => mem::size_of::<c_long>(),
34-
"q" => mem::size_of::<c_longlong>(),
35-
"L" => mem::size_of::<c_ulong>(),
36-
"Q" => mem::size_of::<c_ulonglong>(),
37-
"f" => mem::size_of::<c_float>(),
38-
"d" | "g" => mem::size_of::<c_double>(),
39-
"?" | "B" => mem::size_of::<c_uchar>(),
40-
"P" | "z" | "Z" => mem::size_of::<usize>(),
22+
"u" => size_of::<WideChar>(),
23+
"c" | "b" => size_of::<c_schar>(),
24+
"h" => size_of::<c_short>(),
25+
"H" => size_of::<c_short>(),
26+
"i" => size_of::<c_int>(),
27+
"I" => size_of::<c_uint>(),
28+
"l" => size_of::<c_long>(),
29+
"q" => size_of::<c_longlong>(),
30+
"L" => size_of::<c_ulong>(),
31+
"Q" => size_of::<c_ulonglong>(),
32+
"f" => size_of::<c_float>(),
33+
"d" | "g" => size_of::<c_double>(),
34+
"?" | "B" => size_of::<c_uchar>(),
35+
"P" | "z" | "Z" => size_of::<usize>(),
4136
_ => unreachable!(),
4237
}
4338
}
@@ -85,7 +80,7 @@ fn buffer_copy(
8580
Ok(attr) => {
8681
match bool::try_from_object(vm, attr) {
8782
Ok(b) if !b => {
88-
let buffer = try_buffer_from_object(vm, &obj)?;
83+
let buffer = PyBuffer::try_from_object(vm, &obj)?;
8984
let opts = buffer.get_options().clone();
9085

9186
// TODO: Fix the way the size of stored
@@ -145,7 +140,7 @@ fn buffer_copy(
145140

146141
pub fn default_from_param<T>(zelf: PyRef<T>, value: PyObjectRef, vm: &VirtualMachine) -> PyResult
147142
where
148-
T: PyCDataMethods + PyValue,
143+
T: PyCDataMethods + PyPayload,
149144
{
150145
//TODO: check if this behaves like it should
151146
let cls = zelf.as_object().clone_class();
@@ -161,8 +156,8 @@ where
161156
)))
162157
}
163158
}
164-
#[pyimpl]
165-
pub trait PyCDataFunctions: PyValue {
159+
#[pyclass]
160+
pub trait PyCDataFunctions: PyPayload {
166161
#[pymethod]
167162
fn size_of_instances(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<usize>;
168163

@@ -175,8 +170,8 @@ pub trait PyCDataFunctions: PyValue {
175170
#[pymethod]
176171
fn address_of(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult;
177172
}
178-
#[pyimpl]
179-
pub trait PyCDataMethods: PyValue {
173+
#[pyclass]
174+
pub trait PyCDataMethods: PyPayload {
180175
// A lot of the logic goes in this trait
181176
// There's also other traits that should have different implementations for some functions
182177
// present here
@@ -253,8 +248,8 @@ pub trait PyCDataMethods: PyValue {
253248
}
254249
}
255250

256-
#[pyimpl]
257-
pub trait PyCDataSequenceMethods: PyValue {
251+
#[pyclass]
252+
pub trait PyCDataSequenceMethods: PyPayload {
258253
// CDataType_as_sequence methods are default for all *Type_Type
259254
// Basically the sq_repeat slot is CDataType_repeat
260255
// which transforms into a Array
@@ -276,7 +271,7 @@ pub trait PyCDataSequenceMethods: PyValue {
276271

277272
pub fn generic_get_buffer<T>(zelf: &PyRef<T>, vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>>
278273
where
279-
for<'a> T: PyValue + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
274+
for<'a> T: PyPayload + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
280275
{
281276
if let Ok(buffer) = vm.get_attribute(zelf.as_object().clone(), "_buffer") {
282277
if let Ok(_buffer) = buffer.downcast_exact::<RawBuffer>(vm) {
@@ -325,7 +320,7 @@ impl BufferProtocol for PyCData {
325320
// This trait will be used by all types
326321
impl<T> Buffer for PyCBuffer<T>
327322
where
328-
for<'a> T: PyValue + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
323+
for<'a> T: PyPayload + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
329324
{
330325
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
331326
PyRwLockReadGuard::map(self.data.borrow_value(), |x| unsafe {
@@ -351,7 +346,7 @@ where
351346
#[derive(Debug)]
352347
pub struct PyCBuffer<T>
353348
where
354-
for<'a> T: PyValue + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
349+
for<'a> T: PyPayload + fmt::Debug + BorrowValue<'a> + BorrowValueMut<'a>,
355350
{
356351
pub data: PyRef<T>,
357352
pub options: BufferOptions,
@@ -369,7 +364,7 @@ impl fmt::Debug for RawBuffer {
369364
}
370365
}
371366

372-
impl PyValue for RawBuffer {
367+
impl PyPayload for RawBuffer {
373368
fn class(vm: &VirtualMachine) -> &PyTypeRef {
374369
&vm.ctx.types.object_type
375370
}
@@ -393,7 +388,7 @@ impl fmt::Debug for PyCData {
393388
}
394389
}
395390

396-
impl PyValue for PyCData {
391+
impl PyPayload for PyCData {
397392
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
398393
Self::static_type()
399394
}
@@ -411,7 +406,7 @@ impl PyCData {
411406
}
412407
}
413408

414-
#[pyimpl(flags(BASETYPE), with(BufferProtocol))]
409+
#[pyclass(flags(BASETYPE), with(BufferProtocol))]
415410
impl PyCData {
416411
// PyCData_methods
417412
#[pymethod(magic)]

vm/src/stdlib/ctypes/dll.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub(crate) use _ctypes::*;
44
pub(crate) mod _ctypes {
55
use crate::builtins::pystr::PyStrRef;
66
use crate::builtins::PyIntRef;
7-
use crate::pyobject::{PyResult, TryFromObject};
87
use crate::VirtualMachine;
98

109
use super::super::shared_lib::libcache;

vm/src/stdlib/ctypes/function.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ use crate::builtins::{PyInt, PyTypeRef};
99
use crate::common::lock::PyRwLock;
1010

1111
use crate::function::FuncArgs;
12-
use crate::pyobject::{
13-
PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol,
14-
};
15-
use crate::VirtualMachine;
12+
use crate::{PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine};
1613

1714
use crate::stdlib::ctypes::basics::PyCData;
1815
use crate::stdlib::ctypes::primitive::PyCSimple;
1916

20-
use crate::slots::Callable;
2117
use crate::stdlib::ctypes::dll::dlsym;
18+
use crate::types::Callable;
2219

2320
macro_rules! ffi_type {
2421
($name: ident) => {
@@ -257,26 +254,26 @@ impl fmt::Debug for PyCFuncPtr {
257254
}
258255
}
259256

260-
impl PyValue for PyCFuncPtr {
257+
impl PyPayload for PyCFuncPtr {
261258
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
262259
Self::static_type()
263260
}
264261
}
265262

266-
#[pyimpl(with(Callable), flags(BASETYPE))]
263+
#[pyclass(with(Callable), flags(BASETYPE))]
267264
impl PyCFuncPtr {
268-
#[pyproperty(name = "_argtypes_")]
265+
#[pygetset(name = "_argtypes_")]
269266
fn argtypes(&self, vm: &VirtualMachine) -> PyObjectRef {
270267
vm.ctx
271268
.new_list(unsafe { &*self._argtypes_.as_ptr() }.clone())
272269
}
273270

274-
#[pyproperty(name = "_restype_")]
271+
#[pygetset(name = "_restype_")]
275272
fn restype(&self, _vm: &VirtualMachine) -> PyObjectRef {
276273
unsafe { &*self._restype_.as_ptr() }.clone()
277274
}
278275

279-
#[pyproperty(name = "_argtypes_", setter)]
276+
#[pygetset(name = "_argtypes_", setter)]
280277
fn set_argtypes(&self, argtypes: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
281278
if vm
282279
.isinstance(&argtypes, &vm.ctx.types.list_type)
@@ -322,7 +319,7 @@ impl PyCFuncPtr {
322319
Ok(())
323320
}
324321

325-
#[pyproperty(name = "_restype_", setter)]
322+
#[pygetset(name = "_restype_", setter)]
326323
fn set_restype(&self, restype: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
327324
match vm.isinstance(&restype, PyCSimple::static_type()) {
328325
// TODO: checks related to _type_ are temporary

0 commit comments

Comments
 (0)