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
PyCG/machinery/scopes.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
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 symtable
|
|
22
|
+
|
|
23
|
+
from pycg import utils
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ScopeManager(object):
|
|
27
|
+
"""Manages the scope entries"""
|
|
28
|
+
|
|
29
|
+
def __init__(self):
|
|
30
|
+
self.scopes = {}
|
|
31
|
+
|
|
32
|
+
def handle_module(self, modulename, filename, contents):
|
|
33
|
+
functions = []
|
|
34
|
+
classes = []
|
|
35
|
+
|
|
36
|
+
def process(namespace, parent, table):
|
|
37
|
+
if table.get_name() == "top" and table.get_lineno() == 0:
|
|
38
|
+
name = ""
|
|
39
|
+
else:
|
|
40
|
+
name = table.get_name()
|
|
41
|
+
|
|
42
|
+
if name:
|
|
43
|
+
fullns = utils.join_ns(namespace, name)
|
|
44
|
+
else:
|
|
45
|
+
fullns = namespace
|
|
46
|
+
|
|
47
|
+
if table.get_type() == "function":
|
|
48
|
+
functions.append(fullns)
|
|
49
|
+
|
|
50
|
+
if table.get_type() == "class":
|
|
51
|
+
classes.append(fullns)
|
|
52
|
+
|
|
53
|
+
sc = self.create_scope(fullns, parent)
|
|
54
|
+
|
|
55
|
+
for t in table.get_children():
|
|
56
|
+
process(fullns, sc, t)
|
|
57
|
+
|
|
58
|
+
process(
|
|
59
|
+
modulename, None, symtable.symtable(contents, filename, compile_type="exec")
|
|
60
|
+
)
|
|
61
|
+
return {"functions": functions, "classes": classes}
|
|
62
|
+
|
|
63
|
+
def handle_assign(self, ns, target, defi):
|
|
64
|
+
scope = self.get_scope(ns)
|
|
65
|
+
if scope:
|
|
66
|
+
scope.add_def(target, defi)
|
|
67
|
+
|
|
68
|
+
def get_def(self, current_ns, var_name):
|
|
69
|
+
current_scope = self.get_scope(current_ns)
|
|
70
|
+
while current_scope:
|
|
71
|
+
defi = current_scope.get_def(var_name)
|
|
72
|
+
if defi:
|
|
73
|
+
return defi
|
|
74
|
+
current_scope = current_scope.parent
|
|
75
|
+
|
|
76
|
+
def get_scope(self, namespace):
|
|
77
|
+
if namespace in self.get_scopes():
|
|
78
|
+
return self.get_scopes()[namespace]
|
|
79
|
+
|
|
80
|
+
def create_scope(self, namespace, parent):
|
|
81
|
+
if namespace not in self.scopes:
|
|
82
|
+
sc = ScopeItem(namespace, parent)
|
|
83
|
+
self.scopes[namespace] = sc
|
|
84
|
+
return self.scopes[namespace]
|
|
85
|
+
|
|
86
|
+
def get_scopes(self):
|
|
87
|
+
return self.scopes
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class ScopeItem(object):
|
|
91
|
+
def __init__(self, fullns, parent):
|
|
92
|
+
if parent and not isinstance(parent, ScopeItem):
|
|
93
|
+
raise ScopeError("Parent must be a ScopeItem instance")
|
|
94
|
+
|
|
95
|
+
if not isinstance(fullns, str):
|
|
96
|
+
raise ScopeError("Namespace should be a string")
|
|
97
|
+
|
|
98
|
+
self.parent = parent
|
|
99
|
+
self.defs = {}
|
|
100
|
+
self.lambda_counter = 0
|
|
101
|
+
self.dict_counter = 0
|
|
102
|
+
self.list_counter = 0
|
|
103
|
+
self.fullns = fullns
|
|
104
|
+
|
|
105
|
+
def get_ns(self):
|
|
106
|
+
return self.fullns
|
|
107
|
+
|
|
108
|
+
def get_defs(self):
|
|
109
|
+
return self.defs
|
|
110
|
+
|
|
111
|
+
def get_def(self, name):
|
|
112
|
+
defs = self.get_defs()
|
|
113
|
+
if name in defs:
|
|
114
|
+
return defs[name]
|
|
115
|
+
|
|
116
|
+
def get_lambda_counter(self):
|
|
117
|
+
return self.lambda_counter
|
|
118
|
+
|
|
119
|
+
def get_dict_counter(self):
|
|
120
|
+
return self.dict_counter
|
|
121
|
+
|
|
122
|
+
def get_list_counter(self):
|
|
123
|
+
return self.list_counter
|
|
124
|
+
|
|
125
|
+
def inc_lambda_counter(self, val=1):
|
|
126
|
+
self.lambda_counter += val
|
|
127
|
+
return self.lambda_counter
|
|
128
|
+
|
|
129
|
+
def inc_dict_counter(self, val=1):
|
|
130
|
+
self.dict_counter += val
|
|
131
|
+
return self.dict_counter
|
|
132
|
+
|
|
133
|
+
def inc_list_counter(self, val=1):
|
|
134
|
+
self.list_counter += val
|
|
135
|
+
return self.list_counter
|
|
136
|
+
|
|
137
|
+
def reset_counters(self):
|
|
138
|
+
self.lambda_counter = 0
|
|
139
|
+
self.dict_counter = 0
|
|
140
|
+
self.list_counter = 0
|
|
141
|
+
|
|
142
|
+
def add_def(self, name, defi):
|
|
143
|
+
self.defs[name] = defi
|
|
144
|
+
|
|
145
|
+
def merge_def(self, name, to_merge):
|
|
146
|
+
if name not in self.defs:
|
|
147
|
+
self.defs[name] = to_merge
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
self.defs[name].merge_points_to(to_merge.get_points_to())
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class ScopeError(Exception):
|
|
154
|
+
pass
|
|
File without changes
|
PyCG/processing/base.py
ADDED
|
@@ -0,0 +1,547 @@
|
|
|
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
|
+
|
|
27
|
+
|
|
28
|
+
class ProcessingBase(ast.NodeVisitor):
|
|
29
|
+
def __init__(self, filename, modname, modules_analyzed):
|
|
30
|
+
self.modname = modname
|
|
31
|
+
|
|
32
|
+
self.modules_analyzed = modules_analyzed
|
|
33
|
+
self.modules_analyzed.add(self.modname)
|
|
34
|
+
|
|
35
|
+
self.filename = os.path.abspath(filename)
|
|
36
|
+
|
|
37
|
+
with open(filename, "rt", errors="replace") as f:
|
|
38
|
+
self.contents = f.read()
|
|
39
|
+
|
|
40
|
+
self.name_stack = []
|
|
41
|
+
self.method_stack = []
|
|
42
|
+
self.last_called_names = None
|
|
43
|
+
|
|
44
|
+
def get_modules_analyzed(self):
|
|
45
|
+
return self.modules_analyzed
|
|
46
|
+
|
|
47
|
+
def merge_modules_analyzed(self, analyzed):
|
|
48
|
+
self.modules_analyzed = self.modules_analyzed.union(analyzed)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def current_ns(self):
|
|
52
|
+
return ".".join(self.name_stack)
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def current_method(self):
|
|
56
|
+
return ".".join(self.method_stack)
|
|
57
|
+
|
|
58
|
+
def visit_Module(self, node):
|
|
59
|
+
self.name_stack.append(self.modname)
|
|
60
|
+
self.method_stack.append(self.modname)
|
|
61
|
+
self.scope_manager.get_scope(self.modname).reset_counters()
|
|
62
|
+
self.generic_visit(node)
|
|
63
|
+
self.method_stack.pop()
|
|
64
|
+
self.name_stack.pop()
|
|
65
|
+
|
|
66
|
+
def visit_FunctionDef(self, node):
|
|
67
|
+
self.name_stack.append(node.name)
|
|
68
|
+
self.method_stack.append(node.name)
|
|
69
|
+
if self.scope_manager.get_scope(self.current_ns):
|
|
70
|
+
self.scope_manager.get_scope(self.current_ns).reset_counters()
|
|
71
|
+
for stmt in node.body:
|
|
72
|
+
self.visit(stmt)
|
|
73
|
+
self.method_stack.pop()
|
|
74
|
+
self.name_stack.pop()
|
|
75
|
+
|
|
76
|
+
def visit_Lambda(self, node, lambda_name=None):
|
|
77
|
+
lambda_ns = utils.join_ns(self.current_ns, lambda_name)
|
|
78
|
+
if not self.scope_manager.get_scope(lambda_ns):
|
|
79
|
+
self.scope_manager.create_scope(
|
|
80
|
+
lambda_ns, self.scope_manager.get_scope(self.current_ns)
|
|
81
|
+
)
|
|
82
|
+
self.name_stack.append(lambda_name)
|
|
83
|
+
self.method_stack.append(lambda_name)
|
|
84
|
+
self.visit(node.body)
|
|
85
|
+
self.method_stack.pop()
|
|
86
|
+
self.name_stack.pop()
|
|
87
|
+
|
|
88
|
+
def visit_For(self, node):
|
|
89
|
+
for item in node.body:
|
|
90
|
+
self.visit(item)
|
|
91
|
+
|
|
92
|
+
def visit_Dict(self, node):
|
|
93
|
+
counter = self.scope_manager.get_scope(self.current_ns).inc_dict_counter()
|
|
94
|
+
dict_name = utils.get_dict_name(counter)
|
|
95
|
+
|
|
96
|
+
sc = self.scope_manager.get_scope(utils.join_ns(self.current_ns, dict_name))
|
|
97
|
+
if not sc:
|
|
98
|
+
return
|
|
99
|
+
self.name_stack.append(dict_name)
|
|
100
|
+
sc.reset_counters()
|
|
101
|
+
for key, val in zip(node.keys, node.values):
|
|
102
|
+
if key:
|
|
103
|
+
self.visit(key)
|
|
104
|
+
if val:
|
|
105
|
+
self.visit(val)
|
|
106
|
+
self.name_stack.pop()
|
|
107
|
+
|
|
108
|
+
def visit_List(self, node):
|
|
109
|
+
counter = self.scope_manager.get_scope(self.current_ns).inc_list_counter()
|
|
110
|
+
list_name = utils.get_list_name(counter)
|
|
111
|
+
|
|
112
|
+
sc = self.scope_manager.get_scope(utils.join_ns(self.current_ns, list_name))
|
|
113
|
+
if not sc:
|
|
114
|
+
return
|
|
115
|
+
self.name_stack.append(list_name)
|
|
116
|
+
sc.reset_counters()
|
|
117
|
+
for elt in node.elts:
|
|
118
|
+
self.visit(elt)
|
|
119
|
+
self.name_stack.pop()
|
|
120
|
+
|
|
121
|
+
def visit_BinOp(self, node):
|
|
122
|
+
self.visit(node.left)
|
|
123
|
+
self.visit(node.right)
|
|
124
|
+
|
|
125
|
+
def visit_ClassDef(self, node):
|
|
126
|
+
self.name_stack.append(node.name)
|
|
127
|
+
self.method_stack.append(node.name)
|
|
128
|
+
if self.scope_manager.get_scope(self.current_ns):
|
|
129
|
+
self.scope_manager.get_scope(self.current_ns).reset_counters()
|
|
130
|
+
for stmt in node.body:
|
|
131
|
+
self.visit(stmt)
|
|
132
|
+
self.method_stack.pop()
|
|
133
|
+
self.name_stack.pop()
|
|
134
|
+
|
|
135
|
+
def visit_Tuple(self, node):
|
|
136
|
+
for elt in node.elts:
|
|
137
|
+
self.visit(elt)
|
|
138
|
+
|
|
139
|
+
def _handle_assign(self, targetns, decoded):
|
|
140
|
+
defi = self.def_manager.get(targetns)
|
|
141
|
+
if not defi:
|
|
142
|
+
defi = self.def_manager.create(targetns, utils.constants.NAME_DEF)
|
|
143
|
+
|
|
144
|
+
# check if decoded is iterable
|
|
145
|
+
try:
|
|
146
|
+
iter(decoded)
|
|
147
|
+
except TypeError:
|
|
148
|
+
return defi
|
|
149
|
+
|
|
150
|
+
for d in decoded:
|
|
151
|
+
if isinstance(d, Definition):
|
|
152
|
+
defi.get_name_pointer().add(d.get_ns())
|
|
153
|
+
else:
|
|
154
|
+
defi.get_lit_pointer().add(d)
|
|
155
|
+
return defi
|
|
156
|
+
|
|
157
|
+
def _visit_return(self, node):
|
|
158
|
+
if not node or not node.value:
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
self.visit(node.value)
|
|
162
|
+
|
|
163
|
+
return_ns = utils.join_ns(self.current_ns, utils.constants.RETURN_NAME)
|
|
164
|
+
self._handle_assign(return_ns, self.decode_node(node.value))
|
|
165
|
+
|
|
166
|
+
def _get_target_ns(self, target):
|
|
167
|
+
if isinstance(target, ast.Name):
|
|
168
|
+
return [utils.join_ns(self.current_ns, target.id)]
|
|
169
|
+
if isinstance(target, ast.Attribute):
|
|
170
|
+
bases = self._retrieve_base_names(target)
|
|
171
|
+
res = []
|
|
172
|
+
for base in bases:
|
|
173
|
+
res.append(utils.join_ns(base, target.attr))
|
|
174
|
+
return res
|
|
175
|
+
if isinstance(target, ast.Subscript):
|
|
176
|
+
return self.retrieve_subscript_names(target)
|
|
177
|
+
return []
|
|
178
|
+
|
|
179
|
+
def _visit_assign(self, value, targets):
|
|
180
|
+
self.visit(value)
|
|
181
|
+
|
|
182
|
+
decoded = self.decode_node(value)
|
|
183
|
+
|
|
184
|
+
def do_assign(decoded, target):
|
|
185
|
+
self.visit(target)
|
|
186
|
+
if isinstance(target, ast.Tuple):
|
|
187
|
+
for pos, elt in enumerate(target.elts):
|
|
188
|
+
if not isinstance(decoded, Definition) and pos < len(decoded):
|
|
189
|
+
do_assign(decoded[pos], elt)
|
|
190
|
+
else:
|
|
191
|
+
targetns = self._get_target_ns(target)
|
|
192
|
+
for tns in targetns:
|
|
193
|
+
if not tns:
|
|
194
|
+
continue
|
|
195
|
+
defi = self._handle_assign(tns, decoded)
|
|
196
|
+
splitted = tns.split(".")
|
|
197
|
+
self.scope_manager.handle_assign(
|
|
198
|
+
".".join(splitted[:-1]), splitted[-1], defi
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
for target in targets:
|
|
202
|
+
do_assign(decoded, target)
|
|
203
|
+
|
|
204
|
+
def decode_node(self, node):
|
|
205
|
+
if isinstance(node, ast.Name):
|
|
206
|
+
return [self.scope_manager.get_def(self.current_ns, node.id)]
|
|
207
|
+
elif isinstance(node, ast.Call):
|
|
208
|
+
decoded = self.decode_node(node.func)
|
|
209
|
+
return_defs = []
|
|
210
|
+
for called_def in decoded:
|
|
211
|
+
if not isinstance(called_def, Definition):
|
|
212
|
+
continue
|
|
213
|
+
|
|
214
|
+
return_ns = utils.constants.INVALID_NAME
|
|
215
|
+
if called_def.get_type() == utils.constants.FUN_DEF:
|
|
216
|
+
return_ns = utils.join_ns(
|
|
217
|
+
called_def.get_ns(), utils.constants.RETURN_NAME
|
|
218
|
+
)
|
|
219
|
+
elif (
|
|
220
|
+
called_def.get_type() == utils.constants.CLS_DEF
|
|
221
|
+
or called_def.get_type() == utils.constants.EXT_DEF
|
|
222
|
+
):
|
|
223
|
+
return_ns = called_def.get_ns()
|
|
224
|
+
defi = self.def_manager.get(return_ns)
|
|
225
|
+
if defi:
|
|
226
|
+
return_defs.append(defi)
|
|
227
|
+
|
|
228
|
+
return return_defs
|
|
229
|
+
elif isinstance(node, ast.Lambda):
|
|
230
|
+
lambda_counter = self.scope_manager.get_scope(
|
|
231
|
+
self.current_ns
|
|
232
|
+
).get_lambda_counter()
|
|
233
|
+
lambda_name = utils.get_lambda_name(lambda_counter)
|
|
234
|
+
return [self.scope_manager.get_def(self.current_ns, lambda_name)]
|
|
235
|
+
elif isinstance(node, ast.Tuple):
|
|
236
|
+
decoded = []
|
|
237
|
+
for elt in node.elts:
|
|
238
|
+
decoded.append(self.decode_node(elt))
|
|
239
|
+
return decoded
|
|
240
|
+
elif isinstance(node, ast.BinOp):
|
|
241
|
+
decoded_left = self.decode_node(node.left)
|
|
242
|
+
decoded_right = self.decode_node(node.right)
|
|
243
|
+
# return the non definition types if we're talking about a binop
|
|
244
|
+
# since we only care about the type of the return (num, str, etc)
|
|
245
|
+
if not isinstance(decoded_left, Definition):
|
|
246
|
+
return decoded_left
|
|
247
|
+
if not isinstance(decoded_right, Definition):
|
|
248
|
+
return decoded_right
|
|
249
|
+
elif isinstance(node, ast.Attribute):
|
|
250
|
+
names = self._retrieve_attribute_names(node)
|
|
251
|
+
defis = []
|
|
252
|
+
for name in names:
|
|
253
|
+
defi = self.def_manager.get(name)
|
|
254
|
+
if defi:
|
|
255
|
+
defis.append(defi)
|
|
256
|
+
return defis
|
|
257
|
+
elif isinstance(node, ast.Num):
|
|
258
|
+
return [node.n]
|
|
259
|
+
elif isinstance(node, ast.Str):
|
|
260
|
+
return [node.s]
|
|
261
|
+
elif self._is_literal(node):
|
|
262
|
+
return [node]
|
|
263
|
+
elif isinstance(node, ast.Dict):
|
|
264
|
+
dict_counter = self.scope_manager.get_scope(
|
|
265
|
+
self.current_ns
|
|
266
|
+
).get_dict_counter()
|
|
267
|
+
dict_name = utils.get_dict_name(dict_counter)
|
|
268
|
+
scope_def = self.scope_manager.get_def(self.current_ns, dict_name)
|
|
269
|
+
return [scope_def]
|
|
270
|
+
elif isinstance(node, ast.List):
|
|
271
|
+
list_counter = self.scope_manager.get_scope(
|
|
272
|
+
self.current_ns
|
|
273
|
+
).get_list_counter()
|
|
274
|
+
list_name = utils.get_list_name(list_counter)
|
|
275
|
+
scope_def = self.scope_manager.get_def(self.current_ns, list_name)
|
|
276
|
+
return [scope_def]
|
|
277
|
+
elif isinstance(node, ast.Subscript):
|
|
278
|
+
names = self.retrieve_subscript_names(node)
|
|
279
|
+
defis = []
|
|
280
|
+
for name in names:
|
|
281
|
+
defi = self.def_manager.get(name)
|
|
282
|
+
if defi:
|
|
283
|
+
defis.append(defi)
|
|
284
|
+
return defis
|
|
285
|
+
|
|
286
|
+
return []
|
|
287
|
+
|
|
288
|
+
def _is_literal(self, item):
|
|
289
|
+
return isinstance(item, int) or isinstance(item, str) or isinstance(item, float)
|
|
290
|
+
|
|
291
|
+
def _retrieve_base_names(self, node):
|
|
292
|
+
if not isinstance(node, ast.Attribute):
|
|
293
|
+
raise Exception("The node is not an attribute")
|
|
294
|
+
|
|
295
|
+
if not getattr(self, "closured", None):
|
|
296
|
+
return set()
|
|
297
|
+
|
|
298
|
+
decoded = self.decode_node(node.value)
|
|
299
|
+
if not decoded:
|
|
300
|
+
return set()
|
|
301
|
+
|
|
302
|
+
names = set()
|
|
303
|
+
for name in decoded:
|
|
304
|
+
if not name or not isinstance(name, Definition):
|
|
305
|
+
continue
|
|
306
|
+
|
|
307
|
+
for base in self.closured.get(name.get_ns(), []):
|
|
308
|
+
cls = self.class_manager.get(base)
|
|
309
|
+
if not cls:
|
|
310
|
+
continue
|
|
311
|
+
|
|
312
|
+
for item in cls.get_mro():
|
|
313
|
+
names.add(item)
|
|
314
|
+
return names
|
|
315
|
+
|
|
316
|
+
def _retrieve_parent_names(self, node):
|
|
317
|
+
if not isinstance(node, ast.Attribute):
|
|
318
|
+
raise Exception("The node is not an attribute")
|
|
319
|
+
|
|
320
|
+
decoded = self.decode_node(node.value)
|
|
321
|
+
if not decoded:
|
|
322
|
+
return set()
|
|
323
|
+
|
|
324
|
+
names = set()
|
|
325
|
+
for parent in decoded:
|
|
326
|
+
if not parent or not isinstance(parent, Definition):
|
|
327
|
+
continue
|
|
328
|
+
if getattr(self, "closured", None) and self.closured.get(
|
|
329
|
+
parent.get_ns(), None
|
|
330
|
+
):
|
|
331
|
+
names = names.union(self.closured.get(parent.get_ns()))
|
|
332
|
+
else:
|
|
333
|
+
names.add(parent.get_ns())
|
|
334
|
+
return names
|
|
335
|
+
|
|
336
|
+
def _retrieve_attribute_names(self, node):
|
|
337
|
+
if not getattr(self, "closured", None):
|
|
338
|
+
return set()
|
|
339
|
+
|
|
340
|
+
parent_names = self._retrieve_parent_names(node)
|
|
341
|
+
names = set()
|
|
342
|
+
for parent_name in parent_names:
|
|
343
|
+
for name in self.closured.get(parent_name, []):
|
|
344
|
+
defi = self.def_manager.get(name)
|
|
345
|
+
if not defi:
|
|
346
|
+
continue
|
|
347
|
+
if defi.get_type() == utils.constants.CLS_DEF:
|
|
348
|
+
cls_names = self.find_cls_fun_ns(defi.get_ns(), node.attr)
|
|
349
|
+
if cls_names:
|
|
350
|
+
names = names.union(cls_names)
|
|
351
|
+
if defi.get_type() in [
|
|
352
|
+
utils.constants.FUN_DEF,
|
|
353
|
+
utils.constants.MOD_DEF,
|
|
354
|
+
]:
|
|
355
|
+
names.add(utils.join_ns(name, node.attr))
|
|
356
|
+
if defi.get_type() == utils.constants.EXT_DEF:
|
|
357
|
+
# HACK: extenral attributes can lead to infinite loops
|
|
358
|
+
# Identify them here
|
|
359
|
+
if node.attr in name:
|
|
360
|
+
continue
|
|
361
|
+
ext_name = utils.join_ns(name, node.attr)
|
|
362
|
+
if not self.def_manager.get(ext_name):
|
|
363
|
+
self.def_manager.create(ext_name, utils.constants.EXT_DEF)
|
|
364
|
+
names.add(ext_name)
|
|
365
|
+
return names
|
|
366
|
+
|
|
367
|
+
def iterate_call_args(self, defi, node):
|
|
368
|
+
for pos, arg in enumerate(node.args):
|
|
369
|
+
self.visit(arg)
|
|
370
|
+
decoded = self.decode_node(arg)
|
|
371
|
+
if defi.is_function_def():
|
|
372
|
+
pos_arg_names = defi.get_name_pointer().get_pos_arg(pos)
|
|
373
|
+
# if arguments for this position exist update their namespace
|
|
374
|
+
if not pos_arg_names:
|
|
375
|
+
continue
|
|
376
|
+
for name in pos_arg_names:
|
|
377
|
+
arg_def = self.def_manager.get(name)
|
|
378
|
+
if not arg_def:
|
|
379
|
+
continue
|
|
380
|
+
for d in decoded:
|
|
381
|
+
if isinstance(d, Definition):
|
|
382
|
+
arg_def.get_name_pointer().add(d.get_ns())
|
|
383
|
+
else:
|
|
384
|
+
arg_def.get_lit_pointer().add(d)
|
|
385
|
+
else:
|
|
386
|
+
for d in decoded:
|
|
387
|
+
if isinstance(d, Definition):
|
|
388
|
+
defi.get_name_pointer().add_pos_arg(pos, None, d.get_ns())
|
|
389
|
+
else:
|
|
390
|
+
defi.get_name_pointer().add_pos_lit_arg(pos, None, d)
|
|
391
|
+
|
|
392
|
+
for keyword in node.keywords:
|
|
393
|
+
self.visit(keyword.value)
|
|
394
|
+
decoded = self.decode_node(keyword.value)
|
|
395
|
+
if defi.is_function_def():
|
|
396
|
+
arg_names = defi.get_name_pointer().get_arg(keyword.arg)
|
|
397
|
+
if not arg_names:
|
|
398
|
+
continue
|
|
399
|
+
for name in arg_names:
|
|
400
|
+
arg_def = self.def_manager.get(name)
|
|
401
|
+
if not arg_def:
|
|
402
|
+
continue
|
|
403
|
+
for d in decoded:
|
|
404
|
+
if isinstance(d, Definition):
|
|
405
|
+
arg_def.get_name_pointer().add(d.get_ns())
|
|
406
|
+
else:
|
|
407
|
+
arg_def.get_lit_pointer().add(d)
|
|
408
|
+
else:
|
|
409
|
+
for d in decoded:
|
|
410
|
+
if isinstance(d, Definition):
|
|
411
|
+
defi.get_name_pointer().add_arg(keyword.arg, d.get_ns())
|
|
412
|
+
else:
|
|
413
|
+
defi.get_name_pointer().add_lit_arg(keyword.arg, d)
|
|
414
|
+
|
|
415
|
+
def retrieve_subscript_names(self, node):
|
|
416
|
+
if not isinstance(node, ast.Subscript):
|
|
417
|
+
raise Exception("The node is not an subcript")
|
|
418
|
+
|
|
419
|
+
if not getattr(self, "closured", None):
|
|
420
|
+
return set()
|
|
421
|
+
|
|
422
|
+
if getattr(node.slice, "value", None) and self._is_literal(node.slice.value):
|
|
423
|
+
sl_names = [node.slice.value]
|
|
424
|
+
else:
|
|
425
|
+
sl_names = self.decode_node(node.slice)
|
|
426
|
+
|
|
427
|
+
val_names = self.decode_node(node.value)
|
|
428
|
+
|
|
429
|
+
decoded_vals = set()
|
|
430
|
+
keys = set()
|
|
431
|
+
full_names = set()
|
|
432
|
+
# get all names associated with this variable name
|
|
433
|
+
for n in val_names:
|
|
434
|
+
if n and isinstance(n, Definition) and self.closured.get(n.get_ns(), None):
|
|
435
|
+
decoded_vals |= self.closured.get(n.get_ns())
|
|
436
|
+
for s in sl_names:
|
|
437
|
+
if isinstance(s, Definition) and self.closured.get(s.get_ns(), None):
|
|
438
|
+
# we care about the literals pointed by the name
|
|
439
|
+
# not the namespaces, so retrieve the literals pointed
|
|
440
|
+
for name in self.closured.get(s.get_ns()):
|
|
441
|
+
defi = self.def_manager.get(name)
|
|
442
|
+
if not defi:
|
|
443
|
+
continue
|
|
444
|
+
keys |= defi.get_lit_pointer().get()
|
|
445
|
+
elif isinstance(s, str):
|
|
446
|
+
keys.add(s)
|
|
447
|
+
elif isinstance(s, int):
|
|
448
|
+
keys.add(utils.get_int_name(s))
|
|
449
|
+
|
|
450
|
+
for d in decoded_vals:
|
|
451
|
+
for key in keys:
|
|
452
|
+
# check for existence of var name and key combination
|
|
453
|
+
str_key = str(key)
|
|
454
|
+
if isinstance(key, int):
|
|
455
|
+
str_key = utils.get_int_name(key)
|
|
456
|
+
full_ns = utils.join_ns(d, str_key)
|
|
457
|
+
full_names.add(full_ns)
|
|
458
|
+
|
|
459
|
+
return full_names
|
|
460
|
+
|
|
461
|
+
def retrieve_call_names(self, node):
|
|
462
|
+
names = set()
|
|
463
|
+
if isinstance(node.func, ast.Name):
|
|
464
|
+
defi = self.scope_manager.get_def(self.current_ns, node.func.id)
|
|
465
|
+
if defi:
|
|
466
|
+
names = self.closured.get(defi.get_ns(), None)
|
|
467
|
+
elif isinstance(node.func, ast.Call) and self.last_called_names:
|
|
468
|
+
for name in self.last_called_names:
|
|
469
|
+
return_ns = utils.join_ns(name, utils.constants.RETURN_NAME)
|
|
470
|
+
returns = self.closured.get(return_ns)
|
|
471
|
+
if not returns:
|
|
472
|
+
continue
|
|
473
|
+
for ret in returns:
|
|
474
|
+
defi = self.def_manager.get(ret)
|
|
475
|
+
names.add(defi.get_ns())
|
|
476
|
+
elif isinstance(node.func, ast.Attribute):
|
|
477
|
+
names = self._retrieve_attribute_names(node.func)
|
|
478
|
+
elif isinstance(node.func, ast.Subscript):
|
|
479
|
+
# Calls can be performed only on single indices, not ranges
|
|
480
|
+
full_names = self.retrieve_subscript_names(node.func)
|
|
481
|
+
for n in full_names:
|
|
482
|
+
if self.closured.get(n, None):
|
|
483
|
+
names |= self.closured.get(n)
|
|
484
|
+
|
|
485
|
+
return names
|
|
486
|
+
|
|
487
|
+
def analyze_submodules(self, cls, *args, **kwargs):
|
|
488
|
+
imports = self.import_manager.get_imports(self.modname)
|
|
489
|
+
|
|
490
|
+
for imp in imports:
|
|
491
|
+
self.analyze_submodule(cls, imp, *args, **kwargs)
|
|
492
|
+
|
|
493
|
+
def analyze_submodule(self, cls, imp, *args, **kwargs):
|
|
494
|
+
if imp in self.get_modules_analyzed():
|
|
495
|
+
return
|
|
496
|
+
|
|
497
|
+
fname = self.import_manager.get_filepath(imp)
|
|
498
|
+
|
|
499
|
+
if (
|
|
500
|
+
not fname
|
|
501
|
+
or not fname.endswith(".py")
|
|
502
|
+
or self.import_manager.get_mod_dir() not in fname
|
|
503
|
+
):
|
|
504
|
+
return
|
|
505
|
+
|
|
506
|
+
self.import_manager.set_current_mod(imp, fname)
|
|
507
|
+
|
|
508
|
+
visitor = cls(fname, imp, *args, **kwargs)
|
|
509
|
+
visitor.analyze()
|
|
510
|
+
self.merge_modules_analyzed(visitor.get_modules_analyzed())
|
|
511
|
+
|
|
512
|
+
self.import_manager.set_current_mod(self.modname, self.filename)
|
|
513
|
+
|
|
514
|
+
def find_cls_fun_ns(self, cls_name, fn):
|
|
515
|
+
cls = self.class_manager.get(cls_name)
|
|
516
|
+
if not cls:
|
|
517
|
+
return set()
|
|
518
|
+
|
|
519
|
+
ext_names = set()
|
|
520
|
+
for item in cls.get_mro():
|
|
521
|
+
ns = utils.join_ns(item, fn)
|
|
522
|
+
names = set()
|
|
523
|
+
if getattr(self, "closured", None) and self.closured.get(ns, None):
|
|
524
|
+
names = self.closured[ns]
|
|
525
|
+
else:
|
|
526
|
+
names.add(ns)
|
|
527
|
+
|
|
528
|
+
if self.def_manager.get(ns):
|
|
529
|
+
return names
|
|
530
|
+
|
|
531
|
+
parent = self.def_manager.get(item)
|
|
532
|
+
if parent and parent.get_type() == utils.constants.EXT_DEF:
|
|
533
|
+
ext_names.add(ns)
|
|
534
|
+
|
|
535
|
+
for name in ext_names:
|
|
536
|
+
self.def_manager.create(name, utils.constants.EXT_DEF)
|
|
537
|
+
self.add_ext_mod_node(name)
|
|
538
|
+
return ext_names
|
|
539
|
+
|
|
540
|
+
def add_ext_mod_node(self, name):
|
|
541
|
+
ext_modname = name.split(".")[0]
|
|
542
|
+
ext_mod = self.module_manager.get(ext_modname)
|
|
543
|
+
if not ext_mod:
|
|
544
|
+
ext_mod = self.module_manager.create(ext_modname, None, external=True)
|
|
545
|
+
ext_mod.add_method(ext_modname)
|
|
546
|
+
|
|
547
|
+
ext_mod.add_method(name)
|