tried to fix the problems reported in bug #126735 fixed again some problem

* python/libxml.py: tried to fix the problems reported in
  bug #126735
* xpath.c SAX2.c error.c parser.c valid.c include/libxml/xmlerror.h:
  fixed again some problem trying to use the structured error
  handlers, c.f. bug #126735
* result/VC/ElementValid: tiny change due to the fix
Daniel
diff --git a/ChangeLog b/ChangeLog
index 03bd737..eb68d6b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Mon Dec  8 11:08:45 CET 2003 Daniel Veillard <daniel@veillard.com>
+
+	* python/libxml.py: tried to fix the problems reported in 
+	  bug #126735
+	* xpath.c SAX2.c error.c parser.c valid.c include/libxml/xmlerror.h:
+	  fixed again some problem trying to use the structured error
+	  handlers, c.f. bug #126735
+	* result/VC/ElementValid: tiny change due to the fix
+
 Sun Dec  7 22:27:31 CET 2003 Daniel Veillard <daniel@veillard.com>
 
 	* error.c: fixed __xmlRaiseError to use structured error handlers
diff --git a/SAX2.c b/SAX2.c
index 1841e50..1971aba 100644
--- a/SAX2.c
+++ b/SAX2.c
@@ -45,6 +45,37 @@
             __FILE__, __LINE__);
 
 /**
+ * xmlValidError:
+ * @ctxt:  an XML validation parser context
+ * @error:  the error number
+ * @msg:  the error message
+ * @str1:  extra data
+ * @str2:  extra data
+ *
+ * Handle a validation error
+ */
+static void
+xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
+            const char *msg, const char *str1, const char *str2)
+{
+    xmlStructuredErrorFunc schannel = NULL;
+
+    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
+        (ctxt->instate == XML_PARSER_EOF))
+	return;
+    ctxt->errNo = error;
+    if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
+        schannel = ctxt->sax->serror;
+    __xmlRaiseError(schannel,
+                    ctxt->vctxt.error, ctxt->vctxt.userData,
+                    ctxt, NULL, XML_FROM_DTD, error,
+                    XML_ERR_ERROR, NULL, 0, (const char *) str1,
+		    (const char *) str2, NULL, 0, 0,
+		    msg, (const char *) str1, (const char *) str2);
+    ctxt->valid = 0;
+}
+
+/**
  * xmlSAX2GetPublicId:
  * @ctx: the user data (XML parser context)
  *
@@ -1244,11 +1275,9 @@
 			}
 		    }
 		    if (att == NULL) {
-			if (ctxt->vctxt.error != NULL)
-			    ctxt->vctxt.error(ctxt->vctxt.userData,
+		        xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
       "standalone: attribute %s on %s defaulted from external subset\n",
-					      fulln, attr->elem);
-			ctxt->valid = 0;
+				    fulln, attr->elem);
 		    }
 		}
 		attr = attr->nexth;
@@ -1370,13 +1399,9 @@
 	  (ctxt->myDoc->intSubset->elements == NULL) &&
 	  (ctxt->myDoc->intSubset->attributes == NULL) && 
 	  (ctxt->myDoc->intSubset->entities == NULL)))) {
-	if (ctxt->vctxt.error != NULL) {
-            ctxt->vctxt.error(ctxt->vctxt.userData,
-	      "Validation failed: no DTD found !\n");
-	}
+	xmlErrValid(ctxt, XML_ERR_NO_DTD,
+	  "Validation failed: no DTD found !", NULL, NULL);
 	ctxt->validate = 0;
-	ctxt->valid = 0;
-	ctxt->errNo = XML_ERR_NO_DTD;
     }
        
 
@@ -1947,13 +1972,9 @@
 	  (ctxt->myDoc->intSubset->elements == NULL) &&
 	  (ctxt->myDoc->intSubset->attributes == NULL) && 
 	  (ctxt->myDoc->intSubset->entities == NULL)))) {
-	if (ctxt->vctxt.error != NULL) {
-            ctxt->vctxt.error(ctxt->vctxt.userData,
-	      "Validation failed: no DTD found !\n");
-	}
+	xmlErrValid(ctxt, XML_ERR_NO_DTD,
+	  "Validation failed: no DTD found !", NULL, NULL);
 	ctxt->validate = 0;
-	ctxt->valid = 0;
-	ctxt->errNo = XML_ERR_NO_DTD;
     }
 
     /*
diff --git a/error.c b/error.c
index 1e81b43..fb3c5a7 100644
--- a/error.c
+++ b/error.c
@@ -454,9 +454,9 @@
 	if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
 	    (ctxt->sax->initialized == XML_SAX2_MAGIC))
 	    schannel = ctxt->sax->serror;
-	if (schannel == NULL)
-	    schannel = xmlStructuredError;
     }
+    if (schannel == NULL)
+	schannel = xmlStructuredError;
     if ((domain == XML_FROM_VALID) &&
         ((channel == xmlParserValidityError) ||
 	 (channel == xmlParserValidityWarning))) {
diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h
index b32b5eb..f1559f8 100644
--- a/include/libxml/xmlerror.h
+++ b/include/libxml/xmlerror.h
@@ -234,6 +234,7 @@
     XML_DTD_UNKNOWN_ENTITY, /* 535 */
     XML_DTD_UNKNOWN_ID, /* 536 */
     XML_DTD_UNKNOWN_NOTATION, /* 537 */
+    XML_DTD_STANDALONE_DEFAULTED, /* 538 */
     XML_HTML_STRUCURE_ERROR = 800,
     XML_HTML_UNKNOWN_TAG, /* 801 */
     XML_RNGP_ANYNAME_ATTR_ANCESTOR = 1000,
diff --git a/parser.c b/parser.c
index 5a97036..fde9bdf 100644
--- a/parser.c
+++ b/parser.c
@@ -426,7 +426,7 @@
  * @msg:  the error message
  * @str1:  extra data
  *
- * Handle a warning.
+ * Handle a validity error.
  */
 static void
 xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
diff --git a/python/libxml.py b/python/libxml.py
index 41103c6..92c4c3c 100644
--- a/python/libxml.py
+++ b/python/libxml.py
@@ -362,21 +362,25 @@
         ctxt.xpathFreeContext()
         return res
 
-    #
-    # Selecting nodes using XPath, faster because the context
-    # is allocated just once per xmlDoc.
-    #
+#    #
+#    # Selecting nodes using XPath, faster because the context
+#    # is allocated just once per xmlDoc.
+#    #
+#    # Removed: DV memleaks c.f. #126735
+#    #
+#    def xpathEval2(self, expr):
+#        doc = self.doc
+#        if doc == None:
+#            return None
+#        try:
+#            doc._ctxt.setContextNode(self)
+#        except:
+#            doc._ctxt = doc.xpathNewContext()
+#            doc._ctxt.setContextNode(self)
+#        res = doc._ctxt.xpathEval(expr)
+#        return res
     def xpathEval2(self, expr):
-        doc = self.doc
-        if doc == None:
-            return None
-        try:
-            doc._ctxt.setContextNode(self)
-        except:
-            doc._ctxt = doc.xpathNewContext()
-            doc._ctxt.setContextNode(self)
-        res = doc._ctxt.xpathEval(expr)
-        return res
+        return self.xpathEval(expr)
 
     # support for python2 iterators
     def walk_depth_first(self):
@@ -390,7 +394,7 @@
             self.doc._ctxt.xpathFreeContext()
         except:
             pass
-        libxml2mod.freeDoc(self._o)
+        libxml2mod.xmlFreeDoc(self._o)
 
 
 #
diff --git a/result/VC/ElementValid b/result/VC/ElementValid
index f9372b3..d6b50a7 100644
--- a/result/VC/ElementValid
+++ b/result/VC/ElementValid
@@ -1,3 +1,3 @@
-./test/VC/ElementValid:3: validity error: Validation failed: no DTD found !
+./test/VC/ElementValid:3: validity error : Validation failed: no DTD found !
 <doc/>
     ^
diff --git a/valid.c b/valid.c
index 9fb9b69..3c518d6 100644
--- a/valid.c
+++ b/valid.c
@@ -81,7 +81,7 @@
  * Handle a validation error
  */
 static void
-xmlErrValid(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlParserErrors error,
+xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
             const char *msg, const char *extra)
 {
     xmlGenericErrorFunc channel = NULL;
diff --git a/xpath.c b/xpath.c
index fd97498..9d7187d 100644
--- a/xpath.c
+++ b/xpath.c
@@ -271,9 +271,7 @@
 void
 xmlXPathErr(xmlXPathParserContextPtr ctxt, int error)
 {
-    if (ctxt != NULL)
-        ctxt->error = error;
-    if ((ctxt == NULL) || (ctxt->context == NULL)) {
+    if (ctxt == NULL) {
 	__xmlRaiseError(NULL, NULL, NULL,
 			NULL, NULL, XML_FROM_XPATH,
 			error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
@@ -282,6 +280,17 @@
 			xmlXPathErrorMessages[error]);
 	return;
     }
+    ctxt->error = error;
+    if (ctxt->context == NULL) {
+	__xmlRaiseError(NULL, NULL, NULL,
+			NULL, NULL, XML_FROM_XPATH,
+			error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
+			XML_ERR_ERROR, NULL, 0,
+			(const char *) ctxt->base, NULL, NULL,
+			ctxt->cur - ctxt->base, 0,
+			xmlXPathErrorMessages[error]);
+	return;
+    }
     ctxt->context->lastError.domain = XML_FROM_XPATH;
     ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK -
                            XPATH_EXPRESSION_OK;