Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71671

Loading modules from files through C++

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <roland@rptd.ch>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'programmer': 0.03; 'string.': 0.05; 'error:': 0.07; 'importerror:': 0.07; '22,': 0.09; 'null,': 0.09; 'parsed': 0.09; 'subject:files': 0.09; 'subject:modules': 0.09; 'used.': 0.09; 'worked.': 0.09; 'python': 0.11; 'builtins': 0.16; 'complaining': 0.16; 'fails.': 0.16; 'filename:fname piece:signature': 0.16; 'fullname,': 0.16; 'globals': 0.16; 'in-memory': 0.16; 'received:192.168.1.20': 0.16; 'situation.': 0.16; 'url:ch': 0.16; 'module': 0.19; 'memory': 0.22; 'load': 0.23; 'header:User- Agent:1': 0.23; 'looks': 0.24; '(or': 0.24; 'question': 0.24; "i've": 0.25; 'source': 0.25; 'script': 0.25; 'compiled': 0.26; 'this:': 0.26; 'skip:" 20': 0.27; 'tried': 0.27; 'function': 0.29; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'url:wiki': 0.31; '"",': 0.31; 'candidate.': 0.31; 'loading': 0.31; 'values.': 0.31; 'file': 0.32; 'supposed': 0.32; 'interface': 0.32; 'regular': 0.32; '(most': 0.33; 'totally': 0.33; 'subject:from': 0.34; "can't": 0.35; 'except': 0.35; 'anybody': 0.35; 'but': 0.35; 'c++': 0.36; 'module.': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'files': 0.38; 'recent': 0.39; 'does': 0.39; 'embedded': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'und': 0.40; 'how': 0.40; 'read': 0.60; 'from:charset:utf-8': 0.61; 'name': 0.63; 'myself': 0.63; 'sincerely': 0.63; 'skip:n 10': 0.64; 'map': 0.64; 'finally': 0.65; 'within': 0.65; 'internet': 0.71; 'export': 0.74; 'url:php': 0.85; 'yours': 0.88; 'concluded': 0.91; 'step.': 0.91; 'luck': 0.93
Date Sat, 17 May 2014 02:27:19 +0200
From Roland Plüss <roland@rptd.ch>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Loading modules from files through C++
X-Enigmail-Version 1.6
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="QM4EgEd6eD7aCF90BiPjqMKiOja11DQar"
X-Mailman-Approved-At Sat, 17 May 2014 06:58:18 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10074.1400302700.18130.python-list@python.org> (permalink)
Lines 89
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1400302700 news.xs4all.nl 2902 [2001:888:2000:d::a6]:43445
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71671

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Loading modules from files through C++ Roland Plüss <roland@rptd.ch> - 2014-05-17 02:27 +0200

csiph-web