summaryrefslogtreecommitdiff
path: root/tests/compile/branch_hints.pyx
blob: e6bd0b5c35b171950e9bdf08398dacc051453183 (plain)
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
# mode: compile
# tag: if, unlikely

cimport cython


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[not(@branch_hint)]",
)
def if_simple(x):
    if x:
        x = 2


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[not(@branch_hint)]",
)
def if_return(x):
    if x:
        return 1
    raise TypeError()


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[@branch_hint = 'unlikely']",
)
def if_raise_else(x):
    if x:
        raise TypeError()
    else:
        return 1


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[@branch_hint = 'likely']",
)
def if_else_raise(x):
    if x:
        return 1
    else:
        raise TypeError()


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[@branch_hint = 'unlikely']",
)
def if_raise_else_raise(x):
    if x:
        raise ValueError()
    else:
        raise TypeError()


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[@branch_hint = 'unlikely']",
)
@cython.test_fail_if_path_exists(
    "//IfClauseNode[@branch_hint = 'likely']",
    "//IfClauseNode[not(@branch_hint)]",
)
def if_elif_raise_else_raise(x):
    if x:
        raise ValueError()
    elif not x:
        raise AttributeError()
    else:
        raise TypeError()


@cython.test_assert_path_exists(
    "//IfClauseNode",
    "//IfClauseNode[@branch_hint = 'unlikely']",
    "//IfClauseNode[@branch_hint = 'unlikely']//GILStatNode",
)
@cython.test_fail_if_path_exists(
    "//IfClauseNode[@branch_hint = 'likely']",
    "//IfClauseNode[not(@branch_hint)]",
)
cpdef int nogil_if_raise(int x) except -1 nogil:
    if x:
        raise TypeError()
    elif not x:
        raise ValueError()
    else:
        x = 2