diff -r 6e781cf3d459 -r baf3fe7c6354 python/pngtile.pyx --- a/python/pngtile.pyx Tue Dec 29 15:38:31 2009 +0200 +++ b/python/pngtile.pyx Tue Dec 29 16:44:48 2009 +0200 @@ -2,6 +2,12 @@ struct FILE : pass +cdef extern from "errno.h" : + extern int errno + +cdef extern from "string.h" : + char* strerror (int err) + cdef extern from "Python.h" : int PyFile_Check (object p) FILE* PyFile_AsFile (object p) @@ -34,9 +40,12 @@ int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode) int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr) int pt_image_status (pt_image *image) + int pt_image_update (pt_image *image) int pt_image_tile (pt_image *image, pt_tile_info *info, FILE *out) void pt_image_destroy (pt_image *image) + char* pt_strerror (int err) + OPEN_UPDATE = PT_OPEN_UPDATE CACHE_ERROR = PT_CACHE_ERROR CACHE_FRESH = PT_CACHE_FRESH @@ -46,29 +55,39 @@ class Error (BaseException) : pass +cdef int trap_err (char *op, int ret) except -1 : + if ret < 0 : + raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno))) + + else : + return ret + cdef class Image : cdef pt_image *image def __cinit__ (self, char *png_path, int cache_mode = 0) : - if pt_image_open(&self.image, NULL, png_path, cache_mode) < 0 : - raise Error("pt_image_open: " + png_path) + trap_err("pt_image_open", + pt_image_open(&self.image, NULL, png_path, cache_mode) + ) def info (self) : cdef pt_image_info *image_info - - if pt_image_info_func(self.image, &image_info) < 0 : - raise Error("pt_image_info") + + trap_err("pt_image_info", + pt_image_info_func(self.image, &image_info) + ) return (image_info.width, image_info.height) def status (self) : - cdef int status = pt_image_status(self.image) - - if status < 0 : - raise Error("pt_image_status") - - else : - return status + return trap_err("pt_image_status", + pt_image_status(self.image) + ) + + def update (self) : + trap_err("pt_image_update", + pt_image_update(self.image) + ) def tile (self, size_t width, size_t height, size_t x, size_t y, object out) : cdef FILE *outf @@ -87,8 +106,9 @@ ti.x = x ti.y = y - if pt_image_tile(self.image, &ti, outf) < 0 : - raise Error("pt_image_tile") + trap_err("pt_image_tile", + pt_image_tile(self.image, &ti, outf) + ) def __dealloc__ (self) : if self.image :