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

Discussion in 'Python' started by mathieu gontier, Aug 31, 2004.

  1. 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
     
    mathieu gontier, Aug 31, 2004
    #1
    1. Advertising

  2. mathieu gontier <> 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
     
    Alex Martelli, Sep 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    482
    msalters
    Dec 14, 2004
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    376
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. Replies:
    7
    Views:
    560
    Alex Hunsley
    Aug 12, 2006
  4. Timothy Grant
    Replies:
    5
    Views:
    421
    Timothy Grant
    Aug 6, 2008
  5. Bob Lu
    Replies:
    0
    Views:
    127
    Bob Lu
    Jun 25, 2009
Loading...

Share This Page