@@ -23,42 +23,23 @@ static void rapidxml_DocumentObject_dealloc(rapidxml_DocumentObject* self) {
2323 Py_TYPE (self)->tp_free (reinterpret_cast <PyObject*>(self));
2424}
2525
26- static PyObject* rapidxml_DocumentObject_parse (rapidxml_DocumentObject* self,
27- PyObject* args,
28- PyObject* kwds) {
29- const char * text = NULL ;
30- PyObject* text_obj = NULL ;
31- PyObject* from_file_obj = NULL ;
32- char kw_text[] = " text" ;
33- char kw_from_file[] = " from_file" ;
26+ static int _parse (rapidxml_DocumentObject* self,
27+ Py_buffer* text_buff,
28+ bool from_file) {
29+ const char * text;
3430 std::vector<char > text_vector;
3531
36- static char * kwlist[] = {kw_text, kw_from_file, NULL };
37- if (!PyArg_ParseTupleAndKeywords (args, kwds, " O|O" , kwlist,
38- &text_obj, &from_file_obj)) {
39- return NULL ;
40- }
41-
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- }
32+ text = static_cast <const char *>(text_buff->buf );
33+ if (from_file) {
4734 std::ifstream f (text, std::ios::binary);
4835 if (f.fail ()) {
4936 PyErr_SetString (rapidxml_RapidXmlError, strerror (errno));
50- return NULL ;
37+ return 0 ;
5138 }
5239 text_vector = std::vector<char >((std::istreambuf_iterator<char >(f)),
5340 std::istreambuf_iterator<char >());
5441 text_vector.push_back (0 );
5542 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);
6243 }
6344 try {
6445 self->base .base .document ->clear ();
@@ -67,6 +48,27 @@ static PyObject* rapidxml_DocumentObject_parse(rapidxml_DocumentObject* self,
6748 (self->base .base .document ->allocate_string (text));
6849 } catch (rapidxml::parse_error &e) {
6950 PyErr_SetString (rapidxml_RapidXmlError, e.what ());
51+ return 0 ;
52+ }
53+ return 1 ;
54+ }
55+
56+ static PyObject* rapidxml_DocumentObject_parse (rapidxml_DocumentObject* self,
57+ PyObject* args,
58+ PyObject* kwds) {
59+ Py_buffer text_buff;
60+ PyObject* from_file_obj = NULL ;
61+ char kw_text[] = " text" ;
62+ char kw_from_file[] = " from_file" ;
63+
64+ static char * kwlist[] = {kw_text, kw_from_file, NULL };
65+ if (!PyArg_ParseTupleAndKeywords (args, kwds, " s*|O" , kwlist,
66+ &text_buff, &from_file_obj)) {
67+ return NULL ;
68+ }
69+
70+ if (!_parse (self, &text_buff,
71+ (from_file_obj != NULL ) && PyObject_IsTrue (from_file_obj))) {
7072 return NULL ;
7173 }
7274 Py_INCREF (Py_None);
@@ -76,7 +78,7 @@ static PyObject* rapidxml_DocumentObject_parse(rapidxml_DocumentObject* self,
7678static int rapidxml_DocumentObject_init (rapidxml_DocumentObject* self,
7779 PyObject* args,
7880 PyObject* kwds) {
79- PyObject* text_obj = NULL ;
81+ Py_buffer text_buff = { 0 } ;
8082 PyObject* from_file_obj = NULL ;
8183 char kw_text[] = " text" ;
8284 char kw_from_file[] = " from_file" ;
@@ -85,62 +87,70 @@ static int rapidxml_DocumentObject_init(rapidxml_DocumentObject* self,
8587 return -1 ;
8688 }
8789 static char * kwlist[] = {kw_text, kw_from_file, NULL };
88- if (!PyArg_ParseTupleAndKeywords (args, kwds, " |OO " , kwlist,
89- &text_obj , &from_file_obj)) {
90+ if (!PyArg_ParseTupleAndKeywords (args, kwds, " |s*O " , kwlist,
91+ &text_buff , &from_file_obj)) {
9092 return -1 ;
9193 }
9294 self->base .base .underlying_obj = new rapidxml::xml_document<>();
9395 self->base .base .document = static_cast <rapidxml::xml_document<>*>(self->base .base .underlying_obj );
94- if (text_obj && PyObject_IsTrue (text_obj) ) {
95- rapidxml_DocumentObject_parse (self, args, kwds) ;
96+ if (text_buff. buf ) {
97+ return _parse (self, &text_buff, (from_file_obj != NULL ) && PyObject_IsTrue (from_file_obj)) - 1 ;
9698 }
9799 return 0 ;
98100}
99101
100102static PyObject* rapidxml_DocumentObject_allocate_node (rapidxml_DocumentObject* self,
101103 PyObject* args,
102104 PyObject* kwds) {
103- const char * name = NULL ;
104- const char * value = NULL ;
105+ const char * name;
106+ Py_buffer name_buff = {0 };
107+ const char * value;
108+ Py_buffer value_buff = {0 };
105109 char kw_name[] = " name" ;
106110 char kw_value[] = " value" ;
107111 rapidxml::xml_node<>* node;
108112
109113 static char * kwlist[] = {kw_name, kw_value, NULL };
110- if (!PyArg_ParseTupleAndKeywords (args, kwds, " |ss " , kwlist,
111- &name , &value )) {
114+ if (!PyArg_ParseTupleAndKeywords (args, kwds, " |s*s* " , kwlist,
115+ &name_buff , &value_buff )) {
112116 Py_INCREF (Py_None);
113117 return Py_None;
114118 }
119+ name = static_cast <const char *>(name_buff.buf );
115120 if (name) {
116121 name = self->base .base .document ->allocate_string (name);
117122 }
123+ value = static_cast <const char *>(value_buff.buf );
118124 if (value) {
119125 value = self->base .base .document ->allocate_string (value);
120126 }
121127 node = self->base .base .document ->allocate_node (rapidxml::node_element, name, value);
122128 return _bind_result (reinterpret_cast <rapidxml_BaseObject*>(self),
123129 node, &rapidxml_NodeType);
124130}
125-
131+ # include < iostream >
126132static PyObject* rapidxml_DocumentObject_allocate_attribute (rapidxml_DocumentObject* self,
127133 PyObject* args,
128134 PyObject* kwds) {
129- const char * name = NULL ;
130- const char * value = NULL ;
135+ const char * name;
136+ Py_buffer name_buff = {0 };
137+ const char * value;
138+ Py_buffer value_buff = {0 };
131139 char kw_name[] = " name" ;
132140 char kw_value[] = " value" ;
133141 rapidxml::xml_attribute<>* attribute;
134142
135143 static char * kwlist[] = {kw_name, kw_value, NULL };
136- if (!PyArg_ParseTupleAndKeywords (args, kwds, " |ss " , kwlist,
137- &name , &value )) {
144+ if (!PyArg_ParseTupleAndKeywords (args, kwds, " |s*s* " , kwlist,
145+ &name_buff , &value_buff )) {
138146 Py_INCREF (Py_None);
139147 return Py_None;
140148 }
149+ name = static_cast <const char *>(name_buff.buf );
141150 if (name) {
142151 name = self->base .base .document ->allocate_string (name);
143152 }
153+ value = static_cast <const char *>(value_buff.buf );
144154 if (value) {
145155 value = self->base .base .document ->allocate_string (value);
146156 }
0 commit comments