Hi <br><br>If PyImport_ExecCodeModule have a two parameteres and second parameter is PyObject type  why you are make a call with a unsigned char value ?<br><br>If you search this function name into Google you will can found some examples, use for example PyMarshal_ReadObjectFromString or PyMarshal_ReadObjectFromFile<br>
<br>Bye<br><br><div class="gmail_quote">On Tue, Jul 1, 2008 at 11:36 AM, Marcin Krol <<a href="mailto:mrkafk@gmail.com">mrkafk@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello everyone,<br>
<br>
I'm trying to embed Python interpreter in C code, but in a specific way:<br>
loading compiled bytecode into a memory location and executing it (don't<br>
ask why, complicated reasons).<br>
<br>
PyImport_ExecCodeModule seems like obvious candidate, docs say:<br>
<br>
"Given a module name (possibly of the form package.module) and a code<br>
object read from a Python bytecode file or obtained from the built-in<br>
function compile(), load the module."<br>
<br>
Code:<br>
<br>
---cut---<br>
#include <Python.h><br>
#include <stdio.h><br>
#include <stdlib.h><br>
#include <syslog.h><br>
#include <unistd.h><br>
<br>
<br>
int load_file(char *fname, unsigned char** result)<br>
{<br>
 int size = 0;<br>
 FILE *f = fopen(fname, "rb");<br>
 if (f == NULL)<br>
 {<br>
  *result = NULL;<br>
 return -1;<br>
 }<br>
 fseek(f, 0, SEEK_END);<br>
 size = ftell(f);<br>
 *result = (unsigned char *) malloc(size+1);<br>
 fseek(f, 0, SEEK_SET);<br>
 size = fread(*result, sizeof(unsigned char), size, f);<br>
 return size;<br>
}<br>
<br>
int main(int argc, char **argv)<br>
{<br>
        int size;<br>
        unsigned char *python_code;<br>
        PyObject *mainobj;<br>
        size = load_file("multiply.pyc", &python_code);<br>
<br>
        Py_Initialize();<br>
        mainobj = PyImport_ExecCodeModule("multiply", (PyObject *)<br>
python_code);<br>
        Py_Finalize();<br>
<br>
}<br>
---cut---<br>
<br>
Compiling it following way works fine:<br>
<br>
${CC} testit.c -g -o testit -I/usr/include/python2.4 -lpython2.4 -lm<br>
-lutil -lpthread -ldl  -L/usr/lib/python2.4/config<br>
<br>
<br>
However, the damn thing crashes on this call:<br>
<br>
33              mainobj = PyImport_ExecCodeModule("multiply", (PyObject<br>
*) python_code);<br>
(gdb) n<br>
<br>
Program received signal SIGSEGV, Segmentation fault.<br>
0x0804e7f6 in PyImport_ExecCodeModuleEx ()<br>
<br>
The .pyc file woks just fine in Python interpreter:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
import multiply<br>
multiply.multiply()<br>
</blockquote></blockquote></blockquote>
The result of 12345 x 6789 : 83810205<br>
83810205<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
</blockquote></blockquote></blockquote>
<br>
<br>
What I am doing wrong? Please help.<br><font color="#888888">
<br>
<br>
<br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Pau Freixes<br>Linux GNU/User