summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorMike Hommey <glandium@debian.org>2009-03-01 10:54:34 +0100
committerMike Hommey <glandium@debian.org>2009-03-01 10:54:34 +0100
commit0c1d871e4c5e46a2945cccb2ce765f9be2fe01fb (patch)
tree982382b30eb118c65e1a06b25757dac5c3c69b8d /python
parentd03a853bb0370d89552eceee59df1746da4a37f8 (diff)
downloadlibxml2-0c1d871e4c5e46a2945cccb2ce765f9be2fe01fb.tar.gz
Import upstream version 2.7.1upstream/2.7.1.dfsg
Diffstat (limited to 'python')
-rw-r--r--python/libxml.c147
-rwxr-xr-xpython/setup.py2
-rw-r--r--python/tests/Makefile.am3
-rw-r--r--python/tests/Makefile.in3
-rwxr-xr-xpython/tests/xpathleak.py53
5 files changed, 91 insertions, 117 deletions
diff --git a/python/libxml.c b/python/libxml.c
index 95b3cb9..3f2ede7 100644
--- a/python/libxml.c
+++ b/python/libxml.c
@@ -24,6 +24,7 @@
#include <libxml/xmlIO.h>
#include <libxml/c14n.h>
#include <libxml/xmlreader.h>
+#include <libxml/xmlsave.h>
#include "libxml_wrap.h"
#include "libxml2-py.h"
@@ -2761,6 +2762,9 @@ libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
const char *encoding;
int format;
int len;
+ xmlSaveCtxtPtr ctxt;
+ xmlBufferPtr buf;
+ int options = 0;
if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
&encoding, &format))
@@ -2773,137 +2777,52 @@ libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
}
if (node->type == XML_DOCUMENT_NODE) {
doc = (xmlDocPtr) node;
- xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
- (const char *) encoding, format);
- py_retval = libxml_charPtrWrap((char *) c_retval);
+ node = NULL;
#ifdef LIBXML_HTML_ENABLED
} else if (node->type == XML_HTML_DOCUMENT_NODE) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
doc = (xmlDocPtr) node;
- if (encoding != NULL)
- htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
- encoding = (const char *) htmlGetMetaEncoding(doc);
-
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- /*
- * Fallback to HTML or ASCII when the encoding is unspecified
- */
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("HTML");
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("ascii");
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
-#endif /* LIBXML_HTML_ENABLED */
+ node = NULL;
+#endif
} else {
if (node->type == XML_NAMESPACE_DECL)
doc = NULL;
else
doc = node->doc;
if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
#ifdef LIBXML_HTML_ENABLED
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
- if (encoding != NULL)
- htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
- encoding = (const char *) htmlGetMetaEncoding(doc);
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- /*
- * Fallback to HTML or ASCII when the encoding is unspecified
- */
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("HTML");
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("ascii");
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
#endif /* LIBXML_HTML_ENABLED */
} else {
Py_INCREF(Py_None);
return (Py_None);
}
}
+
+
+ buf = xmlBufferCreate();
+ if (buf == NULL) {
+ Py_INCREF(Py_None);
+ return (Py_None);
+ }
+ if (format) options |= XML_SAVE_FORMAT;
+ ctxt = xmlSaveToBuffer(buf, encoding, options);
+ if (ctxt == NULL) {
+ xmlBufferFree(buf);
+ Py_INCREF(Py_None);
+ return (Py_None);
+ }
+ if (node == NULL)
+ xmlSaveDoc(ctxt, doc);
+ else
+ xmlSaveTree(ctxt, node);
+ xmlSaveClose(ctxt);
+
+ c_retval = buf->content;
+ buf->content = NULL;
+
+ xmlBufferFree(buf);
+ py_retval = libxml_charPtrWrap((char *) c_retval);
+
return (py_retval);
}
diff --git a/python/setup.py b/python/setup.py
index 723a4c9..75b3a40 100755
--- a/python/setup.py
+++ b/python/setup.py
@@ -226,7 +226,7 @@ else:
setup (name = "libxml2-python",
# On *nix, the version number is created from setup.py.in
# On windows, it is set by configure.js
- version = "2.7.0",
+ version = "2.7.1",
description = descr,
author = "Daniel Veillard",
author_email = "veillard@redhat.com",
diff --git a/python/tests/Makefile.am b/python/tests/Makefile.am
index 8a85075..52c89fc 100644
--- a/python/tests/Makefile.am
+++ b/python/tests/Makefile.am
@@ -46,7 +46,8 @@ PYTESTS= \
validSchemas.py \
validRNG.py \
compareNodes.py \
- xpathns.py
+ xpathns.py \
+ xpathleak.py
XMLS= \
tst.xml \
diff --git a/python/tests/Makefile.in b/python/tests/Makefile.in
index d7c00e5..ec3036f 100644
--- a/python/tests/Makefile.in
+++ b/python/tests/Makefile.in
@@ -317,7 +317,8 @@ PYTESTS = \
validSchemas.py \
validRNG.py \
compareNodes.py \
- xpathns.py
+ xpathns.py \
+ xpathleak.py
XMLS = \
tst.xml \
diff --git a/python/tests/xpathleak.py b/python/tests/xpathleak.py
new file mode 100755
index 0000000..dcc144c
--- /dev/null
+++ b/python/tests/xpathleak.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+import sys, libxml2
+
+libxml2.debugMemory(True)
+
+expect="""--> Invalid expression
+--> xmlXPathEval: evaluation failed
+--> Invalid expression
+--> xmlXPathEval: evaluation failed
+--> Invalid expression
+--> xmlXPathEval: evaluation failed
+--> Invalid expression
+--> xmlXPathEval: evaluation failed
+--> Invalid expression
+--> xmlXPathEval: evaluation failed
+--> Invalid expression
+--> xmlXPathEval: evaluation failed
+"""
+err=""
+def callback(ctx, str):
+ global err
+
+ err = err + "%s %s" % (ctx, str)
+
+libxml2.registerErrorHandler(callback, "-->")
+
+doc = libxml2.parseDoc("<fish/>")
+ctxt = doc.xpathNewContext()
+ctxt.setContextNode(doc)
+for expr in (":false()","bad:()","bad(:)",":bad(:)","bad:(:)","bad:bad(:)"):
+ try:
+ ctxt.xpathEval(expr)
+ except libxml2.xpathError, e:
+ pass
+ else:
+ print "Unexpectedly legal expression:", expr
+ctxt.xpathFreeContext()
+doc.freeDoc()
+
+if err != expect:
+ print "error"
+ print "received %s" %(err)
+ print "expected %s" %(expect)
+ sys.exit(1)
+
+libxml2.cleanupParser()
+leakedbytes = libxml2.debugMemory(True)
+if leakedbytes == 0:
+ print "OK"
+else:
+ print "Memory leak", leakedbytes, "bytes"
+ # drop file to .memdump file in cwd, but won't work if not compiled in
+ libxml2.dumpMemory()