Hello. Is it ok to use _setroot() method of _ElementTree? If i set root element will part of tree above current root be freed? Here is my usage: original_doc = etree.fromstring('<bar/>') # savepoint foo = etree.Element("foo") foo.append("p") ... foo.append(original_doc) xslt_transform(foo) # now i want revert changes made after savepoint to original_tree # and discard all elements created after.. # is it a correct way? tree = foo.getroottree() tree._setroot(original_doc) I cant use deepcopy() of original_tree because the point is: i am trying to do modifications of original tree in xslt_transform with extension functions. If memory allocated by foo will not be freed after _setroot() - will it be freed after original_tree is out of scope? -- -------------------------------------------- Турнаев Евгений Викторович +7 906 875 09 43 --------------------------------------------
Evgeny Turnaev, 09.07.2012 17:21:
Is it ok to use _setroot() method of _ElementTree?
Sure, but keep reading.
If i set root element will part of tree above current root be freed?
All it does is that it replaces the reference to the tree by another one. No difference to changing a local variable in your code.
Here is my usage:
original_doc = etree.fromstring('<bar/>')
# savepoint foo = etree.Element("foo") foo.append("p") ... foo.append(original_doc) xslt_transform(foo)
# now i want revert changes made after savepoint to original_tree # and discard all elements created after.. # is it a correct way? tree = foo.getroottree() tree._setroot(original_doc)
Any reason you are using an ElementTree object here at all? You could just keep a reference to the root element.
I cant use deepcopy() of original_tree because the point is: i am trying to do modifications of original tree in xslt_transform with extension functions.
Sounds like a really bad idea to me. XSLT is meant to build a new document, it shouldn't modify the input document. You should try separating the logic that modifies the input document from the logic in the XSLT. I think that would solve the problem you are immediately facing and at the same time make your code cleaner.
If memory allocated by foo will not be freed after _setroot() - will it be freed after original_tree is out of scope?
The solution to your problem has nothing to do with _setroot(). Stefan
participants (2)
-
Evgeny Turnaev
-
Stefan Behnel