1
0
mirror of https://gitlab.gnome.org/GNOME/libxslt synced 2025-07-03 00:42:24 +03:00

added libxslt_xsltCleanup() added parameters to

* python/libxsl.py python/libxslt-python-api.xml python/libxslt.c
  python/libxsltclass.txt: added libxslt_xsltCleanup() added parameters
  to libxslt_xsltApplyStylesheet() removed the memleaks left and
  fixed an import order.
* python/tests/basic.py python/tests/extfunc.py: updated the tests
Daniel
This commit is contained in:
Daniel Veillard
2002-02-06 23:03:38 +00:00
parent 09af30e51e
commit ff22c1a5f9
10 changed files with 643 additions and 563 deletions

View File

@ -1,3 +1,4 @@
import _libxml
import _libxslt
from libxml2 import *

View File

@ -19,6 +19,10 @@
<arg name='URI' type='xmlChar *' info='the namespace or NULL'/>
<arg name='f' type='pythonObject' info='the python function'/>
</function>
<function name='xsltCleanup' file='python'>
<info>Cleanup all libxslt and libxml2 memory allocated</info>
<return type='void'/>
</function>
<!--
<function name='xsltRegisterXPathFunction' file='python'>
<info>Register a Python written function to the XPath interpreter</info>

View File

@ -227,6 +227,7 @@ libxslt_xsltRegisterExtModuleFunction(PyObject *self, PyObject *args) {
py_retval = libxml_intWrap(-1);
return(py_retval);
}
Py_XINCREF(pyobj_f);
ret = xsltRegisterExtModuleFunction(name, ns_uri,
libxslt_xmlXPathFuncCallback);
@ -234,6 +235,17 @@ libxslt_xsltRegisterExtModuleFunction(PyObject *self, PyObject *args) {
return(py_retval);
}
void
deallocateCallback(void *payload, xmlChar *name) {
PyObject *function = (PyObject *) payload;
#ifdef DEBUG_XPATH
printf("deallocateCallback(%s) called\n", name);
#endif
Py_XDECREF(function);
}
/************************************************************************
* *
* Some customized front-ends *
@ -250,14 +262,49 @@ libxslt_xsltApplyStylesheet(PyObject *self, PyObject *args) {
PyObject *pyobj_doc;
PyObject *pyobj_params;
const char **params;
int len = 0, i = 0, j;
PyObject *name;
PyObject *value;
if (!PyArg_ParseTuple(args, "OOO:xsltApplyStylesheet", &pyobj_style, &pyobj_doc, &pyobj_params))
return(NULL);
if (pyobj_params != Py_None) {
printf("libxslt_xsltApplyStylesheet: parameters not yet supported\n");
Py_INCREF(Py_None);
return(Py_None);
if (PyDict_Check(pyobj_params)) {
len = PyDict_Size(pyobj_params);
if (len > 0) {
params = (const char **) xmlMalloc((len + 1) * 2 *
sizeof(char *));
if (params == NULL) {
printf("libxslt_xsltApplyStylesheet: out of memory\n");
Py_INCREF(Py_None);
return(Py_None);
}
j = 0;
while (PyDict_Next(pyobj_params, &i, &name, &value)) {
const char *tmp;
int size;
tmp = PyString_AS_STRING(name);
size = PyString_GET_SIZE(name);
params[j * 2] = xmlCharStrndup(tmp, size);
if (PyString_Check(value)) {
tmp = PyString_AS_STRING(value);
size = PyString_GET_SIZE(value);
params[(j * 2) + 1] = xmlCharStrndup(tmp, size);
} else {
params[(j * 2) + 1] = NULL;
}
j = j + 1;
}
params[j * 2] = NULL;
params[(j * 2) + 1] = NULL;
}
} else {
printf("libxslt_xsltApplyStylesheet: parameters not a dict\n");
Py_INCREF(Py_None);
return(Py_None);
}
} else {
params = NULL;
}
@ -266,9 +313,34 @@ libxslt_xsltApplyStylesheet(PyObject *self, PyObject *args) {
c_retval = xsltApplyStylesheet(style, doc, params);
py_retval = libxml_xmlDocPtrWrap((xmlDocPtr) c_retval);
if (len > 0) {
for (i = 0;i < 2 * len;i++) {
if (params[i] != NULL)
xmlFree((char *)params[i]);
}
xmlFree(params);
}
return(py_retval);
}
/************************************************************************
* *
* Integrated cleanup *
* *
************************************************************************/
PyObject *
libxslt_xsltCleanup(PyObject *self, PyObject *args) {
if (libxslt_extModuleFunctions != NULL) {
xmlHashFree(libxslt_extModuleFunctions, deallocateCallback);
}
xsltCleanupGlobals();
xmlCleanupParser();
Py_INCREF(Py_None);
return(Py_None);
}
/************************************************************************
* *
* The registration stuff *

View File

@ -19,6 +19,7 @@ registerAllExtras()
registerAllFunctions()
# functions from module python
cleanup()
registerExtModuleFunction()
# functions from module transform

View File

@ -16,8 +16,7 @@ doc.freeDoc()
result.freeDoc()
# Memory debug specific
libxslt.cleanupGlobals()
libxml2.cleanupParser()
libxslt.cleanup()
if libxml2.debugMemory(1) == 0:
print "OK"
else:

View File

@ -18,14 +18,15 @@ styledoc = libxml2.parseDoc("""
xmlns:foo='http://example.com/foo'
xsl:exclude-result-prefixes='foo'>
<xsl:param name='bar'>failure</xsl:param>
<xsl:template match='/'>
<article><xsl:value-of select='foo:foo(\"foo\")'/></article>
<article><xsl:value-of select='foo:foo($bar)'/></article>
</xsl:template>
</xsl:stylesheet>
""")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseDoc("<doc/>")
result = style.applyStylesheet(doc, None)
result = style.applyStylesheet(doc, { "bar": "'success'" })
style = None
doc.freeDoc()
@ -33,15 +34,14 @@ root = result.children
if root.name != "article":
print "Unexpected root node name"
sys.exit(1)
if root.content != "FOO":
if root.content != "SUCCESS":
print "Unexpected root node content, extension function failed"
sys.exit(1)
result.freeDoc()
# Memory debug specific
libxslt.cleanupGlobals()
libxml2.cleanupParser()
libxslt.cleanup()
if libxml2.debugMemory(1) == 0:
print "OK"
else: