Skip to content

tracemalloc: _compare_grouped_stats mutates old_group, and a typo in _group_by #158234

Open

Description

@Tony123-tech

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

Activity

  1. StanFromIreland commented on Sep 26, 2026

    @StanFromIreland
    Member

    Can 1 be reproduced using only public APIs?

  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Sep 26, 2026
  3. Tony123-tech commented on Sep 26, 2026

    @Tony123-tech
    ContributorAuthor

    No, it cannot be reproduced using only public APIs. The caller,
    Snapshot.compare_to(), does not reuse the old_group dict, so the
    mutation is currently harmless.

    The issue is that _compare_grouped_stats() modifies its argument,
    which is a bad pattern even if no caller is currently affected. The
    fix is a one-line defensive copy:

    old_group = dict(old_group)
    

    If you think this isn't worth fixing because it's a private function
    with no affected public API, feel free to close it.

  4. picnixz commented on Sep 27, 2026

    @picnixz
    Member

    We reject defensive programming in general if this cannot be reached publicly. Callers are consenting adults.

  5. picnixz commented on Sep 27, 2026

    @picnixz
    Member

    However the fix for the exception is real I think so let's keep it.

  6. picnixz commented on Sep 27, 2026

    @picnixz
    Member

    After looking at the PR I think we can still make the change just in case as the name of the function may suggest a pure function.

  7. added a commit that references this issue on Sep 27, 2026
  8. Tony123-tech commented on Sep 27, 2026

    @Tony123-tech
    ContributorAuthor

    Thanks for the review! I understand the concern about defensive programming. I'll keep the change since the function name suggests it's pure, and the typo fix is clearly correct.

  9. Tony123-tech commented on Sep 27, 2026

    @Tony123-tech
    ContributorAuthor

    Thanks for the review! I understand the concern about defensive programming. I'll keep the change since the function name suggests it's pure, and the typo fix is clearly correct.

  10. removed
    pendingThe issue will be closed if no feedback is provided
    on Sep 27, 2026
  11. added a commit that references this issue on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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