Mercurial > public > mercurial-scm > hg
comparison mercurial/ui.py @ 30537:4b0e6677eed1
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.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 26 Nov 2016 09:14:41 -0800 |
parents | 20a42325fdef |
children | d83ca854fa21 |
comparison
equal
deleted
inserted
replaced
30536:98d7636c4729 | 30537:4b0e6677eed1 |
---|---|
1199 def configoverride(self, overrides, source=""): | 1199 def configoverride(self, overrides, source=""): |
1200 """Context manager for temporary config overrides | 1200 """Context manager for temporary config overrides |
1201 `overrides` must be a dict of the following structure: | 1201 `overrides` must be a dict of the following structure: |
1202 {(section, name) : value}""" | 1202 {(section, name) : value}""" |
1203 backups = {} | 1203 backups = {} |
1204 for (section, name), value in overrides.items(): | 1204 try: |
1205 backups[(section, name)] = self.backupconfig(section, name) | 1205 for (section, name), value in overrides.items(): |
1206 self.setconfig(section, name, value, source) | 1206 backups[(section, name)] = self.backupconfig(section, name) |
1207 yield | 1207 self.setconfig(section, name, value, source) |
1208 for __, backup in backups.items(): | 1208 yield |
1209 self.restoreconfig(backup) | 1209 finally: |
1210 # just restoring ui.quiet config to the previous value is not enough | 1210 for __, backup in backups.items(): |
1211 # as it does not update ui.quiet class member | 1211 self.restoreconfig(backup) |
1212 if ('ui', 'quiet') in overrides: | 1212 # just restoring ui.quiet config to the previous value is not enough |
1213 self.fixconfig(section='ui') | 1213 # as it does not update ui.quiet class member |
1214 if ('ui', 'quiet') in overrides: | |
1215 self.fixconfig(section='ui') | |
1214 | 1216 |
1215 class paths(dict): | 1217 class paths(dict): |
1216 """Represents a collection of paths and their configs. | 1218 """Represents a collection of paths and their configs. |
1217 | 1219 |
1218 Data is initially derived from ui instances and the config files they have | 1220 Data is initially derived from ui instances and the config files they have |