Skip to content

Commit 2fd3abb

Browse files
committed
gh-109500: Allow pstats to load files with empty profiling data
1 parent 1e8ff18 commit 2fd3abb

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎Lib/pstats.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def load_stats(self, arg):
150150
except: # in case this is not unix
151151
pass
152152
self.files = [arg]
153+
return
153154
elif hasattr(arg, 'create_stats'):
154155
arg.create_stats()
155156
self.stats = arg.stats

‎Lib/test/test_pstats.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ def test_add(self):
4545
stats = pstats.Stats(stream=stream)
4646
stats.add(self.stats, self.stats)
4747

48+
def test_empty_stats(self):
49+
# gh-109500: Empty profiling data must not prevent loading or
50+
# combining statistics.
51+
stats_file = support.findfile('pstats.pck')
52+
profile = cProfile.Profile()
53+
with tempfile.TemporaryDirectory() as tmpdir:
54+
filename = os.path.join(tmpdir, 'empty.prof')
55+
profile.dump_stats(filename)
56+
self.assertEqual(pstats.Stats(profile, filename).stats, {})
57+
stats = pstats.Stats(filename, stats_file)
58+
self.assertEqual(stats.stats, self.stats.stats)
59+
4860
def test_dump_and_load_works_correctly(self):
4961
temp_storage_new = tempfile.NamedTemporaryFile(delete=False)
5062
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`pstats.Stats` and :meth:`pstats.Stats.add` raising
2+
:exc:`TypeError` when loading files containing empty profiling data.

0 commit comments

Comments
 (0)