Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
41b30de
metaclass support for #[pyclass] macro
youknowone Aug 7, 2021
b4a1b80
Initial ctypes module structure with dlopen and dlsym
rodrigocam Oct 29, 2020
b9ada0a
Refactoring ctypes module to a PyPy-like file tree
darleybarreto Oct 31, 2020
1fa7e80
Add __getattr__ to deal with attributes at runtime
rodrigocam Oct 31, 2020
b30dbae
Adding a very basic and initial implementation of _SimpleCData and Fu…
darleybarreto Nov 2, 2020
89587f0
Add PyFuncPtr tp_new from DLL and basic tp_call
rodrigocam Nov 3, 2020
b4ddf4d
Adding basic functions to call libffi-rs
darleybarreto Nov 4, 2020
9177473
Fix compile errors
rodrigocam Nov 5, 2020
14b08b9
Changing some pieces of SharedLibrary
darleybarreto Nov 7, 2020
8adf4dd
Fixing some ref problems in functions.rs
darleybarreto Nov 8, 2020
4341c97
Cahnge PyRc to PyRef in data cache
rodrigocam Nov 10, 2020
eb8541c
Fixing arg type casting
darleybarreto Nov 10, 2020
e38b0a8
Refactoring PyCFuncPtr
darleybarreto Nov 12, 2020
c2df5de
Moving dlsym to from_dll
darleybarreto Nov 12, 2020
fa6b19b
Adding proper *mut c_void casting
darleybarreto Nov 13, 2020
7d220c3
Adding 'reopen' lib
darleybarreto Nov 13, 2020
0a92095
Adding function call
darleybarreto Nov 13, 2020
8ceffd9
Fixing clippy warnings
darleybarreto Nov 14, 2020
4c585ab
Fixing dangling ref
darleybarreto Nov 15, 2020
b09ebe0
Starting primitive types impl
darleybarreto Nov 19, 2020
d7888f9
Adding metaclass
darleybarreto Nov 19, 2020
145afc7
Adding PyCDataMethods trait
darleybarreto Nov 21, 2020
aff291f
Adding default value for PySimpleType
darleybarreto Nov 22, 2020
d29c3bf
Adding some comments
darleybarreto Nov 23, 2020
6fdffab
Implement PySimpleType __init__
darleybarreto Nov 26, 2020
568c838
Modifying Buffer for PyCData
darleybarreto Nov 29, 2020
fb07e53
Adding methods for PyCDataMethods
darleybarreto Nov 30, 2020
6122e40
Adding PyCData_NewGetBuffer related code
darleybarreto Dec 4, 2020
6a4f881
Fixing small fixes
darleybarreto Dec 4, 2020
e2e0ac8
Testing PySimpleType basic functionalities
darleybarreto Dec 5, 2020
c1324ce
Refactoring SharedLibrary
darleybarreto Dec 5, 2020
3f40466
Fixing several bugs
darleybarreto Dec 5, 2020
3e52625
Fixing function call
darleybarreto Dec 6, 2020
3454751
Fixing more of function calls
darleybarreto Dec 6, 2020
0f25f4d
PySimpleType from_param initial commit
darleybarreto Dec 7, 2020
c4f27fb
Adding more methods to PySimpleType
darleybarreto Dec 7, 2020
b88e544
Minor fixes to get compiling on master
coolreader18 Dec 9, 2020
4ce1123
Use static_cell for libcache
coolreader18 Dec 13, 2020
e528fe7
Adding RawBuffer & reworking low level function call
darleybarreto Dec 19, 2020
420a67a
Small fixes
darleybarreto Dec 21, 2020
d656650
Initial commit for PyCArray
darleybarreto Dec 21, 2020
2a1f6d7
Reworking CData buffers
darleybarreto Dec 30, 2020
ed44269
Adding PyCArray setitem
darleybarreto Dec 31, 2020
4d2c678
Adding some helper functions and initial tests
darleybarreto Dec 31, 2020
2c440b3
Adding PyCArray's from_param
darleybarreto Jan 1, 2021
0b65e0d
Adding several changes to make some tests pass
darleybarreto Jan 2, 2021
7c529af
Fix build on master
youknowone Aug 6, 2021
fe01b1a
hide ctypes test
youknowone Aug 6, 2021
25e0403
clean up ctypes::array
youknowone Aug 7, 2021
efabe9b
skeleton PyCSimpleType
youknowone Aug 7, 2021
b79477a
submodule extension for ctypes::dll
youknowone Aug 7, 2021
2904015
Merge pull request #4 from youknowone/dll-submodule
darleybarreto Aug 7, 2021
66a91cb
Add all tests from CPython and PyPy
darleybarreto Aug 8, 2021
577c5ae
Add suggestions and bump dependencies
darleybarreto Aug 8, 2021
6eb0794
Starting to add Metas to primitive and array ctypes
darleybarreto Aug 8, 2021
5916218
Fixing some terribly wrong impls and bugs.
darleybarreto Aug 15, 2021
c2ba116
Fix compilation
darleybarreto Aug 21, 2021
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
PySimpleType from_param initial commit
  • Loading branch information
darleybarreto authored and youknowone committed Aug 7, 2021
commit 0f25f4d5b69021d39e2525886d356476f194f80a
22 changes: 22 additions & 0 deletions vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::fmt;

use crate::builtins::PyTypeRef;
use crate::pyobject::{PyValue, StaticType};
use crate::VirtualMachine;

use crate::stdlib::ctypes::basics::PyCData;

#[pyclass(module = "_ctypes", name = "Array", base = "PyCData")]
pub struct PyCArray {}

impl fmt::Debug for PyCArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PyCArray {{}}")
}
}

impl PyValue for PyCArray {
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
Self::static_type()
}
}
4 changes: 2 additions & 2 deletions vm/src/stdlib/ctypes/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn at_address(cls: &PyTypeRef, buf: usize, vm: &VirtualMachine) -> PyResult<Vec<
// @TODO: A sanity check
Err(_) => Err(vm.new_type_error("attribute '__abstract__' must be bool".to_string())),
},
// @TODO: I think it's unreacheble
// @TODO: I think it's unreachable
Err(_) => Err(vm.new_attribute_error("abstract class".to_string())),
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ fn buffer_copy(
}
}
}
// @TODO: I think this is unreacheble...
// @TODO: I think this is unreachable...
Err(_) => Err(vm.new_type_error("abstract class".to_string())),
}
}
Expand Down
4 changes: 4 additions & 0 deletions vm/src/stdlib/ctypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ use crate::pyobject::PyClassImpl;
use crate::pyobject::PyObjectRef;
use crate::VirtualMachine;

mod array;
mod basics;
mod dll;
mod function;
mod pointer;
mod primitive;
mod shared_lib;

use array::PyCArray;
use basics::PyCData;
use dll::*;
use function::PyCFuncPtr;
use pointer::PyCPointer;
use primitive::PySimpleType;

pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
Expand Down
22 changes: 22 additions & 0 deletions vm/src/stdlib/ctypes/pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::fmt;

use crate::builtins::PyTypeRef;
use crate::pyobject::{PyValue, StaticType};
use crate::VirtualMachine;

use crate::stdlib::ctypes::basics::PyCData;

#[pyclass(module = "_ctypes", name = "_Pointer", base = "PyCData")]
pub struct PyCPointer {}

impl fmt::Debug for PyCPointer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "_Pointer {{}}")
}
}

impl PyValue for PyCPointer {
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
Self::static_type()
}
}
279 changes: 222 additions & 57 deletions vm/src/stdlib/ctypes/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,13 @@ use crate::pyobject::{
};
use crate::VirtualMachine;

use crate::stdlib::ctypes::array::PyCArray;
use crate::stdlib::ctypes::basics::PyCData;
use crate::stdlib::ctypes::function::PyCFuncPtr;
use crate::stdlib::ctypes::pointer::PyCPointer;

pub const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfguzZqQ?";

#[pyclass(module = "_ctypes", name = "_SimpleCData", base = "PyCData")]
pub struct PySimpleType {
_type_: String,
value: AtomicCell<PyObjectRc>,
__abstract__: bool,
}

impl fmt::Debug for PySimpleType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = unsafe { (*self.value.as_ptr()).to_string() };

write!(
f,
"PySimpleType {{
_type_: {},
value: {},
}}",
self._type_.as_str(),
value
)
}
}

fn set_primitive(_type_: &str, value: &PyObjectRc, vm: &VirtualMachine) -> PyResult<PyObjectRc> {
match _type_ {
"c" => {
Expand Down Expand Up @@ -143,6 +123,162 @@ fn set_primitive(_type_: &str, value: &PyObjectRc, vm: &VirtualMachine) -> PyRes
}
}

fn generic_xxx_p_from_param(
cls: &PyTypeRef,
value: &PyObjectRef,
type_str: &str,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
if vm.is_none(value)
|| vm.isinstance(value, &vm.ctx.types.str_type)?
|| vm.isinstance(value, &vm.ctx.types.bytes_type)?
{
Ok(PySimpleType {
_type_: type_str.to_string(),
value: AtomicCell::new(value.clone()),
__abstract__: true,
}
.into_object(vm))
} else if vm.isinstance(value, PySimpleType::static_type())?
&& (type_str == "z" || type_str == "Z" || type_str == "P")
{
Ok(value.clone())
} else {
// @TODO: better message
Err(vm.new_type_error("wrong type".to_string()))
}
}

fn from_param_char_p(
cls: &PyTypeRef,
value: &PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
let _type_ = vm
.get_attribute(value.clone(), "_type_")?
.downcast_exact::<PyStr>(vm)
.unwrap();
let type_str = _type_.as_ref();

let res = generic_xxx_p_from_param(cls, value, type_str, vm)?;

if !vm.is_none(&res) {
Ok(res)
} else if (vm.isinstance(value, PyCArray::static_type())?
|| vm.isinstance(value, PyCPointer::static_type())?)
&& (type_str == "z" || type_str == "Z" || type_str == "P")
{
Ok(value.clone())
} else {
// @TODO: Make sure of what goes here
Err(vm.new_type_error("some error".to_string()))
}
}

fn from_param_void_p(
cls: &PyTypeRef,
value: &PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
let _type_ = vm
.get_attribute(value.clone(), "_type_")?
.downcast_exact::<PyStr>(vm)
.unwrap();
let type_str = _type_.as_ref();

let res = generic_xxx_p_from_param(cls, value, type_str, vm)?;

if !vm.is_none(&res) {
Ok(res)
} else if vm.isinstance(value, PyCArray::static_type())? {
Ok(value.clone())
} else if vm.isinstance(value, PyCFuncPtr::static_type())?
|| vm.isinstance(value, PyCPointer::static_type())?
{
// @TODO: Is there a better way of doing this?
if let Some(from_address) = vm.get_method(cls.as_object().clone(), "from_address") {
if let Ok(cdata) = value.clone().downcast_exact::<PyCData>(vm) {
let mut buffer_guard = cdata.borrow_value_mut();
let addr = buffer_guard.as_mut_ptr() as usize;

Ok(vm.invoke(&from_address?, (cls.clone_class(), addr))?)
} else {
// @TODO: Make sure of what goes here
Err(vm.new_type_error("value should be an instance of _CData".to_string()))
}
} else {
// @TODO: Make sure of what goes here
Err(vm.new_attribute_error("class has no from_address method".to_string()))
}
} else if vm.isinstance(value, &vm.ctx.types.int_type)? {
Ok(PySimpleType {
_type_: type_str.to_string(),
value: AtomicCell::new(value.clone()),
__abstract__: true,
}
.into_object(vm))
} else {
// @TODO: Make sure of what goes here
Err(vm.new_type_error("some error".to_string()))
}
}

fn new_simple_type(cls: &PyTypeRef, vm: &VirtualMachine) -> PyResult<PySimpleType> {
let is_abstract = cls.name == PySimpleType::static_type().name;

if is_abstract {
return Err(vm.new_type_error("abstract class".to_string()));
}

match vm.get_attribute(cls.as_object().to_owned(), "_type_") {
Ok(_type_) if vm.isinstance(&_type_, &vm.ctx.types.str_type)? => {
let tp_str = _type_.downcast_exact::<PyStr>(vm).unwrap().to_string();

if tp_str.len() != 1 {
Err(vm.new_value_error(
"class must define a '_type_' attribute which must be a string of length 1"
.to_string(),
))
} else if !SIMPLE_TYPE_CHARS.contains(tp_str.as_str()) {
Err(vm.new_attribute_error(format!("class must define a '_type_' attribute which must be a single character string containing one of {}.",SIMPLE_TYPE_CHARS)))
} else {
Ok(PySimpleType {
_type_: tp_str,
value: AtomicCell::new(vm.ctx.none()),
__abstract__: is_abstract,
})
}
}
Ok(_) => {
Err(vm.new_type_error("class must define a '_type_' string attribute".to_string()))
}
Err(_) => Err(vm.new_attribute_error("class must define a '_type_' attribute".to_string())),
}
}

#[pyclass(module = "_ctypes", name = "_SimpleCData", base = "PyCData")]
pub struct PySimpleType {
_type_: String,
value: AtomicCell<PyObjectRc>,
__abstract__: bool,
}

impl fmt::Debug for PySimpleType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = unsafe { (*self.value.as_ptr()).to_string() };

write!(
f,
"PySimpleType {{
_type_: {},
value: {},
}}",
self._type_.as_str(),
value
)
}
}

impl PyValue for PySimpleType {
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
Self::static_type()
Expand All @@ -153,39 +289,7 @@ impl PyValue for PySimpleType {
impl PySimpleType {
#[pyslot]
fn tp_new(cls: PyTypeRef, _: OptionalArg, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let is_abstract = cls.name == PySimpleType::static_type().name;

if is_abstract {
return Err(vm.new_type_error("abstract class".to_string()));
}

match vm.get_attribute(cls.as_object().to_owned(), "_type_") {
Ok(_type_) => {
if vm.isinstance(&_type_, &vm.ctx.types.str_type)? {
let tp_str = _type_.downcast_exact::<PyStr>(vm).unwrap().to_string();

if tp_str.len() != 1 {
Err(vm.new_value_error("class must define a '_type_' attribute which must be a string of length 1".to_string()))
} else if !SIMPLE_TYPE_CHARS.contains(tp_str.as_str()) {
Err(vm.new_attribute_error(format!("class must define a '_type_' attribute which must be a single character string containing one of {}.",SIMPLE_TYPE_CHARS)))
} else {
PySimpleType {
_type_: tp_str,
value: AtomicCell::new(vm.ctx.none()),
__abstract__: is_abstract,
}
.into_ref_with_type(vm, cls)
}
} else {
Err(vm.new_type_error(
"class must define a '_type_' string attribute".to_string(),
))
}
}
Err(_) => {
Err(vm.new_attribute_error("class must define a '_type_' attribute".to_string()))
}
}
new_simple_type(&cls, vm)?.into_ref_with_type(vm, cls)
}

#[pymethod(magic)]
Expand Down Expand Up @@ -226,7 +330,68 @@ impl PySimpleType {

// From PyCSimpleType_Type PyCSimpleType_methods
#[pyclassmethod]
pub fn from_param(cls: PyTypeRef, vm: &VirtualMachine) {}
pub fn from_param(
cls: PyTypeRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
if cls.name == PySimpleType::static_type().name {
Err(vm.new_type_error("abstract class".to_string()))
} else if vm.isinstance(&value, &cls)? {
Ok(value)
} else {
match vm.get_attribute(cls.as_object().to_owned(), "_type_") {
Ok(tp_obj) if vm.isinstance(&tp_obj, &vm.ctx.types.str_type)? => {
let _type_ = tp_obj.downcast_exact::<PyStr>(vm).unwrap();
let tp_str = _type_.as_ref();

match tp_str {
"z" | "Z" => from_param_char_p(&cls, &value, vm),
"P" => from_param_void_p(&cls, &value, vm),
_ => {
match new_simple_type(&cls, vm) {
Ok(obj) => Ok(obj.into_object(vm)),
Err(e)
if vm.isinstance(
&e.clone().into_object(),
&vm.ctx.exceptions.type_error,
)? || vm.isinstance(
&e.clone().into_object(),
&vm.ctx.exceptions.value_error,
)? =>
{
if let Some(my_base) = cls.base.clone() {
if let Some(from_param) =
vm.get_method(my_base.as_object().clone(), "from_param")
{
Ok(vm.invoke(
&from_param?,
(my_base.clone_class(), value.clone()),
)?)
} else {
// @TODO: Make sure of what goes here
Err(vm.new_attribute_error(
"class has no from_param method".to_string(),
))
}
} else {
// @TODO: This should be unreachable
Err(vm.new_type_error("class has no base".to_string()))
}
}
Err(e) => Err(e),
}
}
}
}
// @TODO: Sanity check, this should be unreachable
_ => {
Err(vm
.new_attribute_error("class must define a '_type_' attribute".to_string()))
}
}
}
}

// Simple_repr
#[pymethod(magic)]
Expand Down