[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: problem using import from PyRun_String
From:       "Patrick Stinson" <patrickkidd.lists () gmail ! com>
Date:       2008-04-11 7:26:32
Message-ID: 664bf2b80804110026h4175426dk97977471ce20d189 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Great, that was the answer I was looking for, thank you. I'll respond with
how well it works.

On Thu, Apr 10, 2008 at 12:16 AM, Gabriel Genellina <gagsl-py2@yahoo.com.ar>
wrote:

> En Wed, 09 Apr 2008 13:31:22 -0300, Patrick Stinson
> <patrickkidd.lists@gmail.com> escribió:
>
> > 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.
>
> Instead of PyRun_String, use PyImport_ExecCodeModuleEx, which takes care
> of setting __builtins__ and other details.
> You will need to compile the source first (using Py_CompileStringFlags)
> which is a good thing anyway, to help catching errors.
>
> > 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.
>
> That's not exactly what CPython does; if you inspect globals() you don't
> see abs, range, etc. Instead, there is a __builtins__ attribute (note the
> "s") that points to the __builtin__ module or its __dict__.
>
> > 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.
>
> When a frame builtins != internal builtins, Python runs in "restricted
> execution mode", and disallows access to some objects and attributes, I
> think that __import__ is one of them.
> See http://docs.python.org/lib/module-rexec.html
> (That's why it's important to use the right way to create a module)
>
> > 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.
>
> Exactly. The simplest way would be to save the source code on disk and
> just import it, letting Python do all the dirty work. But
> compiling+PyImport_ExecCodeModule should work, I presume - any feedback is
> welcome!
>
> --
> 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/

[Attachment #5 (text/html)]

Great, that was the answer I was looking for, thank you. I&#39;ll respond with how \
well it works.<br><br><div class="gmail_quote">On Thu, Apr 10, 2008 at 12:16 AM, \
Gabriel Genellina &lt;<a \
href="mailto:gagsl-py2@yahoo.com.ar">gagsl-py2@yahoo.com.ar</a>&gt; wrote:<br> \
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex;">En Wed, 09 Apr 2008 13:31:22 -0300, Patrick Stinson<br> <div \
class="Ih2E3d">&lt;<a \
href="mailto:patrickkidd.lists@gmail.com">patrickkidd.lists@gmail.com</a>&gt; \
escribió:<br> <br>
</div><div class="Ih2E3d">&gt; Well, I eventually want to add an import hook, but for \
now I&#39;d rather<br> &gt; just<br>
&gt; get the import statement working normally again.<br>
&gt; I have embedded python as a scripting engine in my application. To do<br>
&gt; this,<br>
&gt; I create a new empty module, run the script text using PyRun_String()<br>
&gt; passing the module&#39;s __dict__ as locals and globals. This populates the<br>
&gt; module&#39;s __dict__ with the resulting object references from the script<br>
&gt; text.<br>
<br>
</div>Instead of PyRun_String, use PyImport_ExecCodeModuleEx, which takes care<br>
of setting __builtins__ and other details.<br>
You will need to compile the source first (using Py_CompileStringFlags)<br>
which is a good thing anyway, to help catching errors.<br>
<div class="Ih2E3d"><br>
&gt; As I said before I must be forgetting some other module init stuff<br>
&gt; because I<br>
&gt; had to manually populate the modules&#39; __dict__ with references from the<br>
&gt; __builtin__ module in order to get the basic stuff like abs, range, etc.<br>
<br>
</div>That&#39;s not exactly what CPython does; if you inspect globals() you \
don&#39;t<br> see abs, range, etc. Instead, there is a __builtins__ attribute (note \
the<br> &quot;s&quot;) that points to the __builtin__ module or its __dict__.<br>
<div class="Ih2E3d"><br>
&gt; I understand what __builtin__ is used for, but the C struct pointing to<br>
&gt; the<br>
&gt; code frame that contains the import statement has a builtin member that<br>
&gt; apparently does not contain the __import__ function, hence my question.<br>
&gt; There must be some difference in the way code is parsed and a copy of the<br>
&gt; __builtin__ module is passed normally and the way I am doing it.<br>
<br>
</div>When a frame builtins != internal builtins, Python runs in &quot;restricted<br>
execution mode&quot;, and disallows access to some objects and attributes, I<br>
think that __import__ is one of them.<br>
See <a href="http://docs.python.org/lib/module-rexec.html" \
target="_blank">http://docs.python.org/lib/module-rexec.html</a><br> (That&#39;s why \
it&#39;s important to use the right way to create a module)<br> <div \
class="Ih2E3d"><br> &gt; So, finding the place where this module bootstrapping \
normally happens<br> &gt; would<br>
&gt; be awesome, because I sort of feel like I&#39;m hacking this method running<br>
&gt; into<br>
&gt; problems like this.<br>
<br>
</div>Exactly. The simplest way would be to save the source code on disk and<br>
just import it, letting Python do all the dirty work. But<br>
compiling+PyImport_ExecCodeModule should work, I presume - any feedback is<br>
welcome!<br>
<font color="#888888"><br>
--<br>
</font><div><div></div><div class="Wj3C7c">Gabriel Genellina<br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" \
target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br> \
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Patrick Kidd \
Stinson<br><a href="http://www.patrickkidd.com/">http://www.patrickkidd.com/</a><br><a \
href="http://pkaudio.sourceforge.net/">http://pkaudio.sourceforge.net/</a><br> <a \
href="http://pksampler.sourceforge.net/">http://pksampler.sourceforge.net/</a>



-- 
http://mail.python.org/mailman/listinfo/python-list

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic