Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-113149: improve error when json has trailing comma #113227

Merged

Conversation

carsonRadtke
Copy link
Contributor

@carsonRadtke carsonRadtke commented Dec 17, 2023

Fixes: gh-113149

Adds two new JSONDecodeError types: "Illegal trailing comma before end of {array,object}".

Some test's expected error location had to change because the error now exists with the existence of the comma, rather than the non-existence of a following object.

Had to introduce a Py_ssize_t comma_idx; because we want to ensure that

{ "a": 123   ,            } 
             ^ <- error reported here

Behavior after the PR:

$ echo '[,' | ./python.exe -m json.tool
Expecting value: line 1 column 2 (char 1)
$ echo '{,}' | ./python.exe -m json.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
$ echo '[123,]' | ./python.exe -m json.tool
Illegal trailing comma before end of array: line 1 column 5 (char 4)
$ echo '{"a":123,}' | ./python.exe -m json.tool
Illegal trailing comma before end of object: line 1 column 9 (char 8)
$ echo '[123,  ]' | ./python.exe -m json.tool
Illegal trailing comma before end of array: line 1 column 5 (char 4)
$ echo '{"a":123,  }' | ./python.exe -m json.tool
Illegal trailing comma before end of object: line 1 column 9 (char 8)
$ echo '{"spam":42,}' | ./python.exe -m json.tool
Illegal trailing comma before end of object: line 1 column 11 (char 10)

* add news entry
* cjson impl
* pyjson impl
* update tests
@carsonRadtke carsonRadtke changed the title gh-113226: improve error when json has trailing comma gh-113149: improve error when json has trailing comma Dec 17, 2023
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Very good. Please fix style add add test cases with spaces between comma and closing brace.

Modules/_json.c Outdated Show resolved Hide resolved
Modules/_json.c Outdated Show resolved Hide resolved
@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Dec 17, 2023
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka serhiy-storchaka merged commit cfa25fe into python:main Dec 17, 2023
34 checks passed
@serhiy-storchaka
Copy link
Member

Thank you for your contribution @carsonRadtke.

@carsonRadtke carsonRadtke deleted the cradtke/json-trailing-comma branch December 17, 2023 18:58
cjwatson added a commit to cjwatson/singledispatch-json that referenced this pull request Nov 13, 2024
The previously-expected messages and indexes were changed by
python/cpython#113227.

Fixes: domdfcoding#55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improving error message with trailing comma in json
2 participants