Skip to content

Commit 3af43b6

Browse files
author
Arzaroth Lekva
committed
fix argparsing & py3.5/py3.6 compat
1 parent 292b9fb commit 3af43b6

9 files changed

+61
-73
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ with open('dump.xml', 'w') as f:
2020
f.write(str(r))
2121
r = rapidxml.RapidXml("dump.xml", from_file=True) # loading from file
2222

23-
assert(str(r) == r.unparse(True, False)) # is always True
23+
assert(str(r) == r.unparse(pretty=True, raw=False)) # is always True
2424
assert(repr(r) == r.unparse(pretty=False, raw=False)) # also always True
2525
```
2626

rapidxml/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
__all__ = [
1212
'RapidXml',
1313
'DictNode',
14-
]
14+
]

rapidxml/c_ext/src/base_object.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ static PyObject* rapidxml_BaseObject_copy(rapidxml_BaseObject* self,
4040
PyObject* args,
4141
PyObject* kwds) {
4242
PyObject* other = NULL;
43-
char kw_other[] = "other";
4443

45-
static char* kwlist[] = {kw_other, NULL};
44+
static char* kwlist[] = {"other", NULL};
4645
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
4746
&other)) {
4847
return NULL;

rapidxml/c_ext/src/common.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
int _parse_args_for_name(PyObject* args,
1717
PyObject* kwds,
1818
const char** name) {
19-
char kw_name[] = "name";
2019
Py_buffer buf = {0};
2120

22-
static char* kwlist[] = {kw_name, NULL};
21+
static char* kwlist[] = {"name", NULL};
2322
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s*", kwlist,
2423
&buf)) {
2524
return false;

rapidxml/c_ext/src/document_object.cpp

+4-14
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ static PyObject* rapidxml_DocumentObject_parse(rapidxml_DocumentObject* self,
6565
Py_buffer text_buff;
6666
PyObject* from_file_obj = NULL;
6767
PyObject* read_cdata = NULL;
68-
char kw_text[] = "text";
69-
char kw_from_file[] = "from_file";
70-
char kw_parse_cdata[] = "parse_cdata";
7168

72-
static char* kwlist[] = {kw_text, kw_from_file, kw_parse_cdata, NULL};
69+
static char* kwlist[] = {"text", "from_file", "parse_cdata", NULL};
7370
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s*|OO", kwlist,
7471
&text_buff, &from_file_obj, &read_cdata)) {
7572
return NULL;
@@ -90,14 +87,11 @@ static int rapidxml_DocumentObject_init(rapidxml_DocumentObject* self,
9087
Py_buffer text_buff = {0};
9188
PyObject* from_file_obj = NULL;
9289
PyObject* read_cdata = NULL;
93-
char kw_text[] = "text";
94-
char kw_from_file[] = "from_file";
95-
char kw_parse_cdata[] = "parse_cdata";
9690

9791
if (rapidxml_NodeType.tp_init(reinterpret_cast<PyObject*>(self), args, kwds) < 0) {
9892
return -1;
9993
}
100-
static char* kwlist[] = {kw_text, kw_from_file, kw_parse_cdata, NULL};
94+
static char* kwlist[] = {"text", "from_file", "parse_cdata", NULL};
10195
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s*OO", kwlist,
10296
&text_buff, &from_file_obj, &read_cdata)) {
10397
return -1;
@@ -120,11 +114,9 @@ static PyObject* rapidxml_DocumentObject_allocate_node(rapidxml_DocumentObject*
120114
Py_buffer name_buff = {0};
121115
const char* value;
122116
Py_buffer value_buff = {0};
123-
char kw_name[] = "name";
124-
char kw_value[] = "value";
125117
rapidxml::xml_node<>* node;
126118

127-
static char* kwlist[] = {kw_name, kw_value, NULL};
119+
static char* kwlist[] = {"name", "value", NULL};
128120
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s*s*", kwlist,
129121
&name_buff, &value_buff)) {
130122
Py_INCREF(Py_None);
@@ -150,11 +142,9 @@ static PyObject* rapidxml_DocumentObject_allocate_attribute(rapidxml_DocumentObj
150142
Py_buffer name_buff = {0};
151143
const char* value;
152144
Py_buffer value_buff = {0};
153-
char kw_name[] = "name";
154-
char kw_value[] = "value";
155145
rapidxml::xml_attribute<>* attribute;
156146

157-
static char* kwlist[] = {kw_name, kw_value, NULL};
147+
static char* kwlist[] = {"name", "value", NULL};
158148
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s*s*", kwlist,
159149
&name_buff, &value_buff)) {
160150
Py_INCREF(Py_None);

rapidxml/c_ext/src/node_object.cpp

+38-48
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ static PyObject* rapidxml_NodeObject_last_attribute(rapidxml_NodeObject* self,
164164
static PyObject* rapidxml_NodeObject_prepend_node(rapidxml_NodeObject* self,
165165
PyObject* args,
166166
PyObject* kwds) {
167-
char kw_node[] = "node";
168167
PyObject* node = NULL;
169168

170-
static char* kwlist[] = {kw_node, NULL};
169+
static char* kwlist[] = {"node", NULL};
171170
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
172171
&node)) {
173172
return NULL;
@@ -186,10 +185,9 @@ static PyObject* rapidxml_NodeObject_prepend_node(rapidxml_NodeObject* self,
186185
static PyObject* rapidxml_NodeObject_append_node(rapidxml_NodeObject* self,
187186
PyObject* args,
188187
PyObject* kwds) {
189-
char kw_node[] = "node";
190188
PyObject* node = NULL;
191189

192-
static char* kwlist[] = {kw_node, NULL};
190+
static char* kwlist[] = {"node", NULL};
193191
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
194192
&node)) {
195193
return NULL;
@@ -208,12 +206,10 @@ static PyObject* rapidxml_NodeObject_append_node(rapidxml_NodeObject* self,
208206
static PyObject* rapidxml_NodeObject_insert_node(rapidxml_NodeObject* self,
209207
PyObject* args,
210208
PyObject* kwds) {
211-
char kw_where[] = "where";
212-
char kw_node[] = "node";
213209
PyObject* where = NULL;
214210
PyObject* node = NULL;
215211

216-
static char* kwlist[] = {kw_where, kw_node, NULL};
212+
static char* kwlist[] = {"where", "node", NULL};
217213
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
218214
&where, &node)) {
219215
return NULL;
@@ -254,10 +250,9 @@ static PyObject* rapidxml_NodeObject_remove_last_node(rapidxml_NodeObject* self,
254250
static PyObject* rapidxml_NodeObject_remove_node(rapidxml_NodeObject* self,
255251
PyObject* args,
256252
PyObject* kwds) {
257-
char kw_node[] = "node";
258253
PyObject* node = NULL;
259254

260-
static char* kwlist[] = {kw_node, NULL};
255+
static char* kwlist[] = {"node", NULL};
261256
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
262257
&node)) {
263258
return NULL;
@@ -284,10 +279,9 @@ static PyObject* rapidxml_NodeObject_remove_all_nodes(rapidxml_NodeObject* self,
284279
static PyObject* rapidxml_NodeObject_prepend_attribute(rapidxml_NodeObject* self,
285280
PyObject* args,
286281
PyObject* kwds) {
287-
char kw_attribute[] = "attribute";
288282
PyObject* attribute = NULL;
289283

290-
static char* kwlist[] = {kw_attribute, NULL};
284+
static char* kwlist[] = {"attribute", NULL};
291285
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
292286
&attribute)) {
293287
return NULL;
@@ -306,10 +300,9 @@ static PyObject* rapidxml_NodeObject_prepend_attribute(rapidxml_NodeObject* self
306300
static PyObject* rapidxml_NodeObject_append_attribute(rapidxml_NodeObject* self,
307301
PyObject* args,
308302
PyObject* kwds) {
309-
char kw_attribute[] = "attribute";
310303
PyObject* attribute = NULL;
311304

312-
static char* kwlist[] = {kw_attribute, NULL};
305+
static char* kwlist[] = {"attribute", NULL};
313306
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
314307
&attribute)) {
315308
return NULL;
@@ -328,12 +321,10 @@ static PyObject* rapidxml_NodeObject_append_attribute(rapidxml_NodeObject* self,
328321
static PyObject* rapidxml_NodeObject_insert_attribute(rapidxml_NodeObject* self,
329322
PyObject* args,
330323
PyObject* kwds) {
331-
char kw_where[] = "where";
332-
char kw_attribute[] = "attribute";
333324
PyObject* where = NULL;
334325
PyObject* attribute = NULL;
335326

336-
static char* kwlist[] = {kw_where, kw_attribute, NULL};
327+
static char* kwlist[] = {"where", "attribute", NULL};
337328
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
338329
&where, &attribute)) {
339330
return NULL;
@@ -374,10 +365,9 @@ static PyObject* rapidxml_NodeObject_remove_last_attribute(rapidxml_NodeObject*
374365
static PyObject* rapidxml_NodeObject_remove_attribute(rapidxml_NodeObject* self,
375366
PyObject* args,
376367
PyObject* kwds) {
377-
char kw_attribute[] = "attribute";
378368
PyObject* attribute = NULL;
379369

380-
static char* kwlist[] = {kw_attribute, NULL};
370+
static char* kwlist[] = {"attribute", NULL};
381371
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist,
382372
&attribute)) {
383373
return NULL;
@@ -401,27 +391,16 @@ static PyObject* rapidxml_NodeObject_remove_all_attributes(rapidxml_NodeObject*
401391
return Py_None;
402392
}
403393

404-
static PyObject* rapidxml_NodeObject_unparse(rapidxml_NodeObject* self,
405-
PyObject* args,
406-
PyObject* kwds) {
407-
PyObject* pretty_obj = NULL;
408-
PyObject* raw_obj = NULL;
409-
PyObject* res;
394+
static PyObject* _unparse(rapidxml_NodeObject* self,
395+
bool pretty,
396+
bool raw) {
397+
PyObject* res = NULL;
410398
std::string xml;
411-
char kw_pretty[] = "pretty";
412-
char kw_raw[] = "raw";
413-
414-
static char* kwlist[] = {kw_pretty, kw_raw, NULL};
415-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
416-
&pretty_obj, &raw_obj)) {
417-
return NULL;
418-
}
419399

420400
rapidxml::print(std::back_inserter(xml),
421401
*(static_cast<rapidxml::xml_node<>*>(self->base.underlying_obj)),
422-
((pretty_obj == NULL) || PyObject_Not(pretty_obj))
423-
? rapidxml::print_no_indenting : 0);
424-
if ((raw_obj == NULL) || PyObject_Not(raw_obj)
402+
!pretty ? rapidxml::print_no_indenting : 0);
403+
if (!raw
425404
#if PY_MAJOR_VERSION < 3
426405
|| true
427406
#endif
@@ -433,24 +412,35 @@ static PyObject* rapidxml_NodeObject_unparse(rapidxml_NodeObject* self,
433412
return res;
434413
}
435414

436-
static PyObject* rapidxml_NodeObject___str__(rapidxml_NodeObject* self) {
437-
PyObject* args;
438-
PyObject* res;
415+
static PyObject* rapidxml_NodeObject_unparse(rapidxml_NodeObject* self,
416+
PyObject* args,
417+
PyObject* kwds) {
418+
PyObject* pretty_obj = NULL;
419+
PyObject* raw_obj = NULL;
420+
PyObject* res = NULL;
421+
std::string xml;
439422

440-
args = Py_BuildValue("(O)", Py_True);
441-
res = rapidxml_NodeObject_unparse(self, args, NULL);
442-
Py_DECREF(args);
423+
static char* kwlist[] = {"pretty", "raw", NULL};
424+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:unparse", kwlist,
425+
&pretty_obj, &raw_obj)) {
426+
return NULL;
427+
}
428+
Py_XINCREF(pretty_obj);
429+
Py_XINCREF(raw_obj);
430+
res = _unparse(self,
431+
(pretty_obj != NULL && PyObject_IsTrue(pretty_obj)),
432+
(raw_obj != NULL && PyObject_IsTrue(raw_obj)));
433+
Py_XDECREF(pretty_obj);
434+
Py_XDECREF(raw_obj);
443435
return res;
444436
}
445437

446-
static PyObject* rapidxml_NodeObject___repr__(rapidxml_NodeObject* self) {
447-
PyObject* args;
448-
PyObject* res;
438+
static PyObject* rapidxml_NodeObject___str__(rapidxml_NodeObject* self) {
439+
return _unparse(self, true, false);
440+
}
449441

450-
args = Py_BuildValue("()");
451-
res = rapidxml_NodeObject_unparse(self, args, NULL);
452-
Py_DECREF(args);
453-
return res;
442+
static PyObject* rapidxml_NodeObject___repr__(rapidxml_NodeObject* self) {
443+
return _unparse(self, false, false);
454444
}
455445

456446
static PyObject* rapidxml_NodeObject_children(rapidxml_NodeObject* self,

setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
77
#
88

9-
from setuptools import setup, Extension, find_packages
9+
from setuptools import setup, find_packages
10+
from setuptools import Extension
1011

1112
VERSION = ("2", "1", "0")
1213

@@ -63,5 +64,7 @@
6364
"Programming Language :: Python :: 3",
6465
"Programming Language :: Python :: 3.3",
6566
"Programming Language :: Python :: 3.4",
67+
"Programming Language :: Python :: 3.5",
68+
"Programming Language :: Python :: 3.6",
6669
],
6770
)

test-requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
py==1.4.26
2-
pytest==2.7.0
1+
py==1.4.33
2+
pytest==3.1.0

tests/test_basic.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99
import os
1010
import rapidxml
1111

12-
def test_init(init_rapidxml):
12+
def test_unparse(init_rapidxml):
1313
assert init_rapidxml.unparse() == ('<root><test attr1="one" attr2="two" attr3="three"/>'
1414
'<test2><node id="1"/><node id="2"/><node id="3"/></test2>'
1515
'<test>some text</test></root>')
1616
assert init_rapidxml.unparse() == repr(init_rapidxml)
17+
assert init_rapidxml.unparse(False, False) == repr(init_rapidxml)
18+
assert init_rapidxml.unparse(raw=False) == repr(init_rapidxml)
19+
assert init_rapidxml.unparse(pretty=False) == repr(init_rapidxml)
20+
assert init_rapidxml.unparse(pretty=False, raw=False) == repr(init_rapidxml)
1721
assert init_rapidxml.unparse(True) == str(init_rapidxml)
22+
assert init_rapidxml.unparse(True, False) == str(init_rapidxml)
23+
assert init_rapidxml.unparse(pretty=True) == str(init_rapidxml)
24+
assert init_rapidxml.unparse(pretty=True, raw=False) == str(init_rapidxml)
25+
assert init_rapidxml.unparse(True, raw=False) == str(init_rapidxml)
1826

1927
def test_parse(init_rapidxml):
2028
r = rapidxml.RapidXml()
@@ -115,4 +123,3 @@ def test_assign_cdata(init_rapidxml_with_CDADA):
115123
test = root.first_node("test")
116124
test.value = "some new text"
117125
assert test.value == "some new text"
118-

0 commit comments

Comments
 (0)