views:

499

answers:

2

Where can I find an example implementation of the "New Import Hooks" described in PEP 302?

I would like to implement a custom finder and loader in the most forward compatible way possible. In other words, the implementation should work in python 2.x and 3.x.

+5  A: 

You can find thousands of open-source examples e.g. with a google code search (can't get the URL right for a link with SO's markup, maybe it's too long, so here it is...):

http://www.google.com/codesearch?hl=en&lr=&q="imp.find_module"+"imp.load_module"&sbtn=Search

Edit: as the questioner clarified he's looking for example of implementation, not use, a betted URL for the search is:

http://www.google.com/codesearch?hl=en&sa=N&q="path_hooks"++lang:python&ct=rr&cs_r=lang:python

One readable example (though NOT suitable for production use, as the reddit discussion points out!) is urlimport.

As for supporting Python 2 and Python 3 at the same time, that sounds ambitious -- I don't know of any existing import hook which claims to. In your shoes, I'd start with offering full support for Python 2.6, then once that's working (and has a good battery of tests and makes nary a peep with the -3 switch), I'd 2to3 the sources and see if anything breaks (if so, find out why, fix the 2.6 sources, iterate).

Alex Martelli
That's good stuff, but I'm not looking for an example using imp.load_module. I would like to implement a custom finder and loader.
brad
I updated the question to reflect this...
brad
Oops sorry @brad, so did I and I think our edits clashed in mid-air (please edit it again to whatever wording you prefer, I was just trying to reflect the meaning of your comment of 1 minute ago).
Alex Martelli
No problem, sorry about the ambiguity. I never quite know how to word questions until someone else looks at them.
brad
So is the urlimport example proving useful to you...?
Alex Martelli