PyCG 0.0.3__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.
Files changed (62) hide show
  1. micro-benchmark/__init__.py +0 -0
  2. micro-benchmark/args_test.py +25 -0
  3. micro-benchmark/assignments_test.py +19 -0
  4. micro-benchmark/base.py +87 -0
  5. micro-benchmark/builtins_test.py +16 -0
  6. micro-benchmark/classes_test.py +73 -0
  7. micro-benchmark/create_pytests.py +42 -0
  8. micro-benchmark/decorators_test.py +28 -0
  9. micro-benchmark/dicts_test.py +43 -0
  10. micro-benchmark/direct_calls_test.py +19 -0
  11. micro-benchmark/dynamic_test.py +10 -0
  12. micro-benchmark/exceptions_test.py +16 -0
  13. micro-benchmark/external_test.py +25 -0
  14. micro-benchmark/functions_test.py +19 -0
  15. micro-benchmark/generators_test.py +25 -0
  16. micro-benchmark/imports_test.py +49 -0
  17. micro-benchmark/kwargs_test.py +16 -0
  18. micro-benchmark/lambdas_test.py +22 -0
  19. micro-benchmark/lists_test.py +31 -0
  20. micro-benchmark/mro_test.py +28 -0
  21. micro-benchmark/returns_test.py +19 -0
  22. micro-benchmark-key-errs/__init__.py +0 -0
  23. micro-benchmark-key-errs/assignments_test.py +16 -0
  24. micro-benchmark-key-errs/base.py +81 -0
  25. micro-benchmark-key-errs/classes_test.py +25 -0
  26. micro-benchmark-key-errs/create_pytests.py +42 -0
  27. micro-benchmark-key-errs/dicts_test.py +34 -0
  28. micro-benchmark-key-errs/kwargs_test.py +13 -0
  29. micro-benchmark-key-errs/lists_test.py +13 -0
  30. micro-benchmark-key-errs/parameters_test.py +16 -0
  31. pycg/__init__.py +0 -0
  32. pycg/__main__.py +104 -0
  33. pycg/formats/__init__.py +23 -0
  34. pycg/formats/as_graph.py +32 -0
  35. pycg/formats/base.py +23 -0
  36. pycg/formats/fasten.py +253 -0
  37. pycg/formats/simple.py +32 -0
  38. pycg/machinery/__init__.py +0 -0
  39. pycg/machinery/callgraph.py +59 -0
  40. pycg/machinery/classes.py +77 -0
  41. pycg/machinery/definitions.py +219 -0
  42. pycg/machinery/imports.py +220 -0
  43. pycg/machinery/key_err.py +34 -0
  44. pycg/machinery/modules.py +66 -0
  45. pycg/machinery/pointers.py +146 -0
  46. pycg/machinery/scopes.py +143 -0
  47. pycg/processing/__init__.py +0 -0
  48. pycg/processing/base.py +522 -0
  49. pycg/processing/cgprocessor.py +230 -0
  50. pycg/processing/keyerrprocessor.py +82 -0
  51. pycg/processing/postprocessor.py +308 -0
  52. pycg/processing/preprocessor.py +375 -0
  53. pycg/pycg.py +229 -0
  54. pycg/utils/__init__.py +22 -0
  55. pycg/utils/common.py +39 -0
  56. pycg/utils/constants.py +42 -0
  57. pycg-0.0.3.dist-info/LICENCE +201 -0
  58. pycg-0.0.3.dist-info/METADATA +150 -0
  59. pycg-0.0.3.dist-info/RECORD +62 -0
  60. pycg-0.0.3.dist-info/WHEEL +5 -0
  61. pycg-0.0.3.dist-info/entry_points.txt +3 -0
  62. pycg-0.0.3.dist-info/top_level.txt +3 -0
@@ -0,0 +1,230 @@
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 os
22
+ import ast
23
+
24
+ from pycg import utils
25
+ from pycg.processing.base import ProcessingBase
26
+ from pycg.machinery.callgraph import CallGraph
27
+ from pycg.machinery.definitions import Definition
28
+
29
+ class CallGraphProcessor(ProcessingBase):
30
+ def __init__(self, filename, modname, import_manager,
31
+ scope_manager, def_manager, class_manager,
32
+ module_manager, call_graph=None, modules_analyzed=None):
33
+ super().__init__(filename, modname, modules_analyzed)
34
+ # parent directory of file
35
+ self.parent_dir = os.path.dirname(filename)
36
+
37
+ self.import_manager = import_manager
38
+ self.scope_manager = scope_manager
39
+ self.def_manager = def_manager
40
+ self.class_manager = class_manager
41
+ self.module_manager = module_manager
42
+
43
+ self.call_graph = call_graph
44
+
45
+ self.closured = self.def_manager.transitive_closure()
46
+
47
+ def visit_Module(self, node):
48
+ self.call_graph.add_node(self.modname, self.modname)
49
+ super().visit_Module(node)
50
+
51
+ def visit_For(self, node):
52
+ self.visit(node.iter)
53
+ self.visit(node.target)
54
+ # assign target.id to the return value of __next__ of node.iter.it
55
+ # we need to have a visit for on the postprocessor also
56
+ iter_decoded = self.decode_node(node.iter)
57
+ for item in iter_decoded:
58
+ if not isinstance(item, Definition):
59
+ continue
60
+ names = self.closured.get(item.get_ns(), [])
61
+ for name in names:
62
+ iter_ns = utils.join_ns(name, utils.constants.ITER_METHOD)
63
+ next_ns = utils.join_ns(name, utils.constants.NEXT_METHOD)
64
+ if self.def_manager.get(iter_ns):
65
+ self.call_graph.add_edge(self.current_method, iter_ns)
66
+ if self.def_manager.get(next_ns):
67
+ self.call_graph.add_edge(self.current_method, next_ns)
68
+
69
+ super().visit_For(node)
70
+
71
+ def visit_Lambda(self, node):
72
+ counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
73
+ lambda_name = utils.get_lambda_name(counter)
74
+ lambda_fullns = utils.join_ns(self.current_ns, lambda_name)
75
+
76
+ self.call_graph.add_node(lambda_fullns, self.modname)
77
+
78
+ super().visit_Lambda(node, lambda_name)
79
+
80
+ def visit_Raise(self, node):
81
+ if not node.exc:
82
+ return
83
+ self.visit(node.exc)
84
+ decoded = self.decode_node(node.exc)
85
+ for d in decoded:
86
+ if not isinstance(d, Definition):
87
+ continue
88
+ names = self.closured.get(d.get_ns(), [])
89
+ for name in names:
90
+ pointer_def = self.def_manager.get(name)
91
+ if pointer_def.get_type() == utils.constants.CLS_DEF:
92
+ init_ns = self.find_cls_fun_ns(name, utils.constants.CLS_INIT)
93
+ for ns in init_ns:
94
+ self.call_graph.add_edge(self.current_method, ns)
95
+ if pointer_def.get_type() == utils.constants.EXT_DEF:
96
+ self.call_graph.add_edge(self.current_method, name)
97
+
98
+ def visit_AsyncFunctionDef(self, node):
99
+ self.visit_FunctionDef(node)
100
+
101
+ def visit_FunctionDef(self, node):
102
+ for decorator in node.decorator_list:
103
+ self.visit(decorator)
104
+ decoded = self.decode_node(decorator)
105
+ for d in decoded:
106
+ if not isinstance(d, Definition):
107
+ continue
108
+ names = self.closured.get(d.get_ns(), [])
109
+ for name in names:
110
+ self.call_graph.add_edge(self.current_method, name)
111
+
112
+ self.call_graph.add_node(utils.join_ns(self.current_ns, node.name), self.modname)
113
+ super().visit_FunctionDef(node)
114
+
115
+ def visit_Call(self, node):
116
+ def create_ext_edge(name, ext_modname):
117
+ self.add_ext_mod_node(name)
118
+ self.call_graph.add_node(name, ext_modname)
119
+ self.call_graph.add_edge(self.current_method, name)
120
+
121
+ # First visit the child function so that on the case of
122
+ # func()()()
123
+ # we first visit the call to func and then the other calls
124
+ for arg in node.args:
125
+ self.visit(arg)
126
+
127
+ for keyword in node.keywords:
128
+ self.visit(keyword.value)
129
+
130
+ self.visit(node.func)
131
+
132
+ names = self.retrieve_call_names(node)
133
+ if not names:
134
+ if isinstance(node.func, ast.Attribute) and self.has_ext_parent(node.func):
135
+ # TODO: This doesn't work for cases where there is an assignment of an attribute
136
+ # i.e. import os; lala = os.path; lala.dirname()
137
+ for name in self.get_full_attr_names(node.func):
138
+ ext_modname = name.split(".")[0]
139
+ create_ext_edge(name, ext_modname)
140
+ elif getattr(node.func, "id", None) and self.is_builtin(node.func.id):
141
+ name = utils.join_ns(utils.constants.BUILTIN_NAME, node.func.id)
142
+ create_ext_edge(name, utils.constants.BUILTIN_NAME)
143
+ return
144
+
145
+ self.last_called_names = names
146
+ for pointer in names:
147
+ pointer_def = self.def_manager.get(pointer)
148
+ if not pointer_def or not isinstance(pointer_def, Definition):
149
+ continue
150
+ if pointer_def.is_callable():
151
+ if pointer_def.get_type() == utils.constants.EXT_DEF:
152
+ ext_modname = pointer.split(".")[0]
153
+ create_ext_edge(pointer, ext_modname)
154
+ continue
155
+ self.call_graph.add_edge(self.current_method, pointer)
156
+
157
+ # TODO: This doesn't work and leads to calls from the decorators
158
+ # themselves to the function, creating edges to the first decorator
159
+ #for decorator in pointer_def.decorator_names:
160
+ # dec_names = self.closured.get(decorator, [])
161
+ # for dec_name in dec_names:
162
+ # if self.def_manager.get(dec_name).get_type() == utils.constants.FUN_DEF:
163
+ # self.call_graph.add_edge(self.current_ns, dec_name)
164
+
165
+ if pointer_def.get_type() == utils.constants.CLS_DEF:
166
+ init_ns = self.find_cls_fun_ns(pointer, utils.constants.CLS_INIT)
167
+
168
+ for ns in init_ns:
169
+ self.call_graph.add_edge(self.current_method, ns)
170
+
171
+ def analyze_submodules(self):
172
+ super().analyze_submodules(CallGraphProcessor, self.import_manager,
173
+ self.scope_manager, self.def_manager, self.class_manager, self.module_manager,
174
+ call_graph=self.call_graph, modules_analyzed=self.get_modules_analyzed())
175
+
176
+ def analyze(self):
177
+ self.visit(ast.parse(self.contents, self.filename))
178
+ self.analyze_submodules()
179
+
180
+ def get_all_reachable_functions(self):
181
+ reachable = set()
182
+ names = set()
183
+ current_scope = self.scope_manager.get_scope(self.current_ns)
184
+ while current_scope:
185
+ for name, defi in current_scope.get_defs().items():
186
+ if defi.is_function_def() and not name in names:
187
+ closured = self.closured.get(defi.get_ns())
188
+ for item in closured:
189
+ reachable.add(item)
190
+ names.add(name)
191
+ current_scope = current_scope.parent
192
+
193
+ return reachable
194
+
195
+ def has_ext_parent(self, node):
196
+ if not isinstance(node, ast.Attribute):
197
+ return False
198
+
199
+ while isinstance(node, ast.Attribute):
200
+ parents = self._retrieve_parent_names(node)
201
+ for parent in parents:
202
+ for name in self.closured.get(parent, []):
203
+ defi = self.def_manager.get(name)
204
+ if defi and defi.is_ext_def():
205
+ return True
206
+ node = node.value
207
+ return False
208
+
209
+ def get_full_attr_names(self, node):
210
+ name = ""
211
+ while isinstance(node, ast.Attribute):
212
+ if not name:
213
+ name = node.attr
214
+ else:
215
+ name = node.attr + "." + name
216
+ node = node.value
217
+
218
+ names = []
219
+ if getattr(node, "id", None) == None:
220
+ return names
221
+
222
+ defi = self.scope_manager.get_def(self.current_ns, node.id)
223
+ if defi and self.closured.get(defi.get_ns()):
224
+ for id in self.closured.get(defi.get_ns()):
225
+ names.append(id + "." + name)
226
+
227
+ return names
228
+
229
+ def is_builtin(self, name):
230
+ return name in __builtins__
@@ -0,0 +1,82 @@
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 os
22
+ import ast
23
+ import re
24
+
25
+ from pycg import utils
26
+ from pycg.processing.base import ProcessingBase
27
+
28
+ class KeyErrProcessor(ProcessingBase):
29
+ def __init__(self, filename, modname, import_manager,
30
+ scope_manager, def_manager, class_manager, key_errs, modules_analyzed=None):
31
+ super().__init__(filename, modname, modules_analyzed)
32
+ # parent directory of file
33
+ self.parent_dir = os.path.dirname(filename)
34
+
35
+ self.import_manager = import_manager
36
+ self.scope_manager = scope_manager
37
+ self.def_manager = def_manager
38
+ self.class_manager = class_manager
39
+ self.key_errs = key_errs
40
+
41
+ self.closured = self.def_manager.transitive_closure()
42
+ self.state = "keyerr"
43
+
44
+ def visit_Subscript(self, node):
45
+ self.visit(node.value)
46
+ self.visit(node.slice)
47
+ names = self.retrieve_subscript_names(node)
48
+ for name in names:
49
+ if not self.is_subscriptable(name):
50
+ continue
51
+
52
+ defi = self.def_manager.get(name)
53
+ if not defi:
54
+ splitted = name.split(".")
55
+
56
+ self.key_errs.add(
57
+ filename=os.path.relpath(self.filename, self.import_manager.get_mod_dir()),
58
+ lineno=node.lineno,
59
+ namespace=".".join(splitted[:-1]),
60
+ key=splitted[-1])
61
+
62
+ def is_subscriptable(self, name):
63
+ if re.match(r".*<dict[0-9]+>.*", name):
64
+ return True
65
+
66
+ return False
67
+
68
+ def analyze_submodules(self):
69
+ super().analyze_submodules(KeyErrProcessor, self.import_manager,
70
+ self.scope_manager, self.def_manager, self.class_manager,
71
+ self.key_errs, modules_analyzed=self.get_modules_analyzed())
72
+
73
+ def analyze(self):
74
+ self.visit(ast.parse(self.contents, self.filename))
75
+ self.analyze_submodules()
76
+
77
+ def visit_Lambda(self, node):
78
+ counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
79
+ lambda_name = utils.get_lambda_name(counter)
80
+ lambda_fullns = utils.join_ns(self.current_ns, lambda_name)
81
+
82
+ super().visit_Lambda(node, lambda_name)
@@ -0,0 +1,308 @@
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.processing.base import ProcessingBase
24
+ from pycg.machinery.definitions import Definition
25
+ from pycg import utils
26
+
27
+ class PostProcessor(ProcessingBase):
28
+ def __init__(self, input_file, modname, import_manager,
29
+ scope_manager, def_manager, class_manager, module_manager, modules_analyzed=None):
30
+ super().__init__(input_file, modname, modules_analyzed)
31
+ self.import_manager = import_manager
32
+ self.scope_manager = scope_manager
33
+ self.def_manager = def_manager
34
+ self.class_manager = class_manager
35
+ self.module_manager = module_manager
36
+ self.closured = self.def_manager.transitive_closure()
37
+
38
+ def visit_Lambda(self, node):
39
+ counter = self.scope_manager.get_scope(self.current_ns).inc_lambda_counter()
40
+ lambda_name = utils.get_lambda_name(counter)
41
+ super().visit_Lambda(node, lambda_name)
42
+
43
+ def visit_Call(self, node):
44
+ self.visit(node.func)
45
+
46
+ names = self.retrieve_call_names(node)
47
+ if not names:
48
+ return
49
+
50
+ self.last_called_names = names
51
+
52
+ for name in names:
53
+ defi = self.def_manager.get(name)
54
+ if not defi:
55
+ continue
56
+ if defi.get_type() == utils.constants.CLS_DEF:
57
+ self.update_parent_classes(defi)
58
+ defi = self.def_manager.get(utils.join_ns(defi.get_ns(), utils.constants.CLS_INIT))
59
+ if not defi:
60
+ continue
61
+ self.iterate_call_args(defi, node)
62
+
63
+ def visit_Assign(self, node):
64
+ self._visit_assign(node.value, node.targets)
65
+
66
+ def visit_Return(self, node):
67
+ self._visit_return(node)
68
+
69
+ def visit_Yield(self, node):
70
+ self._visit_return(node)
71
+
72
+ def visit_For(self, node):
73
+ # only handle name targets
74
+ if isinstance(node.target, ast.Name):
75
+ target_def = self.def_manager.get(utils.join_ns(self.current_ns, node.target.id))
76
+ # if the target definition exists
77
+ if target_def:
78
+ iter_decoded = self.decode_node(node.iter)
79
+ # assign the target to the return value
80
+ # of the next function
81
+ for item in iter_decoded:
82
+ if not isinstance(item, Definition):
83
+ continue
84
+ # return value for generators
85
+ for name in self.closured.get(item.get_ns(), []):
86
+ # If there exists a next method on the iterable
87
+ # and if yes, add a pointer to it
88
+ next_defi = self.def_manager.get(utils.join_ns(name,
89
+ utils.constants.NEXT_METHOD, utils.constants.RETURN_NAME))
90
+ if next_defi:
91
+ for name in self.closured.get(next_defi.get_ns(), []):
92
+ target_def.get_name_pointer().add(name)
93
+ else: # otherwise, add a pointer to the name (e.g. a yield)
94
+ target_def.get_name_pointer().add(name)
95
+
96
+ super().visit_For(node)
97
+
98
+ def visit_Return(self, node):
99
+ self._visit_return(node)
100
+
101
+ def visit_Yield(self, node):
102
+ self._visit_return(node)
103
+
104
+ def visit_AsyncFunctionDef(self, node):
105
+ self.visit_FunctionDef(node)
106
+
107
+ def visit_FunctionDef(self, node):
108
+ # here we iterate decorators
109
+ if node.decorator_list:
110
+ fn_def = self.def_manager.get(utils.join_ns(self.current_ns, node.name))
111
+ reversed_decorators = list(reversed(node.decorator_list))
112
+
113
+ # add to the name pointer of the function definition
114
+ # the return value of the first decorator
115
+ # since, now the function is a namespace to that point
116
+ if hasattr(fn_def, "decorator_names") and reversed_decorators:
117
+ last_decoded = self.decode_node(reversed_decorators[-1])
118
+ for d in last_decoded:
119
+ if not isinstance(d, Definition):
120
+ continue
121
+ fn_def.decorator_names.add(utils.join_ns(d.get_ns(), utils.constants.RETURN_NAME))
122
+
123
+ previous_names = self.closured.get(fn_def.get_ns(), set())
124
+ for decorator in reversed_decorators:
125
+ # assign the previous_def as the first parameter of the decorator
126
+ decoded = self.decode_node(decorator)
127
+ new_previous_names = set()
128
+ for d in decoded:
129
+ if not isinstance(d, Definition):
130
+ continue
131
+ for name in self.closured.get(d.get_ns(), []):
132
+ return_ns = utils.join_ns(name, utils.constants.RETURN_NAME)
133
+
134
+ if self.closured.get(return_ns, None) == None:
135
+ continue
136
+
137
+ new_previous_names = new_previous_names.union(self.closured.get(return_ns))
138
+
139
+ for prev_name in previous_names:
140
+ pos_arg_names = d.get_name_pointer().get_pos_arg(0)
141
+ if not pos_arg_names:
142
+ continue
143
+ for name in pos_arg_names:
144
+ arg_def = self.def_manager.get(name)
145
+ arg_def.get_name_pointer().add(prev_name)
146
+ previous_names = new_previous_names
147
+
148
+ super().visit_FunctionDef(node)
149
+
150
+ def visit_ClassDef(self, node):
151
+ # create a definition for the class (node.name)
152
+ cls_def = self.def_manager.handle_class_def(self.current_ns, node.name)
153
+
154
+ # iterate bases to compute MRO for the class
155
+ cls = self.class_manager.get(cls_def.get_ns())
156
+ if not cls:
157
+ cls = self.class_manager.create(cls_def.get_ns(), self.modname)
158
+
159
+ cls.clear_mro()
160
+ for base in node.bases:
161
+ # all bases are of the type ast.Name
162
+ self.visit(base)
163
+
164
+ bases = self.decode_node(base)
165
+ for base_def in bases:
166
+ if not isinstance(base_def, Definition):
167
+ continue
168
+ names = set()
169
+ if base_def.get_name_pointer().get():
170
+ names = base_def.get_name_pointer().get()
171
+ else:
172
+ names.add(base_def.get_ns())
173
+ for name in names:
174
+ # add the base as a parent
175
+ cls.add_parent(name)
176
+
177
+ # add the base's parents
178
+ parent_cls = self.class_manager.get(name)
179
+ if parent_cls:
180
+ cls.add_parent(parent_cls.get_mro())
181
+
182
+ cls.compute_mro()
183
+
184
+ super().visit_ClassDef(node)
185
+
186
+ def visit_List(self, node):
187
+ # Works similarly with dicts
188
+ current_scope = self.scope_manager.get_scope(self.current_ns)
189
+ list_counter = current_scope.inc_list_counter()
190
+ list_name = utils.get_list_name(list_counter)
191
+ list_full_ns = utils.join_ns(self.current_ns, list_name)
192
+
193
+ # create a scope for the list
194
+ list_scope = self.scope_manager.create_scope(list_full_ns, current_scope)
195
+
196
+ # create a list definition
197
+ list_def = self.def_manager.get(list_full_ns)
198
+ if not list_def:
199
+ list_def = self.def_manager.create(list_full_ns, utils.constants.NAME_DEF)
200
+ current_scope.add_def(list_name, list_def)
201
+
202
+ self.name_stack.append(list_name)
203
+ for idx, elt in enumerate(node.elts):
204
+ self.visit(elt)
205
+ key_full_ns = utils.join_ns(list_def.get_ns(), utils.get_int_name(idx))
206
+ key_def = self.def_manager.get(key_full_ns)
207
+ if not key_def:
208
+ key_def = self.def_manager.create(key_full_ns, utils.constants.NAME_DEF)
209
+
210
+ decoded_elt = self.decode_node(elt)
211
+ for v in decoded_elt:
212
+ if isinstance(v, Definition):
213
+ key_def.get_name_pointer().add(v.get_ns())
214
+ else:
215
+ key_def.get_lit_pointer().add(v)
216
+
217
+ self.name_stack.pop()
218
+
219
+ def visit_Dict(self, node):
220
+ # 1. create a scope using a counter
221
+ # 2. Iterate keys and add them as children of the scope
222
+ # 3. Iterate values and makes a points to connection with the keys
223
+ current_scope = self.scope_manager.get_scope(self.current_ns)
224
+ dict_counter = current_scope.inc_dict_counter()
225
+ dict_name = utils.get_dict_name(dict_counter)
226
+ dict_full_ns = utils.join_ns(self.current_ns, dict_name)
227
+
228
+ # create a scope for the dict
229
+ dict_scope = self.scope_manager.create_scope(dict_full_ns, current_scope)
230
+
231
+ # Create a dict definition
232
+ dict_def = self.def_manager.get(dict_full_ns)
233
+ if not dict_def:
234
+ dict_def = self.def_manager.create(dict_full_ns, utils.constants.NAME_DEF)
235
+ # add it to the current scope
236
+ current_scope.add_def(dict_name, dict_def)
237
+
238
+ self.name_stack.append(dict_name)
239
+ for key, value in zip(node.keys, node.values):
240
+ if key:
241
+ self.visit(key)
242
+ if value:
243
+ self.visit(value)
244
+ decoded_key = self.decode_node(key)
245
+ decoded_value = self.decode_node(value)
246
+
247
+ # iterate decoded keys and values
248
+ # to do the assignment operation
249
+ for k in decoded_key:
250
+ if isinstance(k, Definition):
251
+ # get literal pointer
252
+ names = k.get_lit_pointer().get()
253
+ else:
254
+ names = set()
255
+ if isinstance(k, list):
256
+ continue
257
+ names.add(k)
258
+ for name in names:
259
+ # create a definition for the key
260
+ if isinstance(name, int):
261
+ name = utils.get_int_name(name)
262
+ key_full_ns = utils.join_ns(dict_def.get_ns(), str(name))
263
+ key_def = self.def_manager.get(key_full_ns)
264
+ if not key_def:
265
+ key_def = self.def_manager.create(key_full_ns, utils.constants.NAME_DEF)
266
+ dict_scope.add_def(str(name), key_def)
267
+ for v in decoded_value:
268
+ if isinstance(v, Definition):
269
+ key_def.get_name_pointer().add(v.get_ns())
270
+ else:
271
+ key_def.get_lit_pointer().add(v)
272
+ self.name_stack.pop()
273
+
274
+ def update_parent_classes(self, defi):
275
+ cls = self.class_manager.get(defi.get_ns())
276
+ if not cls:
277
+ return
278
+ current_scope = self.scope_manager.get_scope(defi.get_ns())
279
+ for parent in cls.get_mro():
280
+ parent_def = self.def_manager.get(parent)
281
+ if not parent_def:
282
+ continue
283
+ parent_scope = self.scope_manager.get_scope(parent)
284
+ if not parent_scope:
285
+ continue
286
+ parent_items = list(parent_scope.get_defs().keys())
287
+ for key, child_def in current_scope.get_defs().items():
288
+ if key == "__init__":
289
+ continue
290
+ # resolve name from the parent_def
291
+ names = self.find_cls_fun_ns(parent_def.get_ns(), key)
292
+
293
+ new_ns = utils.join_ns(parent_def.get_ns(), key)
294
+ new_def = self.def_manager.get(new_ns)
295
+ if not new_def:
296
+ new_def = self.def_manager.create(new_ns, utils.constants.NAME_DEF)
297
+
298
+ new_def.get_name_pointer().add_set(names)
299
+ new_def.get_name_pointer().add(child_def.get_ns())
300
+
301
+ def analyze_submodules(self):
302
+ super().analyze_submodules(PostProcessor, self.import_manager,
303
+ self.scope_manager, self.def_manager, self.class_manager,
304
+ self.module_manager, modules_analyzed=self.get_modules_analyzed())
305
+
306
+ def analyze(self):
307
+ self.visit(ast.parse(self.contents, self.filename))
308
+ self.analyze_submodules()