如何使用importlib重写字节码?
我正在寻找一种在 Python 2.x 中使用 importlib 重写导入的字节码的方法即时模块。换句话说,我需要在导入期间在编译和执行步骤之间挂钩我自己的函数。除此之外,我希望导入功能能够像内置功能一样工作。
我已经使用 imputil 做到了这一点,但该库并未涵盖所有情况,并且无论如何已被弃用。
I'm looking for a way to use importlib in Python 2.x to rewrite bytecode of imported modules on-the-fly. In other words, I need to hook my own function between the compilation and execution step during import. Besides that I want the import function to work just as the built-in one.
I've already did that with imputil, but that library doesn't cover all cases and is deprecated anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看了
importlib
源代码后,我相信您可以在_bootstrap
模块中子类化PyLoader
并覆盖get_code
>:我假设你知道自己在做什么,但代表世界各地的程序员,我相信我应该说:呃 =p
Having had a look through the
importlib
source code, I believe you could subclassPyLoader
in the_bootstrap
module and overrideget_code
:I assume you know what you're doing, but on behalf of programmers everywhere I believe I should say: ugh =p