Skip to content

gh-158234: Fix typo in tracemalloc cumulative mode error message - #158233

Open
Tony123-tech wants to merge 1 commit into
python:mainfrom
Tony123-tech:gh-158232-fix-tracemalloc-compare-grouped-stats
Open

Tony123-tech wants to merge 1 commit into
python:mainfrom
Tony123-tech:gh-158232-fix-tracemalloc-compare-grouped-stats

Conversation

@Tony123-tech

@Tony123-tech Tony123-tech commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

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. The current caller (Snapshot.compare_to)
    does not reuse the dict, so the bug is latent, but the function
    should not modify its arguments. Fix: copy the dict at the top.

  2. A typo in the error message raised by _group_by():
    "cumulative mode cannot by used" should be
    "cumulative mode cannot be used".

No NEWS entry needed: neither change is user-visible.

@bedevere-app

bedevere-app Bot commented Sep 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Tony123-tech

Copy link
Copy Markdown
Contributor Author

No NEWS entry needed: neither change is user-visible. The
_compare_grouped_stats fix affects a private function, and the typo
fix only changes an error message.

@Tony123-tech Tony123-tech changed the title gh-158232: Fix _compare_grouped_stats mutating old_group and a typo gh-158234: Fix _compare_grouped_stats mutating old_group and a typo Sep 26, 2026
@python python locked and limited conversation to collaborators Sep 27, 2026
@python python unlocked this conversation Sep 27, 2026
@picnixz picnixz closed this Sep 27, 2026
@picnixz picnixz reopened this Sep 27, 2026
@Tony123-tech

Copy link
Copy Markdown
Contributor Author

The typo fix is user-visible only in the sense that the error message
changes. I don't think a NEWS entry is needed. Let me know if you
disagree.

@Tony123-tech
Tony123-tech force-pushed the gh-158232-fix-tracemalloc-compare-grouped-stats branch from be75241 to efe7bd4 Compare September 27, 2026 04:47

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this change because if we ever change the callers, we might have some weird surprises. So I'm not against this.

At least, the typo fix is real IMO and if others disagree with the defensive change, we can still keep the typo fix.

cc @StanFromIreland

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're fixing the typo, I won't object to the defensive change, but let's not add a test.

Also, I would like to highlight that without the typo, we would not accept the other change.

Comment on lines +601 to +617
def test_compare_to_does_not_mutate_group(self):
# gh-158232: _compare_grouped_stats() used to pop from old_group,
# silently draining the caller's dict.
tb_a = traceback(('f.py', 1))
tb_b = traceback(('f.py', 2))
tb_c = traceback(('f.py', 3))

old = {tb_a: tracemalloc.Statistic(tb_a, 100, 2),
tb_b: tracemalloc.Statistic(tb_b, 50, 1)}
new = {tb_a: tracemalloc.Statistic(tb_a, 200, 3),
tb_c: tracemalloc.Statistic(tb_c, 10, 1)}
old_copy = dict(old)

tracemalloc._compare_grouped_stats(old, new)

self.assertEqual(old, old_copy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_compare_to_does_not_mutate_group(self):
# gh-158232: _compare_grouped_stats() used to pop from old_group,
# silently draining the caller's dict.
tb_a = traceback(('f.py', 1))
tb_b = traceback(('f.py', 2))
tb_c = traceback(('f.py', 3))
old = {tb_a: tracemalloc.Statistic(tb_a, 100, 2),
tb_b: tracemalloc.Statistic(tb_b, 50, 1)}
new = {tb_a: tracemalloc.Statistic(tb_a, 200, 3),
tb_c: tracemalloc.Statistic(tb_c, 10, 1)}
old_copy = dict(old)
tracemalloc._compare_grouped_stats(old, new)
self.assertEqual(old, old_copy)

I'd prefer we drop the test. The name is wrong, it's placed oddly, and it refers to a different issue. It also tests an implementation detail.

@StanFromIreland StanFromIreland changed the title gh-158234: Fix _compare_grouped_stats mutating old_group and a typo gh-158234: Fix typo in tracemalloc cumulative mode error message Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants