summaryrefslogtreecommitdiffstats
path: root/Modules/_elementtree.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-05-22 14:07:51 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-05-22 14:07:51 (GMT)
commit66d53fa9ad846a401292eec622a6a98983bed578 (patch)
tree210b779b9e53f5372a645014459aa98145177185 /Modules/_elementtree.c
parent9e62d35e656ff2c8c3343943a9b7b5e5bf39ee2e (diff)
downloadcpython-66d53fa9ad846a401292eec622a6a98983bed578.zip
cpython-66d53fa9ad846a401292eec622a6a98983bed578.tar.gz
cpython-66d53fa9ad846a401292eec622a6a98983bed578.tar.bz2
Issue #16986: ElementTree now correctly parses a string input not only when
an internal XML encoding is UTF-8 or US-ASCII.
Diffstat (limited to 'Modules/_elementtree.c')
-rw-r--r--Modules/_elementtree.c39
1 files changed, 30 insertions, 9 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 029ff7b..5078b83 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3330,7 +3330,7 @@ xmlparser_dealloc(XMLParserObject* self)
}
LOCAL(PyObject*)
-expat_parse(XMLParserObject* self, char* data, int data_len, int final)
+expat_parse(XMLParserObject* self, const char* data, int data_len, int final)
{
int ok;
@@ -3376,16 +3376,37 @@ xmlparser_close(XMLParserObject* self, PyObject* args)
}
static PyObject*
-xmlparser_feed(XMLParserObject* self, PyObject* args)
+xmlparser_feed(XMLParserObject* self, PyObject* arg)
{
/* feed data to parser */
- char* data;
- int data_len;
- if (!PyArg_ParseTuple(args, "s#:feed", &data, &data_len))
- return NULL;
-
- return expat_parse(self, data, data_len, 0);
+ if (PyUnicode_Check(arg)) {
+ Py_ssize_t data_len;
+ const char *data = PyUnicode_AsUTF8AndSize(arg, &data_len);
+ if (data == NULL)
+ return NULL;
+ if (data_len > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError, "size does not fit in an int");
+ return NULL;
+ }
+ /* Explicitly set UTF-8 encoding. Return code ignored. */
+ (void)EXPAT(SetEncoding)(self->parser, "utf-8");
+ return expat_parse(self, data, (int)data_len, 0);
+ }
+ else {
+ Py_buffer view;
+ PyObject *res;
+ if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) < 0)
+ return NULL;
+ if (view.len > INT_MAX) {
+ PyBuffer_Release(&view);
+ PyErr_SetString(PyExc_OverflowError, "size does not fit in an int");
+ return NULL;
+ }
+ res = expat_parse(self, view.buf, (int)view.len, 0);
+ PyBuffer_Release(&view);
+ return res;
+ }
}
static PyObject*
@@ -3570,7 +3591,7 @@ xmlparser_setevents(XMLParserObject *self, PyObject* args)
}
static PyMethodDef xmlparser_methods[] = {
- {"feed", (PyCFunction) xmlparser_feed, METH_VARARGS},
+ {"feed", (PyCFunction) xmlparser_feed, METH_O},
{"close", (PyCFunction) xmlparser_close, METH_VARARGS},
{"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS},
{"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS},