ui: use try..finally in configoverride
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 26 Nov 2016 09:14:41 -0800
changeset 30537 4b0e6677eed1
parent 30536 98d7636c4729
child 30538 c2154979409d
ui: use try..finally in configoverride @contextmanager almost always have their "yield" inside a try..finally block. This is because if the calling code inside the activated context manager raises, the code after the "yield" won't get executed. A "finally" block, however, will get executed in this scenario.
mercurial/ui.py
--- a/mercurial/ui.py	Sat Nov 26 09:07:11 2016 -0800
+++ b/mercurial/ui.py	Sat Nov 26 09:14:41 2016 -0800
@@ -1201,16 +1201,18 @@
         `overrides` must be a dict of the following structure:
         {(section, name) : value}"""
         backups = {}
-        for (section, name), value in overrides.items():
-            backups[(section, name)] = self.backupconfig(section, name)
-            self.setconfig(section, name, value, source)
-        yield
-        for __, backup in backups.items():
-            self.restoreconfig(backup)
-        # just restoring ui.quiet config to the previous value is not enough
-        # as it does not update ui.quiet class member
-        if ('ui', 'quiet') in overrides:
-            self.fixconfig(section='ui')
+        try:
+            for (section, name), value in overrides.items():
+                backups[(section, name)] = self.backupconfig(section, name)
+                self.setconfig(section, name, value, source)
+            yield
+        finally:
+            for __, backup in backups.items():
+                self.restoreconfig(backup)
+            # just restoring ui.quiet config to the previous value is not enough
+            # as it does not update ui.quiet class member
+            if ('ui', 'quiet') in overrides:
+                self.fixconfig(section='ui')
 
 class paths(dict):
     """Represents a collection of paths and their configs.