[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
Eli Bendersky added the comment: Closing, since this isn't a bug and append's behavior is properly documented. Regarding the error message, yes it could probably be better but you would need to enable input validation for that. Since Python is duck typed, often when arguments are not validated you get less-than-good error messages if things go wrong. In this case, append expect something Element-like that would have an iter method, and complains when that's not found. In 3.3 this whole thing was improved by always validating arguments in append/extend etc. and raising a TypeError when something is wrong. I don't think there's good enough reason to change this in 2.7 -- stage: - committed/rejected status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
Changes by Ezio Melotti ezio.melo...@gmail.com: -- nosy: +eli.bendersky, ezio.melotti ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
New submission from kirpit: xml.etree.ElementTree.Element's append method doesn't support iterator/sequence parameters as it's supposed to, for the version 1.3.0. Python 2.7.3 (default, Sep 14 2012, 09:52:31) [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin Type help, copyright, credits or license for more information. import xml.etree.ElementTree as et et.VERSION '1.3.0' root = et.Element('root') sublist = [et.Element('sub'), et.Element('sub')] root.append(sublist) et.tostring(root) Traceback (most recent call last): File stdin, line 1, in module File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 1127, in tostring ElementTree(element).write(file, encoding, method=method) File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 818, in write self._root, encoding, default_namespace File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 878, in _namespaces for elem in iterate(): File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 477, in iter for e in e.iter(tag): AttributeError: 'list' object has no attribute 'iter' root = et.Element('root') root.append(iter(sublist)) et.tostring(root) Traceback (most recent call last): File stdin, line 1, in module File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 1127, in tostring ElementTree(element).write(file, encoding, method=method) File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 818, in write self._root, encoding, default_namespace File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 878, in _namespaces for elem in iterate(): File /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py, line 477, in iter for e in e.iter(tag): AttributeError: 'listiterator' object has no attribute 'iter' -- assignee: ronaldoussoren components: Macintosh, XML messages: 171255 nosy: kirpit, ronaldoussoren priority: normal severity: normal status: open title: xml.etree.ElementTree.Element: append method iterator param is broken type: behavior versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
Ronald Oussoren added the comment: (unassigning as this is not a mac-specific issue) BTW. Is this really a bug, the documentation says that append appends a single element http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.append: quote append(subelement) Adds the element subelement to the end of this elements internal list of subelements. /quote -- assignee: ronaldoussoren - nobody nosy: +nobody ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
kirpit added the comment: well, i've just followed the source code regardless to documentation so you may be right about appending a single element. (kind of newbie around here.) but then, append method is misbehaving about asserting the parameter, isn't it? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16044] xml.etree.ElementTree.Element: append method iterator param is broken
Ronald Oussoren added the comment: Use the extend method to add multiple elements. Both the source and documentation indicate that 'append' is used for appending a single item and 'extend' for appending multiple items (just like with list). IMHO this is not a bug. As an aside: when you import xml.etree.cElementTree you get a faster implementation of the same interface, and this (C-based) implementation does validate arguments. -- resolution: - invalid ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16044 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com