Commits

Yuya Nishihara committed 7f7ecb5

hgext: add general way to reload modules after os.environ change

Comments (0)

Files changed (1)

hgext/chgsupport.py

     def __nonzero__(self):
         return False
 
+_envmodstoreload = [
+    ('LANG LANGUAGE LC_MESSAGES'.split(), [i18n]),  # recreate gettext 't'
+    ]
+
+def _listmodstoreload(newenv):
+    """List modules must be reloaded after os.environ change"""
+    toreload = set()
+    for envkeys, mods in _envmodstoreload:
+        if util.any(newenv.get(k) != os.environ.get(k) for k in envkeys):
+            toreload.update(mods)
+    return toreload
+
 class chgcmdserver(commandserver.server):
     def __init__(self, sui, ui, repo, fin, fout, masterpid):
         super(chgcmdserver, self).__init__(ui, repo, mode='pipe')
         except ValueError:
             raise ValueError('unexpected value in setenv request')
 
-        i18nchanged = util.any(newenv.get(k) != os.environ.get(k)
-                               for k in ('LANG', 'LANGUAGE', 'LC_MESSAGES'))
+        modstoreload = _listmodstoreload(newenv)
 
         if self._sui.debugflag:
             for k in sorted(set(os.environ.keys() + newenv.keys())):
         os.environ.clear()
         os.environ.update(newenv)
 
-        if i18nchanged:
-            self._sui.debug('reload i18n module\n')
-            reload(i18n)  # recreate gettext object 't'
+        for mod in modstoreload:
+            self._sui.debug('reload %s module\n' % mod.__name__)
+            reload(mod)
 
     capabilities = commandserver.server.capabilities.copy()
     capabilities.update({'chdir': chdir,