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

List:       python-list
Subject:    Re: import hooks
From:       "Patrick Stinson" <patrickkidd.lists () gmail ! com>
Date:       2008-04-17 13:30:08
Message-ID: 664bf2b80804170630j47e6c7a8r692f5d80af2cc13f () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Right on, that seemed to work, thanks.
This is different than sys.path_hooks though, which requires a callable or
string subclass?

After some experimentation it looks like you can disallow an import by
raising an import error from your meta_path hook. It seems a little weird
that python will then raise a new ImportError from import.c:find_module(),
but I guess the behavior is desirable..

On Wed, Apr 16, 2008 at 5:17 PM, Gabriel Genellina <gagsl-py2@yahoo.com.ar>
wrote:

> En Wed, 16 Apr 2008 09:04:36 -0300, Patrick Stinson
> <patrickkidd.lists@gmail.com> escribió:
>
> > I am defining a simple finder/loader object and adding it to
> > sys.meta_path
> > like this:
> >
> > PyRun_SimpleString("import sys; import ousiainternal; sys.meta_path =
> > [ousiainternal.OusiaImporter]");
>
> You should append to the existing meta_path, not replace it, erasing any
> previous content.
> And it must be an *instance* of your importer, not the type itself.
> Note that you're polluting the __main__ module namespace by using
> PyRun_SimpleString; I'd use API calls like PySys_GetObject("meta_path")
> and PyList_Append (PEP 302 guarantees it is a list).
>
> > "sys.meta_path.append(Importer)\n";
>
> Here you append to sys.meta_path, but fail to create the instance first.
>
> > PyRun_SimpleString(importer_source);
>
> You should check the return value; I bet you got a -1 (failure).
>
> --
> 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)]

Right on, that seemed to work, thanks.<div><br></div><div>This is different than \
sys.path_hooks though, which requires a callable or string \
subclass?&nbsp;</div><div><br></div><div>After some experimentation it looks like you \
can disallow an import by raising an import error from your meta_path hook. It seems \
a little weird that python will then raise a new ImportError from \
import.c:find_module(), but I guess the behavior is desirable..</div> \
<div></div><div><br><div class="gmail_quote">On Wed, Apr 16, 2008 at 5:17 PM, 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, 16 Apr 2008 09:04:36 -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; I am defining a simple finder/loader object and adding \
it to<br> &gt; sys.meta_path<br>
&gt; like this:<br>
&gt;<br>
&gt; PyRun_SimpleString(&quot;import sys; import ousiainternal; sys.meta_path =<br>
&gt; [ousiainternal.OusiaImporter]&quot;);<br>
<br>
</div>You should append to the existing meta_path, not replace it, erasing any<br>
previous content.<br>
And it must be an *instance* of your importer, not the type itself.<br>
Note that you&#39;re polluting the __main__ module namespace by using<br>
PyRun_SimpleString; I&#39;d use API calls like \
PySys_GetObject(&quot;meta_path&quot;)<br> and PyList_Append (PEP 302 guarantees it \
is a list).<br> <div class="Ih2E3d"><br>
&gt; &quot;sys.meta_path.append(Importer)\n&quot;;<br>
<br>
</div>Here you append to sys.meta_path, but fail to create the instance first.<br>
<br>
&gt; PyRun_SimpleString(importer_source);<br>
<br>
You should check the return value; I bet you got a -1 (failure).<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> </div>



-- 
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