[PATCH 2 of 3] py3: factor out bytechr() function

Yuya Nishihara yuya at tcha.org
Wed Mar 8 10:52:49 EST 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1488979812 -32400
#      Wed Mar 08 22:30:12 2017 +0900
# Node ID a17d13eaa30ec9ef28deba953d281faba0b79909
# Parent  9fc739fcfb59421146294285d450eff80f8dc587
py3: factor out bytechr() function

I also changed xrange(127) to range(127) as the number is relatively small.

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -71,6 +71,9 @@ if ispy3:
     # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
     sysargv = list(map(os.fsencode, sys.argv))
 
+    def bytechr(i):
+        return bytes([i])
+
     def sysstr(s):
         """Return a keyword str to be passed to Python functions such as
         getattr() and str.encode()
@@ -134,6 +137,8 @@ if ispy3:
         return [a.encode('latin-1') for a in ret]
 
 else:
+    bytechr = chr
+
     def sysstr(s):
         return s
 
diff --git a/mercurial/store.py b/mercurial/store.py
--- a/mercurial/store.py
+++ b/mercurial/store.py
@@ -99,12 +99,8 @@ def _buildencodefun():
     'the\\x07quick\\xadshot'
     '''
     e = '_'
-    if pycompat.ispy3:
-        xchr = lambda x: bytes([x])
-        asciistr = [xchr(a) for a in range(127)]
-    else:
-        xchr = chr
-        asciistr = map(chr, xrange(127))
+    xchr = pycompat.bytechr
+    asciistr = list(map(xchr, range(127)))
     capitals = list(range(ord("A"), ord("Z") + 1))
 
     cmap = dict((x, x) for x in asciistr)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -40,12 +40,8 @@ from . import (
 urlreq = util.urlreq
 
 # for use with str.translate(None, _keepalnum), to keep just alphanumerics
-if pycompat.ispy3:
-    _bytes = [bytes([c]) for c in range(256)]
-    _notalnum = [s for s in _bytes if not s.isalnum()]
-else:
-    _notalnum = [c for c in map(chr, range(256)) if not c.isalnum()]
-_keepalnum = ''.join(_notalnum)
+_keepalnum = ''.join(c for c in map(pycompat.bytechr, range(256))
+                     if not c.isalnum())
 
 samplehgrcs = {
     'user':


More information about the Mercurial-devel mailing list