1 | n/a | import contextlib |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | import tracemalloc |
---|
5 | n/a | import unittest |
---|
6 | n/a | from unittest.mock import patch |
---|
7 | n/a | from test.support.script_helper import (assert_python_ok, assert_python_failure, |
---|
8 | n/a | interpreter_requires_environment) |
---|
9 | n/a | from test import support |
---|
10 | n/a | try: |
---|
11 | n/a | import threading |
---|
12 | n/a | except ImportError: |
---|
13 | n/a | threading = None |
---|
14 | n/a | try: |
---|
15 | n/a | import _testcapi |
---|
16 | n/a | except ImportError: |
---|
17 | n/a | _testcapi = None |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | EMPTY_STRING_SIZE = sys.getsizeof(b'') |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | def get_frames(nframe, lineno_delta): |
---|
24 | n/a | frames = [] |
---|
25 | n/a | frame = sys._getframe(1) |
---|
26 | n/a | for index in range(nframe): |
---|
27 | n/a | code = frame.f_code |
---|
28 | n/a | lineno = frame.f_lineno + lineno_delta |
---|
29 | n/a | frames.append((code.co_filename, lineno)) |
---|
30 | n/a | lineno_delta = 0 |
---|
31 | n/a | frame = frame.f_back |
---|
32 | n/a | if frame is None: |
---|
33 | n/a | break |
---|
34 | n/a | return tuple(frames) |
---|
35 | n/a | |
---|
36 | n/a | def allocate_bytes(size): |
---|
37 | n/a | nframe = tracemalloc.get_traceback_limit() |
---|
38 | n/a | bytes_len = (size - EMPTY_STRING_SIZE) |
---|
39 | n/a | frames = get_frames(nframe, 1) |
---|
40 | n/a | data = b'x' * bytes_len |
---|
41 | n/a | return data, tracemalloc.Traceback(frames) |
---|
42 | n/a | |
---|
43 | n/a | def create_snapshots(): |
---|
44 | n/a | traceback_limit = 2 |
---|
45 | n/a | |
---|
46 | n/a | # _tracemalloc._get_traces() returns a list of (domain, size, |
---|
47 | n/a | # traceback_frames) tuples. traceback_frames is a tuple of (filename, |
---|
48 | n/a | # line_number) tuples. |
---|
49 | n/a | raw_traces = [ |
---|
50 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
51 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
52 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
53 | n/a | |
---|
54 | n/a | (1, 2, (('a.py', 5), ('b.py', 4))), |
---|
55 | n/a | |
---|
56 | n/a | (2, 66, (('b.py', 1),)), |
---|
57 | n/a | |
---|
58 | n/a | (3, 7, (('<unknown>', 0),)), |
---|
59 | n/a | ] |
---|
60 | n/a | snapshot = tracemalloc.Snapshot(raw_traces, traceback_limit) |
---|
61 | n/a | |
---|
62 | n/a | raw_traces2 = [ |
---|
63 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
64 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
65 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
66 | n/a | |
---|
67 | n/a | (2, 2, (('a.py', 5), ('b.py', 4))), |
---|
68 | n/a | (2, 5000, (('a.py', 5), ('b.py', 4))), |
---|
69 | n/a | |
---|
70 | n/a | (4, 400, (('c.py', 578),)), |
---|
71 | n/a | ] |
---|
72 | n/a | snapshot2 = tracemalloc.Snapshot(raw_traces2, traceback_limit) |
---|
73 | n/a | |
---|
74 | n/a | return (snapshot, snapshot2) |
---|
75 | n/a | |
---|
76 | n/a | def frame(filename, lineno): |
---|
77 | n/a | return tracemalloc._Frame((filename, lineno)) |
---|
78 | n/a | |
---|
79 | n/a | def traceback(*frames): |
---|
80 | n/a | return tracemalloc.Traceback(frames) |
---|
81 | n/a | |
---|
82 | n/a | def traceback_lineno(filename, lineno): |
---|
83 | n/a | return traceback((filename, lineno)) |
---|
84 | n/a | |
---|
85 | n/a | def traceback_filename(filename): |
---|
86 | n/a | return traceback_lineno(filename, 0) |
---|
87 | n/a | |
---|
88 | n/a | |
---|
89 | n/a | class TestTracemallocEnabled(unittest.TestCase): |
---|
90 | n/a | def setUp(self): |
---|
91 | n/a | if tracemalloc.is_tracing(): |
---|
92 | n/a | self.skipTest("tracemalloc must be stopped before the test") |
---|
93 | n/a | |
---|
94 | n/a | tracemalloc.start(1) |
---|
95 | n/a | |
---|
96 | n/a | def tearDown(self): |
---|
97 | n/a | tracemalloc.stop() |
---|
98 | n/a | |
---|
99 | n/a | def test_get_tracemalloc_memory(self): |
---|
100 | n/a | data = [allocate_bytes(123) for count in range(1000)] |
---|
101 | n/a | size = tracemalloc.get_tracemalloc_memory() |
---|
102 | n/a | self.assertGreaterEqual(size, 0) |
---|
103 | n/a | |
---|
104 | n/a | tracemalloc.clear_traces() |
---|
105 | n/a | size2 = tracemalloc.get_tracemalloc_memory() |
---|
106 | n/a | self.assertGreaterEqual(size2, 0) |
---|
107 | n/a | self.assertLessEqual(size2, size) |
---|
108 | n/a | |
---|
109 | n/a | def test_get_object_traceback(self): |
---|
110 | n/a | tracemalloc.clear_traces() |
---|
111 | n/a | obj_size = 12345 |
---|
112 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
113 | n/a | traceback = tracemalloc.get_object_traceback(obj) |
---|
114 | n/a | self.assertEqual(traceback, obj_traceback) |
---|
115 | n/a | |
---|
116 | n/a | def test_set_traceback_limit(self): |
---|
117 | n/a | obj_size = 10 |
---|
118 | n/a | |
---|
119 | n/a | tracemalloc.stop() |
---|
120 | n/a | self.assertRaises(ValueError, tracemalloc.start, -1) |
---|
121 | n/a | |
---|
122 | n/a | tracemalloc.stop() |
---|
123 | n/a | tracemalloc.start(10) |
---|
124 | n/a | obj2, obj2_traceback = allocate_bytes(obj_size) |
---|
125 | n/a | traceback = tracemalloc.get_object_traceback(obj2) |
---|
126 | n/a | self.assertEqual(len(traceback), 10) |
---|
127 | n/a | self.assertEqual(traceback, obj2_traceback) |
---|
128 | n/a | |
---|
129 | n/a | tracemalloc.stop() |
---|
130 | n/a | tracemalloc.start(1) |
---|
131 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
132 | n/a | traceback = tracemalloc.get_object_traceback(obj) |
---|
133 | n/a | self.assertEqual(len(traceback), 1) |
---|
134 | n/a | self.assertEqual(traceback, obj_traceback) |
---|
135 | n/a | |
---|
136 | n/a | def find_trace(self, traces, traceback): |
---|
137 | n/a | for trace in traces: |
---|
138 | n/a | if trace[2] == traceback._frames: |
---|
139 | n/a | return trace |
---|
140 | n/a | |
---|
141 | n/a | self.fail("trace not found") |
---|
142 | n/a | |
---|
143 | n/a | def test_get_traces(self): |
---|
144 | n/a | tracemalloc.clear_traces() |
---|
145 | n/a | obj_size = 12345 |
---|
146 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
147 | n/a | |
---|
148 | n/a | traces = tracemalloc._get_traces() |
---|
149 | n/a | trace = self.find_trace(traces, obj_traceback) |
---|
150 | n/a | |
---|
151 | n/a | self.assertIsInstance(trace, tuple) |
---|
152 | n/a | domain, size, traceback = trace |
---|
153 | n/a | self.assertEqual(size, obj_size) |
---|
154 | n/a | self.assertEqual(traceback, obj_traceback._frames) |
---|
155 | n/a | |
---|
156 | n/a | tracemalloc.stop() |
---|
157 | n/a | self.assertEqual(tracemalloc._get_traces(), []) |
---|
158 | n/a | |
---|
159 | n/a | def test_get_traces_intern_traceback(self): |
---|
160 | n/a | # dummy wrappers to get more useful and identical frames in the traceback |
---|
161 | n/a | def allocate_bytes2(size): |
---|
162 | n/a | return allocate_bytes(size) |
---|
163 | n/a | def allocate_bytes3(size): |
---|
164 | n/a | return allocate_bytes2(size) |
---|
165 | n/a | def allocate_bytes4(size): |
---|
166 | n/a | return allocate_bytes3(size) |
---|
167 | n/a | |
---|
168 | n/a | # Ensure that two identical tracebacks are not duplicated |
---|
169 | n/a | tracemalloc.stop() |
---|
170 | n/a | tracemalloc.start(4) |
---|
171 | n/a | obj_size = 123 |
---|
172 | n/a | obj1, obj1_traceback = allocate_bytes4(obj_size) |
---|
173 | n/a | obj2, obj2_traceback = allocate_bytes4(obj_size) |
---|
174 | n/a | |
---|
175 | n/a | traces = tracemalloc._get_traces() |
---|
176 | n/a | |
---|
177 | n/a | trace1 = self.find_trace(traces, obj1_traceback) |
---|
178 | n/a | trace2 = self.find_trace(traces, obj2_traceback) |
---|
179 | n/a | domain1, size1, traceback1 = trace1 |
---|
180 | n/a | domain2, size2, traceback2 = trace2 |
---|
181 | n/a | self.assertIs(traceback2, traceback1) |
---|
182 | n/a | |
---|
183 | n/a | def test_get_traced_memory(self): |
---|
184 | n/a | # Python allocates some internals objects, so the test must tolerate |
---|
185 | n/a | # a small difference between the expected size and the real usage |
---|
186 | n/a | max_error = 2048 |
---|
187 | n/a | |
---|
188 | n/a | # allocate one object |
---|
189 | n/a | obj_size = 1024 * 1024 |
---|
190 | n/a | tracemalloc.clear_traces() |
---|
191 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
192 | n/a | size, peak_size = tracemalloc.get_traced_memory() |
---|
193 | n/a | self.assertGreaterEqual(size, obj_size) |
---|
194 | n/a | self.assertGreaterEqual(peak_size, size) |
---|
195 | n/a | |
---|
196 | n/a | self.assertLessEqual(size - obj_size, max_error) |
---|
197 | n/a | self.assertLessEqual(peak_size - size, max_error) |
---|
198 | n/a | |
---|
199 | n/a | # destroy the object |
---|
200 | n/a | obj = None |
---|
201 | n/a | size2, peak_size2 = tracemalloc.get_traced_memory() |
---|
202 | n/a | self.assertLess(size2, size) |
---|
203 | n/a | self.assertGreaterEqual(size - size2, obj_size - max_error) |
---|
204 | n/a | self.assertGreaterEqual(peak_size2, peak_size) |
---|
205 | n/a | |
---|
206 | n/a | # clear_traces() must reset traced memory counters |
---|
207 | n/a | tracemalloc.clear_traces() |
---|
208 | n/a | self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) |
---|
209 | n/a | |
---|
210 | n/a | # allocate another object |
---|
211 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
212 | n/a | size, peak_size = tracemalloc.get_traced_memory() |
---|
213 | n/a | self.assertGreaterEqual(size, obj_size) |
---|
214 | n/a | |
---|
215 | n/a | # stop() also resets traced memory counters |
---|
216 | n/a | tracemalloc.stop() |
---|
217 | n/a | self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) |
---|
218 | n/a | |
---|
219 | n/a | def test_clear_traces(self): |
---|
220 | n/a | obj, obj_traceback = allocate_bytes(123) |
---|
221 | n/a | traceback = tracemalloc.get_object_traceback(obj) |
---|
222 | n/a | self.assertIsNotNone(traceback) |
---|
223 | n/a | |
---|
224 | n/a | tracemalloc.clear_traces() |
---|
225 | n/a | traceback2 = tracemalloc.get_object_traceback(obj) |
---|
226 | n/a | self.assertIsNone(traceback2) |
---|
227 | n/a | |
---|
228 | n/a | def test_is_tracing(self): |
---|
229 | n/a | tracemalloc.stop() |
---|
230 | n/a | self.assertFalse(tracemalloc.is_tracing()) |
---|
231 | n/a | |
---|
232 | n/a | tracemalloc.start() |
---|
233 | n/a | self.assertTrue(tracemalloc.is_tracing()) |
---|
234 | n/a | |
---|
235 | n/a | def test_snapshot(self): |
---|
236 | n/a | obj, source = allocate_bytes(123) |
---|
237 | n/a | |
---|
238 | n/a | # take a snapshot |
---|
239 | n/a | snapshot = tracemalloc.take_snapshot() |
---|
240 | n/a | |
---|
241 | n/a | # write on disk |
---|
242 | n/a | snapshot.dump(support.TESTFN) |
---|
243 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
244 | n/a | |
---|
245 | n/a | # load from disk |
---|
246 | n/a | snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) |
---|
247 | n/a | self.assertEqual(snapshot2.traces, snapshot.traces) |
---|
248 | n/a | |
---|
249 | n/a | # tracemalloc must be tracing memory allocations to take a snapshot |
---|
250 | n/a | tracemalloc.stop() |
---|
251 | n/a | with self.assertRaises(RuntimeError) as cm: |
---|
252 | n/a | tracemalloc.take_snapshot() |
---|
253 | n/a | self.assertEqual(str(cm.exception), |
---|
254 | n/a | "the tracemalloc module must be tracing memory " |
---|
255 | n/a | "allocations to take a snapshot") |
---|
256 | n/a | |
---|
257 | n/a | def test_snapshot_save_attr(self): |
---|
258 | n/a | # take a snapshot with a new attribute |
---|
259 | n/a | snapshot = tracemalloc.take_snapshot() |
---|
260 | n/a | snapshot.test_attr = "new" |
---|
261 | n/a | snapshot.dump(support.TESTFN) |
---|
262 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
263 | n/a | |
---|
264 | n/a | # load() should recreate the attribute |
---|
265 | n/a | snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) |
---|
266 | n/a | self.assertEqual(snapshot2.test_attr, "new") |
---|
267 | n/a | |
---|
268 | n/a | def fork_child(self): |
---|
269 | n/a | if not tracemalloc.is_tracing(): |
---|
270 | n/a | return 2 |
---|
271 | n/a | |
---|
272 | n/a | obj_size = 12345 |
---|
273 | n/a | obj, obj_traceback = allocate_bytes(obj_size) |
---|
274 | n/a | traceback = tracemalloc.get_object_traceback(obj) |
---|
275 | n/a | if traceback is None: |
---|
276 | n/a | return 3 |
---|
277 | n/a | |
---|
278 | n/a | # everything is fine |
---|
279 | n/a | return 0 |
---|
280 | n/a | |
---|
281 | n/a | @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork()') |
---|
282 | n/a | def test_fork(self): |
---|
283 | n/a | # check that tracemalloc is still working after fork |
---|
284 | n/a | pid = os.fork() |
---|
285 | n/a | if not pid: |
---|
286 | n/a | # child |
---|
287 | n/a | exitcode = 1 |
---|
288 | n/a | try: |
---|
289 | n/a | exitcode = self.fork_child() |
---|
290 | n/a | finally: |
---|
291 | n/a | os._exit(exitcode) |
---|
292 | n/a | else: |
---|
293 | n/a | pid2, status = os.waitpid(pid, 0) |
---|
294 | n/a | self.assertTrue(os.WIFEXITED(status)) |
---|
295 | n/a | exitcode = os.WEXITSTATUS(status) |
---|
296 | n/a | self.assertEqual(exitcode, 0) |
---|
297 | n/a | |
---|
298 | n/a | |
---|
299 | n/a | class TestSnapshot(unittest.TestCase): |
---|
300 | n/a | maxDiff = 4000 |
---|
301 | n/a | |
---|
302 | n/a | def test_create_snapshot(self): |
---|
303 | n/a | raw_traces = [(0, 5, (('a.py', 2),))] |
---|
304 | n/a | |
---|
305 | n/a | with contextlib.ExitStack() as stack: |
---|
306 | n/a | stack.enter_context(patch.object(tracemalloc, 'is_tracing', |
---|
307 | n/a | return_value=True)) |
---|
308 | n/a | stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit', |
---|
309 | n/a | return_value=5)) |
---|
310 | n/a | stack.enter_context(patch.object(tracemalloc, '_get_traces', |
---|
311 | n/a | return_value=raw_traces)) |
---|
312 | n/a | |
---|
313 | n/a | snapshot = tracemalloc.take_snapshot() |
---|
314 | n/a | self.assertEqual(snapshot.traceback_limit, 5) |
---|
315 | n/a | self.assertEqual(len(snapshot.traces), 1) |
---|
316 | n/a | trace = snapshot.traces[0] |
---|
317 | n/a | self.assertEqual(trace.size, 5) |
---|
318 | n/a | self.assertEqual(len(trace.traceback), 1) |
---|
319 | n/a | self.assertEqual(trace.traceback[0].filename, 'a.py') |
---|
320 | n/a | self.assertEqual(trace.traceback[0].lineno, 2) |
---|
321 | n/a | |
---|
322 | n/a | def test_filter_traces(self): |
---|
323 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
324 | n/a | filter1 = tracemalloc.Filter(False, "b.py") |
---|
325 | n/a | filter2 = tracemalloc.Filter(True, "a.py", 2) |
---|
326 | n/a | filter3 = tracemalloc.Filter(True, "a.py", 5) |
---|
327 | n/a | |
---|
328 | n/a | original_traces = list(snapshot.traces._traces) |
---|
329 | n/a | |
---|
330 | n/a | # exclude b.py |
---|
331 | n/a | snapshot3 = snapshot.filter_traces((filter1,)) |
---|
332 | n/a | self.assertEqual(snapshot3.traces._traces, [ |
---|
333 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
334 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
335 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
336 | n/a | (1, 2, (('a.py', 5), ('b.py', 4))), |
---|
337 | n/a | (3, 7, (('<unknown>', 0),)), |
---|
338 | n/a | ]) |
---|
339 | n/a | |
---|
340 | n/a | # filter_traces() must not touch the original snapshot |
---|
341 | n/a | self.assertEqual(snapshot.traces._traces, original_traces) |
---|
342 | n/a | |
---|
343 | n/a | # only include two lines of a.py |
---|
344 | n/a | snapshot4 = snapshot3.filter_traces((filter2, filter3)) |
---|
345 | n/a | self.assertEqual(snapshot4.traces._traces, [ |
---|
346 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
347 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
348 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
349 | n/a | (1, 2, (('a.py', 5), ('b.py', 4))), |
---|
350 | n/a | ]) |
---|
351 | n/a | |
---|
352 | n/a | # No filter: just duplicate the snapshot |
---|
353 | n/a | snapshot5 = snapshot.filter_traces(()) |
---|
354 | n/a | self.assertIsNot(snapshot5, snapshot) |
---|
355 | n/a | self.assertIsNot(snapshot5.traces, snapshot.traces) |
---|
356 | n/a | self.assertEqual(snapshot5.traces, snapshot.traces) |
---|
357 | n/a | |
---|
358 | n/a | self.assertRaises(TypeError, snapshot.filter_traces, filter1) |
---|
359 | n/a | |
---|
360 | n/a | def test_filter_traces_domain(self): |
---|
361 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
362 | n/a | filter1 = tracemalloc.Filter(False, "a.py", domain=1) |
---|
363 | n/a | filter2 = tracemalloc.Filter(True, "a.py", domain=1) |
---|
364 | n/a | |
---|
365 | n/a | original_traces = list(snapshot.traces._traces) |
---|
366 | n/a | |
---|
367 | n/a | # exclude a.py of domain 1 |
---|
368 | n/a | snapshot3 = snapshot.filter_traces((filter1,)) |
---|
369 | n/a | self.assertEqual(snapshot3.traces._traces, [ |
---|
370 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
371 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
372 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
373 | n/a | (2, 66, (('b.py', 1),)), |
---|
374 | n/a | (3, 7, (('<unknown>', 0),)), |
---|
375 | n/a | ]) |
---|
376 | n/a | |
---|
377 | n/a | # include domain 1 |
---|
378 | n/a | snapshot3 = snapshot.filter_traces((filter1,)) |
---|
379 | n/a | self.assertEqual(snapshot3.traces._traces, [ |
---|
380 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
381 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
382 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
383 | n/a | (2, 66, (('b.py', 1),)), |
---|
384 | n/a | (3, 7, (('<unknown>', 0),)), |
---|
385 | n/a | ]) |
---|
386 | n/a | |
---|
387 | n/a | def test_filter_traces_domain_filter(self): |
---|
388 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
389 | n/a | filter1 = tracemalloc.DomainFilter(False, domain=3) |
---|
390 | n/a | filter2 = tracemalloc.DomainFilter(True, domain=3) |
---|
391 | n/a | |
---|
392 | n/a | # exclude domain 2 |
---|
393 | n/a | snapshot3 = snapshot.filter_traces((filter1,)) |
---|
394 | n/a | self.assertEqual(snapshot3.traces._traces, [ |
---|
395 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
396 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
397 | n/a | (0, 10, (('a.py', 2), ('b.py', 4))), |
---|
398 | n/a | (1, 2, (('a.py', 5), ('b.py', 4))), |
---|
399 | n/a | (2, 66, (('b.py', 1),)), |
---|
400 | n/a | ]) |
---|
401 | n/a | |
---|
402 | n/a | # include domain 2 |
---|
403 | n/a | snapshot3 = snapshot.filter_traces((filter2,)) |
---|
404 | n/a | self.assertEqual(snapshot3.traces._traces, [ |
---|
405 | n/a | (3, 7, (('<unknown>', 0),)), |
---|
406 | n/a | ]) |
---|
407 | n/a | |
---|
408 | n/a | def test_snapshot_group_by_line(self): |
---|
409 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
410 | n/a | tb_0 = traceback_lineno('<unknown>', 0) |
---|
411 | n/a | tb_a_2 = traceback_lineno('a.py', 2) |
---|
412 | n/a | tb_a_5 = traceback_lineno('a.py', 5) |
---|
413 | n/a | tb_b_1 = traceback_lineno('b.py', 1) |
---|
414 | n/a | tb_c_578 = traceback_lineno('c.py', 578) |
---|
415 | n/a | |
---|
416 | n/a | # stats per file and line |
---|
417 | n/a | stats1 = snapshot.statistics('lineno') |
---|
418 | n/a | self.assertEqual(stats1, [ |
---|
419 | n/a | tracemalloc.Statistic(tb_b_1, 66, 1), |
---|
420 | n/a | tracemalloc.Statistic(tb_a_2, 30, 3), |
---|
421 | n/a | tracemalloc.Statistic(tb_0, 7, 1), |
---|
422 | n/a | tracemalloc.Statistic(tb_a_5, 2, 1), |
---|
423 | n/a | ]) |
---|
424 | n/a | |
---|
425 | n/a | # stats per file and line (2) |
---|
426 | n/a | stats2 = snapshot2.statistics('lineno') |
---|
427 | n/a | self.assertEqual(stats2, [ |
---|
428 | n/a | tracemalloc.Statistic(tb_a_5, 5002, 2), |
---|
429 | n/a | tracemalloc.Statistic(tb_c_578, 400, 1), |
---|
430 | n/a | tracemalloc.Statistic(tb_a_2, 30, 3), |
---|
431 | n/a | ]) |
---|
432 | n/a | |
---|
433 | n/a | # stats diff per file and line |
---|
434 | n/a | statistics = snapshot2.compare_to(snapshot, 'lineno') |
---|
435 | n/a | self.assertEqual(statistics, [ |
---|
436 | n/a | tracemalloc.StatisticDiff(tb_a_5, 5002, 5000, 2, 1), |
---|
437 | n/a | tracemalloc.StatisticDiff(tb_c_578, 400, 400, 1, 1), |
---|
438 | n/a | tracemalloc.StatisticDiff(tb_b_1, 0, -66, 0, -1), |
---|
439 | n/a | tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1), |
---|
440 | n/a | tracemalloc.StatisticDiff(tb_a_2, 30, 0, 3, 0), |
---|
441 | n/a | ]) |
---|
442 | n/a | |
---|
443 | n/a | def test_snapshot_group_by_file(self): |
---|
444 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
445 | n/a | tb_0 = traceback_filename('<unknown>') |
---|
446 | n/a | tb_a = traceback_filename('a.py') |
---|
447 | n/a | tb_b = traceback_filename('b.py') |
---|
448 | n/a | tb_c = traceback_filename('c.py') |
---|
449 | n/a | |
---|
450 | n/a | # stats per file |
---|
451 | n/a | stats1 = snapshot.statistics('filename') |
---|
452 | n/a | self.assertEqual(stats1, [ |
---|
453 | n/a | tracemalloc.Statistic(tb_b, 66, 1), |
---|
454 | n/a | tracemalloc.Statistic(tb_a, 32, 4), |
---|
455 | n/a | tracemalloc.Statistic(tb_0, 7, 1), |
---|
456 | n/a | ]) |
---|
457 | n/a | |
---|
458 | n/a | # stats per file (2) |
---|
459 | n/a | stats2 = snapshot2.statistics('filename') |
---|
460 | n/a | self.assertEqual(stats2, [ |
---|
461 | n/a | tracemalloc.Statistic(tb_a, 5032, 5), |
---|
462 | n/a | tracemalloc.Statistic(tb_c, 400, 1), |
---|
463 | n/a | ]) |
---|
464 | n/a | |
---|
465 | n/a | # stats diff per file |
---|
466 | n/a | diff = snapshot2.compare_to(snapshot, 'filename') |
---|
467 | n/a | self.assertEqual(diff, [ |
---|
468 | n/a | tracemalloc.StatisticDiff(tb_a, 5032, 5000, 5, 1), |
---|
469 | n/a | tracemalloc.StatisticDiff(tb_c, 400, 400, 1, 1), |
---|
470 | n/a | tracemalloc.StatisticDiff(tb_b, 0, -66, 0, -1), |
---|
471 | n/a | tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1), |
---|
472 | n/a | ]) |
---|
473 | n/a | |
---|
474 | n/a | def test_snapshot_group_by_traceback(self): |
---|
475 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
476 | n/a | |
---|
477 | n/a | # stats per file |
---|
478 | n/a | tb1 = traceback(('a.py', 2), ('b.py', 4)) |
---|
479 | n/a | tb2 = traceback(('a.py', 5), ('b.py', 4)) |
---|
480 | n/a | tb3 = traceback(('b.py', 1)) |
---|
481 | n/a | tb4 = traceback(('<unknown>', 0)) |
---|
482 | n/a | stats1 = snapshot.statistics('traceback') |
---|
483 | n/a | self.assertEqual(stats1, [ |
---|
484 | n/a | tracemalloc.Statistic(tb3, 66, 1), |
---|
485 | n/a | tracemalloc.Statistic(tb1, 30, 3), |
---|
486 | n/a | tracemalloc.Statistic(tb4, 7, 1), |
---|
487 | n/a | tracemalloc.Statistic(tb2, 2, 1), |
---|
488 | n/a | ]) |
---|
489 | n/a | |
---|
490 | n/a | # stats per file (2) |
---|
491 | n/a | tb5 = traceback(('c.py', 578)) |
---|
492 | n/a | stats2 = snapshot2.statistics('traceback') |
---|
493 | n/a | self.assertEqual(stats2, [ |
---|
494 | n/a | tracemalloc.Statistic(tb2, 5002, 2), |
---|
495 | n/a | tracemalloc.Statistic(tb5, 400, 1), |
---|
496 | n/a | tracemalloc.Statistic(tb1, 30, 3), |
---|
497 | n/a | ]) |
---|
498 | n/a | |
---|
499 | n/a | # stats diff per file |
---|
500 | n/a | diff = snapshot2.compare_to(snapshot, 'traceback') |
---|
501 | n/a | self.assertEqual(diff, [ |
---|
502 | n/a | tracemalloc.StatisticDiff(tb2, 5002, 5000, 2, 1), |
---|
503 | n/a | tracemalloc.StatisticDiff(tb5, 400, 400, 1, 1), |
---|
504 | n/a | tracemalloc.StatisticDiff(tb3, 0, -66, 0, -1), |
---|
505 | n/a | tracemalloc.StatisticDiff(tb4, 0, -7, 0, -1), |
---|
506 | n/a | tracemalloc.StatisticDiff(tb1, 30, 0, 3, 0), |
---|
507 | n/a | ]) |
---|
508 | n/a | |
---|
509 | n/a | self.assertRaises(ValueError, |
---|
510 | n/a | snapshot.statistics, 'traceback', cumulative=True) |
---|
511 | n/a | |
---|
512 | n/a | def test_snapshot_group_by_cumulative(self): |
---|
513 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
514 | n/a | tb_0 = traceback_filename('<unknown>') |
---|
515 | n/a | tb_a = traceback_filename('a.py') |
---|
516 | n/a | tb_b = traceback_filename('b.py') |
---|
517 | n/a | tb_a_2 = traceback_lineno('a.py', 2) |
---|
518 | n/a | tb_a_5 = traceback_lineno('a.py', 5) |
---|
519 | n/a | tb_b_1 = traceback_lineno('b.py', 1) |
---|
520 | n/a | tb_b_4 = traceback_lineno('b.py', 4) |
---|
521 | n/a | |
---|
522 | n/a | # per file |
---|
523 | n/a | stats = snapshot.statistics('filename', True) |
---|
524 | n/a | self.assertEqual(stats, [ |
---|
525 | n/a | tracemalloc.Statistic(tb_b, 98, 5), |
---|
526 | n/a | tracemalloc.Statistic(tb_a, 32, 4), |
---|
527 | n/a | tracemalloc.Statistic(tb_0, 7, 1), |
---|
528 | n/a | ]) |
---|
529 | n/a | |
---|
530 | n/a | # per line |
---|
531 | n/a | stats = snapshot.statistics('lineno', True) |
---|
532 | n/a | self.assertEqual(stats, [ |
---|
533 | n/a | tracemalloc.Statistic(tb_b_1, 66, 1), |
---|
534 | n/a | tracemalloc.Statistic(tb_b_4, 32, 4), |
---|
535 | n/a | tracemalloc.Statistic(tb_a_2, 30, 3), |
---|
536 | n/a | tracemalloc.Statistic(tb_0, 7, 1), |
---|
537 | n/a | tracemalloc.Statistic(tb_a_5, 2, 1), |
---|
538 | n/a | ]) |
---|
539 | n/a | |
---|
540 | n/a | def test_trace_format(self): |
---|
541 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
542 | n/a | trace = snapshot.traces[0] |
---|
543 | n/a | self.assertEqual(str(trace), 'a.py:2: 10 B') |
---|
544 | n/a | traceback = trace.traceback |
---|
545 | n/a | self.assertEqual(str(traceback), 'a.py:2') |
---|
546 | n/a | frame = traceback[0] |
---|
547 | n/a | self.assertEqual(str(frame), 'a.py:2') |
---|
548 | n/a | |
---|
549 | n/a | def test_statistic_format(self): |
---|
550 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
551 | n/a | stats = snapshot.statistics('lineno') |
---|
552 | n/a | stat = stats[0] |
---|
553 | n/a | self.assertEqual(str(stat), |
---|
554 | n/a | 'b.py:1: size=66 B, count=1, average=66 B') |
---|
555 | n/a | |
---|
556 | n/a | def test_statistic_diff_format(self): |
---|
557 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
558 | n/a | stats = snapshot2.compare_to(snapshot, 'lineno') |
---|
559 | n/a | stat = stats[0] |
---|
560 | n/a | self.assertEqual(str(stat), |
---|
561 | n/a | 'a.py:5: size=5002 B (+5000 B), count=2 (+1), average=2501 B') |
---|
562 | n/a | |
---|
563 | n/a | def test_slices(self): |
---|
564 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
565 | n/a | self.assertEqual(snapshot.traces[:2], |
---|
566 | n/a | (snapshot.traces[0], snapshot.traces[1])) |
---|
567 | n/a | |
---|
568 | n/a | traceback = snapshot.traces[0].traceback |
---|
569 | n/a | self.assertEqual(traceback[:2], |
---|
570 | n/a | (traceback[0], traceback[1])) |
---|
571 | n/a | |
---|
572 | n/a | def test_format_traceback(self): |
---|
573 | n/a | snapshot, snapshot2 = create_snapshots() |
---|
574 | n/a | def getline(filename, lineno): |
---|
575 | n/a | return ' <%s, %s>' % (filename, lineno) |
---|
576 | n/a | with unittest.mock.patch('tracemalloc.linecache.getline', |
---|
577 | n/a | side_effect=getline): |
---|
578 | n/a | tb = snapshot.traces[0].traceback |
---|
579 | n/a | self.assertEqual(tb.format(), |
---|
580 | n/a | [' File "a.py", line 2', |
---|
581 | n/a | ' <a.py, 2>', |
---|
582 | n/a | ' File "b.py", line 4', |
---|
583 | n/a | ' <b.py, 4>']) |
---|
584 | n/a | |
---|
585 | n/a | self.assertEqual(tb.format(limit=1), |
---|
586 | n/a | [' File "a.py", line 2', |
---|
587 | n/a | ' <a.py, 2>']) |
---|
588 | n/a | |
---|
589 | n/a | self.assertEqual(tb.format(limit=-1), |
---|
590 | n/a | []) |
---|
591 | n/a | |
---|
592 | n/a | |
---|
593 | n/a | class TestFilters(unittest.TestCase): |
---|
594 | n/a | maxDiff = 2048 |
---|
595 | n/a | |
---|
596 | n/a | def test_filter_attributes(self): |
---|
597 | n/a | # test default values |
---|
598 | n/a | f = tracemalloc.Filter(True, "abc") |
---|
599 | n/a | self.assertEqual(f.inclusive, True) |
---|
600 | n/a | self.assertEqual(f.filename_pattern, "abc") |
---|
601 | n/a | self.assertIsNone(f.lineno) |
---|
602 | n/a | self.assertEqual(f.all_frames, False) |
---|
603 | n/a | |
---|
604 | n/a | # test custom values |
---|
605 | n/a | f = tracemalloc.Filter(False, "test.py", 123, True) |
---|
606 | n/a | self.assertEqual(f.inclusive, False) |
---|
607 | n/a | self.assertEqual(f.filename_pattern, "test.py") |
---|
608 | n/a | self.assertEqual(f.lineno, 123) |
---|
609 | n/a | self.assertEqual(f.all_frames, True) |
---|
610 | n/a | |
---|
611 | n/a | # parameters passed by keyword |
---|
612 | n/a | f = tracemalloc.Filter(inclusive=False, filename_pattern="test.py", lineno=123, all_frames=True) |
---|
613 | n/a | self.assertEqual(f.inclusive, False) |
---|
614 | n/a | self.assertEqual(f.filename_pattern, "test.py") |
---|
615 | n/a | self.assertEqual(f.lineno, 123) |
---|
616 | n/a | self.assertEqual(f.all_frames, True) |
---|
617 | n/a | |
---|
618 | n/a | # read-only attribute |
---|
619 | n/a | self.assertRaises(AttributeError, setattr, f, "filename_pattern", "abc") |
---|
620 | n/a | |
---|
621 | n/a | def test_filter_match(self): |
---|
622 | n/a | # filter without line number |
---|
623 | n/a | f = tracemalloc.Filter(True, "abc") |
---|
624 | n/a | self.assertTrue(f._match_frame("abc", 0)) |
---|
625 | n/a | self.assertTrue(f._match_frame("abc", 5)) |
---|
626 | n/a | self.assertTrue(f._match_frame("abc", 10)) |
---|
627 | n/a | self.assertFalse(f._match_frame("12356", 0)) |
---|
628 | n/a | self.assertFalse(f._match_frame("12356", 5)) |
---|
629 | n/a | self.assertFalse(f._match_frame("12356", 10)) |
---|
630 | n/a | |
---|
631 | n/a | f = tracemalloc.Filter(False, "abc") |
---|
632 | n/a | self.assertFalse(f._match_frame("abc", 0)) |
---|
633 | n/a | self.assertFalse(f._match_frame("abc", 5)) |
---|
634 | n/a | self.assertFalse(f._match_frame("abc", 10)) |
---|
635 | n/a | self.assertTrue(f._match_frame("12356", 0)) |
---|
636 | n/a | self.assertTrue(f._match_frame("12356", 5)) |
---|
637 | n/a | self.assertTrue(f._match_frame("12356", 10)) |
---|
638 | n/a | |
---|
639 | n/a | # filter with line number > 0 |
---|
640 | n/a | f = tracemalloc.Filter(True, "abc", 5) |
---|
641 | n/a | self.assertFalse(f._match_frame("abc", 0)) |
---|
642 | n/a | self.assertTrue(f._match_frame("abc", 5)) |
---|
643 | n/a | self.assertFalse(f._match_frame("abc", 10)) |
---|
644 | n/a | self.assertFalse(f._match_frame("12356", 0)) |
---|
645 | n/a | self.assertFalse(f._match_frame("12356", 5)) |
---|
646 | n/a | self.assertFalse(f._match_frame("12356", 10)) |
---|
647 | n/a | |
---|
648 | n/a | f = tracemalloc.Filter(False, "abc", 5) |
---|
649 | n/a | self.assertTrue(f._match_frame("abc", 0)) |
---|
650 | n/a | self.assertFalse(f._match_frame("abc", 5)) |
---|
651 | n/a | self.assertTrue(f._match_frame("abc", 10)) |
---|
652 | n/a | self.assertTrue(f._match_frame("12356", 0)) |
---|
653 | n/a | self.assertTrue(f._match_frame("12356", 5)) |
---|
654 | n/a | self.assertTrue(f._match_frame("12356", 10)) |
---|
655 | n/a | |
---|
656 | n/a | # filter with line number 0 |
---|
657 | n/a | f = tracemalloc.Filter(True, "abc", 0) |
---|
658 | n/a | self.assertTrue(f._match_frame("abc", 0)) |
---|
659 | n/a | self.assertFalse(f._match_frame("abc", 5)) |
---|
660 | n/a | self.assertFalse(f._match_frame("abc", 10)) |
---|
661 | n/a | self.assertFalse(f._match_frame("12356", 0)) |
---|
662 | n/a | self.assertFalse(f._match_frame("12356", 5)) |
---|
663 | n/a | self.assertFalse(f._match_frame("12356", 10)) |
---|
664 | n/a | |
---|
665 | n/a | f = tracemalloc.Filter(False, "abc", 0) |
---|
666 | n/a | self.assertFalse(f._match_frame("abc", 0)) |
---|
667 | n/a | self.assertTrue(f._match_frame("abc", 5)) |
---|
668 | n/a | self.assertTrue(f._match_frame("abc", 10)) |
---|
669 | n/a | self.assertTrue(f._match_frame("12356", 0)) |
---|
670 | n/a | self.assertTrue(f._match_frame("12356", 5)) |
---|
671 | n/a | self.assertTrue(f._match_frame("12356", 10)) |
---|
672 | n/a | |
---|
673 | n/a | def test_filter_match_filename(self): |
---|
674 | n/a | def fnmatch(inclusive, filename, pattern): |
---|
675 | n/a | f = tracemalloc.Filter(inclusive, pattern) |
---|
676 | n/a | return f._match_frame(filename, 0) |
---|
677 | n/a | |
---|
678 | n/a | self.assertTrue(fnmatch(True, "abc", "abc")) |
---|
679 | n/a | self.assertFalse(fnmatch(True, "12356", "abc")) |
---|
680 | n/a | self.assertFalse(fnmatch(True, "<unknown>", "abc")) |
---|
681 | n/a | |
---|
682 | n/a | self.assertFalse(fnmatch(False, "abc", "abc")) |
---|
683 | n/a | self.assertTrue(fnmatch(False, "12356", "abc")) |
---|
684 | n/a | self.assertTrue(fnmatch(False, "<unknown>", "abc")) |
---|
685 | n/a | |
---|
686 | n/a | def test_filter_match_filename_joker(self): |
---|
687 | n/a | def fnmatch(filename, pattern): |
---|
688 | n/a | filter = tracemalloc.Filter(True, pattern) |
---|
689 | n/a | return filter._match_frame(filename, 0) |
---|
690 | n/a | |
---|
691 | n/a | # empty string |
---|
692 | n/a | self.assertFalse(fnmatch('abc', '')) |
---|
693 | n/a | self.assertFalse(fnmatch('', 'abc')) |
---|
694 | n/a | self.assertTrue(fnmatch('', '')) |
---|
695 | n/a | self.assertTrue(fnmatch('', '*')) |
---|
696 | n/a | |
---|
697 | n/a | # no * |
---|
698 | n/a | self.assertTrue(fnmatch('abc', 'abc')) |
---|
699 | n/a | self.assertFalse(fnmatch('abc', 'abcd')) |
---|
700 | n/a | self.assertFalse(fnmatch('abc', 'def')) |
---|
701 | n/a | |
---|
702 | n/a | # a* |
---|
703 | n/a | self.assertTrue(fnmatch('abc', 'a*')) |
---|
704 | n/a | self.assertTrue(fnmatch('abc', 'abc*')) |
---|
705 | n/a | self.assertFalse(fnmatch('abc', 'b*')) |
---|
706 | n/a | self.assertFalse(fnmatch('abc', 'abcd*')) |
---|
707 | n/a | |
---|
708 | n/a | # a*b |
---|
709 | n/a | self.assertTrue(fnmatch('abc', 'a*c')) |
---|
710 | n/a | self.assertTrue(fnmatch('abcdcx', 'a*cx')) |
---|
711 | n/a | self.assertFalse(fnmatch('abb', 'a*c')) |
---|
712 | n/a | self.assertFalse(fnmatch('abcdce', 'a*cx')) |
---|
713 | n/a | |
---|
714 | n/a | # a*b*c |
---|
715 | n/a | self.assertTrue(fnmatch('abcde', 'a*c*e')) |
---|
716 | n/a | self.assertTrue(fnmatch('abcbdefeg', 'a*bd*eg')) |
---|
717 | n/a | self.assertFalse(fnmatch('abcdd', 'a*c*e')) |
---|
718 | n/a | self.assertFalse(fnmatch('abcbdefef', 'a*bd*eg')) |
---|
719 | n/a | |
---|
720 | n/a | # replace .pyc suffix with .py |
---|
721 | n/a | self.assertTrue(fnmatch('a.pyc', 'a.py')) |
---|
722 | n/a | self.assertTrue(fnmatch('a.py', 'a.pyc')) |
---|
723 | n/a | |
---|
724 | n/a | if os.name == 'nt': |
---|
725 | n/a | # case insensitive |
---|
726 | n/a | self.assertTrue(fnmatch('aBC', 'ABc')) |
---|
727 | n/a | self.assertTrue(fnmatch('aBcDe', 'Ab*dE')) |
---|
728 | n/a | |
---|
729 | n/a | self.assertTrue(fnmatch('a.pyc', 'a.PY')) |
---|
730 | n/a | self.assertTrue(fnmatch('a.py', 'a.PYC')) |
---|
731 | n/a | else: |
---|
732 | n/a | # case sensitive |
---|
733 | n/a | self.assertFalse(fnmatch('aBC', 'ABc')) |
---|
734 | n/a | self.assertFalse(fnmatch('aBcDe', 'Ab*dE')) |
---|
735 | n/a | |
---|
736 | n/a | self.assertFalse(fnmatch('a.pyc', 'a.PY')) |
---|
737 | n/a | self.assertFalse(fnmatch('a.py', 'a.PYC')) |
---|
738 | n/a | |
---|
739 | n/a | if os.name == 'nt': |
---|
740 | n/a | # normalize alternate separator "/" to the standard separator "\" |
---|
741 | n/a | self.assertTrue(fnmatch(r'a/b', r'a\b')) |
---|
742 | n/a | self.assertTrue(fnmatch(r'a\b', r'a/b')) |
---|
743 | n/a | self.assertTrue(fnmatch(r'a/b\c', r'a\b/c')) |
---|
744 | n/a | self.assertTrue(fnmatch(r'a/b/c', r'a\b\c')) |
---|
745 | n/a | else: |
---|
746 | n/a | # there is no alternate separator |
---|
747 | n/a | self.assertFalse(fnmatch(r'a/b', r'a\b')) |
---|
748 | n/a | self.assertFalse(fnmatch(r'a\b', r'a/b')) |
---|
749 | n/a | self.assertFalse(fnmatch(r'a/b\c', r'a\b/c')) |
---|
750 | n/a | self.assertFalse(fnmatch(r'a/b/c', r'a\b\c')) |
---|
751 | n/a | |
---|
752 | n/a | # as of 3.5, .pyo is no longer munged to .py |
---|
753 | n/a | self.assertFalse(fnmatch('a.pyo', 'a.py')) |
---|
754 | n/a | |
---|
755 | n/a | def test_filter_match_trace(self): |
---|
756 | n/a | t1 = (("a.py", 2), ("b.py", 3)) |
---|
757 | n/a | t2 = (("b.py", 4), ("b.py", 5)) |
---|
758 | n/a | t3 = (("c.py", 5), ('<unknown>', 0)) |
---|
759 | n/a | unknown = (('<unknown>', 0),) |
---|
760 | n/a | |
---|
761 | n/a | f = tracemalloc.Filter(True, "b.py", all_frames=True) |
---|
762 | n/a | self.assertTrue(f._match_traceback(t1)) |
---|
763 | n/a | self.assertTrue(f._match_traceback(t2)) |
---|
764 | n/a | self.assertFalse(f._match_traceback(t3)) |
---|
765 | n/a | self.assertFalse(f._match_traceback(unknown)) |
---|
766 | n/a | |
---|
767 | n/a | f = tracemalloc.Filter(True, "b.py", all_frames=False) |
---|
768 | n/a | self.assertFalse(f._match_traceback(t1)) |
---|
769 | n/a | self.assertTrue(f._match_traceback(t2)) |
---|
770 | n/a | self.assertFalse(f._match_traceback(t3)) |
---|
771 | n/a | self.assertFalse(f._match_traceback(unknown)) |
---|
772 | n/a | |
---|
773 | n/a | f = tracemalloc.Filter(False, "b.py", all_frames=True) |
---|
774 | n/a | self.assertFalse(f._match_traceback(t1)) |
---|
775 | n/a | self.assertFalse(f._match_traceback(t2)) |
---|
776 | n/a | self.assertTrue(f._match_traceback(t3)) |
---|
777 | n/a | self.assertTrue(f._match_traceback(unknown)) |
---|
778 | n/a | |
---|
779 | n/a | f = tracemalloc.Filter(False, "b.py", all_frames=False) |
---|
780 | n/a | self.assertTrue(f._match_traceback(t1)) |
---|
781 | n/a | self.assertFalse(f._match_traceback(t2)) |
---|
782 | n/a | self.assertTrue(f._match_traceback(t3)) |
---|
783 | n/a | self.assertTrue(f._match_traceback(unknown)) |
---|
784 | n/a | |
---|
785 | n/a | f = tracemalloc.Filter(False, "<unknown>", all_frames=False) |
---|
786 | n/a | self.assertTrue(f._match_traceback(t1)) |
---|
787 | n/a | self.assertTrue(f._match_traceback(t2)) |
---|
788 | n/a | self.assertTrue(f._match_traceback(t3)) |
---|
789 | n/a | self.assertFalse(f._match_traceback(unknown)) |
---|
790 | n/a | |
---|
791 | n/a | f = tracemalloc.Filter(True, "<unknown>", all_frames=True) |
---|
792 | n/a | self.assertFalse(f._match_traceback(t1)) |
---|
793 | n/a | self.assertFalse(f._match_traceback(t2)) |
---|
794 | n/a | self.assertTrue(f._match_traceback(t3)) |
---|
795 | n/a | self.assertTrue(f._match_traceback(unknown)) |
---|
796 | n/a | |
---|
797 | n/a | f = tracemalloc.Filter(False, "<unknown>", all_frames=True) |
---|
798 | n/a | self.assertTrue(f._match_traceback(t1)) |
---|
799 | n/a | self.assertTrue(f._match_traceback(t2)) |
---|
800 | n/a | self.assertFalse(f._match_traceback(t3)) |
---|
801 | n/a | self.assertFalse(f._match_traceback(unknown)) |
---|
802 | n/a | |
---|
803 | n/a | |
---|
804 | n/a | class TestCommandLine(unittest.TestCase): |
---|
805 | n/a | def test_env_var_disabled_by_default(self): |
---|
806 | n/a | # not tracing by default |
---|
807 | n/a | code = 'import tracemalloc; print(tracemalloc.is_tracing())' |
---|
808 | n/a | ok, stdout, stderr = assert_python_ok('-c', code) |
---|
809 | n/a | stdout = stdout.rstrip() |
---|
810 | n/a | self.assertEqual(stdout, b'False') |
---|
811 | n/a | |
---|
812 | n/a | @unittest.skipIf(interpreter_requires_environment(), |
---|
813 | n/a | 'Cannot run -E tests when PYTHON env vars are required.') |
---|
814 | n/a | def test_env_var_ignored_with_E(self): |
---|
815 | n/a | """PYTHON* environment variables must be ignored when -E is present.""" |
---|
816 | n/a | code = 'import tracemalloc; print(tracemalloc.is_tracing())' |
---|
817 | n/a | ok, stdout, stderr = assert_python_ok('-E', '-c', code, PYTHONTRACEMALLOC='1') |
---|
818 | n/a | stdout = stdout.rstrip() |
---|
819 | n/a | self.assertEqual(stdout, b'False') |
---|
820 | n/a | |
---|
821 | n/a | def test_env_var_enabled_at_startup(self): |
---|
822 | n/a | # tracing at startup |
---|
823 | n/a | code = 'import tracemalloc; print(tracemalloc.is_tracing())' |
---|
824 | n/a | ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='1') |
---|
825 | n/a | stdout = stdout.rstrip() |
---|
826 | n/a | self.assertEqual(stdout, b'True') |
---|
827 | n/a | |
---|
828 | n/a | def test_env_limit(self): |
---|
829 | n/a | # start and set the number of frames |
---|
830 | n/a | code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())' |
---|
831 | n/a | ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='10') |
---|
832 | n/a | stdout = stdout.rstrip() |
---|
833 | n/a | self.assertEqual(stdout, b'10') |
---|
834 | n/a | |
---|
835 | n/a | def test_env_var_invalid(self): |
---|
836 | n/a | for nframe in (-1, 0, 2**30): |
---|
837 | n/a | with self.subTest(nframe=nframe): |
---|
838 | n/a | with support.SuppressCrashReport(): |
---|
839 | n/a | ok, stdout, stderr = assert_python_failure( |
---|
840 | n/a | '-c', 'pass', |
---|
841 | n/a | PYTHONTRACEMALLOC=str(nframe)) |
---|
842 | n/a | self.assertIn(b'PYTHONTRACEMALLOC: invalid ' |
---|
843 | n/a | b'number of frames', |
---|
844 | n/a | stderr) |
---|
845 | n/a | |
---|
846 | n/a | def test_sys_xoptions(self): |
---|
847 | n/a | for xoptions, nframe in ( |
---|
848 | n/a | ('tracemalloc', 1), |
---|
849 | n/a | ('tracemalloc=1', 1), |
---|
850 | n/a | ('tracemalloc=15', 15), |
---|
851 | n/a | ): |
---|
852 | n/a | with self.subTest(xoptions=xoptions, nframe=nframe): |
---|
853 | n/a | code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())' |
---|
854 | n/a | ok, stdout, stderr = assert_python_ok('-X', xoptions, '-c', code) |
---|
855 | n/a | stdout = stdout.rstrip() |
---|
856 | n/a | self.assertEqual(stdout, str(nframe).encode('ascii')) |
---|
857 | n/a | |
---|
858 | n/a | def test_sys_xoptions_invalid(self): |
---|
859 | n/a | for nframe in (-1, 0, 2**30): |
---|
860 | n/a | with self.subTest(nframe=nframe): |
---|
861 | n/a | with support.SuppressCrashReport(): |
---|
862 | n/a | args = ('-X', 'tracemalloc=%s' % nframe, '-c', 'pass') |
---|
863 | n/a | ok, stdout, stderr = assert_python_failure(*args) |
---|
864 | n/a | self.assertIn(b'-X tracemalloc=NFRAME: invalid ' |
---|
865 | n/a | b'number of frames', |
---|
866 | n/a | stderr) |
---|
867 | n/a | |
---|
868 | n/a | def test_pymem_alloc0(self): |
---|
869 | n/a | # Issue #21639: Check that PyMem_Malloc(0) with tracemalloc enabled |
---|
870 | n/a | # does not crash. |
---|
871 | n/a | code = 'import _testcapi; _testcapi.test_pymem_alloc0(); 1' |
---|
872 | n/a | assert_python_ok('-X', 'tracemalloc', '-c', code) |
---|
873 | n/a | |
---|
874 | n/a | |
---|
875 | n/a | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
---|
876 | n/a | class TestCAPI(unittest.TestCase): |
---|
877 | n/a | maxDiff = 80 * 20 |
---|
878 | n/a | |
---|
879 | n/a | def setUp(self): |
---|
880 | n/a | if tracemalloc.is_tracing(): |
---|
881 | n/a | self.skipTest("tracemalloc must be stopped before the test") |
---|
882 | n/a | |
---|
883 | n/a | self.domain = 5 |
---|
884 | n/a | self.size = 123 |
---|
885 | n/a | self.obj = allocate_bytes(self.size)[0] |
---|
886 | n/a | |
---|
887 | n/a | # for the type "object", id(obj) is the address of its memory block. |
---|
888 | n/a | # This type is not tracked by the garbage collector |
---|
889 | n/a | self.ptr = id(self.obj) |
---|
890 | n/a | |
---|
891 | n/a | def tearDown(self): |
---|
892 | n/a | tracemalloc.stop() |
---|
893 | n/a | |
---|
894 | n/a | def get_traceback(self): |
---|
895 | n/a | frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr) |
---|
896 | n/a | if frames is not None: |
---|
897 | n/a | return tracemalloc.Traceback(frames) |
---|
898 | n/a | else: |
---|
899 | n/a | return None |
---|
900 | n/a | |
---|
901 | n/a | def track(self, release_gil=False, nframe=1): |
---|
902 | n/a | frames = get_frames(nframe, 2) |
---|
903 | n/a | _testcapi.tracemalloc_track(self.domain, self.ptr, self.size, |
---|
904 | n/a | release_gil) |
---|
905 | n/a | return frames |
---|
906 | n/a | |
---|
907 | n/a | def untrack(self): |
---|
908 | n/a | _testcapi.tracemalloc_untrack(self.domain, self.ptr) |
---|
909 | n/a | |
---|
910 | n/a | def get_traced_memory(self): |
---|
911 | n/a | # Get the traced size in the domain |
---|
912 | n/a | snapshot = tracemalloc.take_snapshot() |
---|
913 | n/a | domain_filter = tracemalloc.DomainFilter(True, self.domain) |
---|
914 | n/a | snapshot = snapshot.filter_traces([domain_filter]) |
---|
915 | n/a | return sum(trace.size for trace in snapshot.traces) |
---|
916 | n/a | |
---|
917 | n/a | def check_track(self, release_gil): |
---|
918 | n/a | nframe = 5 |
---|
919 | n/a | tracemalloc.start(nframe) |
---|
920 | n/a | |
---|
921 | n/a | size = tracemalloc.get_traced_memory()[0] |
---|
922 | n/a | |
---|
923 | n/a | frames = self.track(release_gil, nframe) |
---|
924 | n/a | self.assertEqual(self.get_traceback(), |
---|
925 | n/a | tracemalloc.Traceback(frames)) |
---|
926 | n/a | |
---|
927 | n/a | self.assertEqual(self.get_traced_memory(), self.size) |
---|
928 | n/a | |
---|
929 | n/a | def test_track(self): |
---|
930 | n/a | self.check_track(False) |
---|
931 | n/a | |
---|
932 | n/a | def test_track_without_gil(self): |
---|
933 | n/a | # check that calling _PyTraceMalloc_Track() without holding the GIL |
---|
934 | n/a | # works too |
---|
935 | n/a | self.check_track(True) |
---|
936 | n/a | |
---|
937 | n/a | def test_track_already_tracked(self): |
---|
938 | n/a | nframe = 5 |
---|
939 | n/a | tracemalloc.start(nframe) |
---|
940 | n/a | |
---|
941 | n/a | # track a first time |
---|
942 | n/a | self.track() |
---|
943 | n/a | |
---|
944 | n/a | # calling _PyTraceMalloc_Track() must remove the old trace and add |
---|
945 | n/a | # a new trace with the new traceback |
---|
946 | n/a | frames = self.track(nframe=nframe) |
---|
947 | n/a | self.assertEqual(self.get_traceback(), |
---|
948 | n/a | tracemalloc.Traceback(frames)) |
---|
949 | n/a | |
---|
950 | n/a | def test_untrack(self): |
---|
951 | n/a | tracemalloc.start() |
---|
952 | n/a | |
---|
953 | n/a | self.track() |
---|
954 | n/a | self.assertIsNotNone(self.get_traceback()) |
---|
955 | n/a | self.assertEqual(self.get_traced_memory(), self.size) |
---|
956 | n/a | |
---|
957 | n/a | # untrack must remove the trace |
---|
958 | n/a | self.untrack() |
---|
959 | n/a | self.assertIsNone(self.get_traceback()) |
---|
960 | n/a | self.assertEqual(self.get_traced_memory(), 0) |
---|
961 | n/a | |
---|
962 | n/a | # calling _PyTraceMalloc_Untrack() multiple times must not crash |
---|
963 | n/a | self.untrack() |
---|
964 | n/a | self.untrack() |
---|
965 | n/a | |
---|
966 | n/a | def test_stop_track(self): |
---|
967 | n/a | tracemalloc.start() |
---|
968 | n/a | tracemalloc.stop() |
---|
969 | n/a | |
---|
970 | n/a | with self.assertRaises(RuntimeError): |
---|
971 | n/a | self.track() |
---|
972 | n/a | self.assertIsNone(self.get_traceback()) |
---|
973 | n/a | |
---|
974 | n/a | def test_stop_untrack(self): |
---|
975 | n/a | tracemalloc.start() |
---|
976 | n/a | self.track() |
---|
977 | n/a | |
---|
978 | n/a | tracemalloc.stop() |
---|
979 | n/a | with self.assertRaises(RuntimeError): |
---|
980 | n/a | self.untrack() |
---|
981 | n/a | |
---|
982 | n/a | |
---|
983 | n/a | def test_main(): |
---|
984 | n/a | support.run_unittest( |
---|
985 | n/a | TestTracemallocEnabled, |
---|
986 | n/a | TestSnapshot, |
---|
987 | n/a | TestFilters, |
---|
988 | n/a | TestCommandLine, |
---|
989 | n/a | TestCAPI, |
---|
990 | n/a | ) |
---|
991 | n/a | |
---|
992 | n/a | if __name__ == "__main__": |
---|
993 | n/a | test_main() |
---|