From cad5ced6ec9c2108d4cd31599a76134a98440760 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Fri, 4 Mar 2011 13:42:27 -0300 Subject: [PATCH] Add setjmp.pxd and tests --- Cython/Includes/libc/setjmp.pxd | 5 +++ tests/run/setjmp.pyx | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 Cython/Includes/libc/setjmp.pxd create mode 100644 tests/run/setjmp.pyx diff --git a/Cython/Includes/libc/setjmp.pxd b/Cython/Includes/libc/setjmp.pxd new file mode 100644 index 00000000..467280d3 --- /dev/null +++ b/Cython/Includes/libc/setjmp.pxd @@ -0,0 +1,5 @@ +cdef extern from "setjmp.h" nogil: + ctypedef struct jmp_buf: + pass + int setjmp(jmp_buf STATE) + void longjmp(jmp_buf STATE, int VALUE) diff --git a/tests/run/setjmp.pyx b/tests/run/setjmp.pyx new file mode 100644 index 00000000..c033556f --- /dev/null +++ b/tests/run/setjmp.pyx @@ -0,0 +1,54 @@ +from libc.setjmp cimport * + +cdef void check_nonzero(jmp_buf ctx, int x) nogil: + if x == 0: + longjmp(ctx, 1) + +def nonzero(int x): + """ + >>> nonzero(-1) + True + >>> nonzero(0) + False + >>> nonzero(1) + True + >>> nonzero(2) + True + + """ + cdef jmp_buf ctx + if setjmp(ctx) == 0: + check_nonzero(ctx, x) + return True + else: + return False + + +from libc.string cimport strcpy +cdef char error_msg[256] +cdef jmp_buf error_ctx +cdef void error(char msg[]) nogil: + strcpy(error_msg,msg) + longjmp(error_ctx, 1) + +cdef void c_call(int x) nogil: + if x<=0: + error(b"expected a positive value") + +def execute_c_call(int x): + """ + >>> execute_c_call(+2) + >>> execute_c_call(+1) + >>> execute_c_call(+0) + Traceback (most recent call last): + ... + RuntimeError: expected a positive value + >>> execute_c_call(-1) + Traceback (most recent call last): + ... + RuntimeError: expected a positive value + """ + if not setjmp(error_ctx): + c_call(x) + else: + raise RuntimeError(error_msg.decode()) -- 2.26.2