D9318: errors: catch urllib errors specifically instead of using safehasattr()

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Fri Nov 13 07:39:25 UTC 2020


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

REVISION SUMMARY
  Before this patch, we would catch `IOError` and `OSError` and check if
  the instance had a `.code` member (indicates `HTTPError`) or a
  `.reason` member (indicates the more generic `URLError`). It seems to
  me that can simply catch those exception specifically instead, so
  that's what this code does. The existing code is from fbe8834923c5 <https://phab.mercurial-scm.org/rHGfbe8834923c57e2222b621c3cc1a78f326ed59d6>
  (commands: report http exceptions nicely, 2005-06-17), so I suspect
  it's just that there was no `urllib2` (where `URLError` lives) back
  then.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/scmutil.py

CHANGE DETAILS

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -236,20 +236,20 @@
             ui.error(_(b"(did you forget to compile extensions?)\n"))
         elif m in b"zlib".split():
             ui.error(_(b"(is your Python install correct?)\n"))
+    except util.urlerr.httperror as inst:
+        ui.error(_(b"abort: %s\n") % stringutil.forcebytestr(inst))
+    except util.urlerr.urlerror as inst:
+        try:  # usually it is in the form (errno, strerror)
+            reason = inst.reason.args[1]
+        except (AttributeError, IndexError):
+            # it might be anything, for example a string
+            reason = inst.reason
+        if isinstance(reason, pycompat.unicode):
+            # SSLError of Python 2.7.9 contains a unicode
+            reason = encoding.unitolocal(reason)
+        ui.error(_(b"abort: error: %s\n") % stringutil.forcebytestr(reason))
     except (IOError, OSError) as inst:
-        if util.safehasattr(inst, b"code"):  # HTTPError
-            ui.error(_(b"abort: %s\n") % stringutil.forcebytestr(inst))
-        elif util.safehasattr(inst, b"reason"):  # URLError or SSLError
-            try:  # usually it is in the form (errno, strerror)
-                reason = inst.reason.args[1]
-            except (AttributeError, IndexError):
-                # it might be anything, for example a string
-                reason = inst.reason
-            if isinstance(reason, pycompat.unicode):
-                # SSLError of Python 2.7.9 contains a unicode
-                reason = encoding.unitolocal(reason)
-            ui.error(_(b"abort: error: %s\n") % stringutil.forcebytestr(reason))
-        elif (
+        if (
             util.safehasattr(inst, b"args")
             and inst.args
             and inst.args[0] == errno.EPIPE



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


More information about the Mercurial-devel mailing list