Skip to content

Commit c019c44

Browse files
[3.14] gh-154936: Fix JSON control character error position (GH-154941) (#154975)
Co-authored-by: yangbaechu <45089264+yangbaechu@users.noreply.github.com>
1 parent 9e65465 commit c019c44

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

Lib/json/decoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def py_scanstring(s, end, strict=True,
9797
if strict:
9898
#msg = "Invalid control character %r at" % (terminator,)
9999
msg = "Invalid control character {0!r} at".format(terminator)
100-
raise JSONDecodeError(msg, s, end)
100+
raise JSONDecodeError(msg, s, end - 1)
101101
else:
102102
_append(terminator)
103103
continue

Lib/test/test_json/test_unicode.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ def test_ascii_non_printable_decode(self):
4444
'\b\t\n\f\r')
4545
s = ''.join(map(chr, range(32)))
4646
for c in s:
47-
self.assertRaises(self.JSONDecodeError, self.loads, f'"{c}"')
47+
with self.subTest(control_character=ord(c)):
48+
with self.assertRaises(self.JSONDecodeError) as cm:
49+
self.loads(f'"a{c}b"')
50+
error = cm.exception
51+
self.assertEqual(error.pos, 2)
52+
self.assertEqual(error.lineno, 1)
53+
self.assertEqual(error.colno, 3)
4854
self.assertEqual(self.loads(f'"{s}"', strict=False), s)
4955
self.assertEqual(self.loads('"\x7f"'), '\x7f')
5056

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the pure Python :mod:`json` decoder to report the correct position for
2+
invalid literal control characters in JSON strings.

0 commit comments

Comments
 (0)