Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PYTHON API : add new classes in a module from an other module

Reply
Thread Tools

PYTHON API : add new classes in a module from an other module

 
 
mathieu gontier
Guest
Posts: n/a
 
      08-31-2004
Hi every body

I am tring to create new Python modules.

In a first time, I have created a module named FOO in which I have
inserted new classes. No problem :

PyMODINIT_FUNC initFOO( void )
{
PyObject* module = Py_InitModule3( "FOO", 0, "foo module" ) ;
if ( ! module ) return ;

if ( PyType_Ready( &my_type) < 0 ) return ;
Py_INCREF( &my_type );
PyModule_AddObject( module, "my_pytype", (PyObject*) &my_type ) ;
}

In a second time, I would like create a new module named BAR. In the
'init' function of this module, Ii would like add new classes in FOO.
So, I would like white something like the following :

PyMODINIT_FUNC initBAR( void )
{
foo_module = Py_GetModule( "FOO" ) ;
if( ! foo_module ) return ;

if ( PyType_Ready( &my_addtype) < 0 ) return ;
Py_INCREF( &my_addtype );
PyModule_AddObject( foo_module, "my_pyaddtype", (PyObject*)
&my_addtype ) ;
}

So, my question is : does it exist in the Python API, a equivalent
function of "Py_GetModule( <module name> )" (that a have invented for
the example !) ;
or does an other solution exist in order to do the same think ?

Thanks,
Mathieu

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      09-01-2004
mathieu gontier <(E-Mail Removed)> wrote:
...
> foo_module = Py_GetModule( "FOO" ) ;
> if( ! foo_module ) return ;

...
> So, my question is : does it exist in the Python API, a equivalent
> function of "Py_GetModule( <module name> )" (that a have invented for
> the example !) ;
> or does an other solution exist in order to do the same think ?


There is no direct way to "get a module named this way if it is already
imported otherwise do nothing". PyImport_AddModule comes close, but it
will return an empty module (and insert it into sys.modules too...) in
the 'otherwise' case.

PyImport_GetModuleDict returns sys.module to you as a PyObject*
(borrowed reference, so no worry about needing to decref it later), and
then you can call PyDict_GetItemString on it -- the latter DOES return 0
without setting and exception if the string isn't a key in the
dictionary, so overall it may come closest to your desires even though
you do need a two-calls sequence.

Summarizing, then:

{
PyObject* sys_modules = PyImport_GetModuleDict();
PyObject* foo_module = PyDict_GetItemString(sys_modules, "FOO");
if(!foo_module) return;

/* use foo_module freely here, do not decref it when you're done */

}


Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
no module api file after generating ruby core api Bob Lu Ruby 0 06-25-2009 03:07 AM
Re: Finding IP address of localhost via socket API (or other API) Timothy Grant Python 5 08-06-2008 03:35 AM
Passing Classes as Arguments to Functions of Other Classes crouse@physics.wm.edu Java 7 08-12-2006 02:08 AM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
Nested classes within other classes Tony Johansson C++ 2 12-14-2004 11:41 AM



Advertisments