Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b43c76

Browse files
committed
gh-59598: Ignore leading whitespace inJSONDecoder.raw_decode
Whitespace is allowed before JSON objects according to RFC 4627.
1 parentbfc57d4 commit6b43c76

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

‎Lib/json/decoder.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,23 +341,27 @@ def decode(self, s, _w=WHITESPACE.match):
341341
containing a JSON document).
342342
343343
"""
344-
obj,end=self.raw_decode(s,idx=_w(s,0).end())
344+
obj,end=self.raw_decode(s)
345345
end=_w(s,end).end()
346346
ifend!=len(s):
347347
raiseJSONDecodeError("Extra data",s,end)
348348
returnobj
349349

350-
defraw_decode(self,s,idx=0):
350+
defraw_decode(self,s,idx=0,_w=WHITESPACE.match):
351351
"""Decode a JSON document from ``s`` (a ``str`` beginning with
352352
a JSON document) and return a 2-tuple of the Python
353353
representation and the index in ``s`` where the document ended.
354+
Whitespace at the beginning of the document will be ignored.
355+
356+
Optionally, ``idx`` can be used to specify an offset in ``s``
357+
where the document begins.
354358
355359
This can be used to decode a JSON document from a string that may
356360
have extraneous data at the end.
357361
358362
"""
359363
try:
360-
obj,end=self.scan_once(s,idx)
364+
obj,end=self.scan_once(s,idx=_w(s,idx).end())
361365
exceptStopIterationaserr:
362366
raiseJSONDecodeError("Expecting value",s,err.value)fromNone
363367
returnobj,end

‎Lib/test/test_json/test_decode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ def test_limit_int(self):
103103
withself.assertRaises(ValueError):
104104
self.loads('1'* (maxdigits+1))
105105

106+
classTestRawDecode:
107+
deftest_whitespace(self):
108+
decoder=self.json.JSONDecoder()
109+
self.assertEqual(decoder.raw_decode(' {}'), ({},3))
110+
self.assertEqual(decoder.raw_decode(' []'), ([],4))
111+
self.assertEqual(decoder.raw_decode(' ""'), ('',5))
112+
s=' { "key" : "value" , "k":"v" }\n' \
113+
' { "key": "value", "k" :"v"} '
114+
val1,n1=decoder.raw_decode(s)
115+
val2,n2=decoder.raw_decode(s[n1:])
116+
self.assertEqual(val1, {"key":"value","k":"v"})
117+
self.assertEqual(val2, {"key":"value","k":"v"})
106118

107119
classTestPyDecode(TestDecode,PyTest):pass
108120
classTestCDecode(TestDecode,CTest):pass
121+
classTestPyRawDecode(TestRawDecode,PyTest):pass
122+
classTestCRawDecode(TestRawDecode,CTest):pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ignore leading whitespace in:func:`JSONDecoder.raw_decode`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp