Skip to content

Commit

Permalink
Make indent param only accept integers. Fixes #25.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenrobbins committed Sep 20, 2015
1 parent 52117fc commit 2bb9076
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
22 changes: 3 additions & 19 deletions python-rapidjson/rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ rapidjson_dumps(PyObject* self, PyObject* args, PyObject* kwargs)
DatetimeMode datetimeMode = DATETIME_MODE_NONE;

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

This comment has been minimized.

Copy link
@lelit

lelit Dec 4, 2015

Contributor

Why is this an unsigned char? It is assigned below from PyLong_AsLong() and passed to .SetIndent(), both working with (long) integers..

This comment has been minimized.

Copy link
@kenrobbins

kenrobbins Aug 26, 2016

Author Member

just a bug i suppose

This comment has been minimized.

Copy link
@lelit

lelit Aug 27, 2016

Contributor

Ok, I'll tweak it.


static char* kwlist[] = {
"s",
Expand Down Expand Up @@ -733,24 +733,8 @@ rapidjson_dumps(PyObject* self, PyObject* args, PyObject* kwargs)
if (PyLong_Check(indent)) {
indentCharCount = PyLong_AsLong(indent);
}
else if (PyUnicode_Check(indent)) {
char* s = PyUnicode_AsUTF8(indent);

if (s != NULL) {
indentCharCount = strlen(s);
indentChar = *s;
}
}
else if (PyBytes_Check(indent)) {
char* s = PyBytes_AsString(indent);

if (s != NULL) {
indentCharCount = strlen(s);
indentChar = *s;
}
}
else {
PyErr_Format(PyExc_TypeError, "indent must be an int or a string");
PyErr_Format(PyExc_TypeError, "indent must be an int");
return NULL;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def test_indent():

assert rapidjson.dumps(o, indent=4) in expected

with pytest.raises(TypeError):
rapidjson.dumps(o, indent="\t")


@pytest.mark.unit
def test_sort_keys():
Expand Down

0 comments on commit 2bb9076

Please sign in to comment.