I have a function to replace the content of an ElementTree Element by
that of another one which works using Python 2 but not with Python 3.
I get an assertion error.
It was suggested on the Python list that the problem is that in Python 3
slice assignments are done with __setitem__ rather than __setslice__ but
ElementTree has not been adapted to that -- hence the title given to
this bug.
The function that I use to demonstrate this (with a workaround solution)
is as follows:
def replace_element(elem, replacement):
'''replace the content of an ElementTree Element by that of
another
one.
'''
elem.clear()
elem.text = replacement.text
elem.tail = replacement.tail
elem.tag = replacement.tag
elem.attrib = replacement.attrib
try:
elem[:] = replacement[:] # works with Python 2.5 but not 3.1
except AssertionError:
del elem[:]
for child in replacement:
elem.append(child)
I have included a small test file which demonstrate the behaviour. |