Commit cf576871 authored by Jason Madden's avatar Jason Madden

Make deleting the _p_jar of a pure-Python persistent object work like the C version.

parent 2fa92003
``persistent`` Changelog
========================
4.0.8 (Unreleased)
------------------
- Make it possible to delete the ``_p_jar`` of a pure-Python
``Persistent`` object (fixes aborting a ZODB Connection that
has added objects).
4.0.7 (2014-02-20)
------------------
......
......@@ -72,7 +72,16 @@ class Persistent(object):
self.__jar = value
self.__flags = 0
_p_jar = property(_get_jar, _set_jar)
def _del_jar(self):
jar = self.__jar
oid = self.__oid
if jar is not None:
if oid and jar._cache.get(oid):
raise ValueError("can't delete _p_jar of cached object")
self.__setattr__('_Persistent__jar', None)
self.__flags = None
_p_jar = property(_get_jar, _set_jar, _del_jar)
# _p_oid: see IPersistent.
def _get_oid(self):
......
......@@ -86,6 +86,24 @@ class _Persistent_Base(object):
self.assertEqual(inst._p_sticky, False)
self.assertEqual(inst._p_status, 'unsaved')
def test_cannot_del_jar_while_in_cache(self):
inst, _, OID = self._makeOneWithJar()
def _test():
del inst._p_jar
self.assertRaises(ValueError, _test)
def test_del_jar_oid_like_ZODB_abort(self):
# When a ZODB connection aborts,
# it removes registered objects from the cache,
# deletes their jar, deletes their OID
# and finally sets p_changed to false
inst, jar, OID = self._makeOneWithJar()
del jar._cache[OID]
del inst._p_jar
self.assertEqual(inst._p_jar, None)
del inst._p_oid
self.assertEqual(inst._p_oid, None)
def test_assign_p_jar_w_new_jar(self):
inst, jar, OID = self._makeOneWithJar()
new_jar = self._makeJar()
......@@ -1314,11 +1332,17 @@ class PyPersistentTests(unittest.TestCase, _Persistent_Base):
def __init__(self, jar):
self._jar = jar
self._mru = []
self._data = {}
def mru(self, oid):
self._mru.append(oid)
def new_ghost(self, oid, obj):
obj._p_jar = self._jar
obj._p_oid = oid
self._data[oid] = obj
def get(self, oid):
return self._data.get(oid)
def __delitem__(self, oid):
del self._data[oid]
return _Cache(jar)
......
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