diff --git a/mercurial/extensions.py b/mercurial/extensions.py
index fd93b15b5c30d16fd9c9eba61402d07fc4085db3_bWVyY3VyaWFsL2V4dGVuc2lvbnMucHk=..7d88fde2309f6abc1ff81a9764122d8c6d35fca7_bWVyY3VyaWFsL2V4dGVuc2lvbnMucHk= 100644
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -127,6 +127,21 @@
         fn(loaded=True)
     return mod
 
+def _runuisetup(name, ui):
+    uisetup = getattr(_extensions[name], 'uisetup', None)
+    if uisetup:
+        uisetup(ui)
+
+def _runextsetup(name, ui):
+    extsetup = getattr(_extensions[name], 'extsetup', None)
+    if extsetup:
+        try:
+            extsetup(ui)
+        except TypeError:
+            if extsetup.func_code.co_argcount != 0:
+                raise
+            extsetup() # old extsetup with no ui argument
+
 def loadall(ui):
     result = ui.configitems("extensions")
     newindex = len(_order)
@@ -148,8 +163,6 @@
             ui.traceback()
 
     for name in _order[newindex:]:
-        uisetup = getattr(_extensions[name], 'uisetup', None)
-        if uisetup:
-            uisetup(ui)
+        _runuisetup(name, ui)
 
     for name in _order[newindex:]:
@@ -154,13 +167,6 @@
 
     for name in _order[newindex:]:
-        extsetup = getattr(_extensions[name], 'extsetup', None)
-        if extsetup:
-            try:
-                extsetup(ui)
-            except TypeError:
-                if extsetup.func_code.co_argcount != 0:
-                    raise
-                extsetup() # old extsetup with no ui argument
+        _runextsetup(name, ui)
 
     # Call aftercallbacks that were never met.
     for shortname in _aftercallbacks: