1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-07-29 11:41:22 +03:00

applied patch from Stphane 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 Stphane 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
This commit is contained in:
Daniel Veillard
2004-07-01 12:56:30 +00:00
parent 8165a6b130
commit f93a866079
7 changed files with 93 additions and 12 deletions

View File

@ -1,3 +1,12 @@
Thu Jul 1 14:53:36 CEST 2004 Daniel Veillard <daniel@veillard.com>
* python/libxml.c python/generator.py python/libxml.py
python/libxml2-python-api.xml python/libxml2class.txt:
applied patch from St<53>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.
Thu Jul 1 13:18:02 CEST 2004 Daniel Veillard <daniel@veillard.com> Thu Jul 1 13:18:02 CEST 2004 Daniel Veillard <daniel@veillard.com>
* xmlreader.c: seems the reader buffer could be used while not * xmlreader.c: seems the reader buffer could be used while not

View File

@ -302,6 +302,8 @@ def skip_function(name):
return 1 return 1
if name == "xmlFreeParserCtxt": if name == "xmlFreeParserCtxt":
return 1 return 1
if name == "xmlCleanupParser":
return 1
if name == "xmlFreeTextReader": if name == "xmlFreeTextReader":
return 1 return 1
# if name[0:11] == "xmlXPathNew": # if name[0:11] == "xmlXPathNew":

View File

@ -75,6 +75,9 @@ static xmlMallocFunc mallocFunc = NULL;
static xmlReallocFunc reallocFunc = NULL; static xmlReallocFunc reallocFunc = NULL;
static xmlStrdupFunc strdupFunc = NULL; static xmlStrdupFunc strdupFunc = NULL;
static void
libxml_xmlErrorInitialize(void); /* forward declare */
PyObject * PyObject *
libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
{ {
@ -104,13 +107,21 @@ libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
(strdupFunc == xmlMemoryStrdup)) { (strdupFunc == xmlMemoryStrdup)) {
libxmlMemoryAllocatedBase = xmlMemUsed(); libxmlMemoryAllocatedBase = xmlMemUsed();
} else { } 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, ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
xmlMemRealloc, xmlMemoryStrdup); xmlMemRealloc, xmlMemoryStrdup);
if (ret < 0) if (ret < 0)
goto error; goto error;
libxmlMemoryAllocatedBase = xmlMemUsed(); libxmlMemoryAllocatedBase = xmlMemUsed();
/* reinitialize */
xmlInitParser();
libxml_xmlErrorInitialize();
} }
xmlInitParser();
ret = 0; ret = 0;
} else if (libxmlMemoryDebugActivated == 0) { } else if (libxmlMemoryDebugActivated == 0) {
libxmlMemoryAllocatedBase = xmlMemUsed(); libxmlMemoryAllocatedBase = xmlMemUsed();
@ -132,6 +143,29 @@ libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
return (py_retval); return (py_retval);
} }
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 * PyObject *
libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self, libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
ATTRIBUTE_UNUSED PyObject * args) ATTRIBUTE_UNUSED PyObject * args)
@ -3228,20 +3262,19 @@ void
initlibxml2mod(void) initlibxml2mod(void)
{ {
static int initialized = 0; static int initialized = 0;
PyObject *m;
if (initialized != 0) if (initialized != 0)
return; return;
/* XXX xmlInitParser does much more than this */
xmlInitGlobals(); /* intialize the python extension module */
#ifdef LIBXML_OUTPUT_ENABLED Py_InitModule((char *) "libxml2mod", libxmlMethods);
xmlRegisterDefaultOutputCallbacks();
#endif /* LIBXML_OUTPUT_ENABLED */ /* initialize libxml2 */
xmlRegisterDefaultInputCallbacks(); xmlInitParser();
m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
initialized = 1;
libxml_xmlErrorInitialize(); libxml_xmlErrorInitialize();
initialized = 1;
#ifdef MERGED_MODULES #ifdef MERGED_MODULES
initlibxsltmod(); initlibxsltmod();
#endif #endif

View File

@ -618,6 +618,11 @@ class xmlTextReaderCore:
# assert f is _xmlTextReaderErrorFunc # assert f is _xmlTextReaderErrorFunc
return arg 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 # WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
# #

View File

@ -306,5 +306,9 @@
<return type='int' info="The error line" field="line"/> <return type='int' info="The error line" field="line"/>
<arg name='Error' type='xmlErrorPtr' info='the Error'/> <arg name='Error' type='xmlErrorPtr' info='the Error'/>
</function> </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> </symbols>
</api> </api>

View File

@ -104,7 +104,6 @@ nanoHTTPInit()
nanoHTTPScanProxy() nanoHTTPScanProxy()
# functions from module parser # functions from module parser
cleanupParser()
createDocParserCtxt() createDocParserCtxt()
initParser() initParser()
keepBlanksDefault() keepBlanksDefault()
@ -151,6 +150,7 @@ dumpMemory()
htmlCreatePushParser() htmlCreatePushParser()
htmlSAXParseFile() htmlSAXParseFile()
newNode() newNode()
pythonCleanupParser()
setEntityLoader() setEntityLoader()
# functions from module relaxng # functions from module relaxng

View File

@ -29,6 +29,7 @@
#include <ctype.h> #include <ctype.h>
#endif #endif
/* #define DEBUG_MEMORY */
/** /**
* MEM_LIST: * MEM_LIST:
@ -780,6 +781,10 @@ xmlMemoryDump(void)
int int
xmlInitMemory(void) xmlInitMemory(void)
{ {
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlInitMemory()\n");
#endif
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
char *breakpoint; char *breakpoint;
#endif #endif
@ -808,7 +813,6 @@ xmlInitMemory(void)
xmlGenericError(xmlGenericErrorContext, xmlGenericError(xmlGenericErrorContext,
"xmlInitMemory() Ok\n"); "xmlInitMemory() Ok\n");
#endif #endif
return(0); return(0);
} }
@ -819,12 +823,20 @@ xmlInitMemory(void)
*/ */
void void
xmlCleanupMemory(void) { xmlCleanupMemory(void) {
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlCleanupMemory()\n");
#endif
if (xmlMemInitialized == 0) if (xmlMemInitialized == 0)
return; return;
xmlFreeMutex(xmlMemMutex); xmlFreeMutex(xmlMemMutex);
xmlMemMutex = NULL; xmlMemMutex = NULL;
xmlMemInitialized = 0; xmlMemInitialized = 0;
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlCleanupMemory() Ok\n");
#endif
} }
/** /**
@ -845,6 +857,10 @@ xmlCleanupMemory(void) {
int int
xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) { xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlMemSetup()\n");
#endif
if (freeFunc == NULL) if (freeFunc == NULL)
return(-1); return(-1);
if (mallocFunc == NULL) if (mallocFunc == NULL)
@ -858,6 +874,10 @@ xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
xmlMallocAtomic = mallocFunc; xmlMallocAtomic = mallocFunc;
xmlRealloc = reallocFunc; xmlRealloc = reallocFunc;
xmlMemStrdup = strdupFunc; xmlMemStrdup = strdupFunc;
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlMemSetup() Ok\n");
#endif
return(0); return(0);
} }
@ -904,6 +924,10 @@ int
xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc, xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
xmlStrdupFunc strdupFunc) { xmlStrdupFunc strdupFunc) {
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlGcMemSetup()\n");
#endif
if (freeFunc == NULL) if (freeFunc == NULL)
return(-1); return(-1);
if (mallocFunc == NULL) if (mallocFunc == NULL)
@ -919,6 +943,10 @@ xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
xmlMallocAtomic = mallocAtomicFunc; xmlMallocAtomic = mallocAtomicFunc;
xmlRealloc = reallocFunc; xmlRealloc = reallocFunc;
xmlMemStrdup = strdupFunc; xmlMemStrdup = strdupFunc;
#ifdef DEBUG_MEMORY
xmlGenericError(xmlGenericErrorContext,
"xmlGcMemSetup() Ok\n");
#endif
return(0); return(0);
} }