summaryrefslogtreecommitdiff
path: root/tests/compile/branch_hints.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/compile/branch_hints.pyx')
-rw-r--r--tests/compile/branch_hints.pyx91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/compile/branch_hints.pyx b/tests/compile/branch_hints.pyx
new file mode 100644
index 000000000..e6bd0b5c3
--- /dev/null
+++ b/tests/compile/branch_hints.pyx
@@ -0,0 +1,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