comparison mercurial/pycompat.py @ 43489:93f74a7d3f07

merge with stable
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 05 Nov 2019 13:19:24 -0800
parents 8d5489b048b7 579672b347d2
children 313e3a279828
comparison
equal deleted inserted replaced
43488:bfc68404cccd 43489:93f74a7d3f07
10 10
11 from __future__ import absolute_import 11 from __future__ import absolute_import
12 12
13 import getopt 13 import getopt
14 import inspect 14 import inspect
15 import json
15 import os 16 import os
16 import shlex 17 import shlex
17 import sys 18 import sys
18 import tempfile 19 import tempfile
19 20
86 return _rapply(f, xs) 87 return _rapply(f, xs)
87 88
88 89
89 if ispy3: 90 if ispy3:
90 import builtins 91 import builtins
92 import codecs
91 import functools 93 import functools
92 import io 94 import io
93 import struct 95 import struct
94 96
95 if os.name == r'nt' and sys.version_info >= (3, 6): 97 if os.name == r'nt' and sys.version_info >= (3, 6):
344 ret = shlex.split(s.decode('latin-1'), comments, posix) 346 ret = shlex.split(s.decode('latin-1'), comments, posix)
345 return [a.encode('latin-1') for a in ret] 347 return [a.encode('latin-1') for a in ret]
346 348
347 iteritems = lambda x: x.items() 349 iteritems = lambda x: x.items()
348 itervalues = lambda x: x.values() 350 itervalues = lambda x: x.values()
351
352 # Python 3.5's json.load and json.loads require str. We polyfill its
353 # code for detecting encoding from bytes.
354 if sys.version_info[0:2] < (3, 6):
355
356 def _detect_encoding(b):
357 bstartswith = b.startswith
358 if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
359 return 'utf-32'
360 if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
361 return 'utf-16'
362 if bstartswith(codecs.BOM_UTF8):
363 return 'utf-8-sig'
364
365 if len(b) >= 4:
366 if not b[0]:
367 # 00 00 -- -- - utf-32-be
368 # 00 XX -- -- - utf-16-be
369 return 'utf-16-be' if b[1] else 'utf-32-be'
370 if not b[1]:
371 # XX 00 00 00 - utf-32-le
372 # XX 00 00 XX - utf-16-le
373 # XX 00 XX -- - utf-16-le
374 return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
375 elif len(b) == 2:
376 if not b[0]:
377 # 00 XX - utf-16-be
378 return 'utf-16-be'
379 if not b[1]:
380 # XX 00 - utf-16-le
381 return 'utf-16-le'
382 # default
383 return 'utf-8'
384
385 def json_loads(s, *args, **kwargs):
386 if isinstance(s, (bytes, bytearray)):
387 s = s.decode(_detect_encoding(s), 'surrogatepass')
388
389 return json.loads(s, *args, **kwargs)
390
391 else:
392 json_loads = json.loads
349 393
350 else: 394 else:
351 import cStringIO 395 import cStringIO
352 396
353 xrange = xrange 397 xrange = xrange
422 ziplist = zip 466 ziplist = zip
423 rawinput = raw_input 467 rawinput = raw_input
424 getargspec = inspect.getargspec 468 getargspec = inspect.getargspec
425 iteritems = lambda x: x.iteritems() 469 iteritems = lambda x: x.iteritems()
426 itervalues = lambda x: x.itervalues() 470 itervalues = lambda x: x.itervalues()
471 json_loads = json.loads
427 472
428 isjython = sysplatform.startswith(b'java') 473 isjython = sysplatform.startswith(b'java')
429 474
430 isdarwin = sysplatform.startswith(b'darwin') 475 isdarwin = sysplatform.startswith(b'darwin')
431 islinux = sysplatform.startswith(b'linux') 476 islinux = sysplatform.startswith(b'linux')