Skip to content
Merged
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 @@ -112,7 +112,7 @@ def py_scanstring(s, end, strict=True,
char = _b[esc]
except KeyError:
msg = "Invalid \\escape: {0!r}".format(esc)
raise JSONDecodeError(msg, s, end)
raise JSONDecodeError(msg, s, end - 1)
end += 1
else:
uni = _decode_uXXXX(s, end)
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_json/test_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ def test_unexpected_data(self):
'%s: line 1 column %d (char %d)' %
(msg, idx + 1, idx))

def test_invalid_escape_position(self):
# The position points to the backslash of the invalid escape.
test_cases = [
('"\\a"', 1),
('"ab\\x"', 3),
('["\\\'"]', 2),
('{"k": "v\\ "}', 8),
]
for data, idx in test_cases:
with self.subTest(data=data):
with self.assertRaises(self.JSONDecodeError) as cm:
self.loads(data)
err = cm.exception
self.assertStartsWith(err.msg, 'Invalid \\escape')
self.assertEqual(err.pos, idx)
self.assertEqual(err.lineno, 1)
self.assertEqual(err.colno, idx + 1)

def test_extra_data(self):
test_cases = [
('[]]', 'Extra data', 2),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the position reported by the pure Python :mod:`json` decoder for an
invalid escape. It now points to the backslash, like the C decoder.
Loading