Skip to content

Commit 53edfd8

Browse files
committed
gh-59598: Ignore leading whitespace in JSONDecoder.raw_decode
raw_decode now ignores leading whitespace before the JSON value, matching the behavior of decode(). Previously, passing a string like ' {}' to raw_decode would raise JSONDecodeError. The fix uses the existing WHITESPACE regex to skip whitespace at any offset, preserving correct behavior with the idx parameter.
1 parent cfe3e07 commit 53edfd8

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

Lib/json/decoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ def decode(self, s, _w=WHITESPACE.match):
355355
containing a JSON document).
356356
357357
"""
358-
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
358+
obj, end = self.raw_decode(s)
359359
end = _w(s, end).end()
360360
if end != len(s):
361361
raise JSONDecodeError("Extra data", s, end)
362362
return obj
363363

364-
def raw_decode(self, s, idx=0):
364+
def raw_decode(self, s, idx=0, _w=WHITESPACE.match):
365365
"""Decode a JSON document from ``s`` (a ``str`` beginning with
366366
a JSON document) and return a 2-tuple of the Python
367367
representation and the index in ``s`` where the document ended.
@@ -371,7 +371,7 @@ def raw_decode(self, s, idx=0):
371371
372372
"""
373373
try:
374-
obj, end = self.scan_once(s, idx)
374+
obj, end = self.scan_once(s, idx=_w(s, idx).end())
375375
except StopIteration as err:
376376
raise JSONDecodeError("Expecting value", s, err.value) from None
377377
return obj, end

0 commit comments

Comments
 (0)