Python: CG trace: Add some tests using classes

This commit is contained in:
Rasmus Wriedt Larsen
2020-07-21 11:15:07 +02:00
parent eeeadad359
commit 89e8202d11
3 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
def func(self, arg):
print("func", self, arg)
class Foo(object):
def __init__(self, arg):
print("Foo.__init__", self, arg)
def some_method(self):
print("Foo.some_method", self)
return self
f = func
@staticmethod
def some_staticmethod():
print("Foo.some_staticmethod")
@classmethod
def some_classmethod(cls):
print("Foo.some_classmethod", cls)
foo = Foo(42)
foo.some_method()
foo.f(10)
foo.some_staticmethod()
foo.some_classmethod()
foo.some_method().some_method().some_method()
Foo.some_staticmethod()
Foo.some_classmethod()
class Bar(object):
def wat(self):
print("Bar.wat")
# these calls to Bar() are not recorded (since no __init__ function)
bar = Bar()
bar.wat()
Bar().wat()

View File

@@ -24,6 +24,10 @@ class XMLRecordedCall extends XMLElement {
// 2. result.getCall() issubset this.getCall()
not exists(Call call | call = result.getCall() | not this.getCall() = call)
}
override string toString() {
result = this.getName() + " (<..>/" + this.getXMLCall().get_filename_data().regexpCapture(".*/([^/]+)$", 1) + ":" + this.getXMLCall().get_linenum_data() + ")"
}
}
class XMLCall extends XMLElement {
@@ -74,6 +78,10 @@ class XMLPythonCallee extends XMLCallee {
Function getCallee() {
result.getLocation().hasLocationInfo(this.get_filename_data(), this.get_linenum_data(), _, _, _)
or
// if function has decorator, the call will be recorded going to the first
result.getADecorator().getLocation().hasLocationInfo(this.get_filename_data(), this.get_linenum_data(), _, _, _)
}
}

View File

@@ -17,6 +17,7 @@ PYTHON_EXTRACTOR=$(codeql resolve extractor --language=python)
cg-trace --xml "$XMLDIR"/simple.xml example/simple.py
cg-trace --xml "$XMLDIR"/builtins.xml example/builtins.py
cg-trace --xml "$XMLDIR"/multiple-on-one-line.xml example/multiple-on-one-line.py
cg-trace --xml "$XMLDIR"/class-simple.xml example/class-simple.py
rm -rf "$DB"