[cairo] Error checking in language bindings

Steven Chaplin stevech1097 at yahoo.com.au
Thu Dec 13 17:33:42 PST 2007


In Pycairo (and many other language bindings?) most cairo function
bindings include code to check the cairo error status and raise an
exception if an error is found.

In C you might write:
  cairo_set_line_width (cr, 10.0);
  cairo_arc (cr, xc, yc, radius, angle1, angle2);
  cairo_stroke (cr);

In Pycairo this would be translated into something much more verbose
like:
  cairo_set_line_width (cr, 10.0);
  status = cairo_status (cr);
  if (status != CAIRO_STATUS_SUCCESS)
    # raise exception
  cairo_arc (cr, xc, yc, radius, angle1, angle2);
  status = cairo_status (cr);
  if (status != CAIRO_STATUS_SUCCESS)
    # raise exception
  cairo_stroke (cr);
  status = cairo_status (cr);
  if (status != CAIRO_STATUS_SUCCESS)
    # raise exception

This was originally necessary to stop making cairo calls as soon as a
cairo context or surface went into an error state, to prevent the
binding crashing with a segmentation fault or other similar fatal error.

But cairo has for a long time now had nil error objects and is
"error-safe" - if you continue calling cairo operations when cairo is in
an error state the operations are just ignored.

So, would I be right to assume that checking the error state and raising
an exception if in error is no longer essential after every single cairo
operation?
Would it be better to strip out all the error checking and exception
raising and allow the programmer to decide when to check the error
status, like you do in C?

The cairo Appendix A. Creating a language binding for cairo, Error
handling section says:
"A language binding could copy the C approach, and for a language
without exceptions, this is likely the right thing to do. However, for a
language with exceptions, exposing a completely different style of error
handling for cairo would be strange. So, instead, status should be
checked after every call to cairo, and exceptions thrown as necessary."

So for a Python binding for cairo it looks like you must choose to use
either
1) cairo-style error checking - manually call cairo_status() when
required, or
2) Python-style error checking - using exceptions.

I think using cairo-style error checking and being able to call smaller
and faster drawing operations is something than many graphics
application programmers would prefer.

Any comments?

Steve



More information about the cairo mailing list