cmdutil: narrow scope of KeyError in makefilename()
authorYuya Nishihara <yuya@tcha.org>
Sun, 07 Jan 2018 11:26:16 +0900
changeset 36276 428de1a59f2d
parent 36275 f574cc00831a
child 36277 18bdfad8506e
cmdutil: narrow scope of KeyError in makefilename() It seemed too broad to catch a standard Python exception.
mercurial/cmdutil.py
tests/test-export.t
--- a/mercurial/cmdutil.py	Wed Feb 14 21:34:12 2018 -0500
+++ b/mercurial/cmdutil.py	Sun Jan 07 11:26:16 2018 +0900
@@ -903,34 +903,33 @@
         '%': 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"""
--- a/tests/test-export.t	Wed Feb 14 21:34:12 2018 -0500
+++ b/tests/test-export.t	Sun Jan 07 11:26:16 2018 +0900
@@ -186,6 +186,12 @@
   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