applied patch from Stéphane Bidoul to fix some Python bindings
* python/libxml.c python/generator.py python/libxml.py
python/libxml2-python-api.xml python/libxml2class.txt:
applied patch from Stéphane Bidoul to fix some Python bindings
initialization, then had to change the parserCleanup()
to handle memory released there.
* xmlmemory.c: added more debugging comments.
Daniel
diff --git a/python/generator.py b/python/generator.py
index e0f1096..3ae5729 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -302,6 +302,8 @@
return 1
if name == "xmlFreeParserCtxt":
return 1
+ if name == "xmlCleanupParser":
+ return 1
if name == "xmlFreeTextReader":
return 1
# if name[0:11] == "xmlXPathNew":
diff --git a/python/libxml.c b/python/libxml.c
index 4ab69fb..eb44a2b 100644
--- a/python/libxml.c
+++ b/python/libxml.c
@@ -75,6 +75,9 @@
static xmlReallocFunc reallocFunc = NULL;
static xmlStrdupFunc strdupFunc = NULL;
+static void
+libxml_xmlErrorInitialize(void); /* forward declare */
+
PyObject *
libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
{
@@ -104,13 +107,21 @@
(strdupFunc == xmlMemoryStrdup)) {
libxmlMemoryAllocatedBase = xmlMemUsed();
} else {
+ /*
+ * cleanup first, because some memory has been
+ * allocated with the non-debug malloc in xmlInitParser
+ * when the python module was imported
+ */
+ xmlCleanupParser();
ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
xmlMemRealloc, xmlMemoryStrdup);
if (ret < 0)
goto error;
libxmlMemoryAllocatedBase = xmlMemUsed();
+ /* reinitialize */
+ xmlInitParser();
+ libxml_xmlErrorInitialize();
}
- xmlInitParser();
ret = 0;
} else if (libxmlMemoryDebugActivated == 0) {
libxmlMemoryAllocatedBase = xmlMemUsed();
@@ -133,6 +144,29 @@
}
PyObject *
+libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
+ PyObject *args ATTRIBUTE_UNUSED) {
+
+ long freed;
+
+ if (libxmlMemoryDebug) {
+ freed = xmlMemUsed();
+ }
+
+ xmlCleanupParser();
+
+ if (libxmlMemoryDebug) {
+ freed -= xmlMemUsed();
+ libxmlMemoryAllocatedBase -= freed;
+ if (libxmlMemoryAllocatedBase < 0)
+ libxmlMemoryAllocatedBase = 0;
+ }
+
+ Py_INCREF(Py_None);
+ return(Py_None);
+}
+
+PyObject *
libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
ATTRIBUTE_UNUSED PyObject * args)
{
@@ -3228,20 +3262,19 @@
initlibxml2mod(void)
{
static int initialized = 0;
- PyObject *m;
if (initialized != 0)
return;
- /* XXX xmlInitParser does much more than this */
- xmlInitGlobals();
-#ifdef LIBXML_OUTPUT_ENABLED
- xmlRegisterDefaultOutputCallbacks();
-#endif /* LIBXML_OUTPUT_ENABLED */
- xmlRegisterDefaultInputCallbacks();
- m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
- initialized = 1;
+
+ /* intialize the python extension module */
+ Py_InitModule((char *) "libxml2mod", libxmlMethods);
+
+ /* initialize libxml2 */
+ xmlInitParser();
libxml_xmlErrorInitialize();
+ initialized = 1;
+
#ifdef MERGED_MODULES
initlibxsltmod();
#endif
diff --git a/python/libxml.py b/python/libxml.py
index 0c3ba6c..5980974 100644
--- a/python/libxml.py
+++ b/python/libxml.py
@@ -618,6 +618,11 @@
# assert f is _xmlTextReaderErrorFunc
return arg
+#
+# The cleanup now goes though a wrappe in libxml.c
+#
+def cleanupParser():
+ libxml2mod.xmlPythonCleanupParser()
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
#
diff --git a/python/libxml2-python-api.xml b/python/libxml2-python-api.xml
index d5a4cbe..f6ecd83 100644
--- a/python/libxml2-python-api.xml
+++ b/python/libxml2-python-api.xml
@@ -306,5 +306,9 @@
<return type='int' info="The error line" field="line"/>
<arg name='Error' type='xmlErrorPtr' info='the Error'/>
</function>
+ <function name='xmlPythonCleanupParser' file='python'>
+ <info>Cleanup function for the XML library. It tries to reclaim all parsing related global memory allocated for the library processing. It doesn't deallocate any document related memory. Calling this function should not prevent reusing the library but one should call xmlCleanupParser() only when the process has finished using the library or XML document built with it.</info>
+ <return type='void'/>
+ </function>
</symbols>
</api>
diff --git a/python/libxml2class.txt b/python/libxml2class.txt
index 7479843..6266a9d 100644
--- a/python/libxml2class.txt
+++ b/python/libxml2class.txt
@@ -104,7 +104,6 @@
nanoHTTPScanProxy()
# functions from module parser
-cleanupParser()
createDocParserCtxt()
initParser()
keepBlanksDefault()
@@ -151,6 +150,7 @@
htmlCreatePushParser()
htmlSAXParseFile()
newNode()
+pythonCleanupParser()
setEntityLoader()
# functions from module relaxng