[lxml-dev] objectify __setattr__/addattr bug

Hi, funny how s.th. like this could go undetected in all lxml.objectify versions:
python2.4 -i -c 'from lxml import etree, objectify; objectify.enable_recursive_str(); print etree.__version__; print etree.LIBXML_VERSION, etree.LIBXSLT_VERSION; root=objectify.Element("root")' 2.2.2 (2, 6, 32) (1, 1, 23)
root.s1 = 'äöü'
Traceback (most recent call last): File "<stdin>", line 1, in ? File "lxml.objectify.pyx", line 246, in lxml.objectify.ObjectifiedElement.__setattr__ (src/lxml/lxml.objectify.c:3061) File "lxml.objectify.pyx", line 524, in lxml.objectify._appendValue (src/lxml/lxml.objectify.c:5771) File "lxml.objectify.pyx", line 552, in lxml.objectify._setElementValue (src/lxml/lxml.objectify.c:6033) File "public-api.pxi", line 76, in lxml.etree.setNodeText (src/lxml/lxml.etree.c:118529) File "apihelpers.pxi", line 650, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:15144) File "apihelpers.pxi", line 1247, in lxml.etree._utf8 (src/lxml/lxml.etree.c:19727) ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes
root.addattr('s2', 'äöü')
Traceback (most recent call last): File "<stdin>", line 1, in ? File "lxml.objectify.pyx", line 261, in lxml.objectify.ObjectifiedElement.addattr (src/lxml/lxml.objectify.c:3228) File "lxml.objectify.pyx", line 524, in lxml.objectify._appendValue (src/lxml/lxml.objectify.c:5771) File "lxml.objectify.pyx", line 552, in lxml.objectify._setElementValue (src/lxml/lxml.objectify.c:6033) File "public-api.pxi", line 76, in lxml.etree.setNodeText (src/lxml/lxml.etree.c:118529) File "apihelpers.pxi", line 650, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:15144) File "apihelpers.pxi", line 1247, in lxml.etree._utf8 (src/lxml/lxml.etree.c:19727) ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes
print root
root = None [ObjectifiedElement] s1 = u'' [StringElement] * py:pytype = 'str' s2 = u'' [StringElement] * py:pytype = 'str'
So while the assignment (string) rvalue is unacceptable an element gets created nonetheless; I'd say this is a bug.
This fixes it:
$ svn diff Index: src/lxml/lxml.objectify.pyx =================================================================== --- src/lxml/lxml.objectify.pyx (revision 67827) +++ src/lxml/lxml.objectify.pyx (working copy) @@ -523,9 +523,10 @@ for item in value: _appendValue(parent, tag, item) else: - new_element = cetree.makeSubElement( - parent, tag, None, None, None, None) + new_element = cetree.makeElement( + tag, parent._doc, None, None, None, None, None) _setElementValue(new_element, value) + cetree.appendChild(parent, new_element)
cdef _setElementValue(_Element element, value): cdef python.PyObject* _pytype
Add some test(s) and check it in?
Holger

Hi,
jholg@gmx.de wrote:
So while the assignment (string) rvalue is unacceptable an element gets created nonetheless; I'd say this is a bug.
This fixes it:
$ svn diff Index: src/lxml/lxml.objectify.pyx =================================================================== --- src/lxml/lxml.objectify.pyx (revision 67827) +++ src/lxml/lxml.objectify.pyx (working copy) @@ -523,9 +523,10 @@ for item in value: _appendValue(parent, tag, item) else:
new_element = cetree.makeSubElement(
parent, tag, None, None, None, None)
new_element = cetree.makeElement(
tag, parent._doc, None, None, None, None, None) _setElementValue(new_element, value)
cetree.appendChild(parent, new_element)
cdef _setElementValue(_Element element, value): cdef python.PyObject* _pytype
Add some test(s) and check it in?
Yes, please do.
Thanks!
Stefan

Hi,
jholg@gmx.de wrote:
So while the assignment (string) rvalue is unacceptable an element gets
created nonetheless; I'd say this is a bug.
[...] Add some test(s) and check it in?
Yes, please do.
Done:
https://codespeak.net/viewvc/?view=rev&revision=67943
As the testcases need to use unicode data: I'm pretty sure I did not break anything for Py3; I used the helper functions from common_imports but can't currently really test this due to lack of a working Py3 environment.
Holger
participants (2)
-
jholg@gmx.de
-
Stefan Behnel