如何重写回溯中的特定帧? [英] How to rewrite a specific frame in a traceback?

查看:66
本文介绍了如何重写回溯中的特定帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,您可以使用exec()更快地执行compile()字符串.但是,一旦我使用它,当执行程序发生异常时,我们就会丢失信息.

例如,下面是一个调用未知方法的代码段(出于演示目的):

code = 'my_unknown_method()'
bytecode = compile(code, '<string>', 'exec')

然后,我在该字节码上调用exec:

exec bytecode

显示的回溯是:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    exec bytecode
  File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined   

"exec()"框架现在不明显了.我希望有一个更好的例外,例如:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    exec "my_unknown_method()"
  File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined   

有什么想法吗?

注意:

  • 我不想使用compile的第二个参数(文件名)
  • 我已经测试过可以检查并修改框架上的f_code,但这是只读属性.

在对sys.excepthook进行了更多研究之后,我已经看到在python源代码/traceback.c中,当python要显示行内容时,如果找到它们,它们就是fopen()直接文件.根本没有钩子可以显示我们自己的内容.唯一的方法是在磁盘上创建真实的假文件名?有人吗?

我检查了一些jinja2调试代码,它们也正在重写回溯,但没有内容.除挂钩以外,我是否需要自定义?我担心它是因为它不在回溯本身中,如果用户/模块/无论采取什么异常,则回溯将不包含有价值的信息.

解决方案

经过深度搜索后,CPython无法使用其自己的API来确定文件的位置等,并且无法在纯Python中对其进行修补. /p>

In python you can compile() string to be executed faster with exec(). But as soon as i use it, we lost information when an exception happen in the exec.

For example, here is a code snippet that calling a unknown method (for demo purposes):

code = 'my_unknown_method()'
bytecode = compile(code, '<string>', 'exec')

And later, i'm calling exec on that bytecode:

exec bytecode

The traceback showed is:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    exec bytecode
  File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined   

The "exec()" frame is now obscure. I would like to have a better exception like:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    exec "my_unknown_method()"
  File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined   

Any thoughts ?

Notes:

  • I don't want to use the second argument of compile (the filename)
  • I have tested to play with inspect and modify f_code on the frame, but it's readonly attribute.

EDIT: after looking more to sys.excepthook, i've seen that in python source code/traceback.c, when python want to show the line content, they are fopen() directly the file if found. No hook available at all to display our own content. The only way would be to create in real fake filename on disk ? Anyone ?

EDIT2: i checked some jinja2 debug code, and they are rewriting traceback too, but not for content. Would i need a custom except hook ? My concerns with it is since it's not in the traceback itself, if an user/module/whatever take the exception, the traceback will not contain valuable information.

解决方案

After a deep search, it's not possible, CPython is using its own API to determine where the file is etc, and it cannot be patched in pure Python.

这篇关于如何重写回溯中的特定帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆