Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a873690

Browse files
committed
The get() and iter() are now able to accept keyword arguments.
In conformance with the documentation and the Python version. Patch by Franck Michea.
1 parent 9f6a239 commit a873690

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

Lib/test/test_xml_etree.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,11 @@ def wref_cb(w):
17691769
self.assertEqual(flag, True)
17701770
self.assertEqual(wref(), None)
17711771

1772+
def test_get_keyword_args(self):
1773+
e1 = ET.Element('foo' , x=1, y=2, z=3)
1774+
self.assertEqual(e1.get('x', default=7), 1)
1775+
self.assertEqual(e1.get('w', default=7), 7)
1776+
17721777
def test_pickle(self):
17731778
# For now this test only works for the Python version of ET,
17741779
# so set sys.modules accordingly because pickle uses __import__
@@ -1897,6 +1902,11 @@ def test_iter_by_tag(self):
18971902
self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3)
18981903
self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2)
18991904

1905+
# test that iter also accepts 'tag' as a keyword arg
1906+
self.assertEqual(
1907+
summarize_list(doc.iter(tag='room')),
1908+
['room'] * 3)
1909+
19001910
# make sure both tag=None and tag='*' return all tags
19011911
all_tags = ['document', 'house', 'room', 'room',
19021912
'shed', 'house', 'room']

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ Piotr Meyer
794794
Alexis Métaireau
795795
Steven Miale
796796
Trent Mick
797+
Franck Michea
797798
Tom Middleton
798799
Stan Mihai
799800
Stefan Mihaila

Modules/_elementtree.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,16 @@ element_iterfind(ElementObject *self, PyObject *args, PyObject *kwds)
10311031
}
10321032

10331033
static PyObject*
1034-
element_get(ElementObject* self, PyObject* args)
1034+
element_get(ElementObject* self, PyObject* args, PyObject* kwds)
10351035
{
10361036
PyObject* value;
1037+
static char* kwlist[] = {"key", "default", 0};
10371038

10381039
PyObject* key;
10391040
PyObject* default_value = Py_None;
1040-
if (!PyArg_ParseTuple(args, "O|O:get", &key, &default_value))
1041+
1042+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:get", kwlist, &key,
1043+
&default_value))
10411044
return NULL;
10421045

10431046
if (!self->extra || self->extra->attrib == Py_None)
@@ -1085,10 +1088,12 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext);
10851088

10861089

10871090
static PyObject *
1088-
element_iter(ElementObject *self, PyObject *args)
1091+
element_iter(ElementObject *self, PyObject *args, PyObject *kwds)
10891092
{
10901093
PyObject* tag = Py_None;
1091-
if (!PyArg_ParseTuple(args, "|O:iter", &tag))
1094+
static char* kwlist[] = {"tag", 0};
1095+
1096+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:iter", kwlist, &tag))
10921097
return NULL;
10931098

10941099
return create_elementiter(self, tag, 0);
@@ -1555,7 +1560,7 @@ static PyMethodDef element_methods[] = {
15551560

15561561
{"clear", (PyCFunction) element_clearmethod, METH_VARARGS},
15571562

1558-
{"get", (PyCFunction) element_get, METH_VARARGS},
1563+
{"get", (PyCFunction) element_get, METH_VARARGS | METH_KEYWORDS},
15591564
{"set", (PyCFunction) element_set, METH_VARARGS},
15601565

15611566
{"find", (PyCFunction) element_find, METH_VARARGS | METH_KEYWORDS},
@@ -1567,11 +1572,11 @@ static PyMethodDef element_methods[] = {
15671572
{"insert", (PyCFunction) element_insert, METH_VARARGS},
15681573
{"remove", (PyCFunction) element_remove, METH_VARARGS},
15691574

1570-
{"iter", (PyCFunction) element_iter, METH_VARARGS},
1575+
{"iter", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS},
15711576
{"itertext", (PyCFunction) element_itertext, METH_VARARGS},
15721577
{"iterfind", (PyCFunction) element_iterfind, METH_VARARGS | METH_KEYWORDS},
15731578

1574-
{"getiterator", (PyCFunction) element_iter, METH_VARARGS},
1579+
{"getiterator", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS},
15751580
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS},
15761581

15771582
{"items", (PyCFunction) element_items, METH_VARARGS},
@@ -3461,7 +3466,7 @@ static PyTypeObject XMLParser_Type = {
34613466
/* python module interface */
34623467

34633468
static PyMethodDef _functions[] = {
3464-
{"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS},
3469+
{"SubElement", (PyCFunction) subelement, METH_VARARGS | METH_KEYWORDS},
34653470
{NULL, NULL}
34663471
};
34673472

0 commit comments

Comments
 (0)