exceptions, Python, Cython, C

291 views
Skip to first unread message

Robert Miller

unread,
Feb 24, 2011, 11:52:23 PM2/24/11
to sage-devel
I'm working on wrapping a C package in Cython for use in Sage, and I'm
having some trouble. I think the problem has to do with the fact that
the C program uses stderr to report issues. When the program exits, I
get the following message:

SystemError: error return without exception set

I've looked around online, and found a lot of posts about this issue
for specific C packages, where someone is trying to use the C API to
wrap some C package in Python. But none of the solutions I found
seemed relevant. I also looked in the Cython documentation, but it
doesn't have quite enough information to help. (Please prove me wrong
by posting a link!)

When the code is run, it doesn't seem to go down a path which actually
uses stderr, but it does successfully use stdout to report its result,
before raising the error. It does make it all the way to the "return
0;" line, but doesn't seem to actually return to the Cython function
I'm calling it from. Also, I can't use try/except to catch the
SystemError.

Robert Bradshaw

unread,
Feb 25, 2011, 12:35:21 AM2/25/11
to sage-...@googlegroups.com

This usually means you're returning NULL for an object, or perhaps an
error code for a special method (without setting an error). Perhaps
some code would be helpful?

- Robert

Nils Bruin

unread,
Feb 25, 2011, 12:38:13 AM2/25/11
to sage-devel
On Feb 24, 8:52 pm, Robert Miller <rlmills...@gmail.com> wrote:
> I'm working on wrapping a C package in Cython for use in Sage, and I'm
> having some trouble. I think the problem has to do with the fact that
> the C program uses stderr to report issues. When the program exits, I
> get the following message:
>
> SystemError: error return without exception set

I doubt that sending something or not sending something to stderr is
going to make any difference. It's just a writable stream. My guess is
that a routine that is expected to return a PyObject* is returning
NULL. This signals the occurrence of an error in Python and I can
imagine that when the appropriate exception reporting fields are not
set up when this happens, this is what one would expect.

I imagine this could happen if cython thinks a routine returns a
PyObject * (i.e., object -- the default type in cython), but the
routine in reality returns an integer 0. A mistranslated ".h" file
could easily cause this.

Tom Boothby

unread,
Feb 25, 2011, 4:07:18 AM2/25/11
to sage-...@googlegroups.com
FWIW, I get that error message on the following:

sage: cython("""
cdef int foo(int z) except -1:
return z

def bar(z):
return foo(z)
""")
sage: bar(-1)
---------------------------------------------------------------------------
SystemError Traceback (most recent call last)

/home/tom/sage-4.6.2.rc0/<ipython console> in <module>()

Robert Bradshaw

unread,
Feb 25, 2011, 4:25:01 AM2/25/11
to sage-...@googlegroups.com

Yep. You have to write "except? -1" if -1 is a valid non-error return
value. http://docs.cython.org/src/userguide/language_basics.html#error-return-values

- Robert

Jason Grout

unread,
Feb 25, 2011, 11:07:16 AM2/25/11
to sage-...@googlegroups.com

Note that Robert M. said it made it down to a "return 0" line. So if
Cython is expecting a pointer back, but the program does "return 0",
then that would be equivalent to returning the NULL pointer, right?

Hopefully I'm not confusing the issue...

Thanks,

Jason

Robert Bradshaw

unread,
Feb 25, 2011, 12:24:10 PM2/25/11
to sage-...@googlegroups.com

Yes, but NULL is only the error indicator for an object return type,
in which case 0 would be converted to a Python int.

Is the function in question declared as "except 0"?

- Robert

Robert Miller

unread,
Mar 7, 2011, 4:24:55 PM3/7/11
to sage-devel
The problem: I had a C function declared as "int foo()" but was
importing it in Cython as "cdef foo()". Thus when foo returned 0,
Cython thought this was a NULL pointer and raised the error above.

Thanks for the help, everyone.

Robert Bradshaw

unread,
Mar 7, 2011, 5:21:32 PM3/7/11
to sage-...@googlegroups.com
That would do it :). This is why we need automatic .h -> .pxd
declarations. Also, it's been thrown around a couple of times that it
may be worth making "cdef foo()" a warning, forcing the user to
explicitly declare "cdef object foo()" (or, in your case, "cdef int
foo() :-).

> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

Reply all
Reply to author
Forward
0 new messages