You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/ql/test/experimental/dataflow/coverage/classes.py
+57-7Lines changed: 57 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,15 @@
1
-
# These are included so that we can easily evaluate the test code
1
+
# User-defined methods, both instance methods and class methods, can be called in many non-standard ways
2
+
# i.e. differently from simply `c.f()` or `C.f()`. For example, a user-defined `__await__` function will
3
+
# be called by the syntactic constru
4
+
# This should cover all the class calls that we hope to support. It is based on https://docs.python.org/3/reference/datamodel.html.
5
+
#
6
+
# Intended sources should be the variable `SOURCE` and intended sinks should be.
7
+
# arguments to the function `SINK` (see python/ql/test/experimental/dataflow/testConfig.qll).
8
+
#
9
+
# Functions whose name ends with "_with_local_flow" will also be tested for local flow.
10
+
11
+
12
+
# These are included so that we can easily evaluate the test code.
2
13
SOURCE="source"
3
14
defSINK(x):
4
15
print(x)
@@ -17,17 +28,26 @@ def f(a, b):
17
28
# An instance method object combines a class, a class instance and any callable object (normally a user-defined function).
18
29
classC(object):
19
30
20
-
defmethod(self, a, cls):
31
+
defmethod(self, x, cls):
21
32
assertclsisself.__class__
22
-
returna
33
+
returnx
23
34
24
35
@classmethod
25
-
defclassmethod(cls, a):
26
-
returna
36
+
defclassmethod(cls, x):
37
+
returnx
27
38
28
39
@staticmethod
29
-
defstaticmethod():
30
-
returna
40
+
defstaticmethod(x):
41
+
returnx
42
+
43
+
defgen(self, x, count):
44
+
n=count
45
+
whilen>0:
46
+
yieldx
47
+
n-=1
48
+
49
+
asyncdefcoro(self, x):
50
+
returnx
31
51
32
52
c=C()
33
53
@@ -50,9 +70,39 @@ def staticmethod():
50
70
51
71
# Generator functions
52
72
# A function or method which uses the yield statement (see section The yield statement) is called a generator function. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s iterator.__next__() method will cause the function to execute until it provides a value using the yield statement. When the function executes a return statement or falls off the end, a StopIteration exception is raised and the iterator will have reached the end of the set of values to be returned.
73
+
defgen(x, count):
74
+
n=count
75
+
whilen>0:
76
+
yieldx
77
+
n-=1
78
+
79
+
iter=gen(SOURCE, 1)
80
+
SINK(iter.__next__()) # Returns SOURCE, path not found
81
+
SINK(iter.__next__()) # throws StopIteration
82
+
83
+
oiter=c.gen(SOURCE, 1)
84
+
SINK(oiter.__next__())
85
+
SINK(oiter.__next__())
53
86
54
87
# Coroutine functions
55
88
# A function or method which is defined using async def is called a coroutine function. Such a function, when called, returns a coroutine object. It may contain await expressions, as well as async with and async for statements. See also the Coroutine Objects section.
89
+
asyncdefcoro(x):
90
+
returnx
91
+
92
+
importasyncio
93
+
SINK(asyncio.run(coro(SOURCE)))
94
+
SINK(asyncio.run(c.coro(SOURCE)))
95
+
96
+
classA:
97
+
98
+
def__await__(self, x):
99
+
yieldx
100
+
101
+
asyncdefagen(x):
102
+
a=A()
103
+
returnawaita(SOURCE)
104
+
105
+
SINK(asyncio.run(agen(SOURCE))) # fails with TypeError: 'A' object is not callable (possible query?)
56
106
57
107
# Asynchronous generator functions
58
108
# A function or method which is defined using async def and which uses the yield statement is called a asynchronous generator function. Such a function, when called, returns an asynchronous iterator object which can be used in an async for statement to execute the body of the function.
0 commit comments