tracemalloc: _compare_grouped_stats mutates old_group, and a typo in _group_by
Author: Tony123-techCreated Sep 26, 2026Updated Sep 28, 2026
Labelstype-bugstdlib
Two small issues in Lib/tracemalloc.py:
_compare_grouped_stats() mutates its old_group argument by popping keys from it. After the call, old_group is missing every key that was also in new_group.
A typo in the error message raised by _group_by(): "cumulative mode cannot by used" should be "cumulative mode cannot be used".
Reproduction for (1):
from tracemalloc import Statistic, _compare_grouped_stats
old = {"a": Statistic("a", 100, 2), "b": Statistic("b", 50, 1)}
new = {"a": Statistic("a", 200, 3), "c": Statistic("c", 10, 1)}
old_copy = dict(old)
_compare_grouped_stats(old, new)
print(len(old), len(old_copy)) # 1 2Linked PRs
- gh-158233
- gh-158371
Source: python/cpython