PEP 302 Example: New Import Hooks
Asked Answered
E

1

13

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.

Embowel answered 6/6, 2009 at 23:50 Comment(0)
I
2

You can find thousands of open-source examples e.g. with a google code search, 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 better 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).

Inconsolable answered 6/6, 2009 at 23:56 Comment(6)
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.Embowel
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).Inconsolable
No problem, sorry about the ambiguity. I never quite know how to word questions until someone else looks at them.Embowel
So is the urlimport example proving useful to you...?Inconsolable
Can you update these links, now that Code Search has been removed?Irmgard
Three out of the four links in your answer are broken.Bargello

© 2022 - 2024 — McMap. All rights reserved.