Loading modules from files through C++

Roland Plüss roland at rptd.ch
Fri May 16 20:27:19 EDT 2014


I'm using Python in an embedded situation. In particular I have to load
python scripts through a memory interface so regular python module
loading can not be used. I got working so far a module loader object
I've added using C++ to sys.meta_path . Now I'm totally stuck at the
finally loading step.

I've got this a C++ loader method "load_module(fullname)" which does
load the requested module script files into a null-terminated string. I
know that "load_module" has to return the module PyObject*. But I can't
get the python source in the c-string into a module PyObject*.

I've tried so far this:

#CODE#
return Py_CompileStringFlags( content.GetString(),
lookupPath.GetPathNative(), Py_file_input, NULL );
#CODE#

This did not result in a module that worked. In particular the module
script in question contains a method "create". Using "dir(module)" on
the returned value I see no "create" method and the type is a compiled
string. So I tried this:

#CODE#
loadedModule = Py_InitModule3( fullname, NULL, "Loaded module" );
if( ! PyRun_StringFlags( content.GetString(), Py_file_input,
loadedModule, loadedModule, NULL ) ){
    if( PyErr_Occurred() ) PyErr_Print();
    Py_INCREF( Py_None );
    return Py_None;
}
Py_INCREF( loadedModule );
return loadedModule;
#CODE#

I concluded that I have to create the module myself and somehow
eval/compile the c-string into the module. For this I figured out
PyRun_StringFlags would be a suitable candidate. But doing the above I
get an error:

#CODE#
Traceback (most recent call last):
  File "<string>", line 22, in <module>
ImportError: __import__ not found
#CODE#

Looks like builtins are not existing. But from the internet I read that
if code is parsed from a module both globals and locals are the module.
The above code should do this but yet it fails. I tried also with
PyEval_GetGlobals() and PyEval_GetBuiltins() I had no luck either except
the function complaining about receiving NULL values.

Can anybody help how in gods name one is supposed to create a module
from an in-memory c-string when called from within load_module (or
anywhere)?

-- 
Yours sincerely
Plüss Roland

Leader and Head Programmer
- Game: Epsylon ( http://www.indiedb.com/games/epsylon )
- Game Engine: Drag[en]gine ( http://www.indiedb.com/engines/dragengine
, http://dragengine.rptd.ch/wiki )
- Normal Map Generator: DENormGen ( http://epsylon.rptd.ch/denormgen.php )
- As well as various Blender export scripts und game tools

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20140517/437e0efa/attachment.sig>


More information about the Python-list mailing list