Embedding Python

Chris Angelico rosuav at gmail.com
Wed Jun 2 14:08:47 EDT 2021


On Thu, Jun 3, 2021 at 4:05 AM Faraaz Mohammed <faraazc at gmail.com> wrote:
>
> On Tuesday, 1 July 2008 at 21:37:49 UTC+5:30, mk wrote:
> > Carsten Haese wrote:
> > > python_code is a C string containing the raw bytes from your pyc file.
> > > Casting that to a PyObject pointer will not magically transform it into
> > > a Python code object.
> > <scratching my head> well yeah, I kind of didn't think that through..
> > A pyc file contains the following:
> > >
> > > 1) An 8 byte header containing a magic number.
> > > 2) A "marshal" serialization of the code object.
> > >
> > > So, in order to transform those contents into a code object, you need to
> > > skip the 8 byte header and an unmarshal the rest. Basically, replace the
> > > line above with something like this:
> > >
> > > codeobj = PyMarshal_ReadObjectFromString(python_code+8, size-8);
> > > mainobj = PyImport_ExecCodeModule("multiply", codeobj);
> > >
> > > where codeobj is of type (PyObject *).
> > >
> > > Once that works, add magic number checking and exception handling to taste.
> > Thanks. That's exactly what I was looking for.
>
>
> I tried exactly the same thing. but I am still hitting segmentation fault at PyImport_ExecCodeModule(). python version is 2.7.17

You're replying to something from *thirteen years ago*. A lot has
changed in that time. I'd recommend starting over, figuring out what's
going on, and preferably, using Python 3.

ChrisA


More information about the Python-list mailing list