templater: do not abuse SyntaxError to report errors in template map file
authorYuya Nishihara <yuya@tcha.org>
Sat, 26 Mar 2016 18:01:04 +0900
changeset 28628 ed1d90f6e921
parent 28627 d7af9b4ae7dd
child 28629 d6f8a1535224
templater: do not abuse SyntaxError to report errors in template map file SyntaxError is the class representing syntax errors in Python code. We should use a dedicated exception class for our needs. With this change, unnecessary re-wrapping of SyntaxError can be eliminated.
hgext/churn.py
mercurial/cmdutil.py
mercurial/templater.py
tests/test-command-template.t
--- a/hgext/churn.py	Wed Mar 23 13:34:47 2016 -0700
+++ b/hgext/churn.py	Sat Mar 26 18:01:04 2016 +0900
@@ -19,7 +19,6 @@
     cmdutil,
     commands,
     encoding,
-    error,
     patch,
     scmutil,
     util,
@@ -34,12 +33,7 @@
 testedwith = 'internal'
 
 def maketemplater(ui, repo, tmpl):
-    try:
-        t = cmdutil.changeset_templater(ui, repo, False, None, tmpl,
-                                        None, False)
-    except SyntaxError as inst:
-        raise error.Abort(inst.args[0])
-    return t
+    return cmdutil.changeset_templater(ui, repo, False, None, tmpl, None, False)
 
 def changedlines(ui, repo, ctx1, ctx2, fns):
     added, removed = 0, 0
--- a/mercurial/cmdutil.py	Wed Mar 23 13:34:47 2016 -0700
+++ b/mercurial/cmdutil.py	Sat Mar 26 18:01:04 2016 +0900
@@ -1532,8 +1532,6 @@
         except KeyError as inst:
             msg = _("%s: no key named '%s'")
             raise error.Abort(msg % (self.t.mapfile, inst.args[0]))
-        except SyntaxError as inst:
-            raise error.Abort('%s: %s' % (self.t.mapfile, inst.args[0]))
 
 def gettemplate(ui, tmpl, style):
     """
@@ -1590,12 +1588,7 @@
     if not tmpl and not mapfile:
         return changeset_printer(ui, repo, matchfn, opts, buffered)
 
-    try:
-        t = changeset_templater(ui, repo, matchfn, opts, tmpl, mapfile,
-                                buffered)
-    except SyntaxError as inst:
-        raise error.Abort(inst.args[0])
-    return t
+    return changeset_templater(ui, repo, matchfn, opts, tmpl, mapfile, buffered)
 
 def showmarker(ui, marker, index=None):
     """utility function to display obsolescence marker in a readable way
@@ -2843,10 +2836,7 @@
     ui = repo.ui
     tmpl, mapfile = gettemplate(ui, tmpl, None)
 
-    try:
-        t = changeset_templater(ui, repo, None, {}, tmpl, mapfile, False)
-    except SyntaxError as inst:
-        raise error.Abort(inst.args[0])
+    t = changeset_templater(ui, repo, None, {}, tmpl, mapfile, False)
 
     for k, v in repo.ui.configitems('committemplate'):
         if k != 'changeset':
--- a/mercurial/templater.py	Wed Mar 23 13:34:47 2016 -0700
+++ b/mercurial/templater.py	Sat Mar 26 18:01:04 2016 +0900
@@ -978,13 +978,12 @@
 
         for key, val in conf[''].items():
             if not val:
-                raise SyntaxError(_('%s: missing value') % conf.source('', key))
+                raise error.ParseError(_('missing value'), conf.source('', key))
             if val[0] in "'\"":
                 try:
                     self.cache[key] = unquotestring(val)
                 except SyntaxError as inst:
-                    raise SyntaxError('%s: %s' %
-                                      (conf.source('', key), inst.args[0]))
+                    raise error.ParseError(inst.args[0], conf.source('', key))
             else:
                 val = 'default', val
                 if ':' in val[1]:
--- a/tests/test-command-template.t	Wed Mar 23 13:34:47 2016 -0700
+++ b/tests/test-command-template.t	Sat Mar 26 18:01:04 2016 +0900
@@ -1008,7 +1008,7 @@
 
   $ echo 'changeset =' > t
   $ hg log --style t
-  abort: t:1: missing value
+  hg: parse error at t:1: missing value
   [255]
 
 Error if include fails:
@@ -2508,7 +2508,7 @@
 
   $ echo 'x = "f' >> t
   $ hg log
-  abort: t:3: unmatched quotes
+  hg: parse error at t:3: unmatched quotes
   [255]
 
   $ hg log -T '{date'