problem using import from PyRun_String

Patrick Stinson patrickkidd.lists at gmail.com
Wed Apr 9 12:31:22 EDT 2008


Well, I eventually want to add an import hook, but for now I'd rather just
get the import statement working normally again.
I have embedded python as a scripting engine in my application. To do this,
I create a new empty module, run the script text using PyRun_String()
passing the module's __dict__ as locals and globals. This populates the
module's __dict__ with the resulting object references from the script text.
As I said before I must be forgetting some other module init stuff because I
had to manually populate the modules' __dict__ with references from the
__builtin__ module in order to get the basic stuff like abs, range, etc.

I understand what __builtin__ is used for, but the C struct pointing to the
code frame that contains the import statement has a builtin member that
apparently does not contain the __import__ function, hence my question.
There must be some difference in the way code is parsed and a copy of the
__builtin__ module is passed normally and the way I am doing it.

So, finding the place where this module bootstrapping normally happens would
be awesome, because I sort of feel like I'm hacking this method running into
problems like this.

I'll definitely be putting together a wiki on this topic before long. Seems
like an application scripting engine is incredibly easy to implement if you
already know all the tricks, we're just lacking docs.

On Wed, Apr 9, 2008 at 1:00 AM, Gabriel Genellina <gagsl-py2 at yahoo.com.ar>
wrote:

> En Tue, 08 Apr 2008 22:01:18 -0300, Patrick Stinson
> <patrickkidd.lists at gmail.com> escribió:
>
> > I'm creating a module with PyModule_New(), and running a string buffer
> as
> > the module's text using PyRun_String and passing the module's __dict__
> to
> > locals and globals.
>
> Why? Do you want to fake what import does?
>
> > I'm having a problem using the import statement from
> > within PyRun_String(). It complains about "__import__ not found", which
> > after a quick grep it looks like the exception is raised from
> > PyExc_EvalFrameEx() in ceval.c after f->f_builtins module doesn't
> contain
> > "__import__".
> > What __builtin__ module does that point to? a quick print of the
>
> __builtin__ holds all the builtin objects, what a surprise! :)
> http://docs.python.org/lib/module-builtin.html
>
> > Any help?
>
> What do you want to do actually?
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Patrick Kidd Stinson
http://www.patrickkidd.com/
http://pkaudio.sourceforge.net/
http://pksampler.sourceforge.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080409/e28653e9/attachment-0001.html>


More information about the Python-list mailing list