#158234·Python

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:

  1. _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.

  2. 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 2

Linked PRs

  • gh-158233
  • gh-158371