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_wrap.h"
27
#include "libxml2-py.h"
29
#if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
30
#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
31
#elif 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
void initlibxml2mod(void);
49
* macro to flag unimplemented blocks
52
xmlGenericError(xmlGenericErrorContext, \
53
"Unimplemented block at %s:%d\n", \
56
* the following vars are used for XPath extensions, but
57
* are also referenced within the parser cleanup routine.
59
static int libxml_xpathCallbacksInitialized = 0;
61
typedef struct libxml_xpathCallback {
62
xmlXPathContextPtr ctx;
66
} libxml_xpathCallback, *libxml_xpathCallbackPtr;
67
typedef libxml_xpathCallback libxml_xpathCallbackArray[];
68
static int libxml_xpathCallbacksAllocd = 10;
69
static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
70
static int libxml_xpathCallbacksNb = 0;
72
/************************************************************************
74
* Memory debug interface *
76
************************************************************************/
79
extern void xmlMemFree(void *ptr);
80
extern void *xmlMemMalloc(size_t size);
81
extern void *xmlMemRealloc(void *ptr, size_t size);
82
extern char *xmlMemoryStrdup(const char *str);
85
static int libxmlMemoryDebugActivated = 0;
86
static long libxmlMemoryAllocatedBase = 0;
88
static int libxmlMemoryDebug = 0;
89
static xmlFreeFunc freeFunc = NULL;
90
static xmlMallocFunc mallocFunc = NULL;
91
static xmlReallocFunc reallocFunc = NULL;
92
static xmlStrdupFunc strdupFunc = NULL;
95
libxml_xmlErrorInitialize(void); /* forward declare */
98
libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
99
PyObject * args ATTRIBUTE_UNUSED)
106
py_retval = libxml_longWrap(ret);
111
libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
117
if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
121
printf("libxml_xmlDebugMemory(%d) called\n", activate);
125
if (libxmlMemoryDebug == 0) {
127
* First initialize the library and grab the old memory handlers
128
* and switch the library to memory debugging
130
xmlMemGet((xmlFreeFunc *) & freeFunc,
131
(xmlMallocFunc *) & mallocFunc,
132
(xmlReallocFunc *) & reallocFunc,
133
(xmlStrdupFunc *) & strdupFunc);
134
if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
135
(reallocFunc == xmlMemRealloc) &&
136
(strdupFunc == xmlMemoryStrdup)) {
137
libxmlMemoryAllocatedBase = xmlMemUsed();
140
* cleanup first, because some memory has been
141
* allocated with the non-debug malloc in xmlInitParser
142
* when the python module was imported
145
ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
146
xmlMemRealloc, xmlMemoryStrdup);
149
libxmlMemoryAllocatedBase = xmlMemUsed();
152
libxml_xmlErrorInitialize();
155
} else if (libxmlMemoryDebugActivated == 0) {
156
libxmlMemoryAllocatedBase = xmlMemUsed();
159
ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
161
libxmlMemoryDebug = 1;
162
libxmlMemoryDebugActivated = 1;
164
if (libxmlMemoryDebugActivated == 1)
165
ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
168
libxmlMemoryDebugActivated = 0;
171
py_retval = libxml_longWrap(ret);
176
libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
177
PyObject *args ATTRIBUTE_UNUSED) {
182
if (libxmlMemoryDebug) {
183
freed = xmlMemUsed();
188
* Need to confirm whether we really want to do this (required for
189
* memcheck) in all cases...
192
if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
193
for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
194
if ((*libxml_xpathCallbacks)[ix].name != NULL)
195
xmlFree((*libxml_xpathCallbacks)[ix].name);
196
if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
197
xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
199
libxml_xpathCallbacksNb = 0;
200
xmlFree(libxml_xpathCallbacks);
201
libxml_xpathCallbacks = NULL;
204
if (libxmlMemoryDebug) {
205
freed -= xmlMemUsed();
206
libxmlMemoryAllocatedBase -= freed;
207
if (libxmlMemoryAllocatedBase < 0)
208
libxmlMemoryAllocatedBase = 0;
216
libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
217
ATTRIBUTE_UNUSED PyObject * args)
220
if (libxmlMemoryDebug != 0)
226
/************************************************************************
228
* Handling Python FILE I/O at the C level *
229
* The raw I/O attack diectly the File objects, while the *
230
* other routines address the ioWrapper instance instead *
232
************************************************************************/
235
* xmlPythonFileCloseUnref:
236
* @context: the I/O context
238
* Close an I/O channel
241
xmlPythonFileCloseRaw (void * context) {
242
PyObject *file, *ret;
245
printf("xmlPythonFileCloseUnref\n");
247
file = (PyObject *) context;
248
if (file == NULL) return(-1);
249
ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
258
* xmlPythonFileReadRaw:
259
* @context: the I/O context
260
* @buffer: where to drop data
261
* @len: number of bytes to write
263
* Read @len bytes to @buffer from the Python file in the I/O channel
265
* Returns the number of bytes read
268
xmlPythonFileReadRaw (void * context, char * buffer, int len) {
275
printf("xmlPythonFileReadRaw: %d\n", len);
277
file = (PyObject *) context;
278
if (file == NULL) return(-1);
279
ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
281
printf("xmlPythonFileReadRaw: result is NULL\n");
283
} else if (PyString_Check(ret)) {
284
lenread = PyString_Size(ret);
285
data = PyString_AsString(ret);
287
memcpy(buffer, data, len);
289
memcpy(buffer, data, lenread);
292
printf("xmlPythonFileReadRaw: result is not a String\n");
300
* @context: the I/O context
301
* @buffer: where to drop data
302
* @len: number of bytes to write
304
* Read @len bytes to @buffer from the I/O channel.
306
* Returns the number of bytes read
309
xmlPythonFileRead (void * context, char * buffer, int len) {
316
printf("xmlPythonFileRead: %d\n", len);
318
file = (PyObject *) context;
319
if (file == NULL) return(-1);
320
ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
322
printf("xmlPythonFileRead: result is NULL\n");
324
} else if (PyString_Check(ret)) {
325
lenread = PyString_Size(ret);
326
data = PyString_AsString(ret);
328
memcpy(buffer, data, len);
330
memcpy(buffer, data, lenread);
333
printf("xmlPythonFileRead: result is not a String\n");
341
* @context: the I/O context
342
* @buffer: where to drop data
343
* @len: number of bytes to write
345
* Write @len bytes from @buffer to the I/O channel.
347
* Returns the number of bytes written
350
xmlPythonFileWrite (void * context, const char * buffer, int len) {
353
PyObject *ret = NULL;
357
printf("xmlPythonFileWrite: %d\n", len);
359
file = (PyObject *) context;
360
if (file == NULL) return(-1);
361
string = PyString_FromStringAndSize(buffer, len);
362
if (string == NULL) return(-1);
363
if (PyObject_HasAttrString(file, (char *) "io_write")) {
364
ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
366
} else if (PyObject_HasAttrString(file, (char *) "write")) {
367
ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
372
printf("xmlPythonFileWrite: result is NULL\n");
374
} else if (PyInt_Check(ret)) {
375
written = (int) PyInt_AsLong(ret);
377
} else if (ret == Py_None) {
381
printf("xmlPythonFileWrite: result is not an Int nor None\n");
388
* xmlPythonFileClose:
389
* @context: the I/O context
391
* Close an I/O channel
394
xmlPythonFileClose (void * context) {
395
PyObject *file, *ret = NULL;
398
printf("xmlPythonFileClose\n");
400
file = (PyObject *) context;
401
if (file == NULL) return(-1);
402
if (PyObject_HasAttrString(file, (char *) "io_close")) {
403
ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
404
} else if (PyObject_HasAttrString(file, (char *) "flush")) {
405
ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
413
#ifdef LIBXML_OUTPUT_ENABLED
415
* xmlOutputBufferCreatePythonFile:
416
* @file: a PyFile_Type
417
* @encoder: the encoding converter or NULL
419
* Create a buffered output for the progressive saving to a PyFile_Type
422
* Returns the new parser output or NULL
424
static xmlOutputBufferPtr
425
xmlOutputBufferCreatePythonFile(PyObject *file,
426
xmlCharEncodingHandlerPtr encoder) {
427
xmlOutputBufferPtr ret;
429
if (file == NULL) return(NULL);
431
ret = xmlAllocOutputBuffer(encoder);
434
/* Py_INCREF(file); */
435
ret->writecallback = xmlPythonFileWrite;
436
ret->closecallback = xmlPythonFileClose;
443
libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
447
xmlCharEncodingHandlerPtr handler = NULL;
448
xmlOutputBufferPtr buffer;
451
if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
454
if ((encoding != NULL) && (encoding[0] != 0)) {
455
handler = xmlFindCharEncodingHandler((const char *) encoding);
457
buffer = xmlOutputBufferCreatePythonFile(file, handler);
459
printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
460
py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
465
* libxml_outputBufferGetPythonFile:
466
* @buffer: the I/O buffer
468
* read the Python I/O from the CObject
470
* Returns the new parser output or NULL
473
libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
477
xmlOutputBufferPtr obj;
479
if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
483
obj = PyoutputBuffer_Get(buffer);
486
"outputBufferGetPythonFile: obj == NULL\n");
490
if (obj->closecallback != xmlPythonFileClose) {
492
"outputBufferGetPythonFile: not a python file wrapper\n");
496
file = (PyObject *) obj->context;
506
libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
509
xmlOutputBufferPtr out;
512
if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
514
out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
515
/* Buffer may already have been destroyed elsewhere. This is harmless. */
521
c_retval = xmlOutputBufferClose(out);
522
py_retval = libxml_intWrap((int) c_retval);
527
libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
530
xmlOutputBufferPtr out;
533
if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
535
out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
537
c_retval = xmlOutputBufferFlush(out);
538
py_retval = libxml_intWrap((int) c_retval);
543
libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
546
xmlOutputBufferPtr buf;
552
if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
554
buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
555
cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
557
c_retval = xmlSaveFileTo(buf, cur, encoding);
558
/* xmlSaveTo() freed the memory pointed to by buf, so record that in the
560
((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
561
py_retval = libxml_intWrap((int) c_retval);
566
libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
569
xmlOutputBufferPtr buf;
576
if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
578
buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
579
cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
581
c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
582
/* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
583
* in the Python object */
584
((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
585
py_retval = libxml_intWrap((int) c_retval);
588
#endif /* LIBXML_OUTPUT_ENABLED */
592
* xmlParserInputBufferCreatePythonFile:
593
* @file: a PyFile_Type
594
* @encoder: the encoding converter or NULL
596
* Create a buffered output for the progressive saving to a PyFile_Type
599
* Returns the new parser output or NULL
601
static xmlParserInputBufferPtr
602
xmlParserInputBufferCreatePythonFile(PyObject *file,
603
xmlCharEncoding encoding) {
604
xmlParserInputBufferPtr ret;
606
if (file == NULL) return(NULL);
608
ret = xmlAllocParserInputBuffer(encoding);
611
/* Py_INCREF(file); */
612
ret->readcallback = xmlPythonFileRead;
613
ret->closecallback = xmlPythonFileClose;
620
libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
624
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
625
xmlParserInputBufferPtr buffer;
628
if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
631
if ((encoding != NULL) && (encoding[0] != 0)) {
632
enc = xmlParseCharEncoding((const char *) encoding);
634
buffer = xmlParserInputBufferCreatePythonFile(file, enc);
636
printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
637
py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
641
/************************************************************************
643
* Providing the resolver at the Python level *
645
************************************************************************/
647
static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
648
static PyObject *pythonExternalEntityLoaderObjext;
650
static xmlParserInputPtr
651
pythonExternalEntityLoader(const char *URL, const char *ID,
652
xmlParserCtxtPtr ctxt) {
653
xmlParserInputPtr result = NULL;
654
if (pythonExternalEntityLoaderObjext != NULL) {
658
ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
660
printf("pythonExternalEntityLoader: ready to call\n");
663
ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
664
(char *) "(ssO)", URL, ID, ctxtobj);
667
printf("pythonExternalEntityLoader: result ");
668
PyObject_Print(ret, stderr, 0);
673
if (PyObject_HasAttrString(ret, (char *) "read")) {
674
xmlParserInputBufferPtr buf;
676
buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
679
buf->readcallback = xmlPythonFileReadRaw;
680
buf->closecallback = xmlPythonFileCloseRaw;
681
result = xmlNewIOInputStream(ctxt, buf,
682
XML_CHAR_ENCODING_NONE);
687
printf("pythonExternalEntityLoader: can't read %s\n",
691
if (result == NULL) {
693
} else if (URL != NULL) {
694
result->filename = (char *) xmlStrdup((const xmlChar *)URL);
695
result->directory = xmlParserGetDirectory((const char *) URL);
699
if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
700
result = defaultExternalEntityLoader(URL, ID, ctxt);
706
libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
710
if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
715
printf("libxml_xmlSetEntityLoader\n");
717
if (defaultExternalEntityLoader == NULL)
718
defaultExternalEntityLoader = xmlGetExternalEntityLoader();
720
pythonExternalEntityLoaderObjext = loader;
721
xmlSetExternalEntityLoader(pythonExternalEntityLoader);
723
py_retval = PyInt_FromLong(0);
728
/************************************************************************
730
* Handling SAX/xmllib/sgmlop callback interfaces *
732
************************************************************************/
735
pythonStartElement(void *user_data, const xmlChar * name,
736
const xmlChar ** attrs)
743
PyObject *result = NULL;
747
printf("pythonStartElement(%s) called\n", name);
749
handler = (PyObject *) user_data;
750
if (PyObject_HasAttrString(handler, (char *) "startElement"))
752
else if (PyObject_HasAttrString(handler, (char *) "start"))
756
* the xmllib interface always generate a dictionnary,
759
if ((attrs == NULL) && (type == 1)) {
762
} else if (attrs == NULL) {
766
for (i = 0; attrs[i] != NULL; i++) {
767
attrname = PyString_FromString((char *) attrs[i]);
769
if (attrs[i] != NULL) {
770
attrvalue = PyString_FromString((char *) attrs[i]);
775
PyDict_SetItem(dict, attrname, attrvalue);
777
Py_DECREF(attrvalue);
782
result = PyObject_CallMethod(handler, (char *) "startElement",
783
(char *) "sO", name, dict);
785
result = PyObject_CallMethod(handler, (char *) "start",
786
(char *) "sO", name, dict);
787
if (PyErr_Occurred())
795
pythonStartDocument(void *user_data)
801
printf("pythonStartDocument() called\n");
803
handler = (PyObject *) user_data;
804
if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
806
PyObject_CallMethod(handler, (char *) "startDocument", NULL);
807
if (PyErr_Occurred())
814
pythonEndDocument(void *user_data)
820
printf("pythonEndDocument() called\n");
822
handler = (PyObject *) user_data;
823
if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
825
PyObject_CallMethod(handler, (char *) "endDocument", NULL);
826
if (PyErr_Occurred())
831
* The reference to the handler is released there
837
pythonEndElement(void *user_data, const xmlChar * name)
843
printf("pythonEndElement(%s) called\n", name);
845
handler = (PyObject *) user_data;
846
if (PyObject_HasAttrString(handler, (char *) "endElement")) {
847
result = PyObject_CallMethod(handler, (char *) "endElement",
849
if (PyErr_Occurred())
852
} else if (PyObject_HasAttrString(handler, (char *) "end")) {
853
result = PyObject_CallMethod(handler, (char *) "end",
855
if (PyErr_Occurred())
862
pythonReference(void *user_data, const xmlChar * name)
868
printf("pythonReference(%s) called\n", name);
870
handler = (PyObject *) user_data;
871
if (PyObject_HasAttrString(handler, (char *) "reference")) {
872
result = PyObject_CallMethod(handler, (char *) "reference",
874
if (PyErr_Occurred())
881
pythonCharacters(void *user_data, const xmlChar * ch, int len)
884
PyObject *result = NULL;
888
printf("pythonCharacters(%s, %d) called\n", ch, len);
890
handler = (PyObject *) user_data;
891
if (PyObject_HasAttrString(handler, (char *) "characters"))
893
else if (PyObject_HasAttrString(handler, (char *) "data"))
897
result = PyObject_CallMethod(handler, (char *) "characters",
898
(char *) "s#", ch, len);
900
result = PyObject_CallMethod(handler, (char *) "data",
901
(char *) "s#", ch, len);
902
if (PyErr_Occurred())
909
pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
912
PyObject *result = NULL;
916
printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
918
handler = (PyObject *) user_data;
919
if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
921
else if (PyObject_HasAttrString(handler, (char *) "data"))
926
PyObject_CallMethod(handler,
927
(char *) "ignorableWhitespace",
928
(char *) "s#", ch, len);
931
PyObject_CallMethod(handler, (char *) "data",
932
(char *) "s#", ch, len);
938
pythonProcessingInstruction(void *user_data,
939
const xmlChar * target, const xmlChar * data)
945
printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
947
handler = (PyObject *) user_data;
948
if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
949
result = PyObject_CallMethod(handler, (char *)
950
"processingInstruction",
951
(char *) "ss", target, data);
957
pythonComment(void *user_data, const xmlChar * value)
963
printf("pythonComment(%s) called\n", value);
965
handler = (PyObject *) user_data;
966
if (PyObject_HasAttrString(handler, (char *) "comment")) {
968
PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
970
if (PyErr_Occurred())
977
pythonWarning(void *user_data, const char *msg, ...)
985
printf("pythonWarning(%s) called\n", msg);
987
handler = (PyObject *) user_data;
988
if (PyObject_HasAttrString(handler, (char *) "warning")) {
990
vsnprintf(buf, 1023, msg, args);
994
PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
996
if (PyErr_Occurred())
1003
pythonError(void *user_data, const char *msg, ...)
1011
printf("pythonError(%s) called\n", msg);
1013
handler = (PyObject *) user_data;
1014
if (PyObject_HasAttrString(handler, (char *) "error")) {
1015
va_start(args, msg);
1016
vsnprintf(buf, 1023, msg, args);
1020
PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1022
if (PyErr_Occurred())
1029
pythonFatalError(void *user_data, const char *msg, ...)
1037
printf("pythonFatalError(%s) called\n", msg);
1039
handler = (PyObject *) user_data;
1040
if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
1041
va_start(args, msg);
1042
vsnprintf(buf, 1023, msg, args);
1046
PyObject_CallMethod(handler, (char *) "fatalError",
1048
if (PyErr_Occurred())
1055
pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1058
PyObject *result = NULL;
1062
printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1064
handler = (PyObject *) user_data;
1065
if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1067
else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1072
PyObject_CallMethod(handler, (char *) "cdataBlock",
1073
(char *) "s#", ch, len);
1076
PyObject_CallMethod(handler, (char *) "cdata",
1077
(char *) "s#", ch, len);
1078
if (PyErr_Occurred())
1085
pythonExternalSubset(void *user_data,
1086
const xmlChar * name,
1087
const xmlChar * externalID, const xmlChar * systemID)
1093
printf("pythonExternalSubset(%s, %s, %s) called\n",
1094
name, externalID, systemID);
1096
handler = (PyObject *) user_data;
1097
if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
1099
PyObject_CallMethod(handler, (char *) "externalSubset",
1100
(char *) "sss", name, externalID,
1107
pythonEntityDecl(void *user_data,
1108
const xmlChar * name,
1110
const xmlChar * publicId,
1111
const xmlChar * systemId, xmlChar * content)
1116
handler = (PyObject *) user_data;
1117
if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1118
result = PyObject_CallMethod(handler, (char *) "entityDecl",
1119
(char *) "sisss", name, type,
1120
publicId, systemId, content);
1121
if (PyErr_Occurred())
1131
pythonNotationDecl(void *user_data,
1132
const xmlChar * name,
1133
const xmlChar * publicId, const xmlChar * systemId)
1138
handler = (PyObject *) user_data;
1139
if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1140
result = PyObject_CallMethod(handler, (char *) "notationDecl",
1141
(char *) "sss", name, publicId,
1143
if (PyErr_Occurred())
1150
pythonAttributeDecl(void *user_data,
1151
const xmlChar * elem,
1152
const xmlChar * name,
1155
const xmlChar * defaultValue, xmlEnumerationPtr tree)
1160
xmlEnumerationPtr node;
1164
handler = (PyObject *) user_data;
1165
if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
1167
for (node = tree; node != NULL; node = node->next) {
1170
nameList = PyList_New(count);
1172
for (node = tree; node != NULL; node = node->next) {
1173
newName = PyString_FromString((char *) node->name);
1174
PyList_SetItem(nameList, count, newName);
1178
result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1179
(char *) "ssiisO", elem, name, type,
1180
def, defaultValue, nameList);
1181
if (PyErr_Occurred())
1183
Py_XDECREF(nameList);
1189
pythonElementDecl(void *user_data,
1190
const xmlChar * name,
1191
int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
1197
handler = (PyObject *) user_data;
1198
if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1199
/* TODO: wrap in an elementContent object */
1201
("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1203
/* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1204
result = PyObject_CallMethod(handler, (char *) "elementDecl",
1205
(char *) "siO", name, type, obj);
1206
if (PyErr_Occurred())
1213
pythonUnparsedEntityDecl(void *user_data,
1214
const xmlChar * name,
1215
const xmlChar * publicId,
1216
const xmlChar * systemId,
1217
const xmlChar * notationName)
1222
handler = (PyObject *) user_data;
1223
if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1225
PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1226
(char *) "ssss", name, publicId, systemId,
1228
if (PyErr_Occurred())
1235
pythonInternalSubset(void *user_data, const xmlChar * name,
1236
const xmlChar * ExternalID, const xmlChar * SystemID)
1242
printf("pythonInternalSubset(%s, %s, %s) called\n",
1243
name, ExternalID, SystemID);
1245
handler = (PyObject *) user_data;
1246
if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1247
result = PyObject_CallMethod(handler, (char *) "internalSubset",
1248
(char *) "sss", name, ExternalID,
1250
if (PyErr_Occurred())
1256
static xmlSAXHandler pythonSaxHandler = {
1257
pythonInternalSubset,
1258
NULL, /* TODO pythonIsStandalone, */
1259
NULL, /* TODO pythonHasInternalSubset, */
1260
NULL, /* TODO pythonHasExternalSubset, */
1261
NULL, /* TODO pythonResolveEntity, */
1262
NULL, /* TODO pythonGetEntity, */
1265
pythonAttributeDecl,
1267
pythonUnparsedEntityDecl,
1268
NULL, /* OBSOLETED pythonSetDocumentLocator, */
1269
pythonStartDocument,
1275
pythonIgnorableWhitespace,
1276
pythonProcessingInstruction,
1281
NULL, /* TODO pythonGetParameterEntity, */
1283
pythonExternalSubset,
1285
NULL, /* TODO mograte to SAX2 */
1291
/************************************************************************
1293
* Handling of specific parser context *
1295
************************************************************************/
1298
libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1304
PyObject *pyobj_SAX = NULL;
1305
xmlSAXHandlerPtr SAX = NULL;
1306
xmlParserCtxtPtr ret;
1309
if (!PyArg_ParseTuple
1310
(args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1315
printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
1316
pyobj_SAX, chunk, size, URI);
1318
if (pyobj_SAX != Py_None) {
1319
SAX = &pythonSaxHandler;
1320
Py_INCREF(pyobj_SAX);
1321
/* The reference is released in pythonEndDocument() */
1323
ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
1324
pyret = libxml_xmlParserCtxtPtrWrap(ret);
1329
libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1332
#ifdef LIBXML_HTML_ENABLED
1336
PyObject *pyobj_SAX = NULL;
1337
xmlSAXHandlerPtr SAX = NULL;
1338
xmlParserCtxtPtr ret;
1341
if (!PyArg_ParseTuple
1342
(args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1347
printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
1348
pyobj_SAX, chunk, size, URI);
1350
if (pyobj_SAX != Py_None) {
1351
SAX = &pythonSaxHandler;
1352
Py_INCREF(pyobj_SAX);
1353
/* The reference is released in pythonEndDocument() */
1355
ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
1356
XML_CHAR_ENCODING_NONE);
1357
pyret = libxml_xmlParserCtxtPtrWrap(ret);
1362
#endif /* LIBXML_HTML_ENABLED */
1366
libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1370
PyObject *pyobj_SAX = NULL;
1371
xmlSAXHandlerPtr SAX = NULL;
1373
if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1378
printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
1379
pyobj_SAX, URI, recover);
1381
if (pyobj_SAX == Py_None) {
1385
SAX = &pythonSaxHandler;
1386
Py_INCREF(pyobj_SAX);
1387
/* The reference is released in pythonEndDocument() */
1388
xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
1394
libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1396
#ifdef LIBXML_HTML_ENABLED
1398
const char *encoding;
1399
PyObject *pyobj_SAX = NULL;
1400
xmlSAXHandlerPtr SAX = NULL;
1402
if (!PyArg_ParseTuple
1403
(args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1408
printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
1409
pyobj_SAX, URI, encoding);
1411
if (pyobj_SAX == Py_None) {
1415
SAX = &pythonSaxHandler;
1416
Py_INCREF(pyobj_SAX);
1417
/* The reference is released in pythonEndDocument() */
1418
htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1424
#endif /* LIBXML_HTML_ENABLED */
1427
/************************************************************************
1429
* Error message callback *
1431
************************************************************************/
1433
static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1434
static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1436
/* helper to build a xmlMalloc'ed string from a format and va_list */
1438
* disabled the loop, the repeated call to vsnprintf without reset of ap
1439
* in case the initial buffer was too small segfaulted on x86_64
1440
* we now directly vsnprintf on a large buffer.
1443
libxml_buildMessage(const char *msg, va_list ap)
1448
str = (char *) xmlMalloc(1000);
1452
chars = vsnprintf(str, 999, msg, ap);
1460
libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1470
printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1474
if (libxml_xmlPythonErrorFuncHandler == NULL) {
1476
vfprintf(stderr, msg, ap);
1480
if (vsnprintf(str, 999, msg, ap) >= 998)
1484
list = PyTuple_New(2);
1485
PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1486
Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1487
message = libxml_charPtrConstWrap(str);
1488
PyTuple_SetItem(list, 1, message);
1489
result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1496
libxml_xmlErrorInitialize(void)
1499
printf("libxml_xmlErrorInitialize() called\n");
1501
xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1502
xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1506
libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1509
PyObject *py_retval;
1511
PyObject *pyobj_ctx;
1513
if (!PyArg_ParseTuple
1514
(args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1519
printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
1523
if (libxml_xmlPythonErrorFuncHandler != NULL) {
1524
Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
1526
if (libxml_xmlPythonErrorFuncCtxt != NULL) {
1527
Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
1530
Py_XINCREF(pyobj_ctx);
1531
Py_XINCREF(pyobj_f);
1533
/* TODO: check f is a function ! */
1534
libxml_xmlPythonErrorFuncHandler = pyobj_f;
1535
libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1537
py_retval = libxml_intWrap(1);
1542
/************************************************************************
1544
* Per parserCtxt error handler *
1546
************************************************************************/
1552
} xmlParserCtxtPyCtxt;
1553
typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1556
libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1560
xmlParserCtxtPtr ctxt;
1561
xmlParserCtxtPyCtxtPtr pyCtxt;
1564
printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
1567
ctxt = (xmlParserCtxtPtr)ctx;
1568
pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1570
list = PyTuple_New(4);
1571
PyTuple_SetItem(list, 0, pyCtxt->arg);
1572
Py_XINCREF(pyCtxt->arg);
1573
PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1574
PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1575
PyTuple_SetItem(list, 3, Py_None);
1577
result = PyEval_CallObject(pyCtxt->f, list);
1580
/* TODO: manage for the exception to be propagated... */
1588
libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1593
libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1598
libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1603
libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1608
libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1613
libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1618
libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1623
libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1628
libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1630
PyObject *py_retval;
1631
xmlParserCtxtPtr ctxt;
1632
xmlParserCtxtPyCtxtPtr pyCtxt;
1633
PyObject *pyobj_ctxt;
1635
PyObject *pyobj_arg;
1637
if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
1638
&pyobj_ctxt, &pyobj_f, &pyobj_arg))
1640
ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1641
if (ctxt->_private == NULL) {
1642
pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1643
if (pyCtxt == NULL) {
1644
py_retval = libxml_intWrap(-1);
1647
memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1648
ctxt->_private = pyCtxt;
1651
pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1653
/* TODO: check f is a function ! */
1654
Py_XDECREF(pyCtxt->f);
1655
Py_XINCREF(pyobj_f);
1656
pyCtxt->f = pyobj_f;
1657
Py_XDECREF(pyCtxt->arg);
1658
Py_XINCREF(pyobj_arg);
1659
pyCtxt->arg = pyobj_arg;
1661
if (pyobj_f != Py_None) {
1662
ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1663
ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1664
ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1665
ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1668
ctxt->sax->error = xmlParserError;
1669
ctxt->vctxt.error = xmlParserValidityError;
1670
ctxt->sax->warning = xmlParserWarning;
1671
ctxt->vctxt.warning = xmlParserValidityWarning;
1674
py_retval = libxml_intWrap(1);
1679
libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1681
PyObject *py_retval;
1682
xmlParserCtxtPtr ctxt;
1683
xmlParserCtxtPyCtxtPtr pyCtxt;
1684
PyObject *pyobj_ctxt;
1686
if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1689
ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1690
py_retval = PyTuple_New(2);
1691
if (ctxt->_private != NULL) {
1692
pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1694
PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1695
Py_XINCREF(pyCtxt->f);
1696
PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1697
Py_XINCREF(pyCtxt->arg);
1700
/* no python error handler registered */
1701
PyTuple_SetItem(py_retval, 0, Py_None);
1702
Py_XINCREF(Py_None);
1703
PyTuple_SetItem(py_retval, 1, Py_None);
1704
Py_XINCREF(Py_None);
1710
libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1711
xmlParserCtxtPtr ctxt;
1712
PyObject *pyobj_ctxt;
1713
xmlParserCtxtPyCtxtPtr pyCtxt;
1715
if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1717
ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1720
pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1722
Py_XDECREF(pyCtxt->f);
1723
Py_XDECREF(pyCtxt->arg);
1726
xmlFreeParserCtxt(ctxt);
1734
* xmlValidCtxt stuff
1742
} xmlValidCtxtPyCtxt;
1743
typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1746
libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1750
xmlValidCtxtPyCtxtPtr pyCtxt;
1753
printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1756
pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1758
list = PyTuple_New(2);
1759
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1760
PyTuple_SetItem(list, 1, pyCtxt->arg);
1761
Py_XINCREF(pyCtxt->arg);
1762
result = PyEval_CallObject(pyCtxt->error, list);
1765
/* TODO: manage for the exception to be propagated... */
1773
libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
1777
xmlValidCtxtPyCtxtPtr pyCtxt;
1780
printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1783
pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1785
list = PyTuple_New(2);
1786
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1787
PyTuple_SetItem(list, 1, pyCtxt->arg);
1788
Py_XINCREF(pyCtxt->arg);
1789
result = PyEval_CallObject(pyCtxt->warn, list);
1792
/* TODO: manage for the exception to be propagated... */
1800
libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1805
libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1810
libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1815
libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1820
libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1822
PyObject *py_retval;
1823
PyObject *pyobj_error;
1824
PyObject *pyobj_warn;
1825
PyObject *pyobj_ctx;
1826
PyObject *pyobj_arg = Py_None;
1827
xmlValidCtxtPtr ctxt;
1828
xmlValidCtxtPyCtxtPtr pyCtxt;
1830
if (!PyArg_ParseTuple
1831
(args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1835
printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1838
ctxt = PyValidCtxt_Get(pyobj_ctx);
1839
pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1840
if (pyCtxt == NULL) {
1841
py_retval = libxml_intWrap(-1);
1844
memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
1847
/* TODO: check warn and error is a function ! */
1848
Py_XDECREF(pyCtxt->error);
1849
Py_XINCREF(pyobj_error);
1850
pyCtxt->error = pyobj_error;
1852
Py_XDECREF(pyCtxt->warn);
1853
Py_XINCREF(pyobj_warn);
1854
pyCtxt->warn = pyobj_warn;
1856
Py_XDECREF(pyCtxt->arg);
1857
Py_XINCREF(pyobj_arg);
1858
pyCtxt->arg = pyobj_arg;
1860
ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
1861
ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
1862
ctxt->userData = pyCtxt;
1864
py_retval = libxml_intWrap(1);
1870
libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1871
xmlValidCtxtPtr cur;
1872
xmlValidCtxtPyCtxtPtr pyCtxt;
1873
PyObject *pyobj_cur;
1875
if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
1877
cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
1879
pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
1882
Py_XDECREF(pyCtxt->error);
1883
Py_XDECREF(pyCtxt->warn);
1884
Py_XDECREF(pyCtxt->arg);
1888
xmlFreeValidCtxt(cur);
1893
/************************************************************************
1895
* Per xmlTextReader error handler *
1897
************************************************************************/
1903
} xmlTextReaderPyCtxt;
1904
typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1907
libxml_xmlTextReaderErrorCallback(void *arg,
1910
xmlTextReaderLocatorPtr locator)
1912
xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1916
list = PyTuple_New(4);
1917
PyTuple_SetItem(list, 0, pyCtxt->arg);
1918
Py_XINCREF(pyCtxt->arg);
1919
PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
1920
PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1921
PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
1922
result = PyEval_CallObject(pyCtxt->f, list);
1925
/* TODO: manage for the exception to be propagated... */
1933
libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1935
xmlTextReaderPtr reader;
1936
xmlTextReaderPyCtxtPtr pyCtxt;
1937
xmlTextReaderErrorFunc f;
1939
PyObject *pyobj_reader;
1941
PyObject *pyobj_arg;
1942
PyObject *py_retval;
1944
if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1946
reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1947
/* clear previous error handler */
1948
xmlTextReaderGetErrorHandler(reader,&f,&arg);
1950
if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
1951
/* ok, it's our error handler! */
1952
pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1953
Py_XDECREF(pyCtxt->f);
1954
Py_XDECREF(pyCtxt->arg);
1959
* there already an arg, and it's not ours,
1960
* there is definitely something wrong going on here...
1961
* we don't know how to free it, so we bail out...
1963
py_retval = libxml_intWrap(-1);
1967
xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1968
/* set new error handler */
1969
if (pyobj_f != Py_None)
1971
pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1972
if (pyCtxt == NULL) {
1973
py_retval = libxml_intWrap(-1);
1976
Py_XINCREF(pyobj_f);
1977
pyCtxt->f = pyobj_f;
1978
Py_XINCREF(pyobj_arg);
1979
pyCtxt->arg = pyobj_arg;
1980
xmlTextReaderSetErrorHandler(reader,
1981
(xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
1985
py_retval = libxml_intWrap(1);
1990
libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1992
xmlTextReaderPtr reader;
1993
xmlTextReaderPyCtxtPtr pyCtxt;
1994
xmlTextReaderErrorFunc f;
1996
PyObject *pyobj_reader;
1997
PyObject *py_retval;
1999
if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
2001
reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2002
xmlTextReaderGetErrorHandler(reader,&f,&arg);
2003
py_retval = PyTuple_New(2);
2004
if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
2005
/* ok, it's our error handler! */
2006
pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2007
PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2008
Py_XINCREF(pyCtxt->f);
2009
PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2010
Py_XINCREF(pyCtxt->arg);
2014
/* f is null or it's not our error handler */
2015
PyTuple_SetItem(py_retval, 0, Py_None);
2016
Py_XINCREF(Py_None);
2017
PyTuple_SetItem(py_retval, 1, Py_None);
2018
Py_XINCREF(Py_None);
2024
libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2025
xmlTextReaderPtr reader;
2026
PyObject *pyobj_reader;
2027
xmlTextReaderPyCtxtPtr pyCtxt;
2028
xmlTextReaderErrorFunc f;
2031
if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2033
if (!PyCObject_Check(pyobj_reader)) {
2037
reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2038
if (reader == NULL) {
2043
xmlTextReaderGetErrorHandler(reader,&f,&arg);
2045
if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
2046
/* ok, it's our error handler! */
2047
pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2048
Py_XDECREF(pyCtxt->f);
2049
Py_XDECREF(pyCtxt->arg);
2053
* else, something wrong happened, because the error handler is
2054
* not owned by the python bindings...
2058
xmlFreeTextReader(reader);
2063
/************************************************************************
2065
* XPath extensions *
2067
************************************************************************/
2070
libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2072
PyObject *list, *cur, *result;
2073
xmlXPathObjectPtr obj;
2074
xmlXPathContextPtr rctxt;
2075
PyObject *current_function = NULL;
2076
const xmlChar *name;
2077
const xmlChar *ns_uri;
2082
rctxt = ctxt->context;
2085
name = rctxt->function;
2086
ns_uri = rctxt->functionURI;
2088
printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2093
* Find the function, it should be there it was there at lookup
2095
for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2096
if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
2097
(xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2098
(xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2099
current_function = (*libxml_xpathCallbacks)[i].function;
2102
if (current_function == NULL) {
2104
("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2109
list = PyTuple_New(nargs + 1);
2110
PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
2111
for (i = nargs - 1; i >= 0; i--) {
2112
obj = valuePop(ctxt);
2113
cur = libxml_xmlXPathObjectPtrWrap(obj);
2114
PyTuple_SetItem(list, i + 1, cur);
2116
result = PyEval_CallObject(current_function, list);
2119
obj = libxml_xmlXPathObjectPtrConvert(result);
2120
valuePush(ctxt, obj);
2123
static xmlXPathFunction
2124
libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2125
const xmlChar * ns_uri)
2130
printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
2131
ctxt, name, ns_uri);
2134
* This is called once only. The address is then stored in the
2135
* XPath expression evaluation, the proper object to call can
2136
* then still be found using the execution context function
2137
* and functionURI fields.
2139
for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2140
if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2141
(xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2142
(xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2143
return (libxml_xmlXPathFuncCallback);
2150
libxml_xpathCallbacksInitialize(void)
2154
if (libxml_xpathCallbacksInitialized != 0)
2158
printf("libxml_xpathCallbacksInitialized called\n");
2160
libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2161
libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2163
for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2164
(*libxml_xpathCallbacks)[i].ctx = NULL;
2165
(*libxml_xpathCallbacks)[i].name = NULL;
2166
(*libxml_xpathCallbacks)[i].ns_uri = NULL;
2167
(*libxml_xpathCallbacks)[i].function = NULL;
2169
libxml_xpathCallbacksInitialized = 1;
2173
libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2176
PyObject *py_retval;
2180
xmlXPathContextPtr ctx;
2181
PyObject *pyobj_ctx;
2185
if (!PyArg_ParseTuple
2186
(args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2190
ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2191
if (libxml_xpathCallbacksInitialized == 0)
2192
libxml_xpathCallbacksInitialize();
2193
xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2195
if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
2196
py_retval = libxml_intWrap(-1);
2200
printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
2203
for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2204
if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2205
(xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2206
(xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2207
Py_XINCREF(pyobj_f);
2208
Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2209
(*libxml_xpathCallbacks)[i].function = pyobj_f;
2214
if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2215
libxml_xpathCallbacksAllocd+=10;
2216
libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2217
libxml_xpathCallbacks,
2218
libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2220
i = libxml_xpathCallbacksNb++;
2221
Py_XINCREF(pyobj_f);
2222
(*libxml_xpathCallbacks)[i].ctx = ctx;
2223
(*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2224
(*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2225
(*libxml_xpathCallbacks)[i].function = pyobj_f;
2229
py_retval = libxml_intWrap((int) c_retval);
2233
/************************************************************************
2235
* Global properties access *
2237
************************************************************************/
2239
libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2241
PyObject *resultobj, *obj;
2245
if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
2247
cur = PyxmlNode_Get(obj);
2250
printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2253
switch (cur->type) {
2254
case XML_DOCUMENT_NODE:
2255
#ifdef LIBXML_DOCB_ENABLED
2256
case XML_DOCB_DOCUMENT_NODE:
2258
case XML_HTML_DOCUMENT_NODE:{
2259
xmlDocPtr doc = (xmlDocPtr) cur;
2264
case XML_ATTRIBUTE_NODE:{
2265
xmlAttrPtr attr = (xmlAttrPtr) cur;
2270
case XML_NAMESPACE_DECL:{
2271
xmlNsPtr ns = (xmlNsPtr) cur;
2280
resultobj = libxml_constxmlCharPtrWrap(res);
2286
libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2288
PyObject *resultobj, *obj;
2292
if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
2294
cur = PyxmlNode_Get(obj);
2297
printf("libxml_doc: cur = %p\n", cur);
2300
switch (cur->type) {
2301
case XML_DOCUMENT_NODE:
2302
#ifdef LIBXML_DOCB_ENABLED
2303
case XML_DOCB_DOCUMENT_NODE:
2305
case XML_HTML_DOCUMENT_NODE:
2308
case XML_ATTRIBUTE_NODE:{
2309
xmlAttrPtr attr = (xmlAttrPtr) cur;
2314
case XML_NAMESPACE_DECL:
2321
resultobj = libxml_xmlDocPtrWrap(res);
2326
libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2328
PyObject *resultobj, *obj;
2332
if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
2334
cur = PyxmlNode_Get(obj);
2335
if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
2336
res = cur->properties;
2339
resultobj = libxml_xmlAttrPtrWrap(res);
2344
libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2346
PyObject *resultobj, *obj;
2350
if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
2352
cur = PyxmlNode_Get(obj);
2355
printf("libxml_next: cur = %p\n", cur);
2358
switch (cur->type) {
2359
case XML_DOCUMENT_NODE:
2360
#ifdef LIBXML_DOCB_ENABLED
2361
case XML_DOCB_DOCUMENT_NODE:
2363
case XML_HTML_DOCUMENT_NODE:
2366
case XML_ATTRIBUTE_NODE:{
2367
xmlAttrPtr attr = (xmlAttrPtr) cur;
2369
res = (xmlNodePtr) attr->next;
2372
case XML_NAMESPACE_DECL:{
2373
xmlNsPtr ns = (xmlNsPtr) cur;
2375
res = (xmlNodePtr) ns->next;
2383
resultobj = libxml_xmlNodePtrWrap(res);
2388
libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2390
PyObject *resultobj, *obj;
2394
if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
2396
cur = PyxmlNode_Get(obj);
2399
printf("libxml_prev: cur = %p\n", cur);
2402
switch (cur->type) {
2403
case XML_DOCUMENT_NODE:
2404
#ifdef LIBXML_DOCB_ENABLED
2405
case XML_DOCB_DOCUMENT_NODE:
2407
case XML_HTML_DOCUMENT_NODE:
2410
case XML_ATTRIBUTE_NODE:{
2411
xmlAttrPtr attr = (xmlAttrPtr) cur;
2413
res = (xmlNodePtr) attr->prev;
2415
case XML_NAMESPACE_DECL:
2422
resultobj = libxml_xmlNodePtrWrap(res);
2427
libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2429
PyObject *resultobj, *obj;
2433
if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
2435
cur = PyxmlNode_Get(obj);
2438
printf("libxml_children: cur = %p\n", cur);
2441
switch (cur->type) {
2442
case XML_ELEMENT_NODE:
2443
case XML_ENTITY_REF_NODE:
2444
case XML_ENTITY_NODE:
2446
case XML_COMMENT_NODE:
2447
case XML_DOCUMENT_NODE:
2448
#ifdef LIBXML_DOCB_ENABLED
2449
case XML_DOCB_DOCUMENT_NODE:
2451
case XML_HTML_DOCUMENT_NODE:
2453
res = cur->children;
2455
case XML_ATTRIBUTE_NODE:{
2456
xmlAttrPtr attr = (xmlAttrPtr) cur;
2458
res = attr->children;
2465
resultobj = libxml_xmlNodePtrWrap(res);
2470
libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2472
PyObject *resultobj, *obj;
2476
if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2478
cur = PyxmlNode_Get(obj);
2481
printf("libxml_last: cur = %p\n", cur);
2484
switch (cur->type) {
2485
case XML_ELEMENT_NODE:
2486
case XML_ENTITY_REF_NODE:
2487
case XML_ENTITY_NODE:
2489
case XML_COMMENT_NODE:
2490
case XML_DOCUMENT_NODE:
2491
#ifdef LIBXML_DOCB_ENABLED
2492
case XML_DOCB_DOCUMENT_NODE:
2494
case XML_HTML_DOCUMENT_NODE:
2498
case XML_ATTRIBUTE_NODE:{
2499
xmlAttrPtr attr = (xmlAttrPtr) cur;
2507
resultobj = libxml_xmlNodePtrWrap(res);
2512
libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2514
PyObject *resultobj, *obj;
2518
if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
2520
cur = PyxmlNode_Get(obj);
2523
printf("libxml_parent: cur = %p\n", cur);
2526
switch (cur->type) {
2527
case XML_DOCUMENT_NODE:
2528
case XML_HTML_DOCUMENT_NODE:
2529
#ifdef LIBXML_DOCB_ENABLED
2530
case XML_DOCB_DOCUMENT_NODE:
2534
case XML_ATTRIBUTE_NODE:{
2535
xmlAttrPtr attr = (xmlAttrPtr) cur;
2540
case XML_ENTITY_DECL:
2541
case XML_NAMESPACE_DECL:
2542
case XML_XINCLUDE_START:
2543
case XML_XINCLUDE_END:
2550
resultobj = libxml_xmlNodePtrWrap(res);
2555
libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2557
PyObject *resultobj, *obj;
2559
const xmlChar *res = NULL;
2561
if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2563
cur = PyxmlNode_Get(obj);
2566
printf("libxml_type: cur = %p\n", cur);
2569
switch (cur->type) {
2570
case XML_ELEMENT_NODE:
2571
res = (const xmlChar *) "element";
2573
case XML_ATTRIBUTE_NODE:
2574
res = (const xmlChar *) "attribute";
2577
res = (const xmlChar *) "text";
2579
case XML_CDATA_SECTION_NODE:
2580
res = (const xmlChar *) "cdata";
2582
case XML_ENTITY_REF_NODE:
2583
res = (const xmlChar *) "entity_ref";
2585
case XML_ENTITY_NODE:
2586
res = (const xmlChar *) "entity";
2589
res = (const xmlChar *) "pi";
2591
case XML_COMMENT_NODE:
2592
res = (const xmlChar *) "comment";
2594
case XML_DOCUMENT_NODE:
2595
res = (const xmlChar *) "document_xml";
2597
case XML_DOCUMENT_TYPE_NODE:
2598
res = (const xmlChar *) "doctype";
2600
case XML_DOCUMENT_FRAG_NODE:
2601
res = (const xmlChar *) "fragment";
2603
case XML_NOTATION_NODE:
2604
res = (const xmlChar *) "notation";
2606
case XML_HTML_DOCUMENT_NODE:
2607
res = (const xmlChar *) "document_html";
2610
res = (const xmlChar *) "dtd";
2612
case XML_ELEMENT_DECL:
2613
res = (const xmlChar *) "elem_decl";
2615
case XML_ATTRIBUTE_DECL:
2616
res = (const xmlChar *) "attribute_decl";
2618
case XML_ENTITY_DECL:
2619
res = (const xmlChar *) "entity_decl";
2621
case XML_NAMESPACE_DECL:
2622
res = (const xmlChar *) "namespace";
2624
case XML_XINCLUDE_START:
2625
res = (const xmlChar *) "xinclude_start";
2627
case XML_XINCLUDE_END:
2628
res = (const xmlChar *) "xinclude_end";
2630
#ifdef LIBXML_DOCB_ENABLED
2631
case XML_DOCB_DOCUMENT_NODE:
2632
res = (const xmlChar *) "document_docbook";
2637
printf("libxml_type: cur = %p: %s\n", cur, res);
2640
resultobj = libxml_constxmlCharPtrWrap(res);
2644
/************************************************************************
2646
* Specific accessor functions *
2648
************************************************************************/
2650
libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2652
PyObject *py_retval;
2655
PyObject *pyobj_node;
2657
if (!PyArg_ParseTuple
2658
(args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2660
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2662
if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2666
c_retval = node->nsDef;
2667
py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2672
libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2674
PyObject *py_retval;
2677
PyObject *pyobj_node;
2681
if (!PyArg_ParseTuple
2682
(args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
2684
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2687
if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2700
while (ns != NULL) {
2701
if (xmlStrEqual(ns->href, href)) {
2703
prev->next = ns->next;
2705
node->nsDef = ns->next;
2716
py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2721
libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2723
PyObject *py_retval;
2726
PyObject *pyobj_node;
2728
if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2730
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2732
if ((node == NULL) ||
2733
((node->type != XML_ELEMENT_NODE) &&
2734
(node->type != XML_ATTRIBUTE_NODE))) {
2738
c_retval = node->ns;
2739
py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2743
#ifdef LIBXML_OUTPUT_ENABLED
2744
/************************************************************************
2746
* Serialization front-end *
2748
************************************************************************/
2751
libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2753
PyObject *py_retval = NULL;
2755
PyObject *pyobj_node;
2758
const char *encoding;
2762
if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2763
&encoding, &format))
2765
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2771
if (node->type == XML_DOCUMENT_NODE) {
2772
doc = (xmlDocPtr) node;
2773
xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2774
(const char *) encoding, format);
2775
py_retval = libxml_charPtrWrap((char *) c_retval);
2776
#ifdef LIBXML_HTML_ENABLED
2777
} else if (node->type == XML_HTML_DOCUMENT_NODE) {
2778
xmlOutputBufferPtr buf;
2779
xmlCharEncodingHandlerPtr handler = NULL;
2781
doc = (xmlDocPtr) node;
2782
if (encoding != NULL)
2783
htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2784
encoding = (const char *) htmlGetMetaEncoding(doc);
2786
if (encoding != NULL) {
2787
handler = xmlFindCharEncodingHandler(encoding);
2788
if (handler == NULL) {
2795
* Fallback to HTML or ASCII when the encoding is unspecified
2797
if (handler == NULL)
2798
handler = xmlFindCharEncodingHandler("HTML");
2799
if (handler == NULL)
2800
handler = xmlFindCharEncodingHandler("ascii");
2802
buf = xmlAllocOutputBuffer(handler);
2807
htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2808
xmlOutputBufferFlush(buf);
2809
if (buf->conv != NULL) {
2810
len = buf->conv->use;
2811
c_retval = buf->conv->content;
2812
buf->conv->content = NULL;
2814
len = buf->buffer->use;
2815
c_retval = buf->buffer->content;
2816
buf->buffer->content = NULL;
2818
(void) xmlOutputBufferClose(buf);
2819
py_retval = libxml_charPtrWrap((char *) c_retval);
2820
#endif /* LIBXML_HTML_ENABLED */
2822
if (node->type == XML_NAMESPACE_DECL)
2826
if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
2827
xmlOutputBufferPtr buf;
2828
xmlCharEncodingHandlerPtr handler = NULL;
2830
if (encoding != NULL) {
2831
handler = xmlFindCharEncodingHandler(encoding);
2832
if (handler == NULL) {
2838
buf = xmlAllocOutputBuffer(handler);
2843
xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2844
xmlOutputBufferFlush(buf);
2845
if (buf->conv != NULL) {
2846
len = buf->conv->use;
2847
c_retval = buf->conv->content;
2848
buf->conv->content = NULL;
2850
len = buf->buffer->use;
2851
c_retval = buf->buffer->content;
2852
buf->buffer->content = NULL;
2854
(void) xmlOutputBufferClose(buf);
2855
py_retval = libxml_charPtrWrap((char *) c_retval);
2856
#ifdef LIBXML_HTML_ENABLED
2857
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2858
xmlOutputBufferPtr buf;
2859
xmlCharEncodingHandlerPtr handler = NULL;
2861
if (encoding != NULL)
2862
htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2863
encoding = (const char *) htmlGetMetaEncoding(doc);
2864
if (encoding != NULL) {
2865
handler = xmlFindCharEncodingHandler(encoding);
2866
if (handler == NULL) {
2873
* Fallback to HTML or ASCII when the encoding is unspecified
2875
if (handler == NULL)
2876
handler = xmlFindCharEncodingHandler("HTML");
2877
if (handler == NULL)
2878
handler = xmlFindCharEncodingHandler("ascii");
2880
buf = xmlAllocOutputBuffer(handler);
2885
htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2886
xmlOutputBufferFlush(buf);
2887
if (buf->conv != NULL) {
2888
len = buf->conv->use;
2889
c_retval = buf->conv->content;
2890
buf->conv->content = NULL;
2892
len = buf->buffer->use;
2893
c_retval = buf->buffer->content;
2894
buf->buffer->content = NULL;
2896
(void) xmlOutputBufferClose(buf);
2897
py_retval = libxml_charPtrWrap((char *) c_retval);
2898
#endif /* LIBXML_HTML_ENABLED */
2908
libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2910
PyObject *py_file = NULL;
2912
PyObject *pyobj_node;
2915
const char *encoding;
2918
xmlOutputBufferPtr buf;
2919
xmlCharEncodingHandlerPtr handler = NULL;
2921
if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2922
&py_file, &encoding, &format))
2924
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2927
return (PyInt_FromLong((long) -1));
2929
if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
2930
return (PyInt_FromLong((long) -1));
2932
output = PyFile_AsFile(py_file);
2933
if (output == NULL) {
2934
return (PyInt_FromLong((long) -1));
2937
if (node->type == XML_DOCUMENT_NODE) {
2938
doc = (xmlDocPtr) node;
2939
} else if (node->type == XML_HTML_DOCUMENT_NODE) {
2940
doc = (xmlDocPtr) node;
2944
#ifdef LIBXML_HTML_ENABLED
2945
if (doc->type == XML_HTML_DOCUMENT_NODE) {
2946
if (encoding == NULL)
2947
encoding = (const char *) htmlGetMetaEncoding(doc);
2949
#endif /* LIBXML_HTML_ENABLED */
2950
if (encoding != NULL) {
2951
handler = xmlFindCharEncodingHandler(encoding);
2952
if (handler == NULL) {
2953
return (PyInt_FromLong((long) -1));
2956
if (doc->type == XML_HTML_DOCUMENT_NODE) {
2957
if (handler == NULL)
2958
handler = xmlFindCharEncodingHandler("HTML");
2959
if (handler == NULL)
2960
handler = xmlFindCharEncodingHandler("ascii");
2963
buf = xmlOutputBufferCreateFile(output, handler);
2964
if (node->type == XML_DOCUMENT_NODE) {
2965
len = xmlSaveFormatFileTo(buf, doc, encoding, format);
2966
#ifdef LIBXML_HTML_ENABLED
2967
} else if (node->type == XML_HTML_DOCUMENT_NODE) {
2968
htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2969
len = xmlOutputBufferClose(buf);
2970
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2971
htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2972
len = xmlOutputBufferClose(buf);
2973
#endif /* LIBXML_HTML_ENABLED */
2975
xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2976
len = xmlOutputBufferClose(buf);
2978
return (PyInt_FromLong((long) len));
2980
#endif /* LIBXML_OUTPUT_ENABLED */
2982
/************************************************************************
2986
************************************************************************/
2988
libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2990
PyObject *py_retval;
2994
if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2996
node = (xmlNodePtr) xmlNewNode(NULL, name);
2998
printf("NewNode: %s : %p\n", name, (void *) node);
3005
py_retval = libxml_xmlNodePtrWrap(node);
3010
/************************************************************************
3012
* Local Catalog stuff *
3014
************************************************************************/
3016
libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3019
xmlParserCtxtPtr ctxt;
3020
PyObject *pyobj_ctxt;
3022
if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
3025
ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
3028
ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3032
printf("LocalCatalog: %s\n", URL);
3039
#ifdef LIBXML_SCHEMAS_ENABLED
3041
/************************************************************************
3043
* RelaxNG error handler registration *
3045
************************************************************************/
3052
} xmlRelaxNGValidCtxtPyCtxt;
3053
typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
3056
libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3060
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3063
printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3066
pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3068
list = PyTuple_New(2);
3069
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3070
PyTuple_SetItem(list, 1, pyCtxt->arg);
3071
Py_XINCREF(pyCtxt->arg);
3072
result = PyEval_CallObject(pyCtxt->error, list);
3075
/* TODO: manage for the exception to be propagated... */
3083
libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3087
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3090
printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3093
pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3095
list = PyTuple_New(2);
3096
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3097
PyTuple_SetItem(list, 1, pyCtxt->arg);
3098
Py_XINCREF(pyCtxt->arg);
3099
result = PyEval_CallObject(pyCtxt->warn, list);
3102
/* TODO: manage for the exception to be propagated... */
3110
libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3115
libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3120
libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3125
libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3130
libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3132
PyObject *py_retval;
3133
PyObject *pyobj_error;
3134
PyObject *pyobj_warn;
3135
PyObject *pyobj_ctx;
3136
PyObject *pyobj_arg = Py_None;
3137
xmlRelaxNGValidCtxtPtr ctxt;
3138
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3140
if (!PyArg_ParseTuple
3141
(args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3145
printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3148
ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
3149
if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3151
py_retval = libxml_intWrap(-1);
3157
/* first time to set the error handlers */
3158
pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3159
if (pyCtxt == NULL) {
3160
py_retval = libxml_intWrap(-1);
3163
memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3166
/* TODO: check warn and error is a function ! */
3167
Py_XDECREF(pyCtxt->error);
3168
Py_XINCREF(pyobj_error);
3169
pyCtxt->error = pyobj_error;
3171
Py_XDECREF(pyCtxt->warn);
3172
Py_XINCREF(pyobj_warn);
3173
pyCtxt->warn = pyobj_warn;
3175
Py_XDECREF(pyCtxt->arg);
3176
Py_XINCREF(pyobj_arg);
3177
pyCtxt->arg = pyobj_arg;
3179
xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3181
py_retval = libxml_intWrap(1);
3186
libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3187
xmlRelaxNGValidCtxtPtr ctxt;
3188
xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3189
PyObject *pyobj_ctxt;
3191
if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3193
ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3195
if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3199
Py_XDECREF(pyCtxt->error);
3200
Py_XDECREF(pyCtxt->warn);
3201
Py_XDECREF(pyCtxt->arg);
3206
xmlRelaxNGFreeValidCtxt(ctxt);
3216
} xmlSchemaValidCtxtPyCtxt;
3217
typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3220
libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3224
xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3227
printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3230
pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3232
list = PyTuple_New(2);
3233
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3234
PyTuple_SetItem(list, 1, pyCtxt->arg);
3235
Py_XINCREF(pyCtxt->arg);
3236
result = PyEval_CallObject(pyCtxt->error, list);
3239
/* TODO: manage for the exception to be propagated... */
3247
libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3251
xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3254
printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3257
pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3259
list = PyTuple_New(2);
3260
PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3261
PyTuple_SetItem(list, 1, pyCtxt->arg);
3262
Py_XINCREF(pyCtxt->arg);
3263
result = PyEval_CallObject(pyCtxt->warn, list);
3266
/* TODO: manage for the exception to be propagated... */
3274
libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3279
libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3284
libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3289
libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3294
libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3296
PyObject *py_retval;
3297
PyObject *pyobj_error;
3298
PyObject *pyobj_warn;
3299
PyObject *pyobj_ctx;
3300
PyObject *pyobj_arg = Py_None;
3301
xmlSchemaValidCtxtPtr ctxt;
3302
xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3304
if (!PyArg_ParseTuple
3305
(args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3309
printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3312
ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3313
if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3315
py_retval = libxml_intWrap(-1);
3321
/* first time to set the error handlers */
3322
pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3323
if (pyCtxt == NULL) {
3324
py_retval = libxml_intWrap(-1);
3327
memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3330
/* TODO: check warn and error is a function ! */
3331
Py_XDECREF(pyCtxt->error);
3332
Py_XINCREF(pyobj_error);
3333
pyCtxt->error = pyobj_error;
3335
Py_XDECREF(pyCtxt->warn);
3336
Py_XINCREF(pyobj_warn);
3337
pyCtxt->warn = pyobj_warn;
3339
Py_XDECREF(pyCtxt->arg);
3340
Py_XINCREF(pyobj_arg);
3341
pyCtxt->arg = pyobj_arg;
3343
xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3345
py_retval = libxml_intWrap(1);
3350
libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3352
xmlSchemaValidCtxtPtr ctxt;
3353
xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3354
PyObject *pyobj_ctxt;
3356
if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3358
ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3360
if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3364
Py_XDECREF(pyCtxt->error);
3365
Py_XDECREF(pyCtxt->warn);
3366
Py_XDECREF(pyCtxt->arg);
3371
xmlSchemaFreeValidCtxt(ctxt);
3378
#ifdef LIBXML_C14N_ENABLED
3379
#ifdef LIBXML_OUTPUT_ENABLED
3381
/************************************************************************
3383
* XML Canonicalization c14n *
3385
************************************************************************/
3388
PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3390
xmlNodeSetPtr nodeSet;
3393
if (PyTuple_Check(py_nodeset))
3395
else if (PyList_Check(py_nodeset))
3397
else if (py_nodeset == Py_None) {
3402
PyErr_SetString(PyExc_TypeError,
3403
"must be a tuple or list of nodes.");
3407
nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3408
if (nodeSet == NULL) {
3409
PyErr_SetString(PyExc_MemoryError, "");
3413
nodeSet->nodeNr = 0;
3414
nodeSet->nodeMax = (is_tuple
3415
? PyTuple_GET_SIZE(py_nodeset)
3416
: PyList_GET_SIZE(py_nodeset));
3418
= (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3419
* sizeof(xmlNodePtr));
3420
if (nodeSet->nodeTab == NULL) {
3422
PyErr_SetString(PyExc_MemoryError, "");
3425
memset(nodeSet->nodeTab, 0 ,
3426
nodeSet->nodeMax * sizeof(xmlNodePtr));
3430
for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3432
PyxmlNode_Get (is_tuple
3433
? PyTuple_GET_ITEM(py_nodeset, idx)
3434
: PyList_GET_ITEM(py_nodeset, idx));
3436
nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3444
PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3446
/* NOTE: the array should be freed, but the strings are shared
3447
with the python strings and so must not be freed. */
3454
if (PyTuple_Check(py_strings))
3456
else if (PyList_Check(py_strings))
3458
else if (py_strings == Py_None) {
3463
PyErr_SetString(PyExc_TypeError,
3464
"must be a tuple or list of strings.");
3469
? PyTuple_GET_SIZE(py_strings)
3470
: PyList_GET_SIZE(py_strings));
3472
strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3474
if (strings == NULL) {
3475
PyErr_SetString(PyExc_MemoryError, "");
3479
memset(strings, 0 , sizeof(xmlChar *) * count);
3483
for (idx=0; idx < count; ++idx) {
3484
char* s = PyString_AsString
3486
? PyTuple_GET_ITEM(py_strings, idx)
3487
: PyList_GET_ITEM(py_strings, idx));
3489
strings[init_index++] = (xmlChar *)s;
3492
PyErr_SetString(PyExc_TypeError,
3493
"must be a tuple or list of strings.");
3504
libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3507
PyObject *py_retval = NULL;
3509
PyObject *pyobj_doc;
3510
PyObject *pyobj_nodes;
3512
PyObject *pyobj_prefixes;
3516
xmlNodeSetPtr nodes;
3517
xmlChar **prefixes = NULL;
3522
if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3530
doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3532
PyErr_SetString(PyExc_TypeError, "bad document.");
3536
result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3537
if (result < 0) return NULL;
3540
result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3543
xmlFree(nodes->nodeTab);
3550
result = xmlC14NDocDumpMemory(doc,
3558
xmlFree(nodes->nodeTab);
3562
xmlChar ** idx = prefixes;
3563
while (*idx) xmlFree(*(idx++));
3568
PyErr_SetString(PyExc_Exception,
3569
"libxml2 xmlC14NDocDumpMemory failure.");
3573
py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3581
libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3584
PyObject *pyobj_doc;
3586
PyObject *pyobj_nodes;
3588
PyObject *pyobj_prefixes;
3592
xmlNodeSetPtr nodes;
3593
xmlChar **prefixes = NULL;
3595
xmlOutputBufferPtr buf;
3600
if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3609
doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3611
PyErr_SetString(PyExc_TypeError, "bad document.");
3615
if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3616
PyErr_SetString(PyExc_TypeError, "bad file.");
3619
output = PyFile_AsFile(py_file);
3620
if (output == NULL) {
3621
PyErr_SetString(PyExc_TypeError, "bad file.");
3624
buf = xmlOutputBufferCreateFile(output, NULL);
3626
result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3627
if (result < 0) return NULL;
3630
result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3633
xmlFree(nodes->nodeTab);
3640
result = xmlC14NDocSaveTo(doc,
3648
xmlFree(nodes->nodeTab);
3652
xmlChar ** idx = prefixes;
3653
while (*idx) xmlFree(*(idx++));
3657
len = xmlOutputBufferClose(buf);
3660
PyErr_SetString(PyExc_Exception,
3661
"libxml2 xmlC14NDocSaveTo failure.");
3665
return PyInt_FromLong((long) len);
3672
libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3677
if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3679
str = PyCObject_GetDesc(obj);
3680
return Py_BuildValue((char *)"s", str);
3684
libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3686
PyObject *py_node1, *py_node2;
3687
xmlNodePtr node1, node2;
3689
if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
3690
&py_node1, &py_node2))
3692
/* To compare two node objects, we compare their pointer addresses */
3693
node1 = PyxmlNode_Get(py_node1);
3694
node2 = PyxmlNode_Get(py_node2);
3695
if ( node1 == node2 )
3696
return Py_BuildValue((char *)"i", 1);
3698
return Py_BuildValue((char *)"i", 0);
3703
libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3708
if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
3710
/* For simplicity, we use the node pointer address as a hash value */
3711
node1 = PyxmlNode_Get(py_node1);
3713
return PyLong_FromVoidPtr(node1);
3717
/************************************************************************
3719
* The registration stuff *
3721
************************************************************************/
3722
static PyMethodDef libxmlMethods[] = {
3723
#include "libxml2-export.c"
3724
{(char *) "name", libxml_name, METH_VARARGS, NULL},
3725
{(char *) "children", libxml_children, METH_VARARGS, NULL},
3726
{(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3727
{(char *) "last", libxml_last, METH_VARARGS, NULL},
3728
{(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3729
{(char *) "next", libxml_next, METH_VARARGS, NULL},
3730
{(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3731
{(char *) "type", libxml_type, METH_VARARGS, NULL},
3732
{(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3733
{(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
3734
{(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
3735
{(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
3736
{(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
3737
#ifdef LIBXML_OUTPUT_ENABLED
3738
{(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3739
{(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
3740
{(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
3741
{(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3742
{(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3743
{ (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
3744
{ (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3745
{ (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
3746
#endif /* LIBXML_OUTPUT_ENABLED */
3747
{(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3748
{(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
3749
{(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
3750
{(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3751
{(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
3752
{(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
3753
{(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3754
{(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3755
{(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
3756
{(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
3757
#ifdef LIBXML_SCHEMAS_ENABLED
3758
{(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3759
{(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
3760
{(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
3761
{(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
3763
#ifdef LIBXML_C14N_ENABLED
3764
#ifdef LIBXML_OUTPUT_ENABLED
3765
{(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3766
{(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3769
{(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
3770
{(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
3771
{(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
3772
{NULL, NULL, 0, NULL}
3775
#ifdef MERGED_MODULES
3776
extern void initlibxsltmod(void);
3780
initlibxml2mod(void)
3782
static int initialized = 0;
3784
if (initialized != 0)
3787
/* intialize the python extension module */
3788
Py_InitModule((char *) "libxml2mod", libxmlMethods);
3790
/* initialize libxml2 */
3792
libxml_xmlErrorInitialize();
3796
#ifdef MERGED_MODULES