-
Notifications
You must be signed in to change notification settings - Fork 1.3k
_ctypes pt. 4 #5582
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
Merged
Merged
_ctypes pt. 4 #5582
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ff55d52
correct error type when symbol is not found
arihant2math cd1c9e7
restype get/set
arihant2math 47ed34b
tmp
arihant2math ab8aa0c
formatting
arihant2math 818ad39
base of PyCSimple for PyCArray
arihant2math b47bbc3
more tmp
arihant2math bd5d95a
PyCSimple::to_arg
arihant2math af7083c
more snippet tests
arihant2math 6225998
par down ctypes
arihant2math 459699c
nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
arihant2math 503aa31
arguments for ctypes function
arihant2math d9d7475
formatting
arihant2math 79282e5
cleanup
arihant2math b0a87f9
cleanup
arihant2math 3edb1cf
formatting
arihant2math aa2fa5f
clippy
arihant2math 7c19781
make ctypes non-importable and fix extra dbg!
arihant2math 28c5b91
fix compile
arihant2math f5251a5
formatting
arihant2math f0dee6d
fix snippet
arihant2math 7405f28
force failure to import ctypes
arihant2math 86e778e
Update Lib/ctypes/__init__.py
arihant2math 71066c9
Replace comment marker
youknowone 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,107 @@ | ||
#[pyclass(name = "Array", module = "_ctypes")] | ||
pub struct PyCArray {} | ||
use crate::builtins::PyBytes; | ||
use crate::types::Callable; | ||
use crate::{Py, PyObjectRef, PyPayload}; | ||
use crate::{PyResult, VirtualMachine, builtins::PyTypeRef, types::Constructor}; | ||
use crossbeam_utils::atomic::AtomicCell; | ||
use rustpython_common::lock::PyRwLock; | ||
use rustpython_vm::stdlib::ctypes::base::PyCSimple; | ||
|
||
#[pyclass(flags(BASETYPE, IMMUTABLETYPE))] | ||
impl PyCArray {} | ||
// TODO: make it metaclass | ||
#[pyclass(name = "ArrayType", module = "_ctypes")] | ||
#[derive(PyPayload)] | ||
pub struct PyCArrayType { | ||
pub(super) inner: PyCArray, | ||
} | ||
|
||
impl std::fmt::Debug for PyCArrayType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("PyCArrayType") | ||
.field("inner", &self.inner) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Callable for PyCArrayType { | ||
type Args = (); | ||
fn call(zelf: &Py<Self>, _args: Self::Args, vm: &VirtualMachine) -> PyResult { | ||
Ok(PyCArray { | ||
typ: PyRwLock::new(zelf.inner.typ.read().clone()), | ||
length: AtomicCell::new(zelf.inner.length.load()), | ||
value: PyRwLock::new(zelf.inner.value.read().clone()), | ||
} | ||
.into_pyobject(vm)) | ||
} | ||
} | ||
|
||
impl Constructor for PyCArrayType { | ||
type Args = PyObjectRef; | ||
|
||
fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult { | ||
unreachable!() | ||
} | ||
} | ||
|
||
#[pyclass(flags(IMMUTABLETYPE), with(Callable, Constructor))] | ||
impl PyCArrayType {} | ||
|
||
#[pyclass(name = "Array", base = "PyCSimple", module = "_ctypes")] | ||
#[derive(PyPayload)] | ||
pub struct PyCArray { | ||
pub(super) typ: PyRwLock<PyTypeRef>, | ||
pub(super) length: AtomicCell<usize>, | ||
pub(super) value: PyRwLock<PyObjectRef>, | ||
} | ||
|
||
impl std::fmt::Debug for PyCArray { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("PyCArray") | ||
.field("typ", &self.typ) | ||
.field("length", &self.length) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Constructor for PyCArray { | ||
type Args = (PyTypeRef, usize); | ||
|
||
fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult { | ||
Ok(Self { | ||
typ: PyRwLock::new(args.0), | ||
length: AtomicCell::new(args.1), | ||
value: PyRwLock::new(vm.ctx.none()), | ||
} | ||
.into_pyobject(vm)) | ||
} | ||
} | ||
|
||
#[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor))] | ||
impl PyCArray { | ||
#[pygetset(name = "_type_")] | ||
fn typ(&self) -> PyTypeRef { | ||
self.typ.read().clone() | ||
} | ||
|
||
#[pygetset(name = "_length_")] | ||
fn length(&self) -> usize { | ||
self.length.load() | ||
} | ||
|
||
#[pygetset] | ||
fn value(&self) -> PyObjectRef { | ||
self.value.read().clone() | ||
} | ||
|
||
#[pygetset(setter)] | ||
fn set_value(&self, value: PyObjectRef) { | ||
*self.value.write() = value; | ||
} | ||
} | ||
|
||
impl PyCArray { | ||
pub fn to_arg(&self, _vm: &VirtualMachine) -> PyResult<libffi::middle::Arg> { | ||
let value = self.value.read(); | ||
let py_bytes = value.payload::<PyBytes>().unwrap(); | ||
let bytes = py_bytes.as_ref().to_vec(); | ||
Ok(libffi::middle::Arg::new(&bytes)) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.