python / python/cpython

pstats.Stats.get_stats_profile can't handle functions with the same name

Open
#126,850 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

MCVE

import cProfile
import time
import pstats


class A:
    def foo(self):
        time.sleep(1)


class B:
    def foo(self):
        time.sleep(2)


pr = cProfile.Profile()
pr.enable()
A().foo()
B().foo()
pr.create_stats()

ps = pstats.Stats(pr).sort_stats("cumtime")
ps.print_stats(5)

when I ran this in a jupyter notebook, this outputted:

         61 function calls in 3.003 seconds

   Ordered by: cumulative time
   List reduced from 21 to 5 due to restriction <5>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        3    0.000    0.000    3.003    1.001 /usr/local/my_venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3424(run_code)
        3    0.000    0.000    3.002    1.001 {built-in method builtins.exec}
        2    3.002    1.501    3.002    1.501 {built-in method time.sleep}
        1    0.000    0.000    2.002    2.002 /usr/tmp/ipykernel_104016/1099215389.py:12(foo)
        1    0.000    0.000    1.000    1.000 /usr/tmp/ipykernel_104016/1099215389.py:7(foo)

which correctly shows two different versions of a function called foo. However, due to the API design of get_stats_profile, only one of the functions will be stored in the dict FunctionProfile.func_profiles, the one which is last in the insertion order, which in this case since I sorted by cumtime will be A.foo:

print(ps.get_stats_profile().func_profiles["foo"])

outputs:

FunctionProfile(ncalls='1', tottime=0.0, percall_tottime=0.0, cumtime=1.0, percall_cumtime=1.0, file_name='/usr/tmp/ipykernel_104016/1099215389.py', line_number=7)

Maybe get_stats_profile should key the func_profile dictionary on something that's actually unique per profile entry, like filename:lineno(function)?

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Linked PRs
  • gh-149618

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at pstats.Stats.get_stats_profile and inspect how FunctionProfile.func_profiles is populated and keyed. Reproduce the provided MCVE with A.foo and B.foo, then verify that both same-named functions remain distinguishable in the returned profile data.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.