Importing modules in embedded Python

M

Marcin Krol

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.ImportError:
'/usr/lib/python2.4/lib-dynload/timemodule.so: undefined symbol:
PyModule_AddObject' 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("Interrupted..\n");
exit(sig);
}

void terminaterun(int sig)
{
Py_Finalize();
printf("Received SIGTERM. Terminating.\n");
exit(sig);
}

int main(int argc, char **argv)
{
Py_Initialize();
PyObject *pyCode, *mainmodule, *maindict;
mainmodule = PyImport_AddModule("__main__");
maindict = PyModule_GetDict(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_ReadObjectFromString((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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top