Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def py_scanstring(s, end, strict=True,
if strict:
#msg = "Invalid control character %r at" % (terminator,)
msg = "Invalid control character {0!r} at".format(terminator)
raise JSONDecodeError(msg, s, end)
raise JSONDecodeError(msg, s, end - 1)
else:
_append(terminator)
continue
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_json/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ def test_ascii_non_printable_decode(self):
'\b\t\n\f\r')
s = ''.join(map(chr, range(32)))
for c in s:
self.assertRaises(self.JSONDecodeError, self.loads, f'"{c}"')
with self.subTest(control_character=ord(c)):
with self.assertRaises(self.JSONDecodeError) as cm:
self.loads(f'"a{c}b"')
error = cm.exception
self.assertEqual(error.pos, 2)
self.assertEqual(error.lineno, 1)
self.assertEqual(error.colno, 3)
self.assertEqual(self.loads(f'"{s}"', strict=False), s)
self.assertEqual(self.loads('"\x7f"'), '\x7f')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the pure Python :mod:`json` decoder to report the correct position for
invalid literal control characters in JSON strings.
Loading