Global variables in a C extension for Python

Lorenzo Di Gregorio lorenzo.digregorio at gmail.com
Wed Dec 28 11:18:10 EST 2011


Hello,

I've written a C extension for Python which works so far, but now I've
stumbled onto a simple problem for which I just can't find any example
on the web, so here I am crying for help ;-)

I'll trying to reduce the problem to a minimal example.  Let's say I
need to call from Python functions of a C program like:

static int counter = 0;
void do_something(...) {
... counter++; ...
}
void do_something_else(...) {
... counter++; ...
}

So they access a common global variable.  I've written the wrappers
for the functions, but I'd like to place "counter" in the module's
space and have wrappers accessing it like self->counter.  I do not
need to make "counter" visible to Python, I just need the global
static variable available for C.

I've got somehow a clue of how this should work, but not much more
than a clue, and I'd appreciate to see a simple example.

Best Regards,
Lorenzo



More information about the Python-list mailing list