I want to use a pointer to change the value of a python object, here is what I tried:
This works:
def main():
cdef int i
cdef int* iptr
i = 5
iptr = &i
iptr[0] = 1
print(i) # this works, prints 1
and this:
cdef class A:
cdef int i
def __init__(self, i): self.i = i
def main():
cdef int* ptr
cdef A a
a = A(5)
ptr = &a.i
ptr[0] = 1
print(a.i) # this works too
But when I try to use similar code with objects, I can't get it to work:
from cpython cimport PyObject
cdef class A:
cdef object o
def __init__(self, o): self.o = o
def main():
cdef PyObject* ptr
cdef A a
a = A(list())
# I also tried with `<void*>a.o`, `<PyObject*>a` and `<void*>a`
ptr = <PyObject*>a.o
# this segfaults
# ptr[0] = dict()
# print(a.o)
# this doesnt compile: Cannot convert Python object to 'PyObject'
# ptr[0] = None
# print(a.o)
# this doesnt compile: incompatible types when assigning to type ‘PyObject’ from type ‘struct PyObject *’
# ptr[0] = <PyObject>None
# print(a.o)
# this doesnt compile: Cannot assign type 'PyObject *' to 'PyObject'
# ptr[0] = <PyObject*>None
# print(a.o)