From 53edfd8083ff108611b5d562445cc956aab515bd Mon Sep 17 00:00:00 2001 From: Mayank Basena <0mayankbasena@gmail.com> Date: Sat, 4 Jul 2026 16:51:03 +0530 Subject: [PATCH] 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. --- Lib/json/decoder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index 364e44d40cc3073..51199f5cf0ff271 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -355,13 +355,13 @@ def decode(self, s, _w=WHITESPACE.match): containing a JSON document). """ - obj, end = self.raw_decode(s, idx=_w(s, 0).end()) + obj, end = self.raw_decode(s) end = _w(s, end).end() if end != len(s): raise JSONDecodeError("Extra data", s, end) return obj - def raw_decode(self, s, idx=0): + def raw_decode(self, s, idx=0, _w=WHITESPACE.match): """Decode a JSON document from ``s`` (a ``str`` beginning with a JSON document) and return a 2-tuple of the Python representation and the index in ``s`` where the document ended. @@ -371,7 +371,7 @@ def raw_decode(self, s, idx=0): """ try: - obj, end = self.scan_once(s, idx) + obj, end = self.scan_once(s, idx=_w(s, idx).end()) except StopIteration as err: raise JSONDecodeError("Expecting value", s, err.value) from None return obj, end