File tree Expand file tree Collapse file tree 5 files changed +23
-2
lines changed
Expand file tree Collapse file tree 5 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ typedef struct {
6363 Py_ssize_t exports ; /* number of buffer re-exports */
6464 Py_buffer view ; /* private copy of the exporter's view */
6565 char format [_Py_MEMORYVIEW_MAX_FORMAT ]; /* used for casting */
66+ PyObject * weakreflist ;
6667 Py_ssize_t ob_array [1 ]; /* shape, strides, suboffsets */
6768} PyMemoryViewObject ;
6869#endif
Original file line number Diff line number Diff line change @@ -336,6 +336,21 @@ def test_hash_writable(self):
336336 m = self ._view (b )
337337 self .assertRaises (ValueError , hash , m )
338338
339+ def test_weakref (self ):
340+ # Check memoryviews are weakrefable
341+ for tp in self ._types :
342+ b = tp (self ._source )
343+ m = self ._view (b )
344+ L = []
345+ def callback (wr , b = b ):
346+ L .append (b )
347+ wr = weakref .ref (m , callback )
348+ self .assertIs (wr (), m )
349+ del m
350+ test .support .gc_collect ()
351+ self .assertIs (wr (), None )
352+ self .assertIs (L [0 ], b )
353+
339354# Variations on source objects for the buffer: bytes-like objects, then arrays
340355# with itemsize > 1.
341356# NOTE: support for multi-dimensional objects is unimplemented.
Original file line number Diff line number Diff line change @@ -778,7 +778,7 @@ def get_gen(): yield 1
778778 check (int (PyLong_BASE ** 2 - 1 ), size (vh ) + 2 * self .longdigit )
779779 check (int (PyLong_BASE ** 2 ), size (vh ) + 3 * self .longdigit )
780780 # memoryview
781- check (memoryview (b'' ), size (h + 'PPiP4P2i5P3cP ' ))
781+ check (memoryview (b'' ), size (h + 'PPiP4P2i5P3c2P ' ))
782782 # module
783783 check (unittest , size (h + '3P' ))
784784 # None
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ What's New in Python 3.3.0 Alpha 4?
1010Core and Builtins
1111-----------------
1212
13+ - Issue #14930: Make memoryview objects weakrefable.
14+
1315- Issue #14775: Fix a potential quadratic dict build-up due to the garbage
1416 collector repeatedly trying to untrack dicts.
1517
Original file line number Diff line number Diff line change @@ -595,6 +595,7 @@ memory_alloc(int ndim)
595595 mv -> view .shape = mv -> ob_array ;
596596 mv -> view .strides = mv -> ob_array + ndim ;
597597 mv -> view .suboffsets = mv -> ob_array + 2 * ndim ;
598+ mv -> weakreflist = NULL ;
598599
599600 _PyObject_GC_TRACK (mv );
600601 return mv ;
@@ -969,6 +970,8 @@ memory_dealloc(PyMemoryViewObject *self)
969970 _PyObject_GC_UNTRACK (self );
970971 (void )_memory_release (self );
971972 Py_CLEAR (self -> mbuf );
973+ if (self -> weakreflist != NULL )
974+ PyObject_ClearWeakRefs ((PyObject * ) self );
972975 PyObject_GC_Del (self );
973976}
974977
@@ -2608,7 +2611,7 @@ PyTypeObject PyMemoryView_Type = {
26082611 (traverseproc )memory_traverse , /* tp_traverse */
26092612 (inquiry )memory_clear , /* tp_clear */
26102613 memory_richcompare , /* tp_richcompare */
2611- 0 , /* tp_weaklistoffset */
2614+ offsetof( PyMemoryViewObject , weakreflist ), /* tp_weaklistoffset */
26122615 0 , /* tp_iter */
26132616 0 , /* tp_iternext */
26142617 memory_methods , /* tp_methods */
You can’t perform that action at this time.
0 commit comments