@@ -95,6 +95,19 @@ def _isinstance(object, classinfo):
9595 return issubclass (type (object ), classinfo )
9696
9797
98+ def _get_output_fn (file = None ):
99+ """Return function which will be used for writing data to output.
100+
101+ If `file` is none, output will be printed to stdout.
102+ """
103+ if file is None :
104+ def show (inp ):
105+ print (inp )
106+ return show
107+
108+ return file .write
109+
110+
98111def count (typename , objects = None ):
99112 """Count objects tracked by the garbage collector with a given class name.
100113
@@ -267,13 +280,12 @@ def show_most_common_types(
267280 New parameter: ``filter``.
268281
269282 """
270- if file is None :
271- file = sys .stdout
283+ output_fn = _get_output_fn (file )
272284 stats = most_common_types (limit , objects , shortnames = shortnames ,
273285 filter = filter )
274286 width = max (len (name ) for name , count in stats )
275287 for name , count in stats :
276- file . write ('%-*s %i\n ' % (width , name , count ))
288+ output_fn ('%-*s %i\n ' % (width , name , count ))
277289
278290
279291def growth (limit = 10 , peak_stats = {}, shortnames = True , filter = None ):
@@ -354,11 +366,10 @@ def show_growth(limit=10, peak_stats=None, shortnames=True, file=None,
354366 else :
355367 result = growth (limit , peak_stats , shortnames , filter )
356368 if result :
357- if file is None :
358- file = sys .stdout
369+ output_fn = _get_output_fn (file )
359370 width = max (len (name ) for name , _ , _ in result )
360371 for name , count , delta in result :
361- file . write ('%-*s%9d %+9d\n ' % (width , name , count , delta ))
372+ output_fn ('%-*s%9d %+9d\n ' % (width , name , count , delta ))
362373
363374
364375def get_new_ids (skip_update = False , limit = 10 , sortby = 'deltas' ,
@@ -479,18 +490,16 @@ def get_new_ids(skip_update=False, limit=10, sortby='deltas',
479490 rows = rows [:limit ]
480491 if not rows :
481492 return new_ids
482- if file is None :
483- file = sys .stdout
484493 width = max (len (row [0 ]) for row in rows )
485- print ( '=' * ( width + 13 * 4 ), file = file )
486- print ( '%-*s%13s%13s%13s%13s' %
487- ( width , 'Type' , 'Old_ids' , 'Current_ids' , 'New_ids' , 'Count_Deltas' ),
488- file = file )
489- print ('=' * (width + 13 * 4 ), file = file )
494+ output_fn = _get_output_fn ( file )
495+ output_fn ( '=' * ( width + 13 * 4 ))
496+ output_fn ( '%-*s%13s%13s%13s%13s' %
497+ ( width , 'Type' , 'Old_ids' , 'Current_ids' , 'New_ids' , 'Count_Deltas' ) )
498+ output_fn ('=' * (width + 13 * 4 ))
490499 for row_class , old , current , new , delta in rows :
491- print ('%-*s%13d%13d%+13d%+13d' %
492- (width , row_class , old , current , new , delta ), file = file )
493- print ('=' * (width + 13 * 4 ), file = file )
500+ output_fn ('%-*s%13d%13d%+13d%+13d' %
501+ (width , row_class , old , current , new , delta ))
502+ output_fn ('=' * (width + 13 * 4 ))
494503 return new_ids
495504
496505
@@ -944,7 +953,9 @@ def _show_graph(objs, edge_func, swap_source_target,
944953 # Re-wrap it for utf-8
945954 import io
946955 f = io .TextIOWrapper (f .detach (), 'utf-8' )
947- f .write ('digraph ObjectGraph {\n '
956+
957+ output_fn = _get_output_fn (f )
958+ output_fn ('digraph ObjectGraph {\n '
948959 ' node[shape=box, style=filled, fillcolor=white];\n ' )
949960 queue = []
950961 depth = {}
@@ -959,7 +970,7 @@ def _show_graph(objs, edge_func, swap_source_target,
959970 ignore .add (id (sys ._getframe (1 ))) # show_refs/show_backrefs
960971 ignore .add (id (sys ._getframe (1 ).f_locals ))
961972 for obj in objs :
962- f . write (' %s[fontcolor=red];\n ' % (_obj_node_id (obj )))
973+ output_fn (' %s[fontcolor=red];\n ' % (_obj_node_id (obj )))
963974 depth [id (obj )] = 0
964975 queue .append (obj )
965976 del obj
@@ -972,7 +983,7 @@ def _show_graph(objs, edge_func, swap_source_target,
972983 # traversing the reference graph backwards.
973984 target = queue .pop (0 )
974985 tdepth = depth [id (target )]
975- f . write (' %s[label="%s"%s];\n ' % (_obj_node_id (target ),
986+ output_fn (' %s[label="%s"%s];\n ' % (_obj_node_id (target ),
976987 _obj_label (target , extra_info ,
977988 refcounts , shortnames ),
978989 _obj_attrs (target ,
@@ -985,15 +996,15 @@ def _show_graph(objs, edge_func, swap_source_target,
985996 h = .6
986997 s = .6
987998 v = 0.5 + v * 0.5
988- f . write (' %s[fillcolor="%g,%g,%g"];\n '
999+ output_fn (' %s[fillcolor="%g,%g,%g"];\n '
9891000 % (_obj_node_id (target ), h , s , v ))
9901001 if v < 0.5 :
991- f . write (' %s[fontcolor=white];\n ' % (_obj_node_id (target )))
1002+ output_fn (' %s[fontcolor=white];\n ' % (_obj_node_id (target )))
9921003 if hasattr (getattr (target , '__class__' , None ), '__del__' ):
993- f . write (' %s->%s_has_a_del[color=red,style=dotted,'
1004+ output_fn (' %s->%s_has_a_del[color=red,style=dotted,'
9941005 'len=0.25,weight=10];\n ' % (_obj_node_id (target ),
9951006 _obj_node_id (target )))
996- f . write (' %s_has_a_del[label="__del__",shape=doublecircle,'
1007+ output_fn (' %s_has_a_del[label="__del__",shape=doublecircle,'
9971008 'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n '
9981009 % (_obj_node_id (target )))
9991010 if tdepth >= max_depth :
@@ -1017,7 +1028,7 @@ def _show_graph(objs, edge_func, swap_source_target,
10171028 else :
10181029 srcnode , tgtnode = source , target
10191030 elabel = _edge_label (srcnode , tgtnode , shortnames )
1020- f . write (' %s -> %s%s;\n ' % (_obj_node_id (srcnode ),
1031+ output_fn (' %s -> %s%s;\n ' % (_obj_node_id (srcnode ),
10211032 _obj_node_id (tgtnode ), elabel ))
10221033 if id (source ) not in depth :
10231034 depth [id (source )] = tdepth + 1
@@ -1035,14 +1046,14 @@ def _show_graph(objs, edge_func, swap_source_target,
10351046 label = "%d more backreferences" % skipped
10361047 edge = "too_many_%s->%s" % (_obj_node_id (target ),
10371048 _obj_node_id (target ))
1038- f . write (' %s[color=red,style=dotted,len=0.25,weight=10];\n '
1049+ output_fn (' %s[color=red,style=dotted,len=0.25,weight=10];\n '
10391050 % edge )
1040- f . write (' too_many_%s[label="%s",shape=box,height=0.25,'
1051+ output_fn (' too_many_%s[label="%s",shape=box,height=0.25,'
10411052 'color=red,fillcolor="%g,%g,%g",fontsize=6];\n '
10421053 % (_obj_node_id (target ), label , h , s , v ))
1043- f . write (' too_many_%s[fontcolor=white];\n '
1054+ output_fn (' too_many_%s[fontcolor=white];\n '
10441055 % (_obj_node_id (target )))
1045- f . write ("}\n " )
1056+ output_fn ("}\n " )
10461057
10471058 if output :
10481059 return
0 commit comments