forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjgraph.py
More file actions
120 lines (100 loc) · 3.49 KB
/
objgraph.py
File metadata and controls
120 lines (100 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Find all objects reachable from a root object."""
from collections.abc import Iterable
import weakref
import types
from typing import List, Dict, Iterator, Tuple, Mapping
from typing_extensions import Final
method_descriptor_type = type(object.__dir__) # type: Final
method_wrapper_type = type(object().__ne__) # type: Final
wrapper_descriptor_type = type(object.__ne__) # type: Final
FUNCTION_TYPES = (types.BuiltinFunctionType,
types.FunctionType,
types.MethodType,
method_descriptor_type,
wrapper_descriptor_type,
method_wrapper_type) # type: Final
ATTR_BLACKLIST = {
'__doc__',
'__name__',
'__class__',
'__dict__',
} # type: Final
# Instances of these types can't have references to other objects
ATOMIC_TYPE_BLACKLIST = {
bool,
int,
float,
str,
type(None),
object,
} # type: Final
# Don't look at most attributes of these types
COLLECTION_TYPE_BLACKLIST = {
list,
set,
dict,
tuple,
} # type: Final
# Don't return these objects
TYPE_BLACKLIST = {
weakref.ReferenceType,
} # type: Final
def isproperty(o: object, attr: str) -> bool:
return isinstance(getattr(type(o), attr, None), property)
def get_edge_candidates(o: object) -> Iterator[Tuple[object, object]]:
# use getattr because mypyc expects dict, not mappingproxy
if '__getattribute__' in getattr(type(o), '__dict__'): # noqa
return
if type(o) not in COLLECTION_TYPE_BLACKLIST:
for attr in dir(o):
try:
if attr not in ATTR_BLACKLIST and hasattr(o, attr) and not isproperty(o, attr):
e = getattr(o, attr)
if not type(e) in ATOMIC_TYPE_BLACKLIST:
yield attr, e
except AssertionError:
pass
if isinstance(o, Mapping):
for k, v in o.items():
yield k, v
elif isinstance(o, Iterable) and not isinstance(o, str):
for i, e in enumerate(o):
yield i, e
def get_edges(o: object) -> Iterator[Tuple[object, object]]:
for s, e in get_edge_candidates(o):
if (isinstance(e, FUNCTION_TYPES)):
# We don't want to collect methods, but do want to collect values
# in closures and self pointers to other objects
if hasattr(e, '__closure__'):
yield (s, '__closure__'), e.__closure__ # type: ignore
if hasattr(e, '__self__'):
se = e.__self__ # type: ignore
if se is not o and se is not type(o) and hasattr(s, '__self__'):
yield s.__self__, se # type: ignore
else:
if not type(e) in TYPE_BLACKLIST:
yield s, e
def get_reachable_graph(root: object) -> Tuple[Dict[int, object],
Dict[int, Tuple[int, object]]]:
parents = {}
seen = {id(root): root}
worklist = [root]
while worklist:
o = worklist.pop()
for s, e in get_edges(o):
if id(e) in seen:
continue
parents[id(e)] = (id(o), s)
seen[id(e)] = e
worklist.append(e)
return seen, parents
def get_path(o: object,
seen: Dict[int, object],
parents: Dict[int, Tuple[int, object]]) -> List[Tuple[object, object]]:
path = []
while id(o) in parents:
pid, attr = parents[id(o)]
o = seen[pid]
path.append((attr, o))
path.reverse()
return path