[Python-Dev] Const access to CPython objects outside of GIL?

Tim Peters tim.peters at gmail.com
Tue Jul 17 11:59:02 EDT 2018


[Tim]> There is no intention to support GIL-free access to any Python
objects.  So

> > that's the contract:  "All warranties are null & void if you do just
> about
> > anything while not holding the GIL".
>

[Antoine]

> Actually, accessing a Py_buffer that's been obtained in the regular way

(e.g. with PyObject_GetBuffer) is safe even without the GIL.
>

Same as the docs, I use "Python object" to mean a pointer to PyObject.  In
that sense, a Py_buffer is no more a "Python object" than, e.g,, is a
Py_ssize_t.

If someone wants to muck with the `obj` member of a Py_buffer struct,
_then_ they're back in "Python object" territory (`obj` is a PyObject*) and
so are on their own if they don't obtain the GIL.  Likewise all bets are
off if they don't hold the GIL when calling PyObject_GetBuffer() to begin
with, or PyBuffer_Release() when they're done.

If they want to muck with the `buf` member without the GIL, then if they
care about races among multiple threads mucking with `buf`, they'll have to
supply their own mutual exclusion mechanism.

So your "safe" comes with footnotes too, even though Py_buffer isn't itself
a PyObject*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180717/40791e1a/attachment.html>


More information about the Python-Dev mailing list