D10736: errors: move Abort earlier, so more exceptions can subclass it

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed May 19 05:48:07 UTC 2021


martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  I'd like to make at least `InterventionRequired` subclass `Abort` and
  Python requires the superclass to be defined before the subtype.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/error.py

CHANGE DETAILS

diff --git a/mercurial/error.py b/mercurial/error.py
--- a/mercurial/error.py
+++ b/mercurial/error.py
@@ -51,6 +51,39 @@
         super(Hint, self).__init__(*args, **kw)
 
 
+class Abort(Hint, Exception):
+    """Raised if a command needs to print an error and exit."""
+
+    def __init__(self, message, hint=None, detailed_exit_code=None):
+        # type: (bytes, Optional[bytes]) -> None
+        self.message = message
+        self.hint = hint
+        self.detailed_exit_code = detailed_exit_code
+        # Pass the message into the Exception constructor to help extensions
+        # that look for exc.args[0].
+        Exception.__init__(self, message)
+
+    def __bytes__(self):
+        return self.message
+
+    if pycompat.ispy3:
+
+        def __str__(self):
+            # the output would be unreadable if the message was translated,
+            # but do not replace it with encoding.strfromlocal(), which
+            # may raise another exception.
+            return pycompat.sysstr(self.__bytes__())
+
+    def format(self):
+        # type: () -> bytes
+        from .i18n import _
+
+        message = _(b"abort: %s\n") % self.message
+        if self.hint:
+            message += _(b"(%s)\n") % self.hint
+        return message
+
+
 class StorageError(Hint, Exception):
     """Raised when an error occurs in a storage layer.
 
@@ -182,39 +215,6 @@
         )
 
 
-class Abort(Hint, Exception):
-    """Raised if a command needs to print an error and exit."""
-
-    def __init__(self, message, hint=None, detailed_exit_code=None):
-        # type: (bytes, Optional[bytes]) -> None
-        self.message = message
-        self.hint = hint
-        self.detailed_exit_code = detailed_exit_code
-        # Pass the message into the Exception constructor to help extensions
-        # that look for exc.args[0].
-        Exception.__init__(self, message)
-
-    def __bytes__(self):
-        return self.message
-
-    if pycompat.ispy3:
-
-        def __str__(self):
-            # the output would be unreadable if the message was translated,
-            # but do not replace it with encoding.strfromlocal(), which
-            # may raise another exception.
-            return pycompat.sysstr(self.__bytes__())
-
-    def format(self):
-        # type: () -> bytes
-        from .i18n import _
-
-        message = _(b"abort: %s\n") % self.message
-        if self.hint:
-            message += _(b"(%s)\n") % self.hint
-        return message
-
-
 class InputError(Abort):
     """Indicates that the user made an error in their input.
 



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


More information about the Mercurial-devel mailing list