humm, changed the way the SAX parser work when

* parser.c: humm, changed the way the SAX parser work when
  xmlSubstituteEntitiesDefault(1) is set, it will then
  do the entity registration and loading by itself in case the
  user provided SAX getEntity() returns NULL.
* testSAX.c: added --noent to test the behaviour.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 02005ae..43d3261 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Mon Mar 18 19:18:13 CET 2002 Daniel Veillard <daniel@veillard.com>
+
+	* parser.c: humm, changed the way the SAX parser work when
+	  xmlSubstituteEntitiesDefault(1) is set, it will then 
+	  do the entity registration and loading by itself in case the
+	  user provided SAX getEntity() returns NULL.
+	* testSAX.c: added --noent to test the behaviour.
+
 Mon Mar 18 12:44:23 CET 2002 Daniel Veillard <daniel@veillard.com>
 
 	* parser.c: Wilfried Teiken provided a hackish but working
diff --git a/parser.c b/parser.c
index 97bb233..d2fd690 100644
--- a/parser.c
+++ b/parser.c
@@ -79,6 +79,8 @@
 #define XML_PARSER_BIG_BUFFER_SIZE 300
 #define XML_PARSER_BUFFER_SIZE 100
 
+#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
+
 /*
  * List of XML prefixed PI allowed by W3C specs
  */
@@ -3459,6 +3461,21 @@
 		    ctxt->sax->entityDecl(ctxt->userData, name,
 				XML_INTERNAL_GENERAL_ENTITY,
 				NULL, NULL, value);
+		/*
+		 * For expat compatibility in SAX mode.
+		 */
+		if ((ctxt->myDoc == NULL) ||
+		    (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
+		    if (ctxt->myDoc == NULL) {
+			ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
+		    }
+		    if (ctxt->myDoc->intSubset == NULL)
+			ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
+					    BAD_CAST "fake", NULL, NULL);
+
+		    entityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
+			       NULL, NULL, value);
+		}
 	    } else {
 	        URI = xmlParseExternalID(ctxt, &literal, 1);
 		if ((URI == NULL) && (literal == NULL)) {
@@ -3535,6 +3552,24 @@
 			ctxt->sax->entityDecl(ctxt->userData, name,
 				    XML_EXTERNAL_GENERAL_PARSED_ENTITY,
 				    literal, URI, NULL);
+		    /*
+		     * For expat compatibility in SAX mode.
+		     * assuming the entity repalcement was asked for
+		     */
+		    if ((ctxt->replaceEntities != 0) &&
+			((ctxt->myDoc == NULL) ||
+			(xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
+			if (ctxt->myDoc == NULL) {
+			    ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
+			}
+
+			if (ctxt->myDoc->intSubset == NULL)
+			    ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
+						BAD_CAST "fake", NULL, NULL);
+			entityDecl(ctxt, name,
+				    XML_EXTERNAL_GENERAL_PARSED_ENTITY,
+				    literal, URI, NULL);
+		    }
 		}
 	    }
 	}
@@ -3571,6 +3606,9 @@
 	        if ((ctxt->sax != NULL) &&
 		    (ctxt->sax->getEntity != NULL))
 		    cur = ctxt->sax->getEntity(ctxt->userData, name);
+		if ((cur == NULL) && (ctxt->userData==ctxt)) {
+		    cur = getEntity(ctxt, name);
+		}
 	    }
             if (cur != NULL) {
 	        if (cur->orig != NULL)
@@ -5492,6 +5530,9 @@
 			ent = ctxt->sax->getEntity(ctxt->userData, name);
 		    if (ent == NULL)
 		        ent = xmlGetPredefinedEntity(name);
+		    if ((ent == NULL) && (ctxt->userData==ctxt)) {
+			ent = getEntity(ctxt, name);
+		    }
 		}
 		/*
 		 * [ WFC: Entity Declared ]
@@ -5686,6 +5727,9 @@
 			ent = ctxt->sax->getEntity(ctxt->userData, name);
 		    if (ent == NULL)
 		        ent = xmlGetPredefinedEntity(name);
+		    if ((ent == NULL) && (ctxt->userData==ctxt)) {
+			ent = getEntity(ctxt, name);
+		    }
 		}
 		/*
 		 * [ WFC: Entity Declared ]
@@ -7655,6 +7699,15 @@
     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
         ctxt->sax->endDocument(ctxt->userData);
 
+    /*
+     * Remove locally kept entity definitions if the tree was not built
+     */
+    if ((ctxt->myDoc != NULL) &&
+	(xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
+	xmlFreeDoc(ctxt->myDoc);
+	ctxt->myDoc = NULL;
+    }
+
     if (! ctxt->wellFormed) {
 	ctxt->valid = 0;
 	return(-1);
diff --git a/python/Makefile.am b/python/Makefile.am
index e0cd052..ac16cd4 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -57,11 +57,11 @@
 	cd $(srcdir) && $(PYTHON) $(GENERATE)
 
 $(libxml2mod_la_OBJECTS): $(GENERATED)
+
 else
 all: 
 endif
-
-tests: all
+tests test: all
 	cd tests && $(MAKE) tests
 
 clean:
diff --git a/testSAX.c b/testSAX.c
index 0bf394f..559177c 100644
--- a/testSAX.c
+++ b/testSAX.c
@@ -44,6 +44,7 @@
 static int recovery = 0;
 static int push = 0;
 static int speed = 0;
+static int noent = 0;
 
 xmlSAXHandler emptySAXHandlerStruct = {
     NULL, /* internalSubset */
@@ -718,7 +719,11 @@
 	else if ((!strcmp(argv[i], "-speed")) ||
 	         (!strcmp(argv[i], "--speed")))
 	    speed++;
+	else if ((!strcmp(argv[i], "-noent")) ||
+	         (!strcmp(argv[i], "--noent")))
+	    noent++;
     }
+    if (noent != 0) xmlSubstituteEntitiesDefault(1);
     for (i = 1; i < argc ; i++) {
 	if (argv[i][0] != '-') {
 	    parseAndPrintFile(argv[i]);
diff --git a/tree.c b/tree.c
index 98b5b39..900f221 100644
--- a/tree.c
+++ b/tree.c
@@ -556,6 +556,8 @@
     cur->refs = NULL;
     extSubset = cur->extSubset;
     intSubset = cur->intSubset;
+    if (intSubset == extSubset)
+	extSubset = NULL;
     if (extSubset != NULL) {
 	xmlUnlinkNode((xmlNodePtr) cur->extSubset);
 	cur->extSubset = NULL;
diff --git a/xmlmemory.c b/xmlmemory.c
index 3938a04..1c1ed36 100644
--- a/xmlmemory.c
+++ b/xmlmemory.c
@@ -653,6 +653,8 @@
 {
     FILE *dump;
 
+    if (debugMaxMemSize == 0)
+	return;
     dump = fopen(".memdump", "w");
     if (dump == NULL)
 	xmlMemoryDumpFile = stderr;