我试图编写一些代码,以防__import__
被更改,因为the Pydev debugger overrides __import__。在
所以,我需要一种访问内置__import__
函数的方法。在
>>> def fake_import(*args, **kwargs): pass # some other implementation here
...
>>> import __builtin__
>>> __builtin__.__import__ = fake_import
# can I recover the original value of __import__ here?
有关于recovering removed built-ins的问题,但在本例中,全局已被替换。在
查看链接的代码,原始的
builtins.__import__
被传递给新的ImportHookManager
实例,并作为_system_import
属性存储。在因此,根据How to get instance given a method of the instance?的答案,您可以做如下操作:
恢复原来的功能。不过,请注意,前导下划线按约定将此属性标记为私有,并且可以在不发出警告的情况下更改实现/属性名称。在
这是一个有点棘手的问题,因为python不会对重新加载})。在
__builtin__
模块感兴趣,因为它没有更改。您将被迫删除__builtin__
模块,以便强制python重新导入它。您也可以通过使用importlib
绕过__import__
(仅在python3中是这样,在python2中importlib
求助于{虽然我还没有找到恢复}相同的函数:
__import__
本身的方法,但是使用ihooks
我可以得到一个与{相关问题 更多 >
编程相关推荐