Commit 8e2cc6f2 authored by William M. Brack's avatar William M. Brack
Browse files

Changed the number of XPath extension functions allowed to be

* python/libxml.c: Changed the number of XPath extension functions
  allowed to be variable-length (patch supplied by Marc-Antoine
  Parent, bug 143805).  Added code to "unregister" the functions
  when the parser cleanup takes place.
parent 529233cc
Sat Jul 3 11:31:02 HKT 2004 William Brack <wbrack@mmm.com.hk>
* python/libxml.c: Changed the number of XPath extension functions
allowed to be variable-length (patch supplied by Marc-Antoine
Parent, bug 143805). Added code to "unregister" the functions
when the parser cleanup takes place.
Fri Jul 2 14:22:14 CEST 2004 Daniel Veillard <daniel@veillard.com> Fri Jul 2 14:22:14 CEST 2004 Daniel Veillard <daniel@veillard.com>
   
* xmlmemory.c python/libxml.c python/libxml2-python-api.xml: * xmlmemory.c python/libxml.c python/libxml2-python-api.xml:
......
...@@ -52,6 +52,22 @@ void initlibxml2mod(void); ...@@ -52,6 +52,22 @@ void initlibxml2mod(void);
xmlGenericError(xmlGenericErrorContext, \ xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \ "Unimplemented block at %s:%d\n", \
__FILE__, __LINE__); __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;
/************************************************************************ /************************************************************************
* * * *
...@@ -159,13 +175,30 @@ PyObject * ...@@ -159,13 +175,30 @@ PyObject *
libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED, libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args ATTRIBUTE_UNUSED) { PyObject *args ATTRIBUTE_UNUSED) {
long freed; int ix;
long freed = -1;
if (libxmlMemoryDebug) { if (libxmlMemoryDebug) {
freed = xmlMemUsed(); freed = xmlMemUsed();
} }
xmlCleanupParser(); 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) { if (libxmlMemoryDebug) {
freed -= xmlMemUsed(); freed -= xmlMemUsed();
...@@ -1432,7 +1465,7 @@ libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self, ...@@ -1432,7 +1465,7 @@ libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
return (NULL); return (NULL);
#ifdef DEBUG_ERROR #ifdef DEBUG_ERROR
printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx, printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
pyobj_f); pyobj_f);
#endif #endif
...@@ -1822,18 +1855,6 @@ libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) { ...@@ -1822,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 static void
libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
{ {
...@@ -1862,9 +1883,9 @@ libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) ...@@ -1862,9 +1883,9 @@ libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
*/ */
for (i = 0; i < libxml_xpathCallbacksNb; i++) { for (i = 0; i < libxml_xpathCallbacksNb; i++) {
if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */ if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
(xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
(xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
current_function = libxml_xpathCallbacks[i].function; current_function = (*libxml_xpathCallbacks)[i].function;
} }
} }
if (current_function == NULL) { if (current_function == NULL) {
...@@ -1905,9 +1926,9 @@ libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name, ...@@ -1905,9 +1926,9 @@ libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
* and functionURI fields. * and functionURI fields.
*/ */
for (i = 0; i < libxml_xpathCallbacksNb; i++) { for (i = 0; i < libxml_xpathCallbacksNb; i++) {
if ((ctxt == libxml_xpathCallbacks[i].ctx) && if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
(xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
(xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
return (libxml_xmlXPathFuncCallback); return (libxml_xmlXPathFuncCallback);
} }
} }
...@@ -1925,12 +1946,14 @@ libxml_xpathCallbacksInitialize(void) ...@@ -1925,12 +1946,14 @@ libxml_xpathCallbacksInitialize(void)
#ifdef DEBUG_XPATH #ifdef DEBUG_XPATH
printf("libxml_xpathCallbacksInitialized called\n"); printf("libxml_xpathCallbacksInitialized called\n");
#endif #endif
libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
for (i = 0; i < libxml_xpathCallbacksMax; i++) { for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
libxml_xpathCallbacks[i].ctx = NULL; (*libxml_xpathCallbacks)[i].ctx = NULL;
libxml_xpathCallbacks[i].name = NULL; (*libxml_xpathCallbacks)[i].name = NULL;
libxml_xpathCallbacks[i].ns_uri = NULL; (*libxml_xpathCallbacks)[i].ns_uri = NULL;
libxml_xpathCallbacks[i].function = NULL; (*libxml_xpathCallbacks)[i].function = NULL;
} }
libxml_xpathCallbacksInitialized = 1; libxml_xpathCallbacksInitialized = 1;
} }
...@@ -1967,27 +1990,30 @@ libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self, ...@@ -1967,27 +1990,30 @@ libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
ctx, name, ns_uri); ctx, name, ns_uri);
#endif #endif
for (i = 0; i < libxml_xpathCallbacksNb; i++) { for (i = 0; i < libxml_xpathCallbacksNb; i++) {
if ((ctx == libxml_xpathCallbacks[i].ctx) && if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
(xmlStrEqual(name, libxml_xpathCallbacks[i].name)) && (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
(xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) { (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Py_XINCREF(pyobj_f); Py_XINCREF(pyobj_f);
Py_XDECREF(libxml_xpathCallbacks[i].function); Py_XDECREF((*libxml_xpathCallbacks)[i].function);
libxml_xpathCallbacks[i].function = pyobj_f; (*libxml_xpathCallbacks)[i].function = pyobj_f;
c_retval = 1; c_retval = 1;
goto done; goto done;
} }
} }
if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) { if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
printf("libxml_registerXPathFunction() table full\n"); libxml_xpathCallbacksAllocd+=10;
} else { libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
i = libxml_xpathCallbacksNb++; libxml_xpathCallbacks,
Py_XINCREF(pyobj_f); libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
libxml_xpathCallbacks[i].ctx = ctx; }
libxml_xpathCallbacks[i].name = xmlStrdup(name); i = libxml_xpathCallbacksNb++;
libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri); Py_XINCREF(pyobj_f);
libxml_xpathCallbacks[i].function = 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; c_retval = 1;
}
done: done:
py_retval = libxml_intWrap((int) c_retval); py_retval = libxml_intWrap((int) c_retval);
return (py_retval); return (py_retval);
...@@ -3277,7 +3303,7 @@ initlibxml2mod(void) ...@@ -3277,7 +3303,7 @@ initlibxml2mod(void)
if (initialized != 0) if (initialized != 0)
return; return;
/* intialize the python extension module */ /* intialize the python extension module */
Py_InitModule((char *) "libxml2mod", libxmlMethods); Py_InitModule((char *) "libxml2mod", libxmlMethods);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment