mercurial/pycompat.py
changeset 48960 c3a48dd506da
parent 48959 9ac1a4507bb3
child 48994 e45c39273395
equal deleted inserted replaced
48959:9ac1a4507bb3 48960:c3a48dd506da
   374 
   374 
   375 
   375 
   376 iteritems = lambda x: x.items()
   376 iteritems = lambda x: x.items()
   377 itervalues = lambda x: x.values()
   377 itervalues = lambda x: x.values()
   378 
   378 
   379 # Python 3.5's json.load and json.loads require str. We polyfill its
   379 json_loads = json.loads
   380 # code for detecting encoding from bytes.
       
   381 if sys.version_info[0:2] < (3, 6):
       
   382 
       
   383     def _detect_encoding(b):
       
   384         bstartswith = b.startswith
       
   385         if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
       
   386             return 'utf-32'
       
   387         if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
       
   388             return 'utf-16'
       
   389         if bstartswith(codecs.BOM_UTF8):
       
   390             return 'utf-8-sig'
       
   391 
       
   392         if len(b) >= 4:
       
   393             if not b[0]:
       
   394                 # 00 00 -- -- - utf-32-be
       
   395                 # 00 XX -- -- - utf-16-be
       
   396                 return 'utf-16-be' if b[1] else 'utf-32-be'
       
   397             if not b[1]:
       
   398                 # XX 00 00 00 - utf-32-le
       
   399                 # XX 00 00 XX - utf-16-le
       
   400                 # XX 00 XX -- - utf-16-le
       
   401                 return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
       
   402         elif len(b) == 2:
       
   403             if not b[0]:
       
   404                 # 00 XX - utf-16-be
       
   405                 return 'utf-16-be'
       
   406             if not b[1]:
       
   407                 # XX 00 - utf-16-le
       
   408                 return 'utf-16-le'
       
   409         # default
       
   410         return 'utf-8'
       
   411 
       
   412     def json_loads(s, *args, **kwargs):
       
   413         if isinstance(s, (bytes, bytearray)):
       
   414             s = s.decode(_detect_encoding(s), 'surrogatepass')
       
   415 
       
   416         return json.loads(s, *args, **kwargs)
       
   417 
       
   418 else:
       
   419     json_loads = json.loads
       
   420 
   380 
   421 isjython = sysplatform.startswith(b'java')
   381 isjython = sysplatform.startswith(b'java')
   422 
   382 
   423 isdarwin = sysplatform.startswith(b'darwin')
   383 isdarwin = sysplatform.startswith(b'darwin')
   424 islinux = sysplatform.startswith(b'linux')
   384 islinux = sysplatform.startswith(b'linux')