FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-59598: Ignore leading whitespace in `JSONDecoder.raw_decode` by crazymerlyn · Pull Request #117397 · python/cpython · GitHub

Repository navigation

gh-59598: Ignore leading whitespace in JSONDecoder.raw_decode - #117397

Open
crazymerlyn wants to merge 1 commit into
python:mainfrom
crazymerlyn:fix-issue-59598
Open

crazymerlyn wants to merge 1 commit into
python:mainfrom
crazymerlyn:fix-issue-59598

Conversation

crazymerlyn commented Mar 31, 2024 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown

ghost commented Mar 31, 2024 •
edited by ghost
Loading

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

bedevere-app Bot commented Mar 31, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Two small comments, but overall looks good.

Comment thread Lib/json/decoder.py
Comment thread Lib/json/decoder.py Outdated

bedevere-app Bot commented Apr 22, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Copy Markdown
Author

Updated the code to make the matching function an argument and removed the extra call.

Copy link
Copy Markdown
Contributor

@crazymerlyn Thanks for the updates. Your pr still requires a news entry. The easiest way to add this is to use click on the Details button next to the failing bedevere/news in the list of CI checks.

Also see https://devguide.python.org/core-developers/committing/#how-to-add-a-news-entry

Copy link
Copy Markdown
Author

added a news entry

Copy link
Copy Markdown
Author

@eendebakpt Updated with the suggested change.

eendebakpt commented May 7, 2024 •
edited
Loading

Copy link
Copy Markdown
Contributor

@crazymerlyn I read the corresponding issue and opinions on whether we should make this change or not (at least back in 2016!) are not unanimous. A core developer should decide whether to accept this or not. The code in the PR looks good to me.

Since this PR is not backwards compatible and there has been some discussion it is good to update the documentation (e.g. here https://docs.python.org/3/library/json.html#json.JSONDecoder.raw_decode) and add a Changed in version x.xx section (for an example see https://docs.python.org/3/library/bisect.html#bisect.bisect)

Whitespace is allowed before JSON objects according to RFC 4627.
Comment thread Doc/library/json.rst
Comment on lines +405 to +406
.. versionchanged:: 3.14
Now ignores any leading whitespace instead of returning an error

eendebakpt Jun 19, 2025 •
edited by picnixz
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
.. versionchanged:: 3.14
Now ignores any leading whitespace instead of returning an error
.. versionchanged:: next
Leading whitespaces are now ignored instead of raising an exception.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

whitespace is an uncountable noun, so it shouldn’t be pluralized.

Comment on lines +127 to +138
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"})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
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 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"})

In addition, let's test n1 and n2. And test when there are whitespaces inside the values or keys.

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 16, 2026
gaborbernat added a commit to gaborbernat/cpython that referenced this pull request Jun 3, 2026
Keep only the encoder floatstr deferral here; the decoder whitespace
skip is contentious (depends on pythongh-117397 and shows mixed results on
large documents) and moves to its own PR.
github-actions Bot removed the stale Stale PR or inactive for long period of time. label Sep 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONDecoder.raw_decode breaks on leading whitespace

4 participants


Back | FazBrowse Home | New Git URL