-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
bpo-31505: Fix an assertion failure in json, in case _json.make_encoder() received a bad encoder() argument #3643
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
Merged
serhiy-storchaka
merged 5 commits into
python:master
from
orenmn:bpo31505-fix-assert-fails
Sep 24, 2017
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,27 @@ def test_make_encoder(self): | |
| b"\xCD\x7D\x3D\x4E\x12\x4C\xF9\x79\xD7\x52\xBA\x82\xF2\x27\x4A\x7D\xA0\xCA\x75", | ||
| None) | ||
|
|
||
| def test_bad_str_encoder(self): | ||
| # Issue #31505: There shouldn't be an assertion failure in case | ||
| # c_make_encoder() receives a bad encoder() argument. | ||
| def bad_encoder1(*args): | ||
| return None | ||
| enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj), | ||
| bad_encoder1, None, ': ', ', ', | ||
| False, False, False) | ||
| with self.assertRaises(TypeError): | ||
| enc('spam', 4) | ||
|
Member
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. This can be written without context manager: I prefer this form for simple function calls. |
||
| with self.assertRaises(TypeError): | ||
| enc({'spam': 42}, 4) | ||
|
|
||
| def bad_encoder2(*args): | ||
| 1/0 | ||
| enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj), | ||
| bad_encoder2, None, ': ', ', ', | ||
| False, False, False) | ||
| with self.assertRaises(ZeroDivisionError): | ||
| enc('spam', 4) | ||
|
|
||
| def test_bad_bool_args(self): | ||
| def test(name): | ||
| self.json.encoder.JSONEncoder(**{name: BadBool()}).encode({'a': 1}) | ||
|
|
||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core and Builtins/2017-09-18-12-07-39.bpo-31505.VomaFa.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix an assertion failure in `json`, in case `_json.make_encoder()` received | ||
| a bad `encoder()` argument. Patch by Oren Milman. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't related to this issue, but it seems to me that TypeError is raised for different cause than was originally in this test. This test doesn't work as intended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, Victor wrote this test to verify that the interpreter doesn't crash (as part of https://bugs.python.org/issue6986, as originally
PyArg_ParseTupleAndKeywords()was given actual fields of the newPyEncoderObject, instead of temp vars), so raising a TypeError is the wanted outcome here.maybe we can just change the name of the test to 'test_issue6986' (as it tests only this specific issue)?
and maybe do the same for
test_make_scanner()?anyway, I guess any such change should be in a PR referring to bpo-6986..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems your are right. Thank you for a reference.