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') |