Skip to content

Commit 458f0ed

Browse files
committed
temp
1 parent 76daf26 commit 458f0ed

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

extra_tests/snippets/builtins_ctypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ class c_bool(_SimpleCData):
128128

129129
i = c_int(42)
130130
f = c_float(3.14)
131-
s = create_string_buffer(b'\000' * 32)
132-
print(i.value, f.value, repr(s.value))
131+
# s = create_string_buffer(b'\000' * 32)
132+
print(i.value, f.value)

test.dll

5.14 MB
Binary file not shown.

vm/src/stdlib/ctypes.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ pub(crate) mod union;
99
use crate::builtins::PyModule;
1010
use crate::{Py, PyRef, VirtualMachine};
1111
use crate::class::PyClassImpl;
12-
use crate::stdlib::ctypes::base::{PyCData, PyCSimple};
12+
use crate::stdlib::ctypes::base::{PyCData, PyCSimple, PySimpleMeta};
1313

1414
pub fn extend_module_nodes(vm: &VirtualMachine, module: &Py<PyModule>) {
1515
let ctx = &vm.ctx;
16+
PySimpleMeta::make_class(ctx);
1617
extend_module!(vm, module, {
1718
"_CData" => PyCData::make_class(ctx),
1819
"_SimpleCData" => PyCSimple::make_class(ctx),
@@ -142,7 +143,7 @@ pub(crate) mod _ctypes {
142143
} else {
143144
Ok(PyCSimple {
144145
_type_: tp_str,
145-
_value: AtomicCell::new(vm.ctx.none()),
146+
value: AtomicCell::new(vm.ctx.none()),
146147
})
147148
}
148149
} else {
@@ -181,7 +182,7 @@ pub(crate) mod _ctypes {
181182

182183
#[cfg(target_os = "windows")]
183184
#[pyfunction(name = "FreeLibrary")]
184-
fn free_library(handle: usize, vm: &VirtualMachine) -> PyResult<()> {
185+
fn free_library(handle: usize) -> PyResult<()> {
185186
let cache = library::libcache();
186187
let mut cache_write = cache.write();
187188
cache_write.drop_lib(handle);

vm/src/stdlib/ctypes/base.rs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use crate::builtins::{PyBytes, PyFloat, PyInt, PyNone, PyStr};
2-
use crate::{PyObjectRef, PyResult, TryFromObject, VirtualMachine};
1+
use crate::builtins::{PyBytes, PyFloat, PyInt, PyNone, PyStr, PyTypeRef};
2+
use crate::{AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine};
33
use crossbeam_utils::atomic::AtomicCell;
44
use num_traits::ToPrimitive;
55
use rustpython_common::lock::PyRwLock;
66
use std::fmt::Debug;
7+
use crate::function::{Either, OptionalArg};
8+
use crate::stdlib::ctypes::_ctypes::new_simple_type;
9+
use crate::builtins::PyType;
710

811
#[allow(dead_code)]
912
fn set_primitive(_type_: &str, value: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -128,11 +131,24 @@ pub struct PyCData {
128131
#[pyclass]
129132
impl PyCData {}
130133

134+
#[pyclass(module = "_ctypes", name = "PyCSimpleType", base = "PyType")]
135+
pub struct PySimpleMeta {}
136+
137+
#[pyclass(flags(BASETYPE))]
138+
impl PySimpleMeta {
139+
#[pymethod]
140+
fn new(cls: PyTypeRef, _: OptionalArg, vm: &VirtualMachine) -> PyResult {
141+
Ok(PyObjectRef::from(new_simple_type(Either::B(&cls), vm)?
142+
.into_ref_with_type(vm, cls)?
143+
.clone()))
144+
}
145+
}
146+
131147
#[pyclass(
132148
name = "_SimpleCData",
133149
base = "PyCData",
134-
module = "_ctypes"
135-
// TODO: metaclass
150+
module = "_ctypes",
151+
metaclass = "PySimpleMeta"
136152
)]
137153
#[derive(PyPayload)]
138154
pub struct PyCSimple {
@@ -150,16 +166,44 @@ impl Debug for PyCSimple {
150166

151167
#[pyclass(flags(BASETYPE))]
152168
impl PyCSimple {
169+
#[pymethod(magic)]
170+
pub fn __init__(&self, value: OptionalArg, vm: &VirtualMachine) -> PyResult<()> {
171+
if let Some(ref v) = value.into_option() {
172+
let content = set_primitive(self._type_.as_str(), v, vm)?;
173+
self.value.store(content);
174+
} else {
175+
self.value.store(match self._type_.as_str() {
176+
"c" | "u" => PyObjectRef::from(vm.ctx.new_bytes(vec![0])),
177+
"b" | "B" | "h" | "H" | "i" | "I" | "l" | "q" | "L" | "Q" => PyObjectRef::from(vm.ctx.new_int(0)),
178+
"f" | "d" | "g" => PyObjectRef::from(vm.ctx.new_float(0.0)),
179+
"?" => PyObjectRef::from(vm.ctx.new_bool(false)),
180+
_ => vm.ctx.none(), // "z" | "Z" | "P"
181+
});
182+
}
183+
Ok(())
184+
}
153185

154186
#[pygetset(name = "value")]
155-
pub fn value(&self) -> PyObjectRef {
156-
unsafe { (*self.value.as_ptr()).clone() }
187+
pub fn value(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
188+
let cls = instance.class();
189+
let subcls_vec = cls.subclasses.read();
190+
for subcls in subcls_vec.iter() {
191+
println!("subcls {}", subcls);
192+
}
193+
println!("value {}", cls.name().to_string());
194+
let zelf: &Py<Self> = instance.downcast_ref().ok_or_else(|| {
195+
vm.new_type_error("cannot get value of instance".to_string())
196+
})?;
197+
Ok(unsafe { (*zelf.value.as_ptr()).clone() })
157198
}
158199

159200
#[pygetset(name = "value", setter)]
160-
fn set_value(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
161-
let content = set_primitive(self._type_.as_str(), &value, vm)?;
162-
self.value.store(content);
201+
fn set_value(instance: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
202+
let zelf: PyRef<Self> = instance.downcast().map_err(|_| {
203+
vm.new_type_error("cannot set value of instance".to_string())
204+
})?;
205+
let content = set_primitive(zelf._type_.as_str(), &value, vm)?;
206+
zelf.value.store(content);
163207
Ok(())
164208
}
165209
}

0 commit comments

Comments
 (0)