Skip to content

Commit

Permalink
change to set
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener committed Mar 16, 2023
1 parent c1a2a16 commit 66cf824
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pytools/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -205,15 +205,15 @@ 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)
obj = hash_unhashable(obj)
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

Expand Down
18 changes: 9 additions & 9 deletions test/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ 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]
b.append(a)

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, [...]]]")}}

0 comments on commit 66cf824

Please sign in to comment.