def _cleanup_object(self, object): """attempt to call cleanup on object **INPUTS*** *OwnerObject object* -- note: class of object is the expected class, but _cleanup_object doesn't assume this is correct, nor does it check whether the object is a subclass of OwnerObject, only that it is a class instance and that it has a cleanup attribute **OUTPUTS** *STR* -- reason for error (or None if no error). """ if object == None: return None if type(object) == types.ListType: for i in range(object): error_msg = self._cleanup_object(object[i]) if error_msg != None: error_msg = 'element %d of ' % i return error_msg return None elif type(object) == types.DictType: sorted = object.keys() sorted.sort() for key in sorted: error_msg = self._cleanup_object(object[key]) if error_msg != None: error_msg = 'value for key %s of ' % str(key) return error_msg return None if (type(object) != types.InstanceType and not debug.isinstance_of_some_class(object)): return 'because it is not an object, but has type %s' % ( type(object)) try: object.cleanup except AttributeError: return 'because it does not have a cleanup method' try: object.cleanup() except: traceback.print_exc() return 'because its cleanup method threw an exception'
def _cleanup_object(self, object): """attempt to call cleanup on object **INPUTS*** *OwnerObject object* -- note: class of object is the expected class, but _cleanup_object doesn't assume this is correct, nor does it check whether the object is a subclass of OwnerObject, only that it is a class instance and that it has a cleanup attribute **OUTPUTS** *STR* -- reason for error (or None if no error). """ if object == None: return None if type(object) == types.ListType: for i in range(object): error_msg = self._cleanup_object(object[i]) if error_msg != None: error_msg = 'element %d of ' % i return error_msg return None elif type(object) == types.DictType: sorted = object.keys() sorted.sort() for key in sorted: error_msg = self._cleanup_object(object[key]) if error_msg != None: error_msg = 'value for key %s of ' % str(key) return error_msg return None if (type(object) != types.InstanceType and not debug.isinstance_of_some_class(object)): return 'because it is not an object, but has type %s' % (type(object)) try: object.cleanup except AttributeError: return 'because it does not have a cleanup method' try: object.cleanup() except: traceback.print_exc() return 'because its cleanup method threw an exception'
def cleanup(self): """method to cleanup circular references by cleaning up any children, and then removing the reference to the parent **INPUTS** *none* **OUTPUTS** *none* """ # debug.trace_call_stack('OwnerObject.cleanup') self.remove_other_references() debug.trace('OwnerObject.cleanup', 'after remove_other_references') reversed_names = self.owned_objects reversed_names.reverse() msg_prefix = 'Warning: while cleaning up %s,\nunable to cleanup ' \ % repr(self) for name in reversed_names: if self.__dict__.has_key(name): attribute = self.__dict__[name] debug.trace( 'OwnerObject.cleanup', 'cleanup %s with value %s' % (name, repr(attribute))) if attribute == None: continue if type(attribute) == types.ListType: rr = range(len(attribute)) rr.reverse() for i in rr: error_msg = self._cleanup_object(attribute[i]) if error_msg != None: error_msg = 'element %d of attribute %s\n%s\n' \ % (i, name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del attribute[i] elif type(attribute) == types.DictType: sorted = attribute.keys() sorted.sort() for key in sorted: error_msg = self._cleanup_object(attribute[key]) if error_msg != None: error_msg = 'value for key %s of attribute %s\n%s\n' \ % (str(key), name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del attribute[key] elif (type(attribute) == types.InstanceType or debug.isinstance_of_some_class(attribute)): error_msg = self._cleanup_object(attribute) if error_msg != None: error_msg = 'attribute %s\n%s\n' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del self.__dict__[name] else: error_msg = 'because it is not an object, ' \ + 'but has type %s' % (type(attribute)) error_msg = 'attribute %s\n%s\n' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) del self.__dict__[name] else: error_msg = 'because the attribute does not exist\n' error_msg = 'attribute %s\n%s' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) for grandparent in self.grandparents: if self.__dict__.has_key(grandparent): del self.__dict__[grandparent] if self.parent_name != None \ and self.__dict__.has_key(self.parent_name): del self.__dict__[self.parent_name] debug.trace('OwnerObject.cleanup', 'cleanup of %s finished' % repr(self))
def cleanup(self): """method to cleanup circular references by cleaning up any children, and then removing the reference to the parent **INPUTS** *none* **OUTPUTS** *none* """ # debug.trace_call_stack('OwnerObject.cleanup') self.remove_other_references() debug.trace('OwnerObject.cleanup', 'after remove_other_references') reversed_names = self.owned_objects reversed_names.reverse() msg_prefix = 'Warning: while cleaning up %s,\nunable to cleanup ' \ % repr(self) for name in reversed_names: if self.__dict__.has_key(name): attribute = self.__dict__[name] debug.trace('OwnerObject.cleanup', 'cleanup %s with value %s' % (name, repr(attribute))) if attribute == None: continue if type(attribute) == types.ListType: rr = range(len(attribute)) rr.reverse() for i in rr: error_msg = self._cleanup_object(attribute[i]) if error_msg != None: error_msg = 'element %d of attribute %s\n%s\n' \ % (i, name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del attribute[i] elif type(attribute) == types.DictType: sorted = attribute.keys() sorted.sort() for key in sorted: error_msg = self._cleanup_object(attribute[key]) if error_msg != None: error_msg = 'value for key %s of attribute %s\n%s\n' \ % (str(key), name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del attribute[key] elif (type(attribute) == types.InstanceType or debug.isinstance_of_some_class(attribute)): error_msg = self._cleanup_object(attribute) if error_msg != None: error_msg = 'attribute %s\n%s\n' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) else: del self.__dict__[name] else: error_msg = 'because it is not an object, ' \ + 'but has type %s' % (type(attribute)) error_msg = 'attribute %s\n%s\n' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) del self.__dict__[name] else: error_msg = 'because the attribute does not exist\n' error_msg = 'attribute %s\n%s' \ % (name, error_msg) debug.critical_warning(msg_prefix + error_msg) for grandparent in self.grandparents: if self.__dict__.has_key(grandparent): del self.__dict__[grandparent] if self.parent_name != None \ and self.__dict__.has_key(self.parent_name): del self.__dict__[self.parent_name] debug.trace('OwnerObject.cleanup', 'cleanup of %s finished' % repr(self))