From patchwork Sat Mar 26 16:07:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [1, of, 2] templater: do not abuse SyntaxError to report errors in template map file From: Yuya Nishihara X-Patchwork-Id: 14083 Message-Id: <1c2157434cd147c37a08.1459008451@mimosa> To: mercurial-devel@mercurial-scm.org Date: Sun, 27 Mar 2016 01:07:31 +0900 # HG changeset patch # User Yuya Nishihara # Date 1458982864 -32400 # Sat Mar 26 18:01:04 2016 +0900 # Node ID 1c2157434cd147c37a08494ef54d4aa3d5999728 # Parent d7af9b4ae7dd4238a8529015d55ff3a9d5ff4924 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. diff --git a/hgext/churn.py b/hgext/churn.py --- a/hgext/churn.py +++ b/hgext/churn.py @@ -19,7 +19,6 @@ from mercurial import ( cmdutil, commands, encoding, - error, patch, scmutil, util, @@ -34,12 +33,7 @@ command = cmdutil.command(cmdtable) 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 diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -1532,8 +1532,6 @@ class changeset_templater(changeset_prin 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 @@ def show_changeset(ui, repo, opts, buffe 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 @@ def buildcommittemplate(repo, ctx, subs, 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': diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -978,13 +978,12 @@ class templater(object): 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]: diff --git a/tests/test-command-template.t b/tests/test-command-template.t --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -1008,7 +1008,7 @@ Error if style missing value: $ 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 @@ Error on syntax: $ echo 'x = "f' >> t $ hg log - abort: t:3: unmatched quotes + hg: parse error at t:3: unmatched quotes [255] $ hg log -T '{date'