1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import sys
import threading
import time
import unittest
from test import test_support
class UntracedThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
sys.settrace(None)
for i in range(10):
self.untracedcall()
def untracedcall(self):
pass
class TracedThread(threading.Thread):
def __init__(self, on_trace):
threading.Thread.__init__(self)
self.on_trace = on_trace
def trace(self, frame, event, arg):
self.on_trace(frame.f_code.co_name)
def tracedcall(self):
pass
def run(self):
sys.settrace(self.trace)
for i in range(10):
self.tracedcall()
class TracePerThreadTest(unittest.TestCase):
def testTracePerThread(self):
called = []
def ontrace(co_name):
called.append(str(co_name))
untraced = UntracedThread()
traced = TracedThread(ontrace)
untraced.start()
traced.start()
untraced.join()
traced.join()
self.assertEquals(10, called.count('tracedcall'),
"10 tracedcall should be in %s" % called)
self.assert_('untracedcall' not in called,
"untracedcall shouldn't be in %s" % called)
def test_main():
test_support.run_unittest(TracePerThreadTest)
if __name__ == "__main__":
test_main()
|