changeset 36276:428de1a59f2d

cmdutil: narrow scope of KeyError in makefilename() It seemed too broad to catch a standard Python exception.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 07 Jan 2018 11:26:16 +0900
parents f574cc00831a
children 18bdfad8506e
files mercurial/cmdutil.py tests/test-export.t
diffstat 2 files changed, 31 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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