diff --git a/Lib/pstats.py b/Lib/pstats.py index 07ecda07796e44..b599d95909fdb1 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -150,6 +150,7 @@ def load_stats(self, arg): except: # in case this is not unix pass self.files = [arg] + return elif hasattr(arg, 'create_stats'): arg.create_stats() self.stats = arg.stats diff --git a/Lib/test/test_pstats.py b/Lib/test/test_pstats.py index a26a8c1d522a70..23070902493ea5 100644 --- a/Lib/test/test_pstats.py +++ b/Lib/test/test_pstats.py @@ -45,6 +45,18 @@ def test_add(self): stats = pstats.Stats(stream=stream) stats.add(self.stats, self.stats) + def test_empty_stats(self): + # gh-109500: Empty profiling data must not prevent loading or + # combining statistics. + stats_file = support.findfile('pstats.pck') + profile = cProfile.Profile() + with tempfile.TemporaryDirectory() as tmpdir: + filename = os.path.join(tmpdir, 'empty.prof') + profile.dump_stats(filename) + self.assertEqual(pstats.Stats(profile, filename).stats, {}) + stats = pstats.Stats(filename, stats_file) + self.assertEqual(stats.stats, self.stats.stats) + def test_dump_and_load_works_correctly(self): temp_storage_new = tempfile.NamedTemporaryFile(delete=False) try: diff --git a/Misc/NEWS.d/next/Library/2026-09-28-20-09-02.gh-issue-109500._T9cBe.rst b/Misc/NEWS.d/next/Library/2026-09-28-20-09-02.gh-issue-109500._T9cBe.rst new file mode 100644 index 00000000000000..d7440bedd885f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-28-20-09-02.gh-issue-109500._T9cBe.rst @@ -0,0 +1,2 @@ +Fix :class:`pstats.Stats` and :meth:`pstats.Stats.add` raising +:exc:`TypeError` when loading files containing empty profiling data.