Kaydet (Commit) a873690d authored tarafından Eli Bendersky's avatar Eli Bendersky

The get() and iter() are now able to accept keyword arguments.

In conformance with the documentation and the Python version.
Patch by Franck Michea.
üst 9f6a239c
...@@ -1769,6 +1769,11 @@ class BasicElementTest(unittest.TestCase): ...@@ -1769,6 +1769,11 @@ class BasicElementTest(unittest.TestCase):
self.assertEqual(flag, True) self.assertEqual(flag, True)
self.assertEqual(wref(), None) self.assertEqual(wref(), None)
def test_get_keyword_args(self):
e1 = ET.Element('foo' , x=1, y=2, z=3)
self.assertEqual(e1.get('x', default=7), 1)
self.assertEqual(e1.get('w', default=7), 7)
def test_pickle(self): def test_pickle(self):
# For now this test only works for the Python version of ET, # For now this test only works for the Python version of ET,
# so set sys.modules accordingly because pickle uses __import__ # so set sys.modules accordingly because pickle uses __import__
...@@ -1897,6 +1902,11 @@ class ElementIterTest(unittest.TestCase): ...@@ -1897,6 +1902,11 @@ class ElementIterTest(unittest.TestCase):
self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3) self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3)
self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2) self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2)
# test that iter also accepts 'tag' as a keyword arg
self.assertEqual(
summarize_list(doc.iter(tag='room')),
['room'] * 3)
# make sure both tag=None and tag='*' return all tags # make sure both tag=None and tag='*' return all tags
all_tags = ['document', 'house', 'room', 'room', all_tags = ['document', 'house', 'room', 'room',
'shed', 'house', 'room'] 'shed', 'house', 'room']
......
...@@ -794,6 +794,7 @@ Piotr Meyer ...@@ -794,6 +794,7 @@ Piotr Meyer
Alexis Métaireau Alexis Métaireau
Steven Miale Steven Miale
Trent Mick Trent Mick
Franck Michea
Tom Middleton Tom Middleton
Stan Mihai Stan Mihai
Stefan Mihaila Stefan Mihaila
......
...@@ -1031,13 +1031,16 @@ element_iterfind(ElementObject *self, PyObject *args, PyObject *kwds) ...@@ -1031,13 +1031,16 @@ element_iterfind(ElementObject *self, PyObject *args, PyObject *kwds)
} }
static PyObject* static PyObject*
element_get(ElementObject* self, PyObject* args) element_get(ElementObject* self, PyObject* args, PyObject* kwds)
{ {
PyObject* value; PyObject* value;
static char* kwlist[] = {"key", "default", 0};
PyObject* key; PyObject* key;
PyObject* default_value = Py_None; PyObject* default_value = Py_None;
if (!PyArg_ParseTuple(args, "O|O:get", &key, &default_value))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:get", kwlist, &key,
&default_value))
return NULL; return NULL;
if (!self->extra || self->extra->attrib == Py_None) if (!self->extra || self->extra->attrib == Py_None)
...@@ -1085,10 +1088,12 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext); ...@@ -1085,10 +1088,12 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext);
static PyObject * static PyObject *
element_iter(ElementObject *self, PyObject *args) element_iter(ElementObject *self, PyObject *args, PyObject *kwds)
{ {
PyObject* tag = Py_None; PyObject* tag = Py_None;
if (!PyArg_ParseTuple(args, "|O:iter", &tag)) static char* kwlist[] = {"tag", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:iter", kwlist, &tag))
return NULL; return NULL;
return create_elementiter(self, tag, 0); return create_elementiter(self, tag, 0);
...@@ -1555,7 +1560,7 @@ static PyMethodDef element_methods[] = { ...@@ -1555,7 +1560,7 @@ static PyMethodDef element_methods[] = {
{"clear", (PyCFunction) element_clearmethod, METH_VARARGS}, {"clear", (PyCFunction) element_clearmethod, METH_VARARGS},
{"get", (PyCFunction) element_get, METH_VARARGS}, {"get", (PyCFunction) element_get, METH_VARARGS | METH_KEYWORDS},
{"set", (PyCFunction) element_set, METH_VARARGS}, {"set", (PyCFunction) element_set, METH_VARARGS},
{"find", (PyCFunction) element_find, METH_VARARGS | METH_KEYWORDS}, {"find", (PyCFunction) element_find, METH_VARARGS | METH_KEYWORDS},
...@@ -1567,11 +1572,11 @@ static PyMethodDef element_methods[] = { ...@@ -1567,11 +1572,11 @@ static PyMethodDef element_methods[] = {
{"insert", (PyCFunction) element_insert, METH_VARARGS}, {"insert", (PyCFunction) element_insert, METH_VARARGS},
{"remove", (PyCFunction) element_remove, METH_VARARGS}, {"remove", (PyCFunction) element_remove, METH_VARARGS},
{"iter", (PyCFunction) element_iter, METH_VARARGS}, {"iter", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS},
{"itertext", (PyCFunction) element_itertext, METH_VARARGS}, {"itertext", (PyCFunction) element_itertext, METH_VARARGS},
{"iterfind", (PyCFunction) element_iterfind, METH_VARARGS | METH_KEYWORDS}, {"iterfind", (PyCFunction) element_iterfind, METH_VARARGS | METH_KEYWORDS},
{"getiterator", (PyCFunction) element_iter, METH_VARARGS}, {"getiterator", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS},
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS}, {"getchildren", (PyCFunction) element_getchildren, METH_VARARGS},
{"items", (PyCFunction) element_items, METH_VARARGS}, {"items", (PyCFunction) element_items, METH_VARARGS},
...@@ -3461,7 +3466,7 @@ static PyTypeObject XMLParser_Type = { ...@@ -3461,7 +3466,7 @@ static PyTypeObject XMLParser_Type = {
/* python module interface */ /* python module interface */
static PyMethodDef _functions[] = { static PyMethodDef _functions[] = {
{"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS}, {"SubElement", (PyCFunction) subelement, METH_VARARGS | METH_KEYWORDS},
{NULL, NULL} {NULL, NULL}
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment