def test_remove_duplicates(self): """Test that this operations returns a duplicate-free lists. That, is no objects are listed twice. This does not apply to objects with same values.""" (o1, o2, o3, o4, o5) = (1, 'a', 'b', 'c', 5) objects = [o1, o2, o3, o4, o5, o5, o4, o3, o2, o1] expected = set(objects) res = muppy._remove_duplicates(objects) self.assertEqual(len(expected), len(res)) for o in res: self.assert_(o in expected)
def test_remove_duplicates(self): """Test that this operations returns a duplicate-free lists. That, is no objects are listed twice. This does not apply to objects with same values.""" (o1, o2, o3, o4, o5) = (1, "a", "b", "c", 5) objects = [o1, o2, o3, o4, o5, o5, o4, o3, o2, o1] expected = set(objects) res = muppy._remove_duplicates(objects) self.assertEqual(len(expected), len(res)) for o in res: self.assert_(o in expected)
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
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