forked from isagalaev/ijson
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check incoming values in yajl2_c backend
When events are sent to the yajl2_c backend, we assumed the value sent by the user was a tuple of the correct length. This is usually correct, but it's wrong to rely on it blindly. This commit adds the necessary checks on input values sent to the send() method of the *_basecoro generators of the yajl2_c backend. The python-based backends already perform these checks by using the target list assignment syntax (e.g., `a, b = c`) to receive any values sent to them. The checks in the yajl2_c backend are performed using a new ijson_unpack utility function, which mimics the behaviour of target list assignments, performing the same cardinality checks, and raising the same exceptions when things go wrong. Note that the Python C API doesn't offer an equivalent utility function, and the closest alternative (ensuring a PyTuple, then invoking PyArg_UnpackTuple) doesn't yield the same error messages and error types (TypeError v/s ValueError). With this behaviour in place, we can now reliably test across all backends that incorrect inputs raise certain exceptions. Signed-off-by: Rodrigo Tobar <[email protected]>
- Loading branch information
Showing
8 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,9 +1,25 @@ | ||
"""Tests for the ijson.parse method""" | ||
|
||
import pytest | ||
|
||
from .test_base import ARRAY_JSON, ARRAY_JSON_PARSE_EVENTS, JSON, JSON_PARSE_EVENTS | ||
|
||
def test_parse(adaptor): | ||
assert JSON_PARSE_EVENTS == adaptor.parse(JSON) | ||
|
||
def test_parse_array(adaptor): | ||
assert ARRAY_JSON_PARSE_EVENTS == adaptor.parse(ARRAY_JSON) | ||
|
||
def test_coro_needs_input_with_two_elements(backend): | ||
int_element_basic_parse_events = list(backend.basic_parse(b'0', use_float=True)) | ||
# all good | ||
assert [('', 'number', 0)] == list(backend.parse(int_element_basic_parse_events)) | ||
# one more element in event | ||
with pytest.raises(ValueError, match="too many values"): | ||
next(backend.parse(event + ('extra dummy',) for event in int_element_basic_parse_events)) | ||
# one less | ||
with pytest.raises(ValueError, match="not enough values"): | ||
next(backend.parse(event[:-1] for event in int_element_basic_parse_events)) | ||
# not an iterable | ||
with pytest.raises(TypeError, match="cannot unpack"): | ||
next(backend.parse([None])) |