blob: ae8bf1b666ac1eb5fadb9b7de7b3069b8c2c4bb8 [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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000628PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000629"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000699"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000758"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000776"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000789"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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000792for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000793
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000823information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000824
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öwis14f8b4c2002-06-13 20:33:02 +0000895PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000896"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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000901was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000902
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001228PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001229
1230static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001231 PyObject_HEAD_INIT(NULL)
1232 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001233 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001234 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001235 0, /*tp_itemsize*/
1236 /* methods */
1237 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1238 (printfunc)0, /*tp_print*/
1239 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1240 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1241 (cmpfunc)0, /*tp_compare*/
1242 (reprfunc)0, /*tp_repr*/
1243 0, /*tp_as_number*/
1244 0, /*tp_as_sequence*/
1245 0, /*tp_as_mapping*/
1246 (hashfunc)0, /*tp_hash*/
1247 (ternaryfunc)0, /*tp_call*/
1248 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001249 0, /* tp_getattro */
1250 0, /* tp_setattro */
1251 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001252#ifdef Py_TPFLAGS_HAVE_GC
1253 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1254#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001255 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001256#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001257 Xmlparsetype__doc__, /* Documentation string */
1258#ifdef WITH_CYCLE_GC
1259 (traverseproc)xmlparse_traverse, /* tp_traverse */
1260 (inquiry)xmlparse_clear /* tp_clear */
1261#else
1262 0, 0
1263#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001264};
1265
1266/* End of code for xmlparser objects */
1267/* -------------------------------------------------------- */
1268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001269PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001270"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001271Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272
1273static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001274pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1275{
Fred Drakecde79132001-04-25 16:01:30 +00001276 char *encoding = NULL;
1277 char *namespace_separator = NULL;
1278 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001279
Fred Drakecde79132001-04-25 16:01:30 +00001280 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1281 &encoding, &namespace_separator))
1282 return NULL;
1283 if (namespace_separator != NULL
1284 && strlen(namespace_separator) > 1) {
1285 PyErr_SetString(PyExc_ValueError,
1286 "namespace_separator must be at most one"
1287 " character, omitted, or None");
1288 return NULL;
1289 }
1290 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001291}
1292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001293PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001294"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001295Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001296
1297static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001298pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001299{
Fred Drake0582df92000-07-12 04:49:00 +00001300 long code = 0;
1301
1302 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1303 return NULL;
1304 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001305}
1306
1307/* List of methods defined in the module */
1308
1309static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001310 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1311 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1312 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1313 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001314
Fred Drake0582df92000-07-12 04:49:00 +00001315 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001316};
1317
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001318/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(pyexpat_module_documentation,
1321"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001322
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001323#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001324
1325/* 1.5 compatibility: PyModule_AddObject */
1326static int
1327PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1328{
Fred Drakecde79132001-04-25 16:01:30 +00001329 PyObject *dict;
1330 if (!PyModule_Check(m) || o == NULL)
1331 return -1;
1332 dict = PyModule_GetDict(m);
1333 if (dict == NULL)
1334 return -1;
1335 if (PyDict_SetItemString(dict, name, o))
1336 return -1;
1337 Py_DECREF(o);
1338 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001339}
1340
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001341int
1342PyModule_AddIntConstant(PyObject *m, char *name, long value)
1343{
Fred Drakecde79132001-04-25 16:01:30 +00001344 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001345}
1346
Fred Drakea77254a2000-09-29 19:23:29 +00001347static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001348PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1349{
Fred Drakecde79132001-04-25 16:01:30 +00001350 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001351}
1352
1353#endif
1354
Fred Drake4113b132001-03-24 19:58:26 +00001355
1356/* Return a Python string that represents the version number without the
1357 * extra cruft added by revision control, even if the right options were
1358 * given to the "cvs export" command to make it not include the extra
1359 * cruft.
1360 */
1361static PyObject *
1362get_version_string(void)
1363{
1364 static char *rcsid = "$Revision$";
1365 char *rev = rcsid;
1366 int i = 0;
1367
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001368 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001369 ++rev;
1370 while (rev[i] != ' ' && rev[i] != '\0')
1371 ++i;
1372
1373 return PyString_FromStringAndSize(rev, i);
1374}
1375
Fred Drakecde79132001-04-25 16:01:30 +00001376/* Initialization function for the module */
1377
1378#ifndef MODULE_NAME
1379#define MODULE_NAME "pyexpat"
1380#endif
1381
1382#ifndef MODULE_INITFUNC
1383#define MODULE_INITFUNC initpyexpat
1384#endif
1385
1386void MODULE_INITFUNC(void); /* avoid compiler warnings */
1387
Fred Drake6f987622000-08-25 18:03:30 +00001388DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001389MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001390{
1391 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001392 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001393 PyObject *errors_module;
1394 PyObject *modelmod_name;
1395 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001396 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001397
Fred Drake6f987622000-08-25 18:03:30 +00001398 if (errmod_name == NULL)
1399 return;
Fred Drakecde79132001-04-25 16:01:30 +00001400 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001401 if (modelmod_name == NULL)
1402 return;
Fred Drake6f987622000-08-25 18:03:30 +00001403
Fred Drake0582df92000-07-12 04:49:00 +00001404 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001405
Fred Drake0582df92000-07-12 04:49:00 +00001406 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001407 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001408 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001409
Fred Drake0582df92000-07-12 04:49:00 +00001410 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001411 if (ErrorObject == NULL) {
1412 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001413 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001414 if (ErrorObject == NULL)
1415 return;
1416 }
1417 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001418 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001419 Py_INCREF(ErrorObject);
1420 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001421 Py_INCREF(&Xmlparsetype);
1422 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423
Fred Drake4113b132001-03-24 19:58:26 +00001424 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001425 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1426 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001427 {
1428 XML_Expat_Version info = XML_ExpatVersionInfo();
1429 PyModule_AddObject(m, "version_info",
1430 Py_BuildValue("(iii)", info.major,
1431 info.minor, info.micro));
1432 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001433#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001434 init_template_buffer();
1435#endif
Fred Drake0582df92000-07-12 04:49:00 +00001436 /* XXX When Expat supports some way of figuring out how it was
1437 compiled, this should check and set native_encoding
1438 appropriately.
1439 */
Fred Drake93adb692000-09-23 04:55:48 +00001440 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001441
Fred Drake85d835f2001-02-08 15:39:08 +00001442 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001443 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001444 errors_module = PyDict_GetItem(d, errmod_name);
1445 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001446 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001447 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001448 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001449 /* gives away the reference to errors_module */
1450 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001451 }
1452 }
Fred Drake6f987622000-08-25 18:03:30 +00001453 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001454 model_module = PyDict_GetItem(d, modelmod_name);
1455 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001456 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001457 if (model_module != NULL) {
1458 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1459 /* gives away the reference to model_module */
1460 PyModule_AddObject(m, "model", model_module);
1461 }
1462 }
1463 Py_DECREF(modelmod_name);
1464 if (errors_module == NULL || model_module == NULL)
1465 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001466 return;
1467
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001468#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001469 PyModule_AddStringConstant(errors_module, #name, \
1470 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001471
Fred Drake0582df92000-07-12 04:49:00 +00001472 MYCONST(XML_ERROR_NO_MEMORY);
1473 MYCONST(XML_ERROR_SYNTAX);
1474 MYCONST(XML_ERROR_NO_ELEMENTS);
1475 MYCONST(XML_ERROR_INVALID_TOKEN);
1476 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1477 MYCONST(XML_ERROR_PARTIAL_CHAR);
1478 MYCONST(XML_ERROR_TAG_MISMATCH);
1479 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1480 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1481 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1482 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1483 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1484 MYCONST(XML_ERROR_ASYNC_ENTITY);
1485 MYCONST(XML_ERROR_BAD_CHAR_REF);
1486 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1487 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1488 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1489 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1490 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001491 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1492 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1493 MYCONST(XML_ERROR_NOT_STANDALONE);
1494
Fred Drake85d835f2001-02-08 15:39:08 +00001495 PyModule_AddStringConstant(errors_module, "__doc__",
1496 "Constants used to describe error conditions.");
1497
Fred Drake93adb692000-09-23 04:55:48 +00001498#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001499
Fred Drake85d835f2001-02-08 15:39:08 +00001500#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001501 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1502 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1503 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001504#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001505
Fred Drake85d835f2001-02-08 15:39:08 +00001506#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1507 PyModule_AddStringConstant(model_module, "__doc__",
1508 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001509
Fred Drake85d835f2001-02-08 15:39:08 +00001510 MYCONST(XML_CTYPE_EMPTY);
1511 MYCONST(XML_CTYPE_ANY);
1512 MYCONST(XML_CTYPE_MIXED);
1513 MYCONST(XML_CTYPE_NAME);
1514 MYCONST(XML_CTYPE_CHOICE);
1515 MYCONST(XML_CTYPE_SEQ);
1516
1517 MYCONST(XML_CQUANT_NONE);
1518 MYCONST(XML_CQUANT_OPT);
1519 MYCONST(XML_CQUANT_REP);
1520 MYCONST(XML_CQUANT_PLUS);
1521#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001522}
1523
Fred Drake6f987622000-08-25 18:03:30 +00001524static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001525clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001526{
Fred Drakecde79132001-04-25 16:01:30 +00001527 int i = 0;
1528 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001529
Fred Drakecde79132001-04-25 16:01:30 +00001530 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001531 if (initial)
1532 self->handlers[i]=NULL;
1533 else {
Fred Drakecde79132001-04-25 16:01:30 +00001534 temp = self->handlers[i];
1535 self->handlers[i] = NULL;
1536 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001537 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001538 }
Fred Drakecde79132001-04-25 16:01:30 +00001539 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001540}
1541
Fred Drake6f987622000-08-25 18:03:30 +00001542typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001543
Fred Drake6f987622000-08-25 18:03:30 +00001544static void
1545pyxml_UpdatePairedHandlers(xmlparseobject *self,
1546 int startHandler,
1547 int endHandler,
1548 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001549{
Fred Drakecde79132001-04-25 16:01:30 +00001550 void *start_handler = NULL;
1551 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001552
Fred Drake0582df92000-07-12 04:49:00 +00001553 if (self->handlers[startHandler]
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001554 && self->handlers[startHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001555 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001556 }
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001557 if (self->handlers[endHandler]
1558 && self->handlers[endHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001559 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001560 }
1561 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001562}
1563
Fred Drake6f987622000-08-25 18:03:30 +00001564static void
1565pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001566{
1567 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1568 StartElement, EndElement,
1569 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570}
1571
Fred Drake6f987622000-08-25 18:03:30 +00001572static void
1573pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001574{
1575 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1576 StartElement, EndElement,
1577 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001578}
1579
Fred Drake6f987622000-08-25 18:03:30 +00001580static void
1581pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001582{
1583 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1584 StartNamespaceDecl, EndNamespaceDecl,
1585 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001586}
1587
Fred Drake6f987622000-08-25 18:03:30 +00001588static void
1589pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001590{
1591 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1592 StartNamespaceDecl, EndNamespaceDecl,
1593 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001594}
1595
Fred Drake6f987622000-08-25 18:03:30 +00001596static void
1597pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001598{
1599 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1600 StartCdataSection, EndCdataSection,
1601 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001602}
1603
Fred Drake6f987622000-08-25 18:03:30 +00001604static void
1605pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001606{
1607 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1608 StartCdataSection, EndCdataSection,
1609 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001610}
1611
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001612static void
1613pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1614{
1615 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1616 StartDoctypeDecl, EndDoctypeDecl,
1617 (pairsetter)XML_SetDoctypeDeclHandler);
1618}
1619
1620static void
1621pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1622{
1623 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1624 StartDoctypeDecl, EndDoctypeDecl,
1625 (pairsetter)XML_SetDoctypeDeclHandler);
1626}
1627
Fred Drake0582df92000-07-12 04:49:00 +00001628statichere struct HandlerInfo handler_info[] = {
1629 {"StartElementHandler",
1630 pyxml_SetStartElementHandler,
1631 (xmlhandler)my_StartElementHandler},
1632 {"EndElementHandler",
1633 pyxml_SetEndElementHandler,
1634 (xmlhandler)my_EndElementHandler},
1635 {"ProcessingInstructionHandler",
1636 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1637 (xmlhandler)my_ProcessingInstructionHandler},
1638 {"CharacterDataHandler",
1639 (xmlhandlersetter)XML_SetCharacterDataHandler,
1640 (xmlhandler)my_CharacterDataHandler},
1641 {"UnparsedEntityDeclHandler",
1642 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1643 (xmlhandler)my_UnparsedEntityDeclHandler },
1644 {"NotationDeclHandler",
1645 (xmlhandlersetter)XML_SetNotationDeclHandler,
1646 (xmlhandler)my_NotationDeclHandler },
1647 {"StartNamespaceDeclHandler",
1648 pyxml_SetStartNamespaceDeclHandler,
1649 (xmlhandler)my_StartNamespaceDeclHandler },
1650 {"EndNamespaceDeclHandler",
1651 pyxml_SetEndNamespaceDeclHandler,
1652 (xmlhandler)my_EndNamespaceDeclHandler },
1653 {"CommentHandler",
1654 (xmlhandlersetter)XML_SetCommentHandler,
1655 (xmlhandler)my_CommentHandler},
1656 {"StartCdataSectionHandler",
1657 pyxml_SetStartCdataSection,
1658 (xmlhandler)my_StartCdataSectionHandler},
1659 {"EndCdataSectionHandler",
1660 pyxml_SetEndCdataSection,
1661 (xmlhandler)my_EndCdataSectionHandler},
1662 {"DefaultHandler",
1663 (xmlhandlersetter)XML_SetDefaultHandler,
1664 (xmlhandler)my_DefaultHandler},
1665 {"DefaultHandlerExpand",
1666 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1667 (xmlhandler)my_DefaultHandlerExpandHandler},
1668 {"NotStandaloneHandler",
1669 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1670 (xmlhandler)my_NotStandaloneHandler},
1671 {"ExternalEntityRefHandler",
1672 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1673 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001674 {"StartDoctypeDeclHandler",
1675 pyxml_SetStartDoctypeDeclHandler,
1676 (xmlhandler)my_StartDoctypeDeclHandler},
1677 {"EndDoctypeDeclHandler",
1678 pyxml_SetEndDoctypeDeclHandler,
1679 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001680 {"EntityDeclHandler",
1681 (xmlhandlersetter)XML_SetEntityDeclHandler,
1682 (xmlhandler)my_EntityDeclHandler},
1683 {"XmlDeclHandler",
1684 (xmlhandlersetter)XML_SetXmlDeclHandler,
1685 (xmlhandler)my_XmlDeclHandler},
1686 {"ElementDeclHandler",
1687 (xmlhandlersetter)XML_SetElementDeclHandler,
1688 (xmlhandler)my_ElementDeclHandler},
1689 {"AttlistDeclHandler",
1690 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1691 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001692
Fred Drake0582df92000-07-12 04:49:00 +00001693 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001694};