mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Issue #25455: Fixed a crash in repr of ElementTree.Element with recursive tag.
This commit is contained in:
commit
f0ee5ccd19
3 changed files with 25 additions and 4 deletions
|
@ -18,7 +18,7 @@ import weakref
|
|||
|
||||
from itertools import product
|
||||
from test import support
|
||||
from test.support import TESTFN, findfile, import_fresh_module, gc_collect
|
||||
from test.support import TESTFN, findfile, import_fresh_module, gc_collect, swap_attr
|
||||
|
||||
# pyET is the pure-Python implementation.
|
||||
#
|
||||
|
@ -1864,6 +1864,12 @@ class BadElementTest(ElementTestCase, unittest.TestCase):
|
|||
e.extend([ET.Element('bar')])
|
||||
self.assertRaises(ValueError, e.remove, X('baz'))
|
||||
|
||||
def test_recursive_repr(self):
|
||||
# Issue #25455
|
||||
e = ET.Element('foo')
|
||||
with swap_attr(e, 'tag', e):
|
||||
with self.assertRaises(RuntimeError):
|
||||
repr(e) # Should not crash
|
||||
|
||||
class MutatingElementPath(str):
|
||||
def __new__(cls, elem, *args):
|
||||
|
|
|
@ -44,6 +44,8 @@ Core and Builtins
|
|||
Library
|
||||
-------
|
||||
|
||||
- Issue #25455: Fixed a crash in repr of ElementTree.Element with recursive tag.
|
||||
|
||||
- Issue #27294: Improved repr for Tkinter event objects.
|
||||
|
||||
- Issue #20508: Improve exception message of IPv{4,6}Network.__getitem__.
|
||||
|
|
|
@ -1610,10 +1610,23 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
|
|||
static PyObject*
|
||||
element_repr(ElementObject* self)
|
||||
{
|
||||
if (self->tag)
|
||||
return PyUnicode_FromFormat("<Element %R at %p>", self->tag, self);
|
||||
else
|
||||
int status;
|
||||
|
||||
if (self->tag == NULL)
|
||||
return PyUnicode_FromFormat("<Element at %p>", self);
|
||||
|
||||
status = Py_ReprEnter((PyObject *)self);
|
||||
if (status == 0) {
|
||||
PyObject *res;
|
||||
res = PyUnicode_FromFormat("<Element %R at %p>", self->tag, self);
|
||||
Py_ReprLeave((PyObject *)self);
|
||||
return res;
|
||||
}
|
||||
if (status > 0)
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"reentrant call inside %s.__repr__",
|
||||
Py_TYPE(self)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue