classification
Title: xml.etree.cElementTree lacks full dir() on Element
Type: enhancement Stage: needs patch
Components: Extension Modules, XML Versions: Python 3.4, Python 3.3
process
Status: open Resolution:
Dependencies: Update ElementTree with upstream changes
View: 6472
Superseder:
Assigned To: effbot Nosy List: ArcRiley, effbot, flox, santa4nt
Priority: low Keywords: patch

Created on 2010-02-22 16:36 by ArcRiley, last changed 2011-04-14 17:12 by ArcRiley.

Files
File name Uploaded Description Edit
dir_elem_c.patch santa4nt, 2011-03-08 04:56
Messages (5)
msg99777 - (view) Author: Arc Riley (ArcRiley) * Date: 2010-02-22 16:36
>>> import xml.etree.cElementTree as ET
>>> tree = ET.parse('test.xml')
>>> root = tree.getroot()
>>> dir(root)
['!__reduce__', '__class__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'keys', 'makeelement', 'remove', 'set']
>>> root.tag
'{http://testnamespace}root'

The tag property is not listed in dir() making ElementTree more difficult to use for those of us with poor memories.
msg99929 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-23 15:48
Confirmed.

As a workaround, the Python implementation shows its attributes "tag", "text", "tail", "attrib".

Since upstream version of ElementTree drops support for Python < 2.3, we may refactor the Element type, and use the "tp_members" and "tp_getset" slots.
msg130306 - (view) Author: Santoso Wijaya (santa4nt) Date: 2011-03-08 04:56
Attached a patch with test for this:

Following the suggestion, I put "tag", "text", "tail", and "attrib" to be accessible via tp_getset for _etree.Element type.
msg133747 - (view) Author: Santoso Wijaya (santa4nt) Date: 2011-04-14 16:53
Is this a right approach? I converted the hard-coded "attributes" into real, C-API attributes.
msg133751 - (view) Author: Arc Riley (ArcRiley) * Date: 2011-04-14 17:12
It looks right to me, but I would include more verbose pydoc strings.  

IE, "The tail attribute can be used to hold additional data associated with the element" tells me nothing.  You could explain here what .tail actually is, a few XML examples of what would be put in tail or what tail would become, and the API design reason why tail is used in addition to text.
History
Date User Action Args
2011-04-14 17:12:33ArcRileysetmessages: + msg133751
2011-04-14 16:53:36santa4ntsetmessages: + msg133747
2011-03-20 10:43:24santa4ntsetnosy: effbot, ArcRiley, flox, santa4nt
versions: + Python 3.4, - Python 3.1, Python 2.7, Python 3.2
2011-03-08 04:56:47santa4ntsetnosy: effbot, ArcRiley, flox, santa4nt
versions: + Python 3.1, Python 3.3
2011-03-08 04:56:29santa4ntsetfiles: + dir_elem_c.patch

nosy: + santa4nt
messages: + msg130306

keywords: + patch
2010-02-23 15:48:04floxset
nosy: + effbot, flox
versions: + Python 2.7, Python 3.2, - Python 3.1
messages: + msg99929
priority: low
assignee: effbot
dependencies: + Update ElementTree with upstream changes
components: + Extension Modules, XML, - None
type: behavior -> enhancement
stage: needs patch
2010-02-22 16:36:02ArcRileycreate