1 | n/a | import os |
---|
2 | n/a | import sys |
---|
3 | n/a | from test.support import TESTFN, rmtree, unlink, captured_stdout |
---|
4 | n/a | from test.support.script_helper import assert_python_ok, assert_python_failure |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | import trace |
---|
8 | n/a | from trace import Trace |
---|
9 | n/a | |
---|
10 | n/a | from test.tracedmodules import testmod |
---|
11 | n/a | |
---|
12 | n/a | #------------------------------- Utilities -----------------------------------# |
---|
13 | n/a | |
---|
14 | n/a | def fix_ext_py(filename): |
---|
15 | n/a | """Given a .pyc filename converts it to the appropriate .py""" |
---|
16 | n/a | if filename.endswith('.pyc'): |
---|
17 | n/a | filename = filename[:-1] |
---|
18 | n/a | return filename |
---|
19 | n/a | |
---|
20 | n/a | def my_file_and_modname(): |
---|
21 | n/a | """The .py file and module name of this file (__file__)""" |
---|
22 | n/a | modname = os.path.splitext(os.path.basename(__file__))[0] |
---|
23 | n/a | return fix_ext_py(__file__), modname |
---|
24 | n/a | |
---|
25 | n/a | def get_firstlineno(func): |
---|
26 | n/a | return func.__code__.co_firstlineno |
---|
27 | n/a | |
---|
28 | n/a | #-------------------- Target functions for tracing ---------------------------# |
---|
29 | n/a | # |
---|
30 | n/a | # The relative line numbers of lines in these functions matter for verifying |
---|
31 | n/a | # tracing. Please modify the appropriate tests if you change one of the |
---|
32 | n/a | # functions. Absolute line numbers don't matter. |
---|
33 | n/a | # |
---|
34 | n/a | |
---|
35 | n/a | def traced_func_linear(x, y): |
---|
36 | n/a | a = x |
---|
37 | n/a | b = y |
---|
38 | n/a | c = a + b |
---|
39 | n/a | return c |
---|
40 | n/a | |
---|
41 | n/a | def traced_func_loop(x, y): |
---|
42 | n/a | c = x |
---|
43 | n/a | for i in range(5): |
---|
44 | n/a | c += y |
---|
45 | n/a | return c |
---|
46 | n/a | |
---|
47 | n/a | def traced_func_importing(x, y): |
---|
48 | n/a | return x + y + testmod.func(1) |
---|
49 | n/a | |
---|
50 | n/a | def traced_func_simple_caller(x): |
---|
51 | n/a | c = traced_func_linear(x, x) |
---|
52 | n/a | return c + x |
---|
53 | n/a | |
---|
54 | n/a | def traced_func_importing_caller(x): |
---|
55 | n/a | k = traced_func_simple_caller(x) |
---|
56 | n/a | k += traced_func_importing(k, x) |
---|
57 | n/a | return k |
---|
58 | n/a | |
---|
59 | n/a | def traced_func_generator(num): |
---|
60 | n/a | c = 5 # executed once |
---|
61 | n/a | for i in range(num): |
---|
62 | n/a | yield i + c |
---|
63 | n/a | |
---|
64 | n/a | def traced_func_calling_generator(): |
---|
65 | n/a | k = 0 |
---|
66 | n/a | for i in traced_func_generator(10): |
---|
67 | n/a | k += i |
---|
68 | n/a | |
---|
69 | n/a | def traced_doubler(num): |
---|
70 | n/a | return num * 2 |
---|
71 | n/a | |
---|
72 | n/a | def traced_caller_list_comprehension(): |
---|
73 | n/a | k = 10 |
---|
74 | n/a | mylist = [traced_doubler(i) for i in range(k)] |
---|
75 | n/a | return mylist |
---|
76 | n/a | |
---|
77 | n/a | |
---|
78 | n/a | class TracedClass(object): |
---|
79 | n/a | def __init__(self, x): |
---|
80 | n/a | self.a = x |
---|
81 | n/a | |
---|
82 | n/a | def inst_method_linear(self, y): |
---|
83 | n/a | return self.a + y |
---|
84 | n/a | |
---|
85 | n/a | def inst_method_calling(self, x): |
---|
86 | n/a | c = self.inst_method_linear(x) |
---|
87 | n/a | return c + traced_func_linear(x, c) |
---|
88 | n/a | |
---|
89 | n/a | @classmethod |
---|
90 | n/a | def class_method_linear(cls, y): |
---|
91 | n/a | return y * 2 |
---|
92 | n/a | |
---|
93 | n/a | @staticmethod |
---|
94 | n/a | def static_method_linear(y): |
---|
95 | n/a | return y * 2 |
---|
96 | n/a | |
---|
97 | n/a | |
---|
98 | n/a | #------------------------------ Test cases -----------------------------------# |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | class TestLineCounts(unittest.TestCase): |
---|
102 | n/a | """White-box testing of line-counting, via runfunc""" |
---|
103 | n/a | def setUp(self): |
---|
104 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
105 | n/a | self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
---|
106 | n/a | self.my_py_filename = fix_ext_py(__file__) |
---|
107 | n/a | |
---|
108 | n/a | def test_traced_func_linear(self): |
---|
109 | n/a | result = self.tracer.runfunc(traced_func_linear, 2, 5) |
---|
110 | n/a | self.assertEqual(result, 7) |
---|
111 | n/a | |
---|
112 | n/a | # all lines are executed once |
---|
113 | n/a | expected = {} |
---|
114 | n/a | firstlineno = get_firstlineno(traced_func_linear) |
---|
115 | n/a | for i in range(1, 5): |
---|
116 | n/a | expected[(self.my_py_filename, firstlineno + i)] = 1 |
---|
117 | n/a | |
---|
118 | n/a | self.assertEqual(self.tracer.results().counts, expected) |
---|
119 | n/a | |
---|
120 | n/a | def test_traced_func_loop(self): |
---|
121 | n/a | self.tracer.runfunc(traced_func_loop, 2, 3) |
---|
122 | n/a | |
---|
123 | n/a | firstlineno = get_firstlineno(traced_func_loop) |
---|
124 | n/a | expected = { |
---|
125 | n/a | (self.my_py_filename, firstlineno + 1): 1, |
---|
126 | n/a | (self.my_py_filename, firstlineno + 2): 6, |
---|
127 | n/a | (self.my_py_filename, firstlineno + 3): 5, |
---|
128 | n/a | (self.my_py_filename, firstlineno + 4): 1, |
---|
129 | n/a | } |
---|
130 | n/a | self.assertEqual(self.tracer.results().counts, expected) |
---|
131 | n/a | |
---|
132 | n/a | def test_traced_func_importing(self): |
---|
133 | n/a | self.tracer.runfunc(traced_func_importing, 2, 5) |
---|
134 | n/a | |
---|
135 | n/a | firstlineno = get_firstlineno(traced_func_importing) |
---|
136 | n/a | expected = { |
---|
137 | n/a | (self.my_py_filename, firstlineno + 1): 1, |
---|
138 | n/a | (fix_ext_py(testmod.__file__), 2): 1, |
---|
139 | n/a | (fix_ext_py(testmod.__file__), 3): 1, |
---|
140 | n/a | } |
---|
141 | n/a | |
---|
142 | n/a | self.assertEqual(self.tracer.results().counts, expected) |
---|
143 | n/a | |
---|
144 | n/a | def test_trace_func_generator(self): |
---|
145 | n/a | self.tracer.runfunc(traced_func_calling_generator) |
---|
146 | n/a | |
---|
147 | n/a | firstlineno_calling = get_firstlineno(traced_func_calling_generator) |
---|
148 | n/a | firstlineno_gen = get_firstlineno(traced_func_generator) |
---|
149 | n/a | expected = { |
---|
150 | n/a | (self.my_py_filename, firstlineno_calling + 1): 1, |
---|
151 | n/a | (self.my_py_filename, firstlineno_calling + 2): 11, |
---|
152 | n/a | (self.my_py_filename, firstlineno_calling + 3): 10, |
---|
153 | n/a | (self.my_py_filename, firstlineno_gen + 1): 1, |
---|
154 | n/a | (self.my_py_filename, firstlineno_gen + 2): 11, |
---|
155 | n/a | (self.my_py_filename, firstlineno_gen + 3): 10, |
---|
156 | n/a | } |
---|
157 | n/a | self.assertEqual(self.tracer.results().counts, expected) |
---|
158 | n/a | |
---|
159 | n/a | def test_trace_list_comprehension(self): |
---|
160 | n/a | self.tracer.runfunc(traced_caller_list_comprehension) |
---|
161 | n/a | |
---|
162 | n/a | firstlineno_calling = get_firstlineno(traced_caller_list_comprehension) |
---|
163 | n/a | firstlineno_called = get_firstlineno(traced_doubler) |
---|
164 | n/a | expected = { |
---|
165 | n/a | (self.my_py_filename, firstlineno_calling + 1): 1, |
---|
166 | n/a | # List compehentions work differently in 3.x, so the count |
---|
167 | n/a | # below changed compared to 2.x. |
---|
168 | n/a | (self.my_py_filename, firstlineno_calling + 2): 12, |
---|
169 | n/a | (self.my_py_filename, firstlineno_calling + 3): 1, |
---|
170 | n/a | (self.my_py_filename, firstlineno_called + 1): 10, |
---|
171 | n/a | } |
---|
172 | n/a | self.assertEqual(self.tracer.results().counts, expected) |
---|
173 | n/a | |
---|
174 | n/a | |
---|
175 | n/a | def test_linear_methods(self): |
---|
176 | n/a | # XXX todo: later add 'static_method_linear' and 'class_method_linear' |
---|
177 | n/a | # here, once issue1764286 is resolved |
---|
178 | n/a | # |
---|
179 | n/a | for methname in ['inst_method_linear',]: |
---|
180 | n/a | tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
---|
181 | n/a | traced_obj = TracedClass(25) |
---|
182 | n/a | method = getattr(traced_obj, methname) |
---|
183 | n/a | tracer.runfunc(method, 20) |
---|
184 | n/a | |
---|
185 | n/a | firstlineno = get_firstlineno(method) |
---|
186 | n/a | expected = { |
---|
187 | n/a | (self.my_py_filename, firstlineno + 1): 1, |
---|
188 | n/a | } |
---|
189 | n/a | self.assertEqual(tracer.results().counts, expected) |
---|
190 | n/a | |
---|
191 | n/a | class TestRunExecCounts(unittest.TestCase): |
---|
192 | n/a | """A simple sanity test of line-counting, via runctx (exec)""" |
---|
193 | n/a | def setUp(self): |
---|
194 | n/a | self.my_py_filename = fix_ext_py(__file__) |
---|
195 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
196 | n/a | |
---|
197 | n/a | def test_exec_counts(self): |
---|
198 | n/a | self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
---|
199 | n/a | code = r'''traced_func_loop(2, 5)''' |
---|
200 | n/a | code = compile(code, __file__, 'exec') |
---|
201 | n/a | self.tracer.runctx(code, globals(), vars()) |
---|
202 | n/a | |
---|
203 | n/a | firstlineno = get_firstlineno(traced_func_loop) |
---|
204 | n/a | expected = { |
---|
205 | n/a | (self.my_py_filename, firstlineno + 1): 1, |
---|
206 | n/a | (self.my_py_filename, firstlineno + 2): 6, |
---|
207 | n/a | (self.my_py_filename, firstlineno + 3): 5, |
---|
208 | n/a | (self.my_py_filename, firstlineno + 4): 1, |
---|
209 | n/a | } |
---|
210 | n/a | |
---|
211 | n/a | # When used through 'run', some other spurious counts are produced, like |
---|
212 | n/a | # the settrace of threading, which we ignore, just making sure that the |
---|
213 | n/a | # counts fo traced_func_loop were right. |
---|
214 | n/a | # |
---|
215 | n/a | for k in expected.keys(): |
---|
216 | n/a | self.assertEqual(self.tracer.results().counts[k], expected[k]) |
---|
217 | n/a | |
---|
218 | n/a | |
---|
219 | n/a | class TestFuncs(unittest.TestCase): |
---|
220 | n/a | """White-box testing of funcs tracing""" |
---|
221 | n/a | def setUp(self): |
---|
222 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
223 | n/a | self.tracer = Trace(count=0, trace=0, countfuncs=1) |
---|
224 | n/a | self.filemod = my_file_and_modname() |
---|
225 | n/a | self._saved_tracefunc = sys.gettrace() |
---|
226 | n/a | |
---|
227 | n/a | def tearDown(self): |
---|
228 | n/a | if self._saved_tracefunc is not None: |
---|
229 | n/a | sys.settrace(self._saved_tracefunc) |
---|
230 | n/a | |
---|
231 | n/a | def test_simple_caller(self): |
---|
232 | n/a | self.tracer.runfunc(traced_func_simple_caller, 1) |
---|
233 | n/a | |
---|
234 | n/a | expected = { |
---|
235 | n/a | self.filemod + ('traced_func_simple_caller',): 1, |
---|
236 | n/a | self.filemod + ('traced_func_linear',): 1, |
---|
237 | n/a | } |
---|
238 | n/a | self.assertEqual(self.tracer.results().calledfuncs, expected) |
---|
239 | n/a | |
---|
240 | n/a | def test_loop_caller_importing(self): |
---|
241 | n/a | self.tracer.runfunc(traced_func_importing_caller, 1) |
---|
242 | n/a | |
---|
243 | n/a | expected = { |
---|
244 | n/a | self.filemod + ('traced_func_simple_caller',): 1, |
---|
245 | n/a | self.filemod + ('traced_func_linear',): 1, |
---|
246 | n/a | self.filemod + ('traced_func_importing_caller',): 1, |
---|
247 | n/a | self.filemod + ('traced_func_importing',): 1, |
---|
248 | n/a | (fix_ext_py(testmod.__file__), 'testmod', 'func'): 1, |
---|
249 | n/a | } |
---|
250 | n/a | self.assertEqual(self.tracer.results().calledfuncs, expected) |
---|
251 | n/a | |
---|
252 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
253 | n/a | 'pre-existing trace function throws off measurements') |
---|
254 | n/a | def test_inst_method_calling(self): |
---|
255 | n/a | obj = TracedClass(20) |
---|
256 | n/a | self.tracer.runfunc(obj.inst_method_calling, 1) |
---|
257 | n/a | |
---|
258 | n/a | expected = { |
---|
259 | n/a | self.filemod + ('TracedClass.inst_method_calling',): 1, |
---|
260 | n/a | self.filemod + ('TracedClass.inst_method_linear',): 1, |
---|
261 | n/a | self.filemod + ('traced_func_linear',): 1, |
---|
262 | n/a | } |
---|
263 | n/a | self.assertEqual(self.tracer.results().calledfuncs, expected) |
---|
264 | n/a | |
---|
265 | n/a | |
---|
266 | n/a | class TestCallers(unittest.TestCase): |
---|
267 | n/a | """White-box testing of callers tracing""" |
---|
268 | n/a | def setUp(self): |
---|
269 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
270 | n/a | self.tracer = Trace(count=0, trace=0, countcallers=1) |
---|
271 | n/a | self.filemod = my_file_and_modname() |
---|
272 | n/a | |
---|
273 | n/a | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
---|
274 | n/a | 'pre-existing trace function throws off measurements') |
---|
275 | n/a | def test_loop_caller_importing(self): |
---|
276 | n/a | self.tracer.runfunc(traced_func_importing_caller, 1) |
---|
277 | n/a | |
---|
278 | n/a | expected = { |
---|
279 | n/a | ((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'), |
---|
280 | n/a | (self.filemod + ('traced_func_importing_caller',))): 1, |
---|
281 | n/a | ((self.filemod + ('traced_func_simple_caller',)), |
---|
282 | n/a | (self.filemod + ('traced_func_linear',))): 1, |
---|
283 | n/a | ((self.filemod + ('traced_func_importing_caller',)), |
---|
284 | n/a | (self.filemod + ('traced_func_simple_caller',))): 1, |
---|
285 | n/a | ((self.filemod + ('traced_func_importing_caller',)), |
---|
286 | n/a | (self.filemod + ('traced_func_importing',))): 1, |
---|
287 | n/a | ((self.filemod + ('traced_func_importing',)), |
---|
288 | n/a | (fix_ext_py(testmod.__file__), 'testmod', 'func')): 1, |
---|
289 | n/a | } |
---|
290 | n/a | self.assertEqual(self.tracer.results().callers, expected) |
---|
291 | n/a | |
---|
292 | n/a | |
---|
293 | n/a | # Created separately for issue #3821 |
---|
294 | n/a | class TestCoverage(unittest.TestCase): |
---|
295 | n/a | def setUp(self): |
---|
296 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
297 | n/a | |
---|
298 | n/a | def tearDown(self): |
---|
299 | n/a | rmtree(TESTFN) |
---|
300 | n/a | unlink(TESTFN) |
---|
301 | n/a | |
---|
302 | n/a | def _coverage(self, tracer, |
---|
303 | n/a | cmd='import test.support, test.test_pprint;' |
---|
304 | n/a | 'test.support.run_unittest(test.test_pprint.QueryTestCase)'): |
---|
305 | n/a | tracer.run(cmd) |
---|
306 | n/a | r = tracer.results() |
---|
307 | n/a | r.write_results(show_missing=True, summary=True, coverdir=TESTFN) |
---|
308 | n/a | |
---|
309 | n/a | def test_coverage(self): |
---|
310 | n/a | tracer = trace.Trace(trace=0, count=1) |
---|
311 | n/a | with captured_stdout() as stdout: |
---|
312 | n/a | self._coverage(tracer) |
---|
313 | n/a | stdout = stdout.getvalue() |
---|
314 | n/a | self.assertIn("pprint.py", stdout) |
---|
315 | n/a | self.assertIn("case.py", stdout) # from unittest |
---|
316 | n/a | files = os.listdir(TESTFN) |
---|
317 | n/a | self.assertIn("pprint.cover", files) |
---|
318 | n/a | self.assertIn("unittest.case.cover", files) |
---|
319 | n/a | |
---|
320 | n/a | def test_coverage_ignore(self): |
---|
321 | n/a | # Ignore all files, nothing should be traced nor printed |
---|
322 | n/a | libpath = os.path.normpath(os.path.dirname(os.__file__)) |
---|
323 | n/a | # sys.prefix does not work when running from a checkout |
---|
324 | n/a | tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix, |
---|
325 | n/a | libpath], trace=0, count=1) |
---|
326 | n/a | with captured_stdout() as stdout: |
---|
327 | n/a | self._coverage(tracer) |
---|
328 | n/a | if os.path.exists(TESTFN): |
---|
329 | n/a | files = os.listdir(TESTFN) |
---|
330 | n/a | self.assertEqual(files, ['_importlib.cover']) # Ignore __import__ |
---|
331 | n/a | |
---|
332 | n/a | def test_issue9936(self): |
---|
333 | n/a | tracer = trace.Trace(trace=0, count=1) |
---|
334 | n/a | modname = 'test.tracedmodules.testmod' |
---|
335 | n/a | # Ensure that the module is executed in import |
---|
336 | n/a | if modname in sys.modules: |
---|
337 | n/a | del sys.modules[modname] |
---|
338 | n/a | cmd = ("import test.tracedmodules.testmod as t;" |
---|
339 | n/a | "t.func(0); t.func2();") |
---|
340 | n/a | with captured_stdout() as stdout: |
---|
341 | n/a | self._coverage(tracer, cmd) |
---|
342 | n/a | stdout.seek(0) |
---|
343 | n/a | stdout.readline() |
---|
344 | n/a | coverage = {} |
---|
345 | n/a | for line in stdout: |
---|
346 | n/a | lines, cov, module = line.split()[:3] |
---|
347 | n/a | coverage[module] = (int(lines), int(cov[:-1])) |
---|
348 | n/a | # XXX This is needed to run regrtest.py as a script |
---|
349 | n/a | modname = trace._fullmodname(sys.modules[modname].__file__) |
---|
350 | n/a | self.assertIn(modname, coverage) |
---|
351 | n/a | self.assertEqual(coverage[modname], (5, 100)) |
---|
352 | n/a | |
---|
353 | n/a | ### Tests that don't mess with sys.settrace and can be traced |
---|
354 | n/a | ### themselves TODO: Skip tests that do mess with sys.settrace when |
---|
355 | n/a | ### regrtest is invoked with -T option. |
---|
356 | n/a | class Test_Ignore(unittest.TestCase): |
---|
357 | n/a | def test_ignored(self): |
---|
358 | n/a | jn = os.path.join |
---|
359 | n/a | ignore = trace._Ignore(['x', 'y.z'], [jn('foo', 'bar')]) |
---|
360 | n/a | self.assertTrue(ignore.names('x.py', 'x')) |
---|
361 | n/a | self.assertFalse(ignore.names('xy.py', 'xy')) |
---|
362 | n/a | self.assertFalse(ignore.names('y.py', 'y')) |
---|
363 | n/a | self.assertTrue(ignore.names(jn('foo', 'bar', 'baz.py'), 'baz')) |
---|
364 | n/a | self.assertFalse(ignore.names(jn('bar', 'z.py'), 'z')) |
---|
365 | n/a | # Matched before. |
---|
366 | n/a | self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz')) |
---|
367 | n/a | |
---|
368 | n/a | class TestCommandLine(unittest.TestCase): |
---|
369 | n/a | |
---|
370 | n/a | def test_failures(self): |
---|
371 | n/a | _errors = ( |
---|
372 | n/a | (b'filename is missing: required with the main options', '-l', '-T'), |
---|
373 | n/a | (b'cannot specify both --listfuncs and (--trace or --count)', '-lc'), |
---|
374 | n/a | (b'argument -R/--no-report: not allowed with argument -r/--report', '-rR'), |
---|
375 | n/a | (b'must specify one of --trace, --count, --report, --listfuncs, or --trackcalls', '-g'), |
---|
376 | n/a | (b'-r/--report requires -f/--file', '-r'), |
---|
377 | n/a | (b'--summary can only be used with --count or --report', '-sT'), |
---|
378 | n/a | (b'unrecognized arguments: -y', '-y')) |
---|
379 | n/a | for message, *args in _errors: |
---|
380 | n/a | *_, stderr = assert_python_failure('-m', 'trace', *args) |
---|
381 | n/a | self.assertIn(message, stderr) |
---|
382 | n/a | |
---|
383 | n/a | def test_listfuncs_flag_success(self): |
---|
384 | n/a | with open(TESTFN, 'w') as fd: |
---|
385 | n/a | self.addCleanup(unlink, TESTFN) |
---|
386 | n/a | fd.write("a = 1\n") |
---|
387 | n/a | status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN) |
---|
388 | n/a | self.assertIn(b'functions called:', stdout) |
---|
389 | n/a | |
---|
390 | n/a | if __name__ == '__main__': |
---|
391 | n/a | unittest.main() |
---|