Skip to content

Commit 2a7a002

Browse files
authored
gh-69990: Make Profile.print_stats support sorting by multiple values (GH-104590)
Co-authored-by: Chiu-Hsiang Hsu
1 parent 351c103 commit 2a7a002

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

Doc/library/profile.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ functions:
299299
Create a :class:`~pstats.Stats` object based on the current
300300
profile and print the results to stdout.
301301

302+
The *sort* parameter specifies the sorting order of the displayed
303+
statistics. It accepts a single key or a tuple of keys to enable
304+
multi-level sorting, as in :func:`Stats.sort_stats <pstats.Stats.sort_stats>`.
305+
306+
.. versionadded:: 3.13
307+
:meth:`~Profile.print_stats` now accepts a tuple of keys.
308+
302309
.. method:: dump_stats(filename)
303310

304311
Write the results of the current profile to *filename*.

Lib/cProfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class Profile(_lsprof.Profiler):
4141

4242
def print_stats(self, sort=-1):
4343
import pstats
44-
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
44+
if not isinstance(sort, tuple):
45+
sort = (sort,)
46+
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
4547

4648
def dump_stats(self, file):
4749
import marshal

Lib/profile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,9 @@ def simulate_cmd_complete(self):
387387

388388
def print_stats(self, sort=-1):
389389
import pstats
390-
pstats.Stats(self).strip_dirs().sort_stats(sort). \
391-
print_stats()
390+
if not isinstance(sort, tuple):
391+
sort = (sort,)
392+
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
392393

393394
def dump_stats(self, file):
394395
with open(file, 'wb') as f:

Lib/test/test_profile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from difflib import unified_diff
88
from io import StringIO
99
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
10-
from contextlib import contextmanager
10+
from contextlib import contextmanager, redirect_stdout
1111

1212
import profile
1313
from test.profilee import testfunc, timer
@@ -92,6 +92,11 @@ def test_run(self):
9292
self.profilermodule.run("int('1')", filename=TESTFN)
9393
self.assertTrue(os.path.exists(TESTFN))
9494

95+
def test_run_with_sort_by_values(self):
96+
with redirect_stdout(StringIO()) as f:
97+
self.profilermodule.run("int('1')", sort=('tottime', 'stdname'))
98+
self.assertIn("Ordered by: internal time, standard name", f.getvalue())
99+
95100
def test_runctx(self):
96101
with silent():
97102
self.profilermodule.runctx("testfunc()", globals(), locals())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`Profile.print_stats` has been improved to accept multiple sort arguments. Patched by Chiu-Hsiang Hsu and Furkan Onder.

0 commit comments

Comments
 (0)