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: 2 additions & 0 deletions Lib/json/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def _scan_once(string, idx):
raise StopIteration(idx)

def scan_once(string, idx):
if idx < 0:
raise ValueError('idx cannot be negative')
try:
return _scan_once(string, idx)
finally:
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_json/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def test_string_with_utf8_bom(self):
def test_negative_index(self):
d = self.json.JSONDecoder()
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
for doc, idx in (('1', -1), ('[1]', -1), ('[1]', -3), ('"a"', -3)):
with self.subTest(doc=doc, idx=idx):
with self.assertRaisesRegex(ValueError,
'idx cannot be negative'):
d.raw_decode(doc, idx)

def test_unterminated_string(self):
d = self.json.JSONDecoder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The pure Python :mod:`json` decoder now raises :exc:`ValueError` for a
negative *idx* in :meth:`~json.JSONDecoder.raw_decode`, like the C decoder.
Loading