Patchwork D9240: errors: name arguments to ParseError constructor

login
register
mail settings
Submitter phabricator
Date Oct. 22, 2020, 6:18 p.m.
Message ID <differential-rev-PHID-DREV-bup3rx7h5fivuyrznkxv-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47509/
State Superseded
Headers show

Comments

phabricator - Oct. 22, 2020, 6:18 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  As with similar previous patches, this is to improve readability.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9240

AFFECTED FILES
  hgext/eol.py
  mercurial/dispatch.py
  mercurial/error.py
  mercurial/parser.py
  mercurial/revsetlang.py
  mercurial/templater.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -312,9 +312,9 @@ 
 
 
 def _addparseerrorhint(inst, tmpl):
-    if len(inst.args) <= 1:
-        return  # no location
-    loc = inst.args[1]
+    if inst.location is None:
+        return
+    loc = inst.location
     # Offset the caret location by the number of newlines before the
     # location of the error, since we will replace one-char newlines
     # with the two-char literal r'\n'.
diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -642,8 +642,8 @@ 
     try:
         return _parsewith(spec, lookup=lookup)
     except error.ParseError as inst:
-        if len(inst.args) > 1:  # has location
-            loc = inst.args[1]
+        if inst.location is not None:
+            loc = inst.location
             # Remove newlines -- spaces are equivalent whitespace.
             spec = spec.replace(b'\n', b' ')
             # We want the caret to point to the place in the template that
diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -408,10 +408,10 @@ 
 def parseerrordetail(inst):
     """Compose error message from specified ParseError object
     """
-    if len(inst.args) > 1:
-        return _(b'at %d: %s') % (inst.args[1], inst.args[0])
+    if inst.location is not None:
+        return _(b'at %d: %s') % (inst.location, inst.message)
     else:
-        return inst.args[0]
+        return inst.message
 
 
 class alias(object):
diff --git a/mercurial/error.py b/mercurial/error.py
--- a/mercurial/error.py
+++ b/mercurial/error.py
@@ -224,6 +224,17 @@ 
 class ParseError(Hint, Exception):
     """Raised when parsing config files and {rev,file}sets (msg[, pos])"""
 
+    def __init__(self, message, location=None, hint=None):
+        self.message = message
+        self.location = location
+        self.hint = hint
+        # Pass the message and possibly location into the Exception constructor
+        # to help code that look for exc.args.
+        if location is not None:
+            Exception.__init__(self, message, location)
+        else:
+            Exception.__init__(self, message)
+
     __bytes__ = _tobytes
 
 
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -243,15 +243,15 @@ 
     if isinstance(inst, error.UnknownIdentifier):
         # make sure to check fileset first, as revset can invoke fileset
         similar = _getsimilar(inst.symbols, inst.function)
-    if len(inst.args) > 1:
+    if inst.location is not None:
         write(
             _(b"hg: parse error at %s: %s\n")
-            % (pycompat.bytestr(inst.args[1]), inst.args[0])
+            % (pycompat.bytestr(inst.location), inst.message)
         )
-        if inst.args[0].startswith(b' '):
+        if inst.message.startswith(b' '):
             write(_(b"unexpected leading whitespace\n"))
     else:
-        write(_(b"hg: parse error: %s\n") % inst.args[0])
+        write(_(b"hg: parse error: %s\n") % inst.message)
         _reportsimilar(write, similar)
     if inst.hint:
         write(_(b"(%s)\n") % inst.hint)
diff --git a/hgext/eol.py b/hgext/eol.py
--- a/hgext/eol.py
+++ b/hgext/eol.py
@@ -280,7 +280,7 @@ 
                 b"warning: ignoring .hgeol file due to parse error "
                 b"at %s: %s\n"
             )
-            % (inst.args[1], inst.args[0])
+            % (inst.location, inst.message)
         )
     return None