@@ -4527,6 +4527,7 @@ SWIGINTERNINLINE PyObject*
45274527
45284528
45294529static PyObject* _get_store (PyObject* py_self, bool create) {
4530+ // Return a new reference
45304531 if (!PyObject_HasAttrString (py_self, " _private_data_" )) {
45314532 if (!create)
45324533 return NULL ;
@@ -4540,45 +4541,60 @@ static PyObject* _get_store(PyObject* py_self, bool create) {
45404541 }
45414542 return PyObject_GetAttrString (py_self, " _private_data_" );
45424543};
4543- static int store_private (PyObject* py_self, const char * name,
4544- PyObject* val, bool take_ownership=false ) {
4545- int result = 0 ;
4544+ static int private_store_set (PyObject* py_self, const char * name,
4545+ PyObject* val) {
45464546 PyObject* dict = _get_store (py_self, true );
4547- if (dict) {
4548- if (val)
4549- result = PyDict_SetItemString (dict, name, val);
4550- else if (PyDict_GetItemString (dict, name))
4551- result = PyDict_DelItemString (dict, name);
4552- Py_DECREF (dict);
4553- }
4554- else
4555- result = -1 ;
4556- if (take_ownership && val)
4557- Py_DECREF (val);
4547+ if (!dict)
4548+ return -1 ;
4549+ int result = PyDict_SetItemString (dict, name, val);
4550+ Py_DECREF (dict);
45584551 return result;
45594552};
4560- static PyObject* fetch_private (PyObject* py_self, const char * name) {
4553+ static PyObject* private_store_get (PyObject* py_self, const char * name) {
4554+ // Return a borrowed reference
45614555 PyObject* dict = _get_store (py_self, false );
45624556 if (!dict)
45634557 return NULL ;
45644558 PyObject* result = PyDict_GetItemString (dict, name);
4565- if (result) {
4566- Py_INCREF (result);
4567- PyDict_DelItemString (dict, name);
4568- }
45694559 Py_DECREF (dict);
45704560 return result;
45714561};
4562+ static int private_store_del (PyObject* py_self, const char * name) {
4563+ PyObject* dict = _get_store (py_self, false );
4564+ if (!dict)
4565+ return 0 ;
4566+ if (PyDict_DelItemString (dict, name))
4567+ PyErr_Clear ();
4568+ return 0 ;
4569+ };
45724570
45734571
4574- static int release_view (PyObject* py_self) {
4575- PyObject* ref = fetch_private (py_self, " view" );
4572+ static int store_view (PyObject* py_self, PyObject* view,
4573+ PyObject* callback=NULL ) {
4574+ PyObject* views = private_store_get (py_self, " views" );
4575+ if (!views) {
4576+ views = PyList_New (0 );
4577+ private_store_set (py_self, " views" , views);
4578+ Py_DECREF (views);
4579+ }
4580+ PyObject* ref = PyWeakref_NewRef (view, callback);
45764581 if (!ref)
4577- return 0 ;
4578- PyObject* view = PyWeakref_GetObject (ref);
4579- if (PyMemoryView_Check (view))
4580- Py_XDECREF (PyObject_CallMethod (view, " release" , NULL ));
4582+ return -1 ;
4583+ int result = PyList_Append (views, ref);
45814584 Py_DECREF (ref);
4585+ return result;
4586+ };
4587+ static int release_views (PyObject* py_self) {
4588+ PyObject* views = private_store_get (py_self, " views" );
4589+ if (!views)
4590+ return 0 ;
4591+ PyObject* view = NULL ;
4592+ for (Py_ssize_t idx = PyList_Size (views); idx > 0 ; idx--) {
4593+ view = PyWeakref_GetObject (PyList_GetItem (views, idx - 1 ));
4594+ if (PyMemoryView_Check (view))
4595+ Py_XDECREF (PyObject_CallMethod (view, " release" , NULL ));
4596+ }
4597+ private_store_del (py_self, " views" );
45824598 return 0 ;
45834599};
45844600
@@ -5094,7 +5110,7 @@ SWIGINTERN PyObject *_wrap_BasicIo_close(PyObject *self, PyObject *args) {
50945110 }
50955111 resultobj = SWIG_From_int (static_cast < int >(result));
50965112
5097- release_view (self);
5113+ release_views (self);
50985114
50995115 return resultobj;
51005116fail:
@@ -5148,7 +5164,7 @@ SWIGINTERN PyObject *_wrap_BasicIo_write__SWIG_0(PyObject *self, PyObject *args)
51485164 Py_XDECREF (_global_view);
51495165
51505166
5151- release_view (self);
5167+ release_views (self);
51525168
51535169 return resultobj;
51545170fail:
@@ -5199,7 +5215,7 @@ SWIGINTERN PyObject *_wrap_BasicIo_write__SWIG_1(PyObject *self, PyObject *args)
51995215 }
52005216 resultobj = SWIG_From_long (static_cast < long >(result));
52015217
5202- release_view (self);
5218+ release_views (self);
52035219
52045220 return resultobj;
52055221fail:
@@ -5612,10 +5628,8 @@ SWIGINTERN PyObject *_wrap_BasicIo_mmap(PyObject *self, PyObject *args) {
56125628 resultobj = PyMemoryView_FromMemory ((char *)result, result ? arg1->size () : 0 , arg2 ? PyBUF_WRITE : PyBUF_READ);
56135629 if (!resultobj)
56145630 SWIG_fail;
5615- // Release any existing memoryview
5616- release_view (self);
56175631 // Store a weak ref to the new memoryview
5618- if (store_private (self, " view " , PyWeakref_NewRef ( resultobj, NULL ), true ))
5632+ if (store_view (self, resultobj))
56195633 SWIG_fail;
56205634
56215635 return resultobj;
@@ -5652,7 +5666,7 @@ SWIGINTERN PyObject *_wrap_BasicIo_munmap(PyObject *self, PyObject *args) {
56525666 }
56535667 resultobj = SWIG_From_int (static_cast < int >(result));
56545668
5655- release_view (self);
5669+ release_views (self);
56565670
56575671 return resultobj;
56585672fail:
@@ -5875,7 +5889,7 @@ SWIGINTERN PyObject *_wrap_BasicIo_data(PyObject *self, PyObject *args) {
58755889 resultobj = SWIG_NewPointerObj (SWIG_as_voidptr (result), SWIGTYPE_p_DataContext, SWIG_POINTER_OWN | 0 );
58765890
58775891 if (resultobj != Py_None)
5878- if (store_private (resultobj, " _refers_to " , self)) {
5892+ if (private_store_set (resultobj, " refers_to " , self)) {
58795893 SWIG_fail;
58805894 }
58815895
0 commit comments