per interpreter storage for C extensions

"Martin v. Löwis" martin at v.loewis.de
Fri Dec 29 19:40:18 EST 2006


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.

Regards,
Martin



More information about the Python-list mailing list