summaryrefslogtreecommitdiffstats
path: root/Lib/pstats.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-10-22 06:28:01 (GMT)
committerGeorg Brandl <georg@python.org>2010-10-22 06:28:01 (GMT)
commit83938437cb74418ba45d00a99d02c6e095110c60 (patch)
tree543eed563062c83c1ea1c202047a69be42c90131 /Lib/pstats.py
parent68fc9aa318788344d36e72a0347ce967d6156f09 (diff)
downloadcpython-83938437cb74418ba45d00a99d02c6e095110c60.zip
cpython-83938437cb74418ba45d00a99d02c6e095110c60.tar.gz
cpython-83938437cb74418ba45d00a99d02c6e095110c60.tar.bz2
#10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Diffstat (limited to 'Lib/pstats.py')
-rw-r--r--Lib/pstats.py50
1 files changed, 26 insertions, 24 deletions
diff --git a/Lib/pstats.py b/Lib/pstats.py
index 4bb5cf1..8744235 100644
--- a/Lib/pstats.py
+++ b/Lib/pstats.py
@@ -104,7 +104,9 @@ class Stats:
print(file=self.stream)
def load_stats(self, arg):
- if not arg: self.stats = {}
+ if arg is None:
+ self.stats = {}
+ return
elif isinstance(arg, str):
f = open(arg, 'rb')
self.stats = marshal.load(f)
@@ -114,13 +116,13 @@ class Stats:
arg = time.ctime(file_stats.st_mtime) + " " + arg
except: # in case this is not unix
pass
- self.files = [ arg ]
+ self.files = [arg]
elif hasattr(arg, 'create_stats'):
arg.create_stats()
self.stats = arg.stats
arg.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))
return
@@ -135,29 +137,29 @@ class Stats:
self.max_name_len = len(func_std_string(func))
def add(self, *arg_list):
- if not arg_list: return self
- if len(arg_list) > 1: self.add(*arg_list[1:])
- other = arg_list[0]
- if type(self) != type(other):
- other = Stats(other)
- self.files += other.files
- self.total_calls += other.total_calls
- self.prim_calls += other.prim_calls
- self.total_tt += other.total_tt
- for func in other.top_level:
- self.top_level[func] = None
-
- if self.max_name_len < other.max_name_len:
- self.max_name_len = other.max_name_len
+ if not arg_list:
+ return self
+ for item in reversed(arg_list):
+ if type(self) != type(item):
+ item = Stats(item)
+ self.files += item.files
+ self.total_calls += item.total_calls
+ self.prim_calls += item.prim_calls
+ self.total_tt += item.total_tt
+ for func in item.top_level:
+ self.top_level[func] = None
- 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():
- if func in self.stats:
- old_func_stat = self.stats[func]
- else:
- old_func_stat = (0, 0, 0, 0, {},)
- self.stats[func] = add_func_stats(old_func_stat, stat)
+ self.fcn_list = None
+
+ for func, stat in item.stats.items():
+ if func in self.stats:
+ old_func_stat = self.stats[func]
+ else:
+ old_func_stat = (0, 0, 0, 0, {},)
+ self.stats[func] = add_func_stats(old_func_stat, stat)
return self
def dump_stats(self, filename):