From 66cf82404096f224c7c3e08992c8ab4a450d5ad7 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Wed, 15 Mar 2023 23:47:04 -0500 Subject: [PATCH] change to set --- pytools/debug.py | 6 +++--- test/test_debug.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pytools/debug.py b/pytools/debug.py index f84fcebb..d15df7ae 100644 --- a/pytools/debug.py +++ b/pytools/debug.py @@ -193,7 +193,7 @@ def get_object_graph(objects: Collection[object], """ import gc - res: Dict[object, List[object]] = {} + res: Dict[object, Set[object]] = {} def hash_unhashable(obj: object) -> Union[object, Tuple[int, str]]: try: @@ -205,7 +205,7 @@ def hash_unhashable(obj: object) -> Union[object, Tuple[int, str]]: # Collect objects first to differentiate to outside objects later for obj in objects: - res[hash_unhashable(obj)] = [] + res[hash_unhashable(obj)] = set() for obj in objects: refs = gc.get_referents(obj) @@ -213,7 +213,7 @@ def hash_unhashable(obj: object) -> Union[object, Tuple[int, str]]: for r in refs: r = hash_unhashable(r) if r in res or outside_objects: - res.setdefault(obj, []).append(r) + res.setdefault(obj, set()).add(r) return res diff --git a/test/test_debug.py b/test/test_debug.py index cdfa6ad2..ed98e748 100644 --- a/test/test_debug.py +++ b/test/test_debug.py @@ -57,19 +57,19 @@ def test_get_object_graph(): a = (1,) b = (2,) c = (a, b) - assert get_object_graph([a]) == {(1,): []} - assert get_object_graph([a, b]) == {(1,): [], (2,): []} - assert get_object_graph([a, b, c]) == {((1,), (2,)): [(2,), (1,)], # c: [a, b] - (1,): [], # a: [] - (2,): []} # b: [] + assert get_object_graph([a]) == {(1,): set()} + assert get_object_graph([a, b]) == {(1,): set(), (2,): set()} + assert get_object_graph([a, b, c]) == {((1,), (2,)): {(2,), (1,)}, # c: [a, b] + (1,): set(), # a: set() + (2,): set()} # b: set() a = {} b = {"a": a} a["b"] = b assert get_object_graph([a, b]) == { - (id(a), "{'b': {'a': {...}}}"): [(id(b), "{'a': {'b': {...}}}")], - (id(b), "{'a': {'b': {...}}}"): [(id(a), "{'b': {'a': {...}}}")]} + (id(a), "{'b': {'a': {...}}}"): {(id(b), "{'a': {'b': {...}}}")}, + (id(b), "{'a': {'b': {...}}}"): {(id(a), "{'b': {'a': {...}}}")}} b = [42, 4] a = [1, 2, 3, 4, 5, b] @@ -77,6 +77,6 @@ def test_get_object_graph(): assert get_object_graph([a, b]) == { (id(a), "[1, 2, 3, 4, 5, [42, 4, [...]]]"): - [(id(b), "[42, 4, [1, 2, 3, 4, 5, [...]]]")], + {(id(b), "[42, 4, [1, 2, 3, 4, 5, [...]]]")}, (id(b), "[42, 4, [1, 2, 3, 4, 5, [...]]]"): - [(id(a), "[1, 2, 3, 4, 5, [42, 4, [...]]]")]} + {(id(a), "[1, 2, 3, 4, 5, [42, 4, [...]]]")}}