我正在尝试调试 hy 使用bytecode。特别是,每次导入模块时,我都想查看其实际导入的路径,无论是源还是字节码。在引擎盖下,HY用 ementlib
管理模块。它没有明确读取或编写字节;这是由 ementlib.machinery.sourcefileleleoader
来照顾的。因此,看来我想做的是每次导入时,猴子斑点Python的导入系统都可以打印导入路径。我该怎么做?一旦我了解如何为python做,我应该能够弄清楚如何做到这一点。
I'm trying to debug Hy's use of bytecode. In particular, each time a module is imported, I want to see the path it was actually imported from, whether source or bytecode. Under the hood, Hy manages modules with importlib
. It doesn't explicitly read or write bytecode; that's taken care of by importlib.machinery.SourceFileLoader
. So it looks like what I want to do is monkey-patch Python's importing system to print the import path each time an import happens. How can I do that? I should be able to figure out how to do it for Hy once I understand how to do it for Python.
发布评论
评论(2)
不涉及编码的最简单方法是用两个(!)详细标志启动Python:
您将获得大量输出,包括所有导入语句和所有文件Python试图访问以加载模块。在此示例中,我有一个简单的python脚本,可以
导入json
:替代但更复杂:您可以更改
import
语句本身。为此,您可以覆盖__ import __ import __ import __ import __
,由
导入
语句本身调用。这样,您可以打印出所有文件导入
实际打开。The easiest way that does not involve coding, is to start Python with two(!) verbose flags:
you'll get a lot of output, including all the import statements and all the files Python tries to access in order to load the module. In this example I have a simple python script that does
import json
:Alternatively but more complex: you could change the
import
statement itself. For that, you can overwrite__import__
, which is invoked by theimport
statement itself. This way you could print out all the filesimport
actually opens.似乎是一个不错的选择是动态修补
ementlib.machinery.sourcefileleleoader(fullname,path)
and ementlib.machinery.sourcelessfileleleoader(fullname,path,path)变量(a)调用方法和(b)传递给函数的参数。如果您需要做的就是:
并且您不需要导入才能“正常工作”,也许您可以进行修改的版本,例如
您当然会为
>参考的
Seems like a good option would be to dynamically patch
importlib.machinery.SourceFileLoader(fullname, path)
andimportlib.machinery.SourcelessFileLoader(fullname, path)
to each print or write to a variable (a) the calling method and (b) the argument passed to the function.If all you need to do is:
And you don't need the import to "work properly", perhaps you can do a modified version of something like this. For example, I quickly modified their sample code to get this, I have not tested it so it may not work exactly, but it should get you on the right track:
You would of course provide a similar mock for Sourceless file loading for
SourcelessFileLoader
For reference: