This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: xml.etree.cElementTree lacks full dir() on Element
类型: enhancement Stage: resolved
Components: Extension Modules, XML Versions: Python 3.6
process
状态: closed Resolution: fixed
Dependencies: 6472 后续:
分配给: serhiy.storchaka 抄送列表: ArcRiley, christian.heimes, effbot, eli.bendersky, flox, martin.panter, python-dev, santoso.wijaya, serhiy.storchaka
优先级: low 关键字: patch

Created on 2010-02-22 16:36 by ArcRiley, last changed 2022-04-11 14:56 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
dir_elem_c.patch santoso.wijaya, 2011-03-08 04:56
etree_attributes.patch serhiy.storchaka, 2015-11-23 09:49 review
Messages (10)
msg99777 - (view) Author: Arc Riley (ArcRiley) * 日期: 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) 日期: 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 (santoso.wijaya) * 日期: 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 (santoso.wijaya) * 日期: 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) * 日期: 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.
msg194315 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) 日期: 2013-08-03 23:44
Could you please refresh the patch for Python 3.3 and 3.4 (_elementtree went through many changes in 3.3)?
msg255143 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2015-11-23 09:49
Here is a patch updated to 3.6.
msg255245 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2015-11-24 04:04
The patch looks technically good, though I’m not a big fan of the double underscores, like element_get__text(). Maybe element_text_getter() instead, because it has to match the “getter” signature?

Personally, I would say keep the doc strings basic. If you want to add more depth about “tail”, I suggest to keep it to the main RST documentation (which was updated fairly recently). But it would be good to quickly distinguish text and tail other than by name. Maybe:

{"text", ..., "A string of text directly after the start tag, or None"},
{"tail", ..., "A string of text directly after the end tag, or None"},
msg255344 - (view) Author: Roundup Robot (python-dev) (Python triager) 日期: 2015-11-25 13:29
New changeset fef7f041c1a7 by Serhiy Storchaka in branch 'default':
Issue #7990: dir() on ElementTree.Element now lists properties: "tag",
https://hg.python.org/cpython/rev/fef7f041c1a7
msg255346 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2015-11-25 13:30
Thank you Martin. Committed with both your suggestions.
历史
日期 用户 动作 参数
2022-04-11 14:56:58admin修改github: 52238
2015-11-25 13:30:41serhiy.storchaka修改状态: open -> closed
resolution: fixed
消息: + msg255346

stage: patch review -> resolved
2015-11-25 13:29:21python-dev修改抄送: + python-dev
消息: + msg255344
2015-11-24 04:04:50martin.panter修改抄送: + martin.panter
消息: + msg255245
2015-11-23 09:49:36serhiy.storchaka修改assignee: serhiy.storchaka
2015-11-23 09:49:27serhiy.storchaka修改文件: + etree_attributes.patch
versions: + Python 3.6, - Python 3.3, Python 3.4
抄送: + serhiy.storchaka

消息: + msg255143
2013-08-04 01:53:50christian.heimes修改抄送: + christian.heimes
2013-08-03 23:44:53eli.bendersky修改消息: + msg194315
2012-07-21 13:46:57flox修改assignee: effbot -> (no value)

抄送: + eli.bendersky
stage: needs patch -> patch review
2011-04-14 17:12:33ArcRiley修改消息: + msg133751
2011-04-14 16:53:36santoso.wijaya修改消息: + msg133747
2011-03-20 10:43:24santoso.wijaya修改抄送: effbot, ArcRiley, flox, santoso.wijaya
versions: + Python 3.4, - Python 3.1, Python 2.7, Python 3.2
2011-03-08 04:56:47santoso.wijaya修改抄送: effbot, ArcRiley, flox, santoso.wijaya
versions: + Python 3.1, Python 3.3
2011-03-08 04:56:29santoso.wijaya修改文件: + dir_elem_c.patch

抄送: + santoso.wijaya
消息: + msg130306

keywords: + patch
2010-02-23 15:48:04flox修改
assignee: effbot
dependencies: + Update ElementTree with upstream changes
stage: needs patch
versions: + Python 2.7, Python 3.2, - Python 3.1
抄送: + effbot, flox
消息: + msg99929
优先级: low
components: + Extension Modules, XML, - None
type: behavior -> enhancement
2010-02-22 16:36:02ArcRiley创建