changeset 43398:9a8f8c6ed965 stable

fsmonitor: normalize exception types to bytes Unavailable.msg should now always be bytes. We also rename Unavailable.__str__ to __bytes__ as it always returns bytes. We make __str__ a simple wrapper that decodes that result to str. There's probably some excessive strutil.forcebytestr() in fsmonitor/__init__.py now. But at least the exceptions around type coercion should now be gone. Differential Revision: https://phab.mercurial-scm.org/D7214
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 02 Nov 2019 15:02:35 -0700
parents 09ab61c0ab4b
children 742065def6ca
files hgext/fsmonitor/watchmanclient.py
diffstat 1 files changed, 15 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/fsmonitor/watchmanclient.py	Sat Nov 02 14:55:45 2019 -0700
+++ b/hgext/fsmonitor/watchmanclient.py	Sat Nov 02 15:02:35 2019 -0700
@@ -9,8 +9,14 @@
 
 import getpass
 
-from mercurial import util
-from mercurial.utils import procutil
+from mercurial import (
+    encoding,
+    util,
+)
+from mercurial.utils import (
+    procutil,
+    stringutil,
+)
 
 from . import pywatchman
 
@@ -23,12 +29,14 @@
             self.warn = False
         self.invalidate = invalidate
 
-    def __str__(self):
+    def __bytes__(self):
         if self.warn:
             return b'warning: Watchman unavailable: %s' % self.msg
         else:
             return b'Watchman unavailable: %s' % self.msg
 
+    __str__ = encoding.strmethod(__bytes__)
+
 
 class WatchmanNoRoot(Unavailable):
     def __init__(self, root, msg):
@@ -98,10 +106,12 @@
             return self._watchmanclient.query(*watchmanargs)
         except pywatchman.CommandError as ex:
             if b'unable to resolve root' in ex.msg:
-                raise WatchmanNoRoot(self._root, ex.msg)
+                raise WatchmanNoRoot(
+                    self._root, stringutil.forcebytestr(ex.msg)
+                )
             raise Unavailable(ex.msg)
         except pywatchman.WatchmanError as ex:
-            raise Unavailable(str(ex))
+            raise Unavailable(stringutil.forcebytestr(ex))
 
     def command(self, *args):
         try: