Skip to content

gh-126850: Handle multiple functions with same name in StatsProfile#149618

Open
ejthomas wants to merge 4 commits intopython:mainfrom
ejthomas:stats-profile-handle-name-clashes
Open

gh-126850: Handle multiple functions with same name in StatsProfile#149618
ejthomas wants to merge 4 commits intopython:mainfrom
ejthomas:stats-profile-handle-name-clashes

Conversation

@ejthomas
Copy link
Copy Markdown

@ejthomas ejthomas commented May 9, 2026

Adds a new attribute func_profile_by_loc to the StatsProfile class in pstats.py that provides access to the function profiles keyed by a tuple of filename, linenumber and function name (rather than just function name as used in func_profiles), so that profiling data for multiple functions with the same name can be accessed via the StatsProfile data structure.

A new test is added to highlight the new behaviour in test_pstats.py.

A new attribute is used instead of changing the existing behavior for backwards compatibility (though I'm aware this is very similar to the existing attribute, I think this edge case not handled in func_profiles is relatively common when profiling more complex code where certain common names like run() or build() are likely to appear multiple times).

I considered making the key a more formal data structure with named attributes like a frozen dataclass or namedtuple, but when looking at the surrounding code, it looked like a simple tuple was a good fit. I gave it an alias to clarify intent, but I'd be very happy to use an alternative approach if that would improve readability further.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 9, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 9, 2026

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.

@picnixz picnixz changed the title gh-126850 Handle multiple functions with same name in StatsProfile gh-126850: Handle multiple functions with same name in StatsProfile May 9, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 9, 2026

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.

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.

1 participant