blob: 3153693dd277c0f44c14873afaee8e6dd535b18d [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake8188e792001-11-18 02:36:07 +00002#if PY_VERSION_HEX < 0x020000B1
3#include <assert.h>
4#endif
Fred Drake4113b132001-03-24 19:58:26 +00005#include <ctype.h>
6
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007#include "compile.h"
8#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00009#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010
11#ifndef PyGC_HEAD_SIZE
12#define PyGC_HEAD_SIZE 0
13#define PyObject_GC_Init(x)
14#define PyObject_GC_Fini(m)
15#define Py_TPFLAGS_GC 0
16#endif
17
Martin v. Löwis339d0f72001-08-17 18:39:25 +000018#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
19/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
20#define Py_USING_UNICODE
21#endif
22
Fred Drake0582df92000-07-12 04:49:00 +000023enum HandlerTypes {
24 StartElement,
25 EndElement,
26 ProcessingInstruction,
27 CharacterData,
28 UnparsedEntityDecl,
29 NotationDecl,
30 StartNamespaceDecl,
31 EndNamespaceDecl,
32 Comment,
33 StartCdataSection,
34 EndCdataSection,
35 Default,
36 DefaultHandlerExpand,
37 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000038 ExternalEntityRef,
39 StartDoctypeDecl,
40 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000041 EntityDecl,
42 XmlDecl,
43 ElementDecl,
44 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000045 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000046};
47
48static PyObject *ErrorObject;
49
50/* ----------------------------------------------------- */
51
52/* Declarations for objects of type xmlparser */
53
54typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000055 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056
Fred Drake0582df92000-07-12 04:49:00 +000057 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000058 int returns_unicode; /* True if Unicode strings are returned;
59 if false, UTF-8 strings are returned */
60 int ordered_attributes; /* Return attributes as a list. */
61 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000062 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000063 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000064} xmlparseobject;
65
66staticforward PyTypeObject Xmlparsetype;
67
Fred Drake6f987622000-08-25 18:03:30 +000068typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000069typedef void* xmlhandler;
70
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000071struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000072 const char *name;
73 xmlhandlersetter setter;
74 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000075 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076};
77
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000078staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000079
Fred Drakebd6101c2001-02-14 18:29:45 +000080/* Set an integer attribute on the error object; return true on success,
81 * false on an exception.
82 */
83static int
84set_error_attr(PyObject *err, char *name, int value)
85{
86 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000087
Fred Drakebd6101c2001-02-14 18:29:45 +000088 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
89 Py_DECREF(v);
90 return 0;
91 }
92 return 1;
93}
94
95/* Build and set an Expat exception, including positioning
96 * information. Always returns NULL.
97 */
Fred Drake85d835f2001-02-08 15:39:08 +000098static PyObject *
99set_error(xmlparseobject *self)
100{
101 PyObject *err;
102 char buffer[256];
103 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000104 int lineno = XML_GetErrorLineNumber(parser);
105 int column = XML_GetErrorColumnNumber(parser);
106 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000107
Tim Peters885d4572001-11-28 20:27:42 +0000108 PyOS_snprintf(buffer, sizeof(buffer), "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000109 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000110 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000111 if ( err != NULL
112 && set_error_attr(err, "code", code)
113 && set_error_attr(err, "offset", column)
114 && set_error_attr(err, "lineno", lineno)) {
115 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000116 }
117 return NULL;
118}
119
120
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000121#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000122/* Convert a string of XML_Chars into a Unicode string.
123 Returns None if str is a null pointer. */
124
Fred Drake0582df92000-07-12 04:49:00 +0000125static PyObject *
126conv_string_to_unicode(XML_Char *str)
127{
128 /* XXX currently this code assumes that XML_Char is 8-bit,
129 and hence in UTF-8. */
130 /* UTF-8 from Expat, Unicode desired */
131 if (str == NULL) {
132 Py_INCREF(Py_None);
133 return Py_None;
134 }
135 return PyUnicode_DecodeUTF8((const char *)str,
136 strlen((const char *)str),
137 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000138}
139
Fred Drake0582df92000-07-12 04:49:00 +0000140static PyObject *
141conv_string_len_to_unicode(const XML_Char *str, int len)
142{
143 /* XXX currently this code assumes that XML_Char is 8-bit,
144 and hence in UTF-8. */
145 /* UTF-8 from Expat, Unicode desired */
146 if (str == NULL) {
147 Py_INCREF(Py_None);
148 return Py_None;
149 }
Fred Drake6f987622000-08-25 18:03:30 +0000150 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000151}
152#endif
153
154/* Convert a string of XML_Chars into an 8-bit Python string.
155 Returns None if str is a null pointer. */
156
Fred Drake6f987622000-08-25 18:03:30 +0000157static PyObject *
158conv_string_to_utf8(XML_Char *str)
159{
160 /* XXX currently this code assumes that XML_Char is 8-bit,
161 and hence in UTF-8. */
162 /* UTF-8 from Expat, UTF-8 desired */
163 if (str == NULL) {
164 Py_INCREF(Py_None);
165 return Py_None;
166 }
167 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000168}
169
Fred Drake6f987622000-08-25 18:03:30 +0000170static PyObject *
171conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000172{
Fred Drake6f987622000-08-25 18:03:30 +0000173 /* XXX currently this code assumes that XML_Char is 8-bit,
174 and hence in UTF-8. */
175 /* UTF-8 from Expat, UTF-8 desired */
176 if (str == NULL) {
177 Py_INCREF(Py_None);
178 return Py_None;
179 }
180 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000181}
182
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000183/* Callback routines */
184
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000185static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000186
Fred Drake6f987622000-08-25 18:03:30 +0000187static void
188flag_error(xmlparseobject *self)
189{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000190 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000191}
192
193static PyCodeObject*
194getcode(enum HandlerTypes slot, char* func_name, int lineno)
195{
Fred Drakebd6101c2001-02-14 18:29:45 +0000196 PyObject *code = NULL;
197 PyObject *name = NULL;
198 PyObject *nulltuple = NULL;
199 PyObject *filename = NULL;
200
201 if (handler_info[slot].tb_code == NULL) {
202 code = PyString_FromString("");
203 if (code == NULL)
204 goto failed;
205 name = PyString_FromString(func_name);
206 if (name == NULL)
207 goto failed;
208 nulltuple = PyTuple_New(0);
209 if (nulltuple == NULL)
210 goto failed;
211 filename = PyString_FromString(__FILE__);
212 handler_info[slot].tb_code =
213 PyCode_New(0, /* argcount */
214 0, /* nlocals */
215 0, /* stacksize */
216 0, /* flags */
217 code, /* code */
218 nulltuple, /* consts */
219 nulltuple, /* names */
220 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000221#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000222 nulltuple, /* freevars */
223 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000224#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000225 filename, /* filename */
226 name, /* name */
227 lineno, /* firstlineno */
228 code /* lnotab */
229 );
230 if (handler_info[slot].tb_code == NULL)
231 goto failed;
232 Py_DECREF(code);
233 Py_DECREF(nulltuple);
234 Py_DECREF(filename);
235 Py_DECREF(name);
236 }
237 return handler_info[slot].tb_code;
238 failed:
239 Py_XDECREF(code);
240 Py_XDECREF(name);
241 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000242}
243
244static PyObject*
245call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
246{
Fred Drakebd6101c2001-02-14 18:29:45 +0000247 PyThreadState *tstate = PyThreadState_GET();
248 PyFrameObject *f;
249 PyObject *res;
250
251 if (c == NULL)
252 return NULL;
253 f = PyFrame_New(
254 tstate, /*back*/
255 c, /*code*/
256 tstate->frame->f_globals, /*globals*/
257 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000258 );
259 if (f == NULL)
260 return NULL;
261 tstate->frame = f;
262 res = PyEval_CallObject(func, args);
263 if (res == NULL && tstate->curexc_traceback == NULL)
264 PyTraceBack_Here(f);
265 tstate->frame = f->f_back;
266 Py_DECREF(f);
267 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000268}
269
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000270#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000271#define STRING_CONV_FUNC conv_string_to_utf8
272#else
273/* Python 1.6 and later versions */
274#define STRING_CONV_FUNC (self->returns_unicode \
275 ? conv_string_to_unicode : conv_string_to_utf8)
276#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000277
Fred Drake85d835f2001-02-08 15:39:08 +0000278static void
279my_StartElementHandler(void *userData,
280 const XML_Char *name, const XML_Char **atts)
281{
282 xmlparseobject *self = (xmlparseobject *)userData;
283
284 if (self->handlers[StartElement]
285 && self->handlers[StartElement] != Py_None) {
286 PyObject *container, *rv, *args;
287 int i, max;
288
289 /* Set max to the number of slots filled in atts[]; max/2 is
290 * the number of attributes we need to process.
291 */
292 if (self->specified_attributes) {
293 max = XML_GetSpecifiedAttributeCount(self->itself);
294 }
295 else {
296 max = 0;
297 while (atts[max] != NULL)
298 max += 2;
299 }
300 /* Build the container. */
301 if (self->ordered_attributes)
302 container = PyList_New(max);
303 else
304 container = PyDict_New();
305 if (container == NULL) {
306 flag_error(self);
307 return;
308 }
309 for (i = 0; i < max; i += 2) {
310 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
311 PyObject *v;
312 if (n == NULL) {
313 flag_error(self);
314 Py_DECREF(container);
315 return;
316 }
317 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
318 if (v == NULL) {
319 flag_error(self);
320 Py_DECREF(container);
321 Py_DECREF(n);
322 return;
323 }
324 if (self->ordered_attributes) {
325 PyList_SET_ITEM(container, i, n);
326 PyList_SET_ITEM(container, i+1, v);
327 }
328 else if (PyDict_SetItem(container, n, v)) {
329 flag_error(self);
330 Py_DECREF(n);
331 Py_DECREF(v);
332 return;
333 }
334 else {
335 Py_DECREF(n);
336 Py_DECREF(v);
337 }
338 }
339 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
340 if (args == NULL) {
341 Py_DECREF(container);
342 return;
343 }
344 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000345 self->in_callback = 1;
346 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000347 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000348 self->in_callback = 0;
349 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000350 if (rv == NULL) {
351 flag_error(self);
352 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000353 }
Fred Drake85d835f2001-02-08 15:39:08 +0000354 Py_DECREF(rv);
355 }
356}
357
358#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
359 RETURN, GETUSERDATA) \
360static RC \
361my_##NAME##Handler PARAMS {\
362 xmlparseobject *self = GETUSERDATA ; \
363 PyObject *args = NULL; \
364 PyObject *rv = NULL; \
365 INIT \
366\
367 if (self->handlers[NAME] \
368 && self->handlers[NAME] != Py_None) { \
369 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000370 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000371 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000372 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
373 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000374 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000375 Py_DECREF(args); \
376 if (rv == NULL) { \
377 flag_error(self); \
378 return RETURN; \
379 } \
380 CONVERSION \
381 Py_DECREF(rv); \
382 } \
383 return RETURN; \
384}
385
Fred Drake6f987622000-08-25 18:03:30 +0000386#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
387 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
388 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000389
Fred Drake6f987622000-08-25 18:03:30 +0000390#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
391 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
392 rc = PyInt_AsLong(rv);, rc, \
393 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000394
Fred Drake6f987622000-08-25 18:03:30 +0000395VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000396 (void *userData, const XML_Char *name),
397 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000398
Fred Drake6f987622000-08-25 18:03:30 +0000399VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000400 (void *userData,
401 const XML_Char *target,
402 const XML_Char *data),
403 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000404
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000405#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000406VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000407 (void *userData, const XML_Char *data, int len),
408 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000409#else
Fred Drake6f987622000-08-25 18:03:30 +0000410VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000411 (void *userData, const XML_Char *data, int len),
412 ("(N)", (self->returns_unicode
413 ? conv_string_len_to_unicode(data,len)
414 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000415#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000416
Fred Drake6f987622000-08-25 18:03:30 +0000417VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000418 (void *userData,
419 const XML_Char *entityName,
420 const XML_Char *base,
421 const XML_Char *systemId,
422 const XML_Char *publicId,
423 const XML_Char *notationName),
424 ("(O&O&O&O&O&)",
425 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
426 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
427 STRING_CONV_FUNC,notationName))
428
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000429#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000430VOID_HANDLER(EntityDecl,
431 (void *userData,
432 const XML_Char *entityName,
433 int is_parameter_entity,
434 const XML_Char *value,
435 int value_length,
436 const XML_Char *base,
437 const XML_Char *systemId,
438 const XML_Char *publicId,
439 const XML_Char *notationName),
440 ("O&iNO&O&O&O&",
441 STRING_CONV_FUNC,entityName, is_parameter_entity,
442 conv_string_len_to_utf8(value, value_length),
443 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
444 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
445#else
446VOID_HANDLER(EntityDecl,
447 (void *userData,
448 const XML_Char *entityName,
449 int is_parameter_entity,
450 const XML_Char *value,
451 int value_length,
452 const XML_Char *base,
453 const XML_Char *systemId,
454 const XML_Char *publicId,
455 const XML_Char *notationName),
456 ("O&iNO&O&O&O&",
457 STRING_CONV_FUNC,entityName, is_parameter_entity,
458 (self->returns_unicode
459 ? conv_string_len_to_unicode(value, value_length)
460 : conv_string_len_to_utf8(value, value_length)),
461 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
462 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
463#endif
464
465VOID_HANDLER(XmlDecl,
466 (void *userData,
467 const XML_Char *version,
468 const XML_Char *encoding,
469 int standalone),
470 ("(O&O&i)",
471 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
472 standalone))
473
474static PyObject *
475conv_content_model(XML_Content * const model,
476 PyObject *(*conv_string)(XML_Char *))
477{
478 PyObject *result = NULL;
479 PyObject *children = PyTuple_New(model->numchildren);
480 int i;
481
482 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000483 assert(model->numchildren < INT_MAX);
484 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000485 PyObject *child = conv_content_model(&model->children[i],
486 conv_string);
487 if (child == NULL) {
488 Py_XDECREF(children);
489 return NULL;
490 }
491 PyTuple_SET_ITEM(children, i, child);
492 }
493 result = Py_BuildValue("(iiO&N)",
494 model->type, model->quant,
495 conv_string,model->name, children);
496 }
497 return result;
498}
499
500static PyObject *
501conv_content_model_utf8(XML_Content * const model)
502{
503 return conv_content_model(model, conv_string_to_utf8);
504}
505
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000506#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000507static PyObject *
508conv_content_model_unicode(XML_Content * const model)
509{
510 return conv_content_model(model, conv_string_to_unicode);
511}
512
513VOID_HANDLER(ElementDecl,
514 (void *userData,
515 const XML_Char *name,
516 XML_Content *model),
517 ("O&O&",
518 STRING_CONV_FUNC,name,
519 (self->returns_unicode ? conv_content_model_unicode
520 : conv_content_model_utf8),model))
521#else
522VOID_HANDLER(ElementDecl,
523 (void *userData,
524 const XML_Char *name,
525 XML_Content *model),
526 ("O&O&",
527 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
528#endif
529
530VOID_HANDLER(AttlistDecl,
531 (void *userData,
532 const XML_Char *elname,
533 const XML_Char *attname,
534 const XML_Char *att_type,
535 const XML_Char *dflt,
536 int isrequired),
537 ("(O&O&O&O&i)",
538 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
539 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
540 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000541
Fred Drake6f987622000-08-25 18:03:30 +0000542VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000543 (void *userData,
544 const XML_Char *notationName,
545 const XML_Char *base,
546 const XML_Char *systemId,
547 const XML_Char *publicId),
548 ("(O&O&O&O&)",
549 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
550 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000551
Fred Drake6f987622000-08-25 18:03:30 +0000552VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000553 (void *userData,
554 const XML_Char *prefix,
555 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000556 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000557
Fred Drake6f987622000-08-25 18:03:30 +0000558VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000559 (void *userData,
560 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000561 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000562
Fred Drake6f987622000-08-25 18:03:30 +0000563VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000564 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000565 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000566
Fred Drake6f987622000-08-25 18:03:30 +0000567VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000568 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000569 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000570
Fred Drake6f987622000-08-25 18:03:30 +0000571VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000572 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000573 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000574
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000575#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000576VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000577 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000578 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000579
Fred Drake6f987622000-08-25 18:03:30 +0000580VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000581 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000582 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000583#else
Fred Drake6f987622000-08-25 18:03:30 +0000584VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000585 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000586 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000587 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000588 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000589
Fred Drake6f987622000-08-25 18:03:30 +0000590VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000591 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000592 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000593 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000594 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000595#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000596
Fred Drake6f987622000-08-25 18:03:30 +0000597INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000598 (void *userData),
599 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000600
Fred Drake6f987622000-08-25 18:03:30 +0000601RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000602 (XML_Parser parser,
603 const XML_Char *context,
604 const XML_Char *base,
605 const XML_Char *systemId,
606 const XML_Char *publicId),
607 int rc=0;,
608 ("(O&O&O&O&)",
609 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000610 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
611 rc = PyInt_AsLong(rv);, rc,
612 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000613
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000614/* XXX UnknownEncodingHandler */
615
Fred Drake85d835f2001-02-08 15:39:08 +0000616VOID_HANDLER(StartDoctypeDecl,
617 (void *userData, const XML_Char *doctypeName,
618 const XML_Char *sysid, const XML_Char *pubid,
619 int has_internal_subset),
620 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
621 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
622 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000623
624VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000625
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000626/* ---------------------------------------------------------------- */
627
628static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000629"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000630Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000631
632static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000633xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000634{
Fred Drake0582df92000-07-12 04:49:00 +0000635 char *s;
636 int slen;
637 int isFinal = 0;
638 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000639
Fred Drake0582df92000-07-12 04:49:00 +0000640 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
641 return NULL;
642 rv = XML_Parse(self->itself, s, slen, isFinal);
643 if (PyErr_Occurred()) {
644 return NULL;
645 }
646 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000647 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000648 }
649 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000650}
651
Fred Drakeca1f4262000-09-21 20:10:23 +0000652/* File reading copied from cPickle */
653
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000654#define BUF_SIZE 2048
655
Fred Drake0582df92000-07-12 04:49:00 +0000656static int
657readinst(char *buf, int buf_size, PyObject *meth)
658{
659 PyObject *arg = NULL;
660 PyObject *bytes = NULL;
661 PyObject *str = NULL;
662 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000663
Fred Drake676940b2000-09-22 15:21:31 +0000664 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000665 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000666
Fred Drakeca1f4262000-09-21 20:10:23 +0000667 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000668 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000669
Tim Peters954eef72000-09-22 06:01:11 +0000670 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000671
Fred Drakeca1f4262000-09-21 20:10:23 +0000672 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000673 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674
Fred Drake0582df92000-07-12 04:49:00 +0000675 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000676 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000677 PyErr_Format(PyExc_TypeError,
678 "read() did not return a string object (type=%.400s)",
679 str->ob_type->tp_name);
680 goto finally;
681 }
682 len = PyString_GET_SIZE(str);
683 if (len > buf_size) {
684 PyErr_Format(PyExc_ValueError,
685 "read() returned too much data: "
686 "%i bytes requested, %i returned",
687 buf_size, len);
688 Py_DECREF(str);
689 goto finally;
690 }
691 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000692finally:
Fred Drake0582df92000-07-12 04:49:00 +0000693 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000694 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000695 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000696}
697
698static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000699"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000700Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000701
702static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000703xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000704{
Fred Drake0582df92000-07-12 04:49:00 +0000705 int rv = 1;
706 PyObject *f;
707 FILE *fp;
708 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000709
Fred Drake0582df92000-07-12 04:49:00 +0000710 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
711 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000712
Fred Drake0582df92000-07-12 04:49:00 +0000713 if (PyFile_Check(f)) {
714 fp = PyFile_AsFile(f);
715 }
716 else{
717 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000718 readmethod = PyObject_GetAttrString(f, "read");
719 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000720 PyErr_Clear();
721 PyErr_SetString(PyExc_TypeError,
722 "argument must have 'read' attribute");
723 return 0;
724 }
725 }
726 for (;;) {
727 int bytes_read;
728 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
729 if (buf == NULL)
730 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000731
Fred Drake0582df92000-07-12 04:49:00 +0000732 if (fp) {
733 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
734 if (bytes_read < 0) {
735 PyErr_SetFromErrno(PyExc_IOError);
736 return NULL;
737 }
738 }
739 else {
740 bytes_read = readinst(buf, BUF_SIZE, readmethod);
741 if (bytes_read < 0)
742 return NULL;
743 }
744 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
745 if (PyErr_Occurred())
746 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000747
Fred Drake0582df92000-07-12 04:49:00 +0000748 if (!rv || bytes_read == 0)
749 break;
750 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000751 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000752 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000753 }
Fred Drake0582df92000-07-12 04:49:00 +0000754 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000755}
756
757static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000758"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000759Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000760
761static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000762xmlparse_SetBase(xmlparseobject *self, PyObject *args)
763{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764 char *base;
765
Fred Drake0582df92000-07-12 04:49:00 +0000766 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000767 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000768 if (!XML_SetBase(self->itself, base)) {
769 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000771 Py_INCREF(Py_None);
772 return Py_None;
773}
774
775static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000776"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000777Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000778
779static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000780xmlparse_GetBase(xmlparseobject *self, PyObject *args)
781{
782 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000784
Fred Drake0582df92000-07-12 04:49:00 +0000785 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786}
787
Fred Drakebd6101c2001-02-14 18:29:45 +0000788static char xmlparse_GetInputContext__doc__[] =
789"GetInputContext() -> string\n\
790Return the untranslated text of the input that caused the current event.\n\
791If the event was generated by a large amount of text (such as a start tag\n\
792for an element with many attributes), not all of the text may be available.";
793
794static PyObject *
795xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
796{
797 PyObject *result = NULL;
798
799 if (PyArg_ParseTuple(args, ":GetInputContext")) {
800 if (self->in_callback) {
801 int offset, size;
802 const char *buffer
803 = XML_GetInputContext(self->itself, &offset, &size);
804
805 if (buffer != NULL)
806 result = PyString_FromStringAndSize(buffer + offset, size);
807 else {
808 result = Py_None;
809 Py_INCREF(result);
810 }
811 }
812 else {
813 result = Py_None;
814 Py_INCREF(result);
815 }
816 }
817 return result;
818}
Fred Drakebd6101c2001-02-14 18:29:45 +0000819
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000820static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000821"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000822Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000823information passed to the ExternalEntityRefHandler.";
824
825static PyObject *
826xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
827{
828 char *context;
829 char *encoding = NULL;
830 xmlparseobject *new_parser;
831 int i;
832
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000833 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000834 &context, &encoding)) {
835 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000836 }
837
838#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
839 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000840#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000841#ifndef Py_TPFLAGS_HAVE_GC
842 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000843 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000844#else
845 /* Python versions 2.2 and later */
846 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
847#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000848#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000849
850 if (new_parser == NULL)
851 return NULL;
852 new_parser->returns_unicode = self->returns_unicode;
853 new_parser->ordered_attributes = self->ordered_attributes;
854 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000855 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000856 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000857 encoding);
858 new_parser->handlers = 0;
Martin v. Löwis894258c2001-09-23 10:20:10 +0000859#ifdef Py_TPFLAGS_HAVE_GC
860 PyObject_GC_Track(new_parser);
861#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000862 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000863#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000864
865 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000866 Py_DECREF(new_parser);
867 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000868 }
869
870 XML_SetUserData(new_parser->itself, (void *)new_parser);
871
872 /* allocate and clear handlers first */
873 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000874 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000875
876 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000877 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000878 Py_DECREF(new_parser);
879 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000880 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000881 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000882
883 /* then copy handlers from self */
884 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +0000885 if (self->handlers[i]) {
886 Py_INCREF(self->handlers[i]);
887 new_parser->handlers[i] = self->handlers[i];
888 handler_info[i].setter(new_parser->itself,
889 handler_info[i].handler);
890 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000891 }
Fred Drake28adf522000-09-24 22:07:59 +0000892 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000893}
894
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000895static char xmlparse_SetParamEntityParsing__doc__[] =
896"SetParamEntityParsing(flag) -> success\n\
897Controls parsing of parameter entities (including the external DTD\n\
898subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
899XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
900XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
901was successful.";
902
903static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +0000904xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000905{
Fred Drake85d835f2001-02-08 15:39:08 +0000906 int flag;
907 if (!PyArg_ParseTuple(args, "i", &flag))
908 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +0000909 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +0000910 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000911}
912
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000913static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +0000914 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +0000915 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000916 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +0000917 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000918 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000919 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000920 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000921 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000922 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
923 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000924 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
925 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000926 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
927 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000928 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000929};
930
931/* ---------- */
932
933
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000934#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000935
936/*
937 pyexpat international encoding support.
938 Make it as simple as possible.
939*/
940
Martin v. Löwis3af7cc02001-01-22 08:19:10 +0000941static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +0000942PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000943
944static void
945init_template_buffer(void)
946{
947 int i;
Fred Drakebb66a202001-03-01 20:48:17 +0000948 for (i = 0; i < 256; i++) {
949 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +0000950 }
Fred Drakebb66a202001-03-01 20:48:17 +0000951 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +0000952}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000953
954int
955PyUnknownEncodingHandler(void *encodingHandlerData,
956const XML_Char *name,
957XML_Encoding * info)
958{
Fred Drakebb66a202001-03-01 20:48:17 +0000959 PyUnicodeObject *_u_string = NULL;
960 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000961 int i;
962
Fred Drakebb66a202001-03-01 20:48:17 +0000963 /* Yes, supports only 8bit encodings */
964 _u_string = (PyUnicodeObject *)
965 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000966
Fred Drakebb66a202001-03-01 20:48:17 +0000967 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000968 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000969
Fred Drakebb66a202001-03-01 20:48:17 +0000970 for (i = 0; i < 256; i++) {
971 /* Stupid to access directly, but fast */
972 Py_UNICODE c = _u_string->str[i];
973 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000974 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +0000975 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000976 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +0000977 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000978
979 info->data = NULL;
980 info->convert = NULL;
981 info->release = NULL;
982 result=1;
983
984 Py_DECREF(_u_string);
985 return result;
986}
987
988#endif
989
990static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000991newxmlparseobject(char *encoding, char *namespace_separator)
992{
993 int i;
994 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000995
996#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +0000997 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
998 if (self == NULL)
999 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001000
Fred Drake0582df92000-07-12 04:49:00 +00001001 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001002#else
Fred Drake0582df92000-07-12 04:49:00 +00001003 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001004#ifdef Py_TPFLAGS_HAVE_GC
1005 /* Code for versions 2.2 and later */
1006 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1007#else
Fred Drake0582df92000-07-12 04:49:00 +00001008 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001009#endif
Fred Drake0582df92000-07-12 04:49:00 +00001010 if (self == NULL)
1011 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001012
Fred Drake0582df92000-07-12 04:49:00 +00001013 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001014#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001015 self->ordered_attributes = 0;
1016 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001017 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001018 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001019 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001020 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1021 }
Fred Drake85d835f2001-02-08 15:39:08 +00001022 else {
Fred Drake0582df92000-07-12 04:49:00 +00001023 self->itself = XML_ParserCreate(encoding);
1024 }
Martin v. Löwis894258c2001-09-23 10:20:10 +00001025#ifdef Py_TPFLAGS_HAVE_GC
1026 PyObject_GC_Track(self);
1027#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001028 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001029#endif
Fred Drake0582df92000-07-12 04:49:00 +00001030 if (self->itself == NULL) {
1031 PyErr_SetString(PyExc_RuntimeError,
1032 "XML_ParserCreate failed");
1033 Py_DECREF(self);
1034 return NULL;
1035 }
1036 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001037#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1039#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001040
Fred Drake0582df92000-07-12 04:49:00 +00001041 for(i = 0; handler_info[i].name != NULL; i++)
1042 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001043
Fred Drake0582df92000-07-12 04:49:00 +00001044 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001045 if (!self->handlers){
1046 Py_DECREF(self);
1047 return PyErr_NoMemory();
1048 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001049 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001050
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001051 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001052}
1053
1054
1055static void
Fred Drake0582df92000-07-12 04:49:00 +00001056xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001057{
Fred Drake0582df92000-07-12 04:49:00 +00001058 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001059#ifdef Py_TPFLAGS_HAVE_GC
1060 PyObject_GC_UnTrack(self);
1061#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001062 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001063#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001064 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001065 XML_ParserFree(self->itself);
1066 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001067
Fred Drake85d835f2001-02-08 15:39:08 +00001068 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001069 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001070 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001071 temp = self->handlers[i];
1072 self->handlers[i] = NULL;
1073 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001074 }
1075 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001076 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001077#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001078 /* Code for versions before 1.6 */
1079 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001080#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001081#ifndef Py_TPFLAGS_HAVE_GC
1082 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001083 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001084#else
1085 /* Code for versions 2.2 and later. */
1086 PyObject_GC_Del(self);
1087#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001088#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001089}
1090
Fred Drake0582df92000-07-12 04:49:00 +00001091static int
1092handlername2int(const char *name)
1093{
1094 int i;
1095 for (i=0; handler_info[i].name != NULL; i++) {
1096 if (strcmp(name, handler_info[i].name) == 0) {
1097 return i;
1098 }
1099 }
1100 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001101}
1102
1103static PyObject *
1104xmlparse_getattr(xmlparseobject *self, char *name)
1105{
Fred Drake0582df92000-07-12 04:49:00 +00001106 int handlernum;
1107 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001108 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001109 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001110 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001111 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001112 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001113 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001114 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1115 if (strcmp(name, "ordered_attributes") == 0)
1116 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001117 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001118 return PyInt_FromLong((long) self->returns_unicode);
1119 if (strcmp(name, "specified_attributes") == 0)
1120 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001121
Fred Drake0582df92000-07-12 04:49:00 +00001122 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001123
Fred Drake0582df92000-07-12 04:49:00 +00001124 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1125 Py_INCREF(self->handlers[handlernum]);
1126 return self->handlers[handlernum];
1127 }
1128 if (strcmp(name, "__members__") == 0) {
1129 int i;
1130 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001131 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001132 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001133 }
1134 PyList_Append(rc, PyString_FromString("ErrorCode"));
1135 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1136 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1137 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001138 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001139 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001140 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001141
Fred Drake0582df92000-07-12 04:49:00 +00001142 return rc;
1143 }
1144 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001145}
1146
Fred Drake6f987622000-08-25 18:03:30 +00001147static int
1148sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001149{
1150 int handlernum = handlername2int(name);
1151 if (handlernum != -1) {
1152 Py_INCREF(v);
1153 Py_XDECREF(self->handlers[handlernum]);
1154 self->handlers[handlernum] = v;
1155 handler_info[handlernum].setter(self->itself,
1156 handler_info[handlernum].handler);
1157 return 1;
1158 }
1159 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001160}
1161
1162static int
Fred Drake6f987622000-08-25 18:03:30 +00001163xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001164{
Fred Drake6f987622000-08-25 18:03:30 +00001165 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001166 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001167 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1168 return -1;
1169 }
Fred Drake85d835f2001-02-08 15:39:08 +00001170 if (strcmp(name, "ordered_attributes") == 0) {
1171 if (PyObject_IsTrue(v))
1172 self->ordered_attributes = 1;
1173 else
1174 self->ordered_attributes = 0;
1175 return 0;
1176 }
Fred Drake6f987622000-08-25 18:03:30 +00001177 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001178 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001179#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001180 PyErr_SetString(PyExc_ValueError,
1181 "Cannot return Unicode strings in Python 1.5");
1182 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001183#else
Fred Drake6f987622000-08-25 18:03:30 +00001184 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001185#endif
Fred Drake6f987622000-08-25 18:03:30 +00001186 }
1187 else
1188 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001189 return 0;
1190 }
1191 if (strcmp(name, "specified_attributes") == 0) {
1192 if (PyObject_IsTrue(v))
1193 self->specified_attributes = 1;
1194 else
1195 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001196 return 0;
1197 }
1198 if (sethandler(self, name, v)) {
1199 return 0;
1200 }
1201 PyErr_SetString(PyExc_AttributeError, name);
1202 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001203}
1204
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001205#ifdef WITH_CYCLE_GC
1206static int
1207xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1208{
Fred Drakecde79132001-04-25 16:01:30 +00001209 int i, err;
1210 for (i = 0; handler_info[i].name != NULL; i++) {
1211 if (!op->handlers[i])
1212 continue;
1213 err = visit(op->handlers[i], arg);
1214 if (err)
1215 return err;
1216 }
1217 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001218}
1219
1220static int
1221xmlparse_clear(xmlparseobject *op)
1222{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001223 clear_handlers(op, 0);
Fred Drakecde79132001-04-25 16:01:30 +00001224 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001225}
1226#endif
1227
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001228static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001229"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001230
1231static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001232 PyObject_HEAD_INIT(NULL)
1233 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001234 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001235 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001236 0, /*tp_itemsize*/
1237 /* methods */
1238 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1239 (printfunc)0, /*tp_print*/
1240 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1241 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1242 (cmpfunc)0, /*tp_compare*/
1243 (reprfunc)0, /*tp_repr*/
1244 0, /*tp_as_number*/
1245 0, /*tp_as_sequence*/
1246 0, /*tp_as_mapping*/
1247 (hashfunc)0, /*tp_hash*/
1248 (ternaryfunc)0, /*tp_call*/
1249 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001250 0, /* tp_getattro */
1251 0, /* tp_setattro */
1252 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001253#ifdef Py_TPFLAGS_HAVE_GC
1254 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1255#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001256 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001257#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001258 Xmlparsetype__doc__, /* Documentation string */
1259#ifdef WITH_CYCLE_GC
1260 (traverseproc)xmlparse_traverse, /* tp_traverse */
1261 (inquiry)xmlparse_clear /* tp_clear */
1262#else
1263 0, 0
1264#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001265};
1266
1267/* End of code for xmlparser objects */
1268/* -------------------------------------------------------- */
1269
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001270static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001271"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1272Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001273
1274static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001275pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1276{
Fred Drakecde79132001-04-25 16:01:30 +00001277 char *encoding = NULL;
1278 char *namespace_separator = NULL;
1279 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001280
Fred Drakecde79132001-04-25 16:01:30 +00001281 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1282 &encoding, &namespace_separator))
1283 return NULL;
1284 if (namespace_separator != NULL
1285 && strlen(namespace_separator) > 1) {
1286 PyErr_SetString(PyExc_ValueError,
1287 "namespace_separator must be at most one"
1288 " character, omitted, or None");
1289 return NULL;
1290 }
1291 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001292}
1293
1294static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001295"ErrorString(errno) -> string\n\
1296Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001297
1298static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001299pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001300{
Fred Drake0582df92000-07-12 04:49:00 +00001301 long code = 0;
1302
1303 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1304 return NULL;
1305 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001306}
1307
1308/* List of methods defined in the module */
1309
1310static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001311 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1312 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1313 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1314 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001315
Fred Drake0582df92000-07-12 04:49:00 +00001316 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001317};
1318
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001319/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001320
1321static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001322"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001323
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001324#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001325
1326/* 1.5 compatibility: PyModule_AddObject */
1327static int
1328PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1329{
Fred Drakecde79132001-04-25 16:01:30 +00001330 PyObject *dict;
1331 if (!PyModule_Check(m) || o == NULL)
1332 return -1;
1333 dict = PyModule_GetDict(m);
1334 if (dict == NULL)
1335 return -1;
1336 if (PyDict_SetItemString(dict, name, o))
1337 return -1;
1338 Py_DECREF(o);
1339 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001340}
1341
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001342int
1343PyModule_AddIntConstant(PyObject *m, char *name, long value)
1344{
Fred Drakecde79132001-04-25 16:01:30 +00001345 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001346}
1347
Fred Drakea77254a2000-09-29 19:23:29 +00001348static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001349PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1350{
Fred Drakecde79132001-04-25 16:01:30 +00001351 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001352}
1353
1354#endif
1355
Fred Drake4113b132001-03-24 19:58:26 +00001356
1357/* Return a Python string that represents the version number without the
1358 * extra cruft added by revision control, even if the right options were
1359 * given to the "cvs export" command to make it not include the extra
1360 * cruft.
1361 */
1362static PyObject *
1363get_version_string(void)
1364{
1365 static char *rcsid = "$Revision$";
1366 char *rev = rcsid;
1367 int i = 0;
1368
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001369 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001370 ++rev;
1371 while (rev[i] != ' ' && rev[i] != '\0')
1372 ++i;
1373
1374 return PyString_FromStringAndSize(rev, i);
1375}
1376
Fred Drakecde79132001-04-25 16:01:30 +00001377/* Initialization function for the module */
1378
1379#ifndef MODULE_NAME
1380#define MODULE_NAME "pyexpat"
1381#endif
1382
1383#ifndef MODULE_INITFUNC
1384#define MODULE_INITFUNC initpyexpat
1385#endif
1386
1387void MODULE_INITFUNC(void); /* avoid compiler warnings */
1388
Fred Drake6f987622000-08-25 18:03:30 +00001389DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001390MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001391{
1392 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001393 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001394 PyObject *errors_module;
1395 PyObject *modelmod_name;
1396 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001397 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001398
Fred Drake6f987622000-08-25 18:03:30 +00001399 if (errmod_name == NULL)
1400 return;
Fred Drakecde79132001-04-25 16:01:30 +00001401 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001402 if (modelmod_name == NULL)
1403 return;
Fred Drake6f987622000-08-25 18:03:30 +00001404
Fred Drake0582df92000-07-12 04:49:00 +00001405 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001406
Fred Drake0582df92000-07-12 04:49:00 +00001407 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001408 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001409 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001410
Fred Drake0582df92000-07-12 04:49:00 +00001411 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001412 if (ErrorObject == NULL) {
1413 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001414 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001415 if (ErrorObject == NULL)
1416 return;
1417 }
1418 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001419 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001420 Py_INCREF(ErrorObject);
1421 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001422 Py_INCREF(&Xmlparsetype);
1423 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001424
Fred Drake4113b132001-03-24 19:58:26 +00001425 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001426 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1427 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001428 {
1429 XML_Expat_Version info = XML_ExpatVersionInfo();
1430 PyModule_AddObject(m, "version_info",
1431 Py_BuildValue("(iii)", info.major,
1432 info.minor, info.micro));
1433 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001434#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001435 init_template_buffer();
1436#endif
Fred Drake0582df92000-07-12 04:49:00 +00001437 /* XXX When Expat supports some way of figuring out how it was
1438 compiled, this should check and set native_encoding
1439 appropriately.
1440 */
Fred Drake93adb692000-09-23 04:55:48 +00001441 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001442
Fred Drake85d835f2001-02-08 15:39:08 +00001443 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001444 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001445 errors_module = PyDict_GetItem(d, errmod_name);
1446 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001447 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001448 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001449 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001450 /* gives away the reference to errors_module */
1451 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001452 }
1453 }
Fred Drake6f987622000-08-25 18:03:30 +00001454 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001455 model_module = PyDict_GetItem(d, modelmod_name);
1456 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001457 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001458 if (model_module != NULL) {
1459 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1460 /* gives away the reference to model_module */
1461 PyModule_AddObject(m, "model", model_module);
1462 }
1463 }
1464 Py_DECREF(modelmod_name);
1465 if (errors_module == NULL || model_module == NULL)
1466 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001467 return;
1468
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001469#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001470 PyModule_AddStringConstant(errors_module, #name, \
1471 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001472
Fred Drake0582df92000-07-12 04:49:00 +00001473 MYCONST(XML_ERROR_NO_MEMORY);
1474 MYCONST(XML_ERROR_SYNTAX);
1475 MYCONST(XML_ERROR_NO_ELEMENTS);
1476 MYCONST(XML_ERROR_INVALID_TOKEN);
1477 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1478 MYCONST(XML_ERROR_PARTIAL_CHAR);
1479 MYCONST(XML_ERROR_TAG_MISMATCH);
1480 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1481 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1482 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1483 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1484 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1485 MYCONST(XML_ERROR_ASYNC_ENTITY);
1486 MYCONST(XML_ERROR_BAD_CHAR_REF);
1487 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1488 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1489 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1490 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1491 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001492 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1493 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1494 MYCONST(XML_ERROR_NOT_STANDALONE);
1495
Fred Drake85d835f2001-02-08 15:39:08 +00001496 PyModule_AddStringConstant(errors_module, "__doc__",
1497 "Constants used to describe error conditions.");
1498
Fred Drake93adb692000-09-23 04:55:48 +00001499#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001500
Fred Drake85d835f2001-02-08 15:39:08 +00001501#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001502 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1503 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1504 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001505#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001506
Fred Drake85d835f2001-02-08 15:39:08 +00001507#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1508 PyModule_AddStringConstant(model_module, "__doc__",
1509 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001510
Fred Drake85d835f2001-02-08 15:39:08 +00001511 MYCONST(XML_CTYPE_EMPTY);
1512 MYCONST(XML_CTYPE_ANY);
1513 MYCONST(XML_CTYPE_MIXED);
1514 MYCONST(XML_CTYPE_NAME);
1515 MYCONST(XML_CTYPE_CHOICE);
1516 MYCONST(XML_CTYPE_SEQ);
1517
1518 MYCONST(XML_CQUANT_NONE);
1519 MYCONST(XML_CQUANT_OPT);
1520 MYCONST(XML_CQUANT_REP);
1521 MYCONST(XML_CQUANT_PLUS);
1522#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001523}
1524
Fred Drake6f987622000-08-25 18:03:30 +00001525static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001526clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001527{
Fred Drakecde79132001-04-25 16:01:30 +00001528 int i = 0;
1529 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001530
Fred Drakecde79132001-04-25 16:01:30 +00001531 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001532 if (initial)
1533 self->handlers[i]=NULL;
1534 else {
Fred Drakecde79132001-04-25 16:01:30 +00001535 temp = self->handlers[i];
1536 self->handlers[i] = NULL;
1537 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001538 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001539 }
Fred Drakecde79132001-04-25 16:01:30 +00001540 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001541}
1542
Fred Drake6f987622000-08-25 18:03:30 +00001543typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001544
Fred Drake6f987622000-08-25 18:03:30 +00001545static void
1546pyxml_UpdatePairedHandlers(xmlparseobject *self,
1547 int startHandler,
1548 int endHandler,
1549 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001550{
Fred Drakecde79132001-04-25 16:01:30 +00001551 void *start_handler = NULL;
1552 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001553
Fred Drake0582df92000-07-12 04:49:00 +00001554 if (self->handlers[startHandler]
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001555 && self->handlers[startHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001556 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001557 }
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001558 if (self->handlers[endHandler]
1559 && self->handlers[endHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001560 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001561 }
1562 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001563}
1564
Fred Drake6f987622000-08-25 18:03:30 +00001565static void
1566pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001567{
1568 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1569 StartElement, EndElement,
1570 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001571}
1572
Fred Drake6f987622000-08-25 18:03:30 +00001573static void
1574pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001575{
1576 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1577 StartElement, EndElement,
1578 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001579}
1580
Fred Drake6f987622000-08-25 18:03:30 +00001581static void
1582pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001583{
1584 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1585 StartNamespaceDecl, EndNamespaceDecl,
1586 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001587}
1588
Fred Drake6f987622000-08-25 18:03:30 +00001589static void
1590pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001591{
1592 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1593 StartNamespaceDecl, EndNamespaceDecl,
1594 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001595}
1596
Fred Drake6f987622000-08-25 18:03:30 +00001597static void
1598pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001599{
1600 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1601 StartCdataSection, EndCdataSection,
1602 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001603}
1604
Fred Drake6f987622000-08-25 18:03:30 +00001605static void
1606pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001607{
1608 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1609 StartCdataSection, EndCdataSection,
1610 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001611}
1612
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001613static void
1614pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1615{
1616 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1617 StartDoctypeDecl, EndDoctypeDecl,
1618 (pairsetter)XML_SetDoctypeDeclHandler);
1619}
1620
1621static void
1622pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1623{
1624 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1625 StartDoctypeDecl, EndDoctypeDecl,
1626 (pairsetter)XML_SetDoctypeDeclHandler);
1627}
1628
Fred Drake0582df92000-07-12 04:49:00 +00001629statichere struct HandlerInfo handler_info[] = {
1630 {"StartElementHandler",
1631 pyxml_SetStartElementHandler,
1632 (xmlhandler)my_StartElementHandler},
1633 {"EndElementHandler",
1634 pyxml_SetEndElementHandler,
1635 (xmlhandler)my_EndElementHandler},
1636 {"ProcessingInstructionHandler",
1637 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1638 (xmlhandler)my_ProcessingInstructionHandler},
1639 {"CharacterDataHandler",
1640 (xmlhandlersetter)XML_SetCharacterDataHandler,
1641 (xmlhandler)my_CharacterDataHandler},
1642 {"UnparsedEntityDeclHandler",
1643 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1644 (xmlhandler)my_UnparsedEntityDeclHandler },
1645 {"NotationDeclHandler",
1646 (xmlhandlersetter)XML_SetNotationDeclHandler,
1647 (xmlhandler)my_NotationDeclHandler },
1648 {"StartNamespaceDeclHandler",
1649 pyxml_SetStartNamespaceDeclHandler,
1650 (xmlhandler)my_StartNamespaceDeclHandler },
1651 {"EndNamespaceDeclHandler",
1652 pyxml_SetEndNamespaceDeclHandler,
1653 (xmlhandler)my_EndNamespaceDeclHandler },
1654 {"CommentHandler",
1655 (xmlhandlersetter)XML_SetCommentHandler,
1656 (xmlhandler)my_CommentHandler},
1657 {"StartCdataSectionHandler",
1658 pyxml_SetStartCdataSection,
1659 (xmlhandler)my_StartCdataSectionHandler},
1660 {"EndCdataSectionHandler",
1661 pyxml_SetEndCdataSection,
1662 (xmlhandler)my_EndCdataSectionHandler},
1663 {"DefaultHandler",
1664 (xmlhandlersetter)XML_SetDefaultHandler,
1665 (xmlhandler)my_DefaultHandler},
1666 {"DefaultHandlerExpand",
1667 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1668 (xmlhandler)my_DefaultHandlerExpandHandler},
1669 {"NotStandaloneHandler",
1670 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1671 (xmlhandler)my_NotStandaloneHandler},
1672 {"ExternalEntityRefHandler",
1673 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1674 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001675 {"StartDoctypeDeclHandler",
1676 pyxml_SetStartDoctypeDeclHandler,
1677 (xmlhandler)my_StartDoctypeDeclHandler},
1678 {"EndDoctypeDeclHandler",
1679 pyxml_SetEndDoctypeDeclHandler,
1680 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001681 {"EntityDeclHandler",
1682 (xmlhandlersetter)XML_SetEntityDeclHandler,
1683 (xmlhandler)my_EntityDeclHandler},
1684 {"XmlDeclHandler",
1685 (xmlhandlersetter)XML_SetXmlDeclHandler,
1686 (xmlhandler)my_XmlDeclHandler},
1687 {"ElementDeclHandler",
1688 (xmlhandlersetter)XML_SetElementDeclHandler,
1689 (xmlhandler)my_ElementDeclHandler},
1690 {"AttlistDeclHandler",
1691 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1692 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001693
Fred Drake0582df92000-07-12 04:49:00 +00001694 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001695};