Issue #14930: Make memoryview objects weakrefable.

This commit is contained in:
Richard Oudkerk 2012-05-28 21:35:09 +01:00
parent 1cfe7d9a84
commit 3e0a1eb889
5 changed files with 23 additions and 2 deletions

View File

@ -63,6 +63,7 @@ typedef struct {
Py_ssize_t exports; /* number of buffer re-exports */ Py_ssize_t exports; /* number of buffer re-exports */
Py_buffer view; /* private copy of the exporter's view */ Py_buffer view; /* private copy of the exporter's view */
char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* used for casting */ char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* used for casting */
PyObject *weakreflist;
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */ Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
} PyMemoryViewObject; } PyMemoryViewObject;
#endif #endif

View File

@ -336,6 +336,21 @@ class AbstractMemoryTests:
m = self._view(b) m = self._view(b)
self.assertRaises(ValueError, hash, m) 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 # Variations on source objects for the buffer: bytes-like objects, then arrays
# with itemsize > 1. # with itemsize > 1.
# NOTE: support for multi-dimensional objects is unimplemented. # NOTE: support for multi-dimensional objects is unimplemented.

View File

@ -778,7 +778,7 @@ class SizeofTest(unittest.TestCase):
check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit) check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit) check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
# memoryview # memoryview
check(memoryview(b''), size(h + 'PPiP4P2i5P3cP')) check(memoryview(b''), size(h + 'PPiP4P2i5P3c2P'))
# module # module
check(unittest, size(h + '3P')) check(unittest, size(h + '3P'))
# None # None

View File

@ -10,6 +10,8 @@ What's New in Python 3.3.0 Alpha 4?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #14930: Make memoryview objects weakrefable.
- Issue #14775: Fix a potential quadratic dict build-up due to the garbage - Issue #14775: Fix a potential quadratic dict build-up due to the garbage
collector repeatedly trying to untrack dicts. collector repeatedly trying to untrack dicts.

View File

@ -595,6 +595,7 @@ memory_alloc(int ndim)
mv->view.shape = mv->ob_array; mv->view.shape = mv->ob_array;
mv->view.strides = mv->ob_array + ndim; mv->view.strides = mv->ob_array + ndim;
mv->view.suboffsets = mv->ob_array + 2 * ndim; mv->view.suboffsets = mv->ob_array + 2 * ndim;
mv->weakreflist = NULL;
_PyObject_GC_TRACK(mv); _PyObject_GC_TRACK(mv);
return mv; return mv;
@ -969,6 +970,8 @@ memory_dealloc(PyMemoryViewObject *self)
_PyObject_GC_UNTRACK(self); _PyObject_GC_UNTRACK(self);
(void)_memory_release(self); (void)_memory_release(self);
Py_CLEAR(self->mbuf); Py_CLEAR(self->mbuf);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
PyObject_GC_Del(self); PyObject_GC_Del(self);
} }
@ -2608,7 +2611,7 @@ PyTypeObject PyMemoryView_Type = {
(traverseproc)memory_traverse, /* tp_traverse */ (traverseproc)memory_traverse, /* tp_traverse */
(inquiry)memory_clear, /* tp_clear */ (inquiry)memory_clear, /* tp_clear */
memory_richcompare, /* tp_richcompare */ memory_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ offsetof(PyMemoryViewObject, weakreflist),/* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
memory_methods, /* tp_methods */ memory_methods, /* tp_methods */