@@ -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
7576static 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 ;
0 commit comments