diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-09-18 12:20:47 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-09-18 12:20:47 -0400 |
commit | 126211b917626fe7531310971981ee5d06026625 (patch) | |
tree | 7a93e9fca8a684f9a05def712ba91554a6c73717 /libdimension-python/dimension.pyx | |
parent | ca6ed9f1a186c79a17d03050d737b29f6f0edc94 (diff) | |
download | dimension-126211b917626fe7531310971981ee5d06026625.tar.xz |
Add finalizers to Progress objects.
Allows fclose() to be called after the PNG file is written, for
example.
Diffstat (limited to 'libdimension-python/dimension.pyx')
-rw-r--r-- | libdimension-python/dimension.pyx | 12 |
1 files changed, 11 insertions, 1 deletions
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.""" |