Importing modules in embedded Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin Krol

    Importing modules in embedded Python

    Hello everyone,

    I can embed Python interpreter in C code now, but now there's another
    problem, importing modules in Python code doesn't work:

    Exception exceptions.Impo rtError:
    '/usr/lib/python2.4/lib-dynload/timemodule.so: undefined symbol:
    PyModule_AddObj ect' in 'garbage collection' ignored
    Fatal Python error: unexpected exception during garbage collection
    Aborted

    The (autogenerated) C code:
    ---cut---
    #include <Python.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <syslog.h>
    #include <unistd.h>
    #include <signal.h>

    void userbreak(int sig)
    {
    Py_Finalize();
    printf("Interru pted..\n");
    exit(sig);
    }

    void terminaterun(in t sig)
    {
    Py_Finalize();
    printf("Receive d SIGTERM. Terminating.\n" );
    exit(sig);
    }

    int main(int argc, char **argv)
    {
    Py_Initialize() ;
    PyObject *pyCode, *mainmodule, *maindict;
    mainmodule = PyImport_AddMod ule("__main__") ;
    maindict = PyModule_GetDic t(mainmodule);
    unsigned int size = 272;

    unsigned char python_code[] = { 0x63,0x00,0x00,
    0x00,0x00,0x00, 0x00,0x00,0x00, 0x01,0x00,0x00, 0x00,
    0x40,0x00,0x00, 0x00,0x73,0x16, 0x00,0x00,0x00, 0x64,
    0x00,0x00,0x6B, 0x00,0x00,0x5A, 0x00,0x00,0x64, 0x01,
    0x00,0x84,0x00, 0x00,0x5A,0x01, 0x00,0x64,0x00, 0x00,
    0x53,0x28,0x02, 0x00,0x00,0x00, 0x4E,0x63,0x00, 0x00,
    0x00,0x00,0x00, 0x00,0x00,0x00, 0x02,0x00,0x00, 0x00,
    0x43,0x00,0x00, 0x00,0x73,0x23, 0x00,0x00,0x00, 0x74,
    0x00,0x00,0x69, 0x01,0x00,0x64, 0x01,0x00,0x83, 0x01,
    0x00,0x01,0x64, 0x02,0x00,0x47, 0x48,0x74,0x00, 0x00,
    0x69,0x01,0x00, 0x64,0x03,0x00, 0x83,0x01,0x00, 0x01,
    0x64,0x00,0x00, 0x53,0x28,0x04, 0x00,0x00,0x00, 0x4E,
    0x69,0x01,0x00, 0x00,0x00,0x74, 0x07,0x00,0x00, 0x00,
    0x73,0x75,0x63, 0x63,0x65,0x73, 0x73,0x69,0x5A, 0x00,
    0x00,0x00,0x28, 0x02,0x00,0x00, 0x00,0x74,0x04, 0x00,
    0x00,0x00,0x74, 0x69,0x6D,0x65, 0x74,0x05,0x00, 0x00,
    0x00,0x73,0x6C, 0x65,0x65,0x70, 0x28,0x00,0x00, 0x00,
    0x00,0x28,0x00, 0x00,0x00,0x00, 0x28,0x00,0x00, 0x00,
    0x00,0x74,0x07, 0x00,0x00,0x00, 0x74,0x65,0x73, 0x74,
    0x2E,0x70,0x79, 0x74,0x04,0x00, 0x00,0x00,0x74, 0x65,
    0x73,0x74,0x04, 0x00,0x00,0x00, 0x73,0x06,0x00, 0x00,
    0x00,0x00,0x01, 0x0D,0x01,0x05, 0x01,0x28,0x02, 0x00,
    0x00,0x00,0x52, 0x01,0x00,0x00, 0x00,0x52,0x04, 0x00,
    0x00,0x00,0x28, 0x02,0x00,0x00, 0x00,0x52,0x04, 0x00,
    0x00,0x00,0x52, 0x01,0x00,0x00, 0x00,0x28,0x00, 0x00,
    0x00,0x00,0x28, 0x00,0x00,0x00, 0x00,0x52,0x03, 0x00,
    0x00,0x00,0x74, 0x01,0x00,0x00, 0x00,0x3F,0x02, 0x00,
    0x00,0x00,0x73, 0x02,0x00,0x00, 0x00,0x09,0x02 };

    pyCode = PyMarshal_ReadO bjectFromString ((PyObject *)
    python_code, size);

    (void) signal(SIGINT, userbreak);
    (void) signal(SIGTERM, terminaterun);
    PyEval_EvalCode (pyCode, maindict, maindict);

    Py_Finalize();

    }
    ---cut---

    The Python code:

    import time

    def test():
    time.sleep(1)
    print "success"
    time.sleep(90)


    How do I manage to get modules imported? Anybody?

Working...