Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
212b668
Make Profile.print_stats support sorting by mutiple values
furkanonder May 17, 2023
da1b596
📜🤖 Added by blurb_it.
blurb-it[bot] May 17, 2023
1ac24af
Merge branch 'main' into issue-69990
furkanonder May 17, 2023
9f18231
Merge branch 'main' into issue-69990
furkanonder May 25, 2023
f771358
update documentation according to the new feature
furkanonder Jan 28, 2024
1d8c4e4
fix code format
furkanonder Jan 28, 2024
82facb0
update the news entry
furkanonder Jan 28, 2024
80bb65b
fix code format
furkanonder Jan 29, 2024
0253706
update documentation according to the new feature
furkanonder Jan 29, 2024
58c7e35
update the news entry
furkanonder Jan 29, 2024
1905e3f
fix merge conflict
furkanonder Feb 10, 2024
fb4b8cb
use contextlib's redirect_stdout
furkanonder Feb 10, 2024
66e1e82
use isinstance check for the sorting tuple
furkanonder Feb 10, 2024
dfa668d
update documentation of print_stats method
furkanonder Feb 10, 2024
96b1bdd
remove whitespace
furkanonder Feb 10, 2024
e1a70e0
Merge branch 'main' into issue-69990
furkanonder Feb 10, 2024
f6b235f
Update Doc/library/profile.rst
furkanonder Feb 10, 2024
bb0507b
Update Doc/library/profile.rst
furkanonder Feb 10, 2024
4cccb55
Merge branch 'main' into issue-69990
furkanonder Feb 10, 2024
b083716
fix the syntax of the documentation grammar in print_stats
furkanonder Feb 10, 2024
7fdbfab
clarify valid key types in print_stats method
furkanonder Feb 10, 2024
49ca018
clarify valid key types in print_stats method
furkanonder Feb 13, 2024
4ea5e4c
update versionadded section
furkanonder Feb 13, 2024
9978802
Update Doc/library/profile.rst
serhiy-storchaka Feb 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ functions:
Create a :class:`~pstats.Stats` object based on the current
profile and print the results to stdout.

The *sort* parameter specifies the sorting order of the displayed
statistics. It accepts a single sort key or a tuple of sort keys
to enable multi-level sorting.

.. versionadded:: 3.13
:meth:`~Profile.print_stats` can now accept multiple sort arguments.
Copy link
Member

Choose a reason for hiding this comment

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

"can accept multiple sort arguments" looks confusing. Does print_stats('calls', 'time') or print_stats(sort='calls', sort='time') work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example:

import cProfile
from pstats import SortKey

with cProfile.Profile() as pr:
    pr.print_stats(sort=("time", "calls"))  # or pr.print_stats(sort=(SortKey.TIME, SortKey.CALLS))

I believe that adding a small example to the documentation would make it easier to understand the feature. What do you think?


.. method:: dump_stats(filename)

Write the results of the current profile to *filename*.
Expand Down
4 changes: 3 additions & 1 deletion Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class Profile(_lsprof.Profiler):

def print_stats(self, sort=-1):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
if not hasattr(sort, "__iter__") or isinstance(sort, str):
sort = (sort,)
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()

def dump_stats(self, file):
import marshal
Expand Down
5 changes: 3 additions & 2 deletions Lib/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ def simulate_cmd_complete(self):

def print_stats(self, sort=-1):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort). \
print_stats()
if not hasattr(sort, "__iter__") or isinstance(sort, str):
sort = (sort,)
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()

def dump_stats(self, file):
with open(file, 'wb') as f:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def test_run(self):
self.profilermodule.run("int('1')", filename=TESTFN)
self.assertTrue(os.path.exists(TESTFN))

def test_run_with_sort_by_values(self):
s = StringIO()
stdout = sys.stdout
sys.stdout = s
self.profilermodule.run("int('1')", sort=('tottime', 'stdname'))
sys.stdout = stdout
self.assertIn("Ordered by: internal time, standard name", s.getvalue())

def test_runctx(self):
with silent():
self.profilermodule.runctx("testfunc()", globals(), locals())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`Profile.print_stats` has been improved to accept multiple sort arguments. Patched by Chiu-Hsiang Hsu and Furkan Onder.