Back to index

plone3  3.1.7
Public Member Functions
Archetypes.ApeSupport.RemainingState Class Reference

List of all members.

Public Member Functions

def cleanDictCopy
def serialize

Detailed Description

Definition at line 152 of file ApeSupport.py.


Member Function Documentation

cleans out the baseUnit instances of the dict, because the are not picklable 

Definition at line 154 of file ApeSupport.py.

00154 
00155     def cleanDictCopy(self,dict):
00156         ''' cleans out the baseUnit instances of the dict, because the are not picklable '''
00157         res={}
00158 
00159         for k in dict.keys():
00160             v=dict[k]
00161             if type(v) == type({}) or ec_isinstance(v,PersistentMapping):
00162                 v1=self.cleanDictCopy(v)
00163             elif ec_isinstance(v,BaseUnit):
00164                 v1=v.getRaw()
00165             else:
00166                 v1=v
00167             res[k]=v1
00168 
00169         return res
00170 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 171 of file ApeSupport.py.

00171 
00172     def serialize(self, event):
00173         assert IFullSerializationEvent.isImplementedBy(event)
00174         assert isinstance(event.obj, Persistent)
00175 
00176         # Allow pickling of cyclic references to the object.
00177         event.serialized('self', event.obj, False)
00178 
00179         # Ignore previously serialized attributes
00180         state = self.cleanDictCopy(event.obj.__dict__)
00181         for key in state.keys():
00182             if key.startswith('_v_'):
00183                 del state[key]
00184         for attrname in event.get_seralized_attributes():
00185             if state.has_key(attrname):
00186                 del state[attrname]
00187         if not state:
00188             # No data needs to be stored
00189             return ''
00190 
00191         outfile = StringIO()
00192         p = Pickler(outfile, 1)  # Binary pickle
00193         unmanaged = []
00194 
00195         def persistent_id(ob, identify_internal=event.identify_internal,
00196                           unmanaged=unmanaged):
00197             ref = identify_internal(ob)
00198             if ref is None:
00199                 if hasattr(ob, '_p_oid'):
00200                     # Persistent objects that end up in the remainder
00201                     # are unmanaged.  Tell ZODB about them so that
00202                     # ZODB can deal with them specially.
00203                     unmanaged.append(ob)
00204             return ref
00205 
00206         # Preserve order to a reasonable extent by storing a list
00207         # instead of a dictionary.
00208         state_list = state.items()
00209         state_list.sort()
00210         p.persistent_id = persistent_id
00211         try:
00212             p.dump(state_list)
00213         except UnpickleableError, exc:
00214             # Try to reveal which attribute is unpickleable.
00215             attrname = None
00216             attrvalue = None
00217             for key, value in state_list:
00218                 del unmanaged[:]
00219                 outfile.seek(0)
00220                 outfile.truncate()
00221                 p = Pickler(outfile)
00222                 p.persistent_id = persistent_id
00223                 try:
00224                     p.dump(value)
00225                 except UnpickleableError:
00226                     attrname = key
00227                     attrvalue = value
00228                     break
00229             if attrname is not None:
00230                 # Provide a more informative exception.
00231                 if os.environ.get('APE_TRACE_UNPICKLEABLE'):
00232                     # Provide an opportunity to examine
00233                     # the "attrvalue" attribute.
00234                     raise RuntimeError(
00235                         'Unable to pickle the %s attribute, %s, '
00236                         'of %s at %s.  %s.' % (
00237                         repr(attrname), repr(attrvalue), repr(event.obj),
00238                         repr(event.oid), str(exc)))
00239             else:
00240                 # Couldn't help.
00241                 raise
00242 
00243         p.persistent_id = lambda ob: None  # Stop recording references
00244         p.dump(unmanaged)
00245         event.upos.extend(unmanaged)
00246 
00247         s = outfile.getvalue()
00248         return encode_to_text(s, state.keys(), len(unmanaged))
00249 
00250 # helper functions for issubclass and isinstance
00251 # with extension classes.
# borrowed from Greg Ward (thanx Greg :)

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following file: