[cairo] Error checking in language bindings

Owen Taylor otaylor at redhat.com
Fri Dec 14 05:52:01 PST 2007


On Fri, 2007-12-14 at 10:33 +0900, Steven Chaplin wrote:
> 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?

Why would you think so? What advantage would the be in introducing the
"nothing is drawing, I must have did something wrong 20 operations ago"
problems into Pycairo? Are you thinking that checking the status is
a performance problem? Do you have any evidence of this? (There are lots
of other function calls going on when a Python program is running...)

Cairo had the current theory of error handling when the language binding
appendix was written.

- Owen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.cairographics.org/archives/cairo/attachments/20071214/7f77f948/attachment.pgp 


More information about the cairo mailing list