Skip to content

Commit e02ea99

Browse files
authored
Merge branch 'main' into nedbat/split-builtin-stdlib
2 parents 796382e + b1e7554 commit e02ea99

2 files changed

Lines changed: 13 additions & 41 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Remove the accidental acceptance of :class:`bytes` in the C implementation of
2+
:mod:`xml.etree.ElementTree`, a leftover of the Python 2 to Python 3 migration,
3+
for paths of :meth:`~xml.etree.ElementTree.Element.find`
4+
and similar methods, for the tag of :meth:`~xml.etree.ElementTree.Element.iter`,
5+
and for the names of events of :class:`~xml.etree.ElementTree.XMLPullParser`
6+
and :func:`~xml.etree.ElementTree.iterparse`. It now raises the same
7+
exceptions as the Python implementation.

Modules/_elementtree.c

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,24 +1230,6 @@ checkpath(PyObject* tag)
12301230
}
12311231
return 0;
12321232
}
1233-
if (PyBytes_Check(tag)) {
1234-
const char *p = PyBytes_AS_STRING(tag);
1235-
const Py_ssize_t len = PyBytes_GET_SIZE(tag);
1236-
if (len >= 3 && p[0] == '{' && (
1237-
p[1] == '}' || (p[1] == '*' && p[2] == '}'))) {
1238-
/* wildcard: '{}tag' or '{*}tag' */
1239-
return 1;
1240-
}
1241-
for (i = 0; i < len; i++) {
1242-
if (p[i] == '{')
1243-
check = 0;
1244-
else if (p[i] == '}')
1245-
check = 1;
1246-
else if (check && PATHCHAR(p[i]))
1247-
return 1;
1248-
}
1249-
return 0;
1250-
}
12511233

12521234
return 1; /* unknown type; might be path expression */
12531235
}
@@ -1552,10 +1534,6 @@ _elementtree_Element_iter_impl(ElementObject *self, PyTypeObject *cls,
15521534
if (PyUnicode_GET_LENGTH(tag) == 1 && PyUnicode_READ_CHAR(tag, 0) == '*')
15531535
tag = Py_None;
15541536
}
1555-
else if (PyBytes_Check(tag)) {
1556-
if (PyBytes_GET_SIZE(tag) == 1 && *PyBytes_AS_STRING(tag) == '*')
1557-
tag = Py_None;
1558-
}
15591537

15601538
elementtreestate *st = get_elementtree_state_by_cls(cls);
15611539
return create_elementiter(st, self, tag, 0);
@@ -2935,17 +2913,7 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
29352913
self->data = Py_NewRef(data);
29362914
} else {
29372915
/* more than one item; use a list to collect items */
2938-
if (PyBytes_CheckExact(self->data)
2939-
&& _PyObject_IsUniquelyReferenced(self->data)
2940-
&& PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
2941-
/* XXX this code path unused in Python 3? */
2942-
/* expat often generates single character data sections; handle
2943-
the most common case by resizing the existing string... */
2944-
Py_ssize_t size = PyBytes_GET_SIZE(self->data);
2945-
if (_PyBytes_Resize(&self->data, size + 1) < 0)
2946-
return NULL;
2947-
PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0];
2948-
} else if (PyList_CheckExact(self->data)) {
2916+
if (PyList_CheckExact(self->data)) {
29492917
if (PyList_Append(self->data, data) < 0)
29502918
return NULL;
29512919
} else {
@@ -4363,18 +4331,14 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
43634331

43644332
for (i = 0; i < PySequence_Fast_GET_SIZE(events_seq); ++i) {
43654333
PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i);
4366-
const char *event_name = NULL;
4367-
if (PyUnicode_Check(event_name_obj)) {
4368-
event_name = PyUnicode_AsUTF8(event_name_obj);
4369-
} else if (PyBytes_Check(event_name_obj)) {
4370-
event_name = PyBytes_AS_STRING(event_name_obj);
4334+
if (!PyUnicode_Check(event_name_obj)) {
4335+
goto unknown_event;
43714336
}
4337+
const char *event_name = PyUnicode_AsUTF8(event_name_obj);
43724338
if (event_name == NULL) {
43734339
Py_DECREF(events_seq);
4374-
PyErr_Format(PyExc_ValueError, "invalid events sequence");
43754340
return NULL;
43764341
}
4377-
43784342
if (strcmp(event_name, "start") == 0) {
43794343
Py_XSETREF(target->start_event_obj, Py_NewRef(event_name_obj));
43804344
} else if (strcmp(event_name, "end") == 0) {
@@ -4406,7 +4370,8 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
44064370
(XML_ProcessingInstructionHandler) expat_pi_handler
44074371
);
44084372
} else {
4409-
PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name);
4373+
unknown_event:
4374+
PyErr_Format(PyExc_ValueError, "unknown event %R", event_name_obj);
44104375
Py_DECREF(events_seq);
44114376
return NULL;
44124377
}

0 commit comments

Comments
 (0)