How can I elide a function wrapper from the traceback in Python-3?

后端 未结 1 918
离开以前
离开以前 2021-02-19 14:26

The issue

The Phantom Menace

Say i wrote a function decorator which takes the function, and wraps it in another function like so:



        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 14:45

    The simple answer is that you shouldn't do that. Hiding things from the traceback is dangerous. You may think you don't want to show that line because it's trivial or "just a wrapper", but in general you wouldn't write the wrapper function if it didn't do something. Next thing you know there is a bug in the wrapper function which is now unfindable because the wrapper function has erased itself from the traceback.

    Just deal with the extra lines in the traceback, or, if you really want, override sys.excepthook and filter them out at the top level. If you're worried about someone else overriding sys.excepthook too, then wrap all your code in a top-level function that does the exception printing itself. It isn't and shouldn't be easy to hide levels from the traceback.

    0 讨论(0)
提交回复
热议问题