/usr/lib/python2.7/dist-packages/persistent/persistence.py is in python-persistent 4.0.8-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | ##############################################################################
#
# Copyright (c) 2011 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import sys
from zope.interface import implementer
from persistent.interfaces import IPersistent
from persistent.interfaces import GHOST
from persistent.interfaces import UPTODATE
from persistent.interfaces import CHANGED
from persistent.interfaces import STICKY
from persistent.interfaces import OID_TYPE
from persistent.interfaces import SERIAL_TYPE
from persistent.timestamp import TimeStamp
from persistent.timestamp import _ZERO
from persistent._compat import copy_reg
_INITIAL_SERIAL = _ZERO
# Bitwise flags
_CHANGED = 0x0001
_STICKY = 0x0002
_OGA = object.__getattribute__
_OSA = object.__setattr__
# These names can be used from a ghost without causing it to be activated.
SPECIAL_NAMES = ('__class__',
'__del__',
'__dict__',
'__of__',
'__setstate__'
)
@implementer(IPersistent)
class Persistent(object):
""" Pure Python implmentation of Persistent base class
"""
__slots__ = ('__jar', '__oid', '__serial', '__flags', '__size')
def __new__(cls, *args, **kw):
inst = super(Persistent, cls).__new__(cls)
# We bypass the __setattr__ implementation of this object
# at __new__ time, just like the C implementation does. This
# makes us compatible with subclasses that want to access
# properties like _p_changed in their setattr implementation
_OSA(inst, '_Persistent__jar', None)
_OSA(inst, '_Persistent__oid', None)
_OSA(inst, '_Persistent__serial', None)
_OSA(inst, '_Persistent__flags', None)
_OSA(inst, '_Persistent__size', 0)
return inst
# _p_jar: see IPersistent.
def _get_jar(self):
return self.__jar
def _set_jar(self, value):
if self.__jar is not None:
if self.__jar != value:
raise ValueError('Already assigned a data manager')
else:
self.__jar = value
self.__flags = 0
def _del_jar(self):
jar = self.__jar
oid = self.__oid
if jar is not None:
if oid and jar._cache.get(oid):
raise ValueError("can't delete _p_jar of cached object")
self.__setattr__('_Persistent__jar', None)
self.__flags = None
_p_jar = property(_get_jar, _set_jar, _del_jar)
# _p_oid: see IPersistent.
def _get_oid(self):
return self.__oid
def _set_oid(self, value):
if value == self.__oid:
return
if value is not None:
if not isinstance(value, OID_TYPE):
raise ValueError('Invalid OID type: %s' % value)
if self.__jar is not None and self.__oid is not None:
raise ValueError('Already assigned an OID by our jar')
self.__oid = value
def _del_oid(self):
jar = self.__jar
oid = self.__oid
if jar is not None:
if oid and jar._cache.get(oid):
raise ValueError('Cannot delete _p_oid of cached object')
self.__oid = None
_p_oid = property(_get_oid, _set_oid, _del_oid)
# _p_serial: see IPersistent.
def _get_serial(self):
if self.__serial is not None:
return self.__serial
return _INITIAL_SERIAL
def _set_serial(self, value):
if not isinstance(value, SERIAL_TYPE):
raise ValueError('Invalid SERIAL type: %s' % value)
if len(value) != 8:
raise ValueError('SERIAL must be 8 octets')
self.__serial = value
def _del_serial(self):
self.__serial = None
_p_serial = property(_get_serial, _set_serial, _del_serial)
# _p_changed: see IPersistent.
def _get_changed(self):
if self.__jar is None:
return False
if self.__flags is None: # ghost
return None
return bool(self.__flags & _CHANGED)
def _set_changed(self, value):
if self.__flags is None:
if value:
self._p_activate()
self._p_set_changed_flag(value)
else:
if value is None: # -> ghost
self._p_deactivate()
else:
self._p_set_changed_flag(value)
def _del_changed(self):
self._p_invalidate()
_p_changed = property(_get_changed, _set_changed, _del_changed)
# _p_mtime
def _get_mtime(self):
if self.__serial is not None:
ts = TimeStamp(self.__serial)
return ts.timeTime()
_p_mtime = property(_get_mtime)
# _p_state
def _get_state(self):
if self.__jar is None:
return UPTODATE
if self.__flags is None:
return GHOST
if self.__flags & _CHANGED:
result = CHANGED
else:
result = UPTODATE
if self.__flags & _STICKY:
return STICKY
return result
_p_state = property(_get_state)
# _p_estimated_size: XXX don't want to reserve the space?
def _get_estimated_size(self):
return self.__size * 64
def _set_estimated_size(self, value):
if isinstance(value, int):
if value < 0:
raise ValueError('_p_estimated_size must not be negative')
self.__size = _estimated_size_in_24_bits(value)
else:
raise TypeError("_p_estimated_size must be an integer")
def _del_estimated_size(self):
self.__size = 0
_p_estimated_size = property(
_get_estimated_size, _set_estimated_size, _del_estimated_size)
# The '_p_sticky' property is not (yet) part of the API: for now,
# it exists to simplify debugging and testing assertions.
def _get_sticky(self):
if self.__flags is None:
return False
return bool(self.__flags & _STICKY)
def _set_sticky(self, value):
if self.__flags is None:
raise ValueError('Ghost')
if value:
self.__flags |= _STICKY
else:
self.__flags &= ~_STICKY
_p_sticky = property(_get_sticky, _set_sticky)
# The '_p_status' property is not (yet) part of the API: for now,
# it exists to simplify debugging and testing assertions.
def _get_status(self):
if self.__jar is None:
return 'unsaved'
if self.__flags is None:
return 'ghost'
if self.__flags & _STICKY:
return 'sticky'
if self.__flags & _CHANGED:
return 'changed'
return 'saved'
_p_status = property(_get_status)
# Methods from IPersistent.
def __getattribute__(self, name):
""" See IPersistent.
"""
if (not name.startswith('_Persistent__') and
not name.startswith('_p_') and
name not in SPECIAL_NAMES):
if _OGA(self, '_Persistent__flags') is None:
_OGA(self, '_p_activate')()
_OGA(self, '_p_accessed')()
return _OGA(self, name)
def __setattr__(self, name, value):
special_name = (name.startswith('_Persistent__') or
name.startswith('_p_'))
volatile = name.startswith('_v_')
if not special_name:
if _OGA(self, '_Persistent__flags') is None:
_OGA(self, '_p_activate')()
if not volatile:
_OGA(self, '_p_accessed')()
_OSA(self, name, value)
if (_OGA(self, '_Persistent__jar') is not None and
_OGA(self, '_Persistent__oid') is not None and
not special_name and
not volatile):
before = _OGA(self, '_Persistent__flags')
after = before | _CHANGED
if before != after:
_OSA(self, '_Persistent__flags', after)
_OGA(self, '_p_register')()
def __delattr__(self, name):
special_name = (name.startswith('_Persistent__') or
name.startswith('_p_'))
if not special_name:
if _OGA(self, '_Persistent__flags') is None:
_OGA(self, '_p_activate')()
_OGA(self, '_p_accessed')()
before = _OGA(self, '_Persistent__flags')
after = before | _CHANGED
if before != after:
_OSA(self, '_Persistent__flags', after)
if (_OGA(self, '_Persistent__jar') is not None and
_OGA(self, '_Persistent__oid') is not None):
_OGA(self, '_p_register')()
object.__delattr__(self, name)
def _slotnames(self):
slotnames = copy_reg._slotnames(type(self))
return [x for x in slotnames
if not x.startswith('_p_') and
not x.startswith('_v_') and
not x.startswith('_Persistent__') and
x not in Persistent.__slots__]
def __getstate__(self):
""" See IPersistent.
"""
idict = getattr(self, '__dict__', None)
slotnames = self._slotnames()
if idict is not None:
d = dict([x for x in idict.items()
if not x[0].startswith('_p_') and
not x[0].startswith('_v_')])
else:
d = None
if slotnames:
s = {}
for slotname in slotnames:
value = getattr(self, slotname, self)
if value is not self:
s[slotname] = value
return d, s
return d
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()
idict.update(inst_dict)
slotnames = self._slotnames()
if slotnames:
for k, v in slots.items():
setattr(self, k, v)
def __reduce__(self):
""" See IPersistent.
"""
gna = getattr(self, '__getnewargs__', lambda: ())
return (copy_reg.__newobj__,
(type(self),) + gna(), self.__getstate__())
def _p_activate(self):
""" See IPersistent.
"""
before = self.__flags
if self.__flags is None or self._p_state < 0: # Only do this if we're a ghost
self.__flags = 0
if self.__jar is not None and self.__oid is not None:
try:
self.__jar.setstate(self)
except:
self.__flags = before
raise
def _p_deactivate(self):
""" See IPersistent.
"""
if self.__flags is not None and not self.__flags:
self._p_invalidate()
def _p_invalidate(self):
""" See IPersistent.
"""
if self.__jar is not None:
if self.__flags is not None:
self.__flags = None
idict = getattr(self, '__dict__', None)
if idict is not None:
idict.clear()
def _p_getattr(self, name):
""" See IPersistent.
"""
if name.startswith('_p_') or name in SPECIAL_NAMES:
return True
self._p_activate()
self._p_accessed()
return False
def _p_setattr(self, name, value):
""" See IPersistent.
"""
if name.startswith('_p_'):
setattr(self, name, value)
return True
self._p_activate()
self._p_accessed()
return False
def _p_delattr(self, name):
""" See IPersistent.
"""
if name.startswith('_p_'):
delattr(self, name)
return True
self._p_activate()
self._p_accessed()
return False
# Helper methods: not APIs: we name them with '_p_' to bypass
# the __getattribute__ bit which bumps the cache.
def _p_register(self):
if self.__jar is not None and self.__oid is not None:
self.__jar.register(self)
def _p_set_changed_flag(self, value):
if value:
before = self.__flags
after = self.__flags | _CHANGED
if before != after:
self._p_register()
self.__flags = after
else:
self.__flags &= ~_CHANGED
def _p_accessed(self):
# Notify the jar's pickle cache that we have been accessed.
# This relies on what has been (until now) an implementation
# detail, the '_cache' attribute of the jar. We made it a
# private API to avoid the cycle of keeping a reference to
# the cache on the persistent object.
if (self.__jar is not None and
self.__oid is not None and
self._p_state >= 0):
# This scenario 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
try:
self.__jar._cache.mru(self.__oid)
except KeyError:
pass
def _estimated_size_in_24_bits(value):
if value > 1073741696:
return 16777215
return (value//64) + 1
|