Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Importing modules in embedded Python

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mrkafk at gmail

Jul 2, 2008, 8:44 AM

Post #1 of 2 (115 views)
Permalink
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.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?

--
http://mail.python.org/mailman/listinfo/python-list


mrkafk at gmail

Jul 2, 2008, 9:01 AM

Post #2 of 2 (127 views)
Permalink
Re: Importing modules in embedded Python [In reply to]

OK I found the answer in yet another Google search, right after
I gave up and posted here - turns out in order to import dynamically
linked modules, one has to pass -E argument to the linker.


> 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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.