def __setstate__(self, state): """ See IPersistent. """ if isinstance(state,tuple): inst_dict, slots = state else: inst_dict, slots = state, () idict = getattr(self, '__dict__', None) if inst_dict is not None: if idict is None: raise TypeError('No instance dict') idict.clear() for k, v in inst_dict.items(): idict[intern(k)] = v slotnames = self._slotnames() if slotnames: for k, v in slots.items(): setattr(self, k, v)
def __setstate__(self, state): """ See IPersistent. """ if isinstance(state, tuple): inst_dict, slots = state else: inst_dict, slots = state, () idict = getattr(self, '__dict__', None) if inst_dict is not None: if idict is None: raise TypeError('No instance dict') idict.clear() for k, v in inst_dict.items(): idict[intern(k)] = v slotnames = self._slotnames() if slotnames: for k, v in slots.items(): setattr(self, k, v)
def __setstate__(self, state): """ See IPersistent. """ if isinstance(state, tuple): inst_dict, slots = state else: inst_dict, slots = state, () idict = getattr(self, '__dict__', None) if inst_dict is not None: if idict is None: raise TypeError('No instance dict') idict.clear() for k, v in inst_dict.items(): # Normally the keys for instance attributes are interned. # Do that here, but only if it is possible to do so. idict[intern(k) if type(k) is str else k] = v slotnames = self._slotnames() if slotnames: for k, v in slots.items(): setattr(self, k, v)
def __setstate__(self, state): """ See IPersistent. """ if isinstance(state,tuple): inst_dict, slots = state else: inst_dict, slots = state, () idict = getattr(self, '__dict__', None) if inst_dict is not None: if idict is None: raise TypeError('No instance dict') idict.clear() for k, v in inst_dict.items(): # Normally the keys for instance attributes are interned. # Do that here, but only if it is possible to do so. idict[intern(k) if type(k) is str else k] = v slotnames = self._slotnames() if slotnames: for k, v in slots.items(): setattr(self, k, v)
# The KeyError arises in ZODB: ZODB.serialize.ObjectWriter # can assign a jar and an oid to newly seen persistent objects, # but because they are newly created, they aren't in the # pickle cache yet. There doesn't seem to be a way to distinguish # that at this level, all we can do is catch it. # The AttributeError arises in ZODB test cases try: jar._cache.mru(oid) except (AttributeError, KeyError): pass def _p_is_in_cache(self, jar=None): oid = _OGA(self, '_Persistent__oid') if not oid: return False jar = jar or _OGA(self, '_Persistent__jar') cache = getattr(jar, '_cache', None) if cache is not None: return cache.get(oid) is self def _estimated_size_in_24_bits(value): if value > 1073741696: return 16777215 return (value // 64) + 1 _SPECIAL_NAMES.update( [intern('_Persistent' + x) for x in Persistent.__slots__])
# The KeyError arises in ZODB: ZODB.serialize.ObjectWriter # can assign a jar and an oid to newly seen persistent objects, # but because they are newly created, they aren't in the # pickle cache yet. There doesn't seem to be a way to distinguish # that at this level, all we can do is catch it. # The AttributeError arises in ZODB test cases try: jar._cache.mru(oid) except (AttributeError,KeyError): pass def _p_is_in_cache(self, jar=None): oid = _OGA(self, '_Persistent__oid') if not oid: return False jar = jar or _OGA(self, '_Persistent__jar') cache = getattr(jar, '_cache', None) if cache is not None: return cache.get(oid) is self def _estimated_size_in_24_bits(value): if value > 1073741696: return 16777215 return (value//64) + 1 _SPECIAL_NAMES.update([intern('_Persistent' + x) for x in Persistent.__slots__])
# And this is an implementation detail of this class; it holds # the standard names plus the slot names, allowing for just one # check in __getattribute__ _SPECIAL_NAMES = set(SPECIAL_NAMES) # __ring is for use by PickleCachePy and is opaque to us. _SLOTS = ( '__jar', '__oid', '__serial', '__flags', '__size', '__ring', ) _SPECIAL_NAMES.update([intern('_Persistent' + x) for x in _SLOTS]) # Represent 8-byte OIDs as hex integer, just like # ZODB does. _OID_STRUCT = struct.Struct('>Q') _OID_UNPACK = _OID_STRUCT.unpack @use_c_impl @implementer(interfaces.IPersistent) class Persistent(object): """ Pure Python implmentation of Persistent base class """ __slots__ = _SLOTS def __new__(cls, *args, **kw):