bpo-31321: Fix traceback.clear_frames()#3262
bpo-31321: Fix traceback.clear_frames()#3262vstinner wants to merge 1 commit intopython:masterfrom vstinner:clear_frames
Conversation
|
TODO: Convert script attached to http://bugs.python.org/issue31321 to an unit test. |
Lib/traceback.py
Outdated
There was a problem hiding this comment.
This is making clear_frames() a potentially O(n**2) operation. In practice, you don't need this, as tb.tb_next.tb_frame.f_back will be the same as tb.tb_frame (and so on). So the loops needn't be nested.
There was a problem hiding this comment.
Or you can keep the nesting but mark frames as seen as you iterate on them, which will have the same effect while being more general (in case someone changes the traceback chain).
There was a problem hiding this comment.
This is making clear_frames() a potentially O(n**2) operation.
Sorry, I don't know well how traceback objects are created. I didn't even know that you can have two tracebacks objects linked together.
I added a "seen = set()" to prevent iterating twice on the same frame chain. Does it solve your O(n**2) issue?
I also used my example attached to the bpo to enhance the existing unit test.
There was a problem hiding this comment.
You can also use while frame is not None here instead of having a separate test at the end of the loop.
traceback.clear_frames() now iterates on frames to clear all frames of each traceback object, not only the first frame. Enhance test_clear() unit test to check local variables of all frames, not only the inner() frame. Rename also the test to test_clear_frames() to better reflected te name of the tested function.
|
I rebased my change and added a set to avoid O(n**2) complexity, as suggested by Antoine Pitrou (if I understood correctly his comment). @pitrou: I didn't understand your second suggestion to avoid nested loop. If you find a way to avoid the nested loop without breaking my unit test, I'm curious :-) |
|
@vstinner why did you close this PR? Is the fix obsolete? |
traceback.clear_frames() now iterates on frames to clear all frames
of each traceback object, not only the first frame.
https://bugs.python.org/issue31321