[PATCH 2 of 2] extensions: use extensions.get instead of extensions.find when possible

Laurent Charignon lcharignon at fb.com
Thu Jan 14 16:17:49 CST 2016


# HG changeset patch
# User Laurent Charignon <lcharignon at fb.com>
# Date 1452809847 28800
#      Thu Jan 14 14:17:27 2016 -0800
# Node ID 24fa6ac71920ba0c8438cf180e440213d9468bd6
# Parent  c1ba4521aea0906874fe7de5bee156d5070c0e56
extensions: use extensions.get instead of extensions.find when possible

This patch replaces three uses of extensions.find by extensions.get to make
the code safer and more legible.

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -69,18 +69,17 @@ def qrefresh(origfn, ui, repo, *pats, **
     if not opts['interactive']:
         return origfn(ui, repo, *pats, **opts)
 
-    mq = extensions.find('mq')
+    with extensions.get('mq') as mq:
+        def committomq(ui, repo, *pats, **opts):
+            # At this point the working copy contains only changes that
+            # were accepted. All other changes were reverted.
+            # We can't pass *pats here since qrefresh will undo all other
+            # changed files in the patch that aren't in pats.
+            mq.refresh(ui, repo, **opts)
 
-    def committomq(ui, repo, *pats, **opts):
-        # At this point the working copy contains only changes that
-        # were accepted. All other changes were reverted.
-        # We can't pass *pats here since qrefresh will undo all other
-        # changed files in the patch that aren't in pats.
-        mq.refresh(ui, repo, **opts)
-
-    # backup all changed files
-    cmdutil.dorecord(ui, repo, committomq, None, True,
-                    cmdutil.recordfilter, *pats, **opts)
+        # backup all changed files
+        cmdutil.dorecord(ui, repo, committomq, None, True,
+                        cmdutil.recordfilter, *pats, **opts)
 
 # This command registration is replaced during uisetup().
 @command('qrecord',
@@ -122,21 +121,17 @@ def qnew(origfn, ui, repo, patch, *args,
 
 
 def uisetup(ui):
-    try:
-        mq = extensions.find('mq')
-    except KeyError:
-        return
+    with extensions.get('mq') as mq:
+        cmdtable["qrecord"] = \
+            (qrecord,
+             # same options as qnew, but copy them so we don't get
+             # -i/--interactive for qrecord and add white space diff options
+             mq.cmdtable['^qnew'][1][:] + commands.diffwsopts,
+             _('hg qrecord [OPTION]... PATCH [FILE]...'))
 
-    cmdtable["qrecord"] = \
-        (qrecord,
-         # same options as qnew, but copy them so we don't get
-         # -i/--interactive for qrecord and add white space diff options
-         mq.cmdtable['^qnew'][1][:] + commands.diffwsopts,
-         _('hg qrecord [OPTION]... PATCH [FILE]...'))
-
-    _wrapcmd('qnew', mq.cmdtable, qnew, _("interactively record a new patch"))
-    _wrapcmd('qrefresh', mq.cmdtable, qrefresh,
-             _("interactively select changes to refresh"))
+        _wrapcmd('qnew', mq.cmdtable, qnew, _("interactively record a new patch"))
+        _wrapcmd('qrefresh', mq.cmdtable, qrefresh,
+                 _("interactively select changes to refresh"))
 
 def _wrapcmd(cmd, table, wrapfn, msg):
     entry = extensions.wrapcommand(table, cmd, wrapfn)
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -345,15 +345,12 @@ def help_(ui, name, unknowncmd=False, fu
 
         # check if this command shadows a non-trivial (multi-line)
         # extension help text
-        try:
-            mod = extensions.find(name)
+        with extensions.get(name) as mod:
             doc = gettext(mod.__doc__) or ''
             if '\n' in doc.strip():
                 msg = _('(use "hg help -e %s" to show help for '
                         'the %s extension)') % (name, name)
                 rst.append('\n%s\n' % msg)
-        except KeyError:
-            pass
 
         # options
         if not ui.quiet and entry[1]:


More information about the Mercurial-devel mailing list