How to detect wrapped or persistent objects in REQUEST and SESSION
How to detect persisten or wrapped objects in REQUEST and SESSSION
# (c) 2004 Christian Heimes # GPL # Put these lines somewhere in a python file and import it # It will raise an error if you try to safe an acquisition wrapped # or Persistent object in the REQUEST or SESSION from Products.Transience.TransientObject import TransientObject from ZPublisher.BaseRequest import BaseRequest from Persistence import Persistent class PersistentKeyException(Exception): pass class PersistentValueException(Exception): pass class WrappedKeyException(Exception): pass class WrappedValueException(Exception): pass def __setitem__(self, k, v): """ """ if isinstance(k, Persistent): raise PersistentKeyException((k,v)) if hasattr(k, 'aq_chain'): raise WrappedKeyException((k,v)) # These keys are safe if k not in ('SESSION', 'AUTHENTICATED_USER', 'PUBLISHED', ): if isinstance(v, Persistent): raise PersistentValueException((k,v)) if hasattr(v, 'aq_chain'): raise WrappedValueException((k,v)) self.__orig__setitem__(k,v) TransientObject.__orig__setitem__ = TransientObject.__setitem__ TransientObject.__setitem__ = __setitem__ BaseRequest.__orig__setitem__ = BaseRequest.__setitem__ BaseRequest.__setitem__ = __setitem__