Mercurial > public > mercurial-scm > hg
comparison mercurial/encoding.py @ 28508:3c6e94d0811c
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.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 11 Mar 2016 21:27:26 -0800 |
parents | 9bcbd9412225 |
children | 0f6d6fdd3c2a |
comparison
equal
deleted
inserted
replaced
28507:9bcbd9412225 | 28508:3c6e94d0811c |
---|---|
385 lower = -1 | 385 lower = -1 |
386 upper = 1 | 386 upper = 1 |
387 other = 0 | 387 other = 0 |
388 | 388 |
389 _jsonmap = [] | 389 _jsonmap = [] |
390 _jsonmap.extend("\\u%04x" % x for x in xrange(32)) | 390 _jsonmap.extend("\\u%04x" % x for x in range(32)) |
391 _jsonmap.extend(chr(x) for x in xrange(32, 127)) | 391 _jsonmap.extend(chr(x) for x in range(32, 127)) |
392 _jsonmap.append('\\u007f') | 392 _jsonmap.append('\\u007f') |
393 _jsonmap[0x09] = '\\t' | 393 _jsonmap[0x09] = '\\t' |
394 _jsonmap[0x0a] = '\\n' | 394 _jsonmap[0x0a] = '\\n' |
395 _jsonmap[0x22] = '\\"' | 395 _jsonmap[0x22] = '\\"' |
396 _jsonmap[0x5c] = '\\\\' | 396 _jsonmap[0x5c] = '\\\\' |
398 _jsonmap[0x0c] = '\\f' | 398 _jsonmap[0x0c] = '\\f' |
399 _jsonmap[0x0d] = '\\r' | 399 _jsonmap[0x0d] = '\\r' |
400 _paranoidjsonmap = _jsonmap[:] | 400 _paranoidjsonmap = _jsonmap[:] |
401 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>") | 401 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>") |
402 _paranoidjsonmap[0x3e] = '\\u003e' # '>' | 402 _paranoidjsonmap[0x3e] = '\\u003e' # '>' |
403 _jsonmap.extend(chr(x) for x in xrange(128, 256)) | 403 _jsonmap.extend(chr(x) for x in range(128, 256)) |
404 | 404 |
405 def jsonescape(s, paranoid=False): | 405 def jsonescape(s, paranoid=False): |
406 '''returns a string suitable for JSON | 406 '''returns a string suitable for JSON |
407 | 407 |
408 JSON is problematic for us because it doesn't support non-Unicode | 408 JSON is problematic for us because it doesn't support non-Unicode |