[Python-Dev] GIL required for _all_ Python calls?
Guido van Rossum
guido at python.org
Thu Jan 7 05:29:03 CET 2010
On Wed, Jan 6, 2010 at 7:32 PM, Benjamin Peterson <benjamin at python.org> wrote:
> 2010/1/6 John Arbash Meinel <john.arbash.meinel at gmail.com>:
> > AFAIK, the only things that don't require the GIL are macro functions,
> > like PyString_AS_STRING or PyTuple_SET_ITEM. PyErr_SetString, for
> > example, will be increfing and setting the exception state, so certainly
> > needs the GIL to be held.
> As a general rule, I would say, no Py* macros are safe without the gil
> either (the exception being Py_END_ALLOW_THREADS), since they mutate
> Python objects which must be protected.
That's keeping it on the safe side, since there are some macros like
PyString_AS_STRING() that are also safe, *if* you are owning at least
one reference to the string object.
At the same time, "no Py* macros" is not quite strong enough, since if
you called PyString_AS_STRING() before releasing the GIL but you don't
own a reference to the string object, the string might be deallocated
behind your back by another thread.
A better rule would be "you may access the memory buffer in a PyString
or PyUnicode object with the GIL released as long as you own a
reference to the string object." Everything else is out of bounds (or
not worth the bother).
--Guido van Rossum (python.org/~guido)
More information about the Python-Dev