Loading modules from files through C++

R

Roland Plüss

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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlN2rO8ACgkQjKGu9ysRWfp59ACfbr+TqYzxGBMzKCeRJY+KE6ZG
xVAAn3ONeb2nsxjPiYp/U8qfqDQ31aeI
=QpgS
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top