You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tracemalloc: _compare_grouped_stats mutates old_group, and a typo in _group_by #158234
_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".
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.
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.
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.
Activity
StanFromIreland commentedon Sep 26, 2026
Can 1 be reproduced using only public APIs?
Tony123-tech commentedon Sep 26, 2026
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:
If you think this isn't worth fixing because it's a private function
with no affected public API, feel free to close it.
picnixz commentedon Sep 27, 2026
We reject defensive programming in general if this cannot be reached publicly. Callers are consenting adults.
picnixz commentedon Sep 27, 2026
However the fix for the exception is real I think so let's keep it.
picnixz commentedon Sep 27, 2026
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.
pythongh-158234: Fix _compare_grouped_stats mutating old_group and a …
Tony123-tech commentedon Sep 27, 2026
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.
Tony123-tech commentedon Sep 27, 2026
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.
pythongh-158234: Fixed tracemalloc._compare_grouped_stas mutating old…