[issue15903] Make rawiobase_read() read directly to bytes object

Richard Oudkerk report at bugs.python.org
Tue Sep 18 20:19:43 CEST 2012


Richard Oudkerk added the comment:

I am rather confused about the ownership semantics when one uses PyMemoryView_FromBuffer().

It looks as though PyMemoryView_FromBuffer() "steals" ownership of the buffer since, when the associated _PyManagedBufferObject is garbage collected, PyBuffer_Release() is called on its copy of the buffer info.  However, the _PyManagedBufferObject does not own a reference of the base object, so one still needs to decref the base object (at some time when it is safe to do so).

So am I right in thinking that

  PyObject_GetBuffer(obj, &buf, ...);
  view = PyMemoryView_FromBuffer(&buf);     // view->master owns the buffer, but view->master->obj == NULL
  ...
  Py_DECREF(view);                          // releases buffer (assuming no other exports)
  Py_XDECREF(buf.obj);

has balanced refcounting and is more or less equivalent to

  view = PyMemoryView_FromObject(obj);
  ...
  Py_DECREF(view);

The documentation is not very helpful.  It just says that calls to PyObject_GetBuffer() must be matched with calls to PyBuffer_Release().

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15903>
_______________________________________


More information about the Python-bugs-list mailing list