per interpreter storage for C extensions

Robin Becker robin at NOSPAMreportlab.com
Sat Dec 30 07:47:32 EST 2006


Martin v. Löwis wrote:
> Robin Becker schrieb:
>> Is there a simple/cheap way for C code to cache these sorts of module
>> level globals on a per interpreter basis? Is there even a way to tell
>> which interpreter I'm being called in?
> 
> There is no cheap way to add to the interpreter state. As Chris Mellon
> explains, you can use PyThreadState_Get()->interp to find the
> interpreter state. I recommend to do the caching only in the
> single-interpreter case (i.e. for the first interpreter).
> 
> Notice that comparing interpreter states by identity is somewhat
> dangerous: the interpreter may have been deleted with you still
> holding a pointer to it, and a new interpreter may get allocated
> at the same address, making you believe it is the same interpreter.
....
I can eliminate most of the problems by not keeping private pointers to 
the cache variables.

What is worrying is that in the extension init we're creating an 
exception and version string etc and holding a pointer to them in C; is 
it safe to use the same exception in different interpeters? Currently 
the exception is added to the module at init time, but my reading of the 
documentation implies that  all interpreters will see that exception as 
it is copied from the first.
-- 
Robin Becker



More information about the Python-list mailing list