move sections
[python/dscho.git] / Lib / test / test_hotshot.py
blobc6dae363cdcb90ed29642e7dacb90ebc9b873bf5
1 import os
2 import pprint
3 import unittest
4 import tempfile
5 import _hotshot
6 import gc
8 from test import test_support
10 # Silence Py3k warning
11 hotshot = test_support.import_module('hotshot', deprecated=True)
12 from hotshot.log import ENTER, EXIT, LINE
15 def shortfilename(fn):
16 # We use a really shortened filename since an exact match is made,
17 # and the source may be either a Python source file or a
18 # pre-compiled bytecode file.
19 if fn:
20 return os.path.splitext(os.path.basename(fn))[0]
21 else:
22 return fn
25 class UnlinkingLogReader(hotshot.log.LogReader):
26 """Extend the LogReader so the log file is unlinked when we're
27 done with it."""
29 def __init__(self, logfn):
30 self.__logfn = logfn
31 hotshot.log.LogReader.__init__(self, logfn)
33 def next(self, index=None):
34 try:
35 return hotshot.log.LogReader.next(self)
36 except StopIteration:
37 self.close()
38 os.unlink(self.__logfn)
39 raise
42 class HotShotTestCase(unittest.TestCase):
43 def new_profiler(self, lineevents=0, linetimings=1):
44 self.logfn = test_support.TESTFN
45 return hotshot.Profile(self.logfn, lineevents, linetimings)
47 def get_logreader(self):
48 return UnlinkingLogReader(self.logfn)
50 def get_events_wotime(self):
51 L = []
52 for event in self.get_logreader():
53 what, (filename, lineno, funcname), tdelta = event
54 L.append((what, (shortfilename(filename), lineno, funcname)))
55 return L
57 def check_events(self, expected):
58 events = self.get_events_wotime()
59 if events != expected:
60 self.fail(
61 "events did not match expectation; got:\n%s\nexpected:\n%s"
62 % (pprint.pformat(events), pprint.pformat(expected)))
64 def run_test(self, callable, events, profiler=None):
65 if profiler is None:
66 profiler = self.new_profiler()
67 self.assertTrue(not profiler._prof.closed)
68 profiler.runcall(callable)
69 self.assertTrue(not profiler._prof.closed)
70 profiler.close()
71 self.assertTrue(profiler._prof.closed)
72 self.check_events(events)
74 def test_addinfo(self):
75 def f(p):
76 p.addinfo("test-key", "test-value")
77 profiler = self.new_profiler()
78 profiler.runcall(f, profiler)
79 profiler.close()
80 log = self.get_logreader()
81 info = log._info
82 list(log)
83 self.assertTrue(info["test-key"] == ["test-value"])
85 def test_line_numbers(self):
86 def f():
87 y = 2
88 x = 1
89 def g():
90 f()
91 f_lineno = f.func_code.co_firstlineno
92 g_lineno = g.func_code.co_firstlineno
93 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
94 (LINE, ("test_hotshot", g_lineno+1, "g")),
95 (ENTER, ("test_hotshot", f_lineno, "f")),
96 (LINE, ("test_hotshot", f_lineno+1, "f")),
97 (LINE, ("test_hotshot", f_lineno+2, "f")),
98 (EXIT, ("test_hotshot", f_lineno, "f")),
99 (EXIT, ("test_hotshot", g_lineno, "g")),
101 self.run_test(g, events, self.new_profiler(lineevents=1))
103 def test_start_stop(self):
104 # Make sure we don't return NULL in the start() and stop()
105 # methods when there isn't an error. Bug in 2.2 noted by
106 # Anthony Baxter.
107 profiler = self.new_profiler()
108 profiler.start()
109 profiler.stop()
110 profiler.close()
111 os.unlink(self.logfn)
113 def test_bad_sys_path(self):
114 import sys
115 import os
116 orig_path = sys.path
117 coverage = hotshot._hotshot.coverage
118 try:
119 # verify we require a list for sys.path
120 sys.path = 'abc'
121 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
122 # verify that we require sys.path exists
123 del sys.path
124 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
125 finally:
126 sys.path = orig_path
127 if os.path.exists(test_support.TESTFN):
128 os.remove(test_support.TESTFN)
130 def test_logreader_eof_error(self):
131 emptyfile = tempfile.NamedTemporaryFile()
132 try:
133 self.assertRaises((IOError, EOFError), _hotshot.logreader,
134 emptyfile.name)
135 finally:
136 emptyfile.close()
137 gc.collect()
139 def test_main():
140 test_support.run_unittest(HotShotTestCase)
143 if __name__ == "__main__":
144 test_main()