PyCG 0.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyCG/__init__.py +0 -0
- PyCG/__main__.py +89 -0
- PyCG/formats/__init__.py +23 -0
- PyCG/formats/as_graph.py +33 -0
- PyCG/formats/base.py +23 -0
- PyCG/formats/fasten.py +273 -0
- PyCG/formats/simple.py +33 -0
- PyCG/machinery/__init__.py +0 -0
- PyCG/machinery/callgraph.py +59 -0
- PyCG/machinery/classes.py +78 -0
- PyCG/machinery/definitions.py +230 -0
- PyCG/machinery/imports.py +235 -0
- PyCG/machinery/key_err.py +31 -0
- PyCG/machinery/modules.py +64 -0
- PyCG/machinery/pointers.py +149 -0
- PyCG/machinery/scopes.py +154 -0
- PyCG/processing/__init__.py +0 -0
- PyCG/processing/base.py +547 -0
- PyCG/processing/cgprocessor.py +252 -0
- PyCG/processing/keyerrprocessor.py +101 -0
- PyCG/processing/postprocessor.py +342 -0
- PyCG/processing/preprocessor.py +424 -0
- PyCG/pycg.py +263 -0
- PyCG/tests/base.py +25 -0
- PyCG/tests/callgraph_test.py +87 -0
- PyCG/tests/definitions_test.py +90 -0
- PyCG/tests/fasten_format_test.py +319 -0
- PyCG/tests/imports_test.py +189 -0
- PyCG/tests/pointers_test.py +112 -0
- PyCG/tests/scopes_test.py +217 -0
- PyCG/utils/__init__.py +22 -0
- PyCG/utils/common.py +45 -0
- PyCG/utils/constants.py +42 -0
- pycg-0.0.8.dist-info/METADATA +161 -0
- pycg-0.0.8.dist-info/RECORD +37 -0
- pycg-0.0.8.dist-info/WHEEL +4 -0
- pycg-0.0.8.dist-info/licenses/LICENCE +201 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
import ast
|
|
22
|
+
import os
|
|
23
|
+
|
|
24
|
+
from pycg import utils
|
|
25
|
+
from pycg.machinery.definitions import Definition
|
|
26
|
+
from pycg.processing.base import ProcessingBase
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class CallGraphProcessor(ProcessingBase):
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
filename,
|
|
33
|
+
modname,
|
|
34
|
+
import_manager,
|
|
35
|
+
scope_manager,
|
|
36
|
+
def_manager,
|
|
37
|
+
class_manager,
|
|
38
|
+
module_manager,
|
|
39
|
+
call_graph=None,
|
|
40
|
+
modules_analyzed=None,
|
|
41
|
+
):
|
|
42
|
+
super().__init__(filename, modname, modules_analyzed)
|
|
43
|
+
# parent directory of file
|
|
44
|
+
self.parent_dir = os.path.dirname(filename)
|
|
45
|
+
|
|
46
|
+
self.import_manager = import_manager
|
|
47
|
+
self.scope_manager = scope_manager
|
|
48
|
+
self.def_manager = def_manager
|
|
49
|
+
self.class_manager = class_manager
|
|
50
|
+
self.module_manager = module_manager
|
|
51
|
+
|
|
52
|
+
self.call_graph = call_graph
|
|
53
|
+
|
|
54
|
+
self.closured = self.def_manager.transitive_closure()
|
|
55
|
+
|
|
56
|
+
def visit_Module(self, node):
|
|
57
|
+
self.call_graph.add_node(self.modname, self.modname)
|
|
58
|
+
super().visit_Module(node)
|
|
59
|
+
|
|
60
|
+
def visit_For(self, node):
|
|
61
|
+
self.visit(node.iter)
|
|
62
|
+
self.visit(node.target)
|
|
63
|
+
# assign target.id to the return value of __next__ of node.iter.it
|
|
64
|
+
# we need to have a visit for on the postprocessor also
|
|
65
|
+
iter_decoded = self.decode_node(node.iter)
|
|
66
|
+
for item in iter_decoded:
|
|
67
|
+
if not isinstance(item, Definition):
|
|
68
|
+
continue
|
|
69
|
+
names = self.closured.get(item.get_ns(), [])
|
|
70
|
+
for name in names:
|
|
71
|
+
iter_ns = utils.join_ns(name, utils.constants.ITER_METHOD)
|
|
72
|
+
next_ns = utils.join_ns(name, utils.constants.NEXT_METHOD)
|
|
73
|
+
if self.def_manager.get(iter_ns):
|
|
74
|
+
self.call_graph.add_edge(self.current_method, iter_ns)
|
|
75
|
+
if self.def_manager.get(next_ns):
|
|
76
|
+
self.call_graph.add_edge(self.current_method, next_ns)
|
|
77
|
+
|
|
78
|
+
super().visit_For(node)
|
|
79
|
+
|
|
80
|
+
def visit_Lambda(self, node):
|
|
81
|
+
counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
|
|
82
|
+
lambda_name = utils.get_lambda_name(counter)
|
|
83
|
+
lambda_fullns = utils.join_ns(self.current_ns, lambda_name)
|
|
84
|
+
|
|
85
|
+
self.call_graph.add_node(lambda_fullns, self.modname)
|
|
86
|
+
|
|
87
|
+
super().visit_Lambda(node, lambda_name)
|
|
88
|
+
|
|
89
|
+
def visit_Raise(self, node):
|
|
90
|
+
if not node.exc:
|
|
91
|
+
return
|
|
92
|
+
self.visit(node.exc)
|
|
93
|
+
decoded = self.decode_node(node.exc)
|
|
94
|
+
for d in decoded:
|
|
95
|
+
if not isinstance(d, Definition):
|
|
96
|
+
continue
|
|
97
|
+
names = self.closured.get(d.get_ns(), [])
|
|
98
|
+
for name in names:
|
|
99
|
+
pointer_def = self.def_manager.get(name)
|
|
100
|
+
if pointer_def.get_type() == utils.constants.CLS_DEF:
|
|
101
|
+
init_ns = self.find_cls_fun_ns(name, utils.constants.CLS_INIT)
|
|
102
|
+
for ns in init_ns:
|
|
103
|
+
self.call_graph.add_edge(self.current_method, ns)
|
|
104
|
+
if pointer_def.get_type() == utils.constants.EXT_DEF:
|
|
105
|
+
self.call_graph.add_edge(self.current_method, name)
|
|
106
|
+
|
|
107
|
+
def visit_AsyncFunctionDef(self, node):
|
|
108
|
+
self.visit_FunctionDef(node)
|
|
109
|
+
|
|
110
|
+
def visit_FunctionDef(self, node):
|
|
111
|
+
for decorator in node.decorator_list:
|
|
112
|
+
self.visit(decorator)
|
|
113
|
+
decoded = self.decode_node(decorator)
|
|
114
|
+
for d in decoded:
|
|
115
|
+
if not isinstance(d, Definition):
|
|
116
|
+
continue
|
|
117
|
+
names = self.closured.get(d.get_ns(), [])
|
|
118
|
+
for name in names:
|
|
119
|
+
self.call_graph.add_edge(self.current_method, name)
|
|
120
|
+
|
|
121
|
+
self.call_graph.add_node(
|
|
122
|
+
utils.join_ns(self.current_ns, node.name), self.modname
|
|
123
|
+
)
|
|
124
|
+
super().visit_FunctionDef(node)
|
|
125
|
+
|
|
126
|
+
def visit_Call(self, node):
|
|
127
|
+
def create_ext_edge(name, ext_modname):
|
|
128
|
+
self.add_ext_mod_node(name)
|
|
129
|
+
self.call_graph.add_node(name, ext_modname)
|
|
130
|
+
self.call_graph.add_edge(self.current_method, name)
|
|
131
|
+
|
|
132
|
+
# First visit the child function so that on the case of
|
|
133
|
+
# func()()()
|
|
134
|
+
# we first visit the call to func and then the other calls
|
|
135
|
+
for arg in node.args:
|
|
136
|
+
self.visit(arg)
|
|
137
|
+
|
|
138
|
+
for keyword in node.keywords:
|
|
139
|
+
self.visit(keyword.value)
|
|
140
|
+
|
|
141
|
+
self.visit(node.func)
|
|
142
|
+
|
|
143
|
+
names = self.retrieve_call_names(node)
|
|
144
|
+
if not names:
|
|
145
|
+
if isinstance(node.func, ast.Attribute) and self.has_ext_parent(node.func):
|
|
146
|
+
# TODO: This doesn't work for cases
|
|
147
|
+
# where there is an assignment of an attribute
|
|
148
|
+
# i.e. import os; lala = os.path; lala.dirname()
|
|
149
|
+
for name in self.get_full_attr_names(node.func):
|
|
150
|
+
ext_modname = name.split(".")[0]
|
|
151
|
+
create_ext_edge(name, ext_modname)
|
|
152
|
+
elif getattr(node.func, "id", None) and self.is_builtin(node.func.id):
|
|
153
|
+
name = utils.join_ns(utils.constants.BUILTIN_NAME, node.func.id)
|
|
154
|
+
create_ext_edge(name, utils.constants.BUILTIN_NAME)
|
|
155
|
+
return
|
|
156
|
+
|
|
157
|
+
self.last_called_names = names
|
|
158
|
+
for pointer in names:
|
|
159
|
+
pointer_def = self.def_manager.get(pointer)
|
|
160
|
+
if not pointer_def or not isinstance(pointer_def, Definition):
|
|
161
|
+
continue
|
|
162
|
+
if pointer_def.is_callable():
|
|
163
|
+
if pointer_def.get_type() == utils.constants.EXT_DEF:
|
|
164
|
+
ext_modname = pointer.split(".")[0]
|
|
165
|
+
create_ext_edge(pointer, ext_modname)
|
|
166
|
+
continue
|
|
167
|
+
self.call_graph.add_edge(self.current_method, pointer)
|
|
168
|
+
|
|
169
|
+
# TODO: This doesn't work
|
|
170
|
+
# and leads to calls from the decorators
|
|
171
|
+
# themselves to the function,
|
|
172
|
+
# creating edges to the first decorator
|
|
173
|
+
# for decorator in pointer_def.decorator_names:
|
|
174
|
+
# dec_names = self.closured.get(decorator, [])
|
|
175
|
+
# for dec_name in dec_names:
|
|
176
|
+
# if self.def_manager.get(dec_name).
|
|
177
|
+
# get_type() == utils.constants.FUN_DEF:
|
|
178
|
+
# self.call_graph.add_edge(self.current_ns, dec_name)
|
|
179
|
+
|
|
180
|
+
if pointer_def.get_type() == utils.constants.CLS_DEF:
|
|
181
|
+
init_ns = self.find_cls_fun_ns(pointer, utils.constants.CLS_INIT)
|
|
182
|
+
|
|
183
|
+
for ns in init_ns:
|
|
184
|
+
self.call_graph.add_edge(self.current_method, ns)
|
|
185
|
+
|
|
186
|
+
def analyze_submodules(self):
|
|
187
|
+
super().analyze_submodules(
|
|
188
|
+
CallGraphProcessor,
|
|
189
|
+
self.import_manager,
|
|
190
|
+
self.scope_manager,
|
|
191
|
+
self.def_manager,
|
|
192
|
+
self.class_manager,
|
|
193
|
+
self.module_manager,
|
|
194
|
+
call_graph=self.call_graph,
|
|
195
|
+
modules_analyzed=self.get_modules_analyzed(),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def analyze(self):
|
|
199
|
+
self.visit(ast.parse(self.contents, self.filename))
|
|
200
|
+
self.analyze_submodules()
|
|
201
|
+
|
|
202
|
+
def get_all_reachable_functions(self):
|
|
203
|
+
reachable = set()
|
|
204
|
+
names = set()
|
|
205
|
+
current_scope = self.scope_manager.get_scope(self.current_ns)
|
|
206
|
+
while current_scope:
|
|
207
|
+
for name, defi in current_scope.get_defs().items():
|
|
208
|
+
if defi.is_function_def() and name not in names:
|
|
209
|
+
closured = self.closured.get(defi.get_ns())
|
|
210
|
+
for item in closured:
|
|
211
|
+
reachable.add(item)
|
|
212
|
+
names.add(name)
|
|
213
|
+
current_scope = current_scope.parent
|
|
214
|
+
|
|
215
|
+
return reachable
|
|
216
|
+
|
|
217
|
+
def has_ext_parent(self, node):
|
|
218
|
+
if not isinstance(node, ast.Attribute):
|
|
219
|
+
return False
|
|
220
|
+
|
|
221
|
+
while isinstance(node, ast.Attribute):
|
|
222
|
+
parents = self._retrieve_parent_names(node)
|
|
223
|
+
for parent in parents:
|
|
224
|
+
for name in self.closured.get(parent, []):
|
|
225
|
+
defi = self.def_manager.get(name)
|
|
226
|
+
if defi and defi.is_ext_def():
|
|
227
|
+
return True
|
|
228
|
+
node = node.value
|
|
229
|
+
return False
|
|
230
|
+
|
|
231
|
+
def get_full_attr_names(self, node):
|
|
232
|
+
name = ""
|
|
233
|
+
while isinstance(node, ast.Attribute):
|
|
234
|
+
if not name:
|
|
235
|
+
name = node.attr
|
|
236
|
+
else:
|
|
237
|
+
name = node.attr + "." + name
|
|
238
|
+
node = node.value
|
|
239
|
+
|
|
240
|
+
names = []
|
|
241
|
+
if getattr(node, "id", None) is None:
|
|
242
|
+
return names
|
|
243
|
+
|
|
244
|
+
defi = self.scope_manager.get_def(self.current_ns, node.id)
|
|
245
|
+
if defi and self.closured.get(defi.get_ns()):
|
|
246
|
+
for id in self.closured.get(defi.get_ns()):
|
|
247
|
+
names.append(id + "." + name)
|
|
248
|
+
|
|
249
|
+
return names
|
|
250
|
+
|
|
251
|
+
def is_builtin(self, name):
|
|
252
|
+
return name in __builtins__
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
import ast
|
|
22
|
+
import os
|
|
23
|
+
import re
|
|
24
|
+
|
|
25
|
+
from pycg import utils
|
|
26
|
+
from pycg.processing.base import ProcessingBase
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class KeyErrProcessor(ProcessingBase):
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
filename,
|
|
33
|
+
modname,
|
|
34
|
+
import_manager,
|
|
35
|
+
scope_manager,
|
|
36
|
+
def_manager,
|
|
37
|
+
class_manager,
|
|
38
|
+
key_errs,
|
|
39
|
+
modules_analyzed=None,
|
|
40
|
+
):
|
|
41
|
+
super().__init__(filename, modname, modules_analyzed)
|
|
42
|
+
# parent directory of file
|
|
43
|
+
self.parent_dir = os.path.dirname(filename)
|
|
44
|
+
|
|
45
|
+
self.import_manager = import_manager
|
|
46
|
+
self.scope_manager = scope_manager
|
|
47
|
+
self.def_manager = def_manager
|
|
48
|
+
self.class_manager = class_manager
|
|
49
|
+
self.key_errs = key_errs
|
|
50
|
+
|
|
51
|
+
self.closured = self.def_manager.transitive_closure()
|
|
52
|
+
self.state = "keyerr"
|
|
53
|
+
|
|
54
|
+
def visit_Subscript(self, node):
|
|
55
|
+
self.visit(node.value)
|
|
56
|
+
self.visit(node.slice)
|
|
57
|
+
names = self.retrieve_subscript_names(node)
|
|
58
|
+
for name in names:
|
|
59
|
+
if not self.is_subscriptable(name):
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
defi = self.def_manager.get(name)
|
|
63
|
+
if not defi:
|
|
64
|
+
splitted = name.split(".")
|
|
65
|
+
|
|
66
|
+
self.key_errs.add(
|
|
67
|
+
filename=os.path.relpath(
|
|
68
|
+
self.filename, self.import_manager.get_mod_dir()
|
|
69
|
+
),
|
|
70
|
+
lineno=node.lineno,
|
|
71
|
+
namespace=".".join(splitted[:-1]),
|
|
72
|
+
key=splitted[-1],
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def is_subscriptable(self, name):
|
|
76
|
+
if re.match(r".*<dict[0-9]+>.*", name):
|
|
77
|
+
return True
|
|
78
|
+
|
|
79
|
+
return False
|
|
80
|
+
|
|
81
|
+
def analyze_submodules(self):
|
|
82
|
+
super().analyze_submodules(
|
|
83
|
+
KeyErrProcessor,
|
|
84
|
+
self.import_manager,
|
|
85
|
+
self.scope_manager,
|
|
86
|
+
self.def_manager,
|
|
87
|
+
self.class_manager,
|
|
88
|
+
self.key_errs,
|
|
89
|
+
modules_analyzed=self.get_modules_analyzed(),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def analyze(self):
|
|
93
|
+
self.visit(ast.parse(self.contents, self.filename))
|
|
94
|
+
self.analyze_submodules()
|
|
95
|
+
|
|
96
|
+
def visit_Lambda(self, node):
|
|
97
|
+
counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
|
|
98
|
+
lambda_name = utils.get_lambda_name(counter)
|
|
99
|
+
utils.join_ns(self.current_ns, lambda_name)
|
|
100
|
+
|
|
101
|
+
super().visit_Lambda(node, lambda_name)
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
import ast
|
|
22
|
+
|
|
23
|
+
from pycg import utils
|
|
24
|
+
from pycg.machinery.definitions import Definition
|
|
25
|
+
from pycg.processing.base import ProcessingBase
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class PostProcessor(ProcessingBase):
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
input_file,
|
|
32
|
+
modname,
|
|
33
|
+
import_manager,
|
|
34
|
+
scope_manager,
|
|
35
|
+
def_manager,
|
|
36
|
+
class_manager,
|
|
37
|
+
module_manager,
|
|
38
|
+
modules_analyzed=None,
|
|
39
|
+
):
|
|
40
|
+
super().__init__(input_file, modname, modules_analyzed)
|
|
41
|
+
self.import_manager = import_manager
|
|
42
|
+
self.scope_manager = scope_manager
|
|
43
|
+
self.def_manager = def_manager
|
|
44
|
+
self.class_manager = class_manager
|
|
45
|
+
self.module_manager = module_manager
|
|
46
|
+
self.closured = self.def_manager.transitive_closure()
|
|
47
|
+
|
|
48
|
+
def visit_Lambda(self, node):
|
|
49
|
+
counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
|
|
50
|
+
lambda_name = utils.get_lambda_name(counter)
|
|
51
|
+
super().visit_Lambda(node, lambda_name)
|
|
52
|
+
|
|
53
|
+
def visit_Call(self, node):
|
|
54
|
+
self.visit(node.func)
|
|
55
|
+
|
|
56
|
+
names = self.retrieve_call_names(node)
|
|
57
|
+
if not names:
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
self.last_called_names = names
|
|
61
|
+
|
|
62
|
+
for name in names:
|
|
63
|
+
defi = self.def_manager.get(name)
|
|
64
|
+
if not defi:
|
|
65
|
+
continue
|
|
66
|
+
if defi.get_type() == utils.constants.CLS_DEF:
|
|
67
|
+
self.update_parent_classes(defi)
|
|
68
|
+
defi = self.def_manager.get(
|
|
69
|
+
utils.join_ns(defi.get_ns(), utils.constants.CLS_INIT)
|
|
70
|
+
)
|
|
71
|
+
if not defi:
|
|
72
|
+
continue
|
|
73
|
+
self.iterate_call_args(defi, node)
|
|
74
|
+
|
|
75
|
+
def visit_Assign(self, node):
|
|
76
|
+
self._visit_assign(node.value, node.targets)
|
|
77
|
+
|
|
78
|
+
# Redefined in line 118, 121
|
|
79
|
+
# def visit_Return(self, node):
|
|
80
|
+
# self._visit_return(node)
|
|
81
|
+
|
|
82
|
+
# def visit_Yield(self, node):
|
|
83
|
+
# self._visit_return(node)
|
|
84
|
+
|
|
85
|
+
def visit_For(self, node):
|
|
86
|
+
# only handle name targets
|
|
87
|
+
if isinstance(node.target, ast.Name):
|
|
88
|
+
target_def = self.def_manager.get(
|
|
89
|
+
utils.join_ns(self.current_ns, node.target.id)
|
|
90
|
+
)
|
|
91
|
+
# if the target definition exists
|
|
92
|
+
if target_def:
|
|
93
|
+
iter_decoded = self.decode_node(node.iter)
|
|
94
|
+
# assign the target to the return value
|
|
95
|
+
# of the next function
|
|
96
|
+
for item in iter_decoded:
|
|
97
|
+
if not isinstance(item, Definition):
|
|
98
|
+
continue
|
|
99
|
+
# return value for generators
|
|
100
|
+
for name in self.closured.get(item.get_ns(), []):
|
|
101
|
+
# If there exists a next method on the iterable
|
|
102
|
+
# and if yes, add a pointer to it
|
|
103
|
+
next_defi = self.def_manager.get(
|
|
104
|
+
utils.join_ns(
|
|
105
|
+
name,
|
|
106
|
+
utils.constants.NEXT_METHOD,
|
|
107
|
+
utils.constants.RETURN_NAME,
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
if next_defi:
|
|
111
|
+
for name in self.closured.get(next_defi.get_ns(), []):
|
|
112
|
+
target_def.get_name_pointer().add(name)
|
|
113
|
+
else: # otherwise, add a pointer to the name
|
|
114
|
+
# (e.g. a yield)
|
|
115
|
+
target_def.get_name_pointer().add(name)
|
|
116
|
+
|
|
117
|
+
super().visit_For(node)
|
|
118
|
+
|
|
119
|
+
def visit_Return(self, node):
|
|
120
|
+
self._visit_return(node)
|
|
121
|
+
|
|
122
|
+
def visit_Yield(self, node):
|
|
123
|
+
self._visit_return(node)
|
|
124
|
+
|
|
125
|
+
def visit_AsyncFunctionDef(self, node):
|
|
126
|
+
self.visit_FunctionDef(node)
|
|
127
|
+
|
|
128
|
+
def visit_FunctionDef(self, node):
|
|
129
|
+
# here we iterate decorators
|
|
130
|
+
if node.decorator_list:
|
|
131
|
+
fn_def = self.def_manager.get(utils.join_ns(self.current_ns, node.name))
|
|
132
|
+
reversed_decorators = list(reversed(node.decorator_list))
|
|
133
|
+
|
|
134
|
+
# add to the name pointer of the function definition
|
|
135
|
+
# the return value of the first decorator
|
|
136
|
+
# since, now the function is a namespace to that point
|
|
137
|
+
if hasattr(fn_def, "decorator_names") and reversed_decorators:
|
|
138
|
+
last_decoded = self.decode_node(reversed_decorators[-1])
|
|
139
|
+
for d in last_decoded:
|
|
140
|
+
if not isinstance(d, Definition):
|
|
141
|
+
continue
|
|
142
|
+
fn_def.decorator_names.add(
|
|
143
|
+
utils.join_ns(d.get_ns(), utils.constants.RETURN_NAME)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
previous_names = self.closured.get(fn_def.get_ns(), set())
|
|
147
|
+
for decorator in reversed_decorators:
|
|
148
|
+
# assign the previous_def
|
|
149
|
+
# as the first parameter of the decorator
|
|
150
|
+
decoded = self.decode_node(decorator)
|
|
151
|
+
new_previous_names = set()
|
|
152
|
+
for d in decoded:
|
|
153
|
+
if not isinstance(d, Definition):
|
|
154
|
+
continue
|
|
155
|
+
for name in self.closured.get(d.get_ns(), []):
|
|
156
|
+
return_ns = utils.join_ns(name, utils.constants.RETURN_NAME)
|
|
157
|
+
|
|
158
|
+
if self.closured.get(return_ns, None) is None:
|
|
159
|
+
continue
|
|
160
|
+
|
|
161
|
+
new_previous_names = new_previous_names.union(
|
|
162
|
+
self.closured.get(return_ns)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
for prev_name in previous_names:
|
|
166
|
+
pos_arg_names = d.get_name_pointer().get_pos_arg(0)
|
|
167
|
+
if not pos_arg_names:
|
|
168
|
+
continue
|
|
169
|
+
for name in pos_arg_names:
|
|
170
|
+
arg_def = self.def_manager.get(name)
|
|
171
|
+
arg_def.get_name_pointer().add(prev_name)
|
|
172
|
+
previous_names = new_previous_names
|
|
173
|
+
|
|
174
|
+
super().visit_FunctionDef(node)
|
|
175
|
+
|
|
176
|
+
def visit_ClassDef(self, node):
|
|
177
|
+
# create a definition for the class (node.name)
|
|
178
|
+
cls_def = self.def_manager.handle_class_def(self.current_ns, node.name)
|
|
179
|
+
|
|
180
|
+
# iterate bases to compute MRO for the class
|
|
181
|
+
cls = self.class_manager.get(cls_def.get_ns())
|
|
182
|
+
if not cls:
|
|
183
|
+
cls = self.class_manager.create(cls_def.get_ns(), self.modname)
|
|
184
|
+
|
|
185
|
+
cls.clear_mro()
|
|
186
|
+
for base in node.bases:
|
|
187
|
+
# all bases are of the type ast.Name
|
|
188
|
+
self.visit(base)
|
|
189
|
+
|
|
190
|
+
bases = self.decode_node(base)
|
|
191
|
+
for base_def in bases:
|
|
192
|
+
if not isinstance(base_def, Definition):
|
|
193
|
+
continue
|
|
194
|
+
names = set()
|
|
195
|
+
if base_def.get_name_pointer().get():
|
|
196
|
+
names = base_def.get_name_pointer().get()
|
|
197
|
+
else:
|
|
198
|
+
names.add(base_def.get_ns())
|
|
199
|
+
for name in names:
|
|
200
|
+
# add the base as a parent
|
|
201
|
+
cls.add_parent(name)
|
|
202
|
+
|
|
203
|
+
# add the base's parents
|
|
204
|
+
parent_cls = self.class_manager.get(name)
|
|
205
|
+
if parent_cls:
|
|
206
|
+
cls.add_parent(parent_cls.get_mro())
|
|
207
|
+
|
|
208
|
+
cls.compute_mro()
|
|
209
|
+
|
|
210
|
+
super().visit_ClassDef(node)
|
|
211
|
+
|
|
212
|
+
def visit_List(self, node):
|
|
213
|
+
# Works similarly with dicts
|
|
214
|
+
current_scope = self.scope_manager.get_scope(self.current_ns)
|
|
215
|
+
list_counter = current_scope.inc_list_counter()
|
|
216
|
+
list_name = utils.get_list_name(list_counter)
|
|
217
|
+
list_full_ns = utils.join_ns(self.current_ns, list_name)
|
|
218
|
+
|
|
219
|
+
# create a scope for the list
|
|
220
|
+
self.scope_manager.create_scope(list_full_ns, current_scope)
|
|
221
|
+
|
|
222
|
+
# create a list definition
|
|
223
|
+
list_def = self.def_manager.get(list_full_ns)
|
|
224
|
+
if not list_def:
|
|
225
|
+
list_def = self.def_manager.create(list_full_ns, utils.constants.NAME_DEF)
|
|
226
|
+
current_scope.add_def(list_name, list_def)
|
|
227
|
+
|
|
228
|
+
self.name_stack.append(list_name)
|
|
229
|
+
for idx, elt in enumerate(node.elts):
|
|
230
|
+
self.visit(elt)
|
|
231
|
+
key_full_ns = utils.join_ns(list_def.get_ns(), utils.get_int_name(idx))
|
|
232
|
+
key_def = self.def_manager.get(key_full_ns)
|
|
233
|
+
if not key_def:
|
|
234
|
+
key_def = self.def_manager.create(key_full_ns, utils.constants.NAME_DEF)
|
|
235
|
+
|
|
236
|
+
decoded_elt = self.decode_node(elt)
|
|
237
|
+
for v in decoded_elt:
|
|
238
|
+
if isinstance(v, Definition):
|
|
239
|
+
key_def.get_name_pointer().add(v.get_ns())
|
|
240
|
+
else:
|
|
241
|
+
key_def.get_lit_pointer().add(v)
|
|
242
|
+
|
|
243
|
+
self.name_stack.pop()
|
|
244
|
+
|
|
245
|
+
def visit_Dict(self, node):
|
|
246
|
+
# 1. create a scope using a counter
|
|
247
|
+
# 2. Iterate keys and add them as children of the scope
|
|
248
|
+
# 3. Iterate values and makes a points to connection with the keys
|
|
249
|
+
current_scope = self.scope_manager.get_scope(self.current_ns)
|
|
250
|
+
dict_counter = current_scope.inc_dict_counter()
|
|
251
|
+
dict_name = utils.get_dict_name(dict_counter)
|
|
252
|
+
dict_full_ns = utils.join_ns(self.current_ns, dict_name)
|
|
253
|
+
|
|
254
|
+
# create a scope for the dict
|
|
255
|
+
dict_scope = self.scope_manager.create_scope(dict_full_ns, current_scope)
|
|
256
|
+
|
|
257
|
+
# Create a dict definition
|
|
258
|
+
dict_def = self.def_manager.get(dict_full_ns)
|
|
259
|
+
if not dict_def:
|
|
260
|
+
dict_def = self.def_manager.create(dict_full_ns, utils.constants.NAME_DEF)
|
|
261
|
+
# add it to the current scope
|
|
262
|
+
current_scope.add_def(dict_name, dict_def)
|
|
263
|
+
|
|
264
|
+
self.name_stack.append(dict_name)
|
|
265
|
+
for key, value in zip(node.keys, node.values):
|
|
266
|
+
if key:
|
|
267
|
+
self.visit(key)
|
|
268
|
+
if value:
|
|
269
|
+
self.visit(value)
|
|
270
|
+
decoded_key = self.decode_node(key)
|
|
271
|
+
decoded_value = self.decode_node(value)
|
|
272
|
+
|
|
273
|
+
# iterate decoded keys and values
|
|
274
|
+
# to do the assignment operation
|
|
275
|
+
for k in decoded_key:
|
|
276
|
+
if isinstance(k, Definition):
|
|
277
|
+
# get literal pointer
|
|
278
|
+
names = k.get_lit_pointer().get()
|
|
279
|
+
else:
|
|
280
|
+
names = set()
|
|
281
|
+
if isinstance(k, list):
|
|
282
|
+
continue
|
|
283
|
+
names.add(k)
|
|
284
|
+
for name in names:
|
|
285
|
+
# create a definition for the key
|
|
286
|
+
if isinstance(name, int):
|
|
287
|
+
name = utils.get_int_name(name)
|
|
288
|
+
key_full_ns = utils.join_ns(dict_def.get_ns(), str(name))
|
|
289
|
+
key_def = self.def_manager.get(key_full_ns)
|
|
290
|
+
if not key_def:
|
|
291
|
+
key_def = self.def_manager.create(
|
|
292
|
+
key_full_ns, utils.constants.NAME_DEF
|
|
293
|
+
)
|
|
294
|
+
dict_scope.add_def(str(name), key_def)
|
|
295
|
+
for v in decoded_value:
|
|
296
|
+
if isinstance(v, Definition):
|
|
297
|
+
key_def.get_name_pointer().add(v.get_ns())
|
|
298
|
+
else:
|
|
299
|
+
key_def.get_lit_pointer().add(v)
|
|
300
|
+
self.name_stack.pop()
|
|
301
|
+
|
|
302
|
+
def update_parent_classes(self, defi):
|
|
303
|
+
cls = self.class_manager.get(defi.get_ns())
|
|
304
|
+
if not cls:
|
|
305
|
+
return
|
|
306
|
+
current_scope = self.scope_manager.get_scope(defi.get_ns())
|
|
307
|
+
for parent in cls.get_mro():
|
|
308
|
+
parent_def = self.def_manager.get(parent)
|
|
309
|
+
if not parent_def:
|
|
310
|
+
continue
|
|
311
|
+
parent_scope = self.scope_manager.get_scope(parent)
|
|
312
|
+
if not parent_scope:
|
|
313
|
+
continue
|
|
314
|
+
list(parent_scope.get_defs().keys())
|
|
315
|
+
for key, child_def in current_scope.get_defs().items():
|
|
316
|
+
if key == "__init__":
|
|
317
|
+
continue
|
|
318
|
+
# resolve name from the parent_def
|
|
319
|
+
names = self.find_cls_fun_ns(parent_def.get_ns(), key)
|
|
320
|
+
|
|
321
|
+
new_ns = utils.join_ns(parent_def.get_ns(), key)
|
|
322
|
+
new_def = self.def_manager.get(new_ns)
|
|
323
|
+
if not new_def:
|
|
324
|
+
new_def = self.def_manager.create(new_ns, utils.constants.NAME_DEF)
|
|
325
|
+
|
|
326
|
+
new_def.get_name_pointer().add_set(names)
|
|
327
|
+
new_def.get_name_pointer().add(child_def.get_ns())
|
|
328
|
+
|
|
329
|
+
def analyze_submodules(self):
|
|
330
|
+
super().analyze_submodules(
|
|
331
|
+
PostProcessor,
|
|
332
|
+
self.import_manager,
|
|
333
|
+
self.scope_manager,
|
|
334
|
+
self.def_manager,
|
|
335
|
+
self.class_manager,
|
|
336
|
+
self.module_manager,
|
|
337
|
+
modules_analyzed=self.get_modules_analyzed(),
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
def analyze(self):
|
|
341
|
+
self.visit(ast.parse(self.contents, self.filename))
|
|
342
|
+
self.analyze_submodules()
|