From 126211b917626fe7531310971981ee5d06026625 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 18 Sep 2011 12:20:47 -0400 Subject: Add finalizers to Progress objects. Allows fclose() to be called after the PNG file is written, for example. --- libdimension-python/dimension.pyx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index 601fb69..0fed65f 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -42,9 +42,11 @@ def terminal_width(): cdef class Progress: cdef dmnsn_progress *_progress + cdef _finalizer def __cinit__(self): self._progress = NULL + self._finalizer = None def __init__(self): raise RuntimeError("attempt to create a Progress object.") @@ -58,6 +60,8 @@ cdef class Progress: try: if dmnsn_finish_progress(self._progress) != 0: raise RuntimeError("background task failed.") + if self._finalizer is not None: + self._finalizer() finally: self._progress = NULL @@ -554,7 +558,13 @@ cdef class Canvas: if file == NULL: raise OSError(errno, os.strerror(errno)) - return _Progress(dmnsn_png_write_canvas_async(self._canvas, file)) + def finalize(): + if fclose(file) != 0: + raise OSError(errno, os.strerror(errno)) + + progress = _Progress(dmnsn_png_write_canvas_async(self._canvas, file)) + progress._finalizer = finalize + return progress def draw_GL(self): """Export the canvas to the current OpenGL context.""" -- cgit v1.2.3