TypeError: cannot create weak reference to 'weakref' object
I am trying to get psycopg[3] working on PyPy -- it mostly works if I comment out this line in PyPy https://foss.heptapod.net/pypy/pypy/-/blob/branch/default/lib_pypy/_ctypes/primitive.py#L54
Essentially there is a discrepancy in ctypes module behavior as compared to CPython, below is a minimal reproducible case that works fine on CPython but fails on PyPy
from ctypes import py_object
from weakref import ref
class Empty():
pass
e = Empty()
r = ref(e)
pr = py_object(r)
Here are the logs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/om26er/Downloads/pypy3.9-v7.3.9-linux64/lib/pypy3.9/_ctypes/primitive.py", line 409, in __init__
self.value = value
File "/home/om26er/Downloads/pypy3.9-v7.3.9-linux64/lib/pypy3.9/_ctypes/primitive.py", line 265, in _setvalue
num = pyobj_container.add(val)
File "/home/om26er/Downloads/pypy3.9-v7.3.9-linux64/lib/pypy3.9/_ctypes/primitive.py", line 54, in add
self.objs.append(weakref.ref(obj))
TypeError: cannot create weak reference to 'weakref' object