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

login
register
mail settings
Submitter phabricator
Date May 19, 2021, 5:48 a.m.
Message ID <differential-rev-PHID-DREV-aaddbhhc5m42vxld4xjn-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49056/
State Superseded
Headers show

Comments

phabricator - May 19, 2021, 5:48 a.m.
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




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

Patch

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.