From c14c53a3645d81281058d4bb4cff24fa8d6faf33 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Tue, 6 Jul 2004 12:57:17 +0000 Subject: Load /tmp/tmp.DIvcnD/libxml2-2.6.11 into packages/libxml2/branches/upstream/current. --- python/libxml.c | 169 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 120 insertions(+), 49 deletions(-) (limited to 'python/libxml.c') 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(); @@ -132,6 +171,46 @@ libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) return (py_retval); } +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_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 -- cgit v1.2.3