给定任意python程序上cProfile的输出,可以重建原始源代码吗?

2024-04-29 07:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我做了这样的事

python -m cProfile -o profile.prof myprogram.py

你能从有权访问profile.prof的地方重建源代码吗


Tags: py源代码地方profilecprofileprof有权myprogram
1条回答
网友
1楼 · 发布于 2024-04-29 07:30:38

所有.prof文件包含的是一个^{} serialisation的统计数据,与您在不使用-o时看到的数据相同(因此打印这些数据)

它基本上是一个在函数“label”上键入的字典,包含每个函数调用的统计信息。函数标签定义为:

def label(code):
    if isinstance(code, str):
        return ('~', 0, code)    # built-in functions ('~' sorts at the end)
    else:
        return (code.co_filename, code.co_firstlineno, code.co_name)

因此只记录文件名、行号和函数名,而不记录其他内容。你不能从这个重建一个完整的程序

相关问题 更多 >