Example #1
0
    def test_get_referents(self):
        """Test that referents are included in return value.

        Per default, only first level referents should be returned.
        If specified otherwise referents from even more levels are included
        in the result set.

        Duplicates have to be removed."""
        (o1, o2, o3, o4, o5) = (1, 'a', 'b', 4, 5)
        l0 = [o1, o2]
        l1 = [10, 11, 12, 13, l0]
        l2 = [o1, o2, o3, o4, o5, l1]

        #return all objects from first level
        res = muppy.get_referents(l2, level=1)
        self.assertEqual(len(l2), len(res))
        for o in res:
            self.assert_(o in l2)

        # return all objects from first and second level
        res = muppy.get_referents(l2, level=2)
        self.assertEqual(len(l1) + len(l2), len(res))
        for o in res:
            self.assert_((o in l1) or (o in l2))

        # return all objects from all levels, but with duplicates removed
        res = muppy.get_referents(l2, level=4242)
        self.assertEqual(len(l1) + len(l2), len(res))
        for o in res:
            self.assert_((o in l0) or (o in l1) or (o in l2))
    def test_get_referents(self):
        """Test that referents are included in return value.

        Per default, only first level referents should be returned.
        If specified otherwise referents from even more levels are included
        in the result set.

        Duplicates have to be removed."""
        (o1, o2, o3, o4, o5) = (1, "a", "b", 4, 5)
        l0 = [o1, o2]
        l1 = [10, 11, 12, 13, l0]
        l2 = [o1, o2, o3, o4, o5, l1]

        # return all objects from first level
        res = muppy.get_referents(l2, level=1)
        self.assertEqual(len(l2), len(res))
        for o in res:
            self.assert_(o in l2)

        # return all objects from first and second level
        res = muppy.get_referents(l2, level=2)
        self.assertEqual(len(l1) + len(l2), len(res))
        for o in res:
            self.assert_((o in l1) or (o in l2))

        # return all objects from all levels, but with duplicates removed
        res = muppy.get_referents(l2, level=4242)
        self.assertEqual(len(l1) + len(l2), len(res))
        for o in res:
            self.assert_((o in l0) or (o in l1) or (o in l2))
Example #3
0
    def _get_objects(self, ignore=[]):
        """Get all currently existing objects.

        XXX - ToDo: This method is a copy&paste from muppy.get_objects, but
        some modifications are applied. Specifically, it allows to ignore
        objects (which includes the current frame).

        keyword arguments
        ignore -- list of objects to ignore
        """

        def remove_ignore(objects, ignore=[]):
            # remove all objects listed in the ignore list
            res = []
            for o in objects:
                if not compat.object_in_list(o, ignore):
                    res.append(o)
            return res

        tmp = gc.get_objects()
        ignore.append(inspect.currentframe())  # PYCHOK change ignore
        ignore.append(self)  # PYCHOK change ignore
        if hasattr(self, "o0"):
            ignore.append(self.o0)  # PYCHOK change ignore
        if hasattr(self, "o1"):
            ignore.append(self.o1)  # PYCHOK change ignore
        ignore.append(ignore)  # PYCHOK change ignore
        ignore.append(remove_ignore)  # PYCHOK change ignore
        # this implies that referenced objects are also ignored
        tmp = remove_ignore(tmp, ignore)
        res = []
        for o in tmp:
            # gc.get_objects returns only container objects, but we also want
            # the objects referenced by them
            refs = muppy.get_referents(o)
            for ref in refs:
                if not muppy._is_containerobject(ref):
                    # we already got the container objects, now we only add
                    # non-container objects
                    res.append(ref)
        res.extend(tmp)
        res = muppy._remove_duplicates(res)
        if ignore is not None:
            # repeat to filter out objects which may have been referenced
            res = remove_ignore(res, ignore)
        # manual cleanup, see comment above
        del ignore[:]
        return res
Example #4
0
    def _get_objects(self, ignore=[]):
        """Get all currently existing objects.

        XXX - ToDo: This method is a copy&paste from muppy.get_objects, but
        some modifications are applied. Specifically, it allows to ignore
        objects (which includes the current frame).

        keyword arguments
        ignore -- list of objects to ignore
        """
        def remove_ignore(objects, ignore=[]):
            # remove all objects listed in the ignore list
            res = []
            for o in objects:
                if not compat.object_in_list(o, ignore):
                    res.append(o)
            return res

        tmp = gc.get_objects()
        ignore.append(inspect.currentframe())  # PYCHOK change ignore
        ignore.append(self)  # PYCHOK change ignore
        if hasattr(self, 'o0'):
            ignore.append(self.o0)  # PYCHOK change ignore
        if hasattr(self, 'o1'):
            ignore.append(self.o1)  # PYCHOK change ignore
        ignore.append(ignore)  # PYCHOK change ignore
        ignore.append(remove_ignore)  # PYCHOK change ignore
        # this implies that referenced objects are also ignored
        tmp = remove_ignore(tmp, ignore)
        res = []
        for o in tmp:
            # gc.get_objects returns only container objects, but we also want
            # the objects referenced by them
            refs = muppy.get_referents(o)
            for ref in refs:
                if not muppy._is_containerobject(ref):
                    # we already got the container objects, now we only add
                    # non-container objects
                    res.append(ref)
        res.extend(tmp)
        res = muppy._remove_duplicates(res)
        if ignore is not None:
            # repeat to filter out objects which may have been referenced
            res = remove_ignore(res, ignore)
        # manual cleanup, see comment above
        del ignore[:]
        return res
Example #5
0
 def get_summary(self):
     gc.collect()
     # exclude everything in this object itself
     excluded = set(id(o) for o in muppy.get_referents(self))
     return summary.summarize(o for o in muppy.get_objects()
                              if not id(o) in excluded)
Example #6
0
 def get_summary(self):
     gc.collect()
     # exclude everything in this object itself
     excluded = set(id(o) for o in muppy.get_referents(self))
     return summary.summarize(o for o in muppy.get_objects() if not id(o) in excluded)