From 8bb8fd9f8cfe04a9e61464d6d7ef15a0cf821cf2 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Thu, 22 Aug 2019 00:58:16 +0300 Subject: [PATCH] Unwind-protect changes to e.visited using a finally block --- unpythonic/fix.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/unpythonic/fix.py b/unpythonic/fix.py index e4836cac..b4d5a949 100644 --- a/unpythonic/fix.py +++ b/unpythonic/fix.py @@ -157,18 +157,22 @@ def f_fix(*args, **kwargs): value, e.cache[me] = None, bottom(f_fix.__name__, *args, **kwargs) count = 0 while count < n and value != e.cache[me]: - e.visited.add(me) - value, e.cache[me] = e.cache[me], unwrap(f(*args, **kwargs)) - e.visited.clear() + try: + e.visited.add(me) + value, e.cache[me] = e.cache[me], unwrap(f(*args, **kwargs)) + finally: + e.visited.clear() count += 1 return value if me in e.visited: # return e.cache.get(me, bottom(f_fix.__name__, *args) # same effect, except don't compute bottom again if we don't need to. return e.cache[me] if me in e.cache else bottom(f_fix.__name__, *args, **kwargs) - e.visited.add(me) - value = e.cache[me] = unwrap(f(*args, **kwargs)) - e.visited.remove(me) + try: + e.visited.add(me) + value = e.cache[me] = unwrap(f(*args, **kwargs)) + finally: + e.visited.remove(me) return value f_fix.entrypoint = f # just for information return f_fix