-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Ctypes implementation #2364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Ctypes implementation #2364
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 b4a1b80
Initial ctypes module structure with dlopen and dlsym
rodrigocam b9ada0a
Refactoring ctypes module to a PyPy-like file tree
darleybarreto 1fa7e80
Add __getattr__ to deal with attributes at runtime
rodrigocam b30dbae
Adding a very basic and initial implementation of _SimpleCData and Fu…
darleybarreto 89587f0
Add PyFuncPtr tp_new from DLL and basic tp_call
rodrigocam b4ddf4d
Adding basic functions to call libffi-rs
darleybarreto 9177473
Fix compile errors
rodrigocam 14b08b9
Changing some pieces of SharedLibrary
darleybarreto 8adf4dd
Fixing some ref problems in functions.rs
darleybarreto 4341c97
Cahnge PyRc to PyRef in data cache
rodrigocam eb8541c
Fixing arg type casting
darleybarreto e38b0a8
Refactoring PyCFuncPtr
darleybarreto c2df5de
Moving dlsym to from_dll
darleybarreto fa6b19b
Adding proper *mut c_void casting
darleybarreto 7d220c3
Adding 'reopen' lib
darleybarreto 0a92095
Adding function call
darleybarreto 8ceffd9
Fixing clippy warnings
darleybarreto 4c585ab
Fixing dangling ref
darleybarreto b09ebe0
Starting primitive types impl
darleybarreto d7888f9
Adding metaclass
darleybarreto 145afc7
Adding PyCDataMethods trait
darleybarreto aff291f
Adding default value for PySimpleType
darleybarreto d29c3bf
Adding some comments
darleybarreto 6fdffab
Implement PySimpleType __init__
darleybarreto 568c838
Modifying Buffer for PyCData
darleybarreto fb07e53
Adding methods for PyCDataMethods
darleybarreto 6122e40
Adding PyCData_NewGetBuffer related code
darleybarreto 6a4f881
Fixing small fixes
darleybarreto e2e0ac8
Testing PySimpleType basic functionalities
darleybarreto c1324ce
Refactoring SharedLibrary
darleybarreto 3f40466
Fixing several bugs
darleybarreto 3e52625
Fixing function call
darleybarreto 3454751
Fixing more of function calls
darleybarreto 0f25f4d
PySimpleType from_param initial commit
darleybarreto c4f27fb
Adding more methods to PySimpleType
darleybarreto b88e544
Minor fixes to get compiling on master
coolreader18 4ce1123
Use static_cell for libcache
coolreader18 e528fe7
Adding RawBuffer & reworking low level function call
darleybarreto 420a67a
Small fixes
darleybarreto d656650
Initial commit for PyCArray
darleybarreto 2a1f6d7
Reworking CData buffers
darleybarreto ed44269
Adding PyCArray setitem
darleybarreto 4d2c678
Adding some helper functions and initial tests
darleybarreto 2c440b3
Adding PyCArray's from_param
darleybarreto 0b65e0d
Adding several changes to make some tests pass
darleybarreto 7c529af
Fix build on master
youknowone fe01b1a
hide ctypes test
youknowone 25e0403
clean up ctypes::array
youknowone efabe9b
skeleton PyCSimpleType
youknowone b79477a
submodule extension for ctypes::dll
youknowone 2904015
Merge pull request #4 from youknowone/dll-submodule
darleybarreto 66a91cb
Add all tests from CPython and PyPy
darleybarreto 577c5ae
Add suggestions and bump dependencies
darleybarreto 6eb0794
Starting to add Metas to primitive and array ctypes
darleybarreto 5916218
Fixing some terribly wrong impls and bugs.
darleybarreto c2ba116
Fix compilation
darleybarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Minor fixes to get compiling on master
- Loading branch information
commit b88e544d22626d7bbaab5b7199d9b2153362ab63
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,7 @@ use crate::common::lock::{ | |
| }; | ||
| use crate::function::OptionalArg; | ||
| use crate::pyobject::{ | ||
| BorrowValue, PyObjectRc, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, | ||
| TypeProtocol, | ||
| BorrowValue, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol, | ||
| }; | ||
| use crate::slots::BufferProtocol; | ||
| use crate::VirtualMachine; | ||
|
|
@@ -34,7 +33,7 @@ fn at_address(cls: &PyTypeRef, buf: usize, vm: &VirtualMachine) -> PyResult<Vec< | |
| Ok(b) if !b => { | ||
| let len = vm | ||
| .get_attribute(cls.as_object().to_owned(), "_length_") | ||
| .map_or(Ok(1), |o: PyObjectRc| { | ||
| .map_or(Ok(1), |o: PyObjectRef| { | ||
| match i64::try_from_object(vm, o.clone()) { | ||
| Ok(v_int) => { | ||
| if v_int < 0 { | ||
|
|
@@ -233,36 +232,37 @@ impl<'a> BorrowValue<'a> for PyCData { | |
|
|
||
| impl BufferProtocol for PyCData { | ||
| fn get_buffer(zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>> { | ||
| Ok(Box::new(zelf.clone())) | ||
| Ok(Box::new(PyCDataBuffer { | ||
| data: zelf.clone(), | ||
| options: BufferOptions { | ||
| readonly: false, | ||
| len: zelf._buffer.read().len(), | ||
| ..Default::default() | ||
| }, | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct PyCDataBuffer { | ||
| data: PyCDataRef, | ||
| options: BufferOptions, | ||
| } | ||
|
|
||
| // This trait will be used by all types | ||
| impl Buffer for PyCDataRef { | ||
| impl Buffer for PyCDataBuffer { | ||
| fn obj_bytes(&self) -> BorrowedValue<[u8]> { | ||
| PyRwLockReadGuard::map(self.borrow_value(), |x| x.as_slice()).into() | ||
| PyRwLockReadGuard::map(self.data.borrow_value(), |x| x.as_slice()).into() | ||
| } | ||
|
|
||
| fn obj_bytes_mut(&self) -> BorrowedValueMut<[u8]> { | ||
| PyRwLockWriteGuard::map(self.borrow_value_mut(), |x| x.as_mut_slice()).into() | ||
| PyRwLockWriteGuard::map(self.data.borrow_value_mut(), |x| x.as_mut_slice()).into() | ||
| } | ||
|
|
||
| fn release(&self) {} | ||
|
|
||
| fn get_options(&self) -> BorrowedValue<BufferOptions> { | ||
| let guard = self.buffer_options.upgradable_read(); | ||
| let guard = if guard.is_none() { | ||
| let mut w = PyRwLockUpgradableReadGuard::upgrade(guard); | ||
| *w = Some(Box::new(BufferOptions { | ||
| readonly: false, | ||
| len: self._buffer.read().len(), | ||
| ..Default::default() | ||
| })); | ||
| PyRwLockWriteGuard::downgrade(w) | ||
| } else { | ||
| PyRwLockUpgradableReadGuard::downgrade(guard) | ||
| }; | ||
| PyRwLockReadGuard::map(guard, |x| x.as_ref().unwrap().as_ref()).into() | ||
| fn get_options(&self) -> &BufferOptions { | ||
| &self.options | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -271,9 +271,8 @@ impl Buffer for PyCDataRef { | |
| // PyCArray_Type, PyCSimple_Type, PyCFuncPtr_Type | ||
| #[pyclass(module = "_ctypes", name = "_CData")] | ||
| pub struct PyCData { | ||
| _objects: AtomicCell<Vec<PyObjectRc>>, | ||
| _objects: AtomicCell<Vec<PyObjectRef>>, | ||
| _buffer: PyRwLock<Vec<u8>>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the buffer is supposed to be a raw pointer and a length, so that you can mutate the original data it if you want to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like struct RawBuffer{
inner: *mut u8,
size: usize
}? |
||
| buffer_options: PyRwLock<Option<Box<BufferOptions>>>, | ||
| } | ||
|
|
||
| pub type PyCDataRef = PyRef<PyCData>; | ||
|
|
@@ -291,11 +290,10 @@ impl PyValue for PyCData { | |
| } | ||
|
|
||
| impl PyCData { | ||
| fn new(objs: Option<Vec<PyObjectRc>>, buffer: Option<Vec<u8>>) -> Self { | ||
| fn new(objs: Option<Vec<PyObjectRef>>, buffer: Option<Vec<u8>>) -> Self { | ||
| PyCData { | ||
| _objects: AtomicCell::new(objs.unwrap_or_default()), | ||
| _buffer: PyRwLock::new(buffer.unwrap_or_default()), | ||
| buffer_options: PyRwLock::new(None), | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.