From 518ca437265923932587ea257110e19c00f6e29a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 7 Sep 2011 15:55:59 -0400 Subject: Constrain some invariants a bit tighter in Python module. --- libdimension-python/dimension.pyx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index a9ca3ac..556b90a 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -50,15 +50,16 @@ cdef class Progress: raise RuntimeError("attempt to create a Progress object.") def __dealloc__(self): - self.finish() + if self._progress != NULL: + self.finish() def finish(self): - if self._progress != NULL: - try: - if dmnsn_finish_progress(self._progress) != 0: - raise RuntimeError("background task failed.") - finally: - self._progress = NULL + self._assert_unfinished() + try: + if dmnsn_finish_progress(self._progress) != 0: + raise RuntimeError("background task failed.") + finally: + self._progress = NULL def cancel(self): self._assert_unfinished() @@ -88,6 +89,7 @@ cdef _Progress(dmnsn_progress *progress): cdef class Timer: """A timer for Dimension tasks.""" cdef dmnsn_timer _timer + cdef bool _stopped def __init__(self): """ @@ -95,11 +97,16 @@ cdef class Timer: Timing starts as soon as the object is created. """ + self._stopped = False dmnsn_start_timer(&self._timer) def stop(self): """Stop the Timer.""" + if self._stopped: + raise RuntimeError("timer already stopped.") + dmnsn_stop_timer(&self._timer) + self._stopped = True property real: """Real (wall clock) time.""" @@ -121,6 +128,7 @@ cdef class Timer: cdef _Timer(dmnsn_timer timer): cdef Timer self = Timer.__new__(Timer) self._timer = timer + self._stopped = True return self ############ -- cgit v1.2.3