2 * libxml.c: this modules implements the main part of the glue of the
3 * libxml2 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 <fileobject.h>
16 /* #include "config.h" */
17 #include <libxml/xmlmemory.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <libxml/xpath.h>
21 #include <libxml/xmlerror.h>
22 #include <libxml/xpathInternals.h>
23 #include <libxml/xmlmemory.h>
24 #include <libxml/xmlIO.h>
25 #include <libxml/c14n.h>
26 #include <libxml/xmlreader.h>
27 #include <libxml/xmlsave.h>
28 #include "libxml_wrap.h"
29 #include "libxml2-py.h"
31 #if defined(WITH_TRIO)
33 #define vsnprintf trio_vsnprintf
37 /* #define DEBUG_SAX */
38 /* #define DEBUG_XPATH */
39 /* #define DEBUG_ERROR */
40 /* #define DEBUG_MEMORY */
41 /* #define DEBUG_FILES */
42 /* #define DEBUG_LOADER */
44 #if PY_MAJOR_VERSION >= 3
45 PyObject *PyInit_libxml2mod(void);
47 #define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
48 #define PY_IMPORT_STRING PyUnicode_FromString
50 void initlibxml2mod(void);
51 #define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize
52 #define PY_IMPORT_STRING PyString_FromString
59 * macro to flag unimplemented blocks
62 xmlGenericError(xmlGenericErrorContext, \
63 "Unimplemented block at %s:%d\n", \
66 * the following vars are used for XPath extensions, but
67 * are also referenced within the parser cleanup routine.
69 static int libxml_xpathCallbacksInitialized = 0;
71 typedef struct libxml_xpathCallback {
72 xmlXPathContextPtr ctx;
76 } libxml_xpathCallback, *libxml_xpathCallbackPtr;
77 typedef libxml_xpathCallback libxml_xpathCallbackArray[];
78 static int libxml_xpathCallbacksAllocd = 10;
79 static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
80 static int libxml_xpathCallbacksNb = 0;
82 /************************************************************************
84 * Memory debug interface *
86 ************************************************************************/
89 extern void xmlMemFree(void *ptr);
90 extern void *xmlMemMalloc(size_t size);
91 extern void *xmlMemRealloc(void *ptr, size_t size);
92 extern char *xmlMemoryStrdup(const char *str);
95 static int libxmlMemoryDebugActivated = 0;
96 static long libxmlMemoryAllocatedBase = 0;
98 static int libxmlMemoryDebug = 0;
99 static xmlFreeFunc freeFunc = NULL;
100 static xmlMallocFunc mallocFunc = NULL;
101 static xmlReallocFunc reallocFunc = NULL;
102 static xmlStrdupFunc strdupFunc = NULL;
105 libxml_xmlErrorInitialize(void); /* forward declare */
108 libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
109 PyObject * args ATTRIBUTE_UNUSED)
116 py_retval = libxml_longWrap(ret);
121 libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
127 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
131 printf("libxml_xmlDebugMemory(%d) called\n", activate);
135 if (libxmlMemoryDebug == 0) {
137 * First initialize the library and grab the old memory handlers
138 * and switch the library to memory debugging
140 xmlMemGet((xmlFreeFunc *) & freeFunc,
141 (xmlMallocFunc *) & mallocFunc,
142 (xmlReallocFunc *) & reallocFunc,
143 (xmlStrdupFunc *) & strdupFunc);
144 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
145 (reallocFunc == xmlMemRealloc) &&
146 (strdupFunc == xmlMemoryStrdup)) {
147 libxmlMemoryAllocatedBase = xmlMemUsed();
150 * cleanup first, because some memory has been
151 * allocated with the non-debug malloc in xmlInitParser
152 * when the python module was imported
155 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
156 xmlMemRealloc, xmlMemoryStrdup);
159 libxmlMemoryAllocatedBase = xmlMemUsed();
162 libxml_xmlErrorInitialize();
165 } else if (libxmlMemoryDebugActivated == 0) {
166 libxmlMemoryAllocatedBase = xmlMemUsed();
169 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
171 libxmlMemoryDebug = 1;
172 libxmlMemoryDebugActivated = 1;
174 if (libxmlMemoryDebugActivated == 1)
175 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
178 libxmlMemoryDebugActivated = 0;
181 py_retval = libxml_longWrap(ret);
186 libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
187 PyObject *args ATTRIBUTE_UNUSED) {
192 if (libxmlMemoryDebug) {
193 freed = xmlMemUsed();
198 * Need to confirm whether we really want to do this (required for
199 * memcheck) in all cases...
202 if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
203 for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
204 if ((*libxml_xpathCallbacks)[ix].name != NULL)
205 xmlFree((*libxml_xpathCallbacks)[ix].name);
206 if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
207 xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
209 libxml_xpathCallbacksNb = 0;
210 xmlFree(libxml_xpathCallbacks);
211 libxml_xpathCallbacks = NULL;
214 if (libxmlMemoryDebug) {
215 freed -= xmlMemUsed();
216 libxmlMemoryAllocatedBase -= freed;
217 if (libxmlMemoryAllocatedBase < 0)
218 libxmlMemoryAllocatedBase = 0;
226 libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
227 ATTRIBUTE_UNUSED PyObject * args)
230 if (libxmlMemoryDebug != 0)
236 /************************************************************************
238 * Handling Python FILE I/O at the C level *
239 * The raw I/O attack diectly the File objects, while the *
240 * other routines address the ioWrapper instance instead *
242 ************************************************************************/
245 * xmlPythonFileCloseUnref:
246 * @context: the I/O context
248 * Close an I/O channel
251 xmlPythonFileCloseRaw (void * context) {
252 PyObject *file, *ret;
255 printf("xmlPythonFileCloseUnref\n");
257 file = (PyObject *) context;
258 if (file == NULL) return(-1);
259 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
268 * xmlPythonFileReadRaw:
269 * @context: the I/O context
270 * @buffer: where to drop data
271 * @len: number of bytes to write
273 * Read @len bytes to @buffer from the Python file in the I/O channel
275 * Returns the number of bytes read
278 xmlPythonFileReadRaw (void * context, char * buffer, int len) {
285 printf("xmlPythonFileReadRaw: %d\n", len);
287 file = (PyObject *) context;
288 if (file == NULL) return(-1);
289 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
291 printf("xmlPythonFileReadRaw: result is NULL\n");
293 } else if (PyBytes_Check(ret)) {
294 lenread = PyBytes_Size(ret);
295 data = PyBytes_AsString(ret);
296 #ifdef PyUnicode_Check
297 } else if PyUnicode_Check (ret) {
298 #if PY_VERSION_HEX >= 0x03030000
302 /* tmp doesn't need to be deallocated */
303 tmp = PyUnicode_AsUTF8AndSize(ret, &size);
305 lenread = (int) size;
309 b = PyUnicode_AsUTF8String(ret);
311 printf("xmlPythonFileReadRaw: failed to convert to UTF-8\n");
314 lenread = PyBytes_Size(b);
315 data = PyBytes_AsString(b);
320 printf("xmlPythonFileReadRaw: result is not a String\n");
325 memcpy(buffer, data, len);
327 memcpy(buffer, data, lenread);
334 * @context: the I/O context
335 * @buffer: where to drop data
336 * @len: number of bytes to write
338 * Read @len bytes to @buffer from the I/O channel.
340 * Returns the number of bytes read
343 xmlPythonFileRead (void * context, char * buffer, int len) {
350 printf("xmlPythonFileRead: %d\n", len);
352 file = (PyObject *) context;
353 if (file == NULL) return(-1);
354 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
356 printf("xmlPythonFileRead: result is NULL\n");
358 } else if (PyBytes_Check(ret)) {
359 lenread = PyBytes_Size(ret);
360 data = PyBytes_AsString(ret);
361 #ifdef PyUnicode_Check
362 } else if PyUnicode_Check (ret) {
363 #if PY_VERSION_HEX >= 0x03030000
367 /* tmp doesn't need to be deallocated */
368 tmp = PyUnicode_AsUTF8AndSize(ret, &size);
370 lenread = (int) size;
374 b = PyUnicode_AsUTF8String(ret);
376 printf("xmlPythonFileRead: failed to convert to UTF-8\n");
379 lenread = PyBytes_Size(b);
380 data = PyBytes_AsString(b);
385 printf("xmlPythonFileRead: result is not a String\n");
390 memcpy(buffer, data, len);
392 memcpy(buffer, data, lenread);
399 * @context: the I/O context
400 * @buffer: where to drop data
401 * @len: number of bytes to write
403 * Write @len bytes from @buffer to the I/O channel.
405 * Returns the number of bytes written
408 xmlPythonFileWrite (void * context, const char * buffer, int len) {
411 PyObject *ret = NULL;
415 printf("xmlPythonFileWrite: %d\n", len);
417 file = (PyObject *) context;
418 if (file == NULL) return(-1);
419 string = PY_IMPORT_STRING_SIZE(buffer, len);
420 if (string == NULL) return(-1);
421 if (PyObject_HasAttrString(file, (char *) "io_write")) {
422 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
424 } else if (PyObject_HasAttrString(file, (char *) "write")) {
425 ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
430 printf("xmlPythonFileWrite: result is NULL\n");
432 } else if (PyLong_Check(ret)) {
433 written = (int) PyLong_AsLong(ret);
435 } else if (ret == Py_None) {
439 printf("xmlPythonFileWrite: result is not an Int nor None\n");
446 * xmlPythonFileClose:
447 * @context: the I/O context
449 * Close an I/O channel
452 xmlPythonFileClose (void * context) {
453 PyObject *file, *ret = NULL;
456 printf("xmlPythonFileClose\n");
458 file = (PyObject *) context;
459 if (file == NULL) return(-1);
460 if (PyObject_HasAttrString(file, (char *) "io_close")) {
461 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
462 } else if (PyObject_HasAttrString(file, (char *) "flush")) {
463 ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
471 #ifdef LIBXML_OUTPUT_ENABLED
473 * xmlOutputBufferCreatePythonFile:
474 * @file: a PyFile_Type
475 * @encoder: the encoding converter or NULL
477 * Create a buffered output for the progressive saving to a PyFile_Type
480 * Returns the new parser output or NULL
482 static xmlOutputBufferPtr
483 xmlOutputBufferCreatePythonFile(PyObject *file,
484 xmlCharEncodingHandlerPtr encoder) {
485 xmlOutputBufferPtr ret;
487 if (file == NULL) return(NULL);
489 ret = xmlAllocOutputBuffer(encoder);
492 /* Py_INCREF(file); */
493 ret->writecallback = xmlPythonFileWrite;
494 ret->closecallback = xmlPythonFileClose;
501 libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
505 xmlCharEncodingHandlerPtr handler = NULL;
506 xmlOutputBufferPtr buffer;
509 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
512 if ((encoding != NULL) && (encoding[0] != 0)) {
513 handler = xmlFindCharEncodingHandler((const char *) encoding);
515 buffer = xmlOutputBufferCreatePythonFile(file, handler);
517 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
518 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
523 * libxml_outputBufferGetPythonFile:
524 * @buffer: the I/O buffer
526 * read the Python I/O from the CObject
528 * Returns the new parser output or NULL
531 libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
535 xmlOutputBufferPtr obj;
537 if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
541 obj = PyoutputBuffer_Get(buffer);
544 "outputBufferGetPythonFile: obj == NULL\n");
548 if (obj->closecallback != xmlPythonFileClose) {
550 "outputBufferGetPythonFile: not a python file wrapper\n");
554 file = (PyObject *) obj->context;
564 libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
567 xmlOutputBufferPtr out;
570 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
572 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
573 /* Buffer may already have been destroyed elsewhere. This is harmless. */
579 c_retval = xmlOutputBufferClose(out);
580 py_retval = libxml_intWrap((int) c_retval);
585 libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
588 xmlOutputBufferPtr out;
591 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
593 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
595 c_retval = xmlOutputBufferFlush(out);
596 py_retval = libxml_intWrap((int) c_retval);
601 libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
604 xmlOutputBufferPtr buf;
610 if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
612 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
613 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
615 c_retval = xmlSaveFileTo(buf, cur, encoding);
616 /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
618 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
619 py_retval = libxml_intWrap((int) c_retval);
624 libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
627 xmlOutputBufferPtr buf;
634 if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
636 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
637 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
639 c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
640 /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
641 * in the Python object */
642 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
643 py_retval = libxml_intWrap((int) c_retval);
646 #endif /* LIBXML_OUTPUT_ENABLED */
650 * xmlParserInputBufferCreatePythonFile:
651 * @file: a PyFile_Type
652 * @encoder: the encoding converter or NULL
654 * Create a buffered output for the progressive saving to a PyFile_Type
657 * Returns the new parser output or NULL
659 static xmlParserInputBufferPtr
660 xmlParserInputBufferCreatePythonFile(PyObject *file,
661 xmlCharEncoding encoding) {
662 xmlParserInputBufferPtr ret;
664 if (file == NULL) return(NULL);
666 ret = xmlAllocParserInputBuffer(encoding);
669 /* Py_INCREF(file); */
670 ret->readcallback = xmlPythonFileRead;
671 ret->closecallback = xmlPythonFileClose;
678 libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
682 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
683 xmlParserInputBufferPtr buffer;
686 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
689 if ((encoding != NULL) && (encoding[0] != 0)) {
690 enc = xmlParseCharEncoding((const char *) encoding);
692 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
694 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
695 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
699 /************************************************************************
701 * Providing the resolver at the Python level *
703 ************************************************************************/
705 static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
706 static PyObject *pythonExternalEntityLoaderObjext;
708 static xmlParserInputPtr
709 pythonExternalEntityLoader(const char *URL, const char *ID,
710 xmlParserCtxtPtr ctxt) {
711 xmlParserInputPtr result = NULL;
712 if (pythonExternalEntityLoaderObjext != NULL) {
716 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
718 printf("pythonExternalEntityLoader: ready to call\n");
721 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
722 (char *) "(ssO)", URL, ID, ctxtobj);
725 printf("pythonExternalEntityLoader: result ");
726 PyObject_Print(ret, stdout, 0);
731 if (PyObject_HasAttrString(ret, (char *) "read")) {
732 xmlParserInputBufferPtr buf;
734 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
737 buf->readcallback = xmlPythonFileReadRaw;
738 buf->closecallback = xmlPythonFileCloseRaw;
739 result = xmlNewIOInputStream(ctxt, buf,
740 XML_CHAR_ENCODING_NONE);
745 printf("pythonExternalEntityLoader: can't read %s\n",
749 if (result == NULL) {
751 } else if (URL != NULL) {
752 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
753 result->directory = xmlParserGetDirectory((const char *) URL);
757 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
758 result = defaultExternalEntityLoader(URL, ID, ctxt);
764 libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
768 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
772 if (!PyCallable_Check(loader)) {
773 PyErr_SetString(PyExc_ValueError, "entity loader is not callable");
778 printf("libxml_xmlSetEntityLoader\n");
780 if (defaultExternalEntityLoader == NULL)
781 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
783 Py_XDECREF(pythonExternalEntityLoaderObjext);
784 pythonExternalEntityLoaderObjext = loader;
785 Py_XINCREF(pythonExternalEntityLoaderObjext);
786 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
788 py_retval = PyLong_FromLong(0);
792 /************************************************************************
794 * Input callback registration *
796 ************************************************************************/
797 static PyObject *pythonInputOpenCallbackObject;
798 static int pythonInputCallbackID = -1;
801 pythonInputMatchCallback(ATTRIBUTE_UNUSED const char *URI)
803 /* Always return success, real decision whether URI is supported will be
804 * made in open callback. */
809 pythonInputOpenCallback(const char *URI)
813 ret = PyObject_CallFunction(pythonInputOpenCallbackObject,
815 if (ret == Py_None) {
823 libxml_xmlRegisterInputCallback(ATTRIBUTE_UNUSED PyObject *self,
827 if (!PyArg_ParseTuple(args,
828 (const char *)"O:libxml_xmlRegisterInputCallback", &cb))
831 if (!PyCallable_Check(cb)) {
832 PyErr_SetString(PyExc_ValueError, "input callback is not callable");
836 /* Python module registers a single callback and manages the list of
837 * all callbacks internally. This is necessitated by xmlInputMatchCallback
838 * API, which does not allow for passing of data objects to discriminate
839 * different Python methods. */
840 if (pythonInputCallbackID == -1) {
841 pythonInputCallbackID = xmlRegisterInputCallbacks(
842 pythonInputMatchCallback, pythonInputOpenCallback,
843 xmlPythonFileReadRaw, xmlPythonFileCloseRaw);
844 if (pythonInputCallbackID == -1)
845 return PyErr_NoMemory();
846 pythonInputOpenCallbackObject = cb;
847 Py_INCREF(pythonInputOpenCallbackObject);
855 libxml_xmlUnregisterInputCallback(ATTRIBUTE_UNUSED PyObject *self,
856 ATTRIBUTE_UNUSED PyObject *args) {
859 ret = xmlPopInputCallbacks();
860 if (pythonInputCallbackID != -1) {
861 /* Assert that the right input callback was popped. libxml's API does not
862 * allow removal by ID, so all that could be done is an assert. */
863 if (pythonInputCallbackID == ret) {
864 pythonInputCallbackID = -1;
865 Py_DECREF(pythonInputOpenCallbackObject);
866 pythonInputOpenCallbackObject = NULL;
868 PyErr_SetString(PyExc_AssertionError, "popped non-python input callback");
871 } else if (ret == -1) {
872 /* No more callbacks to pop */
873 PyErr_SetString(PyExc_IndexError, "no input callbacks to pop");
881 /************************************************************************
883 * Handling SAX/xmllib/sgmlop callback interfaces *
885 ************************************************************************/
888 pythonStartElement(void *user_data, const xmlChar * name,
889 const xmlChar ** attrs)
896 PyObject *result = NULL;
900 printf("pythonStartElement(%s) called\n", name);
902 handler = (PyObject *) user_data;
903 if (PyObject_HasAttrString(handler, (char *) "startElement"))
905 else if (PyObject_HasAttrString(handler, (char *) "start"))
909 * the xmllib interface always generates a dictionary,
912 if ((attrs == NULL) && (type == 1)) {
915 } else if (attrs == NULL) {
919 for (i = 0; attrs[i] != NULL; i++) {
920 attrname = PY_IMPORT_STRING((char *) attrs[i]);
922 if (attrs[i] != NULL) {
923 attrvalue = PY_IMPORT_STRING((char *) attrs[i]);
928 PyDict_SetItem(dict, attrname, attrvalue);
930 Py_DECREF(attrvalue);
935 result = PyObject_CallMethod(handler, (char *) "startElement",
936 (char *) "sO", name, dict);
938 result = PyObject_CallMethod(handler, (char *) "start",
939 (char *) "sO", name, dict);
940 if (PyErr_Occurred())
948 pythonStartDocument(void *user_data)
954 printf("pythonStartDocument() called\n");
956 handler = (PyObject *) user_data;
957 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
959 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
960 if (PyErr_Occurred())
967 pythonEndDocument(void *user_data)
973 printf("pythonEndDocument() called\n");
975 handler = (PyObject *) user_data;
976 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
978 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
979 if (PyErr_Occurred())
984 * The reference to the handler is released there
990 pythonEndElement(void *user_data, const xmlChar * name)
996 printf("pythonEndElement(%s) called\n", name);
998 handler = (PyObject *) user_data;
999 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
1000 result = PyObject_CallMethod(handler, (char *) "endElement",
1001 (char *) "s", name);
1002 if (PyErr_Occurred())
1005 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
1006 result = PyObject_CallMethod(handler, (char *) "end",
1007 (char *) "s", name);
1008 if (PyErr_Occurred())
1015 pythonReference(void *user_data, const xmlChar * name)
1021 printf("pythonReference(%s) called\n", name);
1023 handler = (PyObject *) user_data;
1024 if (PyObject_HasAttrString(handler, (char *) "reference")) {
1025 result = PyObject_CallMethod(handler, (char *) "reference",
1026 (char *) "s", name);
1027 if (PyErr_Occurred())
1034 pythonCharacters(void *user_data, const xmlChar * ch, int len)
1037 PyObject *result = NULL;
1041 printf("pythonCharacters(%s, %d) called\n", ch, len);
1043 handler = (PyObject *) user_data;
1044 if (PyObject_HasAttrString(handler, (char *) "characters"))
1046 else if (PyObject_HasAttrString(handler, (char *) "data"))
1050 result = PyObject_CallMethod(handler, (char *) "characters",
1051 (char *) "s#", ch, len);
1053 result = PyObject_CallMethod(handler, (char *) "data",
1054 (char *) "s#", ch, len);
1055 if (PyErr_Occurred())
1062 pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
1065 PyObject *result = NULL;
1069 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
1071 handler = (PyObject *) user_data;
1072 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
1074 else if (PyObject_HasAttrString(handler, (char *) "data"))
1079 PyObject_CallMethod(handler,
1080 (char *) "ignorableWhitespace",
1081 (char *) "s#", ch, len);
1084 PyObject_CallMethod(handler, (char *) "data",
1085 (char *) "s#", ch, len);
1091 pythonProcessingInstruction(void *user_data,
1092 const xmlChar * target, const xmlChar * data)
1098 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
1100 handler = (PyObject *) user_data;
1101 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
1102 result = PyObject_CallMethod(handler, (char *)
1103 "processingInstruction",
1104 (char *) "ss", target, data);
1110 pythonComment(void *user_data, const xmlChar * value)
1116 printf("pythonComment(%s) called\n", value);
1118 handler = (PyObject *) user_data;
1119 if (PyObject_HasAttrString(handler, (char *) "comment")) {
1121 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
1123 if (PyErr_Occurred())
1130 pythonWarning(void *user_data, const char *msg, ...)
1138 printf("pythonWarning(%s) called\n", msg);
1140 handler = (PyObject *) user_data;
1141 if (PyObject_HasAttrString(handler, (char *) "warning")) {
1142 va_start(args, msg);
1143 vsnprintf(buf, 1023, msg, args);
1147 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
1149 if (PyErr_Occurred())
1156 pythonError(void *user_data, const char *msg, ...)
1164 printf("pythonError(%s) called\n", msg);
1166 handler = (PyObject *) user_data;
1167 if (PyObject_HasAttrString(handler, (char *) "error")) {
1168 va_start(args, msg);
1169 vsnprintf(buf, 1023, msg, args);
1173 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1175 if (PyErr_Occurred())
1182 pythonFatalError(void *user_data, const char *msg, ...)
1190 printf("pythonFatalError(%s) called\n", msg);
1192 handler = (PyObject *) user_data;
1193 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
1194 va_start(args, msg);
1195 vsnprintf(buf, 1023, msg, args);
1199 PyObject_CallMethod(handler, (char *) "fatalError",
1201 if (PyErr_Occurred())
1208 pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1211 PyObject *result = NULL;
1215 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1217 handler = (PyObject *) user_data;
1218 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1220 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1225 PyObject_CallMethod(handler, (char *) "cdataBlock",
1226 (char *) "s#", ch, len);
1229 PyObject_CallMethod(handler, (char *) "cdata",
1230 (char *) "s#", ch, len);
1231 if (PyErr_Occurred())
1238 pythonExternalSubset(void *user_data,
1239 const xmlChar * name,
1240 const xmlChar * externalID, const xmlChar * systemID)
1246 printf("pythonExternalSubset(%s, %s, %s) called\n",
1247 name, externalID, systemID);
1249 handler = (PyObject *) user_data;
1250 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
1252 PyObject_CallMethod(handler, (char *) "externalSubset",
1253 (char *) "sss", name, externalID,
1260 pythonEntityDecl(void *user_data,
1261 const xmlChar * name,
1263 const xmlChar * publicId,
1264 const xmlChar * systemId, xmlChar * content)
1269 handler = (PyObject *) user_data;
1270 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1271 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1272 (char *) "sisss", name, type,
1273 publicId, systemId, content);
1274 if (PyErr_Occurred())
1284 pythonNotationDecl(void *user_data,
1285 const xmlChar * name,
1286 const xmlChar * publicId, const xmlChar * systemId)
1291 handler = (PyObject *) user_data;
1292 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1293 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1294 (char *) "sss", name, publicId,
1296 if (PyErr_Occurred())
1303 pythonAttributeDecl(void *user_data,
1304 const xmlChar * elem,
1305 const xmlChar * name,
1308 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1313 xmlEnumerationPtr node;
1317 handler = (PyObject *) user_data;
1318 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
1320 for (node = tree; node != NULL; node = node->next) {
1323 nameList = PyList_New(count);
1325 for (node = tree; node != NULL; node = node->next) {
1326 newName = PY_IMPORT_STRING((char *) node->name);
1327 PyList_SetItem(nameList, count, newName);
1331 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1332 (char *) "ssiisO", elem, name, type,
1333 def, defaultValue, nameList);
1334 if (PyErr_Occurred())
1336 Py_XDECREF(nameList);
1342 pythonElementDecl(void *user_data,
1343 const xmlChar * name,
1344 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
1350 handler = (PyObject *) user_data;
1351 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1352 /* TODO: wrap in an elementContent object */
1354 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1356 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1357 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1358 (char *) "siO", name, type, obj);
1359 if (PyErr_Occurred())
1366 pythonUnparsedEntityDecl(void *user_data,
1367 const xmlChar * name,
1368 const xmlChar * publicId,
1369 const xmlChar * systemId,
1370 const xmlChar * notationName)
1375 handler = (PyObject *) user_data;
1376 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1378 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1379 (char *) "ssss", name, publicId, systemId,
1381 if (PyErr_Occurred())
1388 pythonInternalSubset(void *user_data, const xmlChar * name,
1389 const xmlChar * ExternalID, const xmlChar * SystemID)
1395 printf("pythonInternalSubset(%s, %s, %s) called\n",
1396 name, ExternalID, SystemID);
1398 handler = (PyObject *) user_data;
1399 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1400 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1401 (char *) "sss", name, ExternalID,
1403 if (PyErr_Occurred())
1409 static xmlSAXHandler pythonSaxHandler = {
1410 pythonInternalSubset,
1411 NULL, /* TODO pythonIsStandalone, */
1412 NULL, /* TODO pythonHasInternalSubset, */
1413 NULL, /* TODO pythonHasExternalSubset, */
1414 NULL, /* TODO pythonResolveEntity, */
1415 NULL, /* TODO pythonGetEntity, */
1418 pythonAttributeDecl,
1420 pythonUnparsedEntityDecl,
1421 NULL, /* OBSOLETED pythonSetDocumentLocator, */
1422 pythonStartDocument,
1428 pythonIgnorableWhitespace,
1429 pythonProcessingInstruction,
1434 NULL, /* TODO pythonGetParameterEntity, */
1436 pythonExternalSubset,
1438 NULL, /* TODO mograte to SAX2 */
1444 /************************************************************************
1446 * Handling of specific parser context *
1448 ************************************************************************/
1451 libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1457 PyObject *pyobj_SAX = NULL;
1458 xmlSAXHandlerPtr SAX = NULL;
1459 xmlParserCtxtPtr ret;
1462 if (!PyArg_ParseTuple
1463 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1468 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
1469 pyobj_SAX, chunk, size, URI);
1471 if (pyobj_SAX != Py_None) {
1472 SAX = &pythonSaxHandler;
1473 Py_INCREF(pyobj_SAX);
1474 /* The reference is released in pythonEndDocument() */
1476 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
1477 pyret = libxml_xmlParserCtxtPtrWrap(ret);
1482 libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1485 #ifdef LIBXML_HTML_ENABLED
1489 PyObject *pyobj_SAX = NULL;
1490 xmlSAXHandlerPtr SAX = NULL;
1491 xmlParserCtxtPtr ret;
1494 if (!PyArg_ParseTuple
1495 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1500 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
1501 pyobj_SAX, chunk, size, URI);
1503 if (pyobj_SAX != Py_None) {
1504 SAX = &pythonSaxHandler;
1505 Py_INCREF(pyobj_SAX);
1506 /* The reference is released in pythonEndDocument() */
1508 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
1509 XML_CHAR_ENCODING_NONE);
1510 pyret = libxml_xmlParserCtxtPtrWrap(ret);
1515 #endif /* LIBXML_HTML_ENABLED */
1519 libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1521 #ifdef LIBXML_SAX1_ENABLED
1524 PyObject *pyobj_SAX = NULL;
1525 xmlSAXHandlerPtr SAX = NULL;
1527 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1532 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
1533 pyobj_SAX, URI, recover);
1535 if (pyobj_SAX == Py_None) {
1539 SAX = &pythonSaxHandler;
1540 Py_INCREF(pyobj_SAX);
1541 /* The reference is released in pythonEndDocument() */
1542 xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
1543 #endif /* LIBXML_SAX1_ENABLED */
1549 libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1551 #ifdef LIBXML_HTML_ENABLED
1553 const char *encoding;
1554 PyObject *pyobj_SAX = NULL;
1555 xmlSAXHandlerPtr SAX = NULL;
1557 if (!PyArg_ParseTuple
1558 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1563 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
1564 pyobj_SAX, URI, encoding);
1566 if (pyobj_SAX == Py_None) {
1570 SAX = &pythonSaxHandler;
1571 Py_INCREF(pyobj_SAX);
1572 /* The reference is released in pythonEndDocument() */
1573 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1579 #endif /* LIBXML_HTML_ENABLED */
1582 /************************************************************************
1584 * Error message callback *
1586 ************************************************************************/
1588 static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1589 static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1591 /* helper to build a xmlMalloc'ed string from a format and va_list */
1593 * disabled the loop, the repeated call to vsnprintf without reset of ap
1594 * in case the initial buffer was too small segfaulted on x86_64
1595 * we now directly vsnprintf on a large buffer.
1598 libxml_buildMessage(const char *msg, va_list ap)
1603 str = (char *) xmlMalloc(1000);
1607 chars = vsnprintf(str, 999, msg, ap);
1615 libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1625 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1629 if (libxml_xmlPythonErrorFuncHandler == NULL) {
1631 vfprintf(stderr, msg, ap);
1635 if (vsnprintf(str, 999, msg, ap) >= 998)
1639 list = PyTuple_New(2);
1640 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1641 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1642 message = libxml_charPtrConstWrap(str);
1643 PyTuple_SetItem(list, 1, message);
1644 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1651 libxml_xmlErrorInitialize(void)
1654 printf("libxml_xmlErrorInitialize() called\n");
1656 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1657 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1661 libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1664 PyObject *py_retval;
1666 PyObject *pyobj_ctx;
1668 if (!PyArg_ParseTuple
1669 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1674 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
1678 if (libxml_xmlPythonErrorFuncHandler != NULL) {
1679 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
1681 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
1682 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
1685 Py_XINCREF(pyobj_ctx);
1686 Py_XINCREF(pyobj_f);
1688 /* TODO: check f is a function ! */
1689 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1690 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1692 py_retval = libxml_intWrap(1);
1697 /************************************************************************
1699 * Per parserCtxt error handler *
1701 ************************************************************************/
1707 } xmlParserCtxtPyCtxt;
1708 typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1711 libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1715 xmlParserCtxtPtr ctxt;
1716 xmlParserCtxtPyCtxtPtr pyCtxt;
1719 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
1722 ctxt = (xmlParserCtxtPtr)ctx;
1723 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1725 list = PyTuple_New(4);
1726 PyTuple_SetItem(list, 0, pyCtxt->arg);
1727 Py_XINCREF(pyCtxt->arg);
1728 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1729 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1730 PyTuple_SetItem(list, 3, Py_None);
1732 result = PyEval_CallObject(pyCtxt->f, list);
1735 /* TODO: manage for the exception to be propagated... */
1743 libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1748 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1753 libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1758 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1763 libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1768 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1773 libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1778 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1783 libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1785 PyObject *py_retval;
1786 xmlParserCtxtPtr ctxt;
1787 xmlParserCtxtPyCtxtPtr pyCtxt;
1788 PyObject *pyobj_ctxt;
1790 PyObject *pyobj_arg;
1792 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
1793 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1795 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1796 if (ctxt->_private == NULL) {
1797 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1798 if (pyCtxt == NULL) {
1799 py_retval = libxml_intWrap(-1);
1802 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1803 ctxt->_private = pyCtxt;
1806 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1808 /* TODO: check f is a function ! */
1809 Py_XDECREF(pyCtxt->f);
1810 Py_XINCREF(pyobj_f);
1811 pyCtxt->f = pyobj_f;
1812 Py_XDECREF(pyCtxt->arg);
1813 Py_XINCREF(pyobj_arg);
1814 pyCtxt->arg = pyobj_arg;
1816 if (pyobj_f != Py_None) {
1817 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1818 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1819 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1820 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1823 ctxt->sax->error = xmlParserError;
1824 ctxt->vctxt.error = xmlParserValidityError;
1825 ctxt->sax->warning = xmlParserWarning;
1826 ctxt->vctxt.warning = xmlParserValidityWarning;
1829 py_retval = libxml_intWrap(1);
1834 libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1836 PyObject *py_retval;
1837 xmlParserCtxtPtr ctxt;
1838 xmlParserCtxtPyCtxtPtr pyCtxt;
1839 PyObject *pyobj_ctxt;
1841 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1844 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1845 py_retval = PyTuple_New(2);
1846 if (ctxt->_private != NULL) {
1847 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1849 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1850 Py_XINCREF(pyCtxt->f);
1851 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1852 Py_XINCREF(pyCtxt->arg);
1855 /* no python error handler registered */
1856 PyTuple_SetItem(py_retval, 0, Py_None);
1857 Py_XINCREF(Py_None);
1858 PyTuple_SetItem(py_retval, 1, Py_None);
1859 Py_XINCREF(Py_None);
1865 libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1866 xmlParserCtxtPtr ctxt;
1867 PyObject *pyobj_ctxt;
1868 xmlParserCtxtPyCtxtPtr pyCtxt;
1870 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1872 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1875 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1877 Py_XDECREF(pyCtxt->f);
1878 Py_XDECREF(pyCtxt->arg);
1881 xmlFreeParserCtxt(ctxt);
1889 * xmlValidCtxt stuff
1897 } xmlValidCtxtPyCtxt;
1898 typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1901 libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, ATTRIBUTE_UNUSED int severity, char *str)
1905 xmlValidCtxtPyCtxtPtr pyCtxt;
1908 printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1911 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1913 list = PyTuple_New(2);
1914 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1915 PyTuple_SetItem(list, 1, pyCtxt->arg);
1916 Py_XINCREF(pyCtxt->arg);
1917 result = PyEval_CallObject(pyCtxt->error, list);
1920 /* TODO: manage for the exception to be propagated... */
1928 libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, ATTRIBUTE_UNUSED int severity, char *str)
1932 xmlValidCtxtPyCtxtPtr pyCtxt;
1935 printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1938 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1940 list = PyTuple_New(2);
1941 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1942 PyTuple_SetItem(list, 1, pyCtxt->arg);
1943 Py_XINCREF(pyCtxt->arg);
1944 result = PyEval_CallObject(pyCtxt->warn, list);
1947 /* TODO: manage for the exception to be propagated... */
1955 libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1960 libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1965 libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1970 libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1975 libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1977 PyObject *py_retval;
1978 PyObject *pyobj_error;
1979 PyObject *pyobj_warn;
1980 PyObject *pyobj_ctx;
1981 PyObject *pyobj_arg = Py_None;
1982 xmlValidCtxtPtr ctxt;
1983 xmlValidCtxtPyCtxtPtr pyCtxt;
1985 if (!PyArg_ParseTuple
1986 (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1990 printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1993 ctxt = PyValidCtxt_Get(pyobj_ctx);
1994 pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1995 if (pyCtxt == NULL) {
1996 py_retval = libxml_intWrap(-1);
1999 memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
2002 /* TODO: check warn and error is a function ! */
2003 Py_XDECREF(pyCtxt->error);
2004 Py_XINCREF(pyobj_error);
2005 pyCtxt->error = pyobj_error;
2007 Py_XDECREF(pyCtxt->warn);
2008 Py_XINCREF(pyobj_warn);
2009 pyCtxt->warn = pyobj_warn;
2011 Py_XDECREF(pyCtxt->arg);
2012 Py_XINCREF(pyobj_arg);
2013 pyCtxt->arg = pyobj_arg;
2015 ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
2016 ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
2017 ctxt->userData = pyCtxt;
2019 py_retval = libxml_intWrap(1);
2025 libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2026 xmlValidCtxtPtr cur;
2027 xmlValidCtxtPyCtxtPtr pyCtxt;
2028 PyObject *pyobj_cur;
2030 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
2032 cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
2034 pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
2037 Py_XDECREF(pyCtxt->error);
2038 Py_XDECREF(pyCtxt->warn);
2039 Py_XDECREF(pyCtxt->arg);
2043 xmlFreeValidCtxt(cur);
2048 #ifdef LIBXML_READER_ENABLED
2049 /************************************************************************
2051 * Per xmlTextReader error handler *
2053 ************************************************************************/
2059 } xmlTextReaderPyCtxt;
2060 typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
2063 libxml_xmlTextReaderErrorCallback(void *arg,
2066 xmlTextReaderLocatorPtr locator)
2068 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
2072 list = PyTuple_New(4);
2073 PyTuple_SetItem(list, 0, pyCtxt->arg);
2074 Py_XINCREF(pyCtxt->arg);
2075 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
2076 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
2077 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
2078 result = PyEval_CallObject(pyCtxt->f, list);
2081 /* TODO: manage for the exception to be propagated... */
2089 libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
2091 xmlTextReaderPtr reader;
2092 xmlTextReaderPyCtxtPtr pyCtxt;
2093 xmlTextReaderErrorFunc f;
2095 PyObject *pyobj_reader;
2097 PyObject *pyobj_arg;
2098 PyObject *py_retval;
2100 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
2102 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2103 /* clear previous error handler */
2104 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2106 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
2107 /* ok, it's our error handler! */
2108 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2109 Py_XDECREF(pyCtxt->f);
2110 Py_XDECREF(pyCtxt->arg);
2115 * there already an arg, and it's not ours,
2116 * there is definitely something wrong going on here...
2117 * we don't know how to free it, so we bail out...
2119 py_retval = libxml_intWrap(-1);
2123 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
2124 /* set new error handler */
2125 if (pyobj_f != Py_None)
2127 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
2128 if (pyCtxt == NULL) {
2129 py_retval = libxml_intWrap(-1);
2132 Py_XINCREF(pyobj_f);
2133 pyCtxt->f = pyobj_f;
2134 Py_XINCREF(pyobj_arg);
2135 pyCtxt->arg = pyobj_arg;
2136 xmlTextReaderSetErrorHandler(reader,
2137 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
2141 py_retval = libxml_intWrap(1);
2146 libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
2148 xmlTextReaderPtr reader;
2149 xmlTextReaderPyCtxtPtr pyCtxt;
2150 xmlTextReaderErrorFunc f;
2152 PyObject *pyobj_reader;
2153 PyObject *py_retval;
2155 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
2157 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2158 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2159 py_retval = PyTuple_New(2);
2160 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
2161 /* ok, it's our error handler! */
2162 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2163 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2164 Py_XINCREF(pyCtxt->f);
2165 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2166 Py_XINCREF(pyCtxt->arg);
2170 /* f is null or it's not our error handler */
2171 PyTuple_SetItem(py_retval, 0, Py_None);
2172 Py_XINCREF(Py_None);
2173 PyTuple_SetItem(py_retval, 1, Py_None);
2174 Py_XINCREF(Py_None);
2180 libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2181 xmlTextReaderPtr reader;
2182 PyObject *pyobj_reader;
2183 xmlTextReaderPyCtxtPtr pyCtxt;
2184 xmlTextReaderErrorFunc f;
2187 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2189 if (!PyCapsule_CheckExact(pyobj_reader)) {
2193 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2194 if (reader == NULL) {
2199 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2201 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
2202 /* ok, it's our error handler! */
2203 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2204 Py_XDECREF(pyCtxt->f);
2205 Py_XDECREF(pyCtxt->arg);
2209 * else, something wrong happened, because the error handler is
2210 * not owned by the python bindings...
2214 xmlFreeTextReader(reader);
2220 /************************************************************************
2222 * XPath extensions *
2224 ************************************************************************/
2227 libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2229 PyObject *list, *cur, *result;
2230 xmlXPathObjectPtr obj;
2231 xmlXPathContextPtr rctxt;
2232 PyObject *current_function = NULL;
2233 const xmlChar *name;
2234 const xmlChar *ns_uri;
2239 rctxt = ctxt->context;
2242 name = rctxt->function;
2243 ns_uri = rctxt->functionURI;
2245 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2250 * Find the function, it should be there it was there at lookup
2252 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2253 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
2254 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2255 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2256 current_function = (*libxml_xpathCallbacks)[i].function;
2259 if (current_function == NULL) {
2261 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2266 list = PyTuple_New(nargs + 1);
2267 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
2268 for (i = nargs - 1; i >= 0; i--) {
2269 obj = valuePop(ctxt);
2270 cur = libxml_xmlXPathObjectPtrWrap(obj);
2271 PyTuple_SetItem(list, i + 1, cur);
2273 result = PyEval_CallObject(current_function, list);
2276 obj = libxml_xmlXPathObjectPtrConvert(result);
2277 valuePush(ctxt, obj);
2280 static xmlXPathFunction
2281 libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2282 const xmlChar * ns_uri)
2287 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
2288 ctxt, name, ns_uri);
2291 * This is called once only. The address is then stored in the
2292 * XPath expression evaluation, the proper object to call can
2293 * then still be found using the execution context function
2294 * and functionURI fields.
2296 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2297 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2298 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2299 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2300 return (libxml_xmlXPathFuncCallback);
2307 libxml_xpathCallbacksInitialize(void)
2311 if (libxml_xpathCallbacksInitialized != 0)
2315 printf("libxml_xpathCallbacksInitialized called\n");
2317 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2318 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2320 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2321 (*libxml_xpathCallbacks)[i].ctx = NULL;
2322 (*libxml_xpathCallbacks)[i].name = NULL;
2323 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
2324 (*libxml_xpathCallbacks)[i].function = NULL;
2326 libxml_xpathCallbacksInitialized = 1;
2330 libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2333 PyObject *py_retval;
2337 xmlXPathContextPtr ctx;
2338 PyObject *pyobj_ctx;
2342 if (!PyArg_ParseTuple
2343 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2347 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2348 if (libxml_xpathCallbacksInitialized == 0)
2349 libxml_xpathCallbacksInitialize();
2350 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2352 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
2353 py_retval = libxml_intWrap(-1);
2357 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
2360 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2361 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2362 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2363 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2364 Py_XINCREF(pyobj_f);
2365 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2366 (*libxml_xpathCallbacks)[i].function = pyobj_f;
2371 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2372 libxml_xpathCallbacksAllocd+=10;
2373 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2374 libxml_xpathCallbacks,
2375 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2377 i = libxml_xpathCallbacksNb++;
2378 Py_XINCREF(pyobj_f);
2379 (*libxml_xpathCallbacks)[i].ctx = ctx;
2380 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2381 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2382 (*libxml_xpathCallbacks)[i].function = pyobj_f;
2386 py_retval = libxml_intWrap((int) c_retval);
2391 libxml_xmlXPathRegisterVariable(ATTRIBUTE_UNUSED PyObject * self,
2394 PyObject *py_retval;
2398 xmlXPathContextPtr ctx;
2399 xmlXPathObjectPtr val;
2400 PyObject *pyobj_ctx;
2401 PyObject *pyobj_value;
2403 if (!PyArg_ParseTuple
2404 (args, (char *) "OszO:xpathRegisterVariable", &pyobj_ctx, &name,
2405 &ns_uri, &pyobj_value))
2408 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2409 val = libxml_xmlXPathObjectPtrConvert(pyobj_value);
2411 c_retval = xmlXPathRegisterVariableNS(ctx, name, ns_uri, val);
2412 py_retval = libxml_intWrap(c_retval);
2416 /************************************************************************
2418 * Global properties access *
2420 ************************************************************************/
2422 libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2424 PyObject *resultobj, *obj;
2428 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
2430 cur = PyxmlNode_Get(obj);
2433 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2436 switch (cur->type) {
2437 case XML_DOCUMENT_NODE:
2438 #ifdef LIBXML_DOCB_ENABLED
2439 case XML_DOCB_DOCUMENT_NODE:
2441 case XML_HTML_DOCUMENT_NODE:{
2442 xmlDocPtr doc = (xmlDocPtr) cur;
2447 case XML_ATTRIBUTE_NODE:{
2448 xmlAttrPtr attr = (xmlAttrPtr) cur;
2453 case XML_NAMESPACE_DECL:{
2454 xmlNsPtr ns = (xmlNsPtr) cur;
2463 resultobj = libxml_constxmlCharPtrWrap(res);
2469 libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2471 PyObject *resultobj, *obj;
2475 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
2477 cur = PyxmlNode_Get(obj);
2480 printf("libxml_doc: cur = %p\n", cur);
2483 switch (cur->type) {
2484 case XML_DOCUMENT_NODE:
2485 #ifdef LIBXML_DOCB_ENABLED
2486 case XML_DOCB_DOCUMENT_NODE:
2488 case XML_HTML_DOCUMENT_NODE:
2491 case XML_ATTRIBUTE_NODE:{
2492 xmlAttrPtr attr = (xmlAttrPtr) cur;
2497 case XML_NAMESPACE_DECL:
2504 resultobj = libxml_xmlDocPtrWrap(res);
2509 libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2511 PyObject *resultobj, *obj;
2515 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
2517 cur = PyxmlNode_Get(obj);
2518 if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
2519 res = cur->properties;
2522 resultobj = libxml_xmlAttrPtrWrap(res);
2527 libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2529 PyObject *resultobj, *obj;
2533 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
2535 cur = PyxmlNode_Get(obj);
2538 printf("libxml_next: cur = %p\n", cur);
2541 switch (cur->type) {
2542 case XML_DOCUMENT_NODE:
2543 #ifdef LIBXML_DOCB_ENABLED
2544 case XML_DOCB_DOCUMENT_NODE:
2546 case XML_HTML_DOCUMENT_NODE:
2549 case XML_ATTRIBUTE_NODE:{
2550 xmlAttrPtr attr = (xmlAttrPtr) cur;
2552 res = (xmlNodePtr) attr->next;
2555 case XML_NAMESPACE_DECL:{
2556 xmlNsPtr ns = (xmlNsPtr) cur;
2558 res = (xmlNodePtr) ns->next;
2566 resultobj = libxml_xmlNodePtrWrap(res);
2571 libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2573 PyObject *resultobj, *obj;
2577 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
2579 cur = PyxmlNode_Get(obj);
2582 printf("libxml_prev: cur = %p\n", cur);
2585 switch (cur->type) {
2586 case XML_DOCUMENT_NODE:
2587 #ifdef LIBXML_DOCB_ENABLED
2588 case XML_DOCB_DOCUMENT_NODE:
2590 case XML_HTML_DOCUMENT_NODE:
2593 case XML_ATTRIBUTE_NODE:{
2594 xmlAttrPtr attr = (xmlAttrPtr) cur;
2596 res = (xmlNodePtr) attr->prev;
2599 case XML_NAMESPACE_DECL:
2606 resultobj = libxml_xmlNodePtrWrap(res);
2611 libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2613 PyObject *resultobj, *obj;
2617 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
2619 cur = PyxmlNode_Get(obj);
2622 printf("libxml_children: cur = %p\n", cur);
2625 switch (cur->type) {
2626 case XML_ELEMENT_NODE:
2627 case XML_ENTITY_REF_NODE:
2628 case XML_ENTITY_NODE:
2630 case XML_COMMENT_NODE:
2631 case XML_DOCUMENT_NODE:
2632 #ifdef LIBXML_DOCB_ENABLED
2633 case XML_DOCB_DOCUMENT_NODE:
2635 case XML_HTML_DOCUMENT_NODE:
2637 res = cur->children;
2639 case XML_ATTRIBUTE_NODE:{
2640 xmlAttrPtr attr = (xmlAttrPtr) cur;
2642 res = attr->children;
2649 resultobj = libxml_xmlNodePtrWrap(res);
2654 libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2656 PyObject *resultobj, *obj;
2660 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2662 cur = PyxmlNode_Get(obj);
2665 printf("libxml_last: cur = %p\n", cur);
2668 switch (cur->type) {
2669 case XML_ELEMENT_NODE:
2670 case XML_ENTITY_REF_NODE:
2671 case XML_ENTITY_NODE:
2673 case XML_COMMENT_NODE:
2674 case XML_DOCUMENT_NODE:
2675 #ifdef LIBXML_DOCB_ENABLED
2676 case XML_DOCB_DOCUMENT_NODE:
2678 case XML_HTML_DOCUMENT_NODE:
2682 case XML_ATTRIBUTE_NODE:{
2683 xmlAttrPtr attr = (xmlAttrPtr) cur;
2692 resultobj = libxml_xmlNodePtrWrap(res);
2697 libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2699 PyObject *resultobj, *obj;
2703 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
2705 cur = PyxmlNode_Get(obj);
2708 printf("libxml_parent: cur = %p\n", cur);
2711 switch (cur->type) {
2712 case XML_DOCUMENT_NODE:
2713 case XML_HTML_DOCUMENT_NODE:
2714 #ifdef LIBXML_DOCB_ENABLED
2715 case XML_DOCB_DOCUMENT_NODE:
2719 case XML_ATTRIBUTE_NODE:{
2720 xmlAttrPtr attr = (xmlAttrPtr) cur;
2725 case XML_ENTITY_DECL:
2726 case XML_NAMESPACE_DECL:
2727 case XML_XINCLUDE_START:
2728 case XML_XINCLUDE_END:
2735 resultobj = libxml_xmlNodePtrWrap(res);
2740 libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2742 PyObject *resultobj, *obj;
2744 const xmlChar *res = NULL;
2746 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2748 cur = PyxmlNode_Get(obj);
2755 printf("libxml_type: cur = %p\n", cur);
2758 switch (cur->type) {
2759 case XML_ELEMENT_NODE:
2760 res = (const xmlChar *) "element";
2762 case XML_ATTRIBUTE_NODE:
2763 res = (const xmlChar *) "attribute";
2766 res = (const xmlChar *) "text";
2768 case XML_CDATA_SECTION_NODE:
2769 res = (const xmlChar *) "cdata";
2771 case XML_ENTITY_REF_NODE:
2772 res = (const xmlChar *) "entity_ref";
2774 case XML_ENTITY_NODE:
2775 res = (const xmlChar *) "entity";
2778 res = (const xmlChar *) "pi";
2780 case XML_COMMENT_NODE:
2781 res = (const xmlChar *) "comment";
2783 case XML_DOCUMENT_NODE:
2784 res = (const xmlChar *) "document_xml";
2786 case XML_DOCUMENT_TYPE_NODE:
2787 res = (const xmlChar *) "doctype";
2789 case XML_DOCUMENT_FRAG_NODE:
2790 res = (const xmlChar *) "fragment";
2792 case XML_NOTATION_NODE:
2793 res = (const xmlChar *) "notation";
2795 case XML_HTML_DOCUMENT_NODE:
2796 res = (const xmlChar *) "document_html";
2799 res = (const xmlChar *) "dtd";
2801 case XML_ELEMENT_DECL:
2802 res = (const xmlChar *) "elem_decl";
2804 case XML_ATTRIBUTE_DECL:
2805 res = (const xmlChar *) "attribute_decl";
2807 case XML_ENTITY_DECL:
2808 res = (const xmlChar *) "entity_decl";
2810 case XML_NAMESPACE_DECL:
2811 res = (const xmlChar *) "namespace";
2813 case XML_XINCLUDE_START:
2814 res = (const xmlChar *) "xinclude_start";
2816 case XML_XINCLUDE_END:
2817 res = (const xmlChar *) "xinclude_end";
2819 #ifdef LIBXML_DOCB_ENABLED
2820 case XML_DOCB_DOCUMENT_NODE:
2821 res = (const xmlChar *) "document_docbook";
2826 printf("libxml_type: cur = %p: %s\n", cur, res);
2829 resultobj = libxml_constxmlCharPtrWrap(res);
2833 /************************************************************************
2835 * Specific accessor functions *
2837 ************************************************************************/
2839 libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2841 PyObject *py_retval;
2844 PyObject *pyobj_node;
2846 if (!PyArg_ParseTuple
2847 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2849 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2851 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2855 c_retval = node->nsDef;
2856 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2861 libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2863 PyObject *py_retval;
2866 PyObject *pyobj_node;
2870 if (!PyArg_ParseTuple
2871 (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
2873 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2876 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2889 while (ns != NULL) {
2890 if (xmlStrEqual(ns->href, href)) {
2892 prev->next = ns->next;
2894 node->nsDef = ns->next;
2905 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2910 libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2912 PyObject *py_retval;
2915 PyObject *pyobj_node;
2917 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2919 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2921 if ((node == NULL) ||
2922 ((node->type != XML_ELEMENT_NODE) &&
2923 (node->type != XML_ATTRIBUTE_NODE))) {
2927 c_retval = node->ns;
2928 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2932 #ifdef LIBXML_OUTPUT_ENABLED
2933 /************************************************************************
2935 * Serialization front-end *
2937 ************************************************************************/
2940 libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2942 PyObject *py_retval = NULL;
2944 PyObject *pyobj_node;
2947 const char *encoding;
2949 xmlSaveCtxtPtr ctxt;
2953 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2954 &encoding, &format))
2956 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2962 if (node->type == XML_DOCUMENT_NODE) {
2963 doc = (xmlDocPtr) node;
2965 #ifdef LIBXML_HTML_ENABLED
2966 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
2967 doc = (xmlDocPtr) node;
2971 if (node->type == XML_NAMESPACE_DECL)
2975 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
2976 #ifdef LIBXML_HTML_ENABLED
2977 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2978 #endif /* LIBXML_HTML_ENABLED */
2986 buf = xmlBufferCreate();
2991 if (format) options |= XML_SAVE_FORMAT;
2992 ctxt = xmlSaveToBuffer(buf, encoding, options);
2999 xmlSaveDoc(ctxt, doc);
3001 xmlSaveTree(ctxt, node);
3004 c_retval = buf->content;
3005 buf->content = NULL;
3008 py_retval = libxml_charPtrWrap((char *) c_retval);
3014 libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3016 PyObject *py_file = NULL;
3018 PyObject *pyobj_node;
3021 const char *encoding;
3024 xmlOutputBufferPtr buf;
3025 xmlCharEncodingHandlerPtr handler = NULL;
3027 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
3028 &py_file, &encoding, &format))
3030 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
3032 return (PyLong_FromLong((long) -1));
3034 output = PyFile_Get(py_file);
3035 if (output == NULL) {
3036 return (PyLong_FromLong((long) -1));
3039 if (node->type == XML_DOCUMENT_NODE) {
3040 doc = (xmlDocPtr) node;
3041 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
3042 doc = (xmlDocPtr) node;
3046 #ifdef LIBXML_HTML_ENABLED
3047 if (doc->type == XML_HTML_DOCUMENT_NODE) {
3048 if (encoding == NULL)
3049 encoding = (const char *) htmlGetMetaEncoding(doc);
3051 #endif /* LIBXML_HTML_ENABLED */
3052 if (encoding != NULL) {
3053 handler = xmlFindCharEncodingHandler(encoding);
3054 if (handler == NULL) {
3055 return (PyLong_FromLong((long) -1));
3058 if (doc->type == XML_HTML_DOCUMENT_NODE) {
3059 if (handler == NULL)
3060 handler = xmlFindCharEncodingHandler("HTML");
3061 if (handler == NULL)
3062 handler = xmlFindCharEncodingHandler("ascii");
3065 buf = xmlOutputBufferCreateFile(output, handler);
3066 if (node->type == XML_DOCUMENT_NODE) {
3067 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
3068 #ifdef LIBXML_HTML_ENABLED
3069 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
3070 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
3071 len = xmlOutputBufferClose(buf);
3072 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3073 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
3074 len = xmlOutputBufferClose(buf);
3075 #endif /* LIBXML_HTML_ENABLED */
3077 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
3078 len = xmlOutputBufferClose(buf);
3080 PyFile_Release(output);
3081 return (PyLong_FromLong((long) len));
3083 #endif /* LIBXML_OUTPUT_ENABLED */
3085 /************************************************************************
3089 ************************************************************************/
3091 libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3093 PyObject *py_retval;
3097 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
3099 node = (xmlNodePtr) xmlNewNode(NULL, name);
3101 printf("NewNode: %s : %p\n", name, (void *) node);
3108 py_retval = libxml_xmlNodePtrWrap(node);
3113 /************************************************************************
3115 * Local Catalog stuff *
3117 ************************************************************************/
3119 libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3122 xmlParserCtxtPtr ctxt;
3123 PyObject *pyobj_ctxt;
3125 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
3128 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
3131 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3135 printf("LocalCatalog: %s\n", URL);
3142 #ifdef LIBXML_SCHEMAS_ENABLED
3144 /************************************************************************
3146 * RelaxNG error handler registration *
3148 ************************************************************************/
3155 } xmlRelaxNGValidCtxtPyCtxt;
3156 typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
3159 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3163 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3166 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3169 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3171 list = PyTuple_New(2);
3172 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3173 PyTuple_SetItem(list, 1, pyCtxt->arg);
3174 Py_XINCREF(pyCtxt->arg);
3175 result = PyEval_CallObject(pyCtxt->error, list);
3178 /* TODO: manage for the exception to be propagated... */
3186 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3190 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3193 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3196 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3198 list = PyTuple_New(2);
3199 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3200 PyTuple_SetItem(list, 1, pyCtxt->arg);
3201 Py_XINCREF(pyCtxt->arg);
3202 result = PyEval_CallObject(pyCtxt->warn, list);
3205 /* TODO: manage for the exception to be propagated... */
3213 libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3218 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3223 libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3228 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3233 libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3235 PyObject *py_retval;
3236 PyObject *pyobj_error;
3237 PyObject *pyobj_warn;
3238 PyObject *pyobj_ctx;
3239 PyObject *pyobj_arg = Py_None;
3240 xmlRelaxNGValidCtxtPtr ctxt;
3241 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3243 if (!PyArg_ParseTuple
3244 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3248 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3251 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
3252 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3254 py_retval = libxml_intWrap(-1);
3260 /* first time to set the error handlers */
3261 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3262 if (pyCtxt == NULL) {
3263 py_retval = libxml_intWrap(-1);
3266 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3269 /* TODO: check warn and error is a function ! */
3270 Py_XDECREF(pyCtxt->error);
3271 Py_XINCREF(pyobj_error);
3272 pyCtxt->error = pyobj_error;
3274 Py_XDECREF(pyCtxt->warn);
3275 Py_XINCREF(pyobj_warn);
3276 pyCtxt->warn = pyobj_warn;
3278 Py_XDECREF(pyCtxt->arg);
3279 Py_XINCREF(pyobj_arg);
3280 pyCtxt->arg = pyobj_arg;
3282 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3284 py_retval = libxml_intWrap(1);
3289 libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3290 xmlRelaxNGValidCtxtPtr ctxt;
3291 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3292 PyObject *pyobj_ctxt;
3294 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3296 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3298 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3302 Py_XDECREF(pyCtxt->error);
3303 Py_XDECREF(pyCtxt->warn);
3304 Py_XDECREF(pyCtxt->arg);
3309 xmlRelaxNGFreeValidCtxt(ctxt);
3319 } xmlSchemaValidCtxtPyCtxt;
3320 typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3323 libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3327 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3330 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3333 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3335 list = PyTuple_New(2);
3336 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3337 PyTuple_SetItem(list, 1, pyCtxt->arg);
3338 Py_XINCREF(pyCtxt->arg);
3339 result = PyEval_CallObject(pyCtxt->error, list);
3342 /* TODO: manage for the exception to be propagated... */
3350 libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3354 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3357 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3360 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3362 list = PyTuple_New(2);
3363 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3364 PyTuple_SetItem(list, 1, pyCtxt->arg);
3365 Py_XINCREF(pyCtxt->arg);
3366 result = PyEval_CallObject(pyCtxt->warn, list);
3369 /* TODO: manage for the exception to be propagated... */
3377 libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3382 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3387 libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3392 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3397 libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3399 PyObject *py_retval;
3400 PyObject *pyobj_error;
3401 PyObject *pyobj_warn;
3402 PyObject *pyobj_ctx;
3403 PyObject *pyobj_arg = Py_None;
3404 xmlSchemaValidCtxtPtr ctxt;
3405 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3407 if (!PyArg_ParseTuple
3408 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3412 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3415 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3416 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3418 py_retval = libxml_intWrap(-1);
3424 /* first time to set the error handlers */
3425 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3426 if (pyCtxt == NULL) {
3427 py_retval = libxml_intWrap(-1);
3430 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3433 /* TODO: check warn and error is a function ! */
3434 Py_XDECREF(pyCtxt->error);
3435 Py_XINCREF(pyobj_error);
3436 pyCtxt->error = pyobj_error;
3438 Py_XDECREF(pyCtxt->warn);
3439 Py_XINCREF(pyobj_warn);
3440 pyCtxt->warn = pyobj_warn;
3442 Py_XDECREF(pyCtxt->arg);
3443 Py_XINCREF(pyobj_arg);
3444 pyCtxt->arg = pyobj_arg;
3446 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3448 py_retval = libxml_intWrap(1);
3453 libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3455 xmlSchemaValidCtxtPtr ctxt;
3456 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3457 PyObject *pyobj_ctxt;
3459 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3461 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3463 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3467 Py_XDECREF(pyCtxt->error);
3468 Py_XDECREF(pyCtxt->warn);
3469 Py_XDECREF(pyCtxt->arg);
3474 xmlSchemaFreeValidCtxt(ctxt);
3481 #ifdef LIBXML_C14N_ENABLED
3482 #ifdef LIBXML_OUTPUT_ENABLED
3484 /************************************************************************
3486 * XML Canonicalization c14n *
3488 ************************************************************************/
3491 PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3493 xmlNodeSetPtr nodeSet;
3496 if (PyTuple_Check(py_nodeset))
3498 else if (PyList_Check(py_nodeset))
3500 else if (py_nodeset == Py_None) {
3505 PyErr_SetString(PyExc_TypeError,
3506 "must be a tuple or list of nodes.");
3510 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3511 if (nodeSet == NULL) {
3512 PyErr_SetString(PyExc_MemoryError, "");
3516 nodeSet->nodeNr = 0;
3517 nodeSet->nodeMax = (is_tuple
3518 ? PyTuple_GET_SIZE(py_nodeset)
3519 : PyList_GET_SIZE(py_nodeset));
3521 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3522 * sizeof(xmlNodePtr));
3523 if (nodeSet->nodeTab == NULL) {
3525 PyErr_SetString(PyExc_MemoryError, "");
3528 memset(nodeSet->nodeTab, 0 ,
3529 nodeSet->nodeMax * sizeof(xmlNodePtr));
3533 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3535 PyxmlNode_Get (is_tuple
3536 ? PyTuple_GET_ITEM(py_nodeset, idx)
3537 : PyList_GET_ITEM(py_nodeset, idx));
3539 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3547 PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3549 /* NOTE: the array should be freed, but the strings are shared
3550 with the python strings and so must not be freed. */
3557 if (PyTuple_Check(py_strings))
3559 else if (PyList_Check(py_strings))
3561 else if (py_strings == Py_None) {
3566 PyErr_SetString(PyExc_TypeError,
3567 "must be a tuple or list of strings.");
3572 ? PyTuple_GET_SIZE(py_strings)
3573 : PyList_GET_SIZE(py_strings));
3575 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3577 if (strings == NULL) {
3578 PyErr_SetString(PyExc_MemoryError, "");
3582 memset(strings, 0 , sizeof(xmlChar *) * count);
3586 for (idx=0; idx < count; ++idx) {
3587 char* s = PyBytes_AsString
3589 ? PyTuple_GET_ITEM(py_strings, idx)
3590 : PyList_GET_ITEM(py_strings, idx));
3592 strings[init_index++] = (xmlChar *)s;
3595 PyErr_SetString(PyExc_TypeError,
3596 "must be a tuple or list of strings.");
3607 libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3610 PyObject *py_retval = NULL;
3612 PyObject *pyobj_doc;
3613 PyObject *pyobj_nodes;
3615 PyObject *pyobj_prefixes;
3619 xmlNodeSetPtr nodes;
3620 xmlChar **prefixes = NULL;
3625 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3633 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3635 PyErr_SetString(PyExc_TypeError, "bad document.");
3639 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3640 if (result < 0) return NULL;
3643 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3646 xmlFree(nodes->nodeTab);
3653 result = xmlC14NDocDumpMemory(doc,
3661 xmlFree(nodes->nodeTab);
3665 xmlChar ** idx = prefixes;
3666 while (*idx) xmlFree(*(idx++));
3671 PyErr_SetString(PyExc_Exception,
3672 "libxml2 xmlC14NDocDumpMemory failure.");
3676 py_retval = PY_IMPORT_STRING_SIZE((const char *) doc_txt,
3684 libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3687 PyObject *pyobj_doc;
3689 PyObject *pyobj_nodes;
3691 PyObject *pyobj_prefixes;
3695 xmlNodeSetPtr nodes;
3696 xmlChar **prefixes = NULL;
3698 xmlOutputBufferPtr buf;
3703 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3712 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3714 PyErr_SetString(PyExc_TypeError, "bad document.");
3718 output = PyFile_Get(py_file);
3719 if (output == NULL) {
3720 PyErr_SetString(PyExc_TypeError, "bad file.");
3723 buf = xmlOutputBufferCreateFile(output, NULL);
3725 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3726 if (result < 0) return NULL;
3729 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3732 xmlFree(nodes->nodeTab);
3739 result = xmlC14NDocSaveTo(doc,
3747 xmlFree(nodes->nodeTab);
3751 xmlChar ** idx = prefixes;
3752 while (*idx) xmlFree(*(idx++));
3756 PyFile_Release(output);
3757 len = xmlOutputBufferClose(buf);
3760 PyErr_SetString(PyExc_Exception,
3761 "libxml2 xmlC14NDocSaveTo failure.");
3765 return PyLong_FromLong((long) len);
3772 libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3777 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3779 str = PyCapsule_GetPointer(obj, PyCapsule_GetName(obj));
3780 return Py_BuildValue((char *)"s", str);
3784 libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3786 PyObject *py_node1, *py_node2;
3787 xmlNodePtr node1, node2;
3789 if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
3790 &py_node1, &py_node2))
3792 /* To compare two node objects, we compare their pointer addresses */
3793 node1 = PyxmlNode_Get(py_node1);
3794 node2 = PyxmlNode_Get(py_node2);
3795 if ( node1 == node2 )
3796 return Py_BuildValue((char *)"i", 1);
3798 return Py_BuildValue((char *)"i", 0);
3803 libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3808 if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
3810 /* For simplicity, we use the node pointer address as a hash value */
3811 node1 = PyxmlNode_Get(py_node1);
3813 return PyLong_FromVoidPtr(node1);
3817 /************************************************************************
3819 * The registration stuff *
3821 ************************************************************************/
3822 static PyMethodDef libxmlMethods[] = {
3823 #include "libxml2-export.c"
3824 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3825 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3826 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3827 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3828 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3829 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3830 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3831 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3832 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3833 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
3834 {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
3835 {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
3836 {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
3837 #ifdef LIBXML_OUTPUT_ENABLED
3838 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3839 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
3840 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
3841 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3842 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3843 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
3844 { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3845 { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
3846 #endif /* LIBXML_OUTPUT_ENABLED */
3847 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3848 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
3849 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
3850 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3851 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
3852 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
3853 #ifdef LIBXML_READER_ENABLED
3854 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3855 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3856 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
3858 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
3859 #ifdef LIBXML_SCHEMAS_ENABLED
3860 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3861 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
3862 {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
3863 {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
3865 #ifdef LIBXML_C14N_ENABLED
3866 #ifdef LIBXML_OUTPUT_ENABLED
3867 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3868 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3871 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
3872 {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
3873 {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
3874 {(char *) "xmlRegisterInputCallback", libxml_xmlRegisterInputCallback, METH_VARARGS, NULL},
3875 {(char *) "xmlUnregisterInputCallback", libxml_xmlUnregisterInputCallback, METH_VARARGS, NULL},
3876 {NULL, NULL, 0, NULL}
3879 #if PY_MAJOR_VERSION >= 3
3880 #define INITERROR return NULL
3882 static struct PyModuleDef moduledef = {
3883 PyModuleDef_HEAD_INIT,
3895 #define INITERROR return
3897 #ifdef MERGED_MODULES
3898 extern void initlibxsltmod(void);
3903 #if PY_MAJOR_VERSION >= 3
3904 PyObject *PyInit_libxml2mod(void)
3906 void initlibxml2mod(void)
3911 #if PY_MAJOR_VERSION >= 3
3912 module = PyModule_Create(&moduledef);
3914 /* intialize the python extension module */
3915 module = Py_InitModule((char *) "libxml2mod", libxmlMethods);
3920 /* initialize libxml2 */
3922 /* TODO this probably need to be revamped for Python3 */
3923 libxml_xmlErrorInitialize();
3925 #if PY_MAJOR_VERSION < 3
3926 #ifdef MERGED_MODULES
3931 #if PY_MAJOR_VERSION >= 3