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

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

Open
crazymerlyn wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromcrazymerlyn:fix-issue-59598
Open
Show file tree
Hide file tree
Changes fromall 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
3 changes: 3 additions & 0 deletionsDoc/library/json.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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


.. class:: JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Expand Down
10 changes: 7 additions & 3 deletionsLib/json/decoder.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -341,23 +341,27 @@ 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.
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)
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
14 changes: 14 additions & 0 deletionsLib/test/test_json/test_decode.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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"})

class TestPyDecode(TestDecode, PyTest): pass
class TestCDecode(TestDecode, CTest): pass
class TestPyRawDecode(TestRawDecode, PyTest): pass
class TestCRawDecode(TestRawDecode, CTest): pass
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Ignore leading whitespace in :func:`JSONDecoder.raw_decode`
Loading

[8]ページ先頭

©2009-2025 Movatter.jp