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
Title: ElementTree.Element broken attribute setting
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eli.bendersky Nosy List: Arfrever, eli.bendersky, jjstuart, jwilk, python-dev
Priority: normal Keywords: patch

Created on 2013-05-15 22:46 by jwilk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test-element-setattr.py jwilk, 2013-05-15 22:46
Messages (12)
msg189321 - (view) Author: Jakub Wilk (jwilk) Date: 2013-05-15 22:46
Setting attributes on ElementTree.Element objects is broken: it succeeds without exception, but then the following statement fails with AttributeError (unless that statement sets an Element's attribute too):

$ python3.3 test-element-setattr.py 
Hello world!Traceback (most recent call last):
  File "test-element-setattr.py", line 6, in <module>
    print('Hello world!')
AttributeError: ham2
msg189463 - (view) Author: Joe Stuart (jjstuart) Date: 2013-05-17 15:55
It looks like it's being called from the c extension. I would think it should still throw an exception though?


>>> e = etree.Element
>>> e.ham = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'xml.etree.ElementTree.Element'
msg189474 - (view) Author: Joe Stuart (jjstuart) Date: 2013-05-17 18:04
At the end of ElementTree all of the c accelerators are being imported and it looks like only XMLParser is being used. Here is a patch that only imports XMLParser.
msg189477 - (view) Author: Joe Stuart (jjstuart) Date: 2013-05-17 18:19
This patch should fix the issue of the classes being overwritten by the c accelerated ones.
msg189478 - (view) Author: Joe Stuart (jjstuart) Date: 2013-05-17 19:11
Forgot to update the XMLParser() assignment.
msg189497 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2013-05-18 07:15
Replacement of pure-Python classes by C-accelerated classes is intentional.

http://docs.python.org/3.3/library/xml.etree.elementtree.html
"Changed in version 3.3: This module will use a fast implementation whenever available. The xml.etree.cElementTree module is deprecated."
msg189515 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-05-18 14:14
Yes, overwriting the Python classes with C classes is not an error, but the original issue is legit. The error should be more immediately reported.
msg189518 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-05-18 14:41
The problem is that element_setattro is returning a wrong value for error - NULL instead of -1. So the exception is set and is triggered on the next line instead. Will fix for 3.3 and default branches
msg189520 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-05-18 14:53
New changeset 9682241dc8fc by Eli Bendersky in branch '3.3':
Issue #17989: element_setattro returned incorrect error value.
http://hg.python.org/cpython/rev/9682241dc8fc

New changeset b111ae4f83ef by Eli Bendersky in branch 'default':
Issue #17989: element_setattro returned incorrect error value.
http://hg.python.org/cpython/rev/b111ae4f83ef
msg189521 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-05-18 14:54
Fixed. Thanks for the report!

Python 3.4.0a0 (default:1b760f926846+9682241dc8fc+, May 18 2013, 07:52:49) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET; j = ET.Element('j')
>>> j.ham = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Can't set arbitraty attributes on Element
>>>
msg189625 - (view) Author: Joe Stuart (jjstuart) Date: 2013-05-19 21:39
Looks like a typo in arbitrary.

AttributeError: Can't set arbitraty attributes on Element
msg189628 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-05-20 00:00
New changeset 65680e3a7f07 by Eli Bendersky in branch '3.3':
Issue #17989: fix typo in error message
http://hg.python.org/cpython/rev/65680e3a7f07

New changeset 19767536ce84 by Eli Bendersky in branch 'default':
Issue #17989: fix typo in error message
http://hg.python.org/cpython/rev/19767536ce84
History
Date User Action Args
2022-04-11 14:57:45adminsetgithub: 62189
2013-05-20 00:01:03eli.benderskysetstatus: open -> closed
resolution: fixed
stage: resolved
2013-05-20 00:00:40python-devsetmessages: + msg189628
2013-05-19 22:07:10Arfreversetstatus: closed -> open
resolution: fixed -> (no value)
stage: resolved -> (no value)
2013-05-19 21:39:02jjstuartsetmessages: + msg189625
2013-05-18 14:54:53eli.benderskysetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2013-05-18 14:54:41eli.benderskysetmessages: + msg189521
2013-05-18 14:53:52python-devsetnosy: + python-dev
messages: + msg189520
2013-05-18 14:42:22eli.benderskysettype: behavior
stage: needs patch
2013-05-18 14:41:56eli.benderskysetmessages: + msg189518
2013-05-18 14:14:19eli.benderskysetmessages: + msg189515
2013-05-18 14:06:54eli.benderskysetassignee: eli.bendersky
2013-05-18 07:17:42Arfreversetfiles: - ElementTree.patch
2013-05-18 07:15:53Arfreversetmessages: + msg189497
2013-05-17 19:11:02jjstuartsetfiles: + ElementTree.patch

messages: + msg189478
2013-05-17 19:02:32jjstuartsetfiles: - ElementTree.patch
2013-05-17 18:19:31jjstuartsetfiles: + ElementTree.patch

messages: + msg189477
2013-05-17 18:17:16jjstuartsetfiles: - ElementTree.patch
2013-05-17 18:04:28jjstuartsetfiles: + ElementTree.patch
keywords: + patch
messages: + msg189474
2013-05-17 15:55:53jjstuartsetnosy: + jjstuart
messages: + msg189463
2013-05-16 13:57:57Arfreversetnosy: + Arfrever

versions: + Python 3.4
2013-05-16 13:04:44pitrousetnosy: + eli.bendersky
2013-05-15 22:46:51jwilkcreate