[Commits] (vajda) - fixed bug where exceptions happening in SAX handlers would disappear

commits at osafoundation.org commits at osafoundation.org
Thu Apr 1 11:52:12 PST 2004


Commit by: vajda
Modified files:
osaf/chandler/Chandler/repository/item/ItemHandler.py 1.34 1.35
osaf/chandler/Chandler/repository/persistence/PackHandler.py 1.14 1.15
osaf/chandler/Chandler/repository/persistence/Repository.py 1.69 1.70
osaf/chandler/Chandler/repository/persistence/XMLRepository.py 1.68 1.69
osaf/chandler/Chandler/repository/util/SAX.py 1.2 1.3

Log message:
   - fixed bug where exceptions happening in SAX handlers would disappear


ViewCVS links:
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/item/ItemHandler.py.diff?r1=text&tr1=1.34&r2=text&tr2=1.35
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/persistence/PackHandler.py.diff?r1=text&tr1=1.14&r2=text&tr2=1.15
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/persistence/Repository.py.diff?r1=text&tr1=1.69&r2=text&tr2=1.70
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/persistence/XMLRepository.py.diff?r1=text&tr1=1.68&r2=text&tr2=1.69
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/util/SAX.py.diff?r1=text&tr1=1.2&r2=text&tr2=1.3

Index: osaf/chandler/Chandler/repository/persistence/PackHandler.py
diff -u osaf/chandler/Chandler/repository/persistence/PackHandler.py:1.14 osaf/chandler/Chandler/repository/persistence/PackHandler.py:1.15
--- osaf/chandler/Chandler/repository/persistence/PackHandler.py:1.14	Tue Mar 23 21:08:47 2004
+++ osaf/chandler/Chandler/repository/persistence/PackHandler.py	Thu Apr  1 11:51:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.14 $"
-__date__      = "$Date: 2004/03/24 05:08:47 $"
+__revision__  = "$Revision: 1.15 $"
+__date__      = "$Date: 2004/04/01 19:51:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -18,6 +18,8 @@
 
     def __init__(self, path, parent, repository):
 
+        ContentHandler.__init__(self)
+
         self.path = path
         self.cwd = [ os.path.dirname(path) ]
         self.parent = [ parent ]
@@ -30,16 +32,18 @@
 
     def endDocument(self):
 
-        self.repository._resolveStubs()
+        if not self.errorOccurred():
+            self.repository._resolveStubs()
 
     def startElement(self, tag, attrs):
 
-        self.data = ''
-        method = getattr(PackHandler, tag + 'Start', None)
-        if method is not None:
-            method(self, attrs)
+        if not self.errorOccurred():
+            self.data = ''
+            method = getattr(PackHandler, tag + 'Start', None)
+            if method is not None:
+                method(self, attrs)
             
-        self.tagAttrs.append(attrs)
+            self.tagAttrs.append(attrs)
 
     def characters(self, data):
 
@@ -47,11 +51,12 @@
 
     def endElement(self, tag):
 
-        attrs = self.tagAttrs.pop()
+        if not self.errorOccurred():
+            attrs = self.tagAttrs.pop()
 
-        method = getattr(PackHandler, tag + 'End', None)
-        if method is not None:
-            method(self, attrs)
+            method = getattr(PackHandler, tag + 'End', None)
+            if method is not None:
+                method(self, attrs)
 
     def packStart(self, attrs):
 
@@ -60,9 +65,13 @@
 
         if attrs.has_key('file'):
             if not self.repository.find(Path('//', 'Packs', attrs['name'])):
-                self.repository.loadPack(os.path.join(self.cwd[-1],
-                                                      attrs['file']),
-                                         self.parent[-1])
+                try:
+                    self.repository.loadPack(os.path.join(self.cwd[-1],
+                                                          attrs['file']),
+                                             self.parent[-1])
+                except Exception:
+                    self.saveException()
+                    return
 
         else:
             self.name = attrs['name']
@@ -108,6 +117,8 @@
                 if exp.match(file):
                     parent = self.loadItem(os.path.join(self.cwd[-1], file),
                                            self.parent[-1])
+                    if self.errorOccurred():
+                        return
             
         self.parent.append(parent)
 
@@ -121,16 +132,25 @@
         if attrs.has_key('cwd'):
             self.cwd.pop()
 
-        if attrs.get('afterLoadHooks', 'False') == 'True':
-            for hook in self.hooks.pop():
-                hook()
+        try:
+            if attrs.get('afterLoadHooks', 'False') == 'True':
+                for hook in self.hooks.pop():
+                    hook()
+        except Exception:
+            self.saveException()
+            return
 
     def loadItem(self, file, parent):
 
-        items = self.repository._loadItemsFile(file, parent,
-                                               afterLoadHooks=self.hooks[-1])
-
-        for item in items:
-            item._status |= item.NEW
-
-        return items[0]
+        try:
+            items = self.repository._loadItemsFile(file, parent,
+                                                   afterLoadHooks=self.hooks[-1])
+
+            for item in items:
+                item._status |= item.NEW
+
+            return items[0]
+
+        except Exception:
+            self.saveException()
+            return

Index: osaf/chandler/Chandler/repository/item/ItemHandler.py
diff -u osaf/chandler/Chandler/repository/item/ItemHandler.py:1.34 osaf/chandler/Chandler/repository/item/ItemHandler.py:1.35
--- osaf/chandler/Chandler/repository/item/ItemHandler.py:1.34	Wed Mar 31 13:24:19 2004
+++ osaf/chandler/Chandler/repository/item/ItemHandler.py	Thu Apr  1 11:51:39 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.34 $"
-__date__      = "$Date: 2004/03/31 21:24:19 $"
+__revision__  = "$Revision: 1.35 $"
+__date__      = "$Date: 2004/04/01 19:51:39 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -26,6 +26,8 @@
     
     def __init__(self, repository, parent, afterLoadHooks):
 
+        ContentHandler.__init__(self)
+
         self.repository = repository
         self.parent = parent
         self.afterLoadHooks = afterLoadHooks
@@ -611,6 +613,8 @@
 
     def __init__(self, repository, parent, afterLoadHooks):
 
+        ContentHandler.__init__(self)
+
         self.repository = repository
         self.parent = parent
         self.afterLoadHooks = afterLoadHooks
@@ -622,26 +626,36 @@
         
     def startElement(self, tag, attrs):
 
-        if tag == 'item':
-            self.itemHandler = ItemHandler(self.repository, self.parent,
-                                           self.afterLoadHooks)
-            self.itemHandler.startDocument()
-
-        if self.itemHandler is not None:
-            self.itemHandler.startElement(tag, attrs)
+        if self.exception is None:
+            if tag == 'item':
+                self.itemHandler = ItemHandler(self.repository, self.parent,
+                                               self.afterLoadHooks)
+                self.itemHandler.startDocument()
+
+            if self.itemHandler is not None:
+                try:
+                    self.itemHandler.startElement(tag, attrs)
+                except Exception:
+                    self.saveException()
+                    return
 
     def characters(self, data):
 
-        if self.itemHandler is not None:
+        if self.exception is None and self.itemHandler is not None:
             self.itemHandler.characters(data)
 
     def endElement(self, tag):
 
-        if self.itemHandler is not None:
-            self.itemHandler.endElement(tag)
-
-        if tag == 'item':
-            item = self.itemHandler.item
-            self.items.append(self.itemHandler.item)
-            self.itemHandler.endDocument()
-            self.itemHandler = None
+        if self.exception is None:
+            if self.itemHandler is not None:
+                try:
+                    self.itemHandler.endElement(tag)
+                except Exception:
+                    self.saveException()
+                    return
+            
+            if tag == 'item':
+                item = self.itemHandler.item
+                self.items.append(self.itemHandler.item)
+                self.itemHandler.endDocument()
+                self.itemHandler = None

Index: osaf/chandler/Chandler/repository/persistence/Repository.py
diff -u osaf/chandler/Chandler/repository/persistence/Repository.py:1.69 osaf/chandler/Chandler/repository/persistence/Repository.py:1.70
--- osaf/chandler/Chandler/repository/persistence/Repository.py:1.69	Tue Mar 23 21:08:47 2004
+++ osaf/chandler/Chandler/repository/persistence/Repository.py	Thu Apr  1 11:51:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.69 $"
-__date__      = "$Date: 2004/03/24 05:08:47 $"
+__revision__  = "$Revision: 1.70 $"
+__date__      = "$Date: 2004/04/01 19:51:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -358,7 +358,10 @@
         if not packs:
             packs = Item('Packs', self, None)
 
-        libxml2.SAXParseFile(PackHandler(path, parent, self), path, 0)
+        handler = PackHandler(path, parent, self)
+        libxml2.SAXParseFile(handler, path, 0)
+        if handler.errorOccurred():
+            raise handler.saxError()
 
     def dir(self, item=None, path=None):
         'Print out a listing of each item in the repository or under item.'
@@ -396,7 +399,9 @@
             
         handler = ItemsHandler(self, parent or self, afterLoadHooks)
         libxml2.SAXParseFile(handler, path, 0)
-
+        if handler.errorOccurred():
+            raise handler.saxError()
+        
         return handler.items
 
     def _loadItemString(self, string, parent=None, afterLoadHooks=None):
@@ -411,6 +416,8 @@
         handler = ItemHandler(self, parent or self, afterLoadHooks)
         ctx = libxml2.createPushParser(handler, string, len(string), "item")
         ctx.parseChunk('', 0, 1)
+        if handler.errorOccurred():
+            raise handler.saxError()
 
         return handler.item
 

Index: osaf/chandler/Chandler/repository/persistence/XMLRepository.py
diff -u osaf/chandler/Chandler/repository/persistence/XMLRepository.py:1.68 osaf/chandler/Chandler/repository/persistence/XMLRepository.py:1.69
--- osaf/chandler/Chandler/repository/persistence/XMLRepository.py:1.68	Tue Mar 23 21:08:47 2004
+++ osaf/chandler/Chandler/repository/persistence/XMLRepository.py	Thu Apr  1 11:51:40 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.68 $"
-__date__      = "$Date: 2004/03/24 05:08:47 $"
+__revision__  = "$Revision: 1.69 $"
+__date__      = "$Date: 2004/04/01 19:51:40 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -333,6 +333,8 @@
         string = doc.getContent()
         ctx = libxml2.createPushParser(handler, string, len(string), "doc")
         ctx.parseChunk('', 0, 1)
+        if handler.errorOccurred():
+            raise handler.saxError()
             
     def getDocUUID(self, doc):
 

Index: osaf/chandler/Chandler/repository/util/SAX.py
diff -u osaf/chandler/Chandler/repository/util/SAX.py:1.2 osaf/chandler/Chandler/repository/util/SAX.py:1.3
--- osaf/chandler/Chandler/repository/util/SAX.py:1.2	Mon Mar  8 14:56:57 2004
+++ osaf/chandler/Chandler/repository/util/SAX.py	Thu Apr  1 11:51:41 2004
@@ -1,19 +1,51 @@
 
-__revision__  = "$Revision: 1.2 $"
-__date__      = "$Date: 2004/03/08 22:56:57 $"
+__revision__  = "$Revision: 1.3 $"
+__date__      = "$Date: 2004/04/01 19:51:41 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
+import sys, traceback
 
 from libxml2mod import xmlEncodeSpecialChars as escape
 from libxml2 import SAXCallback, createPushParser
+from cStringIO import StringIO
+
+
+class SAXError(Exception):
+    pass
 
 
 class ContentHandler(SAXCallback):
+
+    def __init__(self):
+
+        self.exception = None
     
     def cdataBlock(self, data):
         
         self.characters(data)
+
+    def saveException(self):
+
+        type, value, stack = sys.exc_info()
+        self.exception = traceback.format_exception(type, value, stack)
+
+    def errorOccurred(self):
+
+        return self.exception is not None
+
+    def saxError(self):
+
+        try:
+            buffer = StringIO()
+            buffer.write("(nested exception traceback below)\n\n")
+            for text in self.exception:
+                buffer.write(text)
+
+            return SAXError(buffer.getvalue())
+
+        finally:
+            buffer.close()
 
 
 class XMLGenerator(object):



More information about the Commits mailing list