[Lxml-checkins] r50930 - in lxml/trunk: . src/lxml

scoder at codespeak.net scoder at codespeak.net
Wed Jan 23 17:10:03 CET 2008


Author: scoder
Date: Wed Jan 23 17:10:02 2008
New Revision: 50930

Modified:
   lxml/trunk/   (props changed)
   lxml/trunk/src/lxml/dtd.pxi
   lxml/trunk/src/lxml/lxml.etree.pyx
   lxml/trunk/src/lxml/relaxng.pxi
   lxml/trunk/src/lxml/xmlschema.pxi
Log:
 r3292 at delle:  sbehnel | 2008-01-23 16:39:27 +0100
 cleanup in validation code, paste local error log into exceptions during schema parsing


Modified: lxml/trunk/src/lxml/dtd.pxi
==============================================================================
--- lxml/trunk/src/lxml/dtd.pxi	(original)
+++ lxml/trunk/src/lxml/dtd.pxi	Wed Jan 23 17:10:02 2008
@@ -29,21 +29,27 @@
     cdef tree.xmlDtd* _c_dtd
     def __init__(self, file=None, *, external_id=None):
         self._c_dtd = NULL
+        _Validator.__init__(self)
         if file is not None:
             if python._isString(file):
+                self._error_log.connect()
                 self._c_dtd = xmlparser.xmlParseDTD(NULL, _cstr(file))
+                self._error_log.disconnect()
             elif hasattr(file, 'read'):
                 self._c_dtd = _parseDtdFromFilelike(file)
             else:
-                raise DTDParseError, "parsing from file objects is not supported"
+                raise DTDParseError("file must be a filename or file-like object")
         elif external_id is not None:
+            self._error_log.connect()
             self._c_dtd = xmlparser.xmlParseDTD(external_id, NULL)
+            self._error_log.disconnect()
         else:
-            raise DTDParseError, "either filename or external ID required"
+            raise DTDParseError("either filename or external ID required")
 
         if self._c_dtd is NULL:
-            raise DTDParseError, "error parsing DTD"
-        _Validator.__init__(self)
+            raise DTDParseError(
+                self._error_log._buildExceptionMessage("error parsing DTD"),
+                error_log=self._error_log)
 
     def __dealloc__(self):
         tree.xmlFreeDtd(self._c_dtd)
@@ -77,7 +83,7 @@
 
         self._error_log.disconnect()
         if ret == -1:
-            raise DTDValidateError, "Internal error in DTD validation"
+            raise DTDValidateError("Internal error in DTD validation")
         if ret == 1:
             return True
         else:
@@ -87,15 +93,19 @@
 cdef tree.xmlDtd* _parseDtdFromFilelike(file) except NULL:
     cdef _ExceptionContext exc_context
     cdef _FileReaderContext dtd_parser
+    cdef _ErrorLog error_log
     cdef tree.xmlDtd* c_dtd
     exc_context = _ExceptionContext()
     dtd_parser = _FileReaderContext(file, exc_context, None, None)
+    error_log = _ErrorLog()
 
+    error_log.connect()
     c_dtd = dtd_parser._readDtd()
+    error_log.disconnect()
 
     exc_context._raise_if_stored()
     if c_dtd is NULL:
-        raise DTDParseError, "error parsing DTD"
+        raise DTDParseError("error parsing DTD", error_log=error_log)
     return c_dtd
 
 cdef extern from "etree_defs.h":

Modified: lxml/trunk/src/lxml/lxml.etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/lxml.etree.pyx	(original)
+++ lxml/trunk/src/lxml/lxml.etree.pyx	Wed Jan 23 17:10:02 2008
@@ -93,9 +93,12 @@
     """Main exception base class for lxml.  All other exceptions inherit from
     this one.
     """
-    def __init__(self, *args):
+    def __init__(self, *args, error_log=None):
         _initError(self, *args)
-        self.error_log = __copyGlobalErrorLog()
+        if error_log is None:
+            self.error_log = __copyGlobalErrorLog()
+        else:
+            self.error_log = error_log.copy()
 
 cdef object _LxmlError
 _LxmlError = LxmlError
@@ -2370,14 +2373,14 @@
     def assertValid(self, etree):
         "Raises DocumentInvalid if the document does not comply with the schema."
         if not self(etree):
-            raise DocumentInvalid, self._error_log._buildExceptionMessage(
-                "Document does not comply with schema")
+            raise DocumentInvalid(self._error_log._buildExceptionMessage(
+                "Document does not comply with schema"))
 
     def assert_(self, etree):
         "Raises AssertionError if the document does not comply with the schema."
         if not self(etree):
-            raise AssertionError, self._error_log._buildExceptionMessage(
-                "Document does not comply with schema")
+            raise AssertionError(self._error_log._buildExceptionMessage(
+                "Document does not comply with schema"))
 
     property error_log:
         def __get__(self):

Modified: lxml/trunk/src/lxml/relaxng.pxi
==============================================================================
--- lxml/trunk/src/lxml/relaxng.pxi	(original)
+++ lxml/trunk/src/lxml/relaxng.pxi	Wed Jan 23 17:10:02 2008
@@ -76,8 +76,10 @@
                 if _LIBXML_VERSION_INT < 20624:
                     relaxng.xmlRelaxNGFreeParserCtxt(parser_ctxt)
                 _destroyFakeDoc(doc._c_doc, fake_c_doc)
-            raise RelaxNGParseError, self._error_log._buildExceptionMessage(
-                "Document is not valid Relax NG")
+            raise RelaxNGParseError(
+                self._error_log._buildExceptionMessage(
+                    "Document is not valid Relax NG"),
+                error_log=self._error_log)
         if fake_c_doc is not NULL:
             _destroyFakeDoc(doc._c_doc, fake_c_doc)
 

Modified: lxml/trunk/src/lxml/xmlschema.pxi
==============================================================================
--- lxml/trunk/src/lxml/xmlschema.pxi	(original)
+++ lxml/trunk/src/lxml/xmlschema.pxi	Wed Jan 23 17:10:02 2008
@@ -46,7 +46,7 @@
             c_href = _getNs(c_node)
             if c_href is NULL or \
                    cstd.strcmp(c_href, 'http://www.w3.org/2001/XMLSchema') != 0:
-                raise XMLSchemaParseError, "Document is not XML Schema"
+                raise XMLSchemaParseError("Document is not XML Schema")
 
             fake_c_doc = _fakeRootDoc(doc._c_doc, root_node._c_node)
             self._error_log.connect()
@@ -60,7 +60,7 @@
             self._error_log.connect()
             parser_ctxt = xmlschema.xmlSchemaNewParserCtxt(_cstr(filename))
         else:
-            raise XMLSchemaParseError, "No tree or file given"
+            raise XMLSchemaParseError("No tree or file given")
 
         if parser_ctxt is not NULL:
             self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
@@ -73,8 +73,10 @@
             _destroyFakeDoc(doc._c_doc, fake_c_doc)
 
         if self._c_schema is NULL:
-            raise XMLSchemaParseError, self._error_log._buildExceptionMessage(
-                "Document is not valid XML Schema")
+            raise XMLSchemaParseError(
+                self._error_log._buildExceptionMessage(
+                    "Document is not valid XML Schema"),
+                error_log=self._error_log)
 
     def __dealloc__(self):
         xmlschema.xmlSchemaFree(self._c_schema)


More information about the lxml-checkins mailing list