]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-69990: Make Profile.print_stats support sorting by multiple values (GH-104590)
authorFurkan Onder <furkanonder@protonmail.com>
Fri, 16 Feb 2024 12:03:46 +0000 (15:03 +0300)
committerGitHub <noreply@github.com>
Fri, 16 Feb 2024 12:03:46 +0000 (12:03 +0000)
Co-authored-by: Chiu-Hsiang Hsu
Doc/library/profile.rst
Lib/cProfile.py
Lib/profile.py
Lib/test/test_profile.py
Misc/NEWS.d/next/Library/2023-05-17-21-33-21.gh-issue-69990.Blvz9G.rst [new file with mode: 0644]

index cc059b66fcb84b5df9baafd11d828c60a52e4de4..3ca802e024bc27f3d081cde3321a91aaea72b7c6 100644 (file)
@@ -299,6 +299,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 key or a tuple of keys to enable
+      multi-level sorting, as in :func:`Stats.sort_stats <pstats.Stats.sort_stats>`.
+
+      .. versionadded:: 3.13
+         :meth:`~Profile.print_stats` now accepts a tuple of keys.
+
    .. method:: dump_stats(filename)
 
       Write the results of the current profile to *filename*.
index 135a12c3965c00e8d0afe984b5f9f099cc6c2db0..9c132372dc4ee0497436d45a711426754b807766 100755 (executable)
@@ -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 isinstance(sort, tuple):
+            sort = (sort,)
+        pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
 
     def dump_stats(self, file):
         import marshal
index 4b82523b03d64b08eee4ba212a4d53286ee69a07..f2f8c2f21333e0f64312ff5bf563348058b8357b 100755 (executable)
@@ -387,8 +387,9 @@ class Profile:
 
     def print_stats(self, sort=-1):
         import pstats
-        pstats.Stats(self).strip_dirs().sort_stats(sort). \
-                  print_stats()
+        if not isinstance(sort, tuple):
+            sort = (sort,)
+        pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
 
     def dump_stats(self, file):
         with open(file, 'wb') as f:
index a1dfc9abbb8ef7563b6e3a561368e04aa617e30e..0f16b92334999ce8467a0902cdbbfbc8013f3e96 100644 (file)
@@ -7,7 +7,7 @@ import os
 from difflib import unified_diff
 from io import StringIO
 from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
-from contextlib import contextmanager
+from contextlib import contextmanager, redirect_stdout
 
 import profile
 from test.profilee import testfunc, timer
@@ -92,6 +92,11 @@ class ProfileTest(unittest.TestCase):
         self.profilermodule.run("int('1')", filename=TESTFN)
         self.assertTrue(os.path.exists(TESTFN))
 
+    def test_run_with_sort_by_values(self):
+        with redirect_stdout(StringIO()) as f:
+            self.profilermodule.run("int('1')", sort=('tottime', 'stdname'))
+        self.assertIn("Ordered by: internal time, standard name", f.getvalue())
+
     def test_runctx(self):
         with silent():
             self.profilermodule.runctx("testfunc()", globals(), locals())
diff --git a/Misc/NEWS.d/next/Library/2023-05-17-21-33-21.gh-issue-69990.Blvz9G.rst b/Misc/NEWS.d/next/Library/2023-05-17-21-33-21.gh-issue-69990.Blvz9G.rst
new file mode 100644 (file)
index 0000000..b0cdf44
--- /dev/null
@@ -0,0 +1 @@
+:meth:`Profile.print_stats` has been improved to accept multiple sort arguments. Patched by Chiu-Hsiang Hsu and Furkan Onder.