@@ -109,8 +109,6 @@ mbuf_release(_PyManagedBufferObject *self)
109109 if (self -> flags & _Py_MANAGED_BUFFER_RELEASED )
110110 return ;
111111
112- /* NOTE: at this point self->exports can still be > 0 if this function
113- is called from mbuf_clear() to break up a reference cycle. */
114112 self -> flags |= _Py_MANAGED_BUFFER_RELEASED ;
115113
116114 /* PyBuffer_Release() decrements master->obj and sets it to NULL. */
@@ -1096,32 +1094,19 @@ PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char orde
10961094/* Inform the managed buffer that this particular memoryview will not access
10971095 the underlying buffer again. If no other memoryviews are registered with
10981096 the managed buffer, the underlying buffer is released instantly and
1099- marked as inaccessible for both the memoryview and the managed buffer.
1100-
1101- This function fails if the memoryview itself has exported buffers. */
1102- static int
1097+ marked as inaccessible for both the memoryview and the managed buffer. */
1098+ static void
11031099_memory_release (PyMemoryViewObject * self )
11041100{
1101+ assert (self -> exports == 0 );
11051102 if (self -> flags & _Py_MEMORYVIEW_RELEASED )
1106- return 0 ;
1103+ return ;
11071104
1108- if (self -> exports == 0 ) {
1109- self -> flags |= _Py_MEMORYVIEW_RELEASED ;
1110- assert (self -> mbuf -> exports > 0 );
1111- if (-- self -> mbuf -> exports == 0 )
1112- mbuf_release (self -> mbuf );
1113- return 0 ;
1105+ self -> flags |= _Py_MEMORYVIEW_RELEASED ;
1106+ assert (self -> mbuf -> exports > 0 );
1107+ if (-- self -> mbuf -> exports == 0 ) {
1108+ mbuf_release (self -> mbuf );
11141109 }
1115- if (self -> exports > 0 ) {
1116- PyErr_Format (PyExc_BufferError ,
1117- "memoryview has %zd exported buffer%s" , self -> exports ,
1118- self -> exports == 1 ? "" : "s" );
1119- return -1 ;
1120- }
1121-
1122- PyErr_SetString (PyExc_SystemError ,
1123- "_memory_release(): negative export count" );
1124- return -1 ;
11251110}
11261111
11271112/*[clinic input]
@@ -1134,9 +1119,21 @@ static PyObject *
11341119memoryview_release_impl (PyMemoryViewObject * self )
11351120/*[clinic end generated code: output=d0b7e3ba95b7fcb9 input=bc71d1d51f4a52f0]*/
11361121{
1137- if (_memory_release (self ) < 0 )
1122+ if (self -> exports == 0 ) {
1123+ _memory_release (self );
1124+ Py_RETURN_NONE ;
1125+ }
1126+
1127+ if (self -> exports > 0 ) {
1128+ PyErr_Format (PyExc_BufferError ,
1129+ "memoryview has %zd exported buffer%s" , self -> exports ,
1130+ self -> exports == 1 ? "" : "s" );
11381131 return NULL ;
1139- Py_RETURN_NONE ;
1132+ }
1133+
1134+ PyErr_SetString (PyExc_SystemError ,
1135+ "memoryview: negative export count" );
1136+ return NULL ;
11401137}
11411138
11421139static void
@@ -1145,7 +1142,7 @@ memory_dealloc(PyObject *_self)
11451142 PyMemoryViewObject * self = (PyMemoryViewObject * )_self ;
11461143 assert (self -> exports == 0 );
11471144 _PyObject_GC_UNTRACK (self );
1148- ( void ) _memory_release (self );
1145+ _memory_release (self );
11491146 Py_CLEAR (self -> mbuf );
11501147 if (self -> weakreflist != NULL )
11511148 PyObject_ClearWeakRefs ((PyObject * ) self );
@@ -1164,8 +1161,10 @@ static int
11641161memory_clear (PyObject * _self )
11651162{
11661163 PyMemoryViewObject * self = (PyMemoryViewObject * )_self ;
1167- (void )_memory_release (self );
1168- Py_CLEAR (self -> mbuf );
1164+ if (self -> exports == 0 ) {
1165+ _memory_release (self );
1166+ Py_CLEAR (self -> mbuf );
1167+ }
11691168 return 0 ;
11701169}
11711170
0 commit comments