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
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:
I use ChatGPT to see a quick and easy way to fix this.
Step 1: Define a new structure UniqueFunctionName
classUniqueFunctionName:
""" A class to uniquely represent a function name with its file name and line number. """def__init__(self, func_name, file_name, line_number):
self.func_name=func_nameself.file_name=file_nameself.line_number=line_numberdefto_str(self):
""" Converts the unique function name to a string representation. Example: "function_name (filename.py:42)" """returnf"{self.func_name} ({self.file_name}:{self.line_number})"@classmethoddeffrom_str(cls, unique_str):
""" Parses a string representation to recreate a UniqueFunctionName instance. Example input: "function_name (filename.py:42)" """try:
func_name, location=unique_str.split(" (")
location=location.rstrip(")")
file_name, line_number=location.split(":")
returncls(func_name, file_name, int(line_number))
exceptValueError:
raiseValueError(f"Invalid format for unique function name: {unique_str}")
def__repr__(self):
returnf"UniqueFunctionName(func_name='{self.func_name}', file_name='{self.file_name}', line_number={self.line_number})"
Step 2: Use the new structure in the func_profiles map
@skmendez You down to take this on? I think going through the cpython contribution process teaches a lot about OSS contributions. It'll also uncover the last (hardest) 10% of pushing this over the finish line.
Bug report
Bug description:
MCVE
when I ran this in a jupyter notebook, this outputted:
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 dictFunctionProfile.func_profiles
, the one which is last in the insertion order, which in this case since I sorted bycumtime
will be A.foo:outputs:
Maybe
get_stats_profile
should key thefunc_profile
dictionary on something that's actually unique per profile entry, likefilename:lineno(function)
?CPython versions tested on:
3.10
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: