def test___setstate___empty(self): from persistent.wref import WeakRef from persistent._compat import _b jar = _makeJar() KEY = _b('KEY') KEY2 = _b('KEY2') KEY3 = _b('KEY3') VALUE = _b('VALUE') VALUE2 = _b('VALUE2') VALUE3 = _b('VALUE3') key = jar[KEY] = _makeTarget(oid=KEY) key._p_jar = jar kref = WeakRef(key) value = jar[VALUE] = _makeTarget(oid=VALUE) value._p_jar = jar key2 = _makeTarget(oid=KEY2) key2._p_jar = jar # not findable kref2 = WeakRef(key2) del kref2._v_ob # force a miss value2 = jar[VALUE2] = _makeTarget(oid=VALUE2) value2._p_jar = jar key3 = jar[KEY3] = _makeTarget(oid=KEY3) # findable key3._p_jar = jar kref3 = WeakRef(key3) del kref3._v_ob # force a miss, but win in the lookup value3 = jar[VALUE3] = _makeTarget(oid=VALUE3) value3._p_jar = jar pwkd = self._makeOne(None) pwkd.__setstate__( {'data': [(kref, value), (kref2, value2), (kref3, value3)]}) self.assertTrue(pwkd[key] is value) self.assertTrue(pwkd.get(key2) is None) self.assertTrue(pwkd[key3] is value3)
def test_to_externalOID(self): class P(Persistent): def toExternalOID(self, **_kwargs): return b'abc' wref = PWeakRef(P()) assert_that(wref.toExternalOID(), is_(b'abc'))
def test_to_externalObject(self): class P(Persistent): def toExternalObject(self, **_kwargs): return {'a': 42} wref = PWeakRef(P()) assert_that(wref.toExternalObject(), is_({'a': 42}))
def make_one(self): db = DB(None) conn = db.open() pers = Persistent() conn.add(pers) orig_wref = WeakRef(pers) return orig_wref
def test___getstate___filled(self): from persistent.wref import WeakRef jar = _makeJar() key = jar['key'] = _makeTarget(oid='KEY') key._p_jar = jar value = jar['value'] = _makeTarget(oid='VALUE') value._p_jar = jar pwkd = self._makeOne([(key, value)]) self.assertEqual(pwkd.__getstate__(), {'data': [(WeakRef(key), value)]})
def load_persistent_weakref(self, oid, database_name=None): obj = WeakRef.__new__(WeakRef) obj.oid = oid if database_name is None: obj.dm = self._conn else: obj.database_name = database_name try: obj.dm = self._conn.get_connection(database_name) except KeyError: # XXX Not sure what to do here. It seems wrong to # fail since this is a weak reference. For now we'll # just pretend that the target object has gone. pass return obj
def load_persistent_weakref(self, oid, database_name=None): if not isinstance(oid, bytes): assert isinstance(oid, str) # this happens on Python 3 when all bytes in the oid are < 0x80 oid = oid.encode("ascii") obj = WeakRef.__new__(WeakRef) obj.oid = oid if database_name is None: obj.dm = self._conn else: obj.database_name = database_name try: obj.dm = self._conn.get_connection(database_name) except KeyError: # XXX Not sure what to do here. It seems wrong to # fail since this is a weak reference. For now we'll # just pretend that the target object has gone. pass return obj
def test_persistent_id_noload(self): # make sure we can noload weak references and other list-based # references like we expect. Protect explicitly against the # breakage in CPython 2.7 and zodbpickle < 0.6.0 o = PersistentObject() o._p_oid = b'abcd' top = PersistentObject() top._p_oid = b'efgh' top.ref = WeakRef(o) pickle = serialize.ObjectWriter().serialize(top) refs = [] u = PersistentUnpickler(None, refs.append, BytesIO(pickle)) u.noload() u.noload() self.assertEqual(refs, [['w', (b'abcd', )]])
def load_persistent_weakref(self, oid, database_name=None): if not isinstance(oid, bytes): assert isinstance(oid, str) # this happens on Python 3 when all bytes in the oid are < 0x80 oid = oid.encode('ascii') obj = WeakRef.__new__(WeakRef) obj.oid = oid if database_name is None: obj.dm = self._conn else: obj.database_name = database_name try: obj.dm = self._conn.get_connection(database_name) except KeyError: # XXX Not sure what to do here. It seems wrong to # fail since this is a weak reference. For now we'll # just pretend that the target object has gone. pass return obj
def factory(toplevel, register): # We do need a new object, presumably we're moving databases return WeakRef(ref())
def __init__(self, resource): try: self.ref = WeakRef(resource) except Exception: self.ref = resource
def load_persistent_weakref(self, oid): obj = WeakRef.__new__(WeakRef) obj.oid = oid obj.dm = self._conn return obj