Skip to content

Commit

Permalink
Merge pull request #39 from lelit/issue38
Browse files Browse the repository at this point in the history
Fix issue #38: null-bytes were not dumped correctly
  • Loading branch information
kenrobbins committed Aug 26, 2016
2 parents 1a00fc2 + d083500 commit 0417017
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions python-rapidjson/rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,15 +908,18 @@ rapidjson_dumps_internal(
writer->Double(d);
}
else if (PyBytes_Check(object)) {
char* s = PyBytes_AsString(object);
if (s == NULL)
Py_ssize_t l;
char* s;

if (PyBytes_AsStringAndSize(object, &s, &l) == -1)
goto error;

writer->String(s);
writer->String(s, l);
}
else if (PyUnicode_Check(object)) {
char* s = PyUnicode_AsUTF8(object);
writer->String(s);
Py_ssize_t l;
char* s = PyUnicode_AsUTF8AndSize(object, &l);
writer->String(s, l);
}
else if (PyList_Check(object)) {
writer->StartArray();
Expand Down
8 changes: 7 additions & 1 deletion tests/test_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@pytest.mark.unit
@pytest.mark.parametrize(
'value', [
'A', 1, -1, 2.3, {'foo': 'bar'}, [1, 2, 'a', 1.2, {'foo': 'bar'},],
'A', 'cruel\x00world', 1, -1, 2.3, {'foo': 'bar'}, [1, 2, 'a', 1.2, {'foo': 'bar'},],
sys.maxsize
])
def test_base_values(value):
Expand All @@ -16,6 +16,12 @@ def test_base_values(value):
assert loaded == value


@pytest.mark.unit
def test_bytes_value():
dumped = rapidjson.dumps(b'cruel\x00world')
assert dumped == r'"cruel\u0000world"'


@pytest.mark.unit
def test_larger_structure():
value = {
Expand Down

0 comments on commit 0417017

Please sign in to comment.