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,189 @@
|
|
|
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 copy
|
|
22
|
+
import os
|
|
23
|
+
import sys
|
|
24
|
+
|
|
25
|
+
import mock
|
|
26
|
+
from base import TestBase
|
|
27
|
+
|
|
28
|
+
from pycg.machinery.imports import ImportManager, ImportManagerError, get_custom_loader
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ImportsTest(TestBase):
|
|
32
|
+
def test_create_node(self):
|
|
33
|
+
im = ImportManager()
|
|
34
|
+
im.set_pkg("")
|
|
35
|
+
|
|
36
|
+
name = "node1"
|
|
37
|
+
im.create_node(name)
|
|
38
|
+
self.assertEqual(im.get_filepath(name), "")
|
|
39
|
+
self.assertEqual(im.get_imports(name), set())
|
|
40
|
+
|
|
41
|
+
# if a node already exists it can't be added again
|
|
42
|
+
with self.assertRaises(ImportManagerError):
|
|
43
|
+
im.create_node(name)
|
|
44
|
+
|
|
45
|
+
# no empty node names
|
|
46
|
+
with self.assertRaises(ImportManagerError):
|
|
47
|
+
im.create_node("")
|
|
48
|
+
|
|
49
|
+
with self.assertRaises(ImportManagerError):
|
|
50
|
+
im.create_node(1)
|
|
51
|
+
|
|
52
|
+
def test_set_filepath(self):
|
|
53
|
+
im = ImportManager()
|
|
54
|
+
im.set_pkg("")
|
|
55
|
+
|
|
56
|
+
name = "node1"
|
|
57
|
+
im.create_node(name)
|
|
58
|
+
|
|
59
|
+
filepath1 = "filepath1"
|
|
60
|
+
im.set_filepath(name, filepath1)
|
|
61
|
+
self.assertEqual(im.get_filepath(name), os.path.abspath(filepath1))
|
|
62
|
+
|
|
63
|
+
filepath2 = "filepath2"
|
|
64
|
+
im.set_filepath(name, filepath2)
|
|
65
|
+
self.assertEqual(im.get_filepath(name), os.path.abspath(filepath2))
|
|
66
|
+
|
|
67
|
+
# only non empty strings allowed
|
|
68
|
+
with self.assertRaises(ImportManagerError):
|
|
69
|
+
im.set_filepath(name, "")
|
|
70
|
+
|
|
71
|
+
with self.assertRaises(ImportManagerError):
|
|
72
|
+
im.set_filepath(name, 1)
|
|
73
|
+
|
|
74
|
+
def test_create_edge(self):
|
|
75
|
+
fpath = "input_file.py"
|
|
76
|
+
im = ImportManager()
|
|
77
|
+
im.set_pkg("")
|
|
78
|
+
node1 = "node1"
|
|
79
|
+
node2 = "node2"
|
|
80
|
+
|
|
81
|
+
im.create_node(node1)
|
|
82
|
+
im.create_node(node2)
|
|
83
|
+
|
|
84
|
+
# current module not set
|
|
85
|
+
with self.assertRaises(ImportManagerError):
|
|
86
|
+
im.create_edge(node2)
|
|
87
|
+
|
|
88
|
+
im.set_current_mod(node1, fpath)
|
|
89
|
+
im.create_edge(node2)
|
|
90
|
+
|
|
91
|
+
self.assertEqual(im.get_imports(node1), set([node2]))
|
|
92
|
+
|
|
93
|
+
# only non empty strings allowed
|
|
94
|
+
with self.assertRaises(ImportManagerError):
|
|
95
|
+
im.create_edge("")
|
|
96
|
+
|
|
97
|
+
with self.assertRaises(ImportManagerError):
|
|
98
|
+
im.create_edge(1)
|
|
99
|
+
|
|
100
|
+
def test_hooks(self):
|
|
101
|
+
input_file = "somedir/somedir/input_file.py"
|
|
102
|
+
im = ImportManager()
|
|
103
|
+
im.set_pkg("somedir/somedir")
|
|
104
|
+
old_sys_path = copy.deepcopy(sys.path)
|
|
105
|
+
old_path_hooks = copy.deepcopy(sys.path_hooks)
|
|
106
|
+
custom_loader = "custom_loader"
|
|
107
|
+
|
|
108
|
+
with mock.patch(
|
|
109
|
+
"importlib.machinery.FileFinder.path_hook", return_value=custom_loader
|
|
110
|
+
):
|
|
111
|
+
im.install_hooks()
|
|
112
|
+
|
|
113
|
+
self.assertEqual(sys.path_hooks[0], custom_loader)
|
|
114
|
+
self.assertEqual(sys.path[0], os.path.abspath(os.path.dirname(input_file)))
|
|
115
|
+
|
|
116
|
+
im.remove_hooks()
|
|
117
|
+
self.assertEqual(old_sys_path, sys.path)
|
|
118
|
+
self.assertEqual(old_path_hooks, sys.path_hooks)
|
|
119
|
+
|
|
120
|
+
def test_custom_loader(self):
|
|
121
|
+
fpath = "input_file.py"
|
|
122
|
+
im = ImportManager()
|
|
123
|
+
im.set_pkg("")
|
|
124
|
+
copy.deepcopy(sys.path)
|
|
125
|
+
im.set_current_mod("node1", fpath)
|
|
126
|
+
im.create_node("node1")
|
|
127
|
+
|
|
128
|
+
# an import happens and the loader is called
|
|
129
|
+
loader = get_custom_loader(im)("node2", "filepath")
|
|
130
|
+
|
|
131
|
+
# verify that edges and nodes have been added
|
|
132
|
+
self.assertEqual(im.get_imports("node1"), set(["node2"]))
|
|
133
|
+
self.assertEqual(im.get_filepath("node2"), os.path.abspath("filepath"))
|
|
134
|
+
|
|
135
|
+
loader = get_custom_loader(im)("node2", "filepath")
|
|
136
|
+
self.assertEqual(im.get_imports("node1"), set(["node2"]))
|
|
137
|
+
self.assertEqual(im.get_filepath("node2"), os.path.abspath("filepath"))
|
|
138
|
+
|
|
139
|
+
self.assertEqual(loader.get_filename("filepath"), "filepath")
|
|
140
|
+
self.assertEqual(loader.get_data("filepath"), "")
|
|
141
|
+
|
|
142
|
+
def test_handle_import_level(self):
|
|
143
|
+
fpath = "input_file.py"
|
|
144
|
+
im = ImportManager()
|
|
145
|
+
im.set_pkg("")
|
|
146
|
+
im.set_current_mod("mod1.mod2.mod3", fpath)
|
|
147
|
+
|
|
148
|
+
# gets outside of package scope
|
|
149
|
+
with self.assertRaises(ImportError):
|
|
150
|
+
im._handle_import_level("something", 4)
|
|
151
|
+
|
|
152
|
+
self.assertEqual(im._handle_import_level("smth", 2), ("..smth", "mod1"))
|
|
153
|
+
self.assertEqual(im._handle_import_level("smth", 1), (".smth", "mod1.mod2"))
|
|
154
|
+
self.assertEqual(im._handle_import_level("smth", 0), ("smth", ""))
|
|
155
|
+
|
|
156
|
+
def test_handle_import(self):
|
|
157
|
+
# test builtin modules
|
|
158
|
+
fpath = "input_file.py"
|
|
159
|
+
im = ImportManager()
|
|
160
|
+
im.set_pkg("")
|
|
161
|
+
im.create_node("mod1")
|
|
162
|
+
im.set_current_mod("mod1", fpath)
|
|
163
|
+
|
|
164
|
+
self.assertEqual(im.handle_import("sys", 0), None)
|
|
165
|
+
self.assertEqual(im.handle_import("sys", 10), None)
|
|
166
|
+
|
|
167
|
+
self.assertEqual(im.get_imports("mod1"), set(["sys"]))
|
|
168
|
+
|
|
169
|
+
# test parent package
|
|
170
|
+
class MockImport:
|
|
171
|
+
def __init__(self, name):
|
|
172
|
+
self.__file__ = name
|
|
173
|
+
|
|
174
|
+
with mock.patch(
|
|
175
|
+
"importlib.import_module",
|
|
176
|
+
return_value=MockImport(os.path.abspath("mod2.py")),
|
|
177
|
+
) as mock_import:
|
|
178
|
+
modname = im.handle_import("mod2", 0)
|
|
179
|
+
self.assertEqual(modname, "mod2")
|
|
180
|
+
mock_import.assert_called_with("mod2", package="")
|
|
181
|
+
|
|
182
|
+
with mock.patch(
|
|
183
|
+
"importlib.import_module",
|
|
184
|
+
return_value=MockImport(os.path.abspath("mod2.py")),
|
|
185
|
+
) as mock_import:
|
|
186
|
+
im.set_current_mod("mod1.mod3", fpath)
|
|
187
|
+
modname = im.handle_import("mod2", 1)
|
|
188
|
+
self.assertEqual(modname, "mod2")
|
|
189
|
+
mock_import.assert_called_once_with(".mod2", package="mod1")
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
from base import TestBase
|
|
22
|
+
|
|
23
|
+
from pycg.machinery.pointers import LiteralPointer, NamePointer, Pointer, PointerError
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PointerTest(TestBase):
|
|
27
|
+
def test_merge(self):
|
|
28
|
+
pointer = Pointer()
|
|
29
|
+
pointer2 = Pointer()
|
|
30
|
+
pointer.add("smth")
|
|
31
|
+
pointer.add("smth2")
|
|
32
|
+
pointer2.add("smth3")
|
|
33
|
+
pointer2.add("smth2")
|
|
34
|
+
|
|
35
|
+
pointer.merge(pointer2)
|
|
36
|
+
|
|
37
|
+
self.assertEqual(pointer.get(), set(["smth", "smth2", "smth3"]))
|
|
38
|
+
self.assertEqual(pointer2.get(), set(["smth2", "smth3"]))
|
|
39
|
+
|
|
40
|
+
pointer3 = Pointer()
|
|
41
|
+
pointer.merge(pointer3)
|
|
42
|
+
self.assertEqual(pointer.get(), set(["smth", "smth2", "smth3"]))
|
|
43
|
+
self.assertEqual(pointer3.get(), set())
|
|
44
|
+
|
|
45
|
+
def test_literal_pointer(self):
|
|
46
|
+
pointer = LiteralPointer()
|
|
47
|
+
# assert that for string values, we just include
|
|
48
|
+
# that the literal points to a str or int
|
|
49
|
+
clone = set(["something"])
|
|
50
|
+
pointer.add("something")
|
|
51
|
+
self.assertEqual(pointer.get(), clone)
|
|
52
|
+
|
|
53
|
+
clone.add(1)
|
|
54
|
+
pointer.add(1)
|
|
55
|
+
self.assertEqual(pointer.get(), clone)
|
|
56
|
+
|
|
57
|
+
clone.add(LiteralPointer.UNK_LIT)
|
|
58
|
+
pointer.add({})
|
|
59
|
+
self.assertEqual(pointer.get(), clone)
|
|
60
|
+
|
|
61
|
+
def test_name_pointer(self):
|
|
62
|
+
pointer = NamePointer()
|
|
63
|
+
clone = {}
|
|
64
|
+
name_clone = {}
|
|
65
|
+
|
|
66
|
+
self.assertEqual(pointer.get_args(), clone)
|
|
67
|
+
|
|
68
|
+
name_clone["name0"] = clone[0] = set(["something", "set", "addition"])
|
|
69
|
+
name_clone["name1"] = clone[1] = set(["somethingelse"])
|
|
70
|
+
|
|
71
|
+
pointer.add_pos_arg(0, "name0", "something")
|
|
72
|
+
pointer.add_pos_arg(1, "name1", "somethingelse")
|
|
73
|
+
pointer.add_pos_arg(0, "name0", set(["set", "addition"]))
|
|
74
|
+
|
|
75
|
+
with self.assertRaises(PointerError):
|
|
76
|
+
pointer.add_pos_arg("NaN", "NaN0", "fail")
|
|
77
|
+
|
|
78
|
+
self.assertEqual(pointer.get_pos_args(), clone)
|
|
79
|
+
self.assertEqual(pointer.get_args(), name_clone)
|
|
80
|
+
|
|
81
|
+
name_clone["name2"] = clone[2] = set([LiteralPointer.INT_LIT])
|
|
82
|
+
pointer.add_pos_lit_arg(2, "name2", 2)
|
|
83
|
+
pointer.add_pos_lit_arg(2, "name2", 3)
|
|
84
|
+
self.assertEqual(pointer.get_pos_args(), clone)
|
|
85
|
+
self.assertEqual(pointer.get_args(), name_clone)
|
|
86
|
+
|
|
87
|
+
clone[1].add(LiteralPointer.STR_LIT)
|
|
88
|
+
name_clone["name1"] = clone[1]
|
|
89
|
+
|
|
90
|
+
pointer.add_pos_lit_arg(1, "name1", "str")
|
|
91
|
+
self.assertEqual(pointer.get_pos_args(), clone)
|
|
92
|
+
self.assertEqual(pointer.get_args(), name_clone)
|
|
93
|
+
|
|
94
|
+
with self.assertRaises(PointerError):
|
|
95
|
+
pointer.add_pos_lit_arg("NaN", "NaN0", "fail")
|
|
96
|
+
|
|
97
|
+
def test_name_pointer_merge(self):
|
|
98
|
+
pointer1 = NamePointer()
|
|
99
|
+
pointer1.add("smth1")
|
|
100
|
+
pointer1.add_arg(0, set(["smth2", "smth3"]))
|
|
101
|
+
pointer1.add_arg(1, set(["smth4"]))
|
|
102
|
+
|
|
103
|
+
pointer2 = NamePointer()
|
|
104
|
+
pointer2.add("smth6")
|
|
105
|
+
pointer2.add_arg(0, set(["smth7", "smth8"]))
|
|
106
|
+
pointer2.add_arg(1, set(["smth9"]))
|
|
107
|
+
|
|
108
|
+
pointer1.merge(pointer2)
|
|
109
|
+
|
|
110
|
+
self.assertEqual(pointer1.get(), set(["smth1", "smth6"]))
|
|
111
|
+
self.assertEqual(pointer1.get_arg(0), set(["smth2", "smth3", "smth7", "smth8"]))
|
|
112
|
+
self.assertEqual(pointer1.get_arg(1), set(["smth4", "smth9"]))
|
|
@@ -0,0 +1,217 @@
|
|
|
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 base import TestBase
|
|
24
|
+
from mock import patch
|
|
25
|
+
|
|
26
|
+
from pycg.machinery.scopes import ScopeError, ScopeItem, ScopeManager
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ScopeManagerTest(TestBase):
|
|
30
|
+
def test_handle_module(self):
|
|
31
|
+
class MockTable(object):
|
|
32
|
+
def __init__(self, name, t, children=[], lineno=0):
|
|
33
|
+
self.name = name
|
|
34
|
+
self.type = t
|
|
35
|
+
self.children = children
|
|
36
|
+
self.lineno = lineno
|
|
37
|
+
|
|
38
|
+
def get_type(self):
|
|
39
|
+
return self.type
|
|
40
|
+
|
|
41
|
+
def get_name(self):
|
|
42
|
+
return self.name
|
|
43
|
+
|
|
44
|
+
def get_children(self):
|
|
45
|
+
return self.children
|
|
46
|
+
|
|
47
|
+
def get_lineno(self):
|
|
48
|
+
return self.lineno
|
|
49
|
+
|
|
50
|
+
grndchld1 = MockTable("grndchld1", "variable", [])
|
|
51
|
+
grndchld2 = MockTable("grndchld2", "function", [])
|
|
52
|
+
chld1 = MockTable("chld1", "function", [grndchld1, grndchld2])
|
|
53
|
+
grndchld3 = MockTable("grndchld3", "variable", [])
|
|
54
|
+
chld2 = MockTable("chld2", "function", [grndchld3])
|
|
55
|
+
grndchld4 = MockTable("grndchld4", "function", [])
|
|
56
|
+
chld3 = MockTable("chld3", "class", [grndchld4])
|
|
57
|
+
root = MockTable("top", "module", [chld1, chld2, chld3])
|
|
58
|
+
|
|
59
|
+
sm = ScopeManager()
|
|
60
|
+
with patch.object(symtable, "symtable", return_value=root):
|
|
61
|
+
items = sm.handle_module("root", "", "")
|
|
62
|
+
|
|
63
|
+
self.assertEqual(
|
|
64
|
+
sorted(items["functions"]),
|
|
65
|
+
sorted([
|
|
66
|
+
"root.chld1",
|
|
67
|
+
"root.chld1.grndchld2",
|
|
68
|
+
"root.chld2",
|
|
69
|
+
"root.chld3.grndchld4",
|
|
70
|
+
]),
|
|
71
|
+
)
|
|
72
|
+
self.assertEqual(sorted(items["classes"]), sorted(["root.chld3"]))
|
|
73
|
+
|
|
74
|
+
self.assertEqual(sm.get_scope("root").get_ns(), "root")
|
|
75
|
+
self.assertEqual(sm.get_scope("root").parent, None)
|
|
76
|
+
|
|
77
|
+
self.assertEqual(sm.get_scope("root.chld1").get_ns(), "root.chld1")
|
|
78
|
+
self.assertEqual(sm.get_scope("root.chld1").parent, sm.get_scope("root"))
|
|
79
|
+
|
|
80
|
+
self.assertEqual(sm.get_scope("root.chld2").get_ns(), "root.chld2")
|
|
81
|
+
self.assertEqual(sm.get_scope("root.chld2").parent, sm.get_scope("root"))
|
|
82
|
+
|
|
83
|
+
self.assertEqual(sm.get_scope("root.chld3").get_ns(), "root.chld3")
|
|
84
|
+
self.assertEqual(sm.get_scope("root.chld3").parent, sm.get_scope("root"))
|
|
85
|
+
|
|
86
|
+
self.assertEqual(
|
|
87
|
+
sm.get_scope("root.chld1.grndchld1").get_ns(), "root.chld1.grndchld1"
|
|
88
|
+
)
|
|
89
|
+
self.assertEqual(
|
|
90
|
+
sm.get_scope("root.chld1.grndchld1").parent, sm.get_scope("root.chld1")
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
self.assertEqual(
|
|
94
|
+
sm.get_scope("root.chld1.grndchld2").get_ns(), "root.chld1.grndchld2"
|
|
95
|
+
)
|
|
96
|
+
self.assertEqual(
|
|
97
|
+
sm.get_scope("root.chld1.grndchld2").parent, sm.get_scope("root.chld1")
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
self.assertEqual(
|
|
101
|
+
sm.get_scope("root.chld2.grndchld3").get_ns(), "root.chld2.grndchld3"
|
|
102
|
+
)
|
|
103
|
+
self.assertEqual(
|
|
104
|
+
sm.get_scope("root.chld2.grndchld3").parent, sm.get_scope("root.chld2")
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def test_handle_assign(self):
|
|
108
|
+
sm = ScopeManager()
|
|
109
|
+
|
|
110
|
+
sm.scopes["root"] = ScopeItem("root", None)
|
|
111
|
+
sm.handle_assign("root", "name", "value")
|
|
112
|
+
self.assertEqual(sm.get_def("root", "name"), "value")
|
|
113
|
+
|
|
114
|
+
def test_get_def(self):
|
|
115
|
+
sm = ScopeManager()
|
|
116
|
+
root = "ns"
|
|
117
|
+
chld1 = "ns.chld1"
|
|
118
|
+
chld2 = "ns.chld2"
|
|
119
|
+
grndchld = "ns.chld1.chld1"
|
|
120
|
+
sm.scopes[root] = ScopeItem(root, None) # root scope
|
|
121
|
+
sm.scopes[chld1] = ScopeItem(chld1, sm.scopes[root]) # 1st child scope
|
|
122
|
+
sm.scopes[chld2] = ScopeItem(chld2, sm.scopes[root]) # 2nd child scope
|
|
123
|
+
sm.scopes[grndchld] = ScopeItem(grndchld, sm.scopes[chld1]) # grandchild
|
|
124
|
+
|
|
125
|
+
grndchld_def = ("var", "grndchild_def") # name, value
|
|
126
|
+
chld1_def1 = ("var", "chld1_def")
|
|
127
|
+
chld1_def2 = ("other_var", "chld1_other_def")
|
|
128
|
+
chld2_def = ("some_var", "chld2_some_var")
|
|
129
|
+
root_def = ("var", "root_var")
|
|
130
|
+
|
|
131
|
+
sm.scopes[root].add_def(root_def[0], root_def[1])
|
|
132
|
+
sm.scopes[chld2].add_def(chld2_def[0], chld2_def[1])
|
|
133
|
+
sm.scopes[chld1].add_def(chld1_def1[0], chld1_def1[1])
|
|
134
|
+
sm.scopes[chld1].add_def(chld1_def2[0], chld1_def2[1])
|
|
135
|
+
sm.scopes[grndchld].add_def(grndchld_def[0], grndchld_def[1])
|
|
136
|
+
|
|
137
|
+
# should be able to find a variable defined in its scope
|
|
138
|
+
# also it should get the value of the nearest scope, meaning it's own
|
|
139
|
+
self.assertEqual(sm.get_def("ns.chld1.chld1", grndchld_def[0]), grndchld_def[1])
|
|
140
|
+
# if it doesn't exist get the parent's
|
|
141
|
+
self.assertEqual(sm.get_def("ns.chld1.chld1", chld1_def2[0]), chld1_def2[1])
|
|
142
|
+
# it shouldn't be able to reach a def defined in chld2
|
|
143
|
+
self.assertEqual(sm.get_def("ns.chld1.chld1", chld2_def[0]), None)
|
|
144
|
+
# root doesn't have access to variables defined in lower scopes
|
|
145
|
+
self.assertEqual(sm.get_def("ns", chld1_def2[0]), None)
|
|
146
|
+
# but childs have access to root
|
|
147
|
+
self.assertEqual(sm.get_def("ns.chld2", root_def[0]), root_def[1])
|
|
148
|
+
|
|
149
|
+
def test_get_scope(self):
|
|
150
|
+
sm = ScopeManager()
|
|
151
|
+
st = ScopeItem("ns", None)
|
|
152
|
+
st2 = ScopeItem("ns.ns2", st)
|
|
153
|
+
|
|
154
|
+
sm.scopes["ns"] = st
|
|
155
|
+
# if it exists it should be returned
|
|
156
|
+
self.assertEqual(sm.get_scope("ns"), st)
|
|
157
|
+
sm.scopes["ns.ns2"] = st2
|
|
158
|
+
self.assertEqual(sm.get_scope("ns.ns2"), st2)
|
|
159
|
+
# otherwise None should be returned
|
|
160
|
+
self.assertEqual(sm.get_scope("notexist"), None)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class ScopeItemTest(TestBase):
|
|
164
|
+
def test_setup(self):
|
|
165
|
+
# no issues
|
|
166
|
+
scope = ScopeItem("smth", None)
|
|
167
|
+
scope = ScopeItem("smth", scope)
|
|
168
|
+
# starts of with empty defs
|
|
169
|
+
self.assertEqual(scope.get_defs(), {})
|
|
170
|
+
|
|
171
|
+
# only strs allowed
|
|
172
|
+
with self.assertRaises(ScopeError):
|
|
173
|
+
scope = ScopeItem(1, None)
|
|
174
|
+
|
|
175
|
+
# parent needs to be a ScopeItem instance
|
|
176
|
+
with self.assertRaises(ScopeError):
|
|
177
|
+
scope = ScopeItem("smth", "parent")
|
|
178
|
+
|
|
179
|
+
def test_get_ns(self):
|
|
180
|
+
# test that namespace is set properly
|
|
181
|
+
root_scope = ScopeItem("root", None)
|
|
182
|
+
self.assertEqual(root_scope.get_ns(), "root")
|
|
183
|
+
child_scope = ScopeItem("root.child", root_scope)
|
|
184
|
+
self.assertEqual(child_scope.get_ns(), "root.child")
|
|
185
|
+
|
|
186
|
+
def test_defs(self):
|
|
187
|
+
class MockDef(object):
|
|
188
|
+
def __init__(self):
|
|
189
|
+
self.get_points_to_called = False
|
|
190
|
+
self.merge_points_to_called = False
|
|
191
|
+
|
|
192
|
+
def get_points_to(self, *args, **kwargs):
|
|
193
|
+
self.get_points_to_called = True
|
|
194
|
+
|
|
195
|
+
def merge_points_to(self, *args, **kwargs):
|
|
196
|
+
self.merge_points_to_called = True
|
|
197
|
+
|
|
198
|
+
scope = ScopeItem("smth", None)
|
|
199
|
+
|
|
200
|
+
clone = {"adef": "definition"}
|
|
201
|
+
|
|
202
|
+
scope.add_def("adef", "definition")
|
|
203
|
+
self.assertEqual(scope.get_defs().items(), clone.items())
|
|
204
|
+
self.assertEqual(scope.get_def("adef"), "definition")
|
|
205
|
+
|
|
206
|
+
defi1 = MockDef()
|
|
207
|
+
defi2 = MockDef()
|
|
208
|
+
scope.add_def("defi1", defi1)
|
|
209
|
+
|
|
210
|
+
# appropriate functions were called
|
|
211
|
+
scope.merge_def("defi1", defi2)
|
|
212
|
+
self.assertTrue(defi1.merge_points_to_called)
|
|
213
|
+
self.assertTrue(defi2.get_points_to_called)
|
|
214
|
+
|
|
215
|
+
# If the def doesn't exist just assign to it
|
|
216
|
+
scope.merge_def("defi100", "defi1001")
|
|
217
|
+
self.assertEqual(scope.get_def("defi100"), "defi1001")
|
PyCG/utils/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
from . import constants # noqa: F401
|
|
22
|
+
from .common import * # noqa
|
PyCG/utils/common.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
|
|
23
|
+
|
|
24
|
+
def get_lambda_name(counter):
|
|
25
|
+
return "<lambda{}>".format(counter)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_dict_name(counter):
|
|
29
|
+
return "<dict{}>".format(counter)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_list_name(counter):
|
|
33
|
+
return "<list{}>".format(counter)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_int_name(counter):
|
|
37
|
+
return "<int{}>".format(counter)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def join_ns(*args):
|
|
41
|
+
return ".".join([arg for arg in args])
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def to_mod_name(name, package=None):
|
|
45
|
+
return os.path.splitext(name)[0].replace("/", ".")
|
PyCG/utils/constants.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
RETURN_NAME = "<RETURN>"
|
|
22
|
+
LAMBDA_NAME = "<LAMBDA_{}>" # needs to be formatted
|
|
23
|
+
BUILTIN_NAME = "<builtin>"
|
|
24
|
+
EXT_NAME = "<external>"
|
|
25
|
+
|
|
26
|
+
FUN_DEF = "FUNCTIONDEF"
|
|
27
|
+
NAME_DEF = "NAMEDEF"
|
|
28
|
+
MOD_DEF = "MODULEDEF"
|
|
29
|
+
CLS_DEF = "CLASSDEF"
|
|
30
|
+
EXT_DEF = "EXTERNALDEF"
|
|
31
|
+
|
|
32
|
+
OBJECT_BASE = "object"
|
|
33
|
+
|
|
34
|
+
CLS_INIT = "__init__"
|
|
35
|
+
ITER_METHOD = "__iter__"
|
|
36
|
+
NEXT_METHOD = "__next__"
|
|
37
|
+
STATIC_METHOD = "staticmethod"
|
|
38
|
+
|
|
39
|
+
INVALID_NAME = "<**INVALID**>"
|
|
40
|
+
|
|
41
|
+
CALL_GRAPH_OP = "call-graph"
|
|
42
|
+
KEY_ERR_OP = "key-error"
|