ยปCore Development>Code coverage>Lib/json/scanner.py

Python code coverage for Lib/json/scanner.py

#countcontent
1n/a"""JSON token scanner
2n/a"""
3n/aimport re
4n/atry:
5n/a from _json import make_scanner as c_make_scanner
6n/aexcept ImportError:
7n/a c_make_scanner = None
8n/a
9n/a__all__ = ['make_scanner']
10n/a
11n/aNUMBER_RE = re.compile(
12n/a r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
13n/a (re.VERBOSE | re.MULTILINE | re.DOTALL))
14n/a
15n/adef py_make_scanner(context):
16n/a parse_object = context.parse_object
17n/a parse_array = context.parse_array
18n/a parse_string = context.parse_string
19n/a match_number = NUMBER_RE.match
20n/a strict = context.strict
21n/a parse_float = context.parse_float
22n/a parse_int = context.parse_int
23n/a parse_constant = context.parse_constant
24n/a object_hook = context.object_hook
25n/a object_pairs_hook = context.object_pairs_hook
26n/a memo = context.memo
27n/a
28n/a def _scan_once(string, idx):
29n/a try:
30n/a nextchar = string[idx]
31n/a except IndexError:
32n/a raise StopIteration(idx)
33n/a
34n/a if nextchar == '"':
35n/a return parse_string(string, idx + 1, strict)
36n/a elif nextchar == '{':
37n/a return parse_object((string, idx + 1), strict,
38n/a _scan_once, object_hook, object_pairs_hook, memo)
39n/a elif nextchar == '[':
40n/a return parse_array((string, idx + 1), _scan_once)
41n/a elif nextchar == 'n' and string[idx:idx + 4] == 'null':
42n/a return None, idx + 4
43n/a elif nextchar == 't' and string[idx:idx + 4] == 'true':
44n/a return True, idx + 4
45n/a elif nextchar == 'f' and string[idx:idx + 5] == 'false':
46n/a return False, idx + 5
47n/a
48n/a m = match_number(string, idx)
49n/a if m is not None:
50n/a integer, frac, exp = m.groups()
51n/a if frac or exp:
52n/a res = parse_float(integer + (frac or '') + (exp or ''))
53n/a else:
54n/a res = parse_int(integer)
55n/a return res, m.end()
56n/a elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
57n/a return parse_constant('NaN'), idx + 3
58n/a elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
59n/a return parse_constant('Infinity'), idx + 8
60n/a elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
61n/a return parse_constant('-Infinity'), idx + 9
62n/a else:
63n/a raise StopIteration(idx)
64n/a
65n/a def scan_once(string, idx):
66n/a try:
67n/a return _scan_once(string, idx)
68n/a finally:
69n/a memo.clear()
70n/a
71n/a return _scan_once
72n/a
73n/amake_scanner = c_make_scanner or py_make_scanner