@@ -95,6 +95,19 @@ def _isinstance(object, classinfo):
95
95
return issubclass (type (object ), classinfo )
96
96
97
97
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
+
98
111
def count (typename , objects = None ):
99
112
"""Count objects tracked by the garbage collector with a given class name.
100
113
@@ -267,13 +280,12 @@ def show_most_common_types(
267
280
New parameter: ``filter``.
268
281
269
282
"""
270
- if file is None :
271
- file = sys .stdout
283
+ output_fn = _get_output_fn (file )
272
284
stats = most_common_types (limit , objects , shortnames = shortnames ,
273
285
filter = filter )
274
286
width = max (len (name ) for name , count in stats )
275
287
for name , count in stats :
276
- file . write ('%-*s %i\n ' % (width , name , count ))
288
+ output_fn ('%-*s %i\n ' % (width , name , count ))
277
289
278
290
279
291
def 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,
354
366
else :
355
367
result = growth (limit , peak_stats , shortnames , filter )
356
368
if result :
357
- if file is None :
358
- file = sys .stdout
369
+ output_fn = _get_output_fn (file )
359
370
width = max (len (name ) for name , _ , _ in result )
360
371
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 ))
362
373
363
374
364
375
def 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',
479
490
rows = rows [:limit ]
480
491
if not rows :
481
492
return new_ids
482
- if file is None :
483
- file = sys .stdout
484
493
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 ))
490
499
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 ))
494
503
return new_ids
495
504
496
505
@@ -944,7 +953,9 @@ def _show_graph(objs, edge_func, swap_source_target,
944
953
# Re-wrap it for utf-8
945
954
import io
946
955
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 '
948
959
' node[shape=box, style=filled, fillcolor=white];\n ' )
949
960
queue = []
950
961
depth = {}
@@ -959,7 +970,7 @@ def _show_graph(objs, edge_func, swap_source_target,
959
970
ignore .add (id (sys ._getframe (1 ))) # show_refs/show_backrefs
960
971
ignore .add (id (sys ._getframe (1 ).f_locals ))
961
972
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 )))
963
974
depth [id (obj )] = 0
964
975
queue .append (obj )
965
976
del obj
@@ -972,7 +983,7 @@ def _show_graph(objs, edge_func, swap_source_target,
972
983
# traversing the reference graph backwards.
973
984
target = queue .pop (0 )
974
985
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 ),
976
987
_obj_label (target , extra_info ,
977
988
refcounts , shortnames ),
978
989
_obj_attrs (target ,
@@ -985,15 +996,15 @@ def _show_graph(objs, edge_func, swap_source_target,
985
996
h = .6
986
997
s = .6
987
998
v = 0.5 + v * 0.5
988
- f . write (' %s[fillcolor="%g,%g,%g"];\n '
999
+ output_fn (' %s[fillcolor="%g,%g,%g"];\n '
989
1000
% (_obj_node_id (target ), h , s , v ))
990
1001
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 )))
992
1003
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,'
994
1005
'len=0.25,weight=10];\n ' % (_obj_node_id (target ),
995
1006
_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,'
997
1008
'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n '
998
1009
% (_obj_node_id (target )))
999
1010
if tdepth >= max_depth :
@@ -1017,7 +1028,7 @@ def _show_graph(objs, edge_func, swap_source_target,
1017
1028
else :
1018
1029
srcnode , tgtnode = source , target
1019
1030
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 ),
1021
1032
_obj_node_id (tgtnode ), elabel ))
1022
1033
if id (source ) not in depth :
1023
1034
depth [id (source )] = tdepth + 1
@@ -1035,14 +1046,14 @@ def _show_graph(objs, edge_func, swap_source_target,
1035
1046
label = "%d more backreferences" % skipped
1036
1047
edge = "too_many_%s->%s" % (_obj_node_id (target ),
1037
1048
_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 '
1039
1050
% 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,'
1041
1052
'color=red,fillcolor="%g,%g,%g",fontsize=6];\n '
1042
1053
% (_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 '
1044
1055
% (_obj_node_id (target )))
1045
- f . write ("}\n " )
1056
+ output_fn ("}\n " )
1046
1057
1047
1058
if output :
1048
1059
return
0 commit comments