Submodules in dynamic modules?

Martin Sjögren martin at strakt.com
Thu Jul 26 10:13:34 EDT 2001


On Thu, Jul 26, 2001 at 09:59:47AM -0400, Steven D. Majewski wrote:
> 
> 
> On Thu, 26 Jul 2001, Martin Sjgren wrote:
> 
> > Is there a standardized way to use submodules in a dynamic module written
> > in C? I have a module (foo) that should have a few submodules in it, foo
> > by itself won't contain much at all, but it's nice to group things up in
> > submodules, so I could do
> > 
> > from foo import bar
> > 
> > or
> > 
> > from foo import bar, baz
> > 
> > The way I'm doing it right now is that I've more or less copied the source
> > code from Py_InitModule4() to set things up, and then insert the module
> > object into the dictionary.
> 
> I don't know if there's a standard way and I don't know if it's better,
> but what I have done is to define the macro:
> 
> #define ADD_MODULE(x)     if (-1 == \
>     PyModule_AddObject( m, (x), PyImport_ImportModule((x))))  return NULL
> 
> 
> And in Carbonmodule.c (for MacOSX) where I have a whole bunch of these
> submodules, I have:
> 
>    m = Py_InitModule("Carbon",CarbonMethods );
> 
>    (void) initWin();
>    ADD_MODULE("Win");
>    (void) initMenu();
>    ADD_MODULE("Menu");
>    (void) initEvt();
>    ADD_MODULE("Evt");
>    (void) initDlg();
>    ADD_MODULE("Dlg");

[snip]

I'm not sure we're talking about the same thing here.  I don't want to
import other Python modules, I want to have several submodules in my one C
library file (foomodule.so on unix).  I guess I COULD have several .so
files and import them like you do here, but that would be unnecessarily
complicated!

Thanks for your input anyway. :)

Martin

-- 
Martin Sjögren
  martin at strakt.com              ICQ : 41245059
  Phone: +46 (0)31 405242        Cell: +46 (0)739 169191
  GPG key: http://www.strakt.com/~martin/gpg.html




More information about the Python-list mailing list