Next / Previous / Contents / Shipman's homepage

32. Sox.cleanup(): Cleanup

This method insures that all open elements have been closed.


The original design called for the destructor (.__del__()) method to do this cleanup. However, a class's destructor is called only when its reference count goes to zero. If there are open elements, then each Elt instance on the stack will contain a reference to the parent Sox instance, so merely deleting the Sox instance (with a del statement, for example) does not enter the destructor, because the stack still contains references to the parent instance. Hence we ask the caller to use an explicit call here to insure closing of all open elements.
# - - -   S o x . c l e a n u p

    def cleanup(self):
        '''Insure all elements have been closed.
        if len(self._stack) > 0:
            if len(self._stack) == 1:
                text = ("One <%s> element was not closed." %
                tagNames = ' '.join([s.tag
                                     for s in self._stack])
                text = ("Multiple unclosed elements: %s" % tagNames)
            raise SoxError(text)