[Python-Dev] hasattr() and Unicode strings
Guido van Rossum
guido@beopen.com
Thu, 07 Sep 2000 16:32:16 -0500
> hasattr(), getattr(), and doubtless other built-in functions
> don't accept Unicode strings at all:
>
> >>> import sys
> >>> hasattr(sys, u'abc')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: hasattr, argument 2: expected string, unicode found
>
> Is this a bug or a feature? I'd say bug; the Unicode should be
> coerced using the default ASCII encoding, and an exception raised if
> that isn't possible.
Agreed.
There are probably a bunch of things that need to be changed before
thois works though; getattr() c.s. require a string, then call
PyObject_GetAttr() which also checks for a string unless the object
supports tp_getattro -- but that's only true for classes and
instances.
Also, should we convert the string to 8-bit, or should we allow
Unicode attribute names?
It seems there's no easy fix -- better address this after 2.0 is
released.
--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)