@@ -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,17 @@ 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' ,
498
+ 'New_ids' , 'Count_Deltas' ))
499
+ output_fn ('=' * (width + 13 * 4 ))
490
500
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 )
501
+ output_fn ('%-*s%13d%13d%+13d%+13d' %
502
+ (width , row_class , old , current , new , delta ))
503
+ output_fn ('=' * (width + 13 * 4 ))
494
504
return new_ids
495
505
496
506
@@ -944,8 +954,10 @@ def _show_graph(objs, edge_func, swap_source_target,
944
954
# Re-wrap it for utf-8
945
955
import io
946
956
f = io .TextIOWrapper (f .detach (), 'utf-8' )
947
- f .write ('digraph ObjectGraph {\n '
948
- ' node[shape=box, style=filled, fillcolor=white];\n ' )
957
+
958
+ output_fn = _get_output_fn (f )
959
+ output_fn ('digraph ObjectGraph {\n '
960
+ ' node[shape=box, style=filled, fillcolor=white];\n ' )
949
961
queue = []
950
962
depth = {}
951
963
ignore = set (extra_ignore )
@@ -959,7 +971,7 @@ def _show_graph(objs, edge_func, swap_source_target,
959
971
ignore .add (id (sys ._getframe (1 ))) # show_refs/show_backrefs
960
972
ignore .add (id (sys ._getframe (1 ).f_locals ))
961
973
for obj in objs :
962
- f . write (' %s[fontcolor=red];\n ' % (_obj_node_id (obj )))
974
+ output_fn (' %s[fontcolor=red];\n ' % (_obj_node_id (obj )))
963
975
depth [id (obj )] = 0
964
976
queue .append (obj )
965
977
del obj
@@ -972,11 +984,11 @@ def _show_graph(objs, edge_func, swap_source_target,
972
984
# traversing the reference graph backwards.
973
985
target = queue .pop (0 )
974
986
tdepth = depth [id (target )]
975
- f . write (' %s[label="%s"%s];\n ' % (_obj_node_id (target ),
976
- _obj_label (target , extra_info ,
977
- refcounts , shortnames ),
978
- _obj_attrs (target ,
979
- extra_node_attrs )))
987
+ output_fn (' %s[label="%s"%s];\n ' % (_obj_node_id (target ),
988
+ _obj_label (target , extra_info ,
989
+ refcounts , shortnames ),
990
+ _obj_attrs (target ,
991
+ extra_node_attrs )))
980
992
h , s , v = _gradient ((0 , 0 , 1 ), (0 , 0 , .3 ), tdepth , max_depth )
981
993
if inspect .ismodule (target ):
982
994
h = .3
@@ -985,17 +997,17 @@ def _show_graph(objs, edge_func, swap_source_target,
985
997
h = .6
986
998
s = .6
987
999
v = 0.5 + v * 0.5
988
- f . write (' %s[fillcolor="%g,%g,%g"];\n '
989
- % (_obj_node_id (target ), h , s , v ))
1000
+ output_fn (' %s[fillcolor="%g,%g,%g"];\n '
1001
+ % (_obj_node_id (target ), h , s , v ))
990
1002
if v < 0.5 :
991
- f . write (' %s[fontcolor=white];\n ' % (_obj_node_id (target )))
1003
+ output_fn (' %s[fontcolor=white];\n ' % (_obj_node_id (target )))
992
1004
if hasattr (getattr (target , '__class__' , None ), '__del__' ):
993
- f . write (' %s->%s_has_a_del[color=red,style=dotted,'
994
- 'len=0.25,weight=10];\n ' % (_obj_node_id (target ),
995
- _obj_node_id (target )))
996
- f . write (' %s_has_a_del[label="__del__",shape=doublecircle,'
997
- 'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n '
998
- % (_obj_node_id (target )))
1005
+ output_fn (' %s->%s_has_a_del[color=red,style=dotted,'
1006
+ 'len=0.25,weight=10];\n ' % (_obj_node_id (target ),
1007
+ _obj_node_id (target )))
1008
+ output_fn (' %s_has_a_del[label="__del__",shape=doublecircle,'
1009
+ 'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n '
1010
+ % (_obj_node_id (target )))
999
1011
if tdepth >= max_depth :
1000
1012
continue
1001
1013
if cull_func is not None and cull_func (target ):
@@ -1017,8 +1029,8 @@ def _show_graph(objs, edge_func, swap_source_target,
1017
1029
else :
1018
1030
srcnode , tgtnode = source , target
1019
1031
elabel = _edge_label (srcnode , tgtnode , shortnames )
1020
- f . write (' %s -> %s%s;\n ' % (_obj_node_id (srcnode ),
1021
- _obj_node_id (tgtnode ), elabel ))
1032
+ output_fn (' %s -> %s%s;\n ' % (_obj_node_id (srcnode ),
1033
+ _obj_node_id (tgtnode ), elabel ))
1022
1034
if id (source ) not in depth :
1023
1035
depth [id (source )] = tdepth + 1
1024
1036
queue .append (source )
@@ -1035,14 +1047,14 @@ def _show_graph(objs, edge_func, swap_source_target,
1035
1047
label = "%d more backreferences" % skipped
1036
1048
edge = "too_many_%s->%s" % (_obj_node_id (target ),
1037
1049
_obj_node_id (target ))
1038
- f . write (' %s[color=red,style=dotted,len=0.25,weight=10];\n '
1039
- % edge )
1040
- f . write (' too_many_%s[label="%s",shape=box,height=0.25,'
1041
- 'color=red,fillcolor="%g,%g,%g",fontsize=6];\n '
1042
- % (_obj_node_id (target ), label , h , s , v ))
1043
- f . write (' too_many_%s[fontcolor=white];\n '
1044
- % (_obj_node_id (target )))
1045
- f . write ("}\n " )
1050
+ output_fn (' %s[color=red,style=dotted,len=0.25,weight=10];\n '
1051
+ % edge )
1052
+ output_fn (' too_many_%s[label="%s",shape=box,height=0.25,'
1053
+ 'color=red,fillcolor="%g,%g,%g",fontsize=6];\n '
1054
+ % (_obj_node_id (target ), label , h , s , v ))
1055
+ output_fn (' too_many_%s[fontcolor=white];\n '
1056
+ % (_obj_node_id (target )))
1057
+ output_fn ("}\n " )
1046
1058
1047
1059
if output :
1048
1060
return
0 commit comments