Skip to content

Commit

Permalink
Merge pull request #41 from lelit/tweak-issue25
Browse files Browse the repository at this point in the history
Declare indentCharCount as an unsigned integer
  • Loading branch information
kenrobbins committed Aug 27, 2016
2 parents 0417017 + 79571d0 commit 747b8cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions python-rapidjson/rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ rapidjson_dumps(PyObject* self, PyObject* args, PyObject* kwargs)

bool prettyPrint = false;
const char indentChar = ' ';
unsigned char indentCharCount = 4;
unsigned indentCharCount = 4;

static char* kwlist[] = {
"obj",
Expand Down Expand Up @@ -1199,11 +1199,11 @@ rapidjson_dumps(PyObject* self, PyObject* args, PyObject* kwargs)
if (indent && indent != Py_None) {
prettyPrint = true;

if (PyLong_Check(indent)) {
indentCharCount = PyLong_AsLong(indent);
if (PyLong_Check(indent) && PyLong_AsLong(indent) >= 0) {
indentCharCount = PyLong_AsUnsignedLong(indent);
}
else {
PyErr_SetString(PyExc_TypeError, "indent must be an int");
PyErr_SetString(PyExc_TypeError, "indent must be a non-negative int");
return NULL;
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@ def test_indent():
with pytest.raises(TypeError):
rapidjson.dumps(o, indent="\t")

with pytest.raises(TypeError):
rapidjson.dumps(o, indent=-1)


@pytest.mark.unit
def test_sort_keys():
o = {"a": 1, "z": 2, "b": 3}
expected0 = '{\n"a": 1,\n"b": 3,\n"z": 2\n}'
expected1 = '{"a":1,"b":3,"z":2}'
expected2 = '{\n "a": 1,\n "b": 3,\n "z": 2\n}'

assert rapidjson.dumps(o, sort_keys=True) == expected1
assert rapidjson.dumps(o, sort_keys=True, indent=4) == expected2
assert rapidjson.dumps(o, sort_keys=True, indent=0) == expected0


@pytest.mark.unit
Expand Down

0 comments on commit 747b8cd

Please sign in to comment.