如何修改sys.exc_info()的堆栈列表?

huangapple go评论121阅读模式
英文:

How do I modify the stack list of sys.exc_info()?

问题

  1. 我想要从堆栈列表中移除表示装饰器的一些帧然后再记录它但是当我尝试访问它时调试器说没有名为 `stack` 的属性尽管 PyCharm 显示它
  2. ```py
  3. sys.exc_info()[2].tb_frame.stack # < -- AttributeError,但为什么?

如何修改sys.exc_info()的堆栈列表?

这是我想要做的:

  1. sys.exc_info()[2].tb_frame.stack = sys.exc_info()[2].tb_frame.stack[3:]
  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;d like to remove a couple of frames from the stack list that represent a decorator before logging it, but when I try to get to it, the debugger says that there is no such attribute as `stack` even though PyCharm shows it:
  4. ```py
  5. sys.exc_info()[2].tb_frame.stack # &lt;-- AttributeError, but why?

如何修改sys.exc_info()的堆栈列表?

This is what I would like to do:

  1. sys.exc_info()[2].tb_frame.stack = sys.exc_info()[2].tb_frame.stack[3:]

答案1

得分: 3

对于Python 3.8版本中的traceback.tb_frame对象,我看不到stack属性:

  1. try:
  2. raise Exception('test')
  3. except:
  4. cls, e, tb = sys.exc_info()
  5. hasattr(tb.tb_frame, 'stack')
  6. False

你可以通过在回溯(traceback)上追踪tb_next属性,可能在for循环中执行此操作:

  1. try:
  2. do_something()
  3. except Exception as e:
  4. cls, exc, tb = sys.exc_info()
  5. for _ in range(3):
  6. tb_next = tb.tb_next
  7. # 如果没有那么多帧的情况
  8. if not tb_next:
  9. break
  10. tb = tb_next
  11. raise cls.with_traceback(tb)
英文:

FWIW, I can't see an attribute for stack on a traceback.tb_frame object in python 3.8:

  1. try:
  2. raise Exception(&#39;test&#39;)
  3. except:
  4. cls, e, tb = sys.exc_info()
  5. hasattr(tb.tb_frame, &#39;stack&#39;)
  6. False

You can probably do this in a for loop by tracing the tb_next attribute on the traceback:

  1. try:
  2. do_something()
  3. except Exception as e:
  4. cls, exc, tb = sys.exc_info()
  5. for _ in range(3):
  6. tb_next = tb.tb_next
  7. # in case you don&#39;t have that many frames
  8. if not tb_next:
  9. break
  10. tb = tb_next
  11. raise cls.with_traceback(tb)

答案2

得分: 0

stack 属性不是 Python 中 frame 对象的内置属性。看起来 PyCharm 提供了这个属性以方便调试,但它并不属于标准的 Python 库。

要实现从堆栈列表中移除几个帧以达到你期望的结果,你可以使用 traceback 模块直接操作回溯对象。下面是一个例子:

  1. import traceback
  2. try:
  3. # 在这里放置你的代码
  4. pass
  5. except Exception as e:
  6. tb = e.__traceback__
  7. tb = tb.tb_next.tb_next # 跳过两个帧
  8. e.__traceback__ = tb
  9. raise e

在这个例子中,我们访问异常对象 (e) 的 __traceback__ 属性来获取回溯对象。然后我们通过两次访问 tb_next 属性来跳过两个帧,然后将修改后的回溯对象赋值回 __traceback__。最后,我们重新引发异常以保留原始行为。

请注意,以这种方式修改回溯对象并不是一种常见的做法,应该谨慎使用。它可能会产生意想不到的后果,并使调试变得更加困难。

英文:

The stack attribute is not a built-in attribute of the frame object in Python. It seems that PyCharm is providing this attribute as a convenience for debugging purposes, but it is not part of the standard Python library.

To achieve your desired result of removing a couple of frames from the stack list, you can use the traceback module to manipulate the traceback object directly. Here's an example:

  1. import traceback
  2. try:
  3. # Your code here
  4. pass
  5. except Exception as e:
  6. tb = e.__traceback__
  7. tb = tb.tb_next.tb_next # Skip two frames
  8. e.__traceback__ = tb
  9. raise e

In this example, we access the __traceback__ attribute of the exception object (e) to get the traceback object. We then skip two frames by accessing the tb_next attribute twice, and assign the modified traceback object back to __traceback__. Finally, we re-raise the exception to preserve the original behavior.

Note that modifying the traceback object in this way is not a common practice and should be used with caution. It may have unexpected consequences and make debugging more difficult.

huangapple
  • 本文由 发表于 2023年7月18日 03:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定