Commit f7f40363 authored by Tres Seaver's avatar Tres Seaver

PersistentMapping was inadvertently pickling volatile attributes...

PersistentMapping was inadvertently pickling volatile attributes (http://www.zope.org/Collectors/Zope/2052).

parent 92413962
...@@ -132,6 +132,9 @@ Multidatabase ...@@ -132,6 +132,9 @@ Multidatabase
PersistentMapping PersistentMapping
----------------- -----------------
- (3.6.1b1) PersistentMapping was inadvertently pickling volatile attributes
(http://www.zope.org/Collectors/Zope/2052).
- (3.6b4) ``PersistentMapping`` makes changes by a ``pop()`` method call - (3.6b4) ``PersistentMapping`` makes changes by a ``pop()`` method call
persistent now. persistent now.
......
...@@ -98,8 +98,8 @@ class PersistentMapping(UserDict, persistent.Persistent): ...@@ -98,8 +98,8 @@ class PersistentMapping(UserDict, persistent.Persistent):
# actual internal dictionary using the name _container. # actual internal dictionary using the name _container.
def __getstate__(self): def __getstate__(self):
state = {} state = dict([x for x in self.__dict__.items()
state.update(self.__dict__) if not x[0].startswith('_v_')])
state['_container'] = state['data'] state['_container'] = state['data']
del state['data'] del state['data']
return state return state
......
...@@ -11,11 +11,10 @@ ...@@ -11,11 +11,10 @@
# FOR A PARTICULAR PURPOSE. # FOR A PARTICULAR PURPOSE.
# #
############################################################################## ##############################################################################
"""Test the list interface to PersistentList """Tests for PersistentList
""" """
import unittest import unittest
from persistent.list import PersistentList
l0 = [] l0 = []
l1 = [0] l1 = [0]
...@@ -23,19 +22,33 @@ l2 = [0, 1] ...@@ -23,19 +22,33 @@ l2 = [0, 1]
class TestPList(unittest.TestCase): class TestPList(unittest.TestCase):
def testTheWorld(self): def _getTargetClass(self):
# Test constructors from persistent.list import PersistentList
u = PersistentList() return PersistentList
u0 = PersistentList(l0)
u1 = PersistentList(l1)
u2 = PersistentList(l2)
uu = PersistentList(u) def test_volatile_attributes_not_persisted(self):
uu0 = PersistentList(u0) # http://www.zope.org/Collectors/Zope/2052
uu1 = PersistentList(u1) m = self._getTargetClass()()
uu2 = PersistentList(u2) m.foo = 'bar'
m._v_baz = 'qux'
state = m.__getstate__()
self.failUnless('foo' in state)
self.failIf('_v_baz' in state)
v = PersistentList(tuple(u)) def testTheWorld(self):
# Test constructors
pl = self._getTargetClass()
u = pl()
u0 = pl(l0)
u1 = pl(l1)
u2 = pl(l2)
uu = pl(u)
uu0 = pl(u0)
uu1 = pl(u1)
uu2 = pl(u2)
v = pl(tuple(u))
class OtherList: class OtherList:
def __init__(self, initlist): def __init__(self, initlist):
self.__data = initlist self.__data = initlist
...@@ -43,8 +56,8 @@ class TestPList(unittest.TestCase): ...@@ -43,8 +56,8 @@ class TestPList(unittest.TestCase):
return len(self.__data) return len(self.__data)
def __getitem__(self, i): def __getitem__(self, i):
return self.__data[i] return self.__data[i]
v0 = PersistentList(OtherList(u0)) v0 = pl(OtherList(u0))
vv = PersistentList("this is also a sequence") vv = pl("this is also a sequence")
# Test __repr__ # Test __repr__
eq = self.assertEqual eq = self.assertEqual
...@@ -160,7 +173,7 @@ class TestPList(unittest.TestCase): ...@@ -160,7 +173,7 @@ class TestPList(unittest.TestCase):
# Test pop # Test pop
u = PersistentList([0, -1, 1]) u = pl([0, -1, 1])
u.pop() u.pop()
eq(u, [0, -1], "u == [0, -1]") eq(u, [0, -1], "u == [0, -1]")
u.pop(0) u.pop(0)
...@@ -200,7 +213,7 @@ class TestPList(unittest.TestCase): ...@@ -200,7 +213,7 @@ class TestPList(unittest.TestCase):
# Test sort # Test sort
u = PersistentList([1, 0]) u = pl([1, 0])
u.sort() u.sort()
eq(u, u2, "u == u2") eq(u, u2, "u == u2")
......
############################################################################## ##############################################################################
# #
# Copyright (c) 2005 Zope Corporation and Contributors. # Copyright (c) 2006 Zope Corporation and Contributors.
# All Rights Reserved. # All Rights Reserved.
# #
# This software is subject to the provisions of the Zope Public License, # This software is subject to the provisions of the Zope Public License,
...@@ -11,37 +11,50 @@ ...@@ -11,37 +11,50 @@
# FOR A PARTICULAR PURPOSE. # FOR A PARTICULAR PURPOSE.
# #
############################################################################## ##############################################################################
"""Test the mapping interface to PersistentMapping """Test PersistentMapping
""" """
import unittest import unittest
from persistent.mapping import PersistentMapping
l0 = {} l0 = {}
l1 = {0:0} l1 = {0:0}
l2 = {0:0, 1:1} l2 = {0:0, 1:1}
class TestPMapping(unittest.TestCase): class MappingTests(unittest.TestCase):
def _getTargetClass(self):
from persistent.mapping import PersistentMapping
return PersistentMapping
def test_volatile_attributes_not_persisted(self):
# http://www.zope.org/Collectors/Zope/2052
m = self._getTargetClass()()
m.foo = 'bar'
m._v_baz = 'qux'
state = m.__getstate__()
self.failUnless('foo' in state)
self.failIf('_v_baz' in state)
def testTheWorld(self): def testTheWorld(self):
# Test constructors # Test constructors
u = PersistentMapping() pm = self._getTargetClass()
u0 = PersistentMapping(l0) u = pm()
u1 = PersistentMapping(l1) u0 = pm(l0)
u2 = PersistentMapping(l2) u1 = pm(l1)
u2 = pm(l2)
uu = PersistentMapping(u) uu = pm(u)
uu0 = PersistentMapping(u0) uu0 = pm(u0)
uu1 = PersistentMapping(u1) uu1 = pm(u1)
uu2 = PersistentMapping(u2) uu2 = pm(u2)
class OtherMapping: class OtherMapping:
def __init__(self, initmapping): def __init__(self, initmapping):
self.__data = initmapping self.__data = initmapping
def items(self): def items(self):
return self.__data.items() return self.__data.items()
v0 = PersistentMapping(OtherMapping(u0)) v0 = pm(OtherMapping(u0))
vv = PersistentMapping([(0, 0), (1, 1)]) vv = pm([(0, 0), (1, 1)])
# Test __repr__ # Test __repr__
eq = self.assertEqual eq = self.assertEqual
...@@ -105,7 +118,7 @@ class TestPMapping(unittest.TestCase): ...@@ -105,7 +118,7 @@ class TestPMapping(unittest.TestCase):
# Test update # Test update
l = {"a":"b"} l = {"a":"b"}
u = PersistentMapping(l) u = pm(l)
u.update(u2) u.update(u2)
for i in u: for i in u:
self.failUnless(i in l or i in u2, "i in l or i in u2") self.failUnless(i in l or i in u2, "i in l or i in u2")
...@@ -151,10 +164,8 @@ class TestPMapping(unittest.TestCase): ...@@ -151,10 +164,8 @@ class TestPMapping(unittest.TestCase):
u2.clear() u2.clear()
eq(u2, {}, "u2 == {}") eq(u2, {}, "u2 == {}")
def test_suite(): def test_suite():
return unittest.makeSuite(TestPMapping) return unittest.makeSuite(MappingTests)
if __name__ == "__main__": if __name__ == '__main__':
loader = unittest.TestLoader() unittest.main(defaultTest='test_suite')
unittest.main(testLoader=loader)
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