I have two .pyx
files - bar.pyx
and baz.pyx
. I want to combine them into a single .so
file.
In baz.pyx
I have a function baz
that should do some checks and raise an exception if something goes wrong. In bar.pyx
I want to call baz()
and expect exception to be raised with traceback printed.
Unfortunately, whatever I try, I get some other runtime errors.
Extensions in setup.py
[
Extension(
'testlib.baz', ['src/testlib/baz.pyx'],
),
Extension(
'testlib.foo', ['src/testlib/bar.pyx', 'src/testlib/baz.c'],
),
]
How tested
import testlib.foo
testlib.foo.foo_baz()
Variant 1
# baz.pyx
cdef public baz():
raise ValueError
# bar.pyx
cdef extern baz()
def foo_baz():
baz() # Segmentation fault
Variant 2
# baz.pyx
cdef public int baz() except -1:
PyErr_SetNone(ValueError)
return -1
# bar.pyx
cdef extern int baz() except -1
def foo_baz():
baz() # SystemError: <built-in function foo_baz> returned NULL without setting an error
I can return some value from baz
and raise an exception in foo_baz
depending on return value, but I want as minimum logic to be present in bar.pyx
.
# baz.pyx
cdef public int baz():
return -1
# bar.pyx
cdef extern int baz()
def foo_baz():
if baz() == -1:
raise ValueError # OK