1import unittest 2 3from test import support 4from io import StringIO 5from pstats import SortKey 6from enum import StrEnum, _test_simple_enum 7 8import os 9import pstats 10import tempfile 11import cProfile 12 13class AddCallersTestCase(unittest.TestCase): 14 """Tests for pstats.add_callers helper.""" 15 16 def test_combine_results(self): 17 # pstats.add_callers should combine the call results of both target 18 # and source by adding the call time. See issue1269. 19 # new format: used by the cProfile module 20 target = {"a": (1, 2, 3, 4)} 21 source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)} 22 new_callers = pstats.add_callers(target, source) 23 self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)}) 24 # old format: used by the profile module 25 target = {"a": 1} 26 source = {"a": 1, "b": 5} 27 new_callers = pstats.add_callers(target, source) 28 self.assertEqual(new_callers, {'a': 2, 'b': 5}) 29 30 31class StatsTestCase(unittest.TestCase): 32 def setUp(self): 33 stats_file = support.findfile('pstats.pck') 34 self.stats = pstats.Stats(stats_file) 35 36 def test_add(self): 37 stream = StringIO() 38 stats = pstats.Stats(stream=stream) 39 stats.add(self.stats, self.stats) 40 41 def test_dump_and_load_works_correctly(self): 42 temp_storage_new = tempfile.NamedTemporaryFile(delete=False) 43 try: 44 self.stats.dump_stats(filename=temp_storage_new.name) 45 tmp_stats = pstats.Stats(temp_storage_new.name) 46 self.assertEqual(self.stats.stats, tmp_stats.stats) 47 finally: 48 temp_storage_new.close() 49 os.remove(temp_storage_new.name) 50 51 def test_load_equivalent_to_init(self): 52 stats = pstats.Stats() 53 self.temp_storage = tempfile.NamedTemporaryFile(delete=False) 54 try: 55 cProfile.run('import os', filename=self.temp_storage.name) 56 stats.load_stats(self.temp_storage.name) 57 created = pstats.Stats(self.temp_storage.name) 58 self.assertEqual(stats.stats, created.stats) 59 finally: 60 self.temp_storage.close() 61 os.remove(self.temp_storage.name) 62 63 def test_loading_wrong_types(self): 64 stats = pstats.Stats() 65 with self.assertRaises(TypeError): 66 stats.load_stats(42) 67 68 def test_sort_stats_int(self): 69 valid_args = {-1: 'stdname', 70 0: 'calls', 71 1: 'time', 72 2: 'cumulative'} 73 for arg_int, arg_str in valid_args.items(): 74 self.stats.sort_stats(arg_int) 75 self.assertEqual(self.stats.sort_type, 76 self.stats.sort_arg_dict_default[arg_str][-1]) 77 78 def test_sort_stats_string(self): 79 for sort_name in ['calls', 'ncalls', 'cumtime', 'cumulative', 80 'filename', 'line', 'module', 'name', 'nfl', 'pcalls', 81 'stdname', 'time', 'tottime']: 82 self.stats.sort_stats(sort_name) 83 self.assertEqual(self.stats.sort_type, 84 self.stats.sort_arg_dict_default[sort_name][-1]) 85 86 def test_sort_stats_partial(self): 87 sortkey = 'filename' 88 for sort_name in ['f', 'fi', 'fil', 'file', 'filen', 'filena', 89 'filenam', 'filename']: 90 self.stats.sort_stats(sort_name) 91 self.assertEqual(self.stats.sort_type, 92 self.stats.sort_arg_dict_default[sortkey][-1]) 93 94 def test_sort_stats_enum(self): 95 for member in SortKey: 96 self.stats.sort_stats(member) 97 self.assertEqual( 98 self.stats.sort_type, 99 self.stats.sort_arg_dict_default[member.value][-1]) 100 class CheckedSortKey(StrEnum): 101 CALLS = 'calls', 'ncalls' 102 CUMULATIVE = 'cumulative', 'cumtime' 103 FILENAME = 'filename', 'module' 104 LINE = 'line' 105 NAME = 'name' 106 NFL = 'nfl' 107 PCALLS = 'pcalls' 108 STDNAME = 'stdname' 109 TIME = 'time', 'tottime' 110 def __new__(cls, *values): 111 value = values[0] 112 obj = str.__new__(cls, value) 113 obj._value_ = value 114 for other_value in values[1:]: 115 cls._value2member_map_[other_value] = obj 116 obj._all_values = values 117 return obj 118 _test_simple_enum(CheckedSortKey, SortKey) 119 120 def test_sort_starts_mix(self): 121 self.assertRaises(TypeError, self.stats.sort_stats, 122 'calls', 123 SortKey.TIME) 124 self.assertRaises(TypeError, self.stats.sort_stats, 125 SortKey.TIME, 126 'calls') 127 128 def test_get_stats_profile(self): 129 def pass1(): pass 130 def pass2(): pass 131 def pass3(): pass 132 133 pr = cProfile.Profile() 134 pr.enable() 135 pass1() 136 pass2() 137 pass3() 138 pr.create_stats() 139 ps = pstats.Stats(pr) 140 141 stats_profile = ps.get_stats_profile() 142 funcs_called = set(stats_profile.func_profiles.keys()) 143 self.assertIn('pass1', funcs_called) 144 self.assertIn('pass2', funcs_called) 145 self.assertIn('pass3', funcs_called) 146 147 def test_SortKey_enum(self): 148 self.assertEqual(SortKey.FILENAME, 'filename') 149 self.assertNotEqual(SortKey.FILENAME, SortKey.CALLS) 150 151if __name__ == "__main__": 152 unittest.main() 153