Commit f349051a authored by Jason Madden's avatar Jason Madden

Another CPython/pure-python difference is the handling of interning instance...

Another CPython/pure-python difference is the handling of interning instance keys. C has always let non-str values through, but the python implementation raised a TypeError. Correct that and add a test case.
parent add499ab
......@@ -329,7 +329,15 @@ class Persistent(object):
raise TypeError('No instance dict')
idict.clear()
for k, v in inst_dict.items():
idict[intern(k)] = v
# Normally the keys for instance attributes are interned.
# Do that here, but only if it is possible to do so.
# TODO: On Python 2 codebases that straddle Python3,
# and use 'from __future__ import unicode_literals' it's not
# unheard of to have unicode objects in the __dict__ by accident.
# Should we watch for that and attempt to encode it so it can be
# interned?
idict[intern(k) if type(k) is str else k] = v
slotnames = self._slotnames()
if slotnames:
for k, v in slots.items():
......
......@@ -948,6 +948,19 @@ class _Persistent_Base(object):
key2 = list(inst2.__dict__.keys())[0]
self.assertTrue(key1 is key2)
def test___setstate___doesnt_fail_on_non_string_keys(self):
class Derived(self._getTargetClass()):
pass
inst1 = Derived()
inst1.__setstate__({1: 2})
self.assertTrue(1 in inst1.__dict__)
class MyStr(str):
pass
mystr = MyStr('mystr')
inst1.__setstate__({mystr: 2})
self.assertTrue(mystr in inst1.__dict__)
def test___reduce__(self):
from persistent._compat import copy_reg
inst = self._makeOne()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment