Commit c732b082 authored by Stephan Richter's avatar Stephan Richter

Merge from trunk.

parent 4f8e418d
...@@ -2,18 +2,18 @@ ...@@ -2,18 +2,18 @@
Persistence support Persistence support
=================== ===================
(This document is under construction. More basic documentation will (This document is under construction. More basic documentation will eventually
eventually appear here.) appear here.)
Overriding __getattr__, __getattribute__, __setattr__, and __delattr__ Overriding `__getattr__`, `__getattribute__`, `__setattr__`, and `__delattr__`
----------------------------------------------------------------------- ------------------------------------------------------------------------------
Subclasses can override the attribute-management methods. For the Subclasses can override the attribute-management methods. For the
__getattr__ method, the behavior is like that for regular Python `__getattr__` method, the behavior is like that for regular Python
classes and for earlier versions of ZODB 3. classes and for earlier versions of ZODB 3.
For __getattribute__, __setattr__, and __delattr__, it is necessary to For `__getattribute__`, __setattr__`, and `__delattr__`, it is necessary
call certain methods defined by persistent.Persistent. Detailed to call certain methods defined by `persistent.Persistent`. Detailed
examples and documentation is provided in the test module, examples and documentation is provided in the test module,
persistent.tests.test_overriding_attrs. `persistent.tests.test_overriding_attrs`.
Tests for persistent.Persistent Tests for `persistent.Persistent`
=============================== =================================
This document is an extended doc test that covers the basics of the This document is an extended doc test that covers the basics of the
Persistent base class. The test expects a class named 'P' to be Persistent base class. The test expects a class named `P` to be
provided in its globals. The P class implements the Persistent provided in its globals. The `P` class implements the `Persistent`
interface. interface.
Test framework Test framework
-------------- --------------
The class P needs to behave like ExampleP. (Note that the code below The class `P` needs to behave like `ExampleP`. (Note that the code below
is *not* part of the tests.) is *not* part of the tests.)
class ExampleP(Persistent): ::
class ExampleP(Persistent):
def __init__(self): def __init__(self):
self.x = 0 self.x = 0
def inc(self): def inc(self):
...@@ -20,433 +22,437 @@ class ExampleP(Persistent): ...@@ -20,433 +22,437 @@ class ExampleP(Persistent):
The tests use stub data managers. A data manager is responsible for The tests use stub data managers. A data manager is responsible for
loading and storing the state of a persistent object. It's stored in loading and storing the state of a persistent object. It's stored in
the _p_jar attribute of a persistent object. the ``_p_jar`` attribute of a persistent object.
>>> class DM:
... def __init__(self):
... self.called = 0
... def register(self, ob):
... self.called += 1
... def setstate(self, ob):
... ob.__setstate__({'x': 42})
>>> class DM: >>> class BrokenDM(DM):
... def __init__(self): ... def register(self,ob):
... self.called = 0 ... self.called += 1
... def register(self, ob): ... raise NotImplementedError
... self.called += 1 ... def setstate(self,ob):
... def setstate(self, ob): ... raise NotImplementedError
... ob.__setstate__({'x': 42})
>>> class BrokenDM(DM): >>> from persistent import Persistent
... def register(self,ob):
... self.called += 1
... raise NotImplementedError
... def setstate(self,ob):
... raise NotImplementedError
>>> from persistent import Persistent
Test Persistent without Data Manager Test Persistent without Data Manager
------------------------------------ ------------------------------------
First do some simple tests of a Persistent instance that does not have First do some simple tests of a Persistent instance that does not have
a data manager (_p_jar). a data manager (``_p_jar``).
>>> p = P() >>> p = P()
>>> p.x >>> p.x
0 0
>>> p._p_changed >>> p._p_changed
False False
>>> p._p_state >>> p._p_state
0 0
>>> p._p_jar >>> p._p_jar
>>> p._p_oid >>> p._p_oid
Verify that modifications have no effect on _p_state of _p_changed. Verify that modifications have no effect on ``_p_state`` of ``_p_changed``.
>>> p.inc() >>> p.inc()
>>> p.inc() >>> p.inc()
>>> p.x >>> p.x
2 2
>>> p._p_changed >>> p._p_changed
False False
>>> p._p_state >>> p._p_state
0 0
Try all sorts of different ways to change the object's state. Try all sorts of different ways to change the object's state.
>>> p._p_deactivate() >>> p._p_deactivate()
>>> p._p_state >>> p._p_state
0 0
>>> p._p_changed = True >>> p._p_changed = True
>>> p._p_state >>> p._p_state
0 0
>>> del p._p_changed >>> del p._p_changed
>>> p._p_changed >>> p._p_changed
False False
>>> p._p_state >>> p._p_state
0 0
>>> p.x >>> p.x
2 2
Test Persistent with Data Manager Test Persistent with Data Manager
--------------------------------- ---------------------------------
Next try some tests of an object with a data manager. The DM class is Next try some tests of an object with a data manager. The `DM` class is
a simple testing stub. a simple testing stub.
>>> p = P() >>> p = P()
>>> dm = DM() >>> dm = DM()
>>> p._p_oid = "00000012" >>> p._p_oid = "00000012"
>>> p._p_jar = dm >>> p._p_jar = dm
>>> p._p_changed >>> p._p_changed
0 0
>>> dm.called >>> dm.called
0 0
Modifying the object marks it as changed and registers it with the Modifying the object marks it as changed and registers it with the data
data manager. Subsequent modifications don't have additional manager. Subsequent modifications don't have additional side-effects.
side-effects.
>>> p.inc()
>>> p.inc() >>> p._p_changed
>>> p._p_changed 1
1 >>> dm.called
>>> dm.called 1
1 >>> p.inc()
>>> p.inc() >>> p._p_changed
>>> p._p_changed 1
1 >>> dm.called
>>> dm.called 1
1
It's not possible to deactivate a modified object. It's not possible to deactivate a modified object.
>>> p._p_deactivate() >>> p._p_deactivate()
>>> p._p_changed >>> p._p_changed
1 1
It is possible to invalidate it. That's the key difference It is possible to invalidate it. That's the key difference between
between deactivation and invalidation. deactivation and invalidation.
>>> p._p_invalidate() >>> p._p_invalidate()
>>> p._p_state >>> p._p_state
-1 -1
Now that the object is a ghost, any attempt to modify it will Now that the object is a ghost, any attempt to modify it will require that it
require that it be unghosted first. The test data manager be unghosted first. The test data manager has the odd property that it sets
has the odd property that it sets the object's 'x' attribute the object's ``x`` attribute to ``42`` when it is unghosted.
to 42 when it is unghosted.
>>> p.inc()
>>> p.inc() >>> p.x
>>> p.x 43
43 >>> dm.called
>>> dm.called 2
2
You can manually reset the changed field to ``False``, although it's not clear
You can manually reset the changed field to False, although why you would want to do that. The object changes to the ``UPTODATE`` state
it's not clear why you would want to do that. The object but retains its modifications.
changes to the UPTODATE state but retains its modifications.
>>> p._p_changed = False
>>> p._p_changed = False >>> p._p_state
>>> p._p_state 0
0 >>> p._p_changed
>>> p._p_changed False
False >>> p.x
>>> p.x 43
43
>>> p.inc()
>>> p.inc() >>> p._p_changed
>>> p._p_changed True
True >>> dm.called
>>> dm.called 3
3
``__getstate__()`` and ``__setstate__()``
__getstate__() and __setstate__() -----------------------------------------
---------------------------------
The next several tests cover the ``__getstate__()`` and ``__setstate__()``
The next several tests cover the __getstate__() and __setstate__()
implementations. implementations.
>>> p = P() >>> p = P()
>>> state = p.__getstate__() >>> state = p.__getstate__()
>>> isinstance(state, dict) >>> isinstance(state, dict)
True True
>>> state['x'] >>> state['x']
0 0
>>> p._p_state >>> p._p_state
0 0
Calling setstate always leaves the object in the uptodate state? Calling setstate always leaves the object in the uptodate state?
(I'm not entirely clear on this one.) (I'm not entirely clear on this one.)
>>> p.__setstate__({'x': 5}) >>> p.__setstate__({'x': 5})
>>> p._p_state >>> p._p_state
0 0
Assigning to a volatile attribute has no effect on the object state. Assigning to a volatile attribute has no effect on the object state.
>>> p._v_foo = 2 >>> p._v_foo = 2
>>> p.__getstate__() >>> p.__getstate__()
{'x': 5} {'x': 5}
>>> p._p_state >>> p._p_state
0 0
The _p_serial attribute is not affected by calling setstate. The ``_p_serial`` attribute is not affected by calling setstate.
>>> p._p_serial = "00000012"
>>> p.__setstate__(p.__getstate__())
>>> p._p_serial
'00000012'
>>> p._p_serial = "00000012"
>>> p.__setstate__(p.__getstate__())
>>> p._p_serial
'00000012'
Change Ghost test Change Ghost test
----------------- -----------------
If an object is a ghost and its _p_changed is set to True (any true value), If an object is a ghost and its ``_p_changed`` is set to ``True`` (any true
it should activate (unghostify) the object. This behavior is new in ZODB value), it should activate (unghostify) the object. This behavior is new in
3.6; before then, an attempt to do "ghost._p_changed = True" was ignored. ZODB 3.6; before then, an attempt to do ``ghost._p_changed = True`` was
ignored.
>>> p = P()
>>> p._p_jar = DM() >>> p = P()
>>> p._p_oid = 1 >>> p._p_jar = DM()
>>> p._p_deactivate() >>> p._p_oid = 1
>>> p._p_changed # None >>> p._p_deactivate()
>>> p._p_state # ghost state >>> p._p_changed # None
-1 >>> p._p_state # ghost state
>>> p._p_changed = True -1
>>> p._p_changed >>> p._p_changed = True
1 >>> p._p_changed
>>> p._p_state # changed state 1
1 >>> p._p_state # changed state
>>> p.x 1
42 >>> p.x
42
Activate, deactivate, and invalidate Activate, deactivate, and invalidate
------------------------------------ ------------------------------------
Some of these tests are redundant, but are included to make sure there Some of these tests are redundant, but are included to make sure there
are explicit and simple tests of _p_activate(), _p_deactivate(), and are explicit and simple tests of ``_p_activate()``, ``_p_deactivate()``, and
_p_invalidate(). ``_p_invalidate()``.
>>> p = P() >>> p = P()
>>> p._p_oid = 1 >>> p._p_oid = 1
>>> p._p_jar = DM() >>> p._p_jar = DM()
>>> p._p_deactivate() >>> p._p_deactivate()
>>> p._p_state >>> p._p_state
-1 -1
>>> p._p_activate() >>> p._p_activate()
>>> p._p_state >>> p._p_state
0 0
>>> p.x >>> p.x
42 42
>>> p.inc() >>> p.inc()
>>> p.x >>> p.x
43 43
>>> p._p_state >>> p._p_state
1 1
>>> p._p_invalidate() >>> p._p_invalidate()
>>> p._p_state >>> p._p_state
-1 -1
>>> p.x >>> p.x
42 42
Test failures Test failures
------------- -------------
The following tests cover various errors cases. The following tests cover various errors cases.
When an object is modified, it registers with its data manager. If When an object is modified, it registers with its data manager. If that
that registration fails, the exception is propagated and the object registration fails, the exception is propagated and the object stays in the
stays in the up-to-date state. It shouldn't change to the modified up-to-date state. It shouldn't change to the modified state, because it won't
state, because it won't be saved when the transaction commits. be saved when the transaction commits.
>>> p = P() >>> p = P()
>>> p._p_oid = 1 >>> p._p_oid = 1
>>> p._p_jar = BrokenDM() >>> p._p_jar = BrokenDM()
>>> p._p_state >>> p._p_state
0 0
>>> p._p_jar.called >>> p._p_jar.called
0 0
>>> p._p_changed = 1 >>> p._p_changed = 1
Traceback (most recent call last): Traceback (most recent call last):
... ...
NotImplementedError NotImplementedError
>>> p._p_jar.called >>> p._p_jar.called
1 1
>>> p._p_state >>> p._p_state
0 0
Make sure that exceptions that occur inside the data manager's Make sure that exceptions that occur inside the data manager's ``setstate()``
setstate() method propagate out to the caller. method propagate out to the caller.
>>> p = P() >>> p = P()
>>> p._p_oid = 1 >>> p._p_oid = 1
>>> p._p_jar = BrokenDM() >>> p._p_jar = BrokenDM()
>>> p._p_deactivate() >>> p._p_deactivate()
>>> p._p_state >>> p._p_state
-1 -1
>>> p._p_activate() >>> p._p_activate()
Traceback (most recent call last): Traceback (most recent call last):
... ...
NotImplementedError NotImplementedError
>>> p._p_state >>> p._p_state
-1 -1
Special test to cover layout of __dict__ Special test to cover layout of ``__dict__``
---------------------------------------- --------------------------------------------
We once had a bug in the Persistent class that calculated an incorrect We once had a bug in the `Persistent` class that calculated an incorrect
offset for the __dict__ attribute. It assigned __dict__ and _p_jar to offset for the ``__dict__`` attribute. It assigned ``__dict__`` and
the same location in memory. This is a simple test to make sure they ``_p_jar`` to the same location in memory. This is a simple test to make sure
have different locations. they have different locations.
>>> p = P() >>> p = P()
>>> p.inc() >>> p.inc()
>>> p.inc() >>> p.inc()
>>> 'x' in p.__dict__ >>> 'x' in p.__dict__
True True
>>> p._p_jar >>> p._p_jar
Inheritance and metaclasses Inheritance and metaclasses
--------------------------- ---------------------------
Simple tests to make sure it's possible to inherit from the Persistent Simple tests to make sure it's possible to inherit from the `Persistent` base
base class multiple times. There used to be metaclasses involved in class multiple times. There used to be metaclasses involved in `Persistent`
Persistent that probably made this a more interesting test. that probably made this a more interesting test.
>>> class A(Persistent): >>> class A(Persistent):
... pass ... pass
>>> class B(Persistent): >>> class B(Persistent):
... pass ... pass
>>> class C(A, B): >>> class C(A, B):
... pass ... pass
>>> class D(object): >>> class D(object):
... pass ... pass
>>> class E(D, B): >>> class E(D, B):
... pass ... pass
>>> a = A() >>> a = A()
>>> b = B() >>> b = B()
>>> c = C() >>> c = C()
>>> d = D() >>> d = D()
>>> e = E() >>> e = E()
Also make sure that it's possible to define Persistent classes that Also make sure that it's possible to define `Persistent` classes that have a
have a custom metaclass. custom metaclass.
>>> class alternateMeta(type): >>> class alternateMeta(type):
... type ... type
>>> class alternate(object): >>> class alternate(object):
... __metaclass__ = alternateMeta ... __metaclass__ = alternateMeta
>>> class mixedMeta(alternateMeta, type): >>> class mixedMeta(alternateMeta, type):
... pass ... pass
>>> class mixed(alternate, Persistent): >>> class mixed(alternate, Persistent):
... pass ... pass
>>> class mixed(Persistent, alternate): >>> class mixed(Persistent, alternate):
... pass ... pass
Basic type structure Basic type structure
-------------------- --------------------
>>> Persistent.__dictoffset__ >>> Persistent.__dictoffset__
0 0
>>> Persistent.__weakrefoffset__ >>> Persistent.__weakrefoffset__
0 0
>>> Persistent.__basicsize__ > object.__basicsize__ >>> Persistent.__basicsize__ > object.__basicsize__
True True
>>> P.__dictoffset__ > 0 >>> P.__dictoffset__ > 0
True True
>>> P.__weakrefoffset__ > 0 >>> P.__weakrefoffset__ > 0
True True
>>> P.__dictoffset__ < P.__weakrefoffset__ >>> P.__dictoffset__ < P.__weakrefoffset__
True True
>>> P.__basicsize__ > Persistent.__basicsize__ >>> P.__basicsize__ > Persistent.__basicsize__
True True
Slots Slots
----- -----
These are some simple tests of classes that have an __slots__ These are some simple tests of classes that have an ``__slots__``
attribute. Some of the classes should have slots, others shouldn't. attribute. Some of the classes should have slots, others shouldn't.
>>> class noDict(object): >>> class noDict(object):
... __slots__ = ['foo'] ... __slots__ = ['foo']
>>> class p_noDict(Persistent): >>> class p_noDict(Persistent):
... __slots__ = ['foo'] ... __slots__ = ['foo']
>>> class p_shouldHaveDict(p_noDict): >>> class p_shouldHaveDict(p_noDict):
... pass ... pass
>>> p_noDict.__dictoffset__ >>> p_noDict.__dictoffset__
0 0
>>> x = p_noDict() >>> x = p_noDict()
>>> x.foo = 1 >>> x.foo = 1
>>> x.foo >>> x.foo
1 1
>>> x.bar = 1 >>> x.bar = 1
Traceback (most recent call last): Traceback (most recent call last):
... ...
AttributeError: 'p_noDict' object has no attribute 'bar' AttributeError: 'p_noDict' object has no attribute 'bar'
>>> x._v_bar = 1 >>> x._v_bar = 1
Traceback (most recent call last): Traceback (most recent call last):
... ...
AttributeError: 'p_noDict' object has no attribute '_v_bar' AttributeError: 'p_noDict' object has no attribute '_v_bar'
>>> x.__dict__ >>> x.__dict__
Traceback (most recent call last): Traceback (most recent call last):
... ...
AttributeError: 'p_noDict' object has no attribute '__dict__' AttributeError: 'p_noDict' object has no attribute '__dict__'
The various _p_ attributes are unaffected by slots. The various _p_ attributes are unaffected by slots.
>>> p._p_oid >>> p._p_oid
>>> p._p_jar >>> p._p_jar
>>> p._p_state >>> p._p_state
0 0
If the most-derived class does not specify If the most-derived class does not specify
>>> p_shouldHaveDict.__dictoffset__ > 0 >>> p_shouldHaveDict.__dictoffset__ > 0
True True
>>> x = p_shouldHaveDict() >>> x = p_shouldHaveDict()
>>> isinstance(x.__dict__, dict) >>> isinstance(x.__dict__, dict)
True True
Pickling Pickling
-------- --------
There's actually a substantial effort involved in making subclasses of There's actually a substantial effort involved in making subclasses of
Persistent work with plain-old pickle. The ZODB serialization layer `Persistent` work with plain-old pickle. The ZODB serialization layer never
never calls pickle on an object; it pickles the object's class calls pickle on an object; it pickles the object's class description and its
description and its state as two separate pickles. state as two separate pickles.
>>> import pickle >>> import pickle
>>> p = P() >>> p = P()
>>> p.inc() >>> p.inc()
>>> p2 = pickle.loads(pickle.dumps(p)) >>> p2 = pickle.loads(pickle.dumps(p))
>>> p2.__class__ is P >>> p2.__class__ is P
True True
>>> p2.x == p.x >>> p2.x == p.x
True True
We should also test that pickle works with custom getstate and We should also test that pickle works with custom getstate and setstate.
setstate. Perhaps even reduce. The problem is that pickling depends Perhaps even reduce. The problem is that pickling depends on finding the
on finding the class in a particular module, and classes defined here class in a particular module, and classes defined here won't appear in any
won't appear in any module. We could require each user of the tests module. We could require each user of the tests to define a base class, but
to define a base class, but that might be tedious. that might be tedious.
Interfaces Interfaces
---------- ----------
Some versions of Zope and ZODB have the zope.interfaces package Some versions of Zope and ZODB have the `zope.interfaces` package available.
available. If it is available, then persistent will be associated If it is available, then persistent will be associated with several
with several interfaces. It's hard to write a doctest test that runs interfaces. It's hard to write a doctest test that runs the tests only if
the tests only if zope.interface is available, so this test looks a `zope.interface` is available, so this test looks a little unusual. One
little unusual. One problem is that the assert statements won't do problem is that the assert statements won't do anything if you run with `-O`.
anything if you run with -O.
>>> try:
>>> try: ... import zope.interface
... import zope.interface ... except ImportError:
... except ImportError: ... pass
... pass ... else:
... else: ... from persistent.interfaces import IPersistent
... from persistent.interfaces import IPersistent ... assert IPersistent.implementedBy(Persistent)
... assert IPersistent.implementedBy(Persistent) ... p = Persistent()
... p = Persistent() ... assert IPersistent.providedBy(p)
... assert IPersistent.providedBy(p) ... assert IPersistent.implementedBy(P)
... assert IPersistent.implementedBy(P) ... p = P()
... p = P() ... assert IPersistent.providedBy(p)
... assert IPersistent.providedBy(p)
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