Skip to content

Commit f8289cf

Browse files
committed
Fix printing output in aiomonitor console
1 parent f07423b commit f8289cf

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

objgraph.py

+55-43
Original file line numberDiff line numberDiff line change
@@ -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+
98111
def 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

279291
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,
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

364375
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',
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',
498+
'New_ids', 'Count_Deltas'))
499+
output_fn('='*(width+13*4))
490500
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))
494504
return new_ids
495505

496506

@@ -944,8 +954,10 @@ def _show_graph(objs, edge_func, swap_source_target,
944954
# Re-wrap it for utf-8
945955
import io
946956
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')
949961
queue = []
950962
depth = {}
951963
ignore = set(extra_ignore)
@@ -959,7 +971,7 @@ def _show_graph(objs, edge_func, swap_source_target,
959971
ignore.add(id(sys._getframe(1))) # show_refs/show_backrefs
960972
ignore.add(id(sys._getframe(1).f_locals))
961973
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)))
963975
depth[id(obj)] = 0
964976
queue.append(obj)
965977
del obj
@@ -972,11 +984,11 @@ def _show_graph(objs, edge_func, swap_source_target,
972984
# traversing the reference graph backwards.
973985
target = queue.pop(0)
974986
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)))
980992
h, s, v = _gradient((0, 0, 1), (0, 0, .3), tdepth, max_depth)
981993
if inspect.ismodule(target):
982994
h = .3
@@ -985,17 +997,17 @@ def _show_graph(objs, edge_func, swap_source_target,
985997
h = .6
986998
s = .6
987999
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))
9901002
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)))
9921004
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)))
9991011
if tdepth >= max_depth:
10001012
continue
10011013
if cull_func is not None and cull_func(target):
@@ -1017,8 +1029,8 @@ def _show_graph(objs, edge_func, swap_source_target,
10171029
else:
10181030
srcnode, tgtnode = source, target
10191031
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))
10221034
if id(source) not in depth:
10231035
depth[id(source)] = tdepth + 1
10241036
queue.append(source)
@@ -1035,14 +1047,14 @@ def _show_graph(objs, edge_func, swap_source_target,
10351047
label = "%d more backreferences" % skipped
10361048
edge = "too_many_%s->%s" % (_obj_node_id(target),
10371049
_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")
10461058

10471059
if output:
10481060
return

0 commit comments

Comments
 (0)