Patchwork D7187: py3: fix fsmonitor error message formatting under Python3

login
register
mail settings
Submitter phabricator
Date Nov. 1, 2019, 4:14 p.m.
Message ID <differential-rev-PHID-DREV-qc35dcc6jusgekszvtnd-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42645/
State New
Headers show

Comments

phabricator - Nov. 1, 2019, 4:14 p.m.
touilleMan created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The watchmanclient.Unavailable exception got an invalid __str__
  method resulting in a crash when watchman is not available.
  This fix also solve tests/test-install.t.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  hgext/fsmonitor/__init__.py
  hgext/fsmonitor/watchmanclient.py

CHANGE DETAILS




To: touilleMan, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 1, 2019, 5:22 p.m.
This revision now requires changes to proceed.
indygreg added inline comments.
indygreg requested changes to this revision.

INLINE COMMENTS

> watchmanclient.py:26
>  
> -    def __str__(self):
> +    def __bytes__(self):
>          if self.warn:

There's no `__bytes__` on Python 2. So we'll want to preserve an implementation for Python 2.

This is typically something like `__str__ = encoding.strmethod(__bytes__)`

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7187/new/

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

To: touilleMan, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel

Patch

diff --git a/hgext/fsmonitor/watchmanclient.py b/hgext/fsmonitor/watchmanclient.py
--- a/hgext/fsmonitor/watchmanclient.py
+++ b/hgext/fsmonitor/watchmanclient.py
@@ -10,10 +10,10 @@ 
 import getpass
 
 from mercurial import util
+from mercurial.utils.stringutil import forcebytestr
 
 from . import pywatchman
 
-
 class Unavailable(Exception):
     def __init__(self, msg, warn=True, invalidate=False):
         self.msg = msg
@@ -22,7 +22,7 @@ 
             self.warn = False
         self.invalidate = invalidate
 
-    def __str__(self):
+    def __bytes__(self):
         if self.warn:
             return b'warning: Watchman unavailable: %s' % self.msg
         else:
@@ -100,7 +100,7 @@ 
                 raise WatchmanNoRoot(self._root, ex.msg)
             raise Unavailable(ex.msg)
         except pywatchman.WatchmanError as ex:
-            raise Unavailable(str(ex))
+            raise Unavailable(forcebytestr(ex))
 
     def command(self, *args):
         try:
diff --git a/hgext/fsmonitor/__init__.py b/hgext/fsmonitor/__init__.py
--- a/hgext/fsmonitor/__init__.py
+++ b/hgext/fsmonitor/__init__.py
@@ -132,6 +132,7 @@ 
     util,
 )
 from mercurial import match as matchmod
+from mercurial.utils.stringutil import forcebytestr
 
 from . import (
     pywatchman,
@@ -192,7 +193,7 @@ 
             v[b"version"],
         )
     except watchmanclient.Unavailable as e:
-        err = str(e)
+        err = forcebytestr(e)
     fm.condwrite(
         err,
         b"fsmonitor-watchman-error",