tag:blogger.com,1999:blog-2363643920942057324.post7672746776551574219..comments2023-08-09T23:00:54.857+10:00Comments on Graham Dumpleton: Generating full stack traces for exceptions in Python.Graham Dumpletonhttp://www.blogger.com/profile/13609779138164842374noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-2363643920942057324.post-51448358849081072482018-03-30T03:51:09.537+11:002018-03-30T03:51:09.537+11:00I encountered this problem while developing a GUI ...I encountered this problem while developing a GUI app with PyQt. For other people with the same setup, I wrote a <a href="https://fman.io/blog/pyqt-excepthook/" rel="nofollow">blog post</a> with a solution mirroring the one by Adrien above. Maybe it will spare someone the hours I just spent!Michael Herrmannhttps://www.blogger.com/profile/04833014792035469716noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-52121384717807424792017-11-01T00:50:27.102+11:002017-11-01T00:50:27.102+11:00Hi, thanks for this article which helped me to bet...Hi, thanks for this article which helped me to better understand how traceback, stacks and exceptions work in Python.<br /><br />One possible and elegant solution to the problem is to walk back trough the stack and create fake tracebacks which could be preprended to the actual traceback got from "sys.exc_info". That way, the traceback starts from the root and continues to the error Delganhttps://www.blogger.com/profile/01879066589943384553noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-71481036526765741262017-02-03T13:22:25.689+11:002017-02-03T13:22:25.689+11:00I had the same problem last day, but worse because...I had the same problem last day, but worse because of threading<br /><br />Here my solution:<br /><br />def get_stack(skip=1, tb=None):<br /> stack = traceback.extract_stack()[:-(1+skip)]<br /><br /> # if we are in a properly created thread, preprend the thread stack<br /> if hasattr(threading.current_thread(), "_stack"):<br /> stack = getattr(threading.current_thread(),Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-2218064094238186412015-06-05T16:30:29.841+10:002015-06-05T16:30:29.841+10:00I wanted to see the upper frames some months ago. ...I wanted to see the upper frames some months ago. The issue I created was solved in Python3. I never tried the solution since I am still fixed on Python2. Issue: https://bugs.python.org/issue9427guettlihttps://www.blogger.com/profile/00266664518110871670noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-83073513165197779262015-03-27T11:52:30.168+11:002015-03-27T11:52:30.168+11:00One could possibly argue the case for a sys.setexc...One could possibly argue the case for a sys.setexcept()/sys.getexpect() hooking mechanism in the same vain as those for profile and trace call backs, but would want to see the good use case for it.<br /><br />A problem noted on SO is how you distinguish an internal implementation detail where the exception is caught and suppressed automatically such as StopIteration, AttributeError on getattr() Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-17222732570655013152015-03-27T02:10:45.483+11:002015-03-27T02:10:45.483+11:00And what do you think about idea of Python provide...And what do you think about idea of Python provided hook called upon entering except/finally block?Piotr Dobrogosthttps://www.blogger.com/profile/09529158749729184143noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-27926106267434541342015-03-26T21:45:40.408+11:002015-03-26T21:45:40.408+11:00No, there is no one way of being able to patch Pyt...No, there is no one way of being able to patch Python at that level without changing the C code of the interpreter itself.Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-11794153659588514712015-03-26T21:30:38.950+11:002015-03-26T21:30:38.950+11:00It seems creating a snapshot of the full stack wit...It seems <i>creating a snapshot of the full stack within the same function as where the 'try/except' occurred</i> is really the only way to provide full context of exception. The question is why Python itself does not do this and attach this snapshot to exception? I guess this might be to avoid cost of taking snapshot. If this is the case then Python should provide hook called every time Piotr Dobrogosthttps://www.blogger.com/profile/09529158749729184143noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-82282692302299875432015-03-23T09:55:09.483+11:002015-03-23T09:55:09.483+11:00With chaining of exceptions, because the traceback...With chaining of exceptions, because the traceback of initial exceptions would be retained beyond the life of the try/except for those when re-raising a different exception, I would imaging the issue described with line numbers not then being accurate for the outer stack frames would apply.Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-32505309994196551112015-03-22T03:49:51.144+11:002015-03-22T03:49:51.144+11:00Also, what about Python 3's Exception Chaining...Also, what about Python 3's Exception Chaining (PEP 3134)?<br /><br />I have a library that provides hooks via user-supplied callbacks; and if the callback raises an exception, it gets repackaged in my own exception type with a chain to the original exception. To allow the library to work in both Python 2 and 3 (even with 2to3) I manually construct the chain, such as this in Python 2 syntax:Deron Merandahttps://www.blogger.com/profile/18332381064910373026noreply@blogger.com