Patchwork [1,of,6] cmdutil: narrow scope of KeyError in makefilename()

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 15, 2018, 1:18 p.m.
Message ID <73b14c318785c080637f.1518700719@mimosa>
Download mbox | patch
Permalink /patch/27968/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 15, 2018, 1:18 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1515291976 -32400
#      Sun Jan 07 11:26:16 2018 +0900
# Node ID 73b14c318785c080637fad3100064fe2c9ca1b8b
# Parent  257f3651ada9dbcb2cb97bbe799fa59c57619cc6
cmdutil: narrow scope of KeyError in makefilename()

It seemed too broad to catch a standard Python exception.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -903,34 +903,33 @@  def makefilename(ctx, pat,
         '%': lambda: '%',
         'b': lambda: os.path.basename(ctx.repo().root),
         }
-
-    try:
-        if total is not None:
-            expander['N'] = lambda: '%d' % total
-        if seqno is not None:
-            expander['n'] = lambda: '%d' % seqno
-        if total is not None and seqno is not None:
-            expander['n'] = (lambda: ('%d' % seqno).zfill(len('%d' % total)))
-        if pathname is not None:
-            expander['s'] = lambda: os.path.basename(pathname)
-            expander['d'] = lambda: os.path.dirname(pathname) or '.'
-            expander['p'] = lambda: pathname
-
-        newname = []
-        patlen = len(pat)
-        i = 0
-        while i < patlen:
+    if total is not None:
+        expander['N'] = lambda: '%d' % total
+    if seqno is not None:
+        expander['n'] = lambda: '%d' % seqno
+    if total is not None and seqno is not None:
+        expander['n'] = (lambda: ('%d' % seqno).zfill(len('%d' % total)))
+    if pathname is not None:
+        expander['s'] = lambda: os.path.basename(pathname)
+        expander['d'] = lambda: os.path.dirname(pathname) or '.'
+        expander['p'] = lambda: pathname
+
+    newname = []
+    patlen = len(pat)
+    i = 0
+    while i < patlen:
+        c = pat[i:i + 1]
+        if c == '%':
+            i += 1
             c = pat[i:i + 1]
-            if c == '%':
-                i += 1
-                c = pat[i:i + 1]
+            try:
                 c = expander[c]()
-            newname.append(c)
-            i += 1
-        return ''.join(newname)
-    except KeyError as inst:
-        raise error.Abort(_("invalid format spec '%%%s' in output filename") %
-                         inst.args[0])
+            except KeyError:
+                raise error.Abort(_("invalid format spec '%%%s' in output "
+                                    "filename") % c)
+        newname.append(c)
+        i += 1
+    return ''.join(newname)
 
 def isstdiofilename(pat):
     """True if the given pat looks like a filename denoting stdin/stdout"""
diff --git a/tests/test-export.t b/tests/test-export.t
--- a/tests/test-export.t
+++ b/tests/test-export.t
@@ -186,6 +186,12 @@  Checking if only alphanumeric characters
   exporting patch:
   ____________0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz____.patch
 
+Invalid pattern in file name:
+
+  $ hg export -o '%x.patch' tip
+  abort: invalid format spec '%x' in output filename
+  [255]
+
 Catch exporting unknown revisions (especially empty revsets, see issue3353)
 
   $ hg export