Serializes objects for storage in the database.
The ObjectWriter creates object pickles in the ZODB format. It also detects new persistent objects reachable from the current object.
There are no base classes.
There are no implemented interfaces.
There are no attributes in this class.
persistent_id(obj)
Return the persistent id for obj.
>>> from ZODB.tests.util import P >>> class DummyJar: ... def new_oid(self): ... return 42 ... def db(self): ... return self ... databases = {}
>>> jar = DummyJar() >>> class O: ... _p_jar = jar >>> writer = ObjectWriter(O)
Normally, object references include the oid and a cached named reference to the class. Having the class information available allows fast creation of the ghost, avoiding requiring an additional database lookup.
>>> bob = P(bob
)
>>> oid, cls = writer.persistent_id(bob)
>>> oid
42
>>> cls is P
True
If a persistent object does not already have an oid and jar, these will be assigned by persistent_id():
>>> bob._p_oid 42 >>> bob._p_jar is jar True
If the object already has a persistent id, the id is not changed:
>>> bob._p_oid = 24 >>> oid, cls = writer.persistent_id(bob) >>> oid 24 >>> cls is P True
If the jar doesn't match that of the writer, an error is raised:
>>> bob._p_jar = DummyJar() >>> writer.persistent_id(bob) Traceback (most recent call last): ... InvalidObjectReference: Attempt to store an object from a foreign database connection
Constructor arguments used by __new__(), as returned by __getnewargs__(), can affect memory allocation, but may also change over the life of the object. This makes it useless to cache even the object's class.
>>> class PNewArgs(P): ... def __getnewargs__(self): ... return ()
>>> sam = PNewArgs(sam
)
>>> writer.persistent_id(sam)
42
>>> sam._p_oid
42
>>> sam._p_jar is jar
True
Check that simple objects don't get accused of persistence:
>>> writer.persistent_id(42) >>> writer.persistent_id(object())
Check that a classic class doesn't get identified improperly:
>>> class ClassicClara: ... pass >>> clara = ClassicClara()
>>> writer.persistent_id(clara)
serialize(obj)
There are no known subclasses.