annotate Lib/test/test_tracemalloc.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents eead17ba32d8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
1 import contextlib
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
2 import os
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
3 import sys
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
4 import tracemalloc
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
5 import unittest
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
6 from unittest.mock import patch
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
7 from test.script_helper import assert_python_ok, assert_python_failure
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
8 from test import support
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
9 try:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
10 import threading
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
11 except ImportError:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
12 threading = None
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
13
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
14 EMPTY_STRING_SIZE = sys.getsizeof(b'')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
15
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
16 def get_frames(nframe, lineno_delta):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
17 frames = []
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
18 frame = sys._getframe(1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
19 for index in range(nframe):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
20 code = frame.f_code
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
21 lineno = frame.f_lineno + lineno_delta
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
22 frames.append((code.co_filename, lineno))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
23 lineno_delta = 0
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
24 frame = frame.f_back
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
25 if frame is None:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
26 break
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
27 return tuple(frames)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
28
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
29 def allocate_bytes(size):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
30 nframe = tracemalloc.get_traceback_limit()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
31 bytes_len = (size - EMPTY_STRING_SIZE)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
32 frames = get_frames(nframe, 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
33 data = b'x' * bytes_len
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
34 return data, tracemalloc.Traceback(frames)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
35
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
36 def create_snapshots():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
37 traceback_limit = 2
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
38
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
39 raw_traces = [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
40 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
41 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
42 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
43
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
44 (2, (('a.py', 5), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
45
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
46 (66, (('b.py', 1),)),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
47
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
48 (7, (('<unknown>', 0),)),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
49 ]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
50 snapshot = tracemalloc.Snapshot(raw_traces, traceback_limit)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
51
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
52 raw_traces2 = [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
53 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
54 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
55 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
56
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
57 (2, (('a.py', 5), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
58 (5000, (('a.py', 5), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
59
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
60 (400, (('c.py', 578),)),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
61 ]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
62 snapshot2 = tracemalloc.Snapshot(raw_traces2, traceback_limit)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
63
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
64 return (snapshot, snapshot2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
65
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
66 def frame(filename, lineno):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
67 return tracemalloc._Frame((filename, lineno))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
68
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
69 def traceback(*frames):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
70 return tracemalloc.Traceback(frames)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
71
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
72 def traceback_lineno(filename, lineno):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
73 return traceback((filename, lineno))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
74
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
75 def traceback_filename(filename):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
76 return traceback_lineno(filename, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
77
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
78
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
79 class TestTracemallocEnabled(unittest.TestCase):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
80 def setUp(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
81 if tracemalloc.is_tracing():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
82 self.skipTest("tracemalloc must be stopped before the test")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
83
87402
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
84 tracemalloc.start(1)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
85
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
86 def tearDown(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
87 tracemalloc.stop()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
88
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
89 def test_get_tracemalloc_memory(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
90 data = [allocate_bytes(123) for count in range(1000)]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
91 size = tracemalloc.get_tracemalloc_memory()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
92 self.assertGreaterEqual(size, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
93
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
94 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
95 size2 = tracemalloc.get_tracemalloc_memory()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
96 self.assertGreaterEqual(size2, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
97 self.assertLessEqual(size2, size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
98
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
99 def test_get_object_traceback(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
100 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
101 obj_size = 12345
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
102 obj, obj_traceback = allocate_bytes(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
103 traceback = tracemalloc.get_object_traceback(obj)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
104 self.assertEqual(traceback, obj_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
105
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
106 def test_set_traceback_limit(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
107 obj_size = 10
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
108
87402
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
109 tracemalloc.stop()
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
110 self.assertRaises(ValueError, tracemalloc.start, -1)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
111
87402
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
112 tracemalloc.stop()
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
113 tracemalloc.start(10)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
114 obj2, obj2_traceback = allocate_bytes(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
115 traceback = tracemalloc.get_object_traceback(obj2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
116 self.assertEqual(len(traceback), 10)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
117 self.assertEqual(traceback, obj2_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
118
87402
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
119 tracemalloc.stop()
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
120 tracemalloc.start(1)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
121 obj, obj_traceback = allocate_bytes(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
122 traceback = tracemalloc.get_object_traceback(obj)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
123 self.assertEqual(len(traceback), 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
124 self.assertEqual(traceback, obj_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
125
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
126
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
127 def find_trace(self, traces, traceback):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
128 for trace in traces:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
129 if trace[1] == traceback._frames:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
130 return trace
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
131
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
132 self.fail("trace not found")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
133
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
134 def test_get_traces(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
135 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
136 obj_size = 12345
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
137 obj, obj_traceback = allocate_bytes(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
138
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
139 traces = tracemalloc._get_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
140 trace = self.find_trace(traces, obj_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
141
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
142 self.assertIsInstance(trace, tuple)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
143 size, traceback = trace
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
144 self.assertEqual(size, obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
145 self.assertEqual(traceback, obj_traceback._frames)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
146
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
147 tracemalloc.stop()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
148 self.assertEqual(tracemalloc._get_traces(), [])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
149
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
150
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
151 def test_get_traces_intern_traceback(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
152 # dummy wrappers to get more useful and identical frames in the traceback
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
153 def allocate_bytes2(size):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
154 return allocate_bytes(size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
155 def allocate_bytes3(size):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
156 return allocate_bytes2(size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
157 def allocate_bytes4(size):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
158 return allocate_bytes3(size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
159
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
160 # Ensure that two identical tracebacks are not duplicated
87402
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
161 tracemalloc.stop()
66db0c66a6ee Issue #18874: Remove tracemalloc.set_traceback_limit()
Victor Stinner <victor.stinner@gmail.com>
parents: 87401
diff changeset
162 tracemalloc.start(4)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
163 obj_size = 123
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
164 obj1, obj1_traceback = allocate_bytes4(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
165 obj2, obj2_traceback = allocate_bytes4(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
166
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
167 traces = tracemalloc._get_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
168
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
169 trace1 = self.find_trace(traces, obj1_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
170 trace2 = self.find_trace(traces, obj2_traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
171 size1, traceback1 = trace1
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
172 size2, traceback2 = trace2
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
173 self.assertEqual(traceback2, traceback1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
174 self.assertIs(traceback2, traceback1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
175
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
176 def test_get_traced_memory(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
177 # Python allocates some internals objects, so the test must tolerate
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
178 # a small difference between the expected size and the real usage
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
179 max_error = 2048
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
180
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
181 # allocate one object
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
182 obj_size = 1024 * 1024
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
183 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
184 obj, obj_traceback = allocate_bytes(obj_size)
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
185 size, peak_size = tracemalloc.get_traced_memory()
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
186 self.assertGreaterEqual(size, obj_size)
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
187 self.assertGreaterEqual(peak_size, size)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
188
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
189 self.assertLessEqual(size - obj_size, max_error)
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
190 self.assertLessEqual(peak_size - size, max_error)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
191
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
192 # destroy the object
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
193 obj = None
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
194 size2, peak_size2 = tracemalloc.get_traced_memory()
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
195 self.assertLess(size2, size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
196 self.assertGreaterEqual(size - size2, obj_size - max_error)
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
197 self.assertGreaterEqual(peak_size2, peak_size)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
198
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
199 # clear_traces() must reset traced memory counters
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
200 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
201 self.assertEqual(tracemalloc.get_traced_memory(), (0, 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
202
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
203 # allocate another object
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
204 obj, obj_traceback = allocate_bytes(obj_size)
87615
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
205 size, peak_size = tracemalloc.get_traced_memory()
553144bd7bf1 Close #19798: replace "maximum" term with "peak" in get_traced_memory()
Victor Stinner <victor.stinner@gmail.com>
parents: 87547
diff changeset
206 self.assertGreaterEqual(size, obj_size)
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
207
87547
841dec769a04 Cleanup test_tracemalloc.py. Patch written by Vajrasky Kok.
Victor Stinner <victor.stinner@gmail.com>
parents: 87402
diff changeset
208 # stop() also resets traced memory counters
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
209 tracemalloc.stop()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
210 self.assertEqual(tracemalloc.get_traced_memory(), (0, 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
211
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
212 def test_clear_traces(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
213 obj, obj_traceback = allocate_bytes(123)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
214 traceback = tracemalloc.get_object_traceback(obj)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
215 self.assertIsNotNone(traceback)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
216
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
217 tracemalloc.clear_traces()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
218 traceback2 = tracemalloc.get_object_traceback(obj)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
219 self.assertIsNone(traceback2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
220
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
221 def test_is_tracing(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
222 tracemalloc.stop()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
223 self.assertFalse(tracemalloc.is_tracing())
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
224
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
225 tracemalloc.start()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
226 self.assertTrue(tracemalloc.is_tracing())
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
227
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
228 def test_snapshot(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
229 obj, source = allocate_bytes(123)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
230
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
231 # take a snapshot
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
232 snapshot = tracemalloc.take_snapshot()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
233
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
234 # write on disk
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
235 snapshot.dump(support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
236 self.addCleanup(support.unlink, support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
237
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
238 # load from disk
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
239 snapshot2 = tracemalloc.Snapshot.load(support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
240 self.assertEqual(snapshot2.traces, snapshot.traces)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
241
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
242 # tracemalloc must be tracing memory allocations to take a snapshot
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
243 tracemalloc.stop()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
244 with self.assertRaises(RuntimeError) as cm:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
245 tracemalloc.take_snapshot()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
246 self.assertEqual(str(cm.exception),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
247 "the tracemalloc module must be tracing memory "
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
248 "allocations to take a snapshot")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
249
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
250 def test_snapshot_save_attr(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
251 # take a snapshot with a new attribute
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
252 snapshot = tracemalloc.take_snapshot()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
253 snapshot.test_attr = "new"
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
254 snapshot.dump(support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
255 self.addCleanup(support.unlink, support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
256
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
257 # load() should recreates the attribute
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
258 snapshot2 = tracemalloc.Snapshot.load(support.TESTFN)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
259 self.assertEqual(snapshot2.test_attr, "new")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
260
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
261 def fork_child(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
262 if not tracemalloc.is_tracing():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
263 return 2
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
264
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
265 obj_size = 12345
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
266 obj, obj_traceback = allocate_bytes(obj_size)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
267 traceback = tracemalloc.get_object_traceback(obj)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
268 if traceback is None:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
269 return 3
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
270
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
271 # everything is fine
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
272 return 0
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
273
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
274 @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork()')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
275 def test_fork(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
276 # check that tracemalloc is still working after fork
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
277 pid = os.fork()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
278 if not pid:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
279 # child
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
280 exitcode = 1
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
281 try:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
282 exitcode = self.fork_child()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
283 finally:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
284 os._exit(exitcode)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
285 else:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
286 pid2, status = os.waitpid(pid, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
287 self.assertTrue(os.WIFEXITED(status))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
288 exitcode = os.WEXITSTATUS(status)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
289 self.assertEqual(exitcode, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
290
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
291
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
292 class TestSnapshot(unittest.TestCase):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
293 maxDiff = 4000
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
294
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
295 def test_create_snapshot(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
296 raw_traces = [(5, (('a.py', 2),))]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
297
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
298 with contextlib.ExitStack() as stack:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
299 stack.enter_context(patch.object(tracemalloc, 'is_tracing',
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
300 return_value=True))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
301 stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit',
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
302 return_value=5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
303 stack.enter_context(patch.object(tracemalloc, '_get_traces',
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
304 return_value=raw_traces))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
305
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
306 snapshot = tracemalloc.take_snapshot()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
307 self.assertEqual(snapshot.traceback_limit, 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
308 self.assertEqual(len(snapshot.traces), 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
309 trace = snapshot.traces[0]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
310 self.assertEqual(trace.size, 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
311 self.assertEqual(len(trace.traceback), 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
312 self.assertEqual(trace.traceback[0].filename, 'a.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
313 self.assertEqual(trace.traceback[0].lineno, 2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
314
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
315 def test_filter_traces(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
316 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
317 filter1 = tracemalloc.Filter(False, "b.py")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
318 filter2 = tracemalloc.Filter(True, "a.py", 2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
319 filter3 = tracemalloc.Filter(True, "a.py", 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
320
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
321 original_traces = list(snapshot.traces._traces)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
322
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
323 # exclude b.py
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
324 snapshot3 = snapshot.filter_traces((filter1,))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
325 self.assertEqual(snapshot3.traces._traces, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
326 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
327 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
328 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
329 (2, (('a.py', 5), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
330 (7, (('<unknown>', 0),)),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
331 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
332
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
333 # filter_traces() must not touch the original snapshot
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
334 self.assertEqual(snapshot.traces._traces, original_traces)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
335
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
336 # only include two lines of a.py
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
337 snapshot4 = snapshot3.filter_traces((filter2, filter3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
338 self.assertEqual(snapshot4.traces._traces, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
339 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
340 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
341 (10, (('a.py', 2), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
342 (2, (('a.py', 5), ('b.py', 4))),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
343 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
344
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
345 # No filter: just duplicate the snapshot
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
346 snapshot5 = snapshot.filter_traces(())
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
347 self.assertIsNot(snapshot5, snapshot)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
348 self.assertIsNot(snapshot5.traces, snapshot.traces)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
349 self.assertEqual(snapshot5.traces, snapshot.traces)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
350
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
351 def test_snapshot_group_by_line(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
352 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
353 tb_0 = traceback_lineno('<unknown>', 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
354 tb_a_2 = traceback_lineno('a.py', 2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
355 tb_a_5 = traceback_lineno('a.py', 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
356 tb_b_1 = traceback_lineno('b.py', 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
357 tb_c_578 = traceback_lineno('c.py', 578)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
358
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
359 # stats per file and line
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
360 stats1 = snapshot.statistics('lineno')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
361 self.assertEqual(stats1, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
362 tracemalloc.Statistic(tb_b_1, 66, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
363 tracemalloc.Statistic(tb_a_2, 30, 3),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
364 tracemalloc.Statistic(tb_0, 7, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
365 tracemalloc.Statistic(tb_a_5, 2, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
366 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
367
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
368 # stats per file and line (2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
369 stats2 = snapshot2.statistics('lineno')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
370 self.assertEqual(stats2, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
371 tracemalloc.Statistic(tb_a_5, 5002, 2),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
372 tracemalloc.Statistic(tb_c_578, 400, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
373 tracemalloc.Statistic(tb_a_2, 30, 3),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
374 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
375
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
376 # stats diff per file and line
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
377 statistics = snapshot2.compare_to(snapshot, 'lineno')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
378 self.assertEqual(statistics, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
379 tracemalloc.StatisticDiff(tb_a_5, 5002, 5000, 2, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
380 tracemalloc.StatisticDiff(tb_c_578, 400, 400, 1, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
381 tracemalloc.StatisticDiff(tb_b_1, 0, -66, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
382 tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
383 tracemalloc.StatisticDiff(tb_a_2, 30, 0, 3, 0),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
384 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
385
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
386 def test_snapshot_group_by_file(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
387 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
388 tb_0 = traceback_filename('<unknown>')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
389 tb_a = traceback_filename('a.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
390 tb_b = traceback_filename('b.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
391 tb_c = traceback_filename('c.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
392
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
393 # stats per file
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
394 stats1 = snapshot.statistics('filename')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
395 self.assertEqual(stats1, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
396 tracemalloc.Statistic(tb_b, 66, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
397 tracemalloc.Statistic(tb_a, 32, 4),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
398 tracemalloc.Statistic(tb_0, 7, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
399 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
400
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
401 # stats per file (2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
402 stats2 = snapshot2.statistics('filename')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
403 self.assertEqual(stats2, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
404 tracemalloc.Statistic(tb_a, 5032, 5),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
405 tracemalloc.Statistic(tb_c, 400, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
406 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
407
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
408 # stats diff per file
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
409 diff = snapshot2.compare_to(snapshot, 'filename')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
410 self.assertEqual(diff, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
411 tracemalloc.StatisticDiff(tb_a, 5032, 5000, 5, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
412 tracemalloc.StatisticDiff(tb_c, 400, 400, 1, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
413 tracemalloc.StatisticDiff(tb_b, 0, -66, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
414 tracemalloc.StatisticDiff(tb_0, 0, -7, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
415 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
416
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
417 def test_snapshot_group_by_traceback(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
418 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
419
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
420 # stats per file
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
421 tb1 = traceback(('a.py', 2), ('b.py', 4))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
422 tb2 = traceback(('a.py', 5), ('b.py', 4))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
423 tb3 = traceback(('b.py', 1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
424 tb4 = traceback(('<unknown>', 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
425 stats1 = snapshot.statistics('traceback')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
426 self.assertEqual(stats1, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
427 tracemalloc.Statistic(tb3, 66, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
428 tracemalloc.Statistic(tb1, 30, 3),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
429 tracemalloc.Statistic(tb4, 7, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
430 tracemalloc.Statistic(tb2, 2, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
431 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
432
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
433 # stats per file (2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
434 tb5 = traceback(('c.py', 578))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
435 stats2 = snapshot2.statistics('traceback')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
436 self.assertEqual(stats2, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
437 tracemalloc.Statistic(tb2, 5002, 2),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
438 tracemalloc.Statistic(tb5, 400, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
439 tracemalloc.Statistic(tb1, 30, 3),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
440 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
441
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
442 # stats diff per file
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
443 diff = snapshot2.compare_to(snapshot, 'traceback')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
444 self.assertEqual(diff, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
445 tracemalloc.StatisticDiff(tb2, 5002, 5000, 2, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
446 tracemalloc.StatisticDiff(tb5, 400, 400, 1, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
447 tracemalloc.StatisticDiff(tb3, 0, -66, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
448 tracemalloc.StatisticDiff(tb4, 0, -7, 0, -1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
449 tracemalloc.StatisticDiff(tb1, 30, 0, 3, 0),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
450 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
451
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
452 self.assertRaises(ValueError,
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
453 snapshot.statistics, 'traceback', cumulative=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
454
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
455 def test_snapshot_group_by_cumulative(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
456 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
457 tb_0 = traceback_filename('<unknown>')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
458 tb_a = traceback_filename('a.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
459 tb_b = traceback_filename('b.py')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
460 tb_a_2 = traceback_lineno('a.py', 2)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
461 tb_a_5 = traceback_lineno('a.py', 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
462 tb_b_1 = traceback_lineno('b.py', 1)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
463 tb_b_4 = traceback_lineno('b.py', 4)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
464
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
465 # per file
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
466 stats = snapshot.statistics('filename', True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
467 self.assertEqual(stats, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
468 tracemalloc.Statistic(tb_b, 98, 5),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
469 tracemalloc.Statistic(tb_a, 32, 4),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
470 tracemalloc.Statistic(tb_0, 7, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
471 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
472
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
473 # per line
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
474 stats = snapshot.statistics('lineno', True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
475 self.assertEqual(stats, [
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
476 tracemalloc.Statistic(tb_b_1, 66, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
477 tracemalloc.Statistic(tb_b_4, 32, 4),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
478 tracemalloc.Statistic(tb_a_2, 30, 3),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
479 tracemalloc.Statistic(tb_0, 7, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
480 tracemalloc.Statistic(tb_a_5, 2, 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
481 ])
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
482
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
483 def test_trace_format(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
484 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
485 trace = snapshot.traces[0]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
486 self.assertEqual(str(trace), 'a.py:2: 10 B')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
487 traceback = trace.traceback
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
488 self.assertEqual(str(traceback), 'a.py:2')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
489 frame = traceback[0]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
490 self.assertEqual(str(frame), 'a.py:2')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
491
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
492 def test_statistic_format(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
493 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
494 stats = snapshot.statistics('lineno')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
495 stat = stats[0]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
496 self.assertEqual(str(stat),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
497 'b.py:1: size=66 B, count=1, average=66 B')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
498
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
499 def test_statistic_diff_format(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
500 snapshot, snapshot2 = create_snapshots()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
501 stats = snapshot2.compare_to(snapshot, 'lineno')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
502 stat = stats[0]
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
503 self.assertEqual(str(stat),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
504 'a.py:5: size=5002 B (+5000 B), count=2 (+1), average=2501 B')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
505
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
506
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
507
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
508 class TestFilters(unittest.TestCase):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
509 maxDiff = 2048
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
510
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
511 def test_filter_attributes(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
512 # test default values
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
513 f = tracemalloc.Filter(True, "abc")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
514 self.assertEqual(f.inclusive, True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
515 self.assertEqual(f.filename_pattern, "abc")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
516 self.assertIsNone(f.lineno)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
517 self.assertEqual(f.all_frames, False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
518
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
519 # test custom values
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
520 f = tracemalloc.Filter(False, "test.py", 123, True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
521 self.assertEqual(f.inclusive, False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
522 self.assertEqual(f.filename_pattern, "test.py")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
523 self.assertEqual(f.lineno, 123)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
524 self.assertEqual(f.all_frames, True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
525
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
526 # parameters passed by keyword
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
527 f = tracemalloc.Filter(inclusive=False, filename_pattern="test.py", lineno=123, all_frames=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
528 self.assertEqual(f.inclusive, False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
529 self.assertEqual(f.filename_pattern, "test.py")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
530 self.assertEqual(f.lineno, 123)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
531 self.assertEqual(f.all_frames, True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
532
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
533 # read-only attribute
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
534 self.assertRaises(AttributeError, setattr, f, "filename_pattern", "abc")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
535
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
536 def test_filter_match(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
537 # filter without line number
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
538 f = tracemalloc.Filter(True, "abc")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
539 self.assertTrue(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
540 self.assertTrue(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
541 self.assertTrue(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
542 self.assertFalse(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
543 self.assertFalse(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
544 self.assertFalse(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
545
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
546 f = tracemalloc.Filter(False, "abc")
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
547 self.assertFalse(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
548 self.assertFalse(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
549 self.assertFalse(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
550 self.assertTrue(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
551 self.assertTrue(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
552 self.assertTrue(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
553
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
554 # filter with line number > 0
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
555 f = tracemalloc.Filter(True, "abc", 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
556 self.assertFalse(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
557 self.assertTrue(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
558 self.assertFalse(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
559 self.assertFalse(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
560 self.assertFalse(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
561 self.assertFalse(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
562
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
563 f = tracemalloc.Filter(False, "abc", 5)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
564 self.assertTrue(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
565 self.assertFalse(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
566 self.assertTrue(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
567 self.assertTrue(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
568 self.assertTrue(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
569 self.assertTrue(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
570
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
571 # filter with line number 0
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
572 f = tracemalloc.Filter(True, "abc", 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
573 self.assertTrue(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
574 self.assertFalse(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
575 self.assertFalse(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
576 self.assertFalse(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
577 self.assertFalse(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
578 self.assertFalse(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
579
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
580 f = tracemalloc.Filter(False, "abc", 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
581 self.assertFalse(f._match_frame("abc", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
582 self.assertTrue(f._match_frame("abc", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
583 self.assertTrue(f._match_frame("abc", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
584 self.assertTrue(f._match_frame("12356", 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
585 self.assertTrue(f._match_frame("12356", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
586 self.assertTrue(f._match_frame("12356", 10))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
587
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
588 def test_filter_match_filename(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
589 def fnmatch(inclusive, filename, pattern):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
590 f = tracemalloc.Filter(inclusive, pattern)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
591 return f._match_frame(filename, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
592
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
593 self.assertTrue(fnmatch(True, "abc", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
594 self.assertFalse(fnmatch(True, "12356", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
595 self.assertFalse(fnmatch(True, "<unknown>", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
596
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
597 self.assertFalse(fnmatch(False, "abc", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
598 self.assertTrue(fnmatch(False, "12356", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
599 self.assertTrue(fnmatch(False, "<unknown>", "abc"))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
600
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
601 def test_filter_match_filename_joker(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
602 def fnmatch(filename, pattern):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
603 filter = tracemalloc.Filter(True, pattern)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
604 return filter._match_frame(filename, 0)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
605
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
606 # empty string
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
607 self.assertFalse(fnmatch('abc', ''))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
608 self.assertFalse(fnmatch('', 'abc'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
609 self.assertTrue(fnmatch('', ''))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
610 self.assertTrue(fnmatch('', '*'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
611
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
612 # no *
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
613 self.assertTrue(fnmatch('abc', 'abc'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
614 self.assertFalse(fnmatch('abc', 'abcd'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
615 self.assertFalse(fnmatch('abc', 'def'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
616
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
617 # a*
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
618 self.assertTrue(fnmatch('abc', 'a*'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
619 self.assertTrue(fnmatch('abc', 'abc*'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
620 self.assertFalse(fnmatch('abc', 'b*'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
621 self.assertFalse(fnmatch('abc', 'abcd*'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
622
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
623 # a*b
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
624 self.assertTrue(fnmatch('abc', 'a*c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
625 self.assertTrue(fnmatch('abcdcx', 'a*cx'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
626 self.assertFalse(fnmatch('abb', 'a*c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
627 self.assertFalse(fnmatch('abcdce', 'a*cx'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
628
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
629 # a*b*c
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
630 self.assertTrue(fnmatch('abcde', 'a*c*e'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
631 self.assertTrue(fnmatch('abcbdefeg', 'a*bd*eg'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
632 self.assertFalse(fnmatch('abcdd', 'a*c*e'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
633 self.assertFalse(fnmatch('abcbdefef', 'a*bd*eg'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
634
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
635 # replace .pyc and .pyo suffix with .py
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
636 self.assertTrue(fnmatch('a.pyc', 'a.py'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
637 self.assertTrue(fnmatch('a.pyo', 'a.py'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
638 self.assertTrue(fnmatch('a.py', 'a.pyc'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
639 self.assertTrue(fnmatch('a.py', 'a.pyo'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
640
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
641 if os.name == 'nt':
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
642 # case insensitive
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
643 self.assertTrue(fnmatch('aBC', 'ABc'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
644 self.assertTrue(fnmatch('aBcDe', 'Ab*dE'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
645
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
646 self.assertTrue(fnmatch('a.pyc', 'a.PY'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
647 self.assertTrue(fnmatch('a.PYO', 'a.py'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
648 self.assertTrue(fnmatch('a.py', 'a.PYC'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
649 self.assertTrue(fnmatch('a.PY', 'a.pyo'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
650 else:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
651 # case sensitive
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
652 self.assertFalse(fnmatch('aBC', 'ABc'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
653 self.assertFalse(fnmatch('aBcDe', 'Ab*dE'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
654
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
655 self.assertFalse(fnmatch('a.pyc', 'a.PY'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
656 self.assertFalse(fnmatch('a.PYO', 'a.py'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
657 self.assertFalse(fnmatch('a.py', 'a.PYC'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
658 self.assertFalse(fnmatch('a.PY', 'a.pyo'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
659
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
660 if os.name == 'nt':
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
661 # normalize alternate separator "/" to the standard separator "\"
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
662 self.assertTrue(fnmatch(r'a/b', r'a\b'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
663 self.assertTrue(fnmatch(r'a\b', r'a/b'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
664 self.assertTrue(fnmatch(r'a/b\c', r'a\b/c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
665 self.assertTrue(fnmatch(r'a/b/c', r'a\b\c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
666 else:
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
667 # there is no alternate separator
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
668 self.assertFalse(fnmatch(r'a/b', r'a\b'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
669 self.assertFalse(fnmatch(r'a\b', r'a/b'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
670 self.assertFalse(fnmatch(r'a/b\c', r'a\b/c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
671 self.assertFalse(fnmatch(r'a/b/c', r'a\b\c'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
672
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
673 def test_filter_match_trace(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
674 t1 = (("a.py", 2), ("b.py", 3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
675 t2 = (("b.py", 4), ("b.py", 5))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
676 t3 = (("c.py", 5), ('<unknown>', 0))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
677 unknown = (('<unknown>', 0),)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
678
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
679 f = tracemalloc.Filter(True, "b.py", all_frames=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
680 self.assertTrue(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
681 self.assertTrue(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
682 self.assertFalse(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
683 self.assertFalse(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
684
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
685 f = tracemalloc.Filter(True, "b.py", all_frames=False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
686 self.assertFalse(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
687 self.assertTrue(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
688 self.assertFalse(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
689 self.assertFalse(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
690
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
691 f = tracemalloc.Filter(False, "b.py", all_frames=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
692 self.assertFalse(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
693 self.assertFalse(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
694 self.assertTrue(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
695 self.assertTrue(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
696
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
697 f = tracemalloc.Filter(False, "b.py", all_frames=False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
698 self.assertTrue(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
699 self.assertFalse(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
700 self.assertTrue(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
701 self.assertTrue(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
702
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
703 f = tracemalloc.Filter(False, "<unknown>", all_frames=False)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
704 self.assertTrue(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
705 self.assertTrue(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
706 self.assertTrue(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
707 self.assertFalse(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
708
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
709 f = tracemalloc.Filter(True, "<unknown>", all_frames=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
710 self.assertFalse(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
711 self.assertFalse(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
712 self.assertTrue(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
713 self.assertTrue(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
714
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
715 f = tracemalloc.Filter(False, "<unknown>", all_frames=True)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
716 self.assertTrue(f._match_traceback(t1))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
717 self.assertTrue(f._match_traceback(t2))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
718 self.assertFalse(f._match_traceback(t3))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
719 self.assertFalse(f._match_traceback(unknown))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
720
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
721
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
722 class TestCommandLine(unittest.TestCase):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
723 def test_env_var(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
724 # not tracing by default
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
725 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
726 ok, stdout, stderr = assert_python_ok('-c', code)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
727 stdout = stdout.rstrip()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
728 self.assertEqual(stdout, b'False')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
729
87547
841dec769a04 Cleanup test_tracemalloc.py. Patch written by Vajrasky Kok.
Victor Stinner <victor.stinner@gmail.com>
parents: 87402
diff changeset
730 # PYTHON* environment variables must be ignored when -E option is
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
731 # present
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
732 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
733 ok, stdout, stderr = assert_python_ok('-E', '-c', code, PYTHONTRACEMALLOC='1')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
734 stdout = stdout.rstrip()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
735 self.assertEqual(stdout, b'False')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
736
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
737 # tracing at startup
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
738 code = 'import tracemalloc; print(tracemalloc.is_tracing())'
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
739 ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='1')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
740 stdout = stdout.rstrip()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
741 self.assertEqual(stdout, b'True')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
742
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
743 # start and set the number of frames
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
744 code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())'
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
745 ok, stdout, stderr = assert_python_ok('-c', code, PYTHONTRACEMALLOC='10')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
746 stdout = stdout.rstrip()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
747 self.assertEqual(stdout, b'10')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
748
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
749 def test_env_var_invalid(self):
87616
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
750 for nframe in (-1, 0, 2**30):
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
751 with self.subTest(nframe=nframe):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
752 with support.SuppressCrashReport():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
753 ok, stdout, stderr = assert_python_failure(
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
754 '-c', 'pass',
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
755 PYTHONTRACEMALLOC=str(nframe))
87616
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
756 self.assertIn(b'PYTHONTRACEMALLOC: invalid '
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
757 b'number of frames',
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
758 stderr)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
759
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
760 def test_sys_xoptions(self):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
761 for xoptions, nframe in (
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
762 ('tracemalloc', 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
763 ('tracemalloc=1', 1),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
764 ('tracemalloc=15', 15),
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
765 ):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
766 with self.subTest(xoptions=xoptions, nframe=nframe):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
767 code = 'import tracemalloc; print(tracemalloc.get_traceback_limit())'
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
768 ok, stdout, stderr = assert_python_ok('-X', xoptions, '-c', code)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
769 stdout = stdout.rstrip()
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
770 self.assertEqual(stdout, str(nframe).encode('ascii'))
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
771
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
772 def test_sys_xoptions_invalid(self):
87616
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
773 for nframe in (-1, 0, 2**30):
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
774 with self.subTest(nframe=nframe):
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
775 with support.SuppressCrashReport():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
776 args = ('-X', 'tracemalloc=%s' % nframe, '-c', 'pass')
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
777 ok, stdout, stderr = assert_python_failure(*args)
87616
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
778 self.assertIn(b'-X tracemalloc=NFRAME: invalid '
eead17ba32d8 Closes #19786: tracemalloc, remove the arbitrary limit of 100 frames
Victor Stinner <victor.stinner@gmail.com>
parents: 87615
diff changeset
779 b'number of frames',
87401
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
780 stderr)
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
781
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
782
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
783 def test_main():
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
784 support.run_unittest(
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
785 TestTracemallocEnabled,
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
786 TestSnapshot,
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
787 TestFilters,
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
788 TestCommandLine,
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
789 )
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
790
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
791 if __name__ == "__main__":
6e2089dbc5ad Issue #18874: Implement the PEP 454 (tracemalloc)
Victor Stinner <victor.stinner@gmail.com>
parents:
diff changeset
792 test_main()