|
1 |
| -pub(crate) use _ctypes::make_module; |
| 1 | +pub(crate) mod base; |
| 2 | + |
| 3 | +use crate::builtins::PyModule; |
| 4 | +use crate::{PyRef, VirtualMachine}; |
| 5 | + |
| 6 | +pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> { |
| 7 | + let module = _ctypes::make_module(&vm); |
| 8 | + base::extend_module_nodes(&vm, &module); |
| 9 | + module |
| 10 | +} |
2 | 11 |
|
3 | 12 | #[pymodule]
|
4 |
| -mod _ctypes { |
5 |
| - use crate::{common::lock::PyRwLock, PyObjectRef}; |
| 13 | +pub(crate) mod _ctypes { |
| 14 | + use super::base::PyCSimple; |
| 15 | + use crate::builtins::PyTypeRef; |
| 16 | + use crate::class::StaticType; |
| 17 | + use crate::function::Either; |
| 18 | + use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine}; |
6 | 19 | use crossbeam_utils::atomic::AtomicCell;
|
| 20 | + use std::ffi::{ |
| 21 | + c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong, |
| 22 | + c_ulonglong, |
| 23 | + }; |
| 24 | + use std::mem; |
| 25 | + use widestring::WideChar; |
7 | 26 |
|
8 |
| - pub struct RawBuffer { |
9 |
| - #[allow(dead_code)] |
10 |
| - pub inner: Box<[u8]>, |
11 |
| - #[allow(dead_code)] |
12 |
| - pub size: usize, |
| 27 | + pub fn get_size(ty: &str) -> usize { |
| 28 | + match ty { |
| 29 | + "u" => mem::size_of::<WideChar>(), |
| 30 | + "c" | "b" => mem::size_of::<c_schar>(), |
| 31 | + "h" => mem::size_of::<c_short>(), |
| 32 | + "H" => mem::size_of::<c_short>(), |
| 33 | + "i" => mem::size_of::<c_int>(), |
| 34 | + "I" => mem::size_of::<c_uint>(), |
| 35 | + "l" => mem::size_of::<c_long>(), |
| 36 | + "q" => mem::size_of::<c_longlong>(), |
| 37 | + "L" => mem::size_of::<c_ulong>(), |
| 38 | + "Q" => mem::size_of::<c_ulonglong>(), |
| 39 | + "f" => mem::size_of::<c_float>(), |
| 40 | + "d" | "g" => mem::size_of::<c_double>(), |
| 41 | + "?" | "B" => mem::size_of::<c_uchar>(), |
| 42 | + "P" | "z" | "Z" => mem::size_of::<usize>(), |
| 43 | + _ => unreachable!(), |
| 44 | + } |
13 | 45 | }
|
14 | 46 |
|
15 |
| - #[pyattr] |
16 |
| - #[pyclass(name = "_CData")] |
17 |
| - pub struct PyCData { |
18 |
| - _objects: AtomicCell<Vec<PyObjectRef>>, |
19 |
| - _buffer: PyRwLock<RawBuffer>, |
| 47 | + const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfguzZPqQ?"; |
| 48 | + |
| 49 | + pub fn new_simple_type( |
| 50 | + cls: Either<&PyObjectRef, &PyTypeRef>, |
| 51 | + vm: &VirtualMachine, |
| 52 | + ) -> PyResult<PyCSimple> { |
| 53 | + let cls = match cls { |
| 54 | + Either::A(obj) => obj, |
| 55 | + Either::B(typ) => typ.as_object(), |
| 56 | + }; |
| 57 | + |
| 58 | + if let Ok(_type_) = cls.get_attr("_type_", vm) { |
| 59 | + if _type_.is_instance((&vm.ctx.types.str_type).as_ref(), vm)? { |
| 60 | + let tp_str = _type_.str(vm)?.to_string(); |
| 61 | + |
| 62 | + if tp_str.len() != 1 { |
| 63 | + Err(vm.new_value_error( |
| 64 | + format!("class must define a '_type_' attribute which must be a string of length 1, str: {tp_str}"), |
| 65 | + )) |
| 66 | + } else if !SIMPLE_TYPE_CHARS.contains(tp_str.as_str()) { |
| 67 | + Err(vm.new_attribute_error(format!("class must define a '_type_' attribute which must be\n a single character string containing one of {SIMPLE_TYPE_CHARS}, currently it is {tp_str}."))) |
| 68 | + } else { |
| 69 | + Ok(PyCSimple { |
| 70 | + _type_: tp_str, |
| 71 | + value: AtomicCell::new(vm.ctx.none()), |
| 72 | + }) |
| 73 | + } |
| 74 | + } else { |
| 75 | + Err(vm.new_type_error("class must define a '_type_' string attribute".to_string())) |
| 76 | + } |
| 77 | + } else { |
| 78 | + Err(vm.new_attribute_error("class must define a '_type_' attribute".to_string())) |
| 79 | + } |
20 | 80 | }
|
21 | 81 |
|
22 |
| - #[pyclass] |
23 |
| - impl PyCData {} |
| 82 | + #[pyfunction(name = "sizeof")] |
| 83 | + pub fn size_of(tp: Either<PyTypeRef, PyObjectRef>, vm: &VirtualMachine) -> PyResult<usize> { |
| 84 | + match tp { |
| 85 | + Either::A(type_) if type_.fast_issubclass(PyCSimple::static_type()) => { |
| 86 | + let zelf = new_simple_type(Either::B(&type_), vm)?; |
| 87 | + Ok(get_size(zelf._type_.as_str())) |
| 88 | + } |
| 89 | + Either::B(obj) if obj.has_attr("size_of_instances", vm)? => { |
| 90 | + let size_of_method = obj.get_attr("size_of_instances", vm)?; |
| 91 | + let size_of_return = size_of_method.call(vec![], vm)?; |
| 92 | + Ok(usize::try_from_object(vm, size_of_return)?) |
| 93 | + } |
| 94 | + _ => Err(vm.new_type_error("this type has no size".to_string())), |
| 95 | + } |
| 96 | + } |
24 | 97 |
|
25 | 98 | #[pyfunction]
|
26 | 99 | fn get_errno() -> i32 {
|
|
0 commit comments