mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Python: CG trace: Add some tests using classes
This commit is contained in:
@@ -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()
|
||||
@@ -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(), _, _, _)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user