Commit 5941598b authored by Jason Madden's avatar Jason Madden

Make pure-python Persistent only call jar.setstate() when it is a ghost instead of every time.

parent 6e813bf4
......@@ -4,6 +4,11 @@
4.0.8 (Unreleased)
------------------
- In pure-Python ``Persistent``, calling ``_p_activate`` no longer
corrupts the state of a non-ghost object, and it doesn't throw
``POSKeyError`` if called on an object that has never been committed.
(PR #9)
- In pure-Python ``Persistent``, avoid calling a subclass's ``__setattr__``
at instance creation time. (PR #8)
......
......@@ -330,14 +330,14 @@ class Persistent(object):
""" See IPersistent.
"""
before = self.__flags
if self.__flags is None:
if self.__flags is None or self._p_state < 0: # Only do this if we're a ghost
self.__flags = 0
if self.__jar is not None and self.__oid is not None:
try:
self.__jar.setstate(self)
except:
self.__flags = before
raise
if self.__jar is not None and self.__oid is not None:
try:
self.__jar.setstate(self)
except:
self.__flags = before
raise
def _p_deactivate(self):
""" See IPersistent.
......
......@@ -978,6 +978,19 @@ class _Persistent_Base(object):
inst._p_activate() # noop from 'saved' state
self.assertEqual(inst._p_status, 'saved')
def test__p_activate_only_sets_state_once(self):
inst, jar, OID = self._makeOneWithJar()
# No matter how many times we call _p_activate, it
# only sets state once, the first time
inst._p_invalidate() # make it a ghost
self.assertEqual(list(jar._loaded), [])
inst._p_activate()
self.assertEqual(list(jar._loaded), [OID])
inst._p_activate()
self.assertEqual(list(jar._loaded), [OID])
def test__p_deactivate_from_unsaved(self):
inst = self._makeOne()
inst._p_deactivate()
......
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