[Zope-Checkins] CVS: Zope3/lib/python/Zope/Configuration - xmlconfig.py:1.1.2.3

Jim Fulton jim@zope.com
Tue, 20 Nov 2001 14:58:41 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Configuration
In directory cvs.zope.org:/tmp/cvs-serv29009

Modified Files:
      Tag: Zope-3x-branch
	xmlconfig.py 
Log Message:
Improved error messages to include file names and better
error stringification.



=== Zope3/lib/python/Zope/Configuration/xmlconfig.py 1.1.2.2 => 1.1.2.3 ===
 
 from xml.sax import make_parser
+from xml.sax.xmlreader import InputSource
 from xml.sax.handler import ContentHandler, feature_namespaces
 from meta import execute
 from keyword import iskeyword
+import sys
 
 class ZopeXMLConfigurationError(Exception):
     "Zope XML Configuration error"
 
     def __init__(self, locator, mess):
+        if type(mess) is not type(''):
+            try:
+                mess = "%s: %s" % (mess.__class__.__name__, mess)
+            except AttributeError:
+                mess=str(mess)
         self.lno=locator.getLineNumber()
         self.cno=locator.getColumnNumber()
+        self.sid=locator.getSystemId()
         self.mess=mess
 
     def __str__(self):
-        return "%s at line %s column %s" % (self.mess, self.lno, self.cno)
+        return "%s at line %s column %s of %s" % (
+            self.mess, self.lno, self.cno, self.sid)
 
 class ConfigurationHandler(ContentHandler):
 
@@ -48,7 +57,12 @@
                 kw[aname]=value
 
         if len(stack) == 1:
-            stack.append(execute(*name, **kw))
+            try:
+                stack.append(execute(*name, **kw))
+            except Exception, v:
+                raise ZopeXMLConfigurationError, (
+                    self.__locator, str(v)), sys.exc_info()[2] 
+                
             self.__ns = name[0]
         else:
             if self.__ns != name[0]:
@@ -58,15 +72,28 @@
             if ob is None:
                 raise ZopeXMLConfigurationError(self.__locator,
                                                 'Invalid sub-directive')
-            stack.append(getattr(ob, name[1])(**kw))
+            try:
+                stack.append(getattr(ob, name[1])(**kw))
+            except Exception, v:
+                raise ZopeXMLConfigurationError, (
+                    self.__locator, str(v)), sys.exc_info()[2] 
 
     def endElementNS(self, name, qname):
         ob = self.__stack.pop()
-        if ob is not None: ob()
+        if ob is not None:
+            try:
+                ob()
+            except Exception, v:
+                raise ZopeXMLConfigurationError, (
+                    self.__locator, str(v)), sys.exc_info()[2] 
 
 
 def xmlconfig(file):
+    src=InputSource(getattr(file, 'name', '<string>'))
+    src.setByteStream(file)
+
+    
     parser=make_parser()
     parser.setContentHandler(ConfigurationHandler())
     parser.setFeature(feature_namespaces, 1)
-    parser.parse(file)
+    parser.parse(src)