@@ -36,7 +36,7 @@ def allocate_bytes(size):
36
36
bytes_len = (size - EMPTY_STRING_SIZE )
37
37
frames = get_frames (nframe , 1 )
38
38
data = b'x' * bytes_len
39
- return data , tracemalloc .Traceback (frames )
39
+ return data , tracemalloc .Traceback (frames , min ( len ( frames ), nframe ) )
40
40
41
41
def create_snapshots ():
42
42
traceback_limit = 2
@@ -45,27 +45,27 @@ def create_snapshots():
45
45
# traceback_frames) tuples. traceback_frames is a tuple of (filename,
46
46
# line_number) tuples.
47
47
raw_traces = [
48
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
49
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
50
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
48
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
49
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
50
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
51
51
52
- (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 ))),
52
+ (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
53
53
54
- (2 , 66 , (('b.py' , 1 ),)),
54
+ (2 , 66 , (('b.py' , 1 ),), 1 ),
55
55
56
- (3 , 7 , (('<unknown>' , 0 ),)),
56
+ (3 , 7 , (('<unknown>' , 0 ),), 1 ),
57
57
]
58
58
snapshot = tracemalloc .Snapshot (raw_traces , traceback_limit )
59
59
60
60
raw_traces2 = [
61
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
62
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
63
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
61
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
62
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
63
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
64
64
65
- (2 , 2 , (('a.py' , 5 ), ('b.py' , 4 ))),
66
- (2 , 5000 , (('a.py' , 5 ), ('b.py' , 4 ))),
65
+ (2 , 2 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
66
+ (2 , 5000 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
67
67
68
- (4 , 400 , (('c.py' , 578 ),)),
68
+ (4 , 400 , (('c.py' , 578 ),), 1 ),
69
69
]
70
70
snapshot2 = tracemalloc .Snapshot (raw_traces2 , traceback_limit )
71
71
@@ -125,7 +125,7 @@ def test_new_reference(self):
125
125
126
126
nframe = tracemalloc .get_traceback_limit ()
127
127
frames = get_frames (nframe , - 3 )
128
- obj_traceback = tracemalloc .Traceback (frames )
128
+ obj_traceback = tracemalloc .Traceback (frames , min ( len ( frames ), nframe ) )
129
129
130
130
traceback = tracemalloc .get_object_traceback (obj )
131
131
self .assertIsNotNone (traceback )
@@ -167,7 +167,7 @@ def test_get_traces(self):
167
167
trace = self .find_trace (traces , obj_traceback )
168
168
169
169
self .assertIsInstance (trace , tuple )
170
- domain , size , traceback = trace
170
+ domain , size , traceback , length = trace
171
171
self .assertEqual (size , obj_size )
172
172
self .assertEqual (traceback , obj_traceback ._frames )
173
173
@@ -197,8 +197,8 @@ def allocate_bytes4(size):
197
197
198
198
trace1 = self .find_trace (traces , obj1_traceback )
199
199
trace2 = self .find_trace (traces , obj2_traceback )
200
- domain1 , size1 , traceback1 = trace1
201
- domain2 , size2 , traceback2 = trace2
200
+ domain1 , size1 , traceback1 , length1 = trace1
201
+ domain2 , size2 , traceback2 , length2 = trace2
202
202
self .assertIs (traceback2 , traceback1 )
203
203
204
204
def test_get_traced_memory (self ):
@@ -259,6 +259,9 @@ def test_snapshot(self):
259
259
# take a snapshot
260
260
snapshot = tracemalloc .take_snapshot ()
261
261
262
+ # This can vary
263
+ self .assertGreater (snapshot .traces [1 ].traceback .total_nframe , 10 )
264
+
262
265
# write on disk
263
266
snapshot .dump (support .TESTFN )
264
267
self .addCleanup (support .unlink , support .TESTFN )
@@ -321,7 +324,7 @@ class TestSnapshot(unittest.TestCase):
321
324
maxDiff = 4000
322
325
323
326
def test_create_snapshot (self ):
324
- raw_traces = [(0 , 5 , (('a.py' , 2 ),))]
327
+ raw_traces = [(0 , 5 , (('a.py' , 2 ),), 10 )]
325
328
326
329
with contextlib .ExitStack () as stack :
327
330
stack .enter_context (patch .object (tracemalloc , 'is_tracing' ,
@@ -336,6 +339,7 @@ def test_create_snapshot(self):
336
339
self .assertEqual (len (snapshot .traces ), 1 )
337
340
trace = snapshot .traces [0 ]
338
341
self .assertEqual (trace .size , 5 )
342
+ self .assertEqual (trace .traceback .total_nframe , 10 )
339
343
self .assertEqual (len (trace .traceback ), 1 )
340
344
self .assertEqual (trace .traceback [0 ].filename , 'a.py' )
341
345
self .assertEqual (trace .traceback [0 ].lineno , 2 )
@@ -351,11 +355,11 @@ def test_filter_traces(self):
351
355
# exclude b.py
352
356
snapshot3 = snapshot .filter_traces ((filter1 ,))
353
357
self .assertEqual (snapshot3 .traces ._traces , [
354
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
355
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
356
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
357
- (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 ))),
358
- (3 , 7 , (('<unknown>' , 0 ),)),
358
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
359
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
360
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
361
+ (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
362
+ (3 , 7 , (('<unknown>' , 0 ),), 1 ),
359
363
])
360
364
361
365
# filter_traces() must not touch the original snapshot
@@ -364,10 +368,10 @@ def test_filter_traces(self):
364
368
# only include two lines of a.py
365
369
snapshot4 = snapshot3 .filter_traces ((filter2 , filter3 ))
366
370
self .assertEqual (snapshot4 .traces ._traces , [
367
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
368
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
369
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
370
- (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 ))),
371
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
372
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
373
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
374
+ (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
371
375
])
372
376
373
377
# No filter: just duplicate the snapshot
@@ -388,21 +392,21 @@ def test_filter_traces_domain(self):
388
392
# exclude a.py of domain 1
389
393
snapshot3 = snapshot .filter_traces ((filter1 ,))
390
394
self .assertEqual (snapshot3 .traces ._traces , [
391
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
392
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
393
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
394
- (2 , 66 , (('b.py' , 1 ),)),
395
- (3 , 7 , (('<unknown>' , 0 ),)),
395
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
396
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
397
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
398
+ (2 , 66 , (('b.py' , 1 ),), 1 ),
399
+ (3 , 7 , (('<unknown>' , 0 ),), 1 ),
396
400
])
397
401
398
402
# include domain 1
399
403
snapshot3 = snapshot .filter_traces ((filter1 ,))
400
404
self .assertEqual (snapshot3 .traces ._traces , [
401
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
402
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
403
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
404
- (2 , 66 , (('b.py' , 1 ),)),
405
- (3 , 7 , (('<unknown>' , 0 ),)),
405
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
406
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
407
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
408
+ (2 , 66 , (('b.py' , 1 ),), 1 ),
409
+ (3 , 7 , (('<unknown>' , 0 ),), 1 ),
406
410
])
407
411
408
412
def test_filter_traces_domain_filter (self ):
@@ -413,17 +417,17 @@ def test_filter_traces_domain_filter(self):
413
417
# exclude domain 2
414
418
snapshot3 = snapshot .filter_traces ((filter1 ,))
415
419
self .assertEqual (snapshot3 .traces ._traces , [
416
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
417
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
418
- (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 ))),
419
- (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 ))),
420
- (2 , 66 , (('b.py' , 1 ),)),
420
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
421
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
422
+ (0 , 10 , (('a.py' , 2 ), ('b.py' , 4 )), 3 ),
423
+ (1 , 2 , (('a.py' , 5 ), ('b.py' , 4 )), 3 ),
424
+ (2 , 66 , (('b.py' , 1 ),), 1 ),
421
425
])
422
426
423
427
# include domain 2
424
428
snapshot3 = snapshot .filter_traces ((filter2 ,))
425
429
self .assertEqual (snapshot3 .traces ._traces , [
426
- (3 , 7 , (('<unknown>' , 0 ),)),
430
+ (3 , 7 , (('<unknown>' , 0 ),), 1 ),
427
431
])
428
432
429
433
def test_snapshot_group_by_line (self ):
0 commit comments