[Python-Dev] RE: interning string subclasses
Tim Peters
tim.one@home.com
Wed, 12 Sep 2001 00:50:45 -0400
[Tim]
> Question: Should we complain if someone tries to intern an instance of
> a string subclass? I hate to slow any code on those paths.
[Guido]
> I think in this case intern(s) should return intern(str(s)). The fast
> path checks ob_sinterned first, and that should always point to a real
> string for a string subclass.
I think I'm wrestling with more than one problem here. The bogus one
confusing everything else appears to be this:
>>> class S(str):
... pass
...
[7098 refs]
>>> hash(S('abc'))
0
[7100 refs]
>>> hash(S('cdefg'))
0
[7102 refs]
>>>
That is, PyObject_Hash(s) is always 0 for any instance s of a subclass of
str. This makes dict-based operations "almost always" believe that an s
like this used as a key doesn't match a real string with the same value.
>>> d = {'a': 1}
[7106 refs]
>>> d[S('a')]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: a
[7143 refs]
>>>
This applies too to the intern dict. So first I think str_subtype_new has
to finish initializing the subclass string object. Then I can get confused
about interning for the right reasons <wink>.