Mercurial > cpython-lru_cache-weakref
annotate Lib/json/scanner.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 685f1791fbc5 |
| children |
| rev | line source |
|---|---|
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
1 """JSON token scanner |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 """ |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
3 import re |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
4 try: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
5 from _json import make_scanner as c_make_scanner |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
6 except ImportError: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
7 c_make_scanner = None |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
9 __all__ = ['make_scanner'] |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
11 NUMBER_RE = re.compile( |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
12 r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?', |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
13 (re.VERBOSE | re.MULTILINE | re.DOTALL)) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
15 def py_make_scanner(context): |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
16 parse_object = context.parse_object |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
17 parse_array = context.parse_array |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
18 parse_string = context.parse_string |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
19 match_number = NUMBER_RE.match |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
20 encoding = context.encoding |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
21 strict = context.strict |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
22 parse_float = context.parse_float |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
23 parse_int = context.parse_int |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
24 parse_constant = context.parse_constant |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
25 object_hook = context.object_hook |
|
52427
685f1791fbc5
Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents:
52199
diff
changeset
|
26 object_pairs_hook = context.object_pairs_hook |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
27 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
28 def _scan_once(string, idx): |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
29 try: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
30 nextchar = string[idx] |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
31 except IndexError: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
32 raise StopIteration |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
33 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
34 if nextchar == '"': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
35 return parse_string(string, idx + 1, encoding, strict) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
36 elif nextchar == '{': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
37 return parse_object((string, idx + 1), encoding, strict, |
|
52427
685f1791fbc5
Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents:
52199
diff
changeset
|
38 _scan_once, object_hook, object_pairs_hook) |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
39 elif nextchar == '[': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
40 return parse_array((string, idx + 1), _scan_once) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
41 elif nextchar == 'n' and string[idx:idx + 4] == 'null': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
42 return None, idx + 4 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
43 elif nextchar == 't' and string[idx:idx + 4] == 'true': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
44 return True, idx + 4 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
45 elif nextchar == 'f' and string[idx:idx + 5] == 'false': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
46 return False, idx + 5 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
48 m = match_number(string, idx) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
49 if m is not None: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
50 integer, frac, exp = m.groups() |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
51 if frac or exp: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
52 res = parse_float(integer + (frac or '') + (exp or '')) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
53 else: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
54 res = parse_int(integer) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
55 return res, m.end() |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
56 elif nextchar == 'N' and string[idx:idx + 3] == 'NaN': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
57 return parse_constant('NaN'), idx + 3 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
58 elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
59 return parse_constant('Infinity'), idx + 8 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
60 elif nextchar == '-' and string[idx:idx + 9] == '-Infinity': |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
61 return parse_constant('-Infinity'), idx + 9 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
62 else: |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
63 raise StopIteration |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
64 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
65 return _scan_once |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
67 make_scanner = c_make_scanner or py_make_scanner |
