Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../json
File: decoder.py
"""Implementation of JSONDecoder
[0] Fix | Delete
"""
[1] Fix | Delete
import re
[2] Fix | Delete
[3] Fix | Delete
from json import scanner
[4] Fix | Delete
try:
[5] Fix | Delete
from _json import scanstring as c_scanstring
[6] Fix | Delete
except ImportError:
[7] Fix | Delete
c_scanstring = None
[8] Fix | Delete
[9] Fix | Delete
__all__ = ['JSONDecoder', 'JSONDecodeError']
[10] Fix | Delete
[11] Fix | Delete
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
[12] Fix | Delete
[13] Fix | Delete
NaN = float('nan')
[14] Fix | Delete
PosInf = float('inf')
[15] Fix | Delete
NegInf = float('-inf')
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
class JSONDecodeError(ValueError):
[19] Fix | Delete
"""Subclass of ValueError with the following additional properties:
[20] Fix | Delete
[21] Fix | Delete
msg: The unformatted error message
[22] Fix | Delete
doc: The JSON document being parsed
[23] Fix | Delete
pos: The start index of doc where parsing failed
[24] Fix | Delete
lineno: The line corresponding to pos
[25] Fix | Delete
colno: The column corresponding to pos
[26] Fix | Delete
[27] Fix | Delete
"""
[28] Fix | Delete
# Note that this exception is used from _json
[29] Fix | Delete
def __init__(self, msg, doc, pos):
[30] Fix | Delete
lineno = doc.count('\n', 0, pos) + 1
[31] Fix | Delete
colno = pos - doc.rfind('\n', 0, pos)
[32] Fix | Delete
errmsg = '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
[33] Fix | Delete
ValueError.__init__(self, errmsg)
[34] Fix | Delete
self.msg = msg
[35] Fix | Delete
self.doc = doc
[36] Fix | Delete
self.pos = pos
[37] Fix | Delete
self.lineno = lineno
[38] Fix | Delete
self.colno = colno
[39] Fix | Delete
[40] Fix | Delete
def __reduce__(self):
[41] Fix | Delete
return self.__class__, (self.msg, self.doc, self.pos)
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
_CONSTANTS = {
[45] Fix | Delete
'-Infinity': NegInf,
[46] Fix | Delete
'Infinity': PosInf,
[47] Fix | Delete
'NaN': NaN,
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
STRINGCHUNK = re.compile(r'(.*?)(["\\\x00-\x1f])', FLAGS)
[52] Fix | Delete
BACKSLASH = {
[53] Fix | Delete
'"': '"', '\\': '\\', '/': '/',
[54] Fix | Delete
'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t',
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
def _decode_uXXXX(s, pos):
[58] Fix | Delete
esc = s[pos + 1:pos + 5]
[59] Fix | Delete
if len(esc) == 4 and esc[1] not in 'xX':
[60] Fix | Delete
try:
[61] Fix | Delete
return int(esc, 16)
[62] Fix | Delete
except ValueError:
[63] Fix | Delete
pass
[64] Fix | Delete
msg = "Invalid \\uXXXX escape"
[65] Fix | Delete
raise JSONDecodeError(msg, s, pos)
[66] Fix | Delete
[67] Fix | Delete
def py_scanstring(s, end, strict=True,
[68] Fix | Delete
_b=BACKSLASH, _m=STRINGCHUNK.match):
[69] Fix | Delete
"""Scan the string s for a JSON string. End is the index of the
[70] Fix | Delete
character in s after the quote that started the JSON string.
[71] Fix | Delete
Unescapes all valid JSON string escape sequences and raises ValueError
[72] Fix | Delete
on attempt to decode an invalid string. If strict is False then literal
[73] Fix | Delete
control characters are allowed in the string.
[74] Fix | Delete
[75] Fix | Delete
Returns a tuple of the decoded string and the index of the character in s
[76] Fix | Delete
after the end quote."""
[77] Fix | Delete
chunks = []
[78] Fix | Delete
_append = chunks.append
[79] Fix | Delete
begin = end - 1
[80] Fix | Delete
while 1:
[81] Fix | Delete
chunk = _m(s, end)
[82] Fix | Delete
if chunk is None:
[83] Fix | Delete
raise JSONDecodeError("Unterminated string starting at", s, begin)
[84] Fix | Delete
end = chunk.end()
[85] Fix | Delete
content, terminator = chunk.groups()
[86] Fix | Delete
# Content is contains zero or more unescaped string characters
[87] Fix | Delete
if content:
[88] Fix | Delete
_append(content)
[89] Fix | Delete
# Terminator is the end of string, a literal control character,
[90] Fix | Delete
# or a backslash denoting that an escape sequence follows
[91] Fix | Delete
if terminator == '"':
[92] Fix | Delete
break
[93] Fix | Delete
elif terminator != '\\':
[94] Fix | Delete
if strict:
[95] Fix | Delete
#msg = "Invalid control character %r at" % (terminator,)
[96] Fix | Delete
msg = "Invalid control character {0!r} at".format(terminator)
[97] Fix | Delete
raise JSONDecodeError(msg, s, end)
[98] Fix | Delete
else:
[99] Fix | Delete
_append(terminator)
[100] Fix | Delete
continue
[101] Fix | Delete
try:
[102] Fix | Delete
esc = s[end]
[103] Fix | Delete
except IndexError:
[104] Fix | Delete
raise JSONDecodeError("Unterminated string starting at",
[105] Fix | Delete
s, begin) from None
[106] Fix | Delete
# If not a unicode escape sequence, must be in the lookup table
[107] Fix | Delete
if esc != 'u':
[108] Fix | Delete
try:
[109] Fix | Delete
char = _b[esc]
[110] Fix | Delete
except KeyError:
[111] Fix | Delete
msg = "Invalid \\escape: {0!r}".format(esc)
[112] Fix | Delete
raise JSONDecodeError(msg, s, end)
[113] Fix | Delete
end += 1
[114] Fix | Delete
else:
[115] Fix | Delete
uni = _decode_uXXXX(s, end)
[116] Fix | Delete
end += 5
[117] Fix | Delete
if 0xd800 <= uni <= 0xdbff and s[end:end + 2] == '\\u':
[118] Fix | Delete
uni2 = _decode_uXXXX(s, end + 1)
[119] Fix | Delete
if 0xdc00 <= uni2 <= 0xdfff:
[120] Fix | Delete
uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00))
[121] Fix | Delete
end += 6
[122] Fix | Delete
char = chr(uni)
[123] Fix | Delete
_append(char)
[124] Fix | Delete
return ''.join(chunks), end
[125] Fix | Delete
[126] Fix | Delete
[127] Fix | Delete
# Use speedup if available
[128] Fix | Delete
scanstring = c_scanstring or py_scanstring
[129] Fix | Delete
[130] Fix | Delete
WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS)
[131] Fix | Delete
WHITESPACE_STR = ' \t\n\r'
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
[135] Fix | Delete
memo=None, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
[136] Fix | Delete
s, end = s_and_end
[137] Fix | Delete
pairs = []
[138] Fix | Delete
pairs_append = pairs.append
[139] Fix | Delete
# Backwards compatibility
[140] Fix | Delete
if memo is None:
[141] Fix | Delete
memo = {}
[142] Fix | Delete
memo_get = memo.setdefault
[143] Fix | Delete
# Use a slice to prevent IndexError from being raised, the following
[144] Fix | Delete
# check will raise a more specific ValueError if the string is empty
[145] Fix | Delete
nextchar = s[end:end + 1]
[146] Fix | Delete
# Normally we expect nextchar == '"'
[147] Fix | Delete
if nextchar != '"':
[148] Fix | Delete
if nextchar in _ws:
[149] Fix | Delete
end = _w(s, end).end()
[150] Fix | Delete
nextchar = s[end:end + 1]
[151] Fix | Delete
# Trivial empty object
[152] Fix | Delete
if nextchar == '}':
[153] Fix | Delete
if object_pairs_hook is not None:
[154] Fix | Delete
result = object_pairs_hook(pairs)
[155] Fix | Delete
return result, end + 1
[156] Fix | Delete
pairs = {}
[157] Fix | Delete
if object_hook is not None:
[158] Fix | Delete
pairs = object_hook(pairs)
[159] Fix | Delete
return pairs, end + 1
[160] Fix | Delete
elif nextchar != '"':
[161] Fix | Delete
raise JSONDecodeError(
[162] Fix | Delete
"Expecting property name enclosed in double quotes", s, end)
[163] Fix | Delete
end += 1
[164] Fix | Delete
while True:
[165] Fix | Delete
key, end = scanstring(s, end, strict)
[166] Fix | Delete
key = memo_get(key, key)
[167] Fix | Delete
# To skip some function call overhead we optimize the fast paths where
[168] Fix | Delete
# the JSON key separator is ": " or just ":".
[169] Fix | Delete
if s[end:end + 1] != ':':
[170] Fix | Delete
end = _w(s, end).end()
[171] Fix | Delete
if s[end:end + 1] != ':':
[172] Fix | Delete
raise JSONDecodeError("Expecting ':' delimiter", s, end)
[173] Fix | Delete
end += 1
[174] Fix | Delete
[175] Fix | Delete
try:
[176] Fix | Delete
if s[end] in _ws:
[177] Fix | Delete
end += 1
[178] Fix | Delete
if s[end] in _ws:
[179] Fix | Delete
end = _w(s, end + 1).end()
[180] Fix | Delete
except IndexError:
[181] Fix | Delete
pass
[182] Fix | Delete
[183] Fix | Delete
try:
[184] Fix | Delete
value, end = scan_once(s, end)
[185] Fix | Delete
except StopIteration as err:
[186] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[187] Fix | Delete
pairs_append((key, value))
[188] Fix | Delete
try:
[189] Fix | Delete
nextchar = s[end]
[190] Fix | Delete
if nextchar in _ws:
[191] Fix | Delete
end = _w(s, end + 1).end()
[192] Fix | Delete
nextchar = s[end]
[193] Fix | Delete
except IndexError:
[194] Fix | Delete
nextchar = ''
[195] Fix | Delete
end += 1
[196] Fix | Delete
[197] Fix | Delete
if nextchar == '}':
[198] Fix | Delete
break
[199] Fix | Delete
elif nextchar != ',':
[200] Fix | Delete
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
[201] Fix | Delete
end = _w(s, end).end()
[202] Fix | Delete
nextchar = s[end:end + 1]
[203] Fix | Delete
end += 1
[204] Fix | Delete
if nextchar != '"':
[205] Fix | Delete
raise JSONDecodeError(
[206] Fix | Delete
"Expecting property name enclosed in double quotes", s, end - 1)
[207] Fix | Delete
if object_pairs_hook is not None:
[208] Fix | Delete
result = object_pairs_hook(pairs)
[209] Fix | Delete
return result, end
[210] Fix | Delete
pairs = dict(pairs)
[211] Fix | Delete
if object_hook is not None:
[212] Fix | Delete
pairs = object_hook(pairs)
[213] Fix | Delete
return pairs, end
[214] Fix | Delete
[215] Fix | Delete
def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
[216] Fix | Delete
s, end = s_and_end
[217] Fix | Delete
values = []
[218] Fix | Delete
nextchar = s[end:end + 1]
[219] Fix | Delete
if nextchar in _ws:
[220] Fix | Delete
end = _w(s, end + 1).end()
[221] Fix | Delete
nextchar = s[end:end + 1]
[222] Fix | Delete
# Look-ahead for trivial empty array
[223] Fix | Delete
if nextchar == ']':
[224] Fix | Delete
return values, end + 1
[225] Fix | Delete
_append = values.append
[226] Fix | Delete
while True:
[227] Fix | Delete
try:
[228] Fix | Delete
value, end = scan_once(s, end)
[229] Fix | Delete
except StopIteration as err:
[230] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[231] Fix | Delete
_append(value)
[232] Fix | Delete
nextchar = s[end:end + 1]
[233] Fix | Delete
if nextchar in _ws:
[234] Fix | Delete
end = _w(s, end + 1).end()
[235] Fix | Delete
nextchar = s[end:end + 1]
[236] Fix | Delete
end += 1
[237] Fix | Delete
if nextchar == ']':
[238] Fix | Delete
break
[239] Fix | Delete
elif nextchar != ',':
[240] Fix | Delete
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
[241] Fix | Delete
try:
[242] Fix | Delete
if s[end] in _ws:
[243] Fix | Delete
end += 1
[244] Fix | Delete
if s[end] in _ws:
[245] Fix | Delete
end = _w(s, end + 1).end()
[246] Fix | Delete
except IndexError:
[247] Fix | Delete
pass
[248] Fix | Delete
[249] Fix | Delete
return values, end
[250] Fix | Delete
[251] Fix | Delete
[252] Fix | Delete
class JSONDecoder(object):
[253] Fix | Delete
"""Simple JSON <http://json.org> decoder
[254] Fix | Delete
[255] Fix | Delete
Performs the following translations in decoding by default:
[256] Fix | Delete
[257] Fix | Delete
+---------------+-------------------+
[258] Fix | Delete
| JSON | Python |
[259] Fix | Delete
+===============+===================+
[260] Fix | Delete
| object | dict |
[261] Fix | Delete
+---------------+-------------------+
[262] Fix | Delete
| array | list |
[263] Fix | Delete
+---------------+-------------------+
[264] Fix | Delete
| string | str |
[265] Fix | Delete
+---------------+-------------------+
[266] Fix | Delete
| number (int) | int |
[267] Fix | Delete
+---------------+-------------------+
[268] Fix | Delete
| number (real) | float |
[269] Fix | Delete
+---------------+-------------------+
[270] Fix | Delete
| true | True |
[271] Fix | Delete
+---------------+-------------------+
[272] Fix | Delete
| false | False |
[273] Fix | Delete
+---------------+-------------------+
[274] Fix | Delete
| null | None |
[275] Fix | Delete
+---------------+-------------------+
[276] Fix | Delete
[277] Fix | Delete
It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as
[278] Fix | Delete
their corresponding ``float`` values, which is outside the JSON spec.
[279] Fix | Delete
[280] Fix | Delete
"""
[281] Fix | Delete
[282] Fix | Delete
def __init__(self, *, object_hook=None, parse_float=None,
[283] Fix | Delete
parse_int=None, parse_constant=None, strict=True,
[284] Fix | Delete
object_pairs_hook=None):
[285] Fix | Delete
"""``object_hook``, if specified, will be called with the result
[286] Fix | Delete
of every JSON object decoded and its return value will be used in
[287] Fix | Delete
place of the given ``dict``. This can be used to provide custom
[288] Fix | Delete
deserializations (e.g. to support JSON-RPC class hinting).
[289] Fix | Delete
[290] Fix | Delete
``object_pairs_hook``, if specified will be called with the result of
[291] Fix | Delete
every JSON object decoded with an ordered list of pairs. The return
[292] Fix | Delete
value of ``object_pairs_hook`` will be used instead of the ``dict``.
[293] Fix | Delete
This feature can be used to implement custom decoders.
[294] Fix | Delete
If ``object_hook`` is also defined, the ``object_pairs_hook`` takes
[295] Fix | Delete
priority.
[296] Fix | Delete
[297] Fix | Delete
``parse_float``, if specified, will be called with the string
[298] Fix | Delete
of every JSON float to be decoded. By default this is equivalent to
[299] Fix | Delete
float(num_str). This can be used to use another datatype or parser
[300] Fix | Delete
for JSON floats (e.g. decimal.Decimal).
[301] Fix | Delete
[302] Fix | Delete
``parse_int``, if specified, will be called with the string
[303] Fix | Delete
of every JSON int to be decoded. By default this is equivalent to
[304] Fix | Delete
int(num_str). This can be used to use another datatype or parser
[305] Fix | Delete
for JSON integers (e.g. float).
[306] Fix | Delete
[307] Fix | Delete
``parse_constant``, if specified, will be called with one of the
[308] Fix | Delete
following strings: -Infinity, Infinity, NaN.
[309] Fix | Delete
This can be used to raise an exception if invalid JSON numbers
[310] Fix | Delete
are encountered.
[311] Fix | Delete
[312] Fix | Delete
If ``strict`` is false (true is the default), then control
[313] Fix | Delete
characters will be allowed inside strings. Control characters in
[314] Fix | Delete
this context are those with character codes in the 0-31 range,
[315] Fix | Delete
including ``'\\t'`` (tab), ``'\\n'``, ``'\\r'`` and ``'\\0'``.
[316] Fix | Delete
"""
[317] Fix | Delete
self.object_hook = object_hook
[318] Fix | Delete
self.parse_float = parse_float or float
[319] Fix | Delete
self.parse_int = parse_int or int
[320] Fix | Delete
self.parse_constant = parse_constant or _CONSTANTS.__getitem__
[321] Fix | Delete
self.strict = strict
[322] Fix | Delete
self.object_pairs_hook = object_pairs_hook
[323] Fix | Delete
self.parse_object = JSONObject
[324] Fix | Delete
self.parse_array = JSONArray
[325] Fix | Delete
self.parse_string = scanstring
[326] Fix | Delete
self.memo = {}
[327] Fix | Delete
self.scan_once = scanner.make_scanner(self)
[328] Fix | Delete
[329] Fix | Delete
[330] Fix | Delete
def decode(self, s, _w=WHITESPACE.match):
[331] Fix | Delete
"""Return the Python representation of ``s`` (a ``str`` instance
[332] Fix | Delete
containing a JSON document).
[333] Fix | Delete
[334] Fix | Delete
"""
[335] Fix | Delete
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
[336] Fix | Delete
end = _w(s, end).end()
[337] Fix | Delete
if end != len(s):
[338] Fix | Delete
raise JSONDecodeError("Extra data", s, end)
[339] Fix | Delete
return obj
[340] Fix | Delete
[341] Fix | Delete
def raw_decode(self, s, idx=0):
[342] Fix | Delete
"""Decode a JSON document from ``s`` (a ``str`` beginning with
[343] Fix | Delete
a JSON document) and return a 2-tuple of the Python
[344] Fix | Delete
representation and the index in ``s`` where the document ended.
[345] Fix | Delete
[346] Fix | Delete
This can be used to decode a JSON document from a string that may
[347] Fix | Delete
have extraneous data at the end.
[348] Fix | Delete
[349] Fix | Delete
"""
[350] Fix | Delete
try:
[351] Fix | Delete
obj, end = self.scan_once(s, idx)
[352] Fix | Delete
except StopIteration as err:
[353] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[354] Fix | Delete
return obj, end
[355] Fix | Delete
[356] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function