gh-143016: Fix tracemalloc.Snapshot.filter_traces() for tuple traces storage#143018
Closed
hyongtao-code wants to merge 1 commit intopython:mainfrom
Closed
gh-143016: Fix tracemalloc.Snapshot.filter_traces() for tuple traces storage#143018hyongtao-code wants to merge 1 commit intopython:mainfrom
hyongtao-code wants to merge 1 commit intopython:mainfrom
Conversation
Snapshot.filter_traces() assumed the internal traces container supports .copy(), which fails if a Snapshot is constructed with tuple traces. Fix: use list(self.traces._traces) in the no-filter branch to make a shallow copy without depending on the container type. Add a regression test that constructs a Snapshot with tuple storage and verifies filter_traces(()) works. Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
sobolevn
reviewed
Dec 21, 2025
Member
sobolevn
left a comment
There was a problem hiding this comment.
I am not quite that this change is correct. filters is documented to be a list: https://docs.python.org/3/library/tracemalloc.html#tracemalloc.Snapshot.filter_traces
But now, it will allow more types in, like str or set.
Which is goes against the docs?
Contributor
Author
|
Thanks for pointing this out — you’re right : ) My confusion was about the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace .copy() with list(...) to correctly create a shallow copy for any sequence type.
A regression test is added to cover this case.
Mini Reproducible Script:
Result without patch
Result with patch