summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Oudkerk <shibturn@gmail.com>2012-05-28 20:35:09 (GMT)
committerRichard Oudkerk <shibturn@gmail.com>2012-05-28 20:35:09 (GMT)
commit3e0a1eb8890c25b9d1f98bc2f4a4bbe2a0cdfdce (patch)
tree3b5e75991f7a5e61aea0f6282fec4d7c00c7cc12
parent1cfe7d9a84d5f4d8950f2b5a82be3bb25a725b62 (diff)
downloadcpython-3e0a1eb8890c25b9d1f98bc2f4a4bbe2a0cdfdce.zip
cpython-3e0a1eb8890c25b9d1f98bc2f4a4bbe2a0cdfdce.tar.gz
cpython-3e0a1eb8890c25b9d1f98bc2f4a4bbe2a0cdfdce.tar.bz2
Issue #14930: Make memoryview objects weakrefable.
-rw-r--r--Include/memoryobject.h1
-rw-r--r--Lib/test/test_memoryview.py15
-rw-r--r--Lib/test/test_sys.py2
-rw-r--r--Misc/NEWS2
-rw-r--r--Objects/memoryobject.c5
5 files changed, 23 insertions, 2 deletions
diff --git a/Include/memoryobject.h b/Include/memoryobject.h
index 4ac6f65..c2e1194 100644
--- a/Include/memoryobject.h
+++ b/Include/memoryobject.h
@@ -63,6 +63,7 @@ typedef struct {
Py_ssize_t exports; /* number of buffer re-exports */
Py_buffer view; /* private copy of the exporter's view */
char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* used for casting */
+ PyObject *weakreflist;
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
} PyMemoryViewObject;
#endif
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 8809930..5a33d035 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -336,6 +336,21 @@ class AbstractMemoryTests:
m = self._view(b)
self.assertRaises(ValueError, hash, m)
+ def test_weakref(self):
+ # Check memoryviews are weakrefable
+ for tp in self._types:
+ b = tp(self._source)
+ m = self._view(b)
+ L = []
+ def callback(wr, b=b):
+ L.append(b)
+ wr = weakref.ref(m, callback)
+ self.assertIs(wr(), m)
+ del m
+ test.support.gc_collect()
+ self.assertIs(wr(), None)
+ self.assertIs(L[0], b)
+
# Variations on source objects for the buffer: bytes-like objects, then arrays
# with itemsize > 1.
# NOTE: support for multi-dimensional objects is unimplemented.
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index e3629ff..b024d9a 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -778,7 +778,7 @@ class SizeofTest(unittest.TestCase):
check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
# memoryview
- check(memoryview(b''), size(h + 'PPiP4P2i5P3cP'))
+ check(memoryview(b''), size(h + 'PPiP4P2i5P3c2P'))
# module
check(unittest, size(h + '3P'))
# None
diff --git a/Misc/NEWS b/Misc/NEWS
index 011cdf0..10f05d8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.3.0 Alpha 4?
Core and Builtins
-----------------
+- Issue #14930: Make memoryview objects weakrefable.
+
- Issue #14775: Fix a potential quadratic dict build-up due to the garbage
collector repeatedly trying to untrack dicts.
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 67f7e01..62427d4 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -595,6 +595,7 @@ memory_alloc(int ndim)
mv->view.shape = mv->ob_array;
mv->view.strides = mv->ob_array + ndim;
mv->view.suboffsets = mv->ob_array + 2 * ndim;
+ mv->weakreflist = NULL;
_PyObject_GC_TRACK(mv);
return mv;
@@ -969,6 +970,8 @@ memory_dealloc(PyMemoryViewObject *self)
_PyObject_GC_UNTRACK(self);
(void)_memory_release(self);
Py_CLEAR(self->mbuf);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) self);
PyObject_GC_Del(self);
}
@@ -2608,7 +2611,7 @@ PyTypeObject PyMemoryView_Type = {
(traverseproc)memory_traverse, /* tp_traverse */
(inquiry)memory_clear, /* tp_clear */
memory_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
+ offsetof(PyMemoryViewObject, weakreflist),/* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
memory_methods, /* tp_methods */