aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2023-01-01 20:16:39 +0200
committerMatti Picus <matti.picus@gmail.com>2023-01-01 20:16:39 +0200
commit80d6cdf39ed670705f2b75fdae04cf24372d6da0 (patch)
tree47ad285320e0d8dd4fb6df0444b2dc3b79516f1d
parentadd v7.3.11 releases to version.json and update release note (diff)
downloadpypy-80d6cdf39ed670705f2b75fdae04cf24372d6da0.tar.gz
pypy-80d6cdf39ed670705f2b75fdae04cf24372d6da0.tar.bz2
pypy-80d6cdf39ed670705f2b75fdae04cf24372d6da0.zip
allow creating ctypes.py_object() on a weakref (issue 3883)
-rw-r--r--extra_tests/ctypes_tests/test_structures.py12
-rw-r--r--lib_pypy/_ctypes/primitive.py5
2 files changed, 16 insertions, 1 deletions
diff --git a/extra_tests/ctypes_tests/test_structures.py b/extra_tests/ctypes_tests/test_structures.py
index fa6ed900ae..65861f3a81 100644
--- a/extra_tests/ctypes_tests/test_structures.py
+++ b/extra_tests/ctypes_tests/test_structures.py
@@ -2,6 +2,7 @@ from ctypes import *
import pytest
import sys
+import weakref
def test_subclass_initializer():
@@ -214,3 +215,14 @@ def test_memoryview():
assert mv.format == 'T{>h:a:>h:b:}'
assert mv.shape == (2, 3)
assert mv.itemsize == 4
+
+def test_weakref():
+ # issue 3883: py_object of weakref
+ class Empty():
+ pass
+
+ e = Empty()
+ r = weakref.ref(e)
+ pr = py_object(r)
+ assert pr.value is e
+
diff --git a/lib_pypy/_ctypes/primitive.py b/lib_pypy/_ctypes/primitive.py
index a4d6086104..b1136996ef 100644
--- a/lib_pypy/_ctypes/primitive.py
+++ b/lib_pypy/_ctypes/primitive.py
@@ -51,7 +51,10 @@ class GlobalPyobjContainer(object):
def add(self, obj):
num = len(self.objs)
- self.objs.append(weakref.ref(obj))
+ if isinstance(obj, weakref.ReferenceType):
+ self.objs.append(obj)
+ else:
+ self.objs.append(weakref.ref(obj))
return num
def get(self, num):