Ctypes implementation#2364
Conversation
|
I just pushed a commit to get stuff compiling. Excited to review this! |
Sorry, something went wrong.
|
One thing to note is that some things are directly translation from PyPy, others from CPython's. Also there's that difference I said on Gitter, regarding declaring a class like: class Custom(_SimpleCData):
pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class must define a '_type_' attributewhich here will succeed. |
Sorry, something went wrong.
coolreader18
left a comment
There was a problem hiding this comment.
Here's what I have so far; I think it would be better to use more of libffi's middle API in ctypes::function, just to avoid as much unsafe as possible for something like ctypes 🙃 . Now that the functions are copied from cpython, it should be easier to refactor into something more idiomatic.
Sorry, something went wrong.
|
What are the major blockers for this PR? I think I want to give this a try :) |
Sorry, something went wrong.
Unfortunately there are several problems with the state of things and I haven't been able to continue working on this (and will not be able to do so for a while). The main problem is how it behaves differently from CPython's and PyPy's, a meta class should be added for Struct, CSimpleType, Pointer, ... (all but CFuntion), so that you can do |
Sorry, something went wrong.
3334aa9 to
4725a80
Compare
August 6, 2021 16:10
|
@youknowone I tried to sync with master a while ago, but there was a problem and I got too busy to look at that again, so thanks for that! Are you planning into work on this? If so, I think there are some tests I ported from CPython that I altered just to see if the implementation was working-ish. To be fully compatible to CPython and PyPy, the types must have a metaclass that's not I am happy to help with discussion and pointers, but I'm afraid I don't have much time to code :/ |
Sorry, something went wrong.
|
@darleybarreto Hi, thank you for the comment. I am not planning for working on ctypes at the moment, but looking for a way to merge it as it is for future contributors - because you already worked really a lot of it - if it doesn't break things too much. How do you think about it? |
Sorry, something went wrong.
|
You mean merging as is? |
Sorry, something went wrong.
|
yes, if we can manage it not to break CI |
Sorry, something went wrong.
|
I think that we need to do a few things to help future contributions in this code before merging:
Undoubtedly class _CDataMeta(type):
...
def __mul__(self, length):
return create_array_type(self, length)
...
class SimpleType(_CDataMeta):
...
class SimpleCData(..., metaclass=SimpleType):
...I couldn't find a way to make Point |
Sorry, something went wrong.
|
Thank you for detailed explanation. The metaclass issue is recently solved. Here is the test: $ cat t.py
class _CDataMeta(type):
def __mul__(self, length):
return create_array_type(self, length)
class SimpleType(_CDataMeta):
pass
class SimpleCData(object, metaclass=SimpleType):
pass
print(type(SimpleCData))
assert type(SimpleCData) == SimpleType
$ cargo run t.py
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/rustpython t.py`
<class '__main__.SimpleType'>I will look in the code for 3 |
Sorry, something went wrong.
|
Oh, I'm sorry, I meant to do the same thing in the Rust side. CPython does it in the C side of things by manually filling the type: #define MOD_ADD_TYPE(TYPE_EXPR, TP_TYPE, TP_BASE) \
do { \
PyTypeObject *type = (TYPE_EXPR); \
Py_SET_TYPE(type, TP_TYPE); \
type->tp_base = TP_BASE; \
if (PyModule_AddType(mod, type) < 0) { \
return -1; \
} \
} while (0)
MOD_ADD_TYPE(&Simple_Type, &PyCSimpleType_Type, &PyCData_Type);Here |
Sorry, something went wrong.
|
by watching |
Sorry, something went wrong.
Let me get the CPython definitions as example MOD_ADD_TYPE(&Struct_Type, &PyCStructType_Type, &PyCData_Type);
MOD_ADD_TYPE(&Union_Type, &UnionType_Type, &PyCData_Type);
MOD_ADD_TYPE(&PyCPointer_Type, &PyCPointerType_Type, &PyCData_Type);
MOD_ADD_TYPE(&PyCArray_Type, &PyCArrayType_Type, &PyCData_Type);
MOD_ADD_TYPE(&Simple_Type, &PyCSimpleType_Type, &PyCData_Type);
MOD_ADD_TYPE(&PyCFuncPtr_Type, &PyCFuncPtrType_Type, &PyCData_Type);Here we have |
Sorry, something went wrong.
|
I'm not sure why, but I cannot comment on the following reply
The idea of |
Sorry, something went wrong.
|
What happens when the buffer is destroyed? Does it deletes only the pointer(as a reference) or also deletes the data it contains (as an owner)? If it does both, then does it need a flag to distinguish them? |
Sorry, something went wrong.
|
I forgot where these two functions are used, but based on the docs: |
Sorry, something went wrong.
|
@youknowone I basically patched all your comments, you wrote:
For this code fn obj_bytes(&self) -> BorrowedValue<[u8]> {
PyRwLockReadGuard::map(self.data.borrow_value(), |x| unsafe {
slice::from_raw_parts(x.inner, x.size)
})
.into()
}What did you meant there? |
Sorry, something went wrong.
|
There was a question asking if it does copy. The comment is an answer about it. Creating an slice object doesn't copy anything. |
Sorry, something went wrong.
|
I added a C file that should be compiled as a shared lib and bundled together with the interpreter. Its import _ctypes_test |
Sorry, something went wrong.
|
|
Sorry, something went wrong.
Adding more meta impls.
|
@youknowone I haven't figured out a nice way to implement the data = b'data'
ubyte = c_ubyte * len(data)
byteslike = ubyte.from_buffer_copy(data) # an instance of ubyte made from data by copying things
My implementation of #[pyclassmethod]
fn from_buffer_copy(
cls: PyRef<Self>,
obj: PyObjectRef,
offset: OptionalArg,
vm: &VirtualMachine,
) -> PyResult {
let buffer = try_buffer_from_object(vm, &obj)?;
let opts = buffer.get_options().clone();
let (size, offset) = Self::buffer_check(cls, opts, offset, vm); //ignore this for now
let src_buffer = buffer.obj_bytes();
let empty_instance = ... //creates a new empty instance
let dst_buffer = empty_instance.obj_bytes_mut();
dst_buffer.copy_from_slice(&src_buffer[offset..offset+size]);
Ok(empty_instance.as_object().clone())
}So the idea is simply get the bytes from |
Sorry, something went wrong.
|
I don't know well about your requirements, but I guess you can do same way as how |
Sorry, something went wrong.
|
I think this PR is (practically) not possible to rebase anymore in this state. Do you mind if we squash the commits into single commit for rebase? |
Sorry, something went wrong.
Not at all :) I'm afraid I can't contribute in the following months. I can help others eventually, tho. |
Sorry, something went wrong.
|
Is this effort still ongoing / has this ctypes implementation been abandoned for another? |
Sorry, something went wrong.
Co-authored-by: Jeong YunWon <jeong@youknowone.org> Co-authored-by: Rodrigo Oliveira <rodrigo.redcode@gmail.com> Co-authored-by: Darley Barreto <darleybarreto@gmail.com> Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com>
Co-authored-by: Jeong YunWon <jeong@youknowone.org> Co-authored-by: Rodrigo Oliveira <rodrigo.redcode@gmail.com> Co-authored-by: Darley Barreto <darleybarreto@gmail.com> Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com>
|
Hi folks, I think we should close this, it's too old for any merging attempt. |
Sorry, something went wrong.
|
This is still a best trial of ctypes now. I wouldn't close it unless we actually merge any ctypes implementation. For anyone who will try another ctypes implementation, I hope it can be gradually mergable, not to be suffered by huge size of rebase. |
Sorry, something went wrong.
|
Agreed, the rebase effort was slowed by refactors that have happened since this pr, I'm planning to start incremental work on this soon. |
Sorry, something went wrong.
|
Note that #5572 is now the tracking issue for further ctypes implementation. |
Sorry, something went wrong.
@darleybarreto and I are implementing the
ctypesmodule. This an initial implementation for review. At this stage, we are focusing on Linux platforms to in the future extend to other platforms likectypesfromcpythondoes.