1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
cimport cython
def generator():
yield 2
yield 1
yield 3
def returns_set():
return {"foo", "bar", "baz"}
def returns_tuple():
return (1, 2, 3, 0)
@cython.test_fail_if_path_exists("//SimpleCallNode")
def sorted_arg(x):
"""
>>> a = [3, 2, 1]
>>> sorted_arg(a)
[1, 2, 3]
>>> a
[3, 2, 1]
>>> sorted(generator())
[1, 2, 3]
>>> sorted(returns_set())
['bar', 'baz', 'foo']
>>> sorted(returns_tuple())
[0, 1, 2, 3]
>>> sorted(object())
Traceback (most recent call last):
TypeError: 'object' object is not iterable
"""
return sorted(x)
@cython.test_assert_path_exists(
"//PyMethodCallNode",
)
@cython.test_fail_if_path_exists(
"//GeneralCallNode",
"//SimpleCallNode",
)
def sorted_arg_with_key(x):
"""
>>> a = [3, 2, 1]
>>> sorted_arg_with_key(a)
[3, 2, 1]
>>> a
[3, 2, 1]
>>> sorted_arg_with_key(generator())
[3, 2, 1]
>>> sorted_arg_with_key(returns_tuple())
[3, 2, 1, 0]
>>> sorted_arg_with_key(object())
Traceback (most recent call last):
TypeError: 'object' object is not iterable
"""
return sorted(x, key=lambda x: -x)
@cython.test_fail_if_path_exists("//YieldExprNode",
"//NoneCheckNode")
@cython.test_assert_path_exists("//InlinedGeneratorExpressionNode")
def sorted_genexp():
"""
>>> sorted_genexp()
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
"""
return sorted(i*i for i in range(10,0,-1))
@cython.test_fail_if_path_exists("//YieldExprNode",
"//NoneCheckNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def sorted_listcomp():
"""
>>> sorted_listcomp()
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
"""
return sorted([i*i for i in range(10,0,-1)])
@cython.test_fail_if_path_exists("//SimpleCallNode//SimpleCallNode")
@cython.test_assert_path_exists("//SimpleCallNode/NameNode[@name = 'range']")
def sorted_list_of_range():
"""
>>> sorted_list_of_range()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
return sorted(list(range(10,0,-1)))
@cython.test_fail_if_path_exists("//SimpleCallNode")
def sorted_list_literal():
"""
>>> sorted_list_literal()
[1, 1, 2, 2, 3, 3]
"""
return sorted([3, 1, 2] * 2)
@cython.test_fail_if_path_exists("//SimpleCallNode")
def sorted_tuple_literal():
"""
>>> sorted_tuple_literal()
[1, 1, 2, 2, 3, 3]
"""
return sorted((1, 3, 2) * 2)
@cython.test_fail_if_path_exists("//SimpleCallNode")
def sorted_in_loop(L: list, repeat: cython.int, raise_at: cython.int = -1):
# See https://github.com/cython/cython/issues/6496
"""
>>> L = [3, 1, 2]
>>> sorted_in_loop(L, 3)
OK: [1, 2, 3] [1, 2, 3]
OK: [1, 2, 3] [1, 2, 3]
OK: [1, 2, 3] [1, 2, 3]
[1, 2, 3]
>>> L
[3, 1, 2]
>>> L = [3, 1, 2]
>>> sorted_in_loop(L, 1)
OK: [1, 2, 3] [1, 2, 3]
[1, 2, 3]
>>> L
[3, 1, 2]
>>> L = [3, 1, 2]
>>> sorted_in_loop(L, 5, raise_at=2)
OK: [1, 2, 3] [1, 2, 3]
OK: [1, 2, 3] [1, 2, 3]
EX: [1, 2, 3] [1, 2, 3]
OK: [1, 2, 3] [1, 2, 3]
OK: [1, 2, 3] [1, 2, 3]
[1, 2, 3]
>>> L
[3, 1, 2]
"""
for i in range(repeat):
try:
if i == raise_at:
raise ValueError
L = sorted(L)
print(f"OK: {sorted(L)} {L}")
except ValueError:
L = sorted(L)
print(f"EX: {sorted(L)} {L}")
return L
|