Skip to content

Commit a0e1124

Browse files
author
Arzaroth Lekva
committed
changing arguments type for Document.parse
1 parent db6c1fa commit a0e1124

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

rapidxml/c_ext/src/document_object.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,38 @@ static PyObject* rapidxml_DocumentObject_parse(rapidxml_DocumentObject* self,
2727
PyObject* args,
2828
PyObject* kwds) {
2929
const char* text = NULL;
30-
int from_file = 0;
30+
PyObject* text_obj = NULL;
3131
PyObject* from_file_obj = NULL;
3232
char kw_text[] = "text";
3333
char kw_from_file[] = "from_file";
3434
std::vector<char> text_vector;
35-
PyObject* res;
3635

3736
static char* kwlist[] = {kw_text, kw_from_file, NULL};
38-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|O", kwlist,
39-
&text, &from_file_obj)) {
40-
goto fail;
41-
}
42-
if (from_file_obj) {
43-
from_file = PyObject_IsTrue(from_file_obj);
37+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
38+
&text_obj, &from_file_obj)) {
39+
return NULL;
4440
}
4541

46-
if (from_file) {
42+
if ((from_file_obj != NULL) && PyObject_IsTrue(from_file_obj)) {
43+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|O", kwlist,
44+
&text, &from_file_obj)) {
45+
return NULL;
46+
}
4747
std::ifstream f(text, std::ios::binary);
4848
if (f.fail()) {
4949
PyErr_SetString(rapidxml_RapidXmlError, strerror(errno));
50-
goto fail;
50+
return NULL;
5151
}
5252
text_vector = std::vector<char>((std::istreambuf_iterator<char>(f)),
5353
std::istreambuf_iterator<char>());
5454
text_vector.push_back(0);
5555
text = &text_vector[0];
56+
} else {
57+
if (!PyByteArray_Check(text_obj)) {
58+
PyErr_SetString(PyExc_TypeError, "argument 1 must be bytearray");
59+
return NULL;
60+
}
61+
text = PyByteArray_AsString(text_obj);
5662
}
5763
try {
5864
self->base.base.document->clear();
@@ -61,21 +67,16 @@ static PyObject* rapidxml_DocumentObject_parse(rapidxml_DocumentObject* self,
6167
(self->base.base.document->allocate_string(text));
6268
} catch (rapidxml::parse_error &e) {
6369
PyErr_SetString(rapidxml_RapidXmlError, e.what());
64-
goto fail;
70+
return NULL;
6571
}
66-
res = Py_True;
67-
goto end;
68-
fail:
69-
res = Py_False;
70-
end:
71-
Py_INCREF(res);
72-
return res;
72+
Py_INCREF(Py_None);
73+
return Py_None;
7374
}
7475

7576
static int rapidxml_DocumentObject_init(rapidxml_DocumentObject* self,
7677
PyObject* args,
7778
PyObject* kwds) {
78-
const char* text = NULL;
79+
PyObject* text_obj = NULL;
7980
PyObject* from_file_obj = NULL;
8081
char kw_text[] = "text";
8182
char kw_from_file[] = "from_file";
@@ -84,13 +85,13 @@ static int rapidxml_DocumentObject_init(rapidxml_DocumentObject* self,
8485
return -1;
8586
}
8687
static char* kwlist[] = {kw_text, kw_from_file, NULL};
87-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO", kwlist,
88-
&text, &from_file_obj)) {
88+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
89+
&text_obj, &from_file_obj)) {
8990
return -1;
9091
}
9192
self->base.base.underlying_obj = new rapidxml::xml_document<>();
9293
self->base.base.document = static_cast<rapidxml::xml_document<>*>(self->base.base.underlying_obj);
93-
if (text && *text) {
94+
if (text_obj && PyObject_IsTrue(text_obj)) {
9495
rapidxml_DocumentObject_parse(self, args, kwds);
9596
}
9697
return 0;

rapidxml/c_ext/src/node_object.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,12 @@ static PyObject* rapidxml_NodeObject_unparse(rapidxml_NodeObject* self,
410410
char kw_pretty[] = "pretty";
411411

412412
static char* kwlist[] = {kw_pretty, NULL};
413-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &pretty_obj)) {
414-
pretty = 0;
415-
} else if (pretty_obj) {
416-
pretty = PyObject_IsTrue(pretty_obj);
417-
}
413+
PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &pretty_obj);
414+
418415
rapidxml::print(std::back_inserter(xml),
419416
*(static_cast<rapidxml::xml_node<>*>(self->base.underlying_obj)),
420-
!pretty ? rapidxml::print_no_indenting : 0);
417+
((pretty_obj == NULL) || PyObject_Not(pretty_obj))
418+
? rapidxml::print_no_indenting : 0);
421419
return Py_BuildValue("s", xml.c_str());
422420
}
423421

0 commit comments

Comments
 (0)