diff options
author | Mike Hommey <mh@glandium.org> | 2004-07-06 12:57:17 +0000 |
---|---|---|
committer | Mike Hommey <mh@glandium.org> | 2004-07-06 12:57:17 +0000 |
commit | c14c53a3645d81281058d4bb4cff24fa8d6faf33 (patch) | |
tree | 29bccc2e7499af078a3d1cdcfb517a1dee891be5 /python/libxml.c | |
parent | d4e028c96af89ade493b440d4f2de6b684c03a06 (diff) | |
download | libxml2-c14c53a3645d81281058d4bb4cff24fa8d6faf33.tar.gz |
Load /tmp/tmp.DIvcnD/libxml2-2.6.11 intoupstream/2.6.11
packages/libxml2/branches/upstream/current.
Diffstat (limited to 'python/libxml.c')
-rw-r--r-- | python/libxml.c | 169 |
1 files changed, 120 insertions, 49 deletions
diff --git a/python/libxml.c b/python/libxml.c index 4ab69fb..3db0f5f 100644 --- a/python/libxml.c +++ b/python/libxml.c @@ -52,6 +52,22 @@ void initlibxml2mod(void); xmlGenericError(xmlGenericErrorContext, \ "Unimplemented block at %s:%d\n", \ __FILE__, __LINE__); +/* + * the following vars are used for XPath extensions, but + * are also referenced within the parser cleanup routine. + */ +static int libxml_xpathCallbacksInitialized = 0; + +typedef struct libxml_xpathCallback { + xmlXPathContextPtr ctx; + xmlChar *name; + xmlChar *ns_uri; + PyObject *function; +} libxml_xpathCallback, *libxml_xpathCallbackPtr; +typedef libxml_xpathCallback libxml_xpathCallbackArray[]; +static int libxml_xpathCallbacksAllocd = 10; +static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL; +static int libxml_xpathCallbacksNb = 0; /************************************************************************ * * @@ -75,6 +91,21 @@ static xmlMallocFunc mallocFunc = NULL; static xmlReallocFunc reallocFunc = NULL; static xmlStrdupFunc strdupFunc = NULL; +static void +libxml_xmlErrorInitialize(void); /* forward declare */ + +PyObject * +libxml_xmlMemoryUsed(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) +{ + long ret; + PyObject *py_retval; + + ret = xmlMemUsed(); + + py_retval = libxml_longWrap(ret); + return (py_retval); +} + PyObject * libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) { @@ -104,13 +135,21 @@ libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) (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 +172,46 @@ libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) } PyObject * +libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED, + PyObject *args ATTRIBUTE_UNUSED) { + + int ix; + long freed = -1; + + if (libxmlMemoryDebug) { + freed = xmlMemUsed(); + } + + xmlCleanupParser(); + /* + * Need to confirm whether we really want to do this (required for + * memcheck) in all cases... + */ + + if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */ + for (ix=0; ix<libxml_xpathCallbacksNb; ix++) { + if ((*libxml_xpathCallbacks)[ix].name != NULL) + xmlFree((*libxml_xpathCallbacks)[ix].name); + if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL) + xmlFree((*libxml_xpathCallbacks)[ix].ns_uri); + } + libxml_xpathCallbacksNb = 0; + xmlFree(libxml_xpathCallbacks); + libxml_xpathCallbacks = NULL; + } + + 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) { @@ -1386,7 +1465,7 @@ libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self, return (NULL); #ifdef DEBUG_ERROR - printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx, + printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx, pyobj_f); #endif @@ -1776,18 +1855,6 @@ libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { * * ************************************************************************/ -static int libxml_xpathCallbacksInitialized = 0; - -typedef struct libxml_xpathCallback { - xmlXPathContextPtr ctx; - xmlChar *name; - xmlChar *ns_uri; - PyObject *function; -} libxml_xpathCallback, *libxml_xpathCallbackPtr; -static libxml_xpathCallback libxml_xpathCallbacks[10]; -static int libxml_xpathCallbacksNb = 0; -static int libxml_xpathCallbacksMax = 10; - static void libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) { @@ -1816,9 +1883,9 @@ libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) */ for (i = 0; i < libxml_xpathCallbacksNb; i++) { if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */ - (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && - (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { - current_function = libxml_xpathCallbacks[i].function; + (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && + (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { + current_function = (*libxml_xpathCallbacks)[i].function; } } if (current_function == NULL) { @@ -1859,9 +1926,9 @@ libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name, * and functionURI fields. */ for (i = 0; i < libxml_xpathCallbacksNb; i++) { - if ((ctxt == libxml_xpathCallbacks[i].ctx) && - (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && - (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { + if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) && + (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && + (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { return (libxml_xmlXPathFuncCallback); } } @@ -1879,12 +1946,14 @@ libxml_xpathCallbacksInitialize(void) #ifdef DEBUG_XPATH printf("libxml_xpathCallbacksInitialized called\n"); #endif + libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc( + libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback)); - for (i = 0; i < libxml_xpathCallbacksMax; i++) { - libxml_xpathCallbacks[i].ctx = NULL; - libxml_xpathCallbacks[i].name = NULL; - libxml_xpathCallbacks[i].ns_uri = NULL; - libxml_xpathCallbacks[i].function = NULL; + for (i = 0; i < libxml_xpathCallbacksAllocd; i++) { + (*libxml_xpathCallbacks)[i].ctx = NULL; + (*libxml_xpathCallbacks)[i].name = NULL; + (*libxml_xpathCallbacks)[i].ns_uri = NULL; + (*libxml_xpathCallbacks)[i].function = NULL; } libxml_xpathCallbacksInitialized = 1; } @@ -1921,27 +1990,30 @@ libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self, ctx, name, ns_uri); #endif for (i = 0; i < libxml_xpathCallbacksNb; i++) { - if ((ctx == libxml_xpathCallbacks[i].ctx) && - (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && - (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { + if ((ctx == (*libxml_xpathCallbacks)[i].ctx) && + (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) && + (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) { Py_XINCREF(pyobj_f); - Py_XDECREF(libxml_xpathCallbacks[i].function); - libxml_xpathCallbacks[i].function = pyobj_f; + Py_XDECREF((*libxml_xpathCallbacks)[i].function); + (*libxml_xpathCallbacks)[i].function = pyobj_f; c_retval = 1; goto done; } } - if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) { - printf("libxml_registerXPathFunction() table full\n"); - } else { - i = libxml_xpathCallbacksNb++; - Py_XINCREF(pyobj_f); - libxml_xpathCallbacks[i].ctx = ctx; - libxml_xpathCallbacks[i].name = xmlStrdup(name); - libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri); - libxml_xpathCallbacks[i].function = pyobj_f; + if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) { + libxml_xpathCallbacksAllocd+=10; + libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc( + libxml_xpathCallbacks, + libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback)); + } + i = libxml_xpathCallbacksNb++; + Py_XINCREF(pyobj_f); + (*libxml_xpathCallbacks)[i].ctx = ctx; + (*libxml_xpathCallbacks)[i].name = xmlStrdup(name); + (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri); + (*libxml_xpathCallbacks)[i].function = pyobj_f; c_retval = 1; - } + done: py_retval = libxml_intWrap((int) c_retval); return (py_retval); @@ -3228,20 +3300,19 @@ void 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 |