Import/Create module from buffer
Gruik
benjamin.fremiot at gmail.com
Mon May 12 07:09:45 EDT 2008
On May 12, 12:31 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 12 May 2008 05:49:22 -0300, Gruik <benjamin.frem... at gmail.com> escribió:
>
> > I'm currently working on python embedding with C++. My goal is that
> > the C++ part handle files writing/reading so that the Python part only
> > works with buffers.
>
> > I succeeded in buffer exchanges. The problem is that some of the files
> > I read/write are python files so that, before embedding, I imported
> > them as modules when I needed them.
>
> > But now that the Python part only receive buffers, I can't do it
> > anymore. So I wonder :
> > - is it possible to import module from a buffer instead of files?
> > - or is it possible to create a module object from my buffer?
>
> Yes, first compile the buffer to get a code object, then use PyImport_ExecCodeModule. See how this function is used in import.c
>
> --
> Gabriel Genellina
Thanks for your quick answer !
I think I'll have no problem with that in C++ and I'm going to try it
right after this message.
But before that 1 question: what if I'm in Python ?
Following your solution, I did that in Python :
def load_buffer(buffer) :
compiled_buffer = compile(buffer, "module_name", "exec")
exec(compiled_buffer)
It works great except that I can't have a module object and that it is
as if I did "from module import *"
But I need the module object and not an "import *" behavior.
Any idea about the way to do that?
Benjamin
More information about the Python-list
mailing list