[PATCH 2 of 2] encoding: use range() instead of xrange()

Gregory Szorc gregory.szorc at gmail.com
Sat Mar 12 01:43:57 EST 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1457760446 28800
#      Fri Mar 11 21:27:26 2016 -0800
# Node ID d205c5f57b97ef6e26a85002022c940040c57169
# Parent  bf54b6d99a73008537efc8867984ab474321a0ee
encoding: use range() instead of xrange()

Python 3 doesn't have xrange(). Instead, range() on Python 3
is a generator, like xrange() is on Python 2.

The benefits of xrange() over range() are when there are very
large ranges that are too expensive to pre-allocate. The code
here is only creating <128 values, so the benefits of xrange()
should be negligible.

With this patch, encoding.py imports safely on Python 3.

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -382,30 +382,30 @@ class normcasespecs(object):
     other: the fallback function should always be called
 
     This should be kept in sync with normcase_spec in util.h.'''
     lower = -1
     upper = 1
     other = 0
 
 _jsonmap = []
-_jsonmap.extend("\\u%04x" % x for x in xrange(32))
-_jsonmap.extend(chr(x) for x in xrange(32, 127))
+_jsonmap.extend("\\u%04x" % x for x in range(32))
+_jsonmap.extend(chr(x) for x in range(32, 127))
 _jsonmap.append('\\u007f')
 _jsonmap[0x09] = '\\t'
 _jsonmap[0x0a] = '\\n'
 _jsonmap[0x22] = '\\"'
 _jsonmap[0x5c] = '\\\\'
 _jsonmap[0x08] = '\\b'
 _jsonmap[0x0c] = '\\f'
 _jsonmap[0x0d] = '\\r'
 _paranoidjsonmap = _jsonmap[:]
 _paranoidjsonmap[0x3c] = '\\u003c'  # '<' (e.g. escape "</script>")
 _paranoidjsonmap[0x3e] = '\\u003e'  # '>'
-_jsonmap.extend(chr(x) for x in xrange(128, 256))
+_jsonmap.extend(chr(x) for x in range(128, 256))
 
 def jsonescape(s, paranoid=False):
     '''returns a string suitable for JSON
 
     JSON is problematic for us because it doesn't support non-Unicode
     bytes. To deal with this, we take the following approach:
 
     - localstr objects are converted back to UTF-8


More information about the Mercurial-devel mailing list