Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-59598: Ignore leading whitespace inJSONDecoder.raw_decode
#117397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -402,6 +402,9 @@ Encoders and Decoders | ||||||||||
This can be used to decode a JSON document from a string that may have | ||||||||||
extraneous data at the end. | ||||||||||
.. versionchanged:: 3.14 | ||||||||||
Now ignores any leading whitespace instead of returning an error | ||||||||||
Comment on lines +405 to +406 Contributor
|
..versionchanged::3.14 | |
Now ignores any leading whitespace instead ofreturning anerror | |
..versionchanged::next | |
Leading whitespaces are now ignored instead ofraising anexception. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -341,23 +341,27 @@ def decode(self, s, _w=WHITESPACE.match): | ||
containing a JSON document). | ||
""" | ||
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, _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. | ||
Whitespace at the beginning of the document will be ignored. | ||
Optionally, ``idx`` can be used to specify an offset in ``s`` | ||
where the document begins. | ||
This can be used to decode a JSON document from a string that may | ||
have extraneous data at the end. | ||
""" | ||
try: | ||
obj, end = self.scan_once(s, idx=_w(s, idx).end()) | ||
crazymerlyn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
except StopIteration as err: | ||
raise JSONDecodeError("Expecting value", s, err.value) from None | ||
return obj, end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -124,6 +124,20 @@ def test_limit_int(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
with self.assertRaises(ValueError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.loads('1' * (maxdigits + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestRawDecode: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_whitespace(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
decoder = self.json.JSONDecoder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(decoder.raw_decode(' {}'), ({}, 3)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(decoder.raw_decode(' []'), ([], 4)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(decoder.raw_decode(' ""'), ('', 5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
s = ' { "key" : "value" , "k":"v" } \n' \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
' { "key": "value", "k" :"v"} ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
val1, n1 = decoder.raw_decode(s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
val2, n2 = decoder.raw_decode(s[n1:]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(val1, {"key":"value", "k":"v"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(val2, {"key":"value", "k":"v"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines +127 to +138 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
In addition, let's test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestPyDecode(TestDecode, PyTest): pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestCDecode(TestDecode, CTest): pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestPyRawDecode(TestRawDecode, PyTest): pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestCRawDecode(TestRawDecode, CTest): pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Ignore leading whitespace in :func:`JSONDecoder.raw_decode` |
Uh oh!
There was an error while loading.Please reload this page.