# Path: pympler/garbagegraph.py
# class GarbageGraph(ReferenceGraph):
#     """
#     The ``GarbageGraph`` is a ``ReferenceGraph`` that illustrates the objects
#     building reference cycles. The garbage collector is switched to debug mode
#     (all identified garbage is stored in `gc.garbage`) and the garbage
#     collector is invoked. The collected objects are then illustrated in a
#     directed graph.
# 
#     Large graphs can be reduced to the actual cycles by passing ``reduce=True``
#     to the constructor.
# 
#     It is recommended to disable the garbage collector when using the
#     ``GarbageGraph``.
# 
#     >>> from pympler.garbagegraph import GarbageGraph, start_debug_garbage
#     >>> start_debug_garbage()
#     >>> l = []
#     >>> l.append(l)
#     >>> del l
#     >>> gb = GarbageGraph()
#     >>> gb.render('garbage.eps')
#     True
#     """
#     def __init__(self, reduce=False, collectable=True):
#         """
#         Initialize the GarbageGraph with the objects identified by the garbage
#         collector. If `collectable` is true, every reference cycle is recorded.
#         Otherwise only uncollectable objects are reported.
#         """
#         if collectable:
#             gc.set_debug(gc.DEBUG_SAVEALL)
#         else:
#             gc.set_debug(0)
#         gc.collect()
# 
#         ReferenceGraph.__init__(self, gc.garbage, reduce)
# 
#     def print_stats(self, stream=None):
#         """
#         Log annotated garbage objects to console or file.
# 
#         :param stream: open file, uses sys.stdout if not given
#         """
#         if not stream:  # pragma: no cover
#             stream = sys.stdout
#         self.metadata.sort(key=lambda x: -x.size)
#         stream.write('%-10s %8s %-12s %-46s\n' % ('id', 'size', 'type',
#                                                   'representation'))
#         for g in self.metadata:
#             stream.write('0x%08x %8d %-12s %-46s\n' % (g.id, g.size,
#                                                        trunc(g.type, 12),
#                                                        trunc(g.str, 46)))
#         stream.write('Garbage: %8d collected objects (%s in cycles): %12s\n' %
#                      (self.count, self.num_in_cycles, pp(self.total_size)))
# 
# def start_debug_garbage():
#     """
#     Turn off garbage collector to analyze *collectable* reference cycles.
#     """
#     gc.collect()
#     gc.disable()
# 
# def end_debug_garbage():
#     """
#     Turn garbage collection on and disable debug output.
#     """
#     gc.set_debug(0)
#     gc.enable()
# 
# Path: pympler/refgraph.py
# class _Edge(object):
#     """
#     Describes a reference from one object `src` to another object `dst`.
#     """
#     __slots__ = ('src', 'dst', 'label', 'group')
# 
#     def __init__(self, src, dst, label):
#         self.src = src
#         self.dst = dst
#         self.label = label
#         self.group = None
# 
#     def __repr__(self):
#         return "<%08x => %08x, '%s', %s>" % (self.src, self.dst, self.label,
#                                              self.group)
# 
#     def __hash__(self):
#         return (self.src, self.dst, self.label).__hash__()
# 
#     def __eq__(self, other):
#         return self.__hash__() == other.__hash__()


import gc
import inspect
import os
import sys
import unittest
from io import StringIO
from pympler.garbagegraph import GarbageGraph, start_debug_garbage, end_debug_garbage
from pympler.refgraph import _Edge
        foo.next = bar
        bar.prev = foo

        l = []
        l.append(l)
        idl = id(l)

        del foo
        del bar
        del l

        gb = GarbageGraph()
        subs = gb.split_and_sort()
        self.assertEqual(len(subs), 2)

        self.assertEqual(subs[0].count, 4)
        self.assertEqual(subs[1].count, 1)
        fbg, lig = subs

        self.assertTrue(isinstance(fbg, GarbageGraph))
        self.assertTrue(isinstance(lig, GarbageGraph))

        self.assertEqual(len(fbg.edges), 4, fbg.edges)
        self.assertEqual(len(lig.edges), 1, lig.edges)

        self.assertTrue(_Edge(idl, idl, '') in lig.edges, lig.edges)

        self.assertTrue(_Edge(idfoo, idfd, '__dict__') in fbg.edges, fbg.edges)
        self.assertTrue(_Edge(idfd, idbar, 'next') in fbg.edges, fbg.edges)
        self.assertTrue(_Edge(idbar, idbd, '__dict__') in fbg.edges, fbg.edges)

# Path: pympler/garbagegraph.py
# class GarbageGraph(ReferenceGraph):
#     """
#     The ``GarbageGraph`` is a ``ReferenceGraph`` that illustrates the objects
#     building reference cycles. The garbage collector is switched to debug mode
#     (all identified garbage is stored in `gc.garbage`) and the garbage
#     collector is invoked. The collected objects are then illustrated in a
#     directed graph.
# 
#     Large graphs can be reduced to the actual cycles by passing ``reduce=True``
#     to the constructor.
# 
#     It is recommended to disable the garbage collector when using the
#     ``GarbageGraph``.
# 
#     >>> from pympler.garbagegraph import GarbageGraph, start_debug_garbage
#     >>> start_debug_garbage()
#     >>> l = []
#     >>> l.append(l)
#     >>> del l
#     >>> gb = GarbageGraph()
#     >>> gb.render('garbage.eps')
#     True
#     """
#     def __init__(self, reduce=False, collectable=True):
#         """
#         Initialize the GarbageGraph with the objects identified by the garbage
#         collector. If `collectable` is true, every reference cycle is recorded.
#         Otherwise only uncollectable objects are reported.
#         """
#         if collectable:
#             gc.set_debug(gc.DEBUG_SAVEALL)
#         else:
#             gc.set_debug(0)
#         gc.collect()
# 
#         ReferenceGraph.__init__(self, gc.garbage, reduce)
# 
#     def print_stats(self, stream=None):
#         """
#         Log annotated garbage objects to console or file.
# 
#         :param stream: open file, uses sys.stdout if not given
#         """
#         if not stream:  # pragma: no cover
#             stream = sys.stdout
#         self.metadata.sort(key=lambda x: -x.size)
#         stream.write('%-10s %8s %-12s %-46s\n' % ('id', 'size', 'type',
#                                                   'representation'))
#         for g in self.metadata:
#             stream.write('0x%08x %8d %-12s %-46s\n' % (g.id, g.size,
#                                                        trunc(g.type, 12),
#                                                        trunc(g.str, 46)))
#         stream.write('Garbage: %8d collected objects (%s in cycles): %12s\n' %
#                      (self.count, self.num_in_cycles, pp(self.total_size)))
# 
# def start_debug_garbage():
#     """
#     Turn off garbage collector to analyze *collectable* reference cycles.
#     """
#     gc.collect()
#     gc.disable()
# 
# def end_debug_garbage():
#     """
#     Turn garbage collection on and disable debug output.
#     """
#     gc.set_debug(0)
#     gc.enable()
# 
# Path: pympler/refgraph.py
# class _Edge(object):
#     """
#     Describes a reference from one object `src` to another object `dst`.
#     """
#     __slots__ = ('src', 'dst', 'label', 'group')
# 
#     def __init__(self, src, dst, label):
#         self.src = src
#         self.dst = dst
#         self.label = label
#         self.group = None
# 
#     def __repr__(self):
#         return "<%08x => %08x, '%s', %s>" % (self.src, self.dst, self.label,
#                                              self.group)
# 
#     def __hash__(self):
#         return (self.src, self.dst, self.label).__hash__()
# 
#     def __eq__(self, other):
#         return self.__hash__() == other.__hash__()


# Path: test/gui/test_garbage.py
import gc
import inspect
import os
import sys
import unittest
from io import StringIO
from pympler.garbagegraph import GarbageGraph, start_debug_garbage, end_debug_garbage
from pympler.refgraph import _Edge

        foo.next = bar
        bar.prev = foo

        l = []
        l.append(l)
        idl = id(l)

        del foo
        del bar
        del l

        gb = GarbageGraph()
        subs = gb.split_and_sort()
        self.assertEqual(len(subs), 2)

        self.assertEqual(subs[0].count, 4)
        self.assertEqual(subs[1].count, 1)
        fbg, lig = subs

        self.assertTrue(isinstance(fbg, GarbageGraph))
        self.assertTrue(isinstance(lig, GarbageGraph))

        self.assertEqual(len(fbg.edges), 4, fbg.edges)
        self.assertEqual(len(lig.edges), 1, lig.edges)

        self.assertTrue(_Edge(idl, idl, '') in lig.edges, lig.edges)

        self.assertTrue(_Edge(idfoo, idfd, '__dict__') in fbg.edges, fbg.edges)
        self.assertTrue(_Edge(idfd, idbar, 'next') in fbg.edges, fbg.edges)
        self.assertTrue(_Edge(idbar, idbd, '__dict__') in fbg.edges, fbg.edges)

        self.assertTrue(_Edge(idbd, idfoo, 'prev') in fbg.edges, fbg.edges)