Rev 67: Add get_recursive_items, as another aid to live debugging. in http://bazaar.launchpad.net/~jameinel/meliae/trunk
John Arbash Meinel
john at arbash-meinel.com
Tue Sep 8 19:29:05 BST 2009
At http://bazaar.launchpad.net/~jameinel/meliae/trunk
------------------------------------------------------------
revno: 67
revision-id: john at arbash-meinel.com-20090908182852-u2l55fm79x78v8ex
parent: john at arbash-meinel.com-20090908174103-17nem9vf7bz8r2x7
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Tue 2009-09-08 13:28:52 -0500
message:
Add get_recursive_items, as another aid to live debugging.
This gets the recursive unique referenced items.
You can then do stuff like [a for a in all if isinstance(a, str)] to
find subset information.
-------------- next part --------------
=== modified file 'meliae/scanner.py'
--- a/meliae/scanner.py 2009-09-08 17:41:03 +0000
+++ b/meliae/scanner.py 2009-09-08 18:28:52 +0000
@@ -25,6 +25,9 @@
)
+size_of = _scanner.size_of
+
+
def dump_all_referenced(outf, obj):
"""Recursively dump everything that is referenced from obj."""
# if isinstance(outf, str):
@@ -111,4 +114,25 @@
return len(seen), total_size
-size_of = _scanner.size_of
+def get_recursive_items(obj):
+ """Walk all referred items and return the unique list of them."""
+ all = []
+ pending = [obj]
+ last_item = 0
+ seen = _intset.IDSet()
+ while last_item >= 0:
+ item = pending[last_item]
+ last_item -= 1
+ id_item = id(item)
+ if id_item in seen:
+ continue
+ seen.add(id_item)
+ all.append(item)
+ for child in gc.get_referents(item):
+ if id(child) not in seen:
+ last_item += 1
+ if len(pending) > last_item:
+ pending[last_item] = child
+ else:
+ pending.append(child)
+ return all
=== modified file 'meliae/tests/test_scanner.py'
--- a/meliae/tests/test_scanner.py 2009-09-08 17:41:03 +0000
+++ b/meliae/tests/test_scanner.py 2009-09-08 18:28:52 +0000
@@ -113,3 +113,28 @@
total_size = (scanner.size_of(s1) + scanner.size_of(s2)
+ scanner.size_of(l))
self.assertRecursiveSize(3, total_size, l)
+
+
+
+class TestGetRecursiveItems(tests.TestCase):
+
+ def assertRecursiveItems(self, expected, root):
+ actual = scanner.get_recursive_items(root)
+ self.assertEqual(len(expected), len(actual))
+ by_id = dict((id(x), x) for x in actual)
+ for obj in expected:
+ id_obj = id(obj)
+ self.assertTrue(id_obj in by_id)
+ self.assertTrue(by_id[id_obj] is obj)
+
+ def test_single(self):
+ a = 1
+ self.assertRecursiveItems([a], a)
+
+ def test_dict(self):
+ a = 1
+ b = 2
+ c = 'three'
+ d = 'four'
+ dd = {a:b, c:d}
+ self.assertRecursiveItems([a, b, c, d, dd], dd)
More information about the bazaar-commits
mailing list