Closed
Description
This works with LPython:
def mystruct_set(mystruct: CPtr, k: i16) -> None:
b: Pointer[mystruct]
c_p_pointer(mystruct, b)
b.k = k
But not in CPython mode, because "b" is not declared in c_p_pointer(mystruct, b)
:
UnboundLocalError: local variable 'b' referenced before assignment
Also the implementation of c_p_pointer
makes no sense:
def c_p_pointer(cptr, targettype):
return pointer(targettype)
As it needs to use the cptr
pointer. Rather, the implementation must look something like this:
def mystruct_set(mystruct: CPtr, k: i16) -> None:
struct_type = ctypes.POINTER(mystruct_type)
newa = ctypes.cast(mystruct, struct_type)
newa.contents.k = k