summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2011-09-18 12:20:47 -0400
committerTavian Barnes <tavianator@gmail.com>2011-09-18 12:20:47 -0400
commit126211b917626fe7531310971981ee5d06026625 (patch)
tree7a93e9fca8a684f9a05def712ba91554a6c73717
parentca6ed9f1a186c79a17d03050d737b29f6f0edc94 (diff)
downloaddimension-126211b917626fe7531310971981ee5d06026625.tar.xz
Add finalizers to Progress objects.
Allows fclose() to be called after the PNG file is written, for example.
-rw-r--r--libdimension-python/dimension.pyx12
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."""