Commit a266e219 authored by Georg Brandl's avatar Georg Brandl

#10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a...

#10166: rewrite self-recursion to iteration in pstats.Stats.add().  Also add a unittest and a stats test file.
parent e0039f91
...@@ -104,7 +104,9 @@ class Stats: ...@@ -104,7 +104,9 @@ class Stats:
print(file=self.stream) print(file=self.stream)
def load_stats(self, arg): def load_stats(self, arg):
if not arg: self.stats = {} if arg is None:
self.stats = {}
return
elif isinstance(arg, str): elif isinstance(arg, str):
f = open(arg, 'rb') f = open(arg, 'rb')
self.stats = marshal.load(f) self.stats = marshal.load(f)
...@@ -114,13 +116,13 @@ class Stats: ...@@ -114,13 +116,13 @@ class Stats:
arg = time.ctime(file_stats.st_mtime) + " " + arg arg = time.ctime(file_stats.st_mtime) + " " + arg
except: # in case this is not unix except: # in case this is not unix
pass pass
self.files = [ arg ] self.files = [arg]
elif hasattr(arg, 'create_stats'): elif hasattr(arg, 'create_stats'):
arg.create_stats() arg.create_stats()
self.stats = arg.stats self.stats = arg.stats
arg.stats = {} arg.stats = {}
if not self.stats: if not self.stats:
raise TypeError("Cannot create or construct a %r object from '%r''" raise TypeError("Cannot create or construct a %r object from %r"
% (self.__class__, arg)) % (self.__class__, arg))
return return
...@@ -135,29 +137,29 @@ class Stats: ...@@ -135,29 +137,29 @@ class Stats:
self.max_name_len = len(func_std_string(func)) self.max_name_len = len(func_std_string(func))
def add(self, *arg_list): def add(self, *arg_list):
if not arg_list: return self if not arg_list:
if len(arg_list) > 1: self.add(*arg_list[1:]) return self
other = arg_list[0] for item in reversed(arg_list):
if type(self) != type(other): if type(self) != type(item):
other = Stats(other) item = Stats(item)
self.files += other.files self.files += item.files
self.total_calls += other.total_calls self.total_calls += item.total_calls
self.prim_calls += other.prim_calls self.prim_calls += item.prim_calls
self.total_tt += other.total_tt self.total_tt += item.total_tt
for func in other.top_level: for func in item.top_level:
self.top_level[func] = None self.top_level[func] = None
if self.max_name_len < other.max_name_len:
self.max_name_len = other.max_name_len
self.fcn_list = None if self.max_name_len < item.max_name_len:
self.max_name_len = item.max_name_len
for func, stat in other.stats.items(): self.fcn_list = None
if func in self.stats:
old_func_stat = self.stats[func] for func, stat in item.stats.items():
else: if func in self.stats:
old_func_stat = (0, 0, 0, 0, {},) old_func_stat = self.stats[func]
self.stats[func] = add_func_stats(old_func_stat, stat) else:
old_func_stat = (0, 0, 0, 0, {},)
self.stats[func] = add_func_stats(old_func_stat, stat)
return self return self
def dump_stats(self, filename): def dump_stats(self, filename):
......
This diff was suppressed by a .gitattributes entry.
import unittest import unittest
from test import support from test import support
from io import StringIO
import pstats import pstats
...@@ -8,8 +9,8 @@ class AddCallersTestCase(unittest.TestCase): ...@@ -8,8 +9,8 @@ class AddCallersTestCase(unittest.TestCase):
"""Tests for pstats.add_callers helper.""" """Tests for pstats.add_callers helper."""
def test_combine_results(self): def test_combine_results(self):
"""pstats.add_callers should combine the call results of both target # pstats.add_callers should combine the call results of both target
and source by adding the call time. See issue1269.""" # and source by adding the call time. See issue1269.
# new format: used by the cProfile module # new format: used by the cProfile module
target = {"a": (1, 2, 3, 4)} target = {"a": (1, 2, 3, 4)}
source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)} source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
...@@ -22,9 +23,21 @@ class AddCallersTestCase(unittest.TestCase): ...@@ -22,9 +23,21 @@ class AddCallersTestCase(unittest.TestCase):
self.assertEqual(new_callers, {'a': 2, 'b': 5}) self.assertEqual(new_callers, {'a': 2, 'b': 5})
class StatsTestCase(unittest.TestCase):
def setUp(self):
stats_file = support.findfile('pstats.pck')
self.stats = pstats.Stats(stats_file)
def test_add(self):
stream = StringIO()
stats = pstats.Stats(stream=stream)
stats.add(self.stats, self.stats)
def test_main(): def test_main():
support.run_unittest( support.run_unittest(
AddCallersTestCase AddCallersTestCase,
StatsTestCase,
) )
......
...@@ -43,6 +43,8 @@ Core and Builtins ...@@ -43,6 +43,8 @@ Core and Builtins
Library Library
------- -------
- Issue #10166: Avoid recursion in pstats Stats.add() for many stats items.
- Issue #10163: Skip unreadable registry keys during mimetypes - Issue #10163: Skip unreadable registry keys during mimetypes
initialization. initialization.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment