Skip to content

tracemalloc.Snapshot.filter_traces() fails when internal traces storage is a tuple #143016

@hyongtao-code

Description

@hyongtao-code

Bug report

Bug description:

Snapshot.filter_traces() assumes that the internal traces container supports .copy().
If a Snapshot is constructed with tuple traces, calling filter_traces(()) raises:

AttributeError: 'tuple' object has no attribute 'copy'

Mini Reproducible Script:

import tracemalloc

tracemalloc.start(25)
snap = tracemalloc.take_snapshot()
snap2 = tracemalloc.Snapshot(tuple(snap.traces._traces), snap.traceback_limit)
snap2.filter_traces(())
print("done")

Traceback (on Windows debug build):

D:\MyCode\cpython\PCbuild\amd64>python_d.exe py_tracemalloc.py
Traceback (most recent call last):
  File "D:\MyCode\cpython\PCbuild\amd64\py_tracemalloc.py", line 6, in <module>
    snap2.filter_traces(())
    ~~~~~~~~~~~~~~~~~~~^^^^
  File "D:\MyCode\cpython\Lib\tracemalloc.py", line 474, in filter_traces
    new_traces = self.traces._traces.copy()
                 ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'copy'

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions