@@ -270,9 +270,11 @@ def show_most_common_types(
270270 file .write ('%-*s %i\n ' % (width , name , count ))
271271
272272
273- def show_growth (limit = 10 , peak_stats = {}, shortnames = True , file = None ,
274- filter = None ):
275- """Show the increase in peak object counts since last call.
273+ def growth (limit = 10 , peak_stats = {}, shortnames = True , filter = None ):
274+ """Count the increase in peak object since last call.
275+
276+ Returns a list of (type_name, total_count, increase_delta),
277+ descending order by increase_delta.
276278
277279 Limits the output to ``limit`` largest deltas. You may set ``limit`` to
278280 None to see all of them.
@@ -287,6 +289,40 @@ def show_growth(limit=10, peak_stats={}, shortnames=True, file=None,
287289
288290 The caveats documented in :func:`typestats` apply.
289291
292+ Example:
293+
294+ >>> growth(2)
295+ [(tuple, 12282, 10), (dict, 1922, 7)]
296+
297+ .. versionadded:: 3.2.1
298+
299+ """
300+ gc .collect ()
301+ stats = typestats (shortnames = shortnames , filter = filter )
302+ deltas = {}
303+ for name , count in iteritems (stats ):
304+ old_count = peak_stats .get (name , 0 )
305+ if count > old_count :
306+ deltas [name ] = count - old_count
307+ peak_stats [name ] = count
308+ deltas = sorted (deltas .items (), key = operator .itemgetter (1 ),
309+ reverse = True )
310+ if limit :
311+ deltas = deltas [:limit ]
312+
313+ return [(name , stats [name ], delta ) for name , delta in deltas ]
314+
315+
316+ def show_growth (limit = 10 , peak_stats = None , shortnames = True , file = None ,
317+ filter = None ):
318+ """Show the increase in peak object counts since last call.
319+
320+ if ``peak_stats`` is None, peak object counts will recorded in
321+ func `growth`, and your can record the counts by yourself with set
322+ ``peak_stats`` to a dictionary.
323+
324+ The caveats documented in :func:`growth` apply.
325+
290326 Example:
291327
292328 >>> show_growth()
@@ -307,24 +343,16 @@ def show_growth(limit=10, peak_stats={}, shortnames=True, file=None,
307343 New parameter: ``filter``.
308344
309345 """
310- gc .collect ()
311- stats = typestats (shortnames = shortnames , filter = filter )
312- deltas = {}
313- for name , count in iteritems (stats ):
314- old_count = peak_stats .get (name , 0 )
315- if count > old_count :
316- deltas [name ] = count - old_count
317- peak_stats [name ] = count
318- deltas = sorted (deltas .items (), key = operator .itemgetter (1 ),
319- reverse = True )
320- if limit :
321- deltas = deltas [:limit ]
322- if deltas :
346+ if peak_stats is None :
347+ result = growth (limit , shortnames = shortnames , filter = filter )
348+ else :
349+ result = growth (limit , peak_stats , shortnames , filter )
350+ if result :
323351 if file is None :
324352 file = sys .stdout
325- width = max (len (name ) for name , count in deltas )
326- for name , delta in deltas :
327- file .write ('%-*s%9d %+9d\n ' % (width , name , stats [ name ] , delta ))
353+ width = max (len (name ) for name , _ , _ in result )
354+ for name , count , delta in result :
355+ file .write ('%-*s%9d %+9d\n ' % (width , name , count , delta ))
328356
329357
330358def get_leaking_objects (objects = None ):
0 commit comments