diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2004-08-07 19:20:05 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2004-08-07 19:20:05 (GMT) |
commit | 4c989ddc9c8bd38ab14c9f42511eb567ed219604 (patch) | |
tree | 9e91f6cf6dd52b386920c4123132f846b94e4817 /Lib | |
parent | cbd81556bb975b72697d2989eccfbc9f3e39b3de (diff) | |
download | cpython-4c989ddc9c8bd38ab14c9f42511eb567ed219604.zip cpython-4c989ddc9c8bd38ab14c9f42511eb567ed219604.tar.gz cpython-4c989ddc9c8bd38ab14c9f42511eb567ed219604.tar.bz2 |
Subclasses of string can no longer be interned. The semantics of
interning were not clear here -- a subclass could be mutable, for
example -- and had bugs. Explicitly interning a subclass of string
via intern() will raise a TypeError. Internal operations that attempt
to intern a string subclass will have no effect.
Added a few tests to test_builtin that includes the old buggy code and
verifies that calls like PyObject_SetAttr() don't fail. Perhaps these
tests should have gone in test_string.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_builtin.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index bc5afdc..8e3a925 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -608,6 +608,23 @@ class BuiltinTest(unittest.TestCase): s2 = s.swapcase().swapcase() self.assert_(intern(s2) is s) + # Subclasses of string can't be interned, because they + # provide too much opportunity for insane things to happen. + # We don't want them in the interned dict and if they aren't + # actually interned, we don't want to create the appearance + # that they are by allowing intern() to succeeed. + class S(str): + def __hash__(self): + return 123 + + self.assertRaises(TypeError, intern, S("abc")) + + # It's still safe to pass these strings to routines that + # call intern internally, e.g. PyObject_SetAttr(). + s = S("abc") + setattr(s, s, s) + self.assertEqual(getattr(s, s), s) + def test_iter(self): self.assertRaises(TypeError, iter) self.assertRaises(TypeError, iter, 42, 42) |