watchman: drop some py2 compat code
The `unicode` reference was being flagged by pytype, even though it was never
evaluated on py3. There's more that can be dropped and `compat.py` can probably
be inlined if we don't care about minimizing the code changes from FB. But I
don't feel like dealing with that.
--- a/hgext/fsmonitor/pywatchman/compat.py Wed Jan 04 17:15:19 2023 -0500
+++ b/hgext/fsmonitor/pywatchman/compat.py Thu Jan 05 00:09:48 2023 -0500
@@ -32,37 +32,22 @@
"""Compatibility module across Python 2 and 3."""
-PYTHON2 = sys.version_info < (3, 0)
PYTHON3 = sys.version_info >= (3, 0)
# This is adapted from https://bitbucket.org/gutworth/six, and used under the
# MIT license. See LICENSE for a full copyright notice.
-if PYTHON3:
-
- def reraise(tp, value, tb=None):
- try:
- if value is None:
- value = tp()
- if value.__traceback__ is not tb:
- raise value.with_traceback(tb)
- raise value
- finally:
- value = None
- tb = None
-else:
- exec(
- """
def reraise(tp, value, tb=None):
try:
- raise tp, value, tb
+ if value is None:
+ value = tp()
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
finally:
+ value = None
tb = None
-""".strip()
- )
+
-if PYTHON3:
- UNICODE = str
-else:
- UNICODE = unicode # noqa: F821 We handled versioning above
+UNICODE = str
--- a/hgext/fsmonitor/pywatchman/pybser.py Wed Jan 04 17:15:19 2023 -0500
+++ b/hgext/fsmonitor/pywatchman/pybser.py Thu Jan 05 00:09:48 2023 -0500
@@ -51,17 +51,15 @@
BSER_SKIP = b"\x0c"
BSER_UTF8STRING = b"\x0d"
-if compat.PYTHON3:
- STRING_TYPES = (str, bytes)
- unicode = str
+STRING_TYPES = (str, bytes)
+unicode = str
+
- def tobytes(i):
- return str(i).encode("ascii")
+def tobytes(i):
+ return str(i).encode("ascii")
- long = int
-else:
- STRING_TYPES = (unicode, str)
- tobytes = bytes
+
+long = int
# Leave room for the serialization header, which includes
# our overall length. To make things simpler, we'll use an
@@ -87,7 +85,7 @@
def _buf_pos(buf, pos):
ret = buf[pos]
# Normalize the return type to bytes
- if compat.PYTHON3 and not isinstance(ret, bytes):
+ if not isinstance(ret, bytes):
ret = bytes((ret,))
return ret
@@ -250,10 +248,7 @@
else:
raise RuntimeError("Cannot represent this mapping value")
self.wpos += needed
- if compat.PYTHON3:
- iteritems = val.items()
- else:
- iteritems = val.iteritems() # noqa: B301 Checked version above
+ iteritems = val.items()
for k, v in iteritems:
self.append_string(k)
self.append_recursive(v)