我一直在努力解决一些我认为可能是IronPython中的错误的问题。如果有人能证实这种怀疑或让我直截了当,我将不胜感激。
我遇到的问题是尝试使用sys.meta_path或sys.path_hooks应用导入挂钩。在导入简单的模块和包时,IronPython似乎正确地调用了我的钩子。如果我尝试导入子模块或包(如foo.bar),则会调用钩子来导入foo,但在尝试导入bar时不会调用钩子。
我创建了以下代码来复制问题。在CPython中运行时,相同的代码不会出现任何问题。
import imp
import sys
class ReflectiveLoader2(object):
def __init__(self):
print "Init RL2"
def find_module(self, fullname, path=None):
print "find_module_RL2 called with fullname %s and path %s" % (fullname, path)
self.path = path
return self
def load_module(self, fullname):
print "load_module_RL2 called with fullname %s" % fullname
mod = imp.new_module(fullname)
mod.__name__ = fullname
mod.__loader__ = self
#Simulate a couple scenarios for imports
#Simulate a method
if fullname == "foo":
print "its foo"
mod.__filepath__ = r"C:\not\a\real\path\foo.py"
source = "def fooFunction(thing):print thing*3"
#Simulate another method
elif fullname == "bar":
print "its bar"
mod.__filepath__ = r"C:\not\a\real\path\bar.py"
source = "def barFunction(thing):print thing*4"
#Simulate package
elif fullname == "baz":
print "its baz"
mod.__filepath__ = r"C:\not\a\real\path\baz\__init__.py"
mod.__path__ = r"C:\not\a\real\path\baz"
source = "print 'initializing baz stuff here'"
#Simulate subpackage
elif fullname == "baz.bat":
print "its baz.bat"
mod.__filepath__ = r"C:\not\a\real\path\baz\bat.py"
mod.__path__ = r"C:\not\a\real\path\baz"
source = "def Batfunc():print 'in baz.bat sub package'"
#catchall
else:
print "Not foo, bar or baz its %s" % fullname
source = ""
exec source in mod.__dict__
return mod
sys.meta_path = [ReflectiveLoader2()]
import foo
foo.fooFunction("ABC")
import bar
bar.barFunction("DEF")
import baz.bat
baz.bat.Batfunc()
以下是使用IronPython和CPython的上述代码的输出。我已经在两个单独的系统上运行此代码,以防其中一个安装错误
C:\Users\Administrator\project>"C:\Program Files (x86)\IronPython 2.7\ipy.exe" ReflectiveLoader2.py
Init RL2
find_module_RL2 called with fullname foo and path None
load_module_RL2 called with fullname foo
its foo
ABCABCABC
find_module_RL2 called with fullname bar and path None
load_module_RL2 called with fullname bar
its bar
DEFDEFDEFDEF
find_module_RL2 called with fullname baz and path None
load_module_RL2 called with fullname baz
its baz
initializing baz stuff here
Traceback (most recent call last):
File "ReflectiveLoader2.py", line 63, in <module>
ImportError: No module named bat
C:\Users\Administrator\project>C:\Python27\python.exe ReflectiveLoader2.py
Init RL2
find_module_RL2 called with fullname foo and path None
load_module_RL2 called with fullname foo
its foo
ABCABCABC
find_module_RL2 called with fullname bar and path None
load_module_RL2 called with fullname bar
its bar
DEFDEFDEFDEF
find_module_RL2 called with fullname baz and path None
load_module_RL2 called with fullname baz
its baz
initializing baz stuff here
find_module_RL2 called with fullname baz.bat and path C:\not\a\real\path\baz
load_module_RL2 called with fullname baz.bat
its baz.bat
in baz.bat sub package
编辑: 我忘了提及我试图研究是否有其他人有这个问题和我能找到的最接近的事情是这篇文章
https://mail.python.org/pipermail/ironpython-users/2012-April/015879.html
据我所知,没有采取任何措施来解决所报告的问题
答案 0 :(得分:0)
经过一些额外的故障排除后,我认为这主要是操作员错误。在模块的路径属性方面,CPython稍微宽容一些。如果上面的代码被更改为
mod.__path__ = r"C:\not\a\real\path\baz"
看起来像
mod.__path__ = [r"C:\not\a\real\path\baz",]
一切正常。
如果某人有足够的动力去为IronPython创建类似的类型检查,我发现代码没有在IronPython源文件IronPython \ Runtime \ Importer.cs中检查列表类型,如下所示
object path;
List listPath;
if (scope.__dict__._storage.TryGetPath(out path) && (listPath = path as List) != null) {
return ImportNestedModule(context, scope, new [] {name}, 0, listPath);
}
如果将__path__设置为字符串(代替预期列表),则转换为列表失败,并且永远不会调用ImportNestedModule。
问题已经打开,因为CPython和IronPython之间存在不一致 https://github.com/IronLanguages/main/issues/1202