Skip to content

Commit 97ccc3f

Browse files
committed
Fix tracemalloc.Snapshot.filter_traces() for tuple traces storage
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>
1 parent 5989095 commit 97ccc3f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/test/test_tracemalloc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ def test_filter_traces(self):
446446

447447
self.assertRaises(TypeError, snapshot.filter_traces, filter1)
448448

449+
def test_filter_traces_no_filter_tuple_storage(self):
450+
snapshot, _snapshot2 = create_snapshots()
451+
snap = tracemalloc.Snapshot(tuple(snapshot.traces._traces),
452+
snapshot.traceback_limit)
453+
snap2 = snap.filter_traces(())
454+
self.assertEqual(snap2.traces._traces, list(snap.traces._traces))
455+
449456
def test_filter_traces_domain(self):
450457
snapshot, snapshot2 = create_snapshots()
451458
filter1 = tracemalloc.Filter(False, "a.py", domain=1)

Lib/tracemalloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def filter_traces(self, filters):
471471
exclude_filters,
472472
trace)]
473473
else:
474-
new_traces = self.traces._traces.copy()
474+
new_traces = list(self.traces._traces)
475475
return Snapshot(new_traces, self.traceback_limit)
476476

477477
def _group_by(self, key_type, cumulative):

0 commit comments

Comments
 (0)