errors: name arguments to ParseError constructor
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 22 Oct 2020 09:58:05 -0700
changeset 45776 0fc8b066928a
parent 45775 5bb900885311
child 45777 0883413e09bc
errors: name arguments to ParseError constructor As with similar previous patches, this is to improve readability. Differential Revision: https://phab.mercurial-scm.org/D9240
hgext/eol.py
mercurial/dispatch.py
mercurial/error.py
mercurial/parser.py
mercurial/revsetlang.py
mercurial/templater.py
--- a/hgext/eol.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/hgext/eol.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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
 
--- a/mercurial/dispatch.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/mercurial/dispatch.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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)
--- a/mercurial/error.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/mercurial/error.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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 looks for exc.args.
+        if location is not None:
+            Exception.__init__(self, message, location)
+        else:
+            Exception.__init__(self, message)
+
     __bytes__ = _tobytes
 
 
--- a/mercurial/parser.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/mercurial/parser.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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):
--- a/mercurial/revsetlang.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/mercurial/revsetlang.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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
--- a/mercurial/templater.py	Tue Oct 20 08:58:20 2020 -0700
+++ b/mercurial/templater.py	Thu Oct 22 09:58:05 2020 -0700
@@ -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'.