fixed problem pointed out by Stéphane Bidoul on the list. completed

* python/generator.py, python/libxml2class.txt: fixed problem
  pointed out by Stéphane Bidoul on the list.
* xinclude.c, xpointer.c, xpath.c, include/libxml/xpointer.h:
  completed modifications required to fix Bug 129967 (at last!).
  Now wait to see how long before further trouble...
diff --git a/ChangeLog b/ChangeLog
index 8b88c4e..d3cfd1d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Dec 31 15:55:55 HKT 2003 William Brack <wbrack@mmm.com.hk>
+
+	* python/generator.py, python/libxml2class.txt: fixed problem
+	  pointed out by Stéphane Bidoul on the list.
+	* xinclude.c, xpointer.c, xpath.c, include/libxml/xpointer.h:
+	  completed modifications required to fix Bug 129967 (at last!).
+	  Now wait to see how long before further trouble...
+
 Tue Dec 30 16:26:13 HKT 2003 William Brack <wbrack@mmm.com.hk>
 
 	* parser.c, xmlmemory.c, include/libxml/xmlmemory.h: Fixed
diff --git a/include/libxml/xpointer.h b/include/libxml/xpointer.h
index 2856bdd..2285b0e 100644
--- a/include/libxml/xpointer.h
+++ b/include/libxml/xpointer.h
@@ -103,8 +103,6 @@
 		    xmlXPtrBuildNodeList	(xmlXPathObjectPtr obj);
 XMLPUBFUN void XMLCALL		
 		    xmlXPtrEvalRangePredicate	(xmlXPathParserContextPtr ctxt);
-XMLPUBFUN xmlNodePtr XMLCALL
-		    xmlXPtrAdvanceNode		(xmlNodePtr cur);
 #ifdef __cplusplus
 }
 #endif
diff --git a/python/generator.py b/python/generator.py
index 3896177..ce0b8f3 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -325,6 +325,8 @@
         return 1
     if name == "xmlOutputBufferFlush": # handled by by the superclass
         return 1
+    if name == "xmlErrMemory":
+        return 1
     return 0
 
 def print_function_wrapper(name, output, export, include):
@@ -573,8 +575,6 @@
     wrapper = open("libxml2-py.c", "w")
     wrapper.write("/* Generated */\n\n")
     wrapper.write("#include <Python.h>\n")
-#    wrapper.write("#include \"config.h\"\n")
-    wrapper.write("#define IN_LIBXML\n")
     wrapper.write("#include <libxml/xmlversion.h>\n")
     wrapper.write("#include <libxml/tree.h>\n")
     wrapper.write("#include <libxml/xmlschemastypes.h>\n")
diff --git a/python/libxml2class.txt b/python/libxml2class.txt
index 8eab930..a5de430 100644
--- a/python/libxml2class.txt
+++ b/python/libxml2class.txt
@@ -896,7 +896,6 @@
 
     # functions from module parserInternals
     decodeEntities()
-    errMemory()
     handleEntity()
     namespaceParseNCName()
     namespaceParseNSDef()
diff --git a/xinclude.c b/xinclude.c
index ca66d62..3d27f5e 100644
--- a/xinclude.c
+++ b/xinclude.c
@@ -844,6 +844,7 @@
     return(cur);
 }
 
+xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
 /**
  * xmlXIncludeCopyRange:
  * @ctxt:  the XInclude context
@@ -860,10 +861,12 @@
 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
 	                xmlDocPtr source, xmlXPathObjectPtr range) {
     /* pointers to generated nodes */
-    xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
+    xmlNodePtr list = NULL, last = NULL, listParent = NULL;
+    xmlNodePtr tmp, tmp2;
     /* pointers to traversal nodes */
     xmlNodePtr start, cur, end;
     int index1, index2;
+    int level = 0, lastLevel = 0;
 
     if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
 	(range == NULL))
@@ -881,7 +884,36 @@
     cur = start;
     index1 = range->index;
     index2 = range->index2;
+    /*
+     * level is depth of the current node under consideration
+     * list is the pointer to the root of the output tree
+     * listParent is a pointer to the parent of output tree (within
+       the included file) in case we need to add another level
+     * last is a pointer to the last node added to the output tree
+     * lastLevel is the depth of last (relative to the root)
+     */
     while (cur != NULL) {
+	/*
+	 * Check if our output tree needs a parent
+	 */
+	if (level < 0) {
+	    while (level < 0) {
+	        tmp2 = xmlDocCopyNode(listParent, target, 0);
+	        xmlAddChild(tmp2, list);
+	        list = tmp2;
+	        listParent = listParent->parent;
+	        level++;
+	    }
+	    last = list;
+	    lastLevel = 0;
+	}
+	/*
+	 * Check whether we need to change our insertion point
+	 */
+	while (level < lastLevel) {
+	    last = last->parent;
+	    lastLevel --;
+	}
 	if (cur == end) {	/* Are we at the end of the range? */
 	    if (cur->type == XML_TEXT_NODE) {
 		const xmlChar *content = cur->content;
@@ -904,23 +936,25 @@
 		if (list == NULL)
 		    return(tmp);
 		/* prune and return full set */
-		if (last != NULL)
+		if (level == lastLevel)
 		    xmlAddNextSibling(last, tmp);
 		else 
-		    xmlAddChild(parent, tmp);
+		    xmlAddChild(last, tmp);
 		return(list);
 	    } else {	/* ending node not a text node */
 		tmp = xmlDocCopyNode(cur, target, 0);
-		if (list == NULL)
+		if (list == NULL) {
 		    list = tmp;
-		else {
-		    if (last != NULL)
+		    listParent = cur->parent;
+		} else {
+		    if (level == lastLevel)
 			xmlAddNextSibling(last, tmp);
-		    else
-			xmlAddChild(parent, tmp);
+		    else {
+			xmlAddChild(last, tmp);
+			lastLevel = level;
+		    }
 		}
-		last = NULL;
-		parent = tmp;
+		last = tmp;
 
 		if (index2 > 1) {
 		    end = xmlXIncludeGetNthChild(cur, index2 - 1);
@@ -937,8 +971,7 @@
 		 */
 		continue; /* while */
 	    }
-	} else if ((cur == start) &&	/* Not at the end, are we at start? */
-		   (list == NULL) /* looks superfluous but ... */ ) {
+	} else if (cur == start) {	/* Not at the end, are we at start? */
 	    if ((cur->type == XML_TEXT_NODE) ||
 		(cur->type == XML_CDATA_SECTION_NODE)) {
 		const xmlChar *content = cur->content;
@@ -953,23 +986,20 @@
 		    tmp = xmlNewText(content);
 		}
 		last = list = tmp;
+		listParent = cur->parent;
 	    } else {		/* Not text node */
+		tmp = xmlDocCopyNode(cur, target, 0);
+		list = last = tmp;
+		listParent = cur->parent;
 		if (index1 > 1) {	/* Do we need to position? */
-		    tmp = xmlDocCopyNode(cur, target, 0);
-		    list = tmp;
-		    parent = tmp;
-		    last = NULL;
 		    cur = xmlXIncludeGetNthChild(cur, index1 - 1);
+		    level = lastLevel = 1;
 		    index1 = 0;
 		    /*
 		     * Now gather the remaining nodes from cur to end
 		     */
 		    continue; /* while */
 		}
-		tmp = xmlDocCopyNode(cur, target, 0);
-		list = tmp;
-		parent = NULL;
-		last = tmp;
 	    }
 	} else {
 	    tmp = NULL;
@@ -995,24 +1025,19 @@
 		    break;
 	    }
 	    if (tmp != NULL) {
-		if ((list == NULL) || ((last == NULL) && (parent == NULL)))  {
-		    return(NULL);
-		}
-		if (last != NULL)
+		if (level == lastLevel)
 		    xmlAddNextSibling(last, tmp);
 		else {
-		    xmlAddChild(parent, tmp);
-		    last = tmp;
+		    xmlAddChild(last, tmp);
+		    lastLevel = level;
 		}
+		last = tmp;
 	    }
 	}
 	/*
 	 * Skip to next node in document order
 	 */
-	if ((list == NULL) || ((last == NULL) && (parent == NULL)))  {
-	    return(NULL);
-	}
-	cur = xmlXPtrAdvanceNode(cur);
+	cur = xmlXPtrAdvanceNode(cur, &level);
     }
     return(list);
 }
diff --git a/xpath.c b/xpath.c
index 563e307..4b0ca11 100644
--- a/xpath.c
+++ b/xpath.c
@@ -10447,10 +10447,10 @@
                          * single item in the nodelocset.
                          */
                         ctxt->context->node = oldlocset->locTab[i]->user;
-                        tmp = xmlXPathNewNodeSet(ctxt->context->node);
-                        valuePush(ctxt, tmp);
                         ctxt->context->contextSize = oldlocset->locNr;
                         ctxt->context->proximityPosition = i + 1;
+                        tmp = xmlXPathNewNodeSet(ctxt->context->node);
+                        valuePush(ctxt, tmp);
 
                         if (op->ch2 != -1)
                             total +=
@@ -10632,9 +10632,9 @@
                          * Run the evaluation with a node list made of a
                          * single item in the nodelocset.
                          */
-                        ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
-                        ctxt->context->contextSize = 1;
-                        ctxt->context->proximityPosition = 1;
+                        ctxt->context->node = oldlocset->locTab[i]->user;
+                        ctxt->context->contextSize = oldlocset->locNr;
+                        ctxt->context->proximityPosition = i + 1;
                         tmp = xmlXPathNewNodeSet(ctxt->context->node);
                         valuePush(ctxt, tmp);
 
@@ -10644,10 +10644,6 @@
                                                    &comp->steps[op->ch2]);
                         CHECK_ERROR0;
 
-                        /*
-                         * The result of the evaluation needs to be tested to
-                         * decide whether the filter succeeded or not
-                         */
                         res = valuePop(ctxt);
 			if (res->type == XPATH_LOCATIONSET) {
 			    xmlLocationSetPtr rloc = 
@@ -10706,10 +10702,6 @@
                                                    &comp->steps[op->ch2]);
                             CHECK_ERROR0;
 
-                            /*
-                             * The result of the evaluation need to be tested to
-                             * decided whether the filter succeeded or not
-                             */
                             res = valuePop(ctxt);
                             range =
                                 xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
diff --git a/xpointer.c b/xpointer.c
index adc854c..145b134 100644
--- a/xpointer.c
+++ b/xpointer.c
@@ -123,7 +123,8 @@
  *		A few helper functions for child sequences		*
  *									*
  ************************************************************************/
-
+/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
+xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
 /**
  * xmlXPtrGetArity:
  * @cur:  the node
@@ -1582,7 +1583,7 @@
 	    STRANGE
 	    return(NULL);
 	}
-	cur = xmlXPtrAdvanceNode(cur);
+	cur = xmlXPtrAdvanceNode(cur, NULL);
     }
     return(list);
 }
@@ -2296,12 +2297,14 @@
  * Returns -1 in case of failure, 0 otherwise
  */
 xmlNodePtr
-xmlXPtrAdvanceNode(xmlNodePtr cur) {
+xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
 next:
     if (cur == NULL)
 	return(NULL);
     if (cur->children != NULL) {
         cur = cur->children ;
+	if (level != NULL)
+	    (*level)++;
 	goto found;
     }
     if (cur->next != NULL) {
@@ -2310,6 +2313,8 @@
     }
     do {
         cur = cur->parent;
+	if (level != NULL)
+	    (*level)--;
         if (cur == NULL) return(NULL);
         if (cur->next != NULL) {
 	    cur = cur->next;
@@ -2366,7 +2371,7 @@
 		cur = xmlXPtrGetNthChild(cur, pos);
 		pos = 0;
 	    } else {
-		cur = xmlXPtrAdvanceNode(cur);
+		cur = xmlXPtrAdvanceNode(cur, NULL);
 		pos = 0;
 	    }
 	}
@@ -2401,7 +2406,7 @@
 	}
 	if (pos + bytes >= len) {
 	    bytes -= (len - pos);
-	    cur = xmlXPtrAdvanceNode(cur);
+	    cur = xmlXPtrAdvanceNode(cur, NULL);
 	    cur = 0;
 	} else if (pos + bytes < len) {
 	    pos += bytes;
@@ -2490,7 +2495,7 @@
 		}
 	    }
 	}
-	cur = xmlXPtrAdvanceNode(cur);
+	cur = xmlXPtrAdvanceNode(cur, NULL);
 	if (cur == NULL)
 	    return(0);
 	pos = 0;
@@ -2583,7 +2588,7 @@
 	}
 	if ((cur == *end) && (pos >= *endindex))
 	    return(0);
-	cur = xmlXPtrAdvanceNode(cur);
+	cur = xmlXPtrAdvanceNode(cur, NULL);
 	if (cur == NULL)
 	    return(0);
 	pos = 1;