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.
- micro-benchmark/__init__.py +0 -0
- micro-benchmark/args_test.py +25 -0
- micro-benchmark/assignments_test.py +19 -0
- micro-benchmark/base.py +87 -0
- micro-benchmark/builtins_test.py +16 -0
- micro-benchmark/classes_test.py +73 -0
- micro-benchmark/create_pytests.py +42 -0
- micro-benchmark/decorators_test.py +28 -0
- micro-benchmark/dicts_test.py +43 -0
- micro-benchmark/direct_calls_test.py +19 -0
- micro-benchmark/dynamic_test.py +10 -0
- micro-benchmark/exceptions_test.py +16 -0
- micro-benchmark/external_test.py +25 -0
- micro-benchmark/functions_test.py +19 -0
- micro-benchmark/generators_test.py +25 -0
- micro-benchmark/imports_test.py +49 -0
- micro-benchmark/kwargs_test.py +16 -0
- micro-benchmark/lambdas_test.py +22 -0
- micro-benchmark/lists_test.py +31 -0
- micro-benchmark/mro_test.py +28 -0
- micro-benchmark/returns_test.py +19 -0
- micro-benchmark-key-errs/__init__.py +0 -0
- micro-benchmark-key-errs/assignments_test.py +16 -0
- micro-benchmark-key-errs/base.py +81 -0
- micro-benchmark-key-errs/classes_test.py +25 -0
- micro-benchmark-key-errs/create_pytests.py +42 -0
- micro-benchmark-key-errs/dicts_test.py +34 -0
- micro-benchmark-key-errs/kwargs_test.py +13 -0
- micro-benchmark-key-errs/lists_test.py +13 -0
- micro-benchmark-key-errs/parameters_test.py +16 -0
- pycg/__init__.py +0 -0
- pycg/__main__.py +104 -0
- pycg/formats/__init__.py +23 -0
- pycg/formats/as_graph.py +32 -0
- pycg/formats/base.py +23 -0
- pycg/formats/fasten.py +253 -0
- pycg/formats/simple.py +32 -0
- pycg/machinery/__init__.py +0 -0
- pycg/machinery/callgraph.py +59 -0
- pycg/machinery/classes.py +77 -0
- pycg/machinery/definitions.py +219 -0
- pycg/machinery/imports.py +220 -0
- pycg/machinery/key_err.py +34 -0
- pycg/machinery/modules.py +66 -0
- pycg/machinery/pointers.py +146 -0
- pycg/machinery/scopes.py +143 -0
- pycg/processing/__init__.py +0 -0
- pycg/processing/base.py +522 -0
- pycg/processing/cgprocessor.py +230 -0
- pycg/processing/keyerrprocessor.py +82 -0
- pycg/processing/postprocessor.py +308 -0
- pycg/processing/preprocessor.py +375 -0
- pycg/pycg.py +229 -0
- pycg/utils/__init__.py +22 -0
- pycg/utils/common.py +39 -0
- pycg/utils/constants.py +42 -0
- pycg-0.0.3.dist-info/LICENCE +201 -0
- pycg-0.0.3.dist-info/METADATA +150 -0
- pycg-0.0.3.dist-info/RECORD +62 -0
- pycg-0.0.3.dist-info/WHEEL +5 -0
- pycg-0.0.3.dist-info/entry_points.txt +3 -0
- pycg-0.0.3.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
class ModuleManager:
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.internal = {}
|
|
24
|
+
self.external = {}
|
|
25
|
+
|
|
26
|
+
def create(self, name, fname, external=False):
|
|
27
|
+
mod = Module(name, fname)
|
|
28
|
+
if external:
|
|
29
|
+
self.external[name] = mod
|
|
30
|
+
else:
|
|
31
|
+
self.internal[name] = mod
|
|
32
|
+
return mod
|
|
33
|
+
|
|
34
|
+
def get(self, name):
|
|
35
|
+
if name in self.internal:
|
|
36
|
+
return self.internal[name]
|
|
37
|
+
if name in self.external:
|
|
38
|
+
return self.external[name]
|
|
39
|
+
|
|
40
|
+
def get_internal_modules(self):
|
|
41
|
+
return self.internal
|
|
42
|
+
|
|
43
|
+
def get_external_modules(self):
|
|
44
|
+
return self.external
|
|
45
|
+
|
|
46
|
+
class Module:
|
|
47
|
+
def __init__(self, name, filename):
|
|
48
|
+
self.name = name
|
|
49
|
+
self.filename = filename
|
|
50
|
+
self.methods = dict()
|
|
51
|
+
|
|
52
|
+
def get_name(self):
|
|
53
|
+
return self.name
|
|
54
|
+
|
|
55
|
+
def get_filename(self):
|
|
56
|
+
return self.filename
|
|
57
|
+
|
|
58
|
+
def get_methods(self):
|
|
59
|
+
return self.methods
|
|
60
|
+
|
|
61
|
+
def add_method(self, method, first=None, last=None):
|
|
62
|
+
if not self.methods.get(method, None):
|
|
63
|
+
self.methods[method] = dict(
|
|
64
|
+
name=method,
|
|
65
|
+
first=first,
|
|
66
|
+
last=last)
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
class Pointer(object):
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.values = set()
|
|
24
|
+
|
|
25
|
+
def add(self, item):
|
|
26
|
+
self.values.add(item)
|
|
27
|
+
|
|
28
|
+
def add_set(self, s):
|
|
29
|
+
self.values = self.values.union(s)
|
|
30
|
+
|
|
31
|
+
def get(self):
|
|
32
|
+
return self.values
|
|
33
|
+
|
|
34
|
+
def merge(self, pointer):
|
|
35
|
+
self.values = self.values.union(pointer.values)
|
|
36
|
+
|
|
37
|
+
class LiteralPointer(Pointer):
|
|
38
|
+
STR_LIT = "STRING"
|
|
39
|
+
INT_LIT = "INTEGER"
|
|
40
|
+
UNK_LIT = "UNKNOWN"
|
|
41
|
+
|
|
42
|
+
# no need to add the actual item
|
|
43
|
+
def add(self, item):
|
|
44
|
+
if isinstance(item, str):
|
|
45
|
+
self.values.add(item)
|
|
46
|
+
elif isinstance(item, int):
|
|
47
|
+
self.values.add(item)
|
|
48
|
+
else:
|
|
49
|
+
self.values.add(self.UNK_LIT)
|
|
50
|
+
|
|
51
|
+
class NamePointer(Pointer):
|
|
52
|
+
def __init__(self):
|
|
53
|
+
super().__init__()
|
|
54
|
+
self.pos_to_name = {}
|
|
55
|
+
self.name_to_pos = {}
|
|
56
|
+
self.args = {}
|
|
57
|
+
|
|
58
|
+
def _sanitize_pos(self, pos):
|
|
59
|
+
try:
|
|
60
|
+
int(pos)
|
|
61
|
+
except ValueError:
|
|
62
|
+
raise PointerError("Invalid position for argument")
|
|
63
|
+
|
|
64
|
+
return pos
|
|
65
|
+
|
|
66
|
+
def get_or_create(self, name):
|
|
67
|
+
if not name in self.args:
|
|
68
|
+
self.args[name] = set()
|
|
69
|
+
return self.args[name]
|
|
70
|
+
|
|
71
|
+
def add_arg(self, name, item):
|
|
72
|
+
arg = self.get_or_create(name)
|
|
73
|
+
if isinstance(item, str):
|
|
74
|
+
self.args[name].add(item)
|
|
75
|
+
elif isinstance(item, set):
|
|
76
|
+
self.args[name] = self.args[name].union(item)
|
|
77
|
+
else:
|
|
78
|
+
raise Exception()
|
|
79
|
+
|
|
80
|
+
def add_lit_arg(self, name, item):
|
|
81
|
+
arg = self.get_or_create(name)
|
|
82
|
+
if isinstance(item, str):
|
|
83
|
+
arg.add(LiteralPointer.STR_LIT)
|
|
84
|
+
elif isinstance(item, int):
|
|
85
|
+
arg.add(LiteralPointer.INT_LIT)
|
|
86
|
+
else:
|
|
87
|
+
arg.add(LiteralPointer.UNK_LIT)
|
|
88
|
+
|
|
89
|
+
def add_pos_arg(self, pos, name, item):
|
|
90
|
+
pos = self._sanitize_pos(pos)
|
|
91
|
+
if not name:
|
|
92
|
+
if self.pos_to_name.get(pos, None):
|
|
93
|
+
name = self.pos_to_name[pos]
|
|
94
|
+
else:
|
|
95
|
+
name = str(pos)
|
|
96
|
+
self.pos_to_name[pos] = name
|
|
97
|
+
self.name_to_pos[name] = pos
|
|
98
|
+
|
|
99
|
+
self.add_arg(name, item)
|
|
100
|
+
|
|
101
|
+
def add_name_arg(self, name, item):
|
|
102
|
+
self.add_arg(name, item)
|
|
103
|
+
|
|
104
|
+
def add_pos_lit_arg(self, pos, name, item):
|
|
105
|
+
pos = self._sanitize_pos(pos)
|
|
106
|
+
if not name:
|
|
107
|
+
name = str(pos)
|
|
108
|
+
self.pos_to_name[pos] = name
|
|
109
|
+
self.name_to_pos[name] = pos
|
|
110
|
+
self.add_lit_arg(name, item)
|
|
111
|
+
|
|
112
|
+
def get_pos_arg(self, pos):
|
|
113
|
+
pos = self._sanitize_pos(pos)
|
|
114
|
+
name = self.pos_to_name.get(pos, None)
|
|
115
|
+
return self.get_arg(name)
|
|
116
|
+
|
|
117
|
+
def get_arg(self, name):
|
|
118
|
+
if self.args.get(name, None):
|
|
119
|
+
return self.args[name]
|
|
120
|
+
|
|
121
|
+
def get_args(self):
|
|
122
|
+
return self.args
|
|
123
|
+
|
|
124
|
+
def get_pos_args(self):
|
|
125
|
+
args = {}
|
|
126
|
+
for pos, name in self.pos_to_name.items():
|
|
127
|
+
args[pos] = self.args[name]
|
|
128
|
+
return args
|
|
129
|
+
|
|
130
|
+
def get_pos_of_name(self, name):
|
|
131
|
+
if name in self.name_to_pos:
|
|
132
|
+
return self.name_to_pos[name]
|
|
133
|
+
|
|
134
|
+
def get_pos_names(self):
|
|
135
|
+
return self.pos_to_name
|
|
136
|
+
|
|
137
|
+
def merge(self, pointer):
|
|
138
|
+
super().merge(pointer)
|
|
139
|
+
if hasattr(pointer, "get_pos_names"):
|
|
140
|
+
for pos, name in pointer.get_pos_names().items():
|
|
141
|
+
self.pos_to_name[pos] = name
|
|
142
|
+
for name, arg in pointer.get_args().items():
|
|
143
|
+
self.add_arg(name, arg)
|
|
144
|
+
|
|
145
|
+
class PointerError(Exception):
|
|
146
|
+
pass
|
pycg/machinery/scopes.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
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
|
+
from pycg import utils
|
|
23
|
+
|
|
24
|
+
class ScopeManager(object):
|
|
25
|
+
"""Manages the scope entries"""
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
self.scopes = {}
|
|
29
|
+
|
|
30
|
+
def handle_module(self, modulename, filename, contents):
|
|
31
|
+
functions = []
|
|
32
|
+
classes = []
|
|
33
|
+
def process(namespace, parent, table):
|
|
34
|
+
name = table.get_name() if table.get_name() != 'top' else ''
|
|
35
|
+
if name:
|
|
36
|
+
fullns = utils.join_ns(namespace, name)
|
|
37
|
+
else:
|
|
38
|
+
fullns = namespace
|
|
39
|
+
|
|
40
|
+
if table.get_type() == "function":
|
|
41
|
+
functions.append(fullns)
|
|
42
|
+
|
|
43
|
+
if table.get_type() == "class":
|
|
44
|
+
classes.append(fullns)
|
|
45
|
+
|
|
46
|
+
sc = self.create_scope(fullns, parent)
|
|
47
|
+
|
|
48
|
+
for t in table.get_children():
|
|
49
|
+
process(fullns, sc, t)
|
|
50
|
+
|
|
51
|
+
process(modulename, None, symtable.symtable(contents, filename, compile_type="exec"))
|
|
52
|
+
return {"functions": functions, "classes": classes}
|
|
53
|
+
|
|
54
|
+
def handle_assign(self, ns, target, defi):
|
|
55
|
+
scope = self.get_scope(ns)
|
|
56
|
+
if scope:
|
|
57
|
+
scope.add_def(target, defi)
|
|
58
|
+
|
|
59
|
+
def get_def(self, current_ns, var_name):
|
|
60
|
+
current_scope = self.get_scope(current_ns)
|
|
61
|
+
while current_scope:
|
|
62
|
+
defi = current_scope.get_def(var_name)
|
|
63
|
+
if defi:
|
|
64
|
+
return defi
|
|
65
|
+
current_scope = current_scope.parent
|
|
66
|
+
|
|
67
|
+
def get_scope(self, namespace):
|
|
68
|
+
if namespace in self.get_scopes():
|
|
69
|
+
return self.get_scopes()[namespace]
|
|
70
|
+
|
|
71
|
+
def create_scope(self, namespace, parent):
|
|
72
|
+
if not namespace in self.scopes:
|
|
73
|
+
sc = ScopeItem(namespace, parent)
|
|
74
|
+
self.scopes[namespace] = sc
|
|
75
|
+
return self.scopes[namespace]
|
|
76
|
+
|
|
77
|
+
def get_scopes(self):
|
|
78
|
+
return self.scopes
|
|
79
|
+
|
|
80
|
+
class ScopeItem(object):
|
|
81
|
+
def __init__(self, fullns, parent):
|
|
82
|
+
if parent and not isinstance(parent, ScopeItem):
|
|
83
|
+
raise ScopeError("Parent must be a ScopeItem instance")
|
|
84
|
+
|
|
85
|
+
if not isinstance(fullns, str):
|
|
86
|
+
raise ScopeError("Namespace should be a string")
|
|
87
|
+
|
|
88
|
+
self.parent = parent
|
|
89
|
+
self.defs = {}
|
|
90
|
+
self.lambda_counter = 0
|
|
91
|
+
self.dict_counter = 0
|
|
92
|
+
self.list_counter = 0
|
|
93
|
+
self.fullns = fullns
|
|
94
|
+
|
|
95
|
+
def get_ns(self):
|
|
96
|
+
return self.fullns
|
|
97
|
+
|
|
98
|
+
def get_defs(self):
|
|
99
|
+
return self.defs
|
|
100
|
+
|
|
101
|
+
def get_def(self, name):
|
|
102
|
+
defs = self.get_defs()
|
|
103
|
+
if name in defs:
|
|
104
|
+
return defs[name]
|
|
105
|
+
|
|
106
|
+
def get_lambda_counter(self):
|
|
107
|
+
return self.lambda_counter
|
|
108
|
+
|
|
109
|
+
def get_dict_counter(self):
|
|
110
|
+
return self.dict_counter
|
|
111
|
+
|
|
112
|
+
def get_list_counter(self):
|
|
113
|
+
return self.list_counter
|
|
114
|
+
|
|
115
|
+
def inc_lambda_counter(self, val=1):
|
|
116
|
+
self.lambda_counter += val
|
|
117
|
+
return self.lambda_counter
|
|
118
|
+
|
|
119
|
+
def inc_dict_counter(self, val=1):
|
|
120
|
+
self.dict_counter += val
|
|
121
|
+
return self.dict_counter
|
|
122
|
+
|
|
123
|
+
def inc_list_counter(self, val=1):
|
|
124
|
+
self.list_counter += val
|
|
125
|
+
return self.list_counter
|
|
126
|
+
|
|
127
|
+
def reset_counters(self):
|
|
128
|
+
self.lambda_counter = 0
|
|
129
|
+
self.dict_counter = 0
|
|
130
|
+
self.list_counter = 0
|
|
131
|
+
|
|
132
|
+
def add_def(self, name, defi):
|
|
133
|
+
self.defs[name] = defi
|
|
134
|
+
|
|
135
|
+
def merge_def(self, name, to_merge):
|
|
136
|
+
if not name in self.defs:
|
|
137
|
+
self.defs[name] = to_merge
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
self.defs[name].merge_points_to(to_merge.get_points_to())
|
|
141
|
+
|
|
142
|
+
class ScopeError(Exception):
|
|
143
|
+
pass
|
|
File without changes
|