Visibility of symbols between extension modules
gagsl-py2 at yahoo.com.ar
Sun Oct 21 18:43:54 CEST 2007
En Sun, 21 Oct 2007 08:21:50 -0300, Sudharshan S <sudharsh at gmail.com>
> I have been learning to write extension modules using the C API that
> python provides, and have hit a minor roadblock that is turning out to
> be major headache.
> My project essentially is organized as follows,
> | | |
> bar _foo baz
What is foo? a directory? And bar/_foo/baz, source files in C? Each one
defining a module object, or is there a single module - foo perhaps?
> _foo basically does some init stuff, nothing much. Thats where the
> problem starts. The "handler" variable which is initialized in _foo
> isn't visible to others. One solution that worked was calling the
> initializing function in each of the module's PyMODINIT_FUNC. Debugging
> through the interpreter I found out the it loads all the .so's and
> executes PyMODINIT_FUNCs. But by doing so, the routine to initialize the
> handle gets executed three times, something which i find to be sort of
Ah, ok, after rereading it I see you have three modules.
> The definition of the variable is included in a header and the sources
> of the other modules include this master header, So I do have that
> variable in scope, but its not initialized. _foo does the job of
> declaring that variable.
Is it a global C variable, or a module attribute? If you want it to be
available to other Python code, it should be a module attribute. In this
case, you retrieve the value using PyObject_GetAttrString as with any
If it's a global C variable, once it's assigned it should be visible to
all (but decades of warning against using global variables can't be
The single most common error using the Python API is getting wrong the
reference counts. If you lose a reference, your object may be garbage
collected. If you leak a reference, it will never be freed. Maybe this is
what happens here.
> I did my share of RTFM and found CObjects as a potential alternative.
> But I feel its use rather complicated for a single variable that goes
> out of scope. Is there any other way to solve this, or is my approach
> itself borked =(?, What am I missing?
Perhaps if you explain a bit more what you want to do someone can give
More information about the Python-list