2 * libxslt.c: this modules implements the main part of the glue of the
3 * libxslt library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is either
5 * unpractical or would not match cleanly the Python model.
7 * If compiled with MERGED_MODULES, the entry point will be used to
8 * initialize both the libxml2 and the libxslt wrappers
10 * See Copyright for the status of this software.
15 /* #include "config.h" */
16 #include <libxml/xmlmemory.h>
17 #include <libxml/tree.h>
18 #include <libxml/xpath.h>
19 #include "libexslt/exslt.h"
20 #include "libxslt_wrap.h"
21 #include "libxslt-py.h"
23 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
24 #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
25 #elif defined(XSLT_NEED_TRIO)
27 #define vsnprintf trio_vsnprintf
31 /* #define DEBUG_XPATH */
32 /* #define DEBUG_ERROR */
33 /* #define DEBUG_MEMORY */
34 /* #define DEBUG_EXTENSIONS */
35 /* #define DEBUG_EXTENSIONS */
37 void initlibxsltmod(void);
39 /************************************************************************
41 * Per type specific glue *
43 ************************************************************************/
46 libxslt_xsltStylesheetPtrWrap(xsltStylesheetPtr style) {
50 printf("libxslt_xsltStylesheetPtrWrap: style = %p\n", style);
56 ret = PyCObject_FromVoidPtrAndDesc((void *) style,
57 (char *)"xsltStylesheetPtr", NULL);
62 libxslt_xsltTransformContextPtrWrap(xsltTransformContextPtr ctxt) {
66 printf("libxslt_xsltTransformContextPtrWrap: ctxt = %p\n", ctxt);
72 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
73 (char *)"xsltTransformContextPtr", NULL);
78 libxslt_xsltElemPreCompPtrWrap(xsltElemPreCompPtr ctxt) {
82 printf("libxslt_xsltElemPreCompPtrWrap: ctxt = %p\n", ctxt);
88 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
89 (char *)"xsltElemPreCompPtr", NULL);
93 /************************************************************************
97 ************************************************************************/
99 static xmlHashTablePtr libxslt_extModuleFunctions = NULL;
100 static xmlHashTablePtr libxslt_extModuleElements = NULL;
101 static xmlHashTablePtr libxslt_extModuleElementPreComp = NULL;
104 deallocateCallback(void *payload, xmlChar *name ATTRIBUTE_UNUSED) {
105 PyObject *function = (PyObject *) payload;
107 #ifdef DEBUG_EXTENSIONS
108 printf("deallocateCallback(%s) called\n", name);
111 Py_XDECREF(function);
115 deallocateClasse(void *payload, xmlChar *name ATTRIBUTE_UNUSED) {
116 PyObject *class = (PyObject *) payload;
118 #ifdef DEBUG_EXTENSIONS
119 printf("deallocateClasse(%s) called\n", name);
127 * libxslt_xsltElementPreCompCallback
128 * @style: the stylesheet
129 * @inst: the instruction in the stylesheet
131 * Callback for preprocessing of a custom element
133 static xsltElemPreCompPtr
134 libxslt_xsltElementPreCompCallback(xsltStylesheetPtr style, xmlNodePtr inst,
135 xsltTransformFunction function) {
136 xsltElemPreCompPtr ret;
140 PyObject *pyobj_element_f;
141 PyObject *pyobj_precomp_f;
143 const xmlChar *ns_uri;
146 #ifdef DEBUG_EXTENSIONS
147 printf("libxslt_xsltElementPreCompCallback called\n");
151 xsltTransformError(NULL, NULL, inst,
152 "libxslt_xsltElementPreCompCallback: no transformation context\n");
157 xsltTransformError(NULL, style, inst,
158 "libxslt_xsltElementPreCompCallback: no instruction\n");
159 if (style != NULL) style->errors++;
166 if (inst != NULL && inst->ns != NULL) {
168 ns_uri = inst->ns->href;
170 xsltTransformError(NULL, style, inst,
171 "libxslt_xsltElementPreCompCallback: internal error bad parameter\n");
172 printf("libxslt_xsltElementPreCompCallback: internal error bad parameter\n");
173 if (style != NULL) style->errors++;
178 * Find the functions, they should be there it was there at lookup
180 pyobj_precomp_f = xmlHashLookup2(libxslt_extModuleElementPreComp,
182 if (pyobj_precomp_f == NULL) {
183 xsltTransformError(NULL, style, inst,
184 "libxslt_xsltElementPreCompCallback: internal error, could not find precompile python function!\n");
185 if (style != NULL) style->errors++;
189 pyobj_element_f = xmlHashLookup2(libxslt_extModuleElements,
191 if (pyobj_element_f == NULL) {
192 xsltTransformError(NULL, style, inst,
193 "libxslt_xsltElementPreCompCallback: internal error, could not find element python function!\n");
194 if (style != NULL) style->errors++;
198 args = Py_BuildValue("(OOO)",
199 libxslt_xsltStylesheetPtrWrap(style),
200 libxml_xmlNodePtrWrap(inst),
203 Py_INCREF(pyobj_precomp_f); /* Protect refcount against reentrant manipulation of callback hash */
204 result = PyEval_CallObject(pyobj_precomp_f, args);
205 Py_DECREF(pyobj_precomp_f);
208 /* FIXME allow callbacks to return meaningful information to modify compile process */
209 /* If error, do we need to check the result and throw exception? */
213 ret = xsltNewElemPreComp (style, inst, function);
219 libxslt_xsltElementTransformCallback(xsltTransformContextPtr ctxt,
222 xsltElemPreCompPtr comp)
224 PyObject *args, *result;
225 PyObject *func = NULL;
227 const xmlChar *ns_uri;
232 if (inst != NULL && inst->name != NULL && inst->ns != NULL && inst->ns->href != NULL) {
234 ns_uri = inst->ns->href;
236 printf("libxslt_xsltElementTransformCallback: internal error bad parameter\n");
240 #ifdef DEBUG_EXTENSIONS
241 printf("libxslt_xsltElementTransformCallback called name %s URI %s\n", name, ns_uri);
245 * Find the function, it should be there it was there at lookup
247 func = xmlHashLookup2(libxslt_extModuleElements,
250 printf("libxslt_xsltElementTransformCallback: internal error %s not found !\n",
255 args = Py_BuildValue("OOOO",
256 libxslt_xsltTransformContextPtrWrap(ctxt),
257 libxml_xmlNodePtrWrap(node),
258 libxml_xmlNodePtrWrap(inst),
259 libxslt_xsltElemPreCompPtrWrap(comp));
261 Py_INCREF(func); /* Protect refcount against reentrant manipulation of callback hash */
262 result = PyEval_CallObject(func, args);
266 /* FIXME Check result of callobject and set exception if fail */
272 libxslt_xsltRegisterExtModuleElement(PyObject *self ATTRIBUTE_UNUSED,
278 PyObject *pyobj_element_f;
279 PyObject *pyobj_precomp_f;
281 #ifdef DEBUG_EXTENSIONS
282 printf("libxslt_xsltRegisterExtModuleElement called\n",
286 if (!PyArg_ParseTuple(args, (char *)"szOO:registerExtModuleElement",
287 &name, &ns_uri, &pyobj_precomp_f, &pyobj_element_f))
290 if ((name == NULL) || (pyobj_element_f == NULL) || (pyobj_precomp_f == NULL)) {
291 py_retval = libxml_intWrap(-1);
295 #ifdef DEBUG_EXTENSIONS
296 printf("libxslt_xsltRegisterExtModuleElement(%s, %s) called\n",
300 if (libxslt_extModuleElements == NULL)
301 libxslt_extModuleElements = xmlHashCreate(10);
303 if (libxslt_extModuleElementPreComp == NULL)
304 libxslt_extModuleElementPreComp = xmlHashCreate(10);
306 if (libxslt_extModuleElements == NULL || libxslt_extModuleElementPreComp == NULL) {
307 py_retval = libxml_intWrap(-1);
311 ret = xmlHashAddEntry2(libxslt_extModuleElements, name, ns_uri, pyobj_element_f);
313 py_retval = libxml_intWrap(-1);
316 Py_XINCREF(pyobj_element_f);
318 ret = xmlHashAddEntry2(libxslt_extModuleElementPreComp, name, ns_uri, pyobj_precomp_f);
320 xmlHashRemoveEntry2(libxslt_extModuleElements, name, ns_uri, deallocateCallback);
321 py_retval = libxml_intWrap(-1);
324 Py_XINCREF(pyobj_precomp_f);
326 ret = xsltRegisterExtModuleElement(name, ns_uri,
327 libxslt_xsltElementPreCompCallback,
328 libxslt_xsltElementTransformCallback);
329 py_retval = libxml_intWrap((int) ret);
333 libxslt_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) {
334 PyObject *list, *cur, *result;
335 xmlXPathObjectPtr obj;
336 xmlXPathContextPtr rctxt;
337 PyObject *current_function = NULL;
339 const xmlChar *ns_uri;
344 rctxt = ctxt->context;
347 name = rctxt->function;
348 ns_uri = rctxt->functionURI;
350 printf("libxslt_xmlXPathFuncCallback called name %s URI %s\n", name, ns_uri);
354 * Find the function, it should be there it was there at lookup
356 current_function = xmlHashLookup2(libxslt_extModuleFunctions,
358 if (current_function == NULL) {
359 printf("libxslt_xmlXPathFuncCallback: internal error %s not found !\n",
364 list = PyTuple_New(nargs + 1);
365 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
366 for (i = nargs - 1;i >= 0;i--) {
367 obj = valuePop(ctxt);
368 cur = libxml_xmlXPathObjectPtrWrap(obj);
369 PyTuple_SetItem(list, i + 1, cur);
372 Py_INCREF(current_function);
373 result = PyEval_CallObject(current_function, list);
374 Py_DECREF(current_function);
377 /* Check for null in case of exception */
378 if (result != NULL) {
379 obj = libxml_xmlXPathObjectPtrConvert(result);
380 valuePush(ctxt, obj);
385 libxslt_xsltRegisterExtModuleFunction(PyObject *self ATTRIBUTE_UNUSED,
393 if (!PyArg_ParseTuple(args, (char *)"szO:registerExtModuleFunction",
394 &name, &ns_uri, &pyobj_f))
397 if ((name == NULL) || (pyobj_f == NULL)) {
398 py_retval = libxml_intWrap(-1);
403 printf("libxslt_xsltRegisterExtModuleFunction(%s, %s) called\n",
407 if (libxslt_extModuleFunctions == NULL)
408 libxslt_extModuleFunctions = xmlHashCreate(10);
409 if (libxslt_extModuleFunctions == NULL) {
410 py_retval = libxml_intWrap(-1);
413 ret = xmlHashAddEntry2(libxslt_extModuleFunctions, name, ns_uri, pyobj_f);
415 py_retval = libxml_intWrap(-1);
420 ret = xsltRegisterExtModuleFunction(name, ns_uri,
421 libxslt_xmlXPathFuncCallback);
422 py_retval = libxml_intWrap((int) ret);
426 /************************************************************************
428 * Some customized front-ends *
430 ************************************************************************/
433 libxslt_xsltApplyStylesheet(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
436 xsltStylesheetPtr style;
437 PyObject *pyobj_style;
440 PyObject *pyobj_params;
441 const char **params = NULL;
442 int len = 0, i = 0, j;
446 if (!PyArg_ParseTuple(args, (char *) "OOO:xsltApplyStylesheet",
447 &pyobj_style, &pyobj_doc, &pyobj_params))
450 if (pyobj_params != Py_None) {
451 if (PyDict_Check(pyobj_params)) {
452 len = PyDict_Size(pyobj_params);
454 params = (const char **) xmlMalloc((len + 1) * 2 *
456 if (params == NULL) {
457 printf("libxslt_xsltApplyStylesheet: out of memory\n");
462 while (PyDict_Next(pyobj_params, &i, &name, &value)) {
466 tmp = PyString_AS_STRING(name);
467 size = PyString_GET_SIZE(name);
468 params[j * 2] = (char *) xmlCharStrndup(tmp, size);
469 if (PyString_Check(value)) {
470 tmp = PyString_AS_STRING(value);
471 size = PyString_GET_SIZE(value);
472 params[(j * 2) + 1] = (char *)
473 xmlCharStrndup(tmp, size);
475 params[(j * 2) + 1] = NULL;
479 params[j * 2] = NULL;
480 params[(j * 2) + 1] = NULL;
483 printf("libxslt_xsltApplyStylesheet: parameters not a dict\n");
488 style = (xsltStylesheetPtr) Pystylesheet_Get(pyobj_style);
489 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
491 c_retval = xsltApplyStylesheet(style, doc, params);
492 py_retval = libxml_xmlDocPtrWrap((xmlDocPtr) c_retval);
493 if (params != NULL) {
495 for (i = 0;i < 2 * len;i++) {
496 if (params[i] != NULL)
497 xmlFree((char *)params[i]);
506 libxslt_xsltSaveResultToString(PyObject *self, PyObject *args) {
507 PyObject *py_retval; /* our final return value, a python string */
512 PyObject *pyobj_result;
513 xsltStylesheetPtr style;
514 PyObject *pyobj_style;
516 if (!PyArg_ParseTuple(args, (char *)"OO:xsltSaveResultToString", &pyobj_style, &pyobj_result))
518 result = (xmlDocPtr) PyxmlNode_Get(pyobj_result);
519 style = (xsltStylesheetPtr) Pystylesheet_Get(pyobj_style);
522 /* FIXME: We should probably add more restrictive error checking
523 * and raise an error instead of "just" returning NULL.
524 * FIXME: Documentation and code for xsltSaveResultToString diff
525 * -> emmitted will never be positive non-null.
527 emitted = xsltSaveResultToString(&buffer, &size, result, style);
528 if(!buffer || emitted < 0)
530 /* We haven't tested the aberrant case of a transformation that
531 * renders to an empty string. For now we try to play it save.
536 py_retval = PyString_FromString((char *) buffer);
540 py_retval = PyString_FromString("");
547 /************************************************************************
549 * Error message callback *
551 ************************************************************************/
553 static PyObject *libxslt_xsltPythonErrorFuncHandler = NULL;
554 static PyObject *libxslt_xsltPythonErrorFuncCtxt = NULL;
557 libxslt_xsltErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
570 printf("libxslt_xsltErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
574 if (libxslt_xsltPythonErrorFuncHandler == NULL) {
576 vfprintf(stderr, msg, ap);
579 str = (char *) xmlMalloc(150);
587 chars = vsnprintf(str, size, msg, ap);
589 if ((chars > -1) && (chars < size))
595 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
602 list = PyTuple_New(2);
603 PyTuple_SetItem(list, 0, libxslt_xsltPythonErrorFuncCtxt);
604 Py_XINCREF(libxslt_xsltPythonErrorFuncCtxt);
605 message = libxml_charPtrWrap(str);
606 PyTuple_SetItem(list, 1, message);
607 result = PyEval_CallObject(libxslt_xsltPythonErrorFuncHandler, list);
614 libxslt_xsltErrorInitialize(void)
617 printf("libxslt_xsltErrorInitialize() called\n");
619 xmlSetGenericErrorFunc(NULL, libxslt_xsltErrorFuncHandler);
620 xsltSetGenericErrorFunc(NULL, libxslt_xsltErrorFuncHandler);
624 libxslt_xsltRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
631 if (!PyArg_ParseTuple
632 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
637 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx,
641 if (libxslt_xsltPythonErrorFuncHandler != NULL) {
642 Py_XDECREF(libxslt_xsltPythonErrorFuncHandler);
644 if (libxslt_xsltPythonErrorFuncCtxt != NULL) {
645 Py_XDECREF(libxslt_xsltPythonErrorFuncCtxt);
648 Py_XINCREF(pyobj_ctx);
651 /* TODO: check f is a function ! */
652 libxslt_xsltPythonErrorFuncHandler = pyobj_f;
653 libxslt_xsltPythonErrorFuncCtxt = pyobj_ctx;
655 py_retval = libxml_intWrap(1);
659 /************************************************************************
661 * Extension classes *
663 ************************************************************************/
665 static xmlHashTablePtr libxslt_extModuleClasses = NULL;
668 libxslt_xsltPythonExtModuleStyleInit(xsltStylesheetPtr style,
669 const xmlChar * URI) {
670 PyObject *result = NULL;
671 PyObject *class = NULL;
673 #ifdef DEBUG_EXTENSIONS
674 printf("libxslt_xsltPythonExtModuleStyleInit(%p, %s) called\n",
678 if ((style == NULL) || (URI == NULL))
682 * Find the function, it should be there it was there at lookup
684 class = xmlHashLookup(libxslt_extModuleClasses, URI);
686 fprintf(stderr, "libxslt_xsltPythonExtModuleStyleInit: internal error %s not found !\n", URI);
690 if (PyObject_HasAttrString(class, (char *) "_styleInit")) {
691 result = PyObject_CallMethod(class, (char *) "_styleInit",
692 (char *) "Os", libxslt_xsltStylesheetPtrWrap(style), URI);
694 return((void *)result);
697 libxslt_xsltPythonExtModuleStyleShutdown(xsltStylesheetPtr style,
698 const xmlChar * URI, void *data) {
699 PyObject *class = NULL;
702 #ifdef DEBUG_EXTENSIONS
703 printf("libxslt_xsltPythonExtModuleStyleShutdown(%p, %s, %p) called\n",
707 if ((style == NULL) || (URI == NULL))
711 * Find the function, it should be there it was there at lookup
713 class = xmlHashLookup(libxslt_extModuleClasses, URI);
715 fprintf(stderr, "libxslt_xsltPythonExtModuleStyleShutdown: internal error %s not found !\n", URI);
719 if (PyObject_HasAttrString(class, (char *) "_styleShutdown")) {
720 result = PyObject_CallMethod(class, (char *) "_styleShutdown",
721 (char *) "OsO", libxslt_xsltStylesheetPtrWrap(style),
722 URI, (PyObject *) data);
724 Py_XDECREF((PyObject *)data);
729 libxslt_xsltPythonExtModuleCtxtInit(xsltTransformContextPtr ctxt,
730 const xmlChar * URI) {
731 PyObject *result = NULL;
732 PyObject *class = NULL;
734 #ifdef DEBUG_EXTENSIONS
735 printf("libxslt_xsltPythonExtModuleCtxtInit(%p, %s) called\n",
739 if ((ctxt == NULL) || (URI == NULL))
743 * Find the function, it should be there it was there at lookup
745 class = xmlHashLookup(libxslt_extModuleClasses, URI);
747 fprintf(stderr, "libxslt_xsltPythonExtModuleCtxtInit: internal error %s not found !\n", URI);
751 if (PyObject_HasAttrString(class, (char *) "_ctxtInit")) {
752 result = PyObject_CallMethod(class, (char *) "_ctxtInit",
753 (char *) "Os", libxslt_xsltTransformContextPtrWrap(ctxt),
756 return((void *)result);
759 libxslt_xsltPythonExtModuleCtxtShutdown(xsltTransformContextPtr ctxt,
760 const xmlChar * URI, void *data) {
761 PyObject *class = NULL;
764 #ifdef DEBUG_EXTENSIONS
765 printf("libxslt_xsltPythonExtModuleCtxtShutdown(%p, %s, %p) called\n",
769 if ((ctxt == NULL) || (URI == NULL))
773 * Find the function, it should be there it was there at lookup
775 class = xmlHashLookup(libxslt_extModuleClasses, URI);
777 fprintf(stderr, "libxslt_xsltPythonExtModuleCtxtShutdown: internal error %s not found !\n", URI);
781 if (PyObject_HasAttrString(class, (char *) "_ctxtShutdown")) {
782 result = PyObject_CallMethod(class, (char *) "_ctxtShutdown",
783 (char *) "OsO", libxslt_xsltTransformContextPtrWrap(ctxt),
784 URI, (PyObject *) data);
786 Py_XDECREF((PyObject *)data);
791 libxslt_xsltRegisterExtensionClass(PyObject *self ATTRIBUTE_UNUSED,
798 if (!PyArg_ParseTuple(args, (char *)"zO:registerExtensionClass",
802 if ((ns_uri == NULL) || (pyobj_c == NULL)) {
803 py_retval = libxml_intWrap(-1);
807 #ifdef DEBUG_EXTENSIONS
808 printf("libxslt_xsltRegisterExtensionClass(%s) called\n", ns_uri);
811 if (libxslt_extModuleClasses == NULL)
812 libxslt_extModuleClasses = xmlHashCreate(10);
813 if (libxslt_extModuleClasses == NULL) {
814 py_retval = libxml_intWrap(-1);
817 ret = xmlHashAddEntry(libxslt_extModuleClasses, ns_uri, pyobj_c);
819 py_retval = libxml_intWrap(-1);
824 ret = xsltRegisterExtModuleFull(ns_uri,
825 (xsltExtInitFunction) libxslt_xsltPythonExtModuleCtxtInit,
826 (xsltExtShutdownFunction) libxslt_xsltPythonExtModuleCtxtShutdown,
827 (xsltStyleExtInitFunction) libxslt_xsltPythonExtModuleStyleInit,
828 (xsltStyleExtShutdownFunction) libxslt_xsltPythonExtModuleStyleShutdown);
829 py_retval = libxml_intWrap((int) ret);
836 /************************************************************************
838 * Integrated cleanup *
840 ************************************************************************/
843 libxslt_xsltCleanup(PyObject *self ATTRIBUTE_UNUSED,
844 PyObject *args ATTRIBUTE_UNUSED) {
846 if (libxslt_extModuleFunctions != NULL) {
847 xmlHashFree(libxslt_extModuleFunctions, deallocateCallback);
849 if (libxslt_extModuleElements != NULL) {
850 xmlHashFree(libxslt_extModuleElements, deallocateCallback);
852 if (libxslt_extModuleElementPreComp != NULL) {
853 xmlHashFree(libxslt_extModuleElementPreComp, deallocateCallback);
855 if (libxslt_extModuleClasses != NULL) {
856 xmlHashFree(libxslt_extModuleClasses, deallocateClasse);
858 xsltCleanupGlobals();
864 /************************************************************************
866 * The registration stuff *
868 ************************************************************************/
869 static PyMethodDef libxsltMethods[] = {
870 #include "libxslt-export.c"
871 { NULL, NULL, 0, NULL }
874 #ifdef MERGED_MODULES
875 extern void initlibxml2mod(void);
878 void initlibxsltmod(void) {
879 static int initialized = 0;
882 #ifdef MERGED_MODULES
886 if (initialized != 0)
888 m = Py_InitModule((char *)"libxsltmod", libxsltMethods);
891 * Specific XSLT initializations
893 libxslt_xsltErrorInitialize();
895 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
896 xmlDefaultSAXHandler.cdataBlock = NULL;
898 * Register the EXSLT extensions and the test module