blob: 4d7008dad2c01baae4e4255f13dce648d189eb85 [file] [log] [blame]
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001#include "Python.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002#include "compile.h"
3#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00004#ifdef HAVE_EXPAT_H
5#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00006#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +00007#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
8 + 0x100 * XML_MINOR_VERSION \
9 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010#else
Fred Drake85d835f2001-02-08 15:39:08 +000011/* Assume the oldest Expat that used expat.h and did not have version info */
12#define EXPAT_VERSION 0x015f00
13#endif
14#else /* !defined(HAVE_EXPAT_H) */
15#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000016/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000017#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000018#define EXPAT_VERSION 0x010100
19#endif
Fred Drake85d835f2001-02-08 15:39:08 +000020#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000021
22#ifndef PyGC_HEAD_SIZE
23#define PyGC_HEAD_SIZE 0
24#define PyObject_GC_Init(x)
25#define PyObject_GC_Fini(m)
26#define Py_TPFLAGS_GC 0
27#endif
28
Fred Drake0582df92000-07-12 04:49:00 +000029enum HandlerTypes {
30 StartElement,
31 EndElement,
32 ProcessingInstruction,
33 CharacterData,
34 UnparsedEntityDecl,
35 NotationDecl,
36 StartNamespaceDecl,
37 EndNamespaceDecl,
38 Comment,
39 StartCdataSection,
40 EndCdataSection,
41 Default,
42 DefaultHandlerExpand,
43 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000044 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000045#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000046 StartDoctypeDecl,
47 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000048#endif
49#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000050 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000051 InternalParsedEntityDecl,
52#endif
53#if EXPAT_VERSION >= 0x015f00
54 EntityDecl,
55 XmlDecl,
56 ElementDecl,
57 AttlistDecl,
58#endif
59 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000060};
61
62static PyObject *ErrorObject;
63
64/* ----------------------------------------------------- */
65
66/* Declarations for objects of type xmlparser */
67
68typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000069 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070
Fred Drake0582df92000-07-12 04:49:00 +000071 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000072 int returns_unicode; /* True if Unicode strings are returned;
73 if false, UTF-8 strings are returned */
74 int ordered_attributes; /* Return attributes as a list. */
75 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000076 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000077 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078} xmlparseobject;
79
80staticforward PyTypeObject Xmlparsetype;
81
Fred Drake6f987622000-08-25 18:03:30 +000082typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000083typedef void* xmlhandler;
84
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000085struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000086 const char *name;
87 xmlhandlersetter setter;
88 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000089 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090};
91
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000092staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000093
Fred Drakebd6101c2001-02-14 18:29:45 +000094/* Set an integer attribute on the error object; return true on success,
95 * false on an exception.
96 */
97static int
98set_error_attr(PyObject *err, char *name, int value)
99{
100 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000101
Fred Drakebd6101c2001-02-14 18:29:45 +0000102 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
103 Py_DECREF(v);
104 return 0;
105 }
106 return 1;
107}
108
109/* Build and set an Expat exception, including positioning
110 * information. Always returns NULL.
111 */
Fred Drake85d835f2001-02-08 15:39:08 +0000112static PyObject *
113set_error(xmlparseobject *self)
114{
115 PyObject *err;
116 char buffer[256];
117 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000118 int lineno = XML_GetErrorLineNumber(parser);
119 int column = XML_GetErrorColumnNumber(parser);
120 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000121
122 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000123 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000124 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 if ( err != NULL
126 && set_error_attr(err, "code", code)
127 && set_error_attr(err, "offset", column)
128 && set_error_attr(err, "lineno", lineno)) {
129 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000130 }
131 return NULL;
132}
133
134
135#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000136/* Convert an array of attributes and their values into a Python dict */
137
Fred Drake0582df92000-07-12 04:49:00 +0000138static PyObject *
139conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000140{
Fred Drake0582df92000-07-12 04:49:00 +0000141 PyObject *attrs_obj = NULL;
142 XML_Char **attrs_p, **attrs_k = NULL;
143 int attrs_len;
144 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000145
Fred Drake0582df92000-07-12 04:49:00 +0000146 if ((attrs_obj = PyDict_New()) == NULL)
147 goto finally;
148 for (attrs_len = 0, attrs_p = atts;
149 *attrs_p;
150 attrs_p++, attrs_len++) {
151 if (attrs_len % 2) {
152 rv = PyString_FromString(*attrs_p);
153 if (!rv) {
154 Py_DECREF(attrs_obj);
155 attrs_obj = NULL;
156 goto finally;
157 }
158 if (PyDict_SetItemString(attrs_obj,
159 (char*)*attrs_k, rv) < 0) {
160 Py_DECREF(attrs_obj);
161 attrs_obj = NULL;
162 goto finally;
163 }
164 Py_DECREF(rv);
165 }
166 else
167 attrs_k = attrs_p;
168 }
169 finally:
170 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171}
Fred Drake85d835f2001-02-08 15:39:08 +0000172#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000173
174#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake85d835f2001-02-08 15:39:08 +0000175#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000176static PyObject *
177conv_atts_using_unicode(XML_Char **atts)
178{
Fred Drakeca1f4262000-09-21 20:10:23 +0000179 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000180 XML_Char **attrs_p, **attrs_k = NULL;
181 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000182
Fred Drake0582df92000-07-12 04:49:00 +0000183 if ((attrs_obj = PyDict_New()) == NULL)
184 goto finally;
185 for (attrs_len = 0, attrs_p = atts;
186 *attrs_p;
187 attrs_p++, attrs_len++) {
188 if (attrs_len % 2) {
189 PyObject *attr_str, *value_str;
190 const char *p = (const char *) (*attrs_k);
191 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
192 if (!attr_str) {
193 Py_DECREF(attrs_obj);
194 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000195 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000196 }
197 p = (const char *) *attrs_p;
198 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
199 if (!value_str) {
200 Py_DECREF(attrs_obj);
201 Py_DECREF(attr_str);
202 attrs_obj = NULL;
203 goto finally;
204 }
205 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
206 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000207 Py_DECREF(attr_str);
208 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000209 attrs_obj = NULL;
210 goto finally;
211 }
212 Py_DECREF(attr_str);
213 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000214 }
Fred Drake0582df92000-07-12 04:49:00 +0000215 else
216 attrs_k = attrs_p;
217 }
218 finally:
219 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000220}
Fred Drake85d835f2001-02-08 15:39:08 +0000221#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000223/* Convert a string of XML_Chars into a Unicode string.
224 Returns None if str is a null pointer. */
225
Fred Drake0582df92000-07-12 04:49:00 +0000226static PyObject *
227conv_string_to_unicode(XML_Char *str)
228{
229 /* XXX currently this code assumes that XML_Char is 8-bit,
230 and hence in UTF-8. */
231 /* UTF-8 from Expat, Unicode desired */
232 if (str == NULL) {
233 Py_INCREF(Py_None);
234 return Py_None;
235 }
236 return PyUnicode_DecodeUTF8((const char *)str,
237 strlen((const char *)str),
238 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000239}
240
Fred Drake0582df92000-07-12 04:49:00 +0000241static PyObject *
242conv_string_len_to_unicode(const XML_Char *str, int len)
243{
244 /* XXX currently this code assumes that XML_Char is 8-bit,
245 and hence in UTF-8. */
246 /* UTF-8 from Expat, Unicode desired */
247 if (str == NULL) {
248 Py_INCREF(Py_None);
249 return Py_None;
250 }
Fred Drake6f987622000-08-25 18:03:30 +0000251 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000252}
253#endif
254
255/* Convert a string of XML_Chars into an 8-bit Python string.
256 Returns None if str is a null pointer. */
257
Fred Drake6f987622000-08-25 18:03:30 +0000258static PyObject *
259conv_string_to_utf8(XML_Char *str)
260{
261 /* XXX currently this code assumes that XML_Char is 8-bit,
262 and hence in UTF-8. */
263 /* UTF-8 from Expat, UTF-8 desired */
264 if (str == NULL) {
265 Py_INCREF(Py_None);
266 return Py_None;
267 }
268 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000269}
270
Fred Drake6f987622000-08-25 18:03:30 +0000271static PyObject *
272conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000273{
Fred Drake6f987622000-08-25 18:03:30 +0000274 /* XXX currently this code assumes that XML_Char is 8-bit,
275 and hence in UTF-8. */
276 /* UTF-8 from Expat, UTF-8 desired */
277 if (str == NULL) {
278 Py_INCREF(Py_None);
279 return Py_None;
280 }
281 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000282}
283
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000284/* Callback routines */
285
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000286static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000287
Fred Drake6f987622000-08-25 18:03:30 +0000288static void
289flag_error(xmlparseobject *self)
290{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000291 clear_handlers(self, 1);
292}
293
294static PyCodeObject*
295getcode(enum HandlerTypes slot, char* func_name, int lineno)
296{
Fred Drakebd6101c2001-02-14 18:29:45 +0000297 PyObject *code = NULL;
298 PyObject *name = NULL;
299 PyObject *nulltuple = NULL;
300 PyObject *filename = NULL;
301
302 if (handler_info[slot].tb_code == NULL) {
303 code = PyString_FromString("");
304 if (code == NULL)
305 goto failed;
306 name = PyString_FromString(func_name);
307 if (name == NULL)
308 goto failed;
309 nulltuple = PyTuple_New(0);
310 if (nulltuple == NULL)
311 goto failed;
312 filename = PyString_FromString(__FILE__);
313 handler_info[slot].tb_code =
314 PyCode_New(0, /* argcount */
315 0, /* nlocals */
316 0, /* stacksize */
317 0, /* flags */
318 code, /* code */
319 nulltuple, /* consts */
320 nulltuple, /* names */
321 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000322#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000323 nulltuple, /* freevars */
324 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000325#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000326 filename, /* filename */
327 name, /* name */
328 lineno, /* firstlineno */
329 code /* lnotab */
330 );
331 if (handler_info[slot].tb_code == NULL)
332 goto failed;
333 Py_DECREF(code);
334 Py_DECREF(nulltuple);
335 Py_DECREF(filename);
336 Py_DECREF(name);
337 }
338 return handler_info[slot].tb_code;
339 failed:
340 Py_XDECREF(code);
341 Py_XDECREF(name);
342 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000343}
344
345static PyObject*
346call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
347{
Fred Drakebd6101c2001-02-14 18:29:45 +0000348 PyThreadState *tstate = PyThreadState_GET();
349 PyFrameObject *f;
350 PyObject *res;
351
352 if (c == NULL)
353 return NULL;
354 f = PyFrame_New(
355 tstate, /*back*/
356 c, /*code*/
357 tstate->frame->f_globals, /*globals*/
358 NULL /*locals*/
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000359#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000360 ,NULL /*closure*/
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000361#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000362 );
363 if (f == NULL)
364 return NULL;
365 tstate->frame = f;
366 res = PyEval_CallObject(func, args);
367 if (res == NULL && tstate->curexc_traceback == NULL)
368 PyTraceBack_Here(f);
369 tstate->frame = f->f_back;
370 Py_DECREF(f);
371 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000372}
373
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000374#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
375#define STRING_CONV_FUNC conv_string_to_utf8
376#else
377/* Python 1.6 and later versions */
378#define STRING_CONV_FUNC (self->returns_unicode \
379 ? conv_string_to_unicode : conv_string_to_utf8)
380#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000381
Fred Drake85d835f2001-02-08 15:39:08 +0000382static void
383my_StartElementHandler(void *userData,
384 const XML_Char *name, const XML_Char **atts)
385{
386 xmlparseobject *self = (xmlparseobject *)userData;
387
388 if (self->handlers[StartElement]
389 && self->handlers[StartElement] != Py_None) {
390 PyObject *container, *rv, *args;
391 int i, max;
392
393 /* Set max to the number of slots filled in atts[]; max/2 is
394 * the number of attributes we need to process.
395 */
396 if (self->specified_attributes) {
397 max = XML_GetSpecifiedAttributeCount(self->itself);
398 }
399 else {
400 max = 0;
401 while (atts[max] != NULL)
402 max += 2;
403 }
404 /* Build the container. */
405 if (self->ordered_attributes)
406 container = PyList_New(max);
407 else
408 container = PyDict_New();
409 if (container == NULL) {
410 flag_error(self);
411 return;
412 }
413 for (i = 0; i < max; i += 2) {
414 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
415 PyObject *v;
416 if (n == NULL) {
417 flag_error(self);
418 Py_DECREF(container);
419 return;
420 }
421 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
422 if (v == NULL) {
423 flag_error(self);
424 Py_DECREF(container);
425 Py_DECREF(n);
426 return;
427 }
428 if (self->ordered_attributes) {
429 PyList_SET_ITEM(container, i, n);
430 PyList_SET_ITEM(container, i+1, v);
431 }
432 else if (PyDict_SetItem(container, n, v)) {
433 flag_error(self);
434 Py_DECREF(n);
435 Py_DECREF(v);
436 return;
437 }
438 else {
439 Py_DECREF(n);
440 Py_DECREF(v);
441 }
442 }
443 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
444 if (args == NULL) {
445 Py_DECREF(container);
446 return;
447 }
448 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000449 self->in_callback = 1;
450 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000451 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000452 self->in_callback = 0;
453 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000454 if (rv == NULL) {
455 flag_error(self);
456 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000457 }
Fred Drake85d835f2001-02-08 15:39:08 +0000458 Py_DECREF(rv);
459 }
460}
461
462#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
463 RETURN, GETUSERDATA) \
464static RC \
465my_##NAME##Handler PARAMS {\
466 xmlparseobject *self = GETUSERDATA ; \
467 PyObject *args = NULL; \
468 PyObject *rv = NULL; \
469 INIT \
470\
471 if (self->handlers[NAME] \
472 && self->handlers[NAME] != Py_None) { \
473 args = Py_BuildValue PARAM_FORMAT ;\
474 if (!args) \
475 return RETURN; \
Fred Drakebd6101c2001-02-14 18:29:45 +0000476 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000477 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
478 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000479 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000480 Py_DECREF(args); \
481 if (rv == NULL) { \
482 flag_error(self); \
483 return RETURN; \
484 } \
485 CONVERSION \
486 Py_DECREF(rv); \
487 } \
488 return RETURN; \
489}
490
Fred Drake6f987622000-08-25 18:03:30 +0000491#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
492 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
493 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000494
Fred Drake6f987622000-08-25 18:03:30 +0000495#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
496 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
497 rc = PyInt_AsLong(rv);, rc, \
498 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000499
Fred Drake6f987622000-08-25 18:03:30 +0000500VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000501 (void *userData, const XML_Char *name),
502 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000503
Fred Drake6f987622000-08-25 18:03:30 +0000504VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000505 (void *userData,
506 const XML_Char *target,
507 const XML_Char *data),
508 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000509
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000510#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000511VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000512 (void *userData, const XML_Char *data, int len),
513 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000514#else
Fred Drake6f987622000-08-25 18:03:30 +0000515VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000516 (void *userData, const XML_Char *data, int len),
517 ("(N)", (self->returns_unicode
518 ? conv_string_len_to_unicode(data,len)
519 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000520#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000521
Fred Drake6f987622000-08-25 18:03:30 +0000522VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000523 (void *userData,
524 const XML_Char *entityName,
525 const XML_Char *base,
526 const XML_Char *systemId,
527 const XML_Char *publicId,
528 const XML_Char *notationName),
529 ("(O&O&O&O&O&)",
530 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
531 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
532 STRING_CONV_FUNC,notationName))
533
534#if EXPAT_VERSION >= 0x015f00
535#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
536VOID_HANDLER(EntityDecl,
537 (void *userData,
538 const XML_Char *entityName,
539 int is_parameter_entity,
540 const XML_Char *value,
541 int value_length,
542 const XML_Char *base,
543 const XML_Char *systemId,
544 const XML_Char *publicId,
545 const XML_Char *notationName),
546 ("O&iNO&O&O&O&",
547 STRING_CONV_FUNC,entityName, is_parameter_entity,
548 conv_string_len_to_utf8(value, value_length),
549 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
550 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
551#else
552VOID_HANDLER(EntityDecl,
553 (void *userData,
554 const XML_Char *entityName,
555 int is_parameter_entity,
556 const XML_Char *value,
557 int value_length,
558 const XML_Char *base,
559 const XML_Char *systemId,
560 const XML_Char *publicId,
561 const XML_Char *notationName),
562 ("O&iNO&O&O&O&",
563 STRING_CONV_FUNC,entityName, is_parameter_entity,
564 (self->returns_unicode
565 ? conv_string_len_to_unicode(value, value_length)
566 : conv_string_len_to_utf8(value, value_length)),
567 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
568 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
569#endif
570
571VOID_HANDLER(XmlDecl,
572 (void *userData,
573 const XML_Char *version,
574 const XML_Char *encoding,
575 int standalone),
576 ("(O&O&i)",
577 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
578 standalone))
579
580static PyObject *
581conv_content_model(XML_Content * const model,
582 PyObject *(*conv_string)(XML_Char *))
583{
584 PyObject *result = NULL;
585 PyObject *children = PyTuple_New(model->numchildren);
586 int i;
587
588 if (children != NULL) {
589 for (i = 0; i < model->numchildren; ++i) {
590 PyObject *child = conv_content_model(&model->children[i],
591 conv_string);
592 if (child == NULL) {
593 Py_XDECREF(children);
594 return NULL;
595 }
596 PyTuple_SET_ITEM(children, i, child);
597 }
598 result = Py_BuildValue("(iiO&N)",
599 model->type, model->quant,
600 conv_string,model->name, children);
601 }
602 return result;
603}
604
605static PyObject *
606conv_content_model_utf8(XML_Content * const model)
607{
608 return conv_content_model(model, conv_string_to_utf8);
609}
610
611#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
612static PyObject *
613conv_content_model_unicode(XML_Content * const model)
614{
615 return conv_content_model(model, conv_string_to_unicode);
616}
617
618VOID_HANDLER(ElementDecl,
619 (void *userData,
620 const XML_Char *name,
621 XML_Content *model),
622 ("O&O&",
623 STRING_CONV_FUNC,name,
624 (self->returns_unicode ? conv_content_model_unicode
625 : conv_content_model_utf8),model))
626#else
627VOID_HANDLER(ElementDecl,
628 (void *userData,
629 const XML_Char *name,
630 XML_Content *model),
631 ("O&O&",
632 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
633#endif
634
635VOID_HANDLER(AttlistDecl,
636 (void *userData,
637 const XML_Char *elname,
638 const XML_Char *attname,
639 const XML_Char *att_type,
640 const XML_Char *dflt,
641 int isrequired),
642 ("(O&O&O&O&i)",
643 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
644 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
645 isrequired))
646#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000647
Fred Drake6f987622000-08-25 18:03:30 +0000648VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000649 (void *userData,
650 const XML_Char *notationName,
651 const XML_Char *base,
652 const XML_Char *systemId,
653 const XML_Char *publicId),
654 ("(O&O&O&O&)",
655 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
656 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000657
Fred Drake6f987622000-08-25 18:03:30 +0000658VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000659 (void *userData,
660 const XML_Char *prefix,
661 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000662 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000663
Fred Drake6f987622000-08-25 18:03:30 +0000664VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000665 (void *userData,
666 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000667 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000668
Fred Drake6f987622000-08-25 18:03:30 +0000669VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000670 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000671 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000672
Fred Drake6f987622000-08-25 18:03:30 +0000673VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000675 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000676
Fred Drake6f987622000-08-25 18:03:30 +0000677VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000679 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000681#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000682VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000683 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000684 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685
Fred Drake6f987622000-08-25 18:03:30 +0000686VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000687 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000688 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000689#else
Fred Drake6f987622000-08-25 18:03:30 +0000690VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000691 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000692 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000693 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000694 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000695
Fred Drake6f987622000-08-25 18:03:30 +0000696VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000697 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000698 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000699 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000700 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000702
Fred Drake6f987622000-08-25 18:03:30 +0000703INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 (void *userData),
705 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000706
Fred Drake6f987622000-08-25 18:03:30 +0000707RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000708 (XML_Parser parser,
709 const XML_Char *context,
710 const XML_Char *base,
711 const XML_Char *systemId,
712 const XML_Char *publicId),
713 int rc=0;,
714 ("(O&O&O&O&)",
715 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000716 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
717 rc = PyInt_AsLong(rv);, rc,
718 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000719
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000720/* XXX UnknownEncodingHandler */
721
Fred Drake85d835f2001-02-08 15:39:08 +0000722#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000723VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000724 (void *userData, const XML_Char *doctypeName),
725 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
726 Py_None, Py_None, -1))
727#elif EXPAT_VERSION >= 0x015f00
728VOID_HANDLER(StartDoctypeDecl,
729 (void *userData, const XML_Char *doctypeName,
730 const XML_Char *sysid, const XML_Char *pubid,
731 int has_internal_subset),
732 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
733 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
734 has_internal_subset))
735#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000736
Fred Drake85d835f2001-02-08 15:39:08 +0000737#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000738VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000739#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000740
Fred Drake85d835f2001-02-08 15:39:08 +0000741#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000742VOID_HANDLER(ExternalParsedEntityDecl,
743 (void *userData, const XML_Char *entityName,
744 const XML_Char *base, const XML_Char *systemId,
745 const XML_Char *publicId),
746 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
747 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
748 STRING_CONV_FUNC, publicId))
749
750VOID_HANDLER(InternalParsedEntityDecl,
751 (void *userData, const XML_Char *entityName,
752 const XML_Char *replacementText, int replacementTextLength),
753 ("(O&O&i)", STRING_CONV_FUNC, entityName,
754 STRING_CONV_FUNC, replacementText, replacementTextLength))
755
Fred Drake85d835f2001-02-08 15:39:08 +0000756#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000757
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758/* ---------------------------------------------------------------- */
759
760static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000761"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000762Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000763
764static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000765xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000766{
Fred Drake0582df92000-07-12 04:49:00 +0000767 char *s;
768 int slen;
769 int isFinal = 0;
770 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000771
Fred Drake0582df92000-07-12 04:49:00 +0000772 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
773 return NULL;
774 rv = XML_Parse(self->itself, s, slen, isFinal);
775 if (PyErr_Occurred()) {
776 return NULL;
777 }
778 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000779 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000780 }
781 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000782}
783
Fred Drakeca1f4262000-09-21 20:10:23 +0000784/* File reading copied from cPickle */
785
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786#define BUF_SIZE 2048
787
Fred Drake0582df92000-07-12 04:49:00 +0000788static int
789readinst(char *buf, int buf_size, PyObject *meth)
790{
791 PyObject *arg = NULL;
792 PyObject *bytes = NULL;
793 PyObject *str = NULL;
794 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000795
Fred Drake676940b2000-09-22 15:21:31 +0000796 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000797 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000798
Fred Drakeca1f4262000-09-21 20:10:23 +0000799 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000800 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
Tim Peters954eef72000-09-22 06:01:11 +0000802 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000803
Fred Drakeca1f4262000-09-21 20:10:23 +0000804 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000805 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000806
Fred Drake0582df92000-07-12 04:49:00 +0000807 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000808 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000809 PyErr_Format(PyExc_TypeError,
810 "read() did not return a string object (type=%.400s)",
811 str->ob_type->tp_name);
812 goto finally;
813 }
814 len = PyString_GET_SIZE(str);
815 if (len > buf_size) {
816 PyErr_Format(PyExc_ValueError,
817 "read() returned too much data: "
818 "%i bytes requested, %i returned",
819 buf_size, len);
820 Py_DECREF(str);
821 goto finally;
822 }
823 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000824finally:
Fred Drake0582df92000-07-12 04:49:00 +0000825 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000826 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000827 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828}
829
830static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000831"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000832Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833
834static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000835xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836{
Fred Drake0582df92000-07-12 04:49:00 +0000837 int rv = 1;
838 PyObject *f;
839 FILE *fp;
840 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841
Fred Drake0582df92000-07-12 04:49:00 +0000842 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
843 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000844
Fred Drake0582df92000-07-12 04:49:00 +0000845 if (PyFile_Check(f)) {
846 fp = PyFile_AsFile(f);
847 }
848 else{
849 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000850 readmethod = PyObject_GetAttrString(f, "read");
851 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000852 PyErr_Clear();
853 PyErr_SetString(PyExc_TypeError,
854 "argument must have 'read' attribute");
855 return 0;
856 }
857 }
858 for (;;) {
859 int bytes_read;
860 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
861 if (buf == NULL)
862 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000863
Fred Drake0582df92000-07-12 04:49:00 +0000864 if (fp) {
865 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
866 if (bytes_read < 0) {
867 PyErr_SetFromErrno(PyExc_IOError);
868 return NULL;
869 }
870 }
871 else {
872 bytes_read = readinst(buf, BUF_SIZE, readmethod);
873 if (bytes_read < 0)
874 return NULL;
875 }
876 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
877 if (PyErr_Occurred())
878 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000879
Fred Drake0582df92000-07-12 04:49:00 +0000880 if (!rv || bytes_read == 0)
881 break;
882 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000883 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000884 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000885 }
Fred Drake0582df92000-07-12 04:49:00 +0000886 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000887}
888
889static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000890"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000891Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892
893static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000894xmlparse_SetBase(xmlparseobject *self, PyObject *args)
895{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896 char *base;
897
Fred Drake0582df92000-07-12 04:49:00 +0000898 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000900 if (!XML_SetBase(self->itself, base)) {
901 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903 Py_INCREF(Py_None);
904 return Py_None;
905}
906
907static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000908"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000909Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000910
911static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000912xmlparse_GetBase(xmlparseobject *self, PyObject *args)
913{
914 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000916
Fred Drake0582df92000-07-12 04:49:00 +0000917 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000918}
919
Fred Drakebd6101c2001-02-14 18:29:45 +0000920#if EXPAT_VERSION >= 0x015f00
921static char xmlparse_GetInputContext__doc__[] =
922"GetInputContext() -> string\n\
923Return the untranslated text of the input that caused the current event.\n\
924If the event was generated by a large amount of text (such as a start tag\n\
925for an element with many attributes), not all of the text may be available.";
926
927static PyObject *
928xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
929{
930 PyObject *result = NULL;
931
932 if (PyArg_ParseTuple(args, ":GetInputContext")) {
933 if (self->in_callback) {
934 int offset, size;
935 const char *buffer
936 = XML_GetInputContext(self->itself, &offset, &size);
937
938 if (buffer != NULL)
939 result = PyString_FromStringAndSize(buffer + offset, size);
940 else {
941 result = Py_None;
942 Py_INCREF(result);
943 }
944 }
945 else {
946 result = Py_None;
947 Py_INCREF(result);
948 }
949 }
950 return result;
951}
952#endif
953
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000954static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000955"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000956Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000957information passed to the ExternalEntityRefHandler.";
958
959static PyObject *
960xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
961{
962 char *context;
963 char *encoding = NULL;
964 xmlparseobject *new_parser;
965 int i;
966
967 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
968 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000969 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000970 }
971
972#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
973 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000974#else
Fred Drake85d835f2001-02-08 15:39:08 +0000975 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000976 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000977#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000978
979 if (new_parser == NULL)
980 return NULL;
981 new_parser->returns_unicode = self->returns_unicode;
982 new_parser->ordered_attributes = self->ordered_attributes;
983 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000984 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000985 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000986 encoding);
987 new_parser->handlers = 0;
988 PyObject_GC_Init(new_parser);
989
990 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000991 Py_DECREF(new_parser);
992 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000993 }
994
995 XML_SetUserData(new_parser->itself, (void *)new_parser);
996
997 /* allocate and clear handlers first */
998 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000999 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001000
1001 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001002 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001003 Py_DECREF(new_parser);
1004 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001005 }
1006 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001007
1008 /* then copy handlers from self */
1009 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001010 if (self->handlers[i]) {
1011 Py_INCREF(self->handlers[i]);
1012 new_parser->handlers[i] = self->handlers[i];
1013 handler_info[i].setter(new_parser->itself,
1014 handler_info[i].handler);
1015 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016 }
Fred Drake28adf522000-09-24 22:07:59 +00001017 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001018}
1019
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001020#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001021
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001022static char xmlparse_SetParamEntityParsing__doc__[] =
1023"SetParamEntityParsing(flag) -> success\n\
1024Controls parsing of parameter entities (including the external DTD\n\
1025subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1026XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1027XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1028was successful.";
1029
1030static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001031xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001032{
Fred Drake85d835f2001-02-08 15:39:08 +00001033 int flag;
1034 if (!PyArg_ParseTuple(args, "i", &flag))
1035 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001036 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001037 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038}
1039
Fred Drake85d835f2001-02-08 15:39:08 +00001040#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001041
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001042static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001043 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001044 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001045 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001046 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001047 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001048 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001049 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001051 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1052 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001053#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001054 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1055 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1056#endif
1057#if EXPAT_VERSION >= 0x015f00
1058 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1059 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001060#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001061 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001062};
1063
1064/* ---------- */
1065
1066
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001067#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1068
1069/*
1070 pyexpat international encoding support.
1071 Make it as simple as possible.
1072*/
1073
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001074static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001075PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001076
1077static void
1078init_template_buffer(void)
1079{
1080 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001081 for (i = 0; i < 256; i++) {
1082 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001083 }
Fred Drakebb66a202001-03-01 20:48:17 +00001084 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001085}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001086
1087int
1088PyUnknownEncodingHandler(void *encodingHandlerData,
1089const XML_Char *name,
1090XML_Encoding * info)
1091{
Fred Drakebb66a202001-03-01 20:48:17 +00001092 PyUnicodeObject *_u_string = NULL;
1093 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001094 int i;
1095
Fred Drakebb66a202001-03-01 20:48:17 +00001096 /* Yes, supports only 8bit encodings */
1097 _u_string = (PyUnicodeObject *)
1098 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099
Fred Drakebb66a202001-03-01 20:48:17 +00001100 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102
Fred Drakebb66a202001-03-01 20:48:17 +00001103 for (i = 0; i < 256; i++) {
1104 /* Stupid to access directly, but fast */
1105 Py_UNICODE c = _u_string->str[i];
1106 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001108 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001109 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001110 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001111
1112 info->data = NULL;
1113 info->convert = NULL;
1114 info->release = NULL;
1115 result=1;
1116
1117 Py_DECREF(_u_string);
1118 return result;
1119}
1120
1121#endif
1122
1123static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001124newxmlparseobject(char *encoding, char *namespace_separator)
1125{
1126 int i;
1127 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001128
1129#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001130 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1131 if (self == NULL)
1132 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001133
Fred Drake0582df92000-07-12 04:49:00 +00001134 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001135#else
Fred Drake0582df92000-07-12 04:49:00 +00001136 /* Code for versions 1.6 and later */
1137 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1138 if (self == NULL)
1139 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001140
Fred Drake0582df92000-07-12 04:49:00 +00001141 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001142#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001143 self->ordered_attributes = 0;
1144 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001145 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001146 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001147 if (namespace_separator) {
1148 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1149 }
Fred Drake85d835f2001-02-08 15:39:08 +00001150 else {
Fred Drake0582df92000-07-12 04:49:00 +00001151 self->itself = XML_ParserCreate(encoding);
1152 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001153 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001154 if (self->itself == NULL) {
1155 PyErr_SetString(PyExc_RuntimeError,
1156 "XML_ParserCreate failed");
1157 Py_DECREF(self);
1158 return NULL;
1159 }
1160 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001161#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1162#else
1163 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1164#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001165
Fred Drake0582df92000-07-12 04:49:00 +00001166 for(i = 0; handler_info[i].name != NULL; i++)
1167 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001168
Fred Drake0582df92000-07-12 04:49:00 +00001169 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001170 if (!self->handlers){
1171 Py_DECREF(self);
1172 return PyErr_NoMemory();
1173 }
1174 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001175
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001176 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001177}
1178
1179
1180static void
Fred Drake0582df92000-07-12 04:49:00 +00001181xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001182{
Fred Drake0582df92000-07-12 04:49:00 +00001183 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001184 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001185 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001186 XML_ParserFree(self->itself);
1187 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001188
Fred Drake85d835f2001-02-08 15:39:08 +00001189 if (self->handlers != NULL) {
1190 for (i = 0; handler_info[i].name != NULL; i++) {
1191 Py_XDECREF(self->handlers[i]);
1192 }
1193 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001194 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001195#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001196 /* Code for versions before 1.6 */
1197 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001198#else
Fred Drake0582df92000-07-12 04:49:00 +00001199 /* Code for versions 1.6 and later */
1200 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001201#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001202}
1203
Fred Drake0582df92000-07-12 04:49:00 +00001204static int
1205handlername2int(const char *name)
1206{
1207 int i;
1208 for (i=0; handler_info[i].name != NULL; i++) {
1209 if (strcmp(name, handler_info[i].name) == 0) {
1210 return i;
1211 }
1212 }
1213 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001214}
1215
1216static PyObject *
1217xmlparse_getattr(xmlparseobject *self, char *name)
1218{
Fred Drake0582df92000-07-12 04:49:00 +00001219 int handlernum;
1220 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001221 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001222 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001223 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001224 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001225 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001226 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001227 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1228 if (strcmp(name, "ordered_attributes") == 0)
1229 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001230 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001231 return PyInt_FromLong((long) self->returns_unicode);
1232 if (strcmp(name, "specified_attributes") == 0)
1233 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001234
Fred Drake0582df92000-07-12 04:49:00 +00001235 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001236
Fred Drake0582df92000-07-12 04:49:00 +00001237 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1238 Py_INCREF(self->handlers[handlernum]);
1239 return self->handlers[handlernum];
1240 }
1241 if (strcmp(name, "__members__") == 0) {
1242 int i;
1243 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001244 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001245 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001246 }
1247 PyList_Append(rc, PyString_FromString("ErrorCode"));
1248 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1249 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1250 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001251 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001252 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001253 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001254
Fred Drake0582df92000-07-12 04:49:00 +00001255 return rc;
1256 }
1257 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001258}
1259
Fred Drake6f987622000-08-25 18:03:30 +00001260static int
1261sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001262{
1263 int handlernum = handlername2int(name);
1264 if (handlernum != -1) {
1265 Py_INCREF(v);
1266 Py_XDECREF(self->handlers[handlernum]);
1267 self->handlers[handlernum] = v;
1268 handler_info[handlernum].setter(self->itself,
1269 handler_info[handlernum].handler);
1270 return 1;
1271 }
1272 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001273}
1274
1275static int
Fred Drake6f987622000-08-25 18:03:30 +00001276xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001277{
Fred Drake6f987622000-08-25 18:03:30 +00001278 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001279 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001280 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1281 return -1;
1282 }
Fred Drake85d835f2001-02-08 15:39:08 +00001283 if (strcmp(name, "ordered_attributes") == 0) {
1284 if (PyObject_IsTrue(v))
1285 self->ordered_attributes = 1;
1286 else
1287 self->ordered_attributes = 0;
1288 return 0;
1289 }
Fred Drake6f987622000-08-25 18:03:30 +00001290 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001291 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001292#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001293 PyErr_SetString(PyExc_ValueError,
1294 "Cannot return Unicode strings in Python 1.5");
1295 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001296#else
Fred Drake6f987622000-08-25 18:03:30 +00001297 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001298#endif
Fred Drake6f987622000-08-25 18:03:30 +00001299 }
1300 else
1301 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001302 return 0;
1303 }
1304 if (strcmp(name, "specified_attributes") == 0) {
1305 if (PyObject_IsTrue(v))
1306 self->specified_attributes = 1;
1307 else
1308 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001309 return 0;
1310 }
1311 if (sethandler(self, name, v)) {
1312 return 0;
1313 }
1314 PyErr_SetString(PyExc_AttributeError, name);
1315 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001316}
1317
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001318#ifdef WITH_CYCLE_GC
1319static int
1320xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1321{
1322 int i, err;
1323 for (i = 0; handler_info[i].name != NULL; i++) {
1324 if (!op->handlers[i])
1325 continue;
1326 err = visit(op->handlers[i], arg);
1327 if (err)
1328 return err;
1329 }
1330 return 0;
1331}
1332
1333static int
1334xmlparse_clear(xmlparseobject *op)
1335{
1336 clear_handlers(op, 1);
1337 return 0;
1338}
1339#endif
1340
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001341static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001342"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001343
1344static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001345 PyObject_HEAD_INIT(NULL)
1346 0, /*ob_size*/
1347 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001348 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001349 0, /*tp_itemsize*/
1350 /* methods */
1351 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1352 (printfunc)0, /*tp_print*/
1353 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1354 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1355 (cmpfunc)0, /*tp_compare*/
1356 (reprfunc)0, /*tp_repr*/
1357 0, /*tp_as_number*/
1358 0, /*tp_as_sequence*/
1359 0, /*tp_as_mapping*/
1360 (hashfunc)0, /*tp_hash*/
1361 (ternaryfunc)0, /*tp_call*/
1362 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001363 0, /* tp_getattro */
1364 0, /* tp_setattro */
1365 0, /* tp_as_buffer */
1366 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1367 Xmlparsetype__doc__, /* Documentation string */
1368#ifdef WITH_CYCLE_GC
1369 (traverseproc)xmlparse_traverse, /* tp_traverse */
1370 (inquiry)xmlparse_clear /* tp_clear */
1371#else
1372 0, 0
1373#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001374};
1375
1376/* End of code for xmlparser objects */
1377/* -------------------------------------------------------- */
1378
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001379static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001380"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1381Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001382
1383static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001384pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1385{
1386 char *encoding = NULL;
1387 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001388 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001389
Fred Drake0582df92000-07-12 04:49:00 +00001390 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001391 &encoding, &namespace_separator))
1392 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001393 if (namespace_separator != NULL
1394 && strlen(namespace_separator) != 1) {
1395 PyErr_SetString(PyExc_ValueError,
1396 "namespace_separator must be one character,"
1397 " omitted, or None");
1398 return NULL;
1399 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001400 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001401}
1402
1403static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001404"ErrorString(errno) -> string\n\
1405Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001406
1407static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001408pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001409{
Fred Drake0582df92000-07-12 04:49:00 +00001410 long code = 0;
1411
1412 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1413 return NULL;
1414 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001415}
1416
1417/* List of methods defined in the module */
1418
1419static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001420 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1421 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1422 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1423 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001424
Fred Drake0582df92000-07-12 04:49:00 +00001425 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001426};
1427
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001428/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001429
1430static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001431"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001432
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001433/* Initialization function for the module */
1434
Fred Drake93adb692000-09-23 04:55:48 +00001435void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001436
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001437#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001438
1439/* 1.5 compatibility: PyModule_AddObject */
1440static int
1441PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1442{
1443 PyObject *dict;
1444 if (!PyModule_Check(m) || o == NULL)
1445 return -1;
1446 dict = PyModule_GetDict(m);
1447 if (dict == NULL)
1448 return -1;
1449 if (PyDict_SetItemString(dict, name, o))
1450 return -1;
1451 Py_DECREF(o);
1452 return 0;
1453}
1454
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001455int
1456PyModule_AddIntConstant(PyObject *m, char *name, long value)
1457{
1458 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1459}
1460
Fred Drakea77254a2000-09-29 19:23:29 +00001461static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001462PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1463{
1464 return PyModule_AddObject(m, name, PyString_FromString(value));
1465}
1466
1467#endif
1468
Fred Drake6f987622000-08-25 18:03:30 +00001469DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001470initpyexpat(void)
1471{
1472 PyObject *m, *d;
1473 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001474 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001475 PyObject *errors_module;
1476 PyObject *modelmod_name;
1477 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001478 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001479
Fred Drake6f987622000-08-25 18:03:30 +00001480 if (errmod_name == NULL)
1481 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001482 modelmod_name = PyString_FromString("pyexpat.model");
1483 if (modelmod_name == NULL)
1484 return;
Fred Drake6f987622000-08-25 18:03:30 +00001485
Fred Drake0582df92000-07-12 04:49:00 +00001486 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001487
Fred Drake0582df92000-07-12 04:49:00 +00001488 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001489 m = Py_InitModule3("pyexpat", pyexpat_methods,
1490 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001491
Fred Drake0582df92000-07-12 04:49:00 +00001492 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001493 if (ErrorObject == NULL) {
1494 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001495 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001496 if (ErrorObject == NULL)
1497 return;
1498 }
1499 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001500 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001501 Py_INCREF(ErrorObject);
1502 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001503 Py_INCREF(&Xmlparsetype);
1504 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001505
Fred Drake93adb692000-09-23 04:55:48 +00001506 PyModule_AddObject(m, "__version__",
1507 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake85d835f2001-02-08 15:39:08 +00001508#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001509 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1510 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001511 {
1512 XML_Expat_Version info = XML_ExpatVersionInfo();
1513 PyModule_AddObject(m, "version_info",
1514 Py_BuildValue("(iii)", info.major,
1515 info.minor, info.micro));
1516 }
Fred Drake738293d2000-12-21 17:25:07 +00001517#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001518#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1519#else
1520 init_template_buffer();
1521#endif
Fred Drake0582df92000-07-12 04:49:00 +00001522 /* XXX When Expat supports some way of figuring out how it was
1523 compiled, this should check and set native_encoding
1524 appropriately.
1525 */
Fred Drake93adb692000-09-23 04:55:48 +00001526 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001527
Fred Drake85d835f2001-02-08 15:39:08 +00001528 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001529 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001530 errors_module = PyDict_GetItem(d, errmod_name);
1531 if (errors_module == NULL) {
1532 errors_module = PyModule_New("pyexpat.errors");
1533 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001534 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001535 /* gives away the reference to errors_module */
1536 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001537 }
1538 }
Fred Drake6f987622000-08-25 18:03:30 +00001539 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001540 model_module = PyDict_GetItem(d, modelmod_name);
1541 if (model_module == NULL) {
1542 model_module = PyModule_New("pyexpat.model");
1543 if (model_module != NULL) {
1544 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1545 /* gives away the reference to model_module */
1546 PyModule_AddObject(m, "model", model_module);
1547 }
1548 }
1549 Py_DECREF(modelmod_name);
1550 if (errors_module == NULL || model_module == NULL)
1551 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001552 return;
1553
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001554#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001555 PyModule_AddStringConstant(errors_module, #name, \
1556 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001557
Fred Drake0582df92000-07-12 04:49:00 +00001558 MYCONST(XML_ERROR_NO_MEMORY);
1559 MYCONST(XML_ERROR_SYNTAX);
1560 MYCONST(XML_ERROR_NO_ELEMENTS);
1561 MYCONST(XML_ERROR_INVALID_TOKEN);
1562 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1563 MYCONST(XML_ERROR_PARTIAL_CHAR);
1564 MYCONST(XML_ERROR_TAG_MISMATCH);
1565 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1566 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1567 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1568 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1569 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1570 MYCONST(XML_ERROR_ASYNC_ENTITY);
1571 MYCONST(XML_ERROR_BAD_CHAR_REF);
1572 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1573 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1574 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1575 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1576 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001577 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1578 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1579 MYCONST(XML_ERROR_NOT_STANDALONE);
1580
Fred Drake85d835f2001-02-08 15:39:08 +00001581 PyModule_AddStringConstant(errors_module, "__doc__",
1582 "Constants used to describe error conditions.");
1583
Fred Drake93adb692000-09-23 04:55:48 +00001584#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001585
1586#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001587#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001588 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1589 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1590 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001591#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001592#endif
1593
Fred Drake85d835f2001-02-08 15:39:08 +00001594#if EXPAT_VERSION >= 0x015f00
1595#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1596 PyModule_AddStringConstant(model_module, "__doc__",
1597 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001598
Fred Drake85d835f2001-02-08 15:39:08 +00001599 MYCONST(XML_CTYPE_EMPTY);
1600 MYCONST(XML_CTYPE_ANY);
1601 MYCONST(XML_CTYPE_MIXED);
1602 MYCONST(XML_CTYPE_NAME);
1603 MYCONST(XML_CTYPE_CHOICE);
1604 MYCONST(XML_CTYPE_SEQ);
1605
1606 MYCONST(XML_CQUANT_NONE);
1607 MYCONST(XML_CQUANT_OPT);
1608 MYCONST(XML_CQUANT_REP);
1609 MYCONST(XML_CQUANT_PLUS);
1610#undef MYCONST
1611#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001612}
1613
Fred Drake6f987622000-08-25 18:03:30 +00001614static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001615clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001616{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001617 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001618
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001619 for (; handler_info[i].name!=NULL; i++) {
1620 if (decref){
1621 Py_XDECREF(self->handlers[i]);
1622 }
1623 self->handlers[i]=NULL;
1624 handler_info[i].setter(self->itself, NULL);
1625 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001626}
1627
Fred Drake6f987622000-08-25 18:03:30 +00001628typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001629
Fred Drake6f987622000-08-25 18:03:30 +00001630static void
1631pyxml_UpdatePairedHandlers(xmlparseobject *self,
1632 int startHandler,
1633 int endHandler,
1634 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001635{
1636 void *start_handler=NULL;
1637 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001638
Fred Drake0582df92000-07-12 04:49:00 +00001639 if (self->handlers[startHandler]
1640 && self->handlers[endHandler]!=Py_None) {
1641 start_handler=handler_info[startHandler].handler;
1642 }
1643 if (self->handlers[EndElement]
1644 && self->handlers[EndElement] !=Py_None) {
1645 end_handler=handler_info[endHandler].handler;
1646 }
1647 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001648}
1649
Fred Drake6f987622000-08-25 18:03:30 +00001650static void
1651pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001652{
1653 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1654 StartElement, EndElement,
1655 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001656}
1657
Fred Drake6f987622000-08-25 18:03:30 +00001658static void
1659pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001660{
1661 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1662 StartElement, EndElement,
1663 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001664}
1665
Fred Drake6f987622000-08-25 18:03:30 +00001666static void
1667pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001668{
1669 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1670 StartNamespaceDecl, EndNamespaceDecl,
1671 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001672}
1673
Fred Drake6f987622000-08-25 18:03:30 +00001674static void
1675pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001676{
1677 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1678 StartNamespaceDecl, EndNamespaceDecl,
1679 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001680}
1681
Fred Drake6f987622000-08-25 18:03:30 +00001682static void
1683pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001684{
1685 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1686 StartCdataSection, EndCdataSection,
1687 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001688}
1689
Fred Drake6f987622000-08-25 18:03:30 +00001690static void
1691pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001692{
1693 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1694 StartCdataSection, EndCdataSection,
1695 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001696}
1697
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001698#if EXPAT_VERSION >= 0x010200
1699
1700static void
1701pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1702{
1703 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1704 StartDoctypeDecl, EndDoctypeDecl,
1705 (pairsetter)XML_SetDoctypeDeclHandler);
1706}
1707
1708static void
1709pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1710{
1711 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1712 StartDoctypeDecl, EndDoctypeDecl,
1713 (pairsetter)XML_SetDoctypeDeclHandler);
1714}
1715
1716#endif
1717
Fred Drake0582df92000-07-12 04:49:00 +00001718statichere struct HandlerInfo handler_info[] = {
1719 {"StartElementHandler",
1720 pyxml_SetStartElementHandler,
1721 (xmlhandler)my_StartElementHandler},
1722 {"EndElementHandler",
1723 pyxml_SetEndElementHandler,
1724 (xmlhandler)my_EndElementHandler},
1725 {"ProcessingInstructionHandler",
1726 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1727 (xmlhandler)my_ProcessingInstructionHandler},
1728 {"CharacterDataHandler",
1729 (xmlhandlersetter)XML_SetCharacterDataHandler,
1730 (xmlhandler)my_CharacterDataHandler},
1731 {"UnparsedEntityDeclHandler",
1732 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1733 (xmlhandler)my_UnparsedEntityDeclHandler },
1734 {"NotationDeclHandler",
1735 (xmlhandlersetter)XML_SetNotationDeclHandler,
1736 (xmlhandler)my_NotationDeclHandler },
1737 {"StartNamespaceDeclHandler",
1738 pyxml_SetStartNamespaceDeclHandler,
1739 (xmlhandler)my_StartNamespaceDeclHandler },
1740 {"EndNamespaceDeclHandler",
1741 pyxml_SetEndNamespaceDeclHandler,
1742 (xmlhandler)my_EndNamespaceDeclHandler },
1743 {"CommentHandler",
1744 (xmlhandlersetter)XML_SetCommentHandler,
1745 (xmlhandler)my_CommentHandler},
1746 {"StartCdataSectionHandler",
1747 pyxml_SetStartCdataSection,
1748 (xmlhandler)my_StartCdataSectionHandler},
1749 {"EndCdataSectionHandler",
1750 pyxml_SetEndCdataSection,
1751 (xmlhandler)my_EndCdataSectionHandler},
1752 {"DefaultHandler",
1753 (xmlhandlersetter)XML_SetDefaultHandler,
1754 (xmlhandler)my_DefaultHandler},
1755 {"DefaultHandlerExpand",
1756 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1757 (xmlhandler)my_DefaultHandlerExpandHandler},
1758 {"NotStandaloneHandler",
1759 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1760 (xmlhandler)my_NotStandaloneHandler},
1761 {"ExternalEntityRefHandler",
1762 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1763 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001764#if EXPAT_VERSION >= 0x010200
1765 {"StartDoctypeDeclHandler",
1766 pyxml_SetStartDoctypeDeclHandler,
1767 (xmlhandler)my_StartDoctypeDeclHandler},
1768 {"EndDoctypeDeclHandler",
1769 pyxml_SetEndDoctypeDeclHandler,
1770 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001771#endif
1772#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001773 {"ExternalParsedEntityDeclHandler",
1774 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1775 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1776 {"InternalParsedEntityDeclHandler",
1777 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1778 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001779#endif
1780#if EXPAT_VERSION >= 0x015f00
1781 {"EntityDeclHandler",
1782 (xmlhandlersetter)XML_SetEntityDeclHandler,
1783 (xmlhandler)my_EntityDeclHandler},
1784 {"XmlDeclHandler",
1785 (xmlhandlersetter)XML_SetXmlDeclHandler,
1786 (xmlhandler)my_XmlDeclHandler},
1787 {"ElementDeclHandler",
1788 (xmlhandlersetter)XML_SetElementDeclHandler,
1789 (xmlhandler)my_ElementDeclHandler},
1790 {"AttlistDeclHandler",
1791 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1792 (xmlhandler)my_AttlistDeclHandler},
1793#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001794
Fred Drake0582df92000-07-12 04:49:00 +00001795 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001796};