From 94ff8dd9790e0902860574f42c908932d01efc38 Mon Sep 17 00:00:00 2001 From: Sindre Johansen Date: Wed, 10 Dec 2014 14:23:42 +0100 Subject: [PATCH] Now iter() calls finish even on an exception As I reported in issue #14 raising an exception in a loop over a iterator got from a iter() call will not finalize the bar. This commit uses a try, finally block to ensure that finish is always called --- progress/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/progress/__init__.py b/progress/__init__.py index 5107bc0..a860e2c 100644 --- a/progress/__init__.py +++ b/progress/__init__.py @@ -73,10 +73,12 @@ class Infinite(object): self.update() def iter(self, it): - for x in it: - yield x - self.next() - self.finish() + try: + for x in it: + yield x + self.next() + finally: + self.finish() class Progress(Infinite): @@ -117,7 +119,9 @@ class Progress(Infinite): except TypeError: pass - for x in it: - yield x - self.next() - self.finish() + try: + for x in it: + yield x + self.next() + finally: + self.finish()