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.
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).
发布评论
评论(1)
您可以找到数千个开源示例,例如使用 谷歌代码搜索,这里是:
编辑:正如提问者澄清的那样,他正在寻找实现的示例,而不是使用,更好的 网址搜索的 是:
一个可读的示例(尽管不适合生产使用,因为 reddit讨论指出!)是urlimport。
至于同时支持 Python 2 和 Python 3,这听起来雄心勃勃——我不知道有任何现有的导入钩子声称如此。 站在你的立场上,我会首先提供对 Python 2.6 的全面支持,然后一旦它起作用(并且进行了一系列良好的测试并且不使用
-3
开关),我就会2to3
源代码,看看是否有任何问题(如果有,找出原因,修复 2.6 源代码,迭代)。You can find thousands of open-source examples e.g. with a google code search, here it is:
Edit: as the questioner clarified he's looking for example of implementation, not use, a better URL for the search is:
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'd2to3
the sources and see if anything breaks (if so, find out why, fix the 2.6 sources, iterate).