2.3a1: importhooks oddity

Just just at xs4all.nl
Thu Jan 2 18:47:47 CET 2003


In article <ySNQ9.8812$9N5.914447 at newsread2.prod.itd.earthlink.net>,
 Hans Nowak <wurmy at earthlink.net> wrote:

> Howdy y'all,
> 
> I downloaded both the Windows installer and the source code for Python 2.3a1. 
> While playing with the new import hooks, I noticed something peculiar. 
> Consider 
> this piece of code:
> 
> import sys
> 
> print sys.version
> 
> def f(path):
>      print "Wahey!", path
>      raise ImportError
> 
> sys.path_hooks.append(f)
> 
> import nothing  # needless to say, doesn't exist
> 
> I built my own version with Cygwin, using a simple configure & make install, 
> nothing fancy. When I run the code, I get the following output:
> 
> $ python test-importhooks-2.py
> 2.3a1 (#1, Jan  1 2003, 21:44:23)
> [GCC 2.95.3-5 (cygwin special)]
> Wahey!
> Traceback (most recent call last):
>    File "test-importhooks-2.py", line 13, in ?
>      import nothing  # needless to say, doesn't exist
> ImportError: No module named nothing
> 
> This is what I expected. However, when I run this with the Windows version, I 
> get this output:
> 
> (P:\test\2.3) $ python test-importhooks-2.py
> 2.3a1 (#38, Dec 31 2002, 17:53:59) [MSC v.1200 32 bit (Intel)]
> Traceback (most recent call last):
>    File "test-importhooks-2.py", line 13, in ?
>      import nothing  # needless to say, doesn't exist
> ImportError: No module named nothing
> 
> In other words, function f doesn't seem to be called.
> 
> Is this a bug, a feature, or am I doing something wrong?

You may have to do sys.path_importer_cache.clear() to make sure no 
handler is registered for the path (this is most likely a None value, 
signalling it should use the builtin mechanism). Check PEP 302 for the 
details. You should then get a "Wahey!" for each member of sys.path (and 
as a bonus no file system import will work after you installed your hook 
;-)

Just




More information about the Python-list mailing list