Commit d612b6ea authored by Jim Fulton's avatar Jim Fulton

For instances of persistent classes, save a class-module/clas-name

tuple if the class has a non-empty module string.
parent ad1b7691
...@@ -141,10 +141,10 @@ class BaseObjectWriter: ...@@ -141,10 +141,10 @@ class BaseObjectWriter:
>>> jar = DummyJar() >>> jar = DummyJar()
>>> writer = BaseObjectWriter(jar) >>> writer = BaseObjectWriter(jar)
Normally, object references include the oid and a cached Normally, object references include the oid and a cached named
reference to the class. Having the class available allows reference to the class. Having the class information
fast creation of the ghost, avoiding requiring an additional available allows fast creation of the ghost, avoiding
database lookup. requiring an additional database lookup.
>>> bob = P('bob') >>> bob = P('bob')
>>> oid, cls = writer.persistent_id(bob) >>> oid, cls = writer.persistent_id(bob)
...@@ -276,8 +276,16 @@ class BaseObjectWriter: ...@@ -276,8 +276,16 @@ class BaseObjectWriter:
# It's possible that __getnewargs__ is degenerate and # It's possible that __getnewargs__ is degenerate and
# returns (), but we don't want to have to deghostify # returns (), but we don't want to have to deghostify
# the object to find out. # the object to find out.
# Note that this has the odd effect that, if the class has
# __getnewargs__ of its own, we'll lose the optimization
# of caching the class info.
return oid return oid
# Note that we never get here for persistent classes.
# We'll use driect refs for normal classes.
return oid, klass return oid, klass
def serialize(self, obj): def serialize(self, obj):
...@@ -285,13 +293,19 @@ class BaseObjectWriter: ...@@ -285,13 +293,19 @@ class BaseObjectWriter:
# We don't want to be fooled by proxies. # We don't want to be fooled by proxies.
klass = type(obj) klass = type(obj)
# We want to serialize persistent classes by name if they have
# a non-None non-empty module so as not to have a direct
# ref. This is important when copying. We probably want to
# revisit this in the future.
if (isinstance(getattr(klass, '_p_oid', 0), _oidtypes)
and klass.__module__):
klass = klass.__module__, klass.__name__
newargs = getattr(obj, "__getnewargs__", None) newargs = getattr(obj, "__getnewargs__", None)
if newargs is None: if newargs is not None:
meta = klass newargs = newargs()
else:
meta = klass, newargs()
return self._dump(meta, obj.__getstate__()) return self._dump((klass, newargs), obj.__getstate__())
def _dump(self, classmeta, state): def _dump(self, classmeta, state):
# To reuse the existing cStringIO object, we must reset # To reuse the existing cStringIO object, we must reset
...@@ -304,6 +318,8 @@ class BaseObjectWriter: ...@@ -304,6 +318,8 @@ class BaseObjectWriter:
self._file.truncate() self._file.truncate()
return self._file.getvalue() return self._file.getvalue()
_oidtypes = str, type(None)
class ObjectWriter(BaseObjectWriter): class ObjectWriter(BaseObjectWriter):
def __init__(self, obj): def __init__(self, obj):
......
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