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: hasattr, delattr, getattr fail with unnormalized names
Type: Stage:
Components: Unicode Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Jim.Jewett, benjamin.peterson, ezio.melotti, rhettinger
Priority: normal Keywords:

Created on 2012-01-16 05:05 by Jim.Jewett, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg151320 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2012-01-16 05:05
The documentation for hasattr, getattr, and delattr state that they are equivalent to object.attribute access; this isn't quite true, because object.attribute uses a NFKC-normalized version of the string as only the secondary location, while hasattr, getattr, and delattr (assuming an object rather than an Identifier or string) don't seem to do the normalization at all.

I think the simplest fix would be to normalize and retry when hasattr, getattr, and delattr fail with a string, but I'm not sure that normalization shouldn't be the only string tried. 

>>> o.º
Traceback (most recent call last):
  File "<pyshell#820>", line 1, in <module>
    o.º
AttributeError: 'Object' object has no attribute 'o'
>>> o.o
Traceback (most recent call last):
  File "<pyshell#821>", line 1, in <module>
    o.o
AttributeError: 'Object' object has no attribute 'o'
>>> o.º=[]
>>> hasattr(o, "º")
False
>>> getattr(o, "º")
Traceback (most recent call last):
  File "<pyshell#824>", line 1, in <module>
    getattr(o, "º")
AttributeError: 'Object' object has no attribute 'º'
>>> delattr(o, "º")
Traceback (most recent call last):
  File "<pyshell#825>", line 1, in <module>
    delattr(o, "º")
AttributeError: º
>>> o.º
[]
>>> o.º is o.o
True
>>> o.o
[]
>>> del o.º
>>> o.o
Traceback (most recent call last):
  File "<pyshell#830>", line 1, in <module>
    o.o
AttributeError: 'Object' object has no attribute 'o'

>>> o.º = 5
>>> hasattr(o, "º")
False
>>> hasattr(o, "o")
True
>>> hasattr(o, "o")
True
>>> o.º
5
>>> delattr(o, "o")
>>> o.º
msg151371 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-01-16 15:10
Normalizing the string in getattr() is unacceptable. We'll just have to document, that all identifiers are in NFKC.
msg151405 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2012-01-16 20:14
Why is normalization in getattr unacceptable?  I won't pretend to *like* it, but the difference between two canonically equal strings really is (by definition) just a representational issue.

Would it be OK to normalize in object's own implementation, so that custom classes could avoid the normalization, but it would happen by default?
msg151407 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-01-16 20:21
Because it's (very) expensive and the method you propose would fail in the case that someone had put the normalized and unnormalized string in the object's __dict__.
msg151806 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-01-23 10:23
I concur with Benjamin on all counts.
History
Date User Action Args
2022-04-11 14:57:25adminsetgithub: 58002
2018-01-02 17:52:57benjamin.petersonlinkissue32483 superseder
2012-01-23 14:03:22benjamin.petersonsetstatus: open -> closed
resolution: wont fix
2012-01-23 10:23:05rhettingersetnosy: + rhettinger
messages: + msg151806
2012-01-16 20:21:20benjamin.petersonsetmessages: + msg151407
2012-01-16 20:14:14Jim.Jewettsetmessages: + msg151405
2012-01-16 15:10:30benjamin.petersonsetmessages: + msg151371
2012-01-16 12:22:51pitrousetnosy: + benjamin.peterson

versions: + Python 3.2, Python 3.3
2012-01-16 05:05:25Jim.Jewettcreate