def _find_leak_boundary_for_class_helper(self, referrer_class):

        # Construct the root object before we capture the 'peramanent' memory
        # state (or existing memory state...)
        root=referrer_class()
        root.name = 'root'
        print
        print 'id:', id(root)

        # Snapshot all the object ids in the process.
        state = MemoryState()
        state.capture_state()

        # Now build a simple dependency graph: root->intermediate->leaf
        intermediate = referrer_class()
        root.child = intermediate

        leaf = referrer_class()
        intermediate.child = leaf

        # We are pretending that we know 'leaf' is leaked here, and we
        # want to find any objects prior to our capture_state call that
        # refer directly or indirectly to leaf.
        leaks = state.find_leak_boundary_objects(leaf)

        referrer_ids = get_all_referrers(leaf)
        self.assertTrue(id(root) in referrer_ids)

        import gc
        referrers = gc.get_referrers(intermediate)
        print [type(item) for item in referrers]
    def test_get_all_referrers2(self):

        class Referrer:
            pass

        l = []

        # Have a foo before in memory just to make things intersting
        leaf = Referrer()
        l.append(leaf)

        referrers = get_all_referrers(leaf)

        self.assertTrue(id(l) in referrers)
    def _find_leak_boundary_for_class_helper(self, referrer_class):

        # Construct the root object before we capture the 'peramanent' memory
        # state (or existing memory state...)
        root=referrer_class()
        root.name = 'root'
        print
        print 'id:', id(root)

        # Snapshot all the object ids in the process.
        state = MemoryState()
        state.capture_state()

        # Now build a simple dependency graph: root->intermediate->leaf
        intermediate = referrer_class()
        root.child = intermediate

        leaf = referrer_class()
        intermediate.child = leaf

        # We are pretending that we know 'leaf' is leaked here, and we
        # want to find any objects prior to our capture_state call that
        # refer directly or indirectly to leaf.
        leaks = state.find_leak_boundary_objects(leaf)

        referrer_ids = get_all_referrers(leaf)
        self.assertTrue(id(root) in referrer_ids)

        import gc
        referrers = gc.get_referrers(intermediate)
        print [type(item) for item in referrers]
        print root.__dict__ in referrers

        raise nose.SkipTest
        # FIXME:
        #   this assertion succeeds on OS X 10.4 but fails
        #   on Linux and Windows.  It is unclear why.
        self.assertEqual(len(leaks), 1)

        leak_holder, leaked_objects = leaks[0]
        #print leaked_objects[0].name

        # FIXME:
        #   These two assertions don't work properly with nosetests
        self.assertEqual(root, leak_holder)
        self.assertEqual(intermediate, leaked_objects[0])
    def test_get_all_referrers(self):
        """ Are all items in a reference chain actual found in the reference chain?
        """

        class Referrer:
            pass

        class Leaf:
            pass

        # Have a foo before in memory just to make things intersting
        root=Referrer()
        intermediate = Referrer()
        leaf = Leaf()
        intermediate.child = leaf
        root.child = intermediate

        referrers = get_all_referrers(leaf)
        self.assertTrue(id(root) in referrers)
        self.assertTrue(id(intermediate) in referrers)