Skip to content

Commit 93c9f59

Browse files
committed
Python: Extract version specific coverage/classes.py tests
Since we can analyze operator.py from Python3, but not in Python 2 (since it's implemented in C), we get a difference for the index tests. note: `operator.length_hint` is only available in Python 3.4 and later, so would always fail under Python 2.
1 parent 2cc8fba commit 93c9f59

File tree

8 files changed

+130
-22
lines changed

8 files changed

+130
-22
lines changed

python/ql/test/experimental/dataflow/coverage-py2/argumentRoutingTest.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../coverage/argumentRoutingTest.ql
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Python 2 specific tests, like the one in coverage/classes.py
2+
#
3+
# User-defined methods, both instance methods and class methods, can be called in many non-standard ways
4+
# i.e. differently from simply `c.f()` or `C.f()`. For example, a user-defined `__await__` method on a
5+
# class `C` will be called by the syntactic construct `await c` when `c` is an instance of `C`.
6+
#
7+
# These tests should cover all the class calls that we hope to support.
8+
# It is based on https://docs.python.org/3/reference/datamodel.html, and headings refer there.
9+
#
10+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
11+
# This can be checked by running validTest.py.
12+
13+
import sys
14+
import os
15+
16+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
17+
from testlib import expects
18+
19+
20+
def SINK1(x):
21+
pass
22+
23+
24+
def SINK2(x):
25+
pass
26+
27+
28+
def SINK3(x):
29+
pass
30+
31+
32+
def SINK4(x):
33+
pass
34+
35+
36+
def OK():
37+
print("OK")
38+
39+
40+
# 3.3.8. Emulating numeric types
41+
42+
# object.__index__(self)
43+
class With_index:
44+
def __index__(self):
45+
SINK1(self)
46+
OK() # Call not found
47+
return 0
48+
49+
50+
def test_index():
51+
import operator
52+
53+
with_index = With_index() #$ MISSING: arg1="SSA variable with_index" func=With_index.__index__
54+
operator.index(with_index)

python/ql/test/experimental/dataflow/coverage-py3/argumentRoutingTest.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../coverage/argumentRoutingTest.ql
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Python 3 specific tests, like the one in coverage/classes.py
2+
#
3+
# User-defined methods, both instance methods and class methods, can be called in many non-standard ways
4+
# i.e. differently from simply `c.f()` or `C.f()`. For example, a user-defined `__await__` method on a
5+
# class `C` will be called by the syntactic construct `await c` when `c` is an instance of `C`.
6+
#
7+
# These tests should cover all the class calls that we hope to support.
8+
# It is based on https://docs.python.org/3/reference/datamodel.html, and headings refer there.
9+
#
10+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
11+
# This can be checked by running validTest.py.
12+
13+
import sys
14+
import os
15+
16+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
17+
from testlib import expects
18+
19+
20+
def SINK1(x):
21+
pass
22+
23+
24+
def SINK2(x):
25+
pass
26+
27+
28+
def SINK3(x):
29+
pass
30+
31+
32+
def SINK4(x):
33+
pass
34+
35+
36+
def OK():
37+
print("OK")
38+
39+
40+
41+
# 3.3.7. Emulating container types
42+
43+
# object.__length_hint__(self)
44+
class With_length_hint:
45+
def __length_hint__(self):
46+
SINK1(self)
47+
OK()
48+
return 0
49+
50+
51+
def test_length_hint():
52+
import operator
53+
54+
with_length_hint = With_length_hint() #$ arg1="SSA variable with_length_hint" func=With_length_hint.__length_hint__
55+
operator.length_hint(with_length_hint)
56+
57+
58+
# 3.3.8. Emulating numeric types
59+
60+
# object.__index__(self)
61+
class With_index:
62+
def __index__(self):
63+
SINK1(self)
64+
OK() # Call not found
65+
return 0
66+
67+
68+
def test_index():
69+
import operator
70+
71+
with_index = With_index() #$ arg1="SSA variable with_index" func=With_index.__index__
72+
operator.index(with_index)

python/ql/test/experimental/dataflow/coverage/classes.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -535,21 +535,6 @@ def test_len_if():
535535
pass
536536

537537

538-
# object.__length_hint__(self)
539-
class With_length_hint:
540-
def __length_hint__(self):
541-
SINK1(self)
542-
OK() # Call not found
543-
return 0
544-
545-
546-
def test_length_hint():
547-
import operator
548-
549-
with_length_hint = With_length_hint() #$ arg1="SSA variable with_length_hint" func=With_length_hint.__length_hint__
550-
operator.length_hint(with_length_hint)
551-
552-
553538
# object.__getitem__(self, key)
554539
class With_getitem:
555540
def __getitem__(self, key):
@@ -1378,13 +1363,6 @@ def __index__(self):
13781363
return 0
13791364

13801365

1381-
def test_index():
1382-
import operator
1383-
1384-
with_index = With_index() #$ arg1="SSA variable with_index" func=With_index.__index__
1385-
operator.index(with_index)
1386-
1387-
13881366
def test_index_slicing():
13891367
with_index = With_index() #$ MISSING: arg1="SSA variable with_index" func=With_index.__index__
13901368
[0][with_index:1]

python/ql/test/experimental/dataflow/validTest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def check_tests_valid_after_version(testFile, version):
6464
check_tests_valid("coverage.test")
6565
check_tests_valid("coverage.argumentPassing")
6666
check_tests_valid("coverage.datamodel")
67+
check_tests_valid("coverage-py2.classes")
68+
check_tests_valid("coverage-py3.classes")
6769
check_tests_valid("variable-capture.in")
6870
check_tests_valid("variable-capture.nonlocal")
6971
check_tests_valid("variable-capture.dict")

0 commit comments

Comments
 (0)