How can I modify a Python traceback object when raising an exception?

后端 未结 7 1309
梦如初夏
梦如初夏 2020-11-29 09:56

I\'m working on a Python library used by third-party developers to write extensions for our core application.

I\'d like to know if it\'s possible to modify the trace

7条回答
  •  渐次进展
    2020-11-29 10:45

    You can remove the top of the traceback easily with by raising with the tb_next element of the traceback:

    except:
        ei = sys.exc_info()
        raise ei[0], ei[1], ei[2].tb_next
    

    tb_next is a read_only attribute, so I don't know of a way to remove stuff from the bottom. You might be able to screw with the properties mechanism to allow access to the property, but I don't know how to do that.

提交回复
热议问题