Skip to content

Conversation

@czgdp1807
Copy link
Collaborator

Closes #1798

targettype_ptr = convert_type_to_ctype(targettype)
if isinstance(targettype, Array):
if py_is_dataclass(targettype._type):
return ctypes.cast(cptr.value, ctypes.py_object).value
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cptr.value is nothing but the address of foos: Foo[1] = empty(1, dtype=Foo). And ctypes.cast(cptr.value, ctypes.py_object).value just gives the original numpy array back, so effectively it becomes a pointer and you can easily make changes to it in other functions.

That's the only way forward if we want to keep using numpy arrays for arrays of dataclasses. If we can avoid using numpy arrays for dataclasses then we can define our own empty in lpython.py or in our own version of numpy.py. In our empty we can just create a ctypes.Structure array and return it via empty. For all the types like i32, etc we can keep calling numpy.empty. But I would not go for it unless extremely necessary.

@certik certik merged commit 1aad65e into lcompilers:main May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

casting between struct array and CPtr causes lpython to segfault despite generating valid C code

2 participants