Patchwork D2295: py3: get bytes-repr of network errors portably

login
register
mail settings
Submitter phabricator
Date Feb. 17, 2018, 6:41 a.m.
Message ID <differential-rev-PHID-DREV-lizp7tmdswpiw3qsvljx-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/28014/
State Superseded
Headers show

Comments

phabricator - Feb. 17, 2018, 6:41 a.m.
durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This resolves a lot of weird issues in Python 3 around error strings.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/hgweb/hgweb_mod.py
  mercurial/hgweb/request.py
  mercurial/hgweb/webutil.py
  mercurial/wireproto.py
  mercurial/wireprotoserver.py

CHANGE DETAILS




To: durin42, #hg-reviewers
Cc: mercurial-devel
phabricator - Feb. 17, 2018, 9:43 a.m.
pulkit added a comment.


  For converting error messages to bytes, util.forcebytestr() is preferred. https://phab.mercurial-scm.org/D2269#inline-5791

INLINE COMMENTS

> request.py:125
> +            self.server_write = self._start_response(
> +                pycompat.sysstr(status), self.headers)
>              self._start_response = None

This one looks like an unrelated change.

REPOSITORY
  rHG Mercurial

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

To: durin42, #hg-reviewers
Cc: pulkit, mercurial-devel
phabricator - Feb. 17, 2018, 9:57 a.m.
yuja added a comment.


  Here the exception type is limited, so using `bytestr()` should be okay.

REPOSITORY
  rHG Mercurial

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

To: durin42, #hg-reviewers
Cc: yuja, pulkit, mercurial-devel

Patch

diff --git a/mercurial/wireprotoserver.py b/mercurial/wireprotoserver.py
--- a/mercurial/wireprotoserver.py
+++ b/mercurial/wireprotoserver.py
@@ -339,7 +339,7 @@ 
 
     # TODO This response body assumes the failed command was
     # "unbundle." That assumption is not always valid.
-    req.respond(e, HGTYPE, body='0\n%s\n' % e)
+    req.respond(e, HGTYPE, body='0\n%s\n' % pycompat.bytestr(e))
 
     return ''
 
diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -879,11 +879,11 @@ 
         # cleanly forward Abort error to the client
         if not exchange.bundle2requested(opts.get('bundlecaps')):
             if proto.name == 'http-v1':
-                return ooberror(str(exc) + '\n')
+                return ooberror(pycompat.bytestr(exc) + '\n')
             raise # cannot do better for bundle1 + ssh
         # bundle2 request expect a bundle2 reply
         bundler = bundle2.bundle20(repo.ui)
-        manargs = [('message', str(exc))]
+        manargs = [('message', pycompat.bytestr(exc))]
         advargs = []
         if exc.hint is not None:
             advargs.append(('hint', exc.hint))
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -347,7 +347,7 @@ 
     try:
         return util.processlinerange(fromline, toline)
     except error.ParseError as exc:
-        raise ErrorResponse(HTTP_BAD_REQUEST, str(exc))
+        raise ErrorResponse(HTTP_BAD_REQUEST, pycompat.bytestr(exc))
 
 def formatlinerange(fromline, toline):
     return '%d:%d' % (fromline + 1, toline)
diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py
--- a/mercurial/hgweb/request.py
+++ b/mercurial/hgweb/request.py
@@ -121,7 +121,8 @@ 
             elif isinstance(status, int):
                 status = statusmessage(status)
 
-            self.server_write = self._start_response(status, self.headers)
+            self.server_write = self._start_response(
+                pycompat.sysstr(status), self.headers)
             self._start_response = None
             self.headers = []
         if body is not None:
diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -443,20 +443,20 @@ 
 
         except (error.LookupError, error.RepoLookupError) as err:
             req.respond(HTTP_NOT_FOUND, ctype)
-            msg = str(err)
+            msg = pycompat.bytestr(err)
             if (util.safehasattr(err, 'name') and
                 not isinstance(err,  error.ManifestLookupError)):
                 msg = 'revision not found: %s' % err.name
             return tmpl('error', error=msg)
         except (error.RepoError, error.RevlogError) as inst:
             req.respond(HTTP_SERVER_ERROR, ctype)
-            return tmpl('error', error=str(inst))
+            return tmpl('error', error=pycompat.bytestr(inst))
         except ErrorResponse as inst:
             req.respond(inst, ctype)
             if inst.code == HTTP_NOT_MODIFIED:
                 # Not allowed to return a body on a 304
                 return ['']
-            return tmpl('error', error=str(inst))
+            return tmpl('error', error=pycompat.bytestr(inst))
 
     def check_perm(self, rctx, req, op):
         for permhook in permhooks: