Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89e8202

Browse files
committed
Python: CG trace: Add some tests using classes
1 parent eeeadad commit 89e8202

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def func(self, arg):
2+
print("func", self, arg)
3+
4+
5+
class Foo(object):
6+
def __init__(self, arg):
7+
print("Foo.__init__", self, arg)
8+
9+
def some_method(self):
10+
print("Foo.some_method", self)
11+
return self
12+
13+
f = func
14+
15+
@staticmethod
16+
def some_staticmethod():
17+
print("Foo.some_staticmethod")
18+
19+
@classmethod
20+
def some_classmethod(cls):
21+
print("Foo.some_classmethod", cls)
22+
23+
24+
foo = Foo(42)
25+
foo.some_method()
26+
foo.f(10)
27+
foo.some_staticmethod()
28+
foo.some_classmethod()
29+
foo.some_method().some_method().some_method()
30+
31+
32+
Foo.some_staticmethod()
33+
Foo.some_classmethod()
34+
35+
36+
class Bar(object):
37+
def wat(self):
38+
print("Bar.wat")
39+
40+
41+
# these calls to Bar() are not recorded (since no __init__ function)
42+
bar = Bar()
43+
bar.wat()
44+
Bar().wat()

python/tools/recorded-call-graph-metrics/ql/RecordedCalls.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class XMLRecordedCall extends XMLElement {
2424
// 2. result.getCall() issubset this.getCall()
2525
not exists(Call call | call = result.getCall() | not this.getCall() = call)
2626
}
27+
28+
override string toString() {
29+
result = this.getName() + " (<..>/" + this.getXMLCall().get_filename_data().regexpCapture(".*/([^/]+)$", 1) + ":" + this.getXMLCall().get_linenum_data() + ")"
30+
}
2731
}
2832

2933
class XMLCall extends XMLElement {
@@ -74,6 +78,10 @@ class XMLPythonCallee extends XMLCallee {
7478

7579
Function getCallee() {
7680
result.getLocation().hasLocationInfo(this.get_filename_data(), this.get_linenum_data(), _, _, _)
81+
or
82+
// if function has decorator, the call will be recorded going to the first
83+
result.getADecorator().getLocation().hasLocationInfo(this.get_filename_data(), this.get_linenum_data(), _, _, _)
84+
7785
}
7886
}
7987

python/tools/recorded-call-graph-metrics/recreate-db.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PYTHON_EXTRACTOR=$(codeql resolve extractor --language=python)
1717
cg-trace --xml "$XMLDIR"/simple.xml example/simple.py
1818
cg-trace --xml "$XMLDIR"/builtins.xml example/builtins.py
1919
cg-trace --xml "$XMLDIR"/multiple-on-one-line.xml example/multiple-on-one-line.py
20+
cg-trace --xml "$XMLDIR"/class-simple.xml example/class-simple.py
2021

2122

2223
rm -rf "$DB"

0 commit comments

Comments
 (0)