Skip to content

Commit 429e350

Browse files
committed
Handle NaN & Infinity
1 parent f8d697a commit 429e350

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Lib/json/tool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ def main():
8383

8484

8585
color_pattern = re.compile(r'''
86-
(?P<string>"(\\.|[^"\\])*?") | # String
87-
(?P<number>[\d\-+.Ee]+) | # Number
88-
(?P<boolean>true|false) | # Boolean
89-
(?P<null>null) # Null
86+
(?P<string>"(\\.|[^"\\])*?") | # String
87+
(?P<number>NaN|-?Infinity|[\d\-+.Ee]+) | # Number
88+
(?P<boolean>true|false) | # Boolean
89+
(?P<null>null) # Null
9090
''', re.VERBOSE)
9191

9292

Lib/test/test_json/test_tool.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def test_colors(self):
264264
('null', b'\x1b[36mnull\x1b[0m'),
265265
('true', b'\x1b[36mtrue\x1b[0m'),
266266
('false', b'\x1b[36mfalse\x1b[0m'),
267+
('NaN', b'\x1b[33mNaN\x1b[0m'),
268+
('Infinity', b'\x1b[33mInfinity\x1b[0m'),
269+
('-Infinity', b'\x1b[33m-Infinity\x1b[0m'),
267270
('"foo"', b'\x1b[32m"foo"\x1b[0m'),
268271
(r'" \"foo\" "', b'\x1b[32m" \\"foo\\" "\x1b[0m'),
269272
('123', b'\x1b[33m123\x1b[0m'),
@@ -278,7 +281,13 @@ def test_colors(self):
278281
{
279282
\x1b[32m"\\\\\\\\"\x1b[0m: \x1b[32m""\x1b[0m
280283
}'''),
281-
('{"foo": "bar", "baz": 1234, "qux": [true, false, null]}',
284+
('''\
285+
{
286+
"foo": "bar",
287+
"baz": 1234,
288+
"qux": [true, false, null],
289+
"xyz": [NaN, -Infinity, Infinity]
290+
}''',
282291
b'''\
283292
{
284293
\x1b[32m"foo"\x1b[0m: \x1b[32m"bar"\x1b[0m,
@@ -287,6 +296,11 @@ def test_colors(self):
287296
\x1b[36mtrue\x1b[0m,
288297
\x1b[36mfalse\x1b[0m,
289298
\x1b[36mnull\x1b[0m
299+
],
300+
\x1b[32m"xyz"\x1b[0m: [
301+
\x1b[33mNaN\x1b[0m,
302+
\x1b[33m-Infinity\x1b[0m,
303+
\x1b[33mInfinity\x1b[0m
290304
]
291305
}'''),
292306
)

0 commit comments

Comments
 (0)