blob: ab5ca1841808e9403ad449c77155c8a4e47fd7a6 [file] [log] [blame]
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001#include "Python.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002#include "compile.h"
3#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00004#ifdef HAVE_EXPAT_H
5#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00006#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +00007#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
8 + 0x100 * XML_MINOR_VERSION \
9 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010#else
Fred Drake85d835f2001-02-08 15:39:08 +000011/* Assume the oldest Expat that used expat.h and did not have version info */
12#define EXPAT_VERSION 0x015f00
13#endif
14#else /* !defined(HAVE_EXPAT_H) */
15#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000016/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000017#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000018#define EXPAT_VERSION 0x010100
19#endif
Fred Drake85d835f2001-02-08 15:39:08 +000020#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000021
22#ifndef PyGC_HEAD_SIZE
23#define PyGC_HEAD_SIZE 0
24#define PyObject_GC_Init(x)
25#define PyObject_GC_Fini(m)
26#define Py_TPFLAGS_GC 0
27#endif
28
Fred Drake0582df92000-07-12 04:49:00 +000029enum HandlerTypes {
30 StartElement,
31 EndElement,
32 ProcessingInstruction,
33 CharacterData,
34 UnparsedEntityDecl,
35 NotationDecl,
36 StartNamespaceDecl,
37 EndNamespaceDecl,
38 Comment,
39 StartCdataSection,
40 EndCdataSection,
41 Default,
42 DefaultHandlerExpand,
43 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000044 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000045#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000046 StartDoctypeDecl,
47 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000048#endif
49#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000050 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000051 InternalParsedEntityDecl,
52#endif
53#if EXPAT_VERSION >= 0x015f00
54 EntityDecl,
55 XmlDecl,
56 ElementDecl,
57 AttlistDecl,
58#endif
59 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000060};
61
62static PyObject *ErrorObject;
63
64/* ----------------------------------------------------- */
65
66/* Declarations for objects of type xmlparser */
67
68typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000069 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070
Fred Drake0582df92000-07-12 04:49:00 +000071 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000072 int returns_unicode; /* True if Unicode strings are returned;
73 if false, UTF-8 strings are returned */
74 int ordered_attributes; /* Return attributes as a list. */
75 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000076 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000077 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078} xmlparseobject;
79
80staticforward PyTypeObject Xmlparsetype;
81
Fred Drake6f987622000-08-25 18:03:30 +000082typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000083typedef void* xmlhandler;
84
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000085struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000086 const char *name;
87 xmlhandlersetter setter;
88 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000089 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090};
91
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000092staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000093
Fred Drakebd6101c2001-02-14 18:29:45 +000094/* Set an integer attribute on the error object; return true on success,
95 * false on an exception.
96 */
97static int
98set_error_attr(PyObject *err, char *name, int value)
99{
100 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000101
Fred Drakebd6101c2001-02-14 18:29:45 +0000102 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
103 Py_DECREF(v);
104 return 0;
105 }
106 return 1;
107}
108
109/* Build and set an Expat exception, including positioning
110 * information. Always returns NULL.
111 */
Fred Drake85d835f2001-02-08 15:39:08 +0000112static PyObject *
113set_error(xmlparseobject *self)
114{
115 PyObject *err;
116 char buffer[256];
117 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000118 int lineno = XML_GetErrorLineNumber(parser);
119 int column = XML_GetErrorColumnNumber(parser);
120 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000121
122 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000123 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000124 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 if ( err != NULL
126 && set_error_attr(err, "code", code)
127 && set_error_attr(err, "offset", column)
128 && set_error_attr(err, "lineno", lineno)) {
129 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000130 }
131 return NULL;
132}
133
134
135#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000136/* Convert an array of attributes and their values into a Python dict */
137
Fred Drake0582df92000-07-12 04:49:00 +0000138static PyObject *
139conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000140{
Fred Drake0582df92000-07-12 04:49:00 +0000141 PyObject *attrs_obj = NULL;
142 XML_Char **attrs_p, **attrs_k = NULL;
143 int attrs_len;
144 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000145
Fred Drake0582df92000-07-12 04:49:00 +0000146 if ((attrs_obj = PyDict_New()) == NULL)
147 goto finally;
148 for (attrs_len = 0, attrs_p = atts;
149 *attrs_p;
150 attrs_p++, attrs_len++) {
151 if (attrs_len % 2) {
152 rv = PyString_FromString(*attrs_p);
153 if (!rv) {
154 Py_DECREF(attrs_obj);
155 attrs_obj = NULL;
156 goto finally;
157 }
158 if (PyDict_SetItemString(attrs_obj,
159 (char*)*attrs_k, rv) < 0) {
160 Py_DECREF(attrs_obj);
161 attrs_obj = NULL;
162 goto finally;
163 }
164 Py_DECREF(rv);
165 }
166 else
167 attrs_k = attrs_p;
168 }
169 finally:
170 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171}
Fred Drake85d835f2001-02-08 15:39:08 +0000172#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000173
174#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake85d835f2001-02-08 15:39:08 +0000175#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000176static PyObject *
177conv_atts_using_unicode(XML_Char **atts)
178{
Fred Drakeca1f4262000-09-21 20:10:23 +0000179 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000180 XML_Char **attrs_p, **attrs_k = NULL;
181 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000182
Fred Drake0582df92000-07-12 04:49:00 +0000183 if ((attrs_obj = PyDict_New()) == NULL)
184 goto finally;
185 for (attrs_len = 0, attrs_p = atts;
186 *attrs_p;
187 attrs_p++, attrs_len++) {
188 if (attrs_len % 2) {
189 PyObject *attr_str, *value_str;
190 const char *p = (const char *) (*attrs_k);
191 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
192 if (!attr_str) {
193 Py_DECREF(attrs_obj);
194 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000195 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000196 }
197 p = (const char *) *attrs_p;
198 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
199 if (!value_str) {
200 Py_DECREF(attrs_obj);
201 Py_DECREF(attr_str);
202 attrs_obj = NULL;
203 goto finally;
204 }
205 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
206 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000207 Py_DECREF(attr_str);
208 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000209 attrs_obj = NULL;
210 goto finally;
211 }
212 Py_DECREF(attr_str);
213 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000214 }
Fred Drake0582df92000-07-12 04:49:00 +0000215 else
216 attrs_k = attrs_p;
217 }
218 finally:
219 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000220}
Fred Drake85d835f2001-02-08 15:39:08 +0000221#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000223/* Convert a string of XML_Chars into a Unicode string.
224 Returns None if str is a null pointer. */
225
Fred Drake0582df92000-07-12 04:49:00 +0000226static PyObject *
227conv_string_to_unicode(XML_Char *str)
228{
229 /* XXX currently this code assumes that XML_Char is 8-bit,
230 and hence in UTF-8. */
231 /* UTF-8 from Expat, Unicode desired */
232 if (str == NULL) {
233 Py_INCREF(Py_None);
234 return Py_None;
235 }
236 return PyUnicode_DecodeUTF8((const char *)str,
237 strlen((const char *)str),
238 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000239}
240
Fred Drake0582df92000-07-12 04:49:00 +0000241static PyObject *
242conv_string_len_to_unicode(const XML_Char *str, int len)
243{
244 /* XXX currently this code assumes that XML_Char is 8-bit,
245 and hence in UTF-8. */
246 /* UTF-8 from Expat, Unicode desired */
247 if (str == NULL) {
248 Py_INCREF(Py_None);
249 return Py_None;
250 }
Fred Drake6f987622000-08-25 18:03:30 +0000251 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000252}
253#endif
254
255/* Convert a string of XML_Chars into an 8-bit Python string.
256 Returns None if str is a null pointer. */
257
Fred Drake6f987622000-08-25 18:03:30 +0000258static PyObject *
259conv_string_to_utf8(XML_Char *str)
260{
261 /* XXX currently this code assumes that XML_Char is 8-bit,
262 and hence in UTF-8. */
263 /* UTF-8 from Expat, UTF-8 desired */
264 if (str == NULL) {
265 Py_INCREF(Py_None);
266 return Py_None;
267 }
268 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000269}
270
Fred Drake6f987622000-08-25 18:03:30 +0000271static PyObject *
272conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000273{
Fred Drake6f987622000-08-25 18:03:30 +0000274 /* XXX currently this code assumes that XML_Char is 8-bit,
275 and hence in UTF-8. */
276 /* UTF-8 from Expat, UTF-8 desired */
277 if (str == NULL) {
278 Py_INCREF(Py_None);
279 return Py_None;
280 }
281 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000282}
283
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000284/* Callback routines */
285
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000286static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000287
Fred Drake6f987622000-08-25 18:03:30 +0000288static void
289flag_error(xmlparseobject *self)
290{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000291 clear_handlers(self, 1);
292}
293
294static PyCodeObject*
295getcode(enum HandlerTypes slot, char* func_name, int lineno)
296{
Fred Drakebd6101c2001-02-14 18:29:45 +0000297 PyObject *code = NULL;
298 PyObject *name = NULL;
299 PyObject *nulltuple = NULL;
300 PyObject *filename = NULL;
301
302 if (handler_info[slot].tb_code == NULL) {
303 code = PyString_FromString("");
304 if (code == NULL)
305 goto failed;
306 name = PyString_FromString(func_name);
307 if (name == NULL)
308 goto failed;
309 nulltuple = PyTuple_New(0);
310 if (nulltuple == NULL)
311 goto failed;
312 filename = PyString_FromString(__FILE__);
313 handler_info[slot].tb_code =
314 PyCode_New(0, /* argcount */
315 0, /* nlocals */
316 0, /* stacksize */
317 0, /* flags */
318 code, /* code */
319 nulltuple, /* consts */
320 nulltuple, /* names */
321 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000322#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000323 nulltuple, /* freevars */
324 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000325#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000326 filename, /* filename */
327 name, /* name */
328 lineno, /* firstlineno */
329 code /* lnotab */
330 );
331 if (handler_info[slot].tb_code == NULL)
332 goto failed;
333 Py_DECREF(code);
334 Py_DECREF(nulltuple);
335 Py_DECREF(filename);
336 Py_DECREF(name);
337 }
338 return handler_info[slot].tb_code;
339 failed:
340 Py_XDECREF(code);
341 Py_XDECREF(name);
342 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000343}
344
345static PyObject*
346call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
347{
Fred Drakebd6101c2001-02-14 18:29:45 +0000348 PyThreadState *tstate = PyThreadState_GET();
349 PyFrameObject *f;
350 PyObject *res;
351
352 if (c == NULL)
353 return NULL;
354 f = PyFrame_New(
355 tstate, /*back*/
356 c, /*code*/
357 tstate->frame->f_globals, /*globals*/
358 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000359 );
360 if (f == NULL)
361 return NULL;
362 tstate->frame = f;
363 res = PyEval_CallObject(func, args);
364 if (res == NULL && tstate->curexc_traceback == NULL)
365 PyTraceBack_Here(f);
366 tstate->frame = f->f_back;
367 Py_DECREF(f);
368 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000369}
370
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000371#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
372#define STRING_CONV_FUNC conv_string_to_utf8
373#else
374/* Python 1.6 and later versions */
375#define STRING_CONV_FUNC (self->returns_unicode \
376 ? conv_string_to_unicode : conv_string_to_utf8)
377#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000378
Fred Drake85d835f2001-02-08 15:39:08 +0000379static void
380my_StartElementHandler(void *userData,
381 const XML_Char *name, const XML_Char **atts)
382{
383 xmlparseobject *self = (xmlparseobject *)userData;
384
385 if (self->handlers[StartElement]
386 && self->handlers[StartElement] != Py_None) {
387 PyObject *container, *rv, *args;
388 int i, max;
389
390 /* Set max to the number of slots filled in atts[]; max/2 is
391 * the number of attributes we need to process.
392 */
393 if (self->specified_attributes) {
394 max = XML_GetSpecifiedAttributeCount(self->itself);
395 }
396 else {
397 max = 0;
398 while (atts[max] != NULL)
399 max += 2;
400 }
401 /* Build the container. */
402 if (self->ordered_attributes)
403 container = PyList_New(max);
404 else
405 container = PyDict_New();
406 if (container == NULL) {
407 flag_error(self);
408 return;
409 }
410 for (i = 0; i < max; i += 2) {
411 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
412 PyObject *v;
413 if (n == NULL) {
414 flag_error(self);
415 Py_DECREF(container);
416 return;
417 }
418 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
419 if (v == NULL) {
420 flag_error(self);
421 Py_DECREF(container);
422 Py_DECREF(n);
423 return;
424 }
425 if (self->ordered_attributes) {
426 PyList_SET_ITEM(container, i, n);
427 PyList_SET_ITEM(container, i+1, v);
428 }
429 else if (PyDict_SetItem(container, n, v)) {
430 flag_error(self);
431 Py_DECREF(n);
432 Py_DECREF(v);
433 return;
434 }
435 else {
436 Py_DECREF(n);
437 Py_DECREF(v);
438 }
439 }
440 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
441 if (args == NULL) {
442 Py_DECREF(container);
443 return;
444 }
445 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000446 self->in_callback = 1;
447 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000448 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000449 self->in_callback = 0;
450 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000451 if (rv == NULL) {
452 flag_error(self);
453 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000454 }
Fred Drake85d835f2001-02-08 15:39:08 +0000455 Py_DECREF(rv);
456 }
457}
458
459#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
460 RETURN, GETUSERDATA) \
461static RC \
462my_##NAME##Handler PARAMS {\
463 xmlparseobject *self = GETUSERDATA ; \
464 PyObject *args = NULL; \
465 PyObject *rv = NULL; \
466 INIT \
467\
468 if (self->handlers[NAME] \
469 && self->handlers[NAME] != Py_None) { \
470 args = Py_BuildValue PARAM_FORMAT ;\
471 if (!args) \
472 return RETURN; \
Fred Drakebd6101c2001-02-14 18:29:45 +0000473 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000474 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
475 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000476 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000477 Py_DECREF(args); \
478 if (rv == NULL) { \
479 flag_error(self); \
480 return RETURN; \
481 } \
482 CONVERSION \
483 Py_DECREF(rv); \
484 } \
485 return RETURN; \
486}
487
Fred Drake6f987622000-08-25 18:03:30 +0000488#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
489 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
490 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000491
Fred Drake6f987622000-08-25 18:03:30 +0000492#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
493 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
494 rc = PyInt_AsLong(rv);, rc, \
495 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000496
Fred Drake6f987622000-08-25 18:03:30 +0000497VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000498 (void *userData, const XML_Char *name),
499 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000500
Fred Drake6f987622000-08-25 18:03:30 +0000501VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000502 (void *userData,
503 const XML_Char *target,
504 const XML_Char *data),
505 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000506
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000507#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000508VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000509 (void *userData, const XML_Char *data, int len),
510 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000511#else
Fred Drake6f987622000-08-25 18:03:30 +0000512VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000513 (void *userData, const XML_Char *data, int len),
514 ("(N)", (self->returns_unicode
515 ? conv_string_len_to_unicode(data,len)
516 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000517#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000518
Fred Drake6f987622000-08-25 18:03:30 +0000519VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000520 (void *userData,
521 const XML_Char *entityName,
522 const XML_Char *base,
523 const XML_Char *systemId,
524 const XML_Char *publicId,
525 const XML_Char *notationName),
526 ("(O&O&O&O&O&)",
527 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
528 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
529 STRING_CONV_FUNC,notationName))
530
531#if EXPAT_VERSION >= 0x015f00
532#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
533VOID_HANDLER(EntityDecl,
534 (void *userData,
535 const XML_Char *entityName,
536 int is_parameter_entity,
537 const XML_Char *value,
538 int value_length,
539 const XML_Char *base,
540 const XML_Char *systemId,
541 const XML_Char *publicId,
542 const XML_Char *notationName),
543 ("O&iNO&O&O&O&",
544 STRING_CONV_FUNC,entityName, is_parameter_entity,
545 conv_string_len_to_utf8(value, value_length),
546 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
547 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
548#else
549VOID_HANDLER(EntityDecl,
550 (void *userData,
551 const XML_Char *entityName,
552 int is_parameter_entity,
553 const XML_Char *value,
554 int value_length,
555 const XML_Char *base,
556 const XML_Char *systemId,
557 const XML_Char *publicId,
558 const XML_Char *notationName),
559 ("O&iNO&O&O&O&",
560 STRING_CONV_FUNC,entityName, is_parameter_entity,
561 (self->returns_unicode
562 ? conv_string_len_to_unicode(value, value_length)
563 : conv_string_len_to_utf8(value, value_length)),
564 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
565 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
566#endif
567
568VOID_HANDLER(XmlDecl,
569 (void *userData,
570 const XML_Char *version,
571 const XML_Char *encoding,
572 int standalone),
573 ("(O&O&i)",
574 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
575 standalone))
576
577static PyObject *
578conv_content_model(XML_Content * const model,
579 PyObject *(*conv_string)(XML_Char *))
580{
581 PyObject *result = NULL;
582 PyObject *children = PyTuple_New(model->numchildren);
583 int i;
584
585 if (children != NULL) {
586 for (i = 0; i < model->numchildren; ++i) {
587 PyObject *child = conv_content_model(&model->children[i],
588 conv_string);
589 if (child == NULL) {
590 Py_XDECREF(children);
591 return NULL;
592 }
593 PyTuple_SET_ITEM(children, i, child);
594 }
595 result = Py_BuildValue("(iiO&N)",
596 model->type, model->quant,
597 conv_string,model->name, children);
598 }
599 return result;
600}
601
602static PyObject *
603conv_content_model_utf8(XML_Content * const model)
604{
605 return conv_content_model(model, conv_string_to_utf8);
606}
607
608#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
609static PyObject *
610conv_content_model_unicode(XML_Content * const model)
611{
612 return conv_content_model(model, conv_string_to_unicode);
613}
614
615VOID_HANDLER(ElementDecl,
616 (void *userData,
617 const XML_Char *name,
618 XML_Content *model),
619 ("O&O&",
620 STRING_CONV_FUNC,name,
621 (self->returns_unicode ? conv_content_model_unicode
622 : conv_content_model_utf8),model))
623#else
624VOID_HANDLER(ElementDecl,
625 (void *userData,
626 const XML_Char *name,
627 XML_Content *model),
628 ("O&O&",
629 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
630#endif
631
632VOID_HANDLER(AttlistDecl,
633 (void *userData,
634 const XML_Char *elname,
635 const XML_Char *attname,
636 const XML_Char *att_type,
637 const XML_Char *dflt,
638 int isrequired),
639 ("(O&O&O&O&i)",
640 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
641 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
642 isrequired))
643#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000644
Fred Drake6f987622000-08-25 18:03:30 +0000645VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000646 (void *userData,
647 const XML_Char *notationName,
648 const XML_Char *base,
649 const XML_Char *systemId,
650 const XML_Char *publicId),
651 ("(O&O&O&O&)",
652 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
653 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000654
Fred Drake6f987622000-08-25 18:03:30 +0000655VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000656 (void *userData,
657 const XML_Char *prefix,
658 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000659 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000660
Fred Drake6f987622000-08-25 18:03:30 +0000661VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000662 (void *userData,
663 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000664 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000665
Fred Drake6f987622000-08-25 18:03:30 +0000666VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000667 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000668 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000669
Fred Drake6f987622000-08-25 18:03:30 +0000670VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000671 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000672 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000673
Fred Drake6f987622000-08-25 18:03:30 +0000674VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000675 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000676 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000677
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000678#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000679VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000680 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000681 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682
Fred Drake6f987622000-08-25 18:03:30 +0000683VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000684 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000685 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000686#else
Fred Drake6f987622000-08-25 18:03:30 +0000687VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000688 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000689 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000690 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000691 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000692
Fred Drake6f987622000-08-25 18:03:30 +0000693VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000695 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000696 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000697 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000698#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000699
Fred Drake6f987622000-08-25 18:03:30 +0000700INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701 (void *userData),
702 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000703
Fred Drake6f987622000-08-25 18:03:30 +0000704RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000705 (XML_Parser parser,
706 const XML_Char *context,
707 const XML_Char *base,
708 const XML_Char *systemId,
709 const XML_Char *publicId),
710 int rc=0;,
711 ("(O&O&O&O&)",
712 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000713 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
714 rc = PyInt_AsLong(rv);, rc,
715 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000716
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000717/* XXX UnknownEncodingHandler */
718
Fred Drake85d835f2001-02-08 15:39:08 +0000719#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000720VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000721 (void *userData, const XML_Char *doctypeName),
722 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
723 Py_None, Py_None, -1))
724#elif EXPAT_VERSION >= 0x015f00
725VOID_HANDLER(StartDoctypeDecl,
726 (void *userData, const XML_Char *doctypeName,
727 const XML_Char *sysid, const XML_Char *pubid,
728 int has_internal_subset),
729 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
730 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
731 has_internal_subset))
732#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000733
Fred Drake85d835f2001-02-08 15:39:08 +0000734#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000735VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000736#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000737
Fred Drake85d835f2001-02-08 15:39:08 +0000738#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000739VOID_HANDLER(ExternalParsedEntityDecl,
740 (void *userData, const XML_Char *entityName,
741 const XML_Char *base, const XML_Char *systemId,
742 const XML_Char *publicId),
743 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
744 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
745 STRING_CONV_FUNC, publicId))
746
747VOID_HANDLER(InternalParsedEntityDecl,
748 (void *userData, const XML_Char *entityName,
749 const XML_Char *replacementText, int replacementTextLength),
750 ("(O&O&i)", STRING_CONV_FUNC, entityName,
751 STRING_CONV_FUNC, replacementText, replacementTextLength))
752
Fred Drake85d835f2001-02-08 15:39:08 +0000753#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000754
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000755/* ---------------------------------------------------------------- */
756
757static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000758"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000759Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000760
761static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000762xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000763{
Fred Drake0582df92000-07-12 04:49:00 +0000764 char *s;
765 int slen;
766 int isFinal = 0;
767 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000768
Fred Drake0582df92000-07-12 04:49:00 +0000769 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
770 return NULL;
771 rv = XML_Parse(self->itself, s, slen, isFinal);
772 if (PyErr_Occurred()) {
773 return NULL;
774 }
775 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000776 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000777 }
778 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000779}
780
Fred Drakeca1f4262000-09-21 20:10:23 +0000781/* File reading copied from cPickle */
782
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783#define BUF_SIZE 2048
784
Fred Drake0582df92000-07-12 04:49:00 +0000785static int
786readinst(char *buf, int buf_size, PyObject *meth)
787{
788 PyObject *arg = NULL;
789 PyObject *bytes = NULL;
790 PyObject *str = NULL;
791 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000792
Fred Drake676940b2000-09-22 15:21:31 +0000793 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000794 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000795
Fred Drakeca1f4262000-09-21 20:10:23 +0000796 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000797 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000798
Tim Peters954eef72000-09-22 06:01:11 +0000799 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000800
Fred Drakeca1f4262000-09-21 20:10:23 +0000801 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000802 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000803
Fred Drake0582df92000-07-12 04:49:00 +0000804 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000805 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000806 PyErr_Format(PyExc_TypeError,
807 "read() did not return a string object (type=%.400s)",
808 str->ob_type->tp_name);
809 goto finally;
810 }
811 len = PyString_GET_SIZE(str);
812 if (len > buf_size) {
813 PyErr_Format(PyExc_ValueError,
814 "read() returned too much data: "
815 "%i bytes requested, %i returned",
816 buf_size, len);
817 Py_DECREF(str);
818 goto finally;
819 }
820 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000821finally:
Fred Drake0582df92000-07-12 04:49:00 +0000822 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000823 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000824 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825}
826
827static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000828"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000829Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000830
831static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000832xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833{
Fred Drake0582df92000-07-12 04:49:00 +0000834 int rv = 1;
835 PyObject *f;
836 FILE *fp;
837 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838
Fred Drake0582df92000-07-12 04:49:00 +0000839 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
840 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841
Fred Drake0582df92000-07-12 04:49:00 +0000842 if (PyFile_Check(f)) {
843 fp = PyFile_AsFile(f);
844 }
845 else{
846 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000847 readmethod = PyObject_GetAttrString(f, "read");
848 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000849 PyErr_Clear();
850 PyErr_SetString(PyExc_TypeError,
851 "argument must have 'read' attribute");
852 return 0;
853 }
854 }
855 for (;;) {
856 int bytes_read;
857 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
858 if (buf == NULL)
859 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000860
Fred Drake0582df92000-07-12 04:49:00 +0000861 if (fp) {
862 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
863 if (bytes_read < 0) {
864 PyErr_SetFromErrno(PyExc_IOError);
865 return NULL;
866 }
867 }
868 else {
869 bytes_read = readinst(buf, BUF_SIZE, readmethod);
870 if (bytes_read < 0)
871 return NULL;
872 }
873 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
874 if (PyErr_Occurred())
875 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000876
Fred Drake0582df92000-07-12 04:49:00 +0000877 if (!rv || bytes_read == 0)
878 break;
879 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000880 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000881 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000882 }
Fred Drake0582df92000-07-12 04:49:00 +0000883 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000884}
885
886static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000887"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000888Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000889
890static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000891xmlparse_SetBase(xmlparseobject *self, PyObject *args)
892{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000893 char *base;
894
Fred Drake0582df92000-07-12 04:49:00 +0000895 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000897 if (!XML_SetBase(self->itself, base)) {
898 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000900 Py_INCREF(Py_None);
901 return Py_None;
902}
903
904static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000905"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000906Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907
908static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000909xmlparse_GetBase(xmlparseobject *self, PyObject *args)
910{
911 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000912 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000913
Fred Drake0582df92000-07-12 04:49:00 +0000914 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915}
916
Fred Drakebd6101c2001-02-14 18:29:45 +0000917#if EXPAT_VERSION >= 0x015f00
918static char xmlparse_GetInputContext__doc__[] =
919"GetInputContext() -> string\n\
920Return the untranslated text of the input that caused the current event.\n\
921If the event was generated by a large amount of text (such as a start tag\n\
922for an element with many attributes), not all of the text may be available.";
923
924static PyObject *
925xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
926{
927 PyObject *result = NULL;
928
929 if (PyArg_ParseTuple(args, ":GetInputContext")) {
930 if (self->in_callback) {
931 int offset, size;
932 const char *buffer
933 = XML_GetInputContext(self->itself, &offset, &size);
934
935 if (buffer != NULL)
936 result = PyString_FromStringAndSize(buffer + offset, size);
937 else {
938 result = Py_None;
939 Py_INCREF(result);
940 }
941 }
942 else {
943 result = Py_None;
944 Py_INCREF(result);
945 }
946 }
947 return result;
948}
949#endif
950
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000951static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000952"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000953Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000954information passed to the ExternalEntityRefHandler.";
955
956static PyObject *
957xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
958{
959 char *context;
960 char *encoding = NULL;
961 xmlparseobject *new_parser;
962 int i;
963
964 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
965 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000966 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000967 }
968
969#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
970 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000971#else
Fred Drake85d835f2001-02-08 15:39:08 +0000972 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000973 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000974#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000975
976 if (new_parser == NULL)
977 return NULL;
978 new_parser->returns_unicode = self->returns_unicode;
979 new_parser->ordered_attributes = self->ordered_attributes;
980 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000981 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000982 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000983 encoding);
984 new_parser->handlers = 0;
985 PyObject_GC_Init(new_parser);
986
987 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000988 Py_DECREF(new_parser);
989 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000990 }
991
992 XML_SetUserData(new_parser->itself, (void *)new_parser);
993
994 /* allocate and clear handlers first */
995 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000996 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000997
998 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000999 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001000 Py_DECREF(new_parser);
1001 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001002 }
1003 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001004
1005 /* then copy handlers from self */
1006 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001007 if (self->handlers[i]) {
1008 Py_INCREF(self->handlers[i]);
1009 new_parser->handlers[i] = self->handlers[i];
1010 handler_info[i].setter(new_parser->itself,
1011 handler_info[i].handler);
1012 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001013 }
Fred Drake28adf522000-09-24 22:07:59 +00001014 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001015}
1016
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001017#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001018
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001019static char xmlparse_SetParamEntityParsing__doc__[] =
1020"SetParamEntityParsing(flag) -> success\n\
1021Controls parsing of parameter entities (including the external DTD\n\
1022subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1023XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1024XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1025was successful.";
1026
1027static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001028xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001029{
Fred Drake85d835f2001-02-08 15:39:08 +00001030 int flag;
1031 if (!PyArg_ParseTuple(args, "i", &flag))
1032 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001033 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001034 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001035}
1036
Fred Drake85d835f2001-02-08 15:39:08 +00001037#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001038
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001039static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001040 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001041 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001042 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001043 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001044 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001045 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001046 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001047 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001048 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1049 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001050#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001051 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1052 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1053#endif
1054#if EXPAT_VERSION >= 0x015f00
1055 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1056 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001057#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001058 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001059};
1060
1061/* ---------- */
1062
1063
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001064#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1065
1066/*
1067 pyexpat international encoding support.
1068 Make it as simple as possible.
1069*/
1070
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001071static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001072PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073
1074static void
1075init_template_buffer(void)
1076{
1077 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001078 for (i = 0; i < 256; i++) {
1079 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001080 }
Fred Drakebb66a202001-03-01 20:48:17 +00001081 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001082}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001083
1084int
1085PyUnknownEncodingHandler(void *encodingHandlerData,
1086const XML_Char *name,
1087XML_Encoding * info)
1088{
Fred Drakebb66a202001-03-01 20:48:17 +00001089 PyUnicodeObject *_u_string = NULL;
1090 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001091 int i;
1092
Fred Drakebb66a202001-03-01 20:48:17 +00001093 /* Yes, supports only 8bit encodings */
1094 _u_string = (PyUnicodeObject *)
1095 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001096
Fred Drakebb66a202001-03-01 20:48:17 +00001097 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001098 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099
Fred Drakebb66a202001-03-01 20:48:17 +00001100 for (i = 0; i < 256; i++) {
1101 /* Stupid to access directly, but fast */
1102 Py_UNICODE c = _u_string->str[i];
1103 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001104 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001105 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001106 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001107 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108
1109 info->data = NULL;
1110 info->convert = NULL;
1111 info->release = NULL;
1112 result=1;
1113
1114 Py_DECREF(_u_string);
1115 return result;
1116}
1117
1118#endif
1119
1120static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001121newxmlparseobject(char *encoding, char *namespace_separator)
1122{
1123 int i;
1124 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001125
1126#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001127 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1128 if (self == NULL)
1129 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001130
Fred Drake0582df92000-07-12 04:49:00 +00001131 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001132#else
Fred Drake0582df92000-07-12 04:49:00 +00001133 /* Code for versions 1.6 and later */
1134 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1135 if (self == NULL)
1136 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001137
Fred Drake0582df92000-07-12 04:49:00 +00001138 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001139#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001140 self->ordered_attributes = 0;
1141 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001142 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001143 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001144 if (namespace_separator) {
1145 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1146 }
Fred Drake85d835f2001-02-08 15:39:08 +00001147 else {
Fred Drake0582df92000-07-12 04:49:00 +00001148 self->itself = XML_ParserCreate(encoding);
1149 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001150 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001151 if (self->itself == NULL) {
1152 PyErr_SetString(PyExc_RuntimeError,
1153 "XML_ParserCreate failed");
1154 Py_DECREF(self);
1155 return NULL;
1156 }
1157 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001158#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1159#else
1160 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1161#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001162
Fred Drake0582df92000-07-12 04:49:00 +00001163 for(i = 0; handler_info[i].name != NULL; i++)
1164 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001165
Fred Drake0582df92000-07-12 04:49:00 +00001166 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001167 if (!self->handlers){
1168 Py_DECREF(self);
1169 return PyErr_NoMemory();
1170 }
1171 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001172
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001173 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001174}
1175
1176
1177static void
Fred Drake0582df92000-07-12 04:49:00 +00001178xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001179{
Fred Drake0582df92000-07-12 04:49:00 +00001180 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001181 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001182 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001183 XML_ParserFree(self->itself);
1184 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001185
Fred Drake85d835f2001-02-08 15:39:08 +00001186 if (self->handlers != NULL) {
1187 for (i = 0; handler_info[i].name != NULL; i++) {
1188 Py_XDECREF(self->handlers[i]);
1189 }
1190 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001191 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001192#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001193 /* Code for versions before 1.6 */
1194 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001195#else
Fred Drake0582df92000-07-12 04:49:00 +00001196 /* Code for versions 1.6 and later */
1197 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001198#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001199}
1200
Fred Drake0582df92000-07-12 04:49:00 +00001201static int
1202handlername2int(const char *name)
1203{
1204 int i;
1205 for (i=0; handler_info[i].name != NULL; i++) {
1206 if (strcmp(name, handler_info[i].name) == 0) {
1207 return i;
1208 }
1209 }
1210 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001211}
1212
1213static PyObject *
1214xmlparse_getattr(xmlparseobject *self, char *name)
1215{
Fred Drake0582df92000-07-12 04:49:00 +00001216 int handlernum;
1217 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001218 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001219 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001220 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001221 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001222 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001223 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001224 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1225 if (strcmp(name, "ordered_attributes") == 0)
1226 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001227 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001228 return PyInt_FromLong((long) self->returns_unicode);
1229 if (strcmp(name, "specified_attributes") == 0)
1230 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001231
Fred Drake0582df92000-07-12 04:49:00 +00001232 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001233
Fred Drake0582df92000-07-12 04:49:00 +00001234 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1235 Py_INCREF(self->handlers[handlernum]);
1236 return self->handlers[handlernum];
1237 }
1238 if (strcmp(name, "__members__") == 0) {
1239 int i;
1240 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001241 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001242 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001243 }
1244 PyList_Append(rc, PyString_FromString("ErrorCode"));
1245 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1246 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1247 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001248 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001249 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001250 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001251
Fred Drake0582df92000-07-12 04:49:00 +00001252 return rc;
1253 }
1254 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001255}
1256
Fred Drake6f987622000-08-25 18:03:30 +00001257static int
1258sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001259{
1260 int handlernum = handlername2int(name);
1261 if (handlernum != -1) {
1262 Py_INCREF(v);
1263 Py_XDECREF(self->handlers[handlernum]);
1264 self->handlers[handlernum] = v;
1265 handler_info[handlernum].setter(self->itself,
1266 handler_info[handlernum].handler);
1267 return 1;
1268 }
1269 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001270}
1271
1272static int
Fred Drake6f987622000-08-25 18:03:30 +00001273xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001274{
Fred Drake6f987622000-08-25 18:03:30 +00001275 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001276 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001277 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1278 return -1;
1279 }
Fred Drake85d835f2001-02-08 15:39:08 +00001280 if (strcmp(name, "ordered_attributes") == 0) {
1281 if (PyObject_IsTrue(v))
1282 self->ordered_attributes = 1;
1283 else
1284 self->ordered_attributes = 0;
1285 return 0;
1286 }
Fred Drake6f987622000-08-25 18:03:30 +00001287 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001288 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001289#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001290 PyErr_SetString(PyExc_ValueError,
1291 "Cannot return Unicode strings in Python 1.5");
1292 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001293#else
Fred Drake6f987622000-08-25 18:03:30 +00001294 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001295#endif
Fred Drake6f987622000-08-25 18:03:30 +00001296 }
1297 else
1298 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001299 return 0;
1300 }
1301 if (strcmp(name, "specified_attributes") == 0) {
1302 if (PyObject_IsTrue(v))
1303 self->specified_attributes = 1;
1304 else
1305 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001306 return 0;
1307 }
1308 if (sethandler(self, name, v)) {
1309 return 0;
1310 }
1311 PyErr_SetString(PyExc_AttributeError, name);
1312 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001313}
1314
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001315#ifdef WITH_CYCLE_GC
1316static int
1317xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1318{
1319 int i, err;
1320 for (i = 0; handler_info[i].name != NULL; i++) {
1321 if (!op->handlers[i])
1322 continue;
1323 err = visit(op->handlers[i], arg);
1324 if (err)
1325 return err;
1326 }
1327 return 0;
1328}
1329
1330static int
1331xmlparse_clear(xmlparseobject *op)
1332{
1333 clear_handlers(op, 1);
1334 return 0;
1335}
1336#endif
1337
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001338static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001339"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001340
1341static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001342 PyObject_HEAD_INIT(NULL)
1343 0, /*ob_size*/
1344 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001345 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001346 0, /*tp_itemsize*/
1347 /* methods */
1348 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1349 (printfunc)0, /*tp_print*/
1350 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1351 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1352 (cmpfunc)0, /*tp_compare*/
1353 (reprfunc)0, /*tp_repr*/
1354 0, /*tp_as_number*/
1355 0, /*tp_as_sequence*/
1356 0, /*tp_as_mapping*/
1357 (hashfunc)0, /*tp_hash*/
1358 (ternaryfunc)0, /*tp_call*/
1359 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001360 0, /* tp_getattro */
1361 0, /* tp_setattro */
1362 0, /* tp_as_buffer */
1363 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1364 Xmlparsetype__doc__, /* Documentation string */
1365#ifdef WITH_CYCLE_GC
1366 (traverseproc)xmlparse_traverse, /* tp_traverse */
1367 (inquiry)xmlparse_clear /* tp_clear */
1368#else
1369 0, 0
1370#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001371};
1372
1373/* End of code for xmlparser objects */
1374/* -------------------------------------------------------- */
1375
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001376static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001377"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1378Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001379
1380static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001381pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1382{
1383 char *encoding = NULL;
1384 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001385 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001386
Fred Drake0582df92000-07-12 04:49:00 +00001387 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001388 &encoding, &namespace_separator))
1389 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001390 if (namespace_separator != NULL
1391 && strlen(namespace_separator) != 1) {
1392 PyErr_SetString(PyExc_ValueError,
1393 "namespace_separator must be one character,"
1394 " omitted, or None");
1395 return NULL;
1396 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001397 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001398}
1399
1400static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001401"ErrorString(errno) -> string\n\
1402Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001403
1404static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001405pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001406{
Fred Drake0582df92000-07-12 04:49:00 +00001407 long code = 0;
1408
1409 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1410 return NULL;
1411 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001412}
1413
1414/* List of methods defined in the module */
1415
1416static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001417 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1418 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1419 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1420 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001421
Fred Drake0582df92000-07-12 04:49:00 +00001422 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423};
1424
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001425/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001426
1427static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001428"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001429
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001430/* Initialization function for the module */
1431
Fred Drake93adb692000-09-23 04:55:48 +00001432void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001433
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001434#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001435
1436/* 1.5 compatibility: PyModule_AddObject */
1437static int
1438PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1439{
1440 PyObject *dict;
1441 if (!PyModule_Check(m) || o == NULL)
1442 return -1;
1443 dict = PyModule_GetDict(m);
1444 if (dict == NULL)
1445 return -1;
1446 if (PyDict_SetItemString(dict, name, o))
1447 return -1;
1448 Py_DECREF(o);
1449 return 0;
1450}
1451
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001452int
1453PyModule_AddIntConstant(PyObject *m, char *name, long value)
1454{
1455 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1456}
1457
Fred Drakea77254a2000-09-29 19:23:29 +00001458static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001459PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1460{
1461 return PyModule_AddObject(m, name, PyString_FromString(value));
1462}
1463
1464#endif
1465
Fred Drake6f987622000-08-25 18:03:30 +00001466DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001467initpyexpat(void)
1468{
1469 PyObject *m, *d;
1470 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001471 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001472 PyObject *errors_module;
1473 PyObject *modelmod_name;
1474 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001475 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001476
Fred Drake6f987622000-08-25 18:03:30 +00001477 if (errmod_name == NULL)
1478 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001479 modelmod_name = PyString_FromString("pyexpat.model");
1480 if (modelmod_name == NULL)
1481 return;
Fred Drake6f987622000-08-25 18:03:30 +00001482
Fred Drake0582df92000-07-12 04:49:00 +00001483 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001484
Fred Drake0582df92000-07-12 04:49:00 +00001485 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001486 m = Py_InitModule3("pyexpat", pyexpat_methods,
1487 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001488
Fred Drake0582df92000-07-12 04:49:00 +00001489 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001490 if (ErrorObject == NULL) {
1491 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001492 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001493 if (ErrorObject == NULL)
1494 return;
1495 }
1496 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001497 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001498 Py_INCREF(ErrorObject);
1499 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001500 Py_INCREF(&Xmlparsetype);
1501 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001502
Fred Drake93adb692000-09-23 04:55:48 +00001503 PyModule_AddObject(m, "__version__",
1504 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake85d835f2001-02-08 15:39:08 +00001505#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001506 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1507 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001508 {
1509 XML_Expat_Version info = XML_ExpatVersionInfo();
1510 PyModule_AddObject(m, "version_info",
1511 Py_BuildValue("(iii)", info.major,
1512 info.minor, info.micro));
1513 }
Fred Drake738293d2000-12-21 17:25:07 +00001514#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001515#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1516#else
1517 init_template_buffer();
1518#endif
Fred Drake0582df92000-07-12 04:49:00 +00001519 /* XXX When Expat supports some way of figuring out how it was
1520 compiled, this should check and set native_encoding
1521 appropriately.
1522 */
Fred Drake93adb692000-09-23 04:55:48 +00001523 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001524
Fred Drake85d835f2001-02-08 15:39:08 +00001525 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001526 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001527 errors_module = PyDict_GetItem(d, errmod_name);
1528 if (errors_module == NULL) {
1529 errors_module = PyModule_New("pyexpat.errors");
1530 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001531 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001532 /* gives away the reference to errors_module */
1533 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001534 }
1535 }
Fred Drake6f987622000-08-25 18:03:30 +00001536 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001537 model_module = PyDict_GetItem(d, modelmod_name);
1538 if (model_module == NULL) {
1539 model_module = PyModule_New("pyexpat.model");
1540 if (model_module != NULL) {
1541 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1542 /* gives away the reference to model_module */
1543 PyModule_AddObject(m, "model", model_module);
1544 }
1545 }
1546 Py_DECREF(modelmod_name);
1547 if (errors_module == NULL || model_module == NULL)
1548 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001549 return;
1550
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001551#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001552 PyModule_AddStringConstant(errors_module, #name, \
1553 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001554
Fred Drake0582df92000-07-12 04:49:00 +00001555 MYCONST(XML_ERROR_NO_MEMORY);
1556 MYCONST(XML_ERROR_SYNTAX);
1557 MYCONST(XML_ERROR_NO_ELEMENTS);
1558 MYCONST(XML_ERROR_INVALID_TOKEN);
1559 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1560 MYCONST(XML_ERROR_PARTIAL_CHAR);
1561 MYCONST(XML_ERROR_TAG_MISMATCH);
1562 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1563 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1564 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1565 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1566 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1567 MYCONST(XML_ERROR_ASYNC_ENTITY);
1568 MYCONST(XML_ERROR_BAD_CHAR_REF);
1569 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1570 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1571 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1572 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1573 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001574 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1575 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1576 MYCONST(XML_ERROR_NOT_STANDALONE);
1577
Fred Drake85d835f2001-02-08 15:39:08 +00001578 PyModule_AddStringConstant(errors_module, "__doc__",
1579 "Constants used to describe error conditions.");
1580
Fred Drake93adb692000-09-23 04:55:48 +00001581#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001582
1583#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001584#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001585 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1586 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1587 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001588#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001589#endif
1590
Fred Drake85d835f2001-02-08 15:39:08 +00001591#if EXPAT_VERSION >= 0x015f00
1592#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1593 PyModule_AddStringConstant(model_module, "__doc__",
1594 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001595
Fred Drake85d835f2001-02-08 15:39:08 +00001596 MYCONST(XML_CTYPE_EMPTY);
1597 MYCONST(XML_CTYPE_ANY);
1598 MYCONST(XML_CTYPE_MIXED);
1599 MYCONST(XML_CTYPE_NAME);
1600 MYCONST(XML_CTYPE_CHOICE);
1601 MYCONST(XML_CTYPE_SEQ);
1602
1603 MYCONST(XML_CQUANT_NONE);
1604 MYCONST(XML_CQUANT_OPT);
1605 MYCONST(XML_CQUANT_REP);
1606 MYCONST(XML_CQUANT_PLUS);
1607#undef MYCONST
1608#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001609}
1610
Fred Drake6f987622000-08-25 18:03:30 +00001611static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001612clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001613{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001614 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001615
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001616 for (; handler_info[i].name!=NULL; i++) {
1617 if (decref){
1618 Py_XDECREF(self->handlers[i]);
1619 }
1620 self->handlers[i]=NULL;
1621 handler_info[i].setter(self->itself, NULL);
1622 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001623}
1624
Fred Drake6f987622000-08-25 18:03:30 +00001625typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001626
Fred Drake6f987622000-08-25 18:03:30 +00001627static void
1628pyxml_UpdatePairedHandlers(xmlparseobject *self,
1629 int startHandler,
1630 int endHandler,
1631 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001632{
1633 void *start_handler=NULL;
1634 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001635
Fred Drake0582df92000-07-12 04:49:00 +00001636 if (self->handlers[startHandler]
1637 && self->handlers[endHandler]!=Py_None) {
1638 start_handler=handler_info[startHandler].handler;
1639 }
1640 if (self->handlers[EndElement]
1641 && self->handlers[EndElement] !=Py_None) {
1642 end_handler=handler_info[endHandler].handler;
1643 }
1644 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001645}
1646
Fred Drake6f987622000-08-25 18:03:30 +00001647static void
1648pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001649{
1650 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1651 StartElement, EndElement,
1652 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001653}
1654
Fred Drake6f987622000-08-25 18:03:30 +00001655static void
1656pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001657{
1658 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1659 StartElement, EndElement,
1660 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001661}
1662
Fred Drake6f987622000-08-25 18:03:30 +00001663static void
1664pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001665{
1666 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1667 StartNamespaceDecl, EndNamespaceDecl,
1668 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001669}
1670
Fred Drake6f987622000-08-25 18:03:30 +00001671static void
1672pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001673{
1674 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1675 StartNamespaceDecl, EndNamespaceDecl,
1676 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001677}
1678
Fred Drake6f987622000-08-25 18:03:30 +00001679static void
1680pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001681{
1682 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1683 StartCdataSection, EndCdataSection,
1684 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001685}
1686
Fred Drake6f987622000-08-25 18:03:30 +00001687static void
1688pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001689{
1690 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1691 StartCdataSection, EndCdataSection,
1692 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001693}
1694
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001695#if EXPAT_VERSION >= 0x010200
1696
1697static void
1698pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1699{
1700 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1701 StartDoctypeDecl, EndDoctypeDecl,
1702 (pairsetter)XML_SetDoctypeDeclHandler);
1703}
1704
1705static void
1706pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1707{
1708 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1709 StartDoctypeDecl, EndDoctypeDecl,
1710 (pairsetter)XML_SetDoctypeDeclHandler);
1711}
1712
1713#endif
1714
Fred Drake0582df92000-07-12 04:49:00 +00001715statichere struct HandlerInfo handler_info[] = {
1716 {"StartElementHandler",
1717 pyxml_SetStartElementHandler,
1718 (xmlhandler)my_StartElementHandler},
1719 {"EndElementHandler",
1720 pyxml_SetEndElementHandler,
1721 (xmlhandler)my_EndElementHandler},
1722 {"ProcessingInstructionHandler",
1723 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1724 (xmlhandler)my_ProcessingInstructionHandler},
1725 {"CharacterDataHandler",
1726 (xmlhandlersetter)XML_SetCharacterDataHandler,
1727 (xmlhandler)my_CharacterDataHandler},
1728 {"UnparsedEntityDeclHandler",
1729 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1730 (xmlhandler)my_UnparsedEntityDeclHandler },
1731 {"NotationDeclHandler",
1732 (xmlhandlersetter)XML_SetNotationDeclHandler,
1733 (xmlhandler)my_NotationDeclHandler },
1734 {"StartNamespaceDeclHandler",
1735 pyxml_SetStartNamespaceDeclHandler,
1736 (xmlhandler)my_StartNamespaceDeclHandler },
1737 {"EndNamespaceDeclHandler",
1738 pyxml_SetEndNamespaceDeclHandler,
1739 (xmlhandler)my_EndNamespaceDeclHandler },
1740 {"CommentHandler",
1741 (xmlhandlersetter)XML_SetCommentHandler,
1742 (xmlhandler)my_CommentHandler},
1743 {"StartCdataSectionHandler",
1744 pyxml_SetStartCdataSection,
1745 (xmlhandler)my_StartCdataSectionHandler},
1746 {"EndCdataSectionHandler",
1747 pyxml_SetEndCdataSection,
1748 (xmlhandler)my_EndCdataSectionHandler},
1749 {"DefaultHandler",
1750 (xmlhandlersetter)XML_SetDefaultHandler,
1751 (xmlhandler)my_DefaultHandler},
1752 {"DefaultHandlerExpand",
1753 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1754 (xmlhandler)my_DefaultHandlerExpandHandler},
1755 {"NotStandaloneHandler",
1756 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1757 (xmlhandler)my_NotStandaloneHandler},
1758 {"ExternalEntityRefHandler",
1759 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1760 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001761#if EXPAT_VERSION >= 0x010200
1762 {"StartDoctypeDeclHandler",
1763 pyxml_SetStartDoctypeDeclHandler,
1764 (xmlhandler)my_StartDoctypeDeclHandler},
1765 {"EndDoctypeDeclHandler",
1766 pyxml_SetEndDoctypeDeclHandler,
1767 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001768#endif
1769#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001770 {"ExternalParsedEntityDeclHandler",
1771 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1772 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1773 {"InternalParsedEntityDeclHandler",
1774 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1775 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001776#endif
1777#if EXPAT_VERSION >= 0x015f00
1778 {"EntityDeclHandler",
1779 (xmlhandlersetter)XML_SetEntityDeclHandler,
1780 (xmlhandler)my_EntityDeclHandler},
1781 {"XmlDeclHandler",
1782 (xmlhandlersetter)XML_SetXmlDeclHandler,
1783 (xmlhandler)my_XmlDeclHandler},
1784 {"ElementDeclHandler",
1785 (xmlhandlersetter)XML_SetElementDeclHandler,
1786 (xmlhandler)my_ElementDeclHandler},
1787 {"AttlistDeclHandler",
1788 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1789 (xmlhandler)my_AttlistDeclHandler},
1790#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001791
Fred Drake0582df92000-07-12 04:49:00 +00001792 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001793};