Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def getOid(self, obj):
if isinstance(obj, (Persistent, type, wref.WeakRef)):
return getattr(obj, '_p_oid', None)
# XXX should be done by connection
obj._p_oid = oid
obj._p_jar = self._conn
# When an object is created, it is put in the UPTODATE
# state. We must explicitly deactivate it to turn it into
# a ghost.
obj._p_changed = None
self._cache[oid] = obj
return obj
elif isinstance(oid, list):
# see weakref.py
[oid] = oid
obj = WeakRef.__new__(WeakRef)
obj.oid = oid
obj.dm = self._conn
return obj
obj = self._cache.get(oid, None)
if obj is not None:
return obj
return self._conn.get(oid)
>>> 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)
"""
# Most objects are not persistent. The following cheap test
# identifies most of them. For these, we return None,
# signalling that the object should be pickled normally.
if not isinstance(obj, (Persistent, type, WeakRef)):
# Not persistent, pickle normally
return None
# Any persistent object must have an oid:
try:
oid = obj._p_oid
except AttributeError:
# Not persistent, pickle normally
return None
if not (oid is None or isinstance(oid, str)):
# Deserves a closer look:
# Make sure it's not a descr
if hasattr(oid, '__get__'):
# The oid is a decriptor. That means obj is a non-persistent
def __getitem__(self, key):
return self.data[WeakRef(key)]
def update(self, adict):
if isinstance(adict, PersistentWeakKeyDictionary):
self.data.update(adict.data)
else:
for k, v in adict.items():
self.data[WeakRef(k)] = v
def __eq__(self, other):
if not isinstance(other, WeakRef):
return False
self = self()
if self is None:
raise TypeError('Weakly-referenced object has gone away')
other = other()
if other is None:
raise TypeError('Weakly-referenced object has gone away')
return self == other
def get(self, key, default=None):
"""D.get(k[, d]) -> D[k] if k in D, else d.
"""
return self.data.get(WeakRef(key), default)
def __delitem__(self, key):
del self.data[WeakRef(key)]
def __setitem__(self, key, value):
self.data[WeakRef(key)] = value