blob: b4634c82c4a6ce414d89f833561769191a2bce7e [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 Drake85d835f2001-02-08 15:39:08 +0000500#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000501#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake85d835f2001-02-08 15:39:08 +0000502VOID_HANDLER(StartElement,
503 (void *userData, const XML_Char *name, const XML_Char **atts),
504 ("(O&O&)", STRING_CONV_FUNC, name,
505 conv_atts_using_string, atts))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000506#else
507/* Python 1.6 and later */
Fred Drake85d835f2001-02-08 15:39:08 +0000508VOID_HANDLER(StartElement,
509 (void *userData, const XML_Char *name, const XML_Char **atts),
510 ("(O&O&)", STRING_CONV_FUNC, name,
511 (self->returns_unicode
512 ? conv_atts_using_unicode
513 : conv_atts_using_string), atts))
514#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000515#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000516
Fred Drake6f987622000-08-25 18:03:30 +0000517VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000518 (void *userData, const XML_Char *name),
519 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000520
Fred Drake6f987622000-08-25 18:03:30 +0000521VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000522 (void *userData,
523 const XML_Char *target,
524 const XML_Char *data),
525 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000526
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000527#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000528VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000529 (void *userData, const XML_Char *data, int len),
530 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000531#else
Fred Drake6f987622000-08-25 18:03:30 +0000532VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000533 (void *userData, const XML_Char *data, int len),
534 ("(N)", (self->returns_unicode
535 ? conv_string_len_to_unicode(data,len)
536 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000537#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000538
Fred Drake6f987622000-08-25 18:03:30 +0000539VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000540 (void *userData,
541 const XML_Char *entityName,
542 const XML_Char *base,
543 const XML_Char *systemId,
544 const XML_Char *publicId,
545 const XML_Char *notationName),
546 ("(O&O&O&O&O&)",
547 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
548 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
549 STRING_CONV_FUNC,notationName))
550
551#if EXPAT_VERSION >= 0x015f00
552#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
553VOID_HANDLER(EntityDecl,
554 (void *userData,
555 const XML_Char *entityName,
556 int is_parameter_entity,
557 const XML_Char *value,
558 int value_length,
559 const XML_Char *base,
560 const XML_Char *systemId,
561 const XML_Char *publicId,
562 const XML_Char *notationName),
563 ("O&iNO&O&O&O&",
564 STRING_CONV_FUNC,entityName, is_parameter_entity,
565 conv_string_len_to_utf8(value, value_length),
566 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
567 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
568#else
569VOID_HANDLER(EntityDecl,
570 (void *userData,
571 const XML_Char *entityName,
572 int is_parameter_entity,
573 const XML_Char *value,
574 int value_length,
575 const XML_Char *base,
576 const XML_Char *systemId,
577 const XML_Char *publicId,
578 const XML_Char *notationName),
579 ("O&iNO&O&O&O&",
580 STRING_CONV_FUNC,entityName, is_parameter_entity,
581 (self->returns_unicode
582 ? conv_string_len_to_unicode(value, value_length)
583 : conv_string_len_to_utf8(value, value_length)),
584 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
585 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
586#endif
587
588VOID_HANDLER(XmlDecl,
589 (void *userData,
590 const XML_Char *version,
591 const XML_Char *encoding,
592 int standalone),
593 ("(O&O&i)",
594 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
595 standalone))
596
597static PyObject *
598conv_content_model(XML_Content * const model,
599 PyObject *(*conv_string)(XML_Char *))
600{
601 PyObject *result = NULL;
602 PyObject *children = PyTuple_New(model->numchildren);
603 int i;
604
605 if (children != NULL) {
606 for (i = 0; i < model->numchildren; ++i) {
607 PyObject *child = conv_content_model(&model->children[i],
608 conv_string);
609 if (child == NULL) {
610 Py_XDECREF(children);
611 return NULL;
612 }
613 PyTuple_SET_ITEM(children, i, child);
614 }
615 result = Py_BuildValue("(iiO&N)",
616 model->type, model->quant,
617 conv_string,model->name, children);
618 }
619 return result;
620}
621
622static PyObject *
623conv_content_model_utf8(XML_Content * const model)
624{
625 return conv_content_model(model, conv_string_to_utf8);
626}
627
628#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
629static PyObject *
630conv_content_model_unicode(XML_Content * const model)
631{
632 return conv_content_model(model, conv_string_to_unicode);
633}
634
635VOID_HANDLER(ElementDecl,
636 (void *userData,
637 const XML_Char *name,
638 XML_Content *model),
639 ("O&O&",
640 STRING_CONV_FUNC,name,
641 (self->returns_unicode ? conv_content_model_unicode
642 : conv_content_model_utf8),model))
643#else
644VOID_HANDLER(ElementDecl,
645 (void *userData,
646 const XML_Char *name,
647 XML_Content *model),
648 ("O&O&",
649 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
650#endif
651
652VOID_HANDLER(AttlistDecl,
653 (void *userData,
654 const XML_Char *elname,
655 const XML_Char *attname,
656 const XML_Char *att_type,
657 const XML_Char *dflt,
658 int isrequired),
659 ("(O&O&O&O&i)",
660 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
661 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
662 isrequired))
663#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000664
Fred Drake6f987622000-08-25 18:03:30 +0000665VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000666 (void *userData,
667 const XML_Char *notationName,
668 const XML_Char *base,
669 const XML_Char *systemId,
670 const XML_Char *publicId),
671 ("(O&O&O&O&)",
672 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
673 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674
Fred Drake6f987622000-08-25 18:03:30 +0000675VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000676 (void *userData,
677 const XML_Char *prefix,
678 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000679 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680
Fred Drake6f987622000-08-25 18:03:30 +0000681VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000682 (void *userData,
683 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000684 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685
Fred Drake6f987622000-08-25 18:03:30 +0000686VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000687 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000688 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000689
Fred Drake6f987622000-08-25 18:03:30 +0000690VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000691 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000692 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000693
Fred Drake6f987622000-08-25 18:03:30 +0000694VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000695 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000696 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000697
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000698#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000699VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000700 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000701 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000702
Fred Drake6f987622000-08-25 18:03:30 +0000703VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000705 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000706#else
Fred Drake6f987622000-08-25 18:03:30 +0000707VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000708 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000709 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000710 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000711 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000712
Fred Drake6f987622000-08-25 18:03:30 +0000713VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000714 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000715 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000716 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000717 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000718#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000719
Fred Drake6f987622000-08-25 18:03:30 +0000720INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000721 (void *userData),
722 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000723
Fred Drake6f987622000-08-25 18:03:30 +0000724RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000725 (XML_Parser parser,
726 const XML_Char *context,
727 const XML_Char *base,
728 const XML_Char *systemId,
729 const XML_Char *publicId),
730 int rc=0;,
731 ("(O&O&O&O&)",
732 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000733 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
734 rc = PyInt_AsLong(rv);, rc,
735 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000736
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000737/* XXX UnknownEncodingHandler */
738
Fred Drake85d835f2001-02-08 15:39:08 +0000739#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000740VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000741 (void *userData, const XML_Char *doctypeName),
742 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
743 Py_None, Py_None, -1))
744#elif EXPAT_VERSION >= 0x015f00
745VOID_HANDLER(StartDoctypeDecl,
746 (void *userData, const XML_Char *doctypeName,
747 const XML_Char *sysid, const XML_Char *pubid,
748 int has_internal_subset),
749 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
750 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
751 has_internal_subset))
752#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000753
Fred Drake85d835f2001-02-08 15:39:08 +0000754#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000755VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000756#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000757
Fred Drake85d835f2001-02-08 15:39:08 +0000758#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000759VOID_HANDLER(ExternalParsedEntityDecl,
760 (void *userData, const XML_Char *entityName,
761 const XML_Char *base, const XML_Char *systemId,
762 const XML_Char *publicId),
763 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
764 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
765 STRING_CONV_FUNC, publicId))
766
767VOID_HANDLER(InternalParsedEntityDecl,
768 (void *userData, const XML_Char *entityName,
769 const XML_Char *replacementText, int replacementTextLength),
770 ("(O&O&i)", STRING_CONV_FUNC, entityName,
771 STRING_CONV_FUNC, replacementText, replacementTextLength))
772
Fred Drake85d835f2001-02-08 15:39:08 +0000773#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000774
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000775/* ---------------------------------------------------------------- */
776
777static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000778"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000779Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000780
781static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000782xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783{
Fred Drake0582df92000-07-12 04:49:00 +0000784 char *s;
785 int slen;
786 int isFinal = 0;
787 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000788
Fred Drake0582df92000-07-12 04:49:00 +0000789 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
790 return NULL;
791 rv = XML_Parse(self->itself, s, slen, isFinal);
792 if (PyErr_Occurred()) {
793 return NULL;
794 }
795 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000796 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000797 }
798 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000799}
800
Fred Drakeca1f4262000-09-21 20:10:23 +0000801/* File reading copied from cPickle */
802
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000803#define BUF_SIZE 2048
804
Fred Drake0582df92000-07-12 04:49:00 +0000805static int
806readinst(char *buf, int buf_size, PyObject *meth)
807{
808 PyObject *arg = NULL;
809 PyObject *bytes = NULL;
810 PyObject *str = NULL;
811 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000812
Fred Drake676940b2000-09-22 15:21:31 +0000813 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000814 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000815
Fred Drakeca1f4262000-09-21 20:10:23 +0000816 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000817 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000818
Tim Peters954eef72000-09-22 06:01:11 +0000819 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000820
Fred Drakeca1f4262000-09-21 20:10:23 +0000821 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000822 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823
Fred Drake0582df92000-07-12 04:49:00 +0000824 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000825 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000826 PyErr_Format(PyExc_TypeError,
827 "read() did not return a string object (type=%.400s)",
828 str->ob_type->tp_name);
829 goto finally;
830 }
831 len = PyString_GET_SIZE(str);
832 if (len > buf_size) {
833 PyErr_Format(PyExc_ValueError,
834 "read() returned too much data: "
835 "%i bytes requested, %i returned",
836 buf_size, len);
837 Py_DECREF(str);
838 goto finally;
839 }
840 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841finally:
Fred Drake0582df92000-07-12 04:49:00 +0000842 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000843 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000844 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000845}
846
847static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000848"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000849Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000850
851static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000852xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000853{
Fred Drake0582df92000-07-12 04:49:00 +0000854 int rv = 1;
855 PyObject *f;
856 FILE *fp;
857 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000858
Fred Drake0582df92000-07-12 04:49:00 +0000859 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
860 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000861
Fred Drake0582df92000-07-12 04:49:00 +0000862 if (PyFile_Check(f)) {
863 fp = PyFile_AsFile(f);
864 }
865 else{
866 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000867 readmethod = PyObject_GetAttrString(f, "read");
868 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000869 PyErr_Clear();
870 PyErr_SetString(PyExc_TypeError,
871 "argument must have 'read' attribute");
872 return 0;
873 }
874 }
875 for (;;) {
876 int bytes_read;
877 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
878 if (buf == NULL)
879 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000880
Fred Drake0582df92000-07-12 04:49:00 +0000881 if (fp) {
882 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
883 if (bytes_read < 0) {
884 PyErr_SetFromErrno(PyExc_IOError);
885 return NULL;
886 }
887 }
888 else {
889 bytes_read = readinst(buf, BUF_SIZE, readmethod);
890 if (bytes_read < 0)
891 return NULL;
892 }
893 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
894 if (PyErr_Occurred())
895 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896
Fred Drake0582df92000-07-12 04:49:00 +0000897 if (!rv || bytes_read == 0)
898 break;
899 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000900 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000901 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000902 }
Fred Drake0582df92000-07-12 04:49:00 +0000903 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000904}
905
906static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000907"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000908Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000909
910static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000911xmlparse_SetBase(xmlparseobject *self, PyObject *args)
912{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000913 char *base;
914
Fred Drake0582df92000-07-12 04:49:00 +0000915 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000916 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000917 if (!XML_SetBase(self->itself, base)) {
918 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000919 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920 Py_INCREF(Py_None);
921 return Py_None;
922}
923
924static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000925"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000926Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000927
928static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000929xmlparse_GetBase(xmlparseobject *self, PyObject *args)
930{
931 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000932 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000933
Fred Drake0582df92000-07-12 04:49:00 +0000934 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000935}
936
Fred Drakebd6101c2001-02-14 18:29:45 +0000937#if EXPAT_VERSION >= 0x015f00
938static char xmlparse_GetInputContext__doc__[] =
939"GetInputContext() -> string\n\
940Return the untranslated text of the input that caused the current event.\n\
941If the event was generated by a large amount of text (such as a start tag\n\
942for an element with many attributes), not all of the text may be available.";
943
944static PyObject *
945xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
946{
947 PyObject *result = NULL;
948
949 if (PyArg_ParseTuple(args, ":GetInputContext")) {
950 if (self->in_callback) {
951 int offset, size;
952 const char *buffer
953 = XML_GetInputContext(self->itself, &offset, &size);
954
955 if (buffer != NULL)
956 result = PyString_FromStringAndSize(buffer + offset, size);
957 else {
958 result = Py_None;
959 Py_INCREF(result);
960 }
961 }
962 else {
963 result = Py_None;
964 Py_INCREF(result);
965 }
966 }
967 return result;
968}
969#endif
970
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000971static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000972"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000973Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000974information passed to the ExternalEntityRefHandler.";
975
976static PyObject *
977xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
978{
979 char *context;
980 char *encoding = NULL;
981 xmlparseobject *new_parser;
982 int i;
983
984 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
985 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000986 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000987 }
988
989#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
990 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000991#else
Fred Drake85d835f2001-02-08 15:39:08 +0000992 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000993 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000994#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000995
996 if (new_parser == NULL)
997 return NULL;
998 new_parser->returns_unicode = self->returns_unicode;
999 new_parser->ordered_attributes = self->ordered_attributes;
1000 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001001 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001002 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001003 encoding);
1004 new_parser->handlers = 0;
1005 PyObject_GC_Init(new_parser);
1006
1007 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001008 Py_DECREF(new_parser);
1009 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001010 }
1011
1012 XML_SetUserData(new_parser->itself, (void *)new_parser);
1013
1014 /* allocate and clear handlers first */
1015 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001016 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001017
1018 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001019 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001020 Py_DECREF(new_parser);
1021 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001022 }
1023 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001024
1025 /* then copy handlers from self */
1026 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001027 if (self->handlers[i]) {
1028 Py_INCREF(self->handlers[i]);
1029 new_parser->handlers[i] = self->handlers[i];
1030 handler_info[i].setter(new_parser->itself,
1031 handler_info[i].handler);
1032 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001033 }
Fred Drake28adf522000-09-24 22:07:59 +00001034 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001035}
1036
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001037#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001038
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001039static char xmlparse_SetParamEntityParsing__doc__[] =
1040"SetParamEntityParsing(flag) -> success\n\
1041Controls parsing of parameter entities (including the external DTD\n\
1042subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1043XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1044XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1045was successful.";
1046
1047static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001048xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001049{
Fred Drake85d835f2001-02-08 15:39:08 +00001050 int flag;
1051 if (!PyArg_ParseTuple(args, "i", &flag))
1052 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001053 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001054 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001055}
1056
Fred Drake85d835f2001-02-08 15:39:08 +00001057#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001058
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001059static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001060 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001061 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001062 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001063 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001064 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001065 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001066 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001067 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001068 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1069 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001070#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001071 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1072 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1073#endif
1074#if EXPAT_VERSION >= 0x015f00
1075 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1076 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001078 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001079};
1080
1081/* ---------- */
1082
1083
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001084#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1085
1086/*
1087 pyexpat international encoding support.
1088 Make it as simple as possible.
1089*/
1090
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001091static char template_buffer[257];
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092PyObject * template_string=NULL;
1093
1094static void
1095init_template_buffer(void)
1096{
1097 int i;
1098 for (i=0;i<256;i++) {
1099 template_buffer[i]=i;
1100 };
1101 template_buffer[256]=0;
1102};
1103
1104int
1105PyUnknownEncodingHandler(void *encodingHandlerData,
1106const XML_Char *name,
1107XML_Encoding * info)
1108{
1109 PyUnicodeObject * _u_string=NULL;
1110 int result=0;
1111 int i;
1112
1113 _u_string=(PyUnicodeObject *) PyUnicode_Decode(template_buffer, 256, name, "replace"); // Yes, supports only 8bit encodings
1114
1115 if (_u_string==NULL) {
1116 return result;
1117 };
1118
1119 for (i=0; i<256; i++) {
1120 Py_UNICODE c = _u_string->str[i] ; // Stupid to access directly, but fast
1121 if (c==Py_UNICODE_REPLACEMENT_CHARACTER) {
1122 info->map[i] = -1;
1123 } else {
1124 info->map[i] = c;
1125 };
1126 };
1127
1128 info->data = NULL;
1129 info->convert = NULL;
1130 info->release = NULL;
1131 result=1;
1132
1133 Py_DECREF(_u_string);
1134 return result;
1135}
1136
1137#endif
1138
1139static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001140newxmlparseobject(char *encoding, char *namespace_separator)
1141{
1142 int i;
1143 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001144
1145#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001146 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1147 if (self == NULL)
1148 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001149
Fred Drake0582df92000-07-12 04:49:00 +00001150 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001151#else
Fred Drake0582df92000-07-12 04:49:00 +00001152 /* Code for versions 1.6 and later */
1153 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1154 if (self == NULL)
1155 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001156
Fred Drake0582df92000-07-12 04:49:00 +00001157 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001158#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001159 self->ordered_attributes = 0;
1160 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001161 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001162 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001163 if (namespace_separator) {
1164 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1165 }
Fred Drake85d835f2001-02-08 15:39:08 +00001166 else {
Fred Drake0582df92000-07-12 04:49:00 +00001167 self->itself = XML_ParserCreate(encoding);
1168 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001169 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001170 if (self->itself == NULL) {
1171 PyErr_SetString(PyExc_RuntimeError,
1172 "XML_ParserCreate failed");
1173 Py_DECREF(self);
1174 return NULL;
1175 }
1176 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001177#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1178#else
1179 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1180#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001181
Fred Drake0582df92000-07-12 04:49:00 +00001182 for(i = 0; handler_info[i].name != NULL; i++)
1183 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001184
Fred Drake0582df92000-07-12 04:49:00 +00001185 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001186 if (!self->handlers){
1187 Py_DECREF(self);
1188 return PyErr_NoMemory();
1189 }
1190 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001191
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001192 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001193}
1194
1195
1196static void
Fred Drake0582df92000-07-12 04:49:00 +00001197xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001198{
Fred Drake0582df92000-07-12 04:49:00 +00001199 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001200 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001201 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001202 XML_ParserFree(self->itself);
1203 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001204
Fred Drake85d835f2001-02-08 15:39:08 +00001205 if (self->handlers != NULL) {
1206 for (i = 0; handler_info[i].name != NULL; i++) {
1207 Py_XDECREF(self->handlers[i]);
1208 }
1209 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001210 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001211#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001212 /* Code for versions before 1.6 */
1213 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001214#else
Fred Drake0582df92000-07-12 04:49:00 +00001215 /* Code for versions 1.6 and later */
1216 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001217#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001218}
1219
Fred Drake0582df92000-07-12 04:49:00 +00001220static int
1221handlername2int(const char *name)
1222{
1223 int i;
1224 for (i=0; handler_info[i].name != NULL; i++) {
1225 if (strcmp(name, handler_info[i].name) == 0) {
1226 return i;
1227 }
1228 }
1229 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001230}
1231
1232static PyObject *
1233xmlparse_getattr(xmlparseobject *self, char *name)
1234{
Fred Drake0582df92000-07-12 04:49:00 +00001235 int handlernum;
1236 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001237 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001238 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001239 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001240 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001241 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001242 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001243 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1244 if (strcmp(name, "ordered_attributes") == 0)
1245 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001246 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001247 return PyInt_FromLong((long) self->returns_unicode);
1248 if (strcmp(name, "specified_attributes") == 0)
1249 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001250
Fred Drake0582df92000-07-12 04:49:00 +00001251 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001252
Fred Drake0582df92000-07-12 04:49:00 +00001253 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1254 Py_INCREF(self->handlers[handlernum]);
1255 return self->handlers[handlernum];
1256 }
1257 if (strcmp(name, "__members__") == 0) {
1258 int i;
1259 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001260 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001261 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001262 }
1263 PyList_Append(rc, PyString_FromString("ErrorCode"));
1264 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1265 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1266 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001267 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001268 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001269 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001270
Fred Drake0582df92000-07-12 04:49:00 +00001271 return rc;
1272 }
1273 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001274}
1275
Fred Drake6f987622000-08-25 18:03:30 +00001276static int
1277sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001278{
1279 int handlernum = handlername2int(name);
1280 if (handlernum != -1) {
1281 Py_INCREF(v);
1282 Py_XDECREF(self->handlers[handlernum]);
1283 self->handlers[handlernum] = v;
1284 handler_info[handlernum].setter(self->itself,
1285 handler_info[handlernum].handler);
1286 return 1;
1287 }
1288 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001289}
1290
1291static int
Fred Drake6f987622000-08-25 18:03:30 +00001292xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001293{
Fred Drake6f987622000-08-25 18:03:30 +00001294 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001295 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001296 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1297 return -1;
1298 }
Fred Drake85d835f2001-02-08 15:39:08 +00001299 if (strcmp(name, "ordered_attributes") == 0) {
1300 if (PyObject_IsTrue(v))
1301 self->ordered_attributes = 1;
1302 else
1303 self->ordered_attributes = 0;
1304 return 0;
1305 }
Fred Drake6f987622000-08-25 18:03:30 +00001306 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001307 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001308#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001309 PyErr_SetString(PyExc_ValueError,
1310 "Cannot return Unicode strings in Python 1.5");
1311 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001312#else
Fred Drake6f987622000-08-25 18:03:30 +00001313 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001314#endif
Fred Drake6f987622000-08-25 18:03:30 +00001315 }
1316 else
1317 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001318 return 0;
1319 }
1320 if (strcmp(name, "specified_attributes") == 0) {
1321 if (PyObject_IsTrue(v))
1322 self->specified_attributes = 1;
1323 else
1324 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001325 return 0;
1326 }
1327 if (sethandler(self, name, v)) {
1328 return 0;
1329 }
1330 PyErr_SetString(PyExc_AttributeError, name);
1331 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001332}
1333
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001334#ifdef WITH_CYCLE_GC
1335static int
1336xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1337{
1338 int i, err;
1339 for (i = 0; handler_info[i].name != NULL; i++) {
1340 if (!op->handlers[i])
1341 continue;
1342 err = visit(op->handlers[i], arg);
1343 if (err)
1344 return err;
1345 }
1346 return 0;
1347}
1348
1349static int
1350xmlparse_clear(xmlparseobject *op)
1351{
1352 clear_handlers(op, 1);
1353 return 0;
1354}
1355#endif
1356
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001357static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001358"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001359
1360static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001361 PyObject_HEAD_INIT(NULL)
1362 0, /*ob_size*/
1363 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001364 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001365 0, /*tp_itemsize*/
1366 /* methods */
1367 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1368 (printfunc)0, /*tp_print*/
1369 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1370 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1371 (cmpfunc)0, /*tp_compare*/
1372 (reprfunc)0, /*tp_repr*/
1373 0, /*tp_as_number*/
1374 0, /*tp_as_sequence*/
1375 0, /*tp_as_mapping*/
1376 (hashfunc)0, /*tp_hash*/
1377 (ternaryfunc)0, /*tp_call*/
1378 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001379 0, /* tp_getattro */
1380 0, /* tp_setattro */
1381 0, /* tp_as_buffer */
1382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1383 Xmlparsetype__doc__, /* Documentation string */
1384#ifdef WITH_CYCLE_GC
1385 (traverseproc)xmlparse_traverse, /* tp_traverse */
1386 (inquiry)xmlparse_clear /* tp_clear */
1387#else
1388 0, 0
1389#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001390};
1391
1392/* End of code for xmlparser objects */
1393/* -------------------------------------------------------- */
1394
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001395static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001396"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1397Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001398
1399static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001400pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1401{
1402 char *encoding = NULL;
1403 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001404 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001405
Fred Drake0582df92000-07-12 04:49:00 +00001406 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001407 &encoding, &namespace_separator))
1408 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001409 if (namespace_separator != NULL
1410 && strlen(namespace_separator) != 1) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "namespace_separator must be one character,"
1413 " omitted, or None");
1414 return NULL;
1415 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001416 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001417}
1418
1419static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001420"ErrorString(errno) -> string\n\
1421Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001422
1423static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001424pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001425{
Fred Drake0582df92000-07-12 04:49:00 +00001426 long code = 0;
1427
1428 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1429 return NULL;
1430 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431}
1432
1433/* List of methods defined in the module */
1434
1435static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001436 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1437 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1438 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1439 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001440
Fred Drake0582df92000-07-12 04:49:00 +00001441 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001442};
1443
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001444/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001445
1446static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001447"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001448
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001449/* Initialization function for the module */
1450
Fred Drake93adb692000-09-23 04:55:48 +00001451void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001452
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001453#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001454
1455/* 1.5 compatibility: PyModule_AddObject */
1456static int
1457PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1458{
1459 PyObject *dict;
1460 if (!PyModule_Check(m) || o == NULL)
1461 return -1;
1462 dict = PyModule_GetDict(m);
1463 if (dict == NULL)
1464 return -1;
1465 if (PyDict_SetItemString(dict, name, o))
1466 return -1;
1467 Py_DECREF(o);
1468 return 0;
1469}
1470
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001471int
1472PyModule_AddIntConstant(PyObject *m, char *name, long value)
1473{
1474 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1475}
1476
Fred Drakea77254a2000-09-29 19:23:29 +00001477static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001478PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1479{
1480 return PyModule_AddObject(m, name, PyString_FromString(value));
1481}
1482
1483#endif
1484
Fred Drake6f987622000-08-25 18:03:30 +00001485DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001486initpyexpat(void)
1487{
1488 PyObject *m, *d;
1489 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001490 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001491 PyObject *errors_module;
1492 PyObject *modelmod_name;
1493 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001494 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001495
Fred Drake6f987622000-08-25 18:03:30 +00001496 if (errmod_name == NULL)
1497 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001498 modelmod_name = PyString_FromString("pyexpat.model");
1499 if (modelmod_name == NULL)
1500 return;
Fred Drake6f987622000-08-25 18:03:30 +00001501
Fred Drake0582df92000-07-12 04:49:00 +00001502 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001503
Fred Drake0582df92000-07-12 04:49:00 +00001504 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001505 m = Py_InitModule3("pyexpat", pyexpat_methods,
1506 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001507
Fred Drake0582df92000-07-12 04:49:00 +00001508 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001509 if (ErrorObject == NULL) {
1510 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001511 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001512 if (ErrorObject == NULL)
1513 return;
1514 }
1515 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001516 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001517 Py_INCREF(ErrorObject);
1518 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001519 Py_INCREF(&Xmlparsetype);
1520 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001521
Fred Drake93adb692000-09-23 04:55:48 +00001522 PyModule_AddObject(m, "__version__",
1523 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake85d835f2001-02-08 15:39:08 +00001524#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001525 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1526 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001527 {
1528 XML_Expat_Version info = XML_ExpatVersionInfo();
1529 PyModule_AddObject(m, "version_info",
1530 Py_BuildValue("(iii)", info.major,
1531 info.minor, info.micro));
1532 }
Fred Drake738293d2000-12-21 17:25:07 +00001533#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001534#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1535#else
1536 init_template_buffer();
1537#endif
Fred Drake0582df92000-07-12 04:49:00 +00001538 /* XXX When Expat supports some way of figuring out how it was
1539 compiled, this should check and set native_encoding
1540 appropriately.
1541 */
Fred Drake93adb692000-09-23 04:55:48 +00001542 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001543
Fred Drake85d835f2001-02-08 15:39:08 +00001544 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001545 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001546 errors_module = PyDict_GetItem(d, errmod_name);
1547 if (errors_module == NULL) {
1548 errors_module = PyModule_New("pyexpat.errors");
1549 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001550 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001551 /* gives away the reference to errors_module */
1552 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001553 }
1554 }
Fred Drake6f987622000-08-25 18:03:30 +00001555 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001556 model_module = PyDict_GetItem(d, modelmod_name);
1557 if (model_module == NULL) {
1558 model_module = PyModule_New("pyexpat.model");
1559 if (model_module != NULL) {
1560 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1561 /* gives away the reference to model_module */
1562 PyModule_AddObject(m, "model", model_module);
1563 }
1564 }
1565 Py_DECREF(modelmod_name);
1566 if (errors_module == NULL || model_module == NULL)
1567 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001568 return;
1569
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001571 PyModule_AddStringConstant(errors_module, #name, \
1572 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001573
Fred Drake0582df92000-07-12 04:49:00 +00001574 MYCONST(XML_ERROR_NO_MEMORY);
1575 MYCONST(XML_ERROR_SYNTAX);
1576 MYCONST(XML_ERROR_NO_ELEMENTS);
1577 MYCONST(XML_ERROR_INVALID_TOKEN);
1578 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1579 MYCONST(XML_ERROR_PARTIAL_CHAR);
1580 MYCONST(XML_ERROR_TAG_MISMATCH);
1581 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1582 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1583 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1584 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1585 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1586 MYCONST(XML_ERROR_ASYNC_ENTITY);
1587 MYCONST(XML_ERROR_BAD_CHAR_REF);
1588 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1589 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1590 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1591 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1592 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001593 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1594 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1595 MYCONST(XML_ERROR_NOT_STANDALONE);
1596
Fred Drake85d835f2001-02-08 15:39:08 +00001597 PyModule_AddStringConstant(errors_module, "__doc__",
1598 "Constants used to describe error conditions.");
1599
Fred Drake93adb692000-09-23 04:55:48 +00001600#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001601
1602#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001603#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001604 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1605 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1606 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001607#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001608#endif
1609
Fred Drake85d835f2001-02-08 15:39:08 +00001610#if EXPAT_VERSION >= 0x015f00
1611#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1612 PyModule_AddStringConstant(model_module, "__doc__",
1613 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001614
Fred Drake85d835f2001-02-08 15:39:08 +00001615 MYCONST(XML_CTYPE_EMPTY);
1616 MYCONST(XML_CTYPE_ANY);
1617 MYCONST(XML_CTYPE_MIXED);
1618 MYCONST(XML_CTYPE_NAME);
1619 MYCONST(XML_CTYPE_CHOICE);
1620 MYCONST(XML_CTYPE_SEQ);
1621
1622 MYCONST(XML_CQUANT_NONE);
1623 MYCONST(XML_CQUANT_OPT);
1624 MYCONST(XML_CQUANT_REP);
1625 MYCONST(XML_CQUANT_PLUS);
1626#undef MYCONST
1627#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001628}
1629
Fred Drake6f987622000-08-25 18:03:30 +00001630static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001631clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001632{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001633 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001634
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001635 for (; handler_info[i].name!=NULL; i++) {
1636 if (decref){
1637 Py_XDECREF(self->handlers[i]);
1638 }
1639 self->handlers[i]=NULL;
1640 handler_info[i].setter(self->itself, NULL);
1641 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001642}
1643
Fred Drake6f987622000-08-25 18:03:30 +00001644typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001645
Fred Drake6f987622000-08-25 18:03:30 +00001646static void
1647pyxml_UpdatePairedHandlers(xmlparseobject *self,
1648 int startHandler,
1649 int endHandler,
1650 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001651{
1652 void *start_handler=NULL;
1653 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001654
Fred Drake0582df92000-07-12 04:49:00 +00001655 if (self->handlers[startHandler]
1656 && self->handlers[endHandler]!=Py_None) {
1657 start_handler=handler_info[startHandler].handler;
1658 }
1659 if (self->handlers[EndElement]
1660 && self->handlers[EndElement] !=Py_None) {
1661 end_handler=handler_info[endHandler].handler;
1662 }
1663 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001664}
1665
Fred Drake6f987622000-08-25 18:03:30 +00001666static void
1667pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001668{
1669 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1670 StartElement, EndElement,
1671 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001672}
1673
Fred Drake6f987622000-08-25 18:03:30 +00001674static void
1675pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001676{
1677 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1678 StartElement, EndElement,
1679 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001680}
1681
Fred Drake6f987622000-08-25 18:03:30 +00001682static void
1683pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001684{
1685 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1686 StartNamespaceDecl, EndNamespaceDecl,
1687 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001688}
1689
Fred Drake6f987622000-08-25 18:03:30 +00001690static void
1691pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001692{
1693 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1694 StartNamespaceDecl, EndNamespaceDecl,
1695 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001696}
1697
Fred Drake6f987622000-08-25 18:03:30 +00001698static void
1699pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001700{
1701 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1702 StartCdataSection, EndCdataSection,
1703 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001704}
1705
Fred Drake6f987622000-08-25 18:03:30 +00001706static void
1707pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001708{
1709 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1710 StartCdataSection, EndCdataSection,
1711 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001712}
1713
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001714#if EXPAT_VERSION >= 0x010200
1715
1716static void
1717pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1718{
1719 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1720 StartDoctypeDecl, EndDoctypeDecl,
1721 (pairsetter)XML_SetDoctypeDeclHandler);
1722}
1723
1724static void
1725pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1726{
1727 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1728 StartDoctypeDecl, EndDoctypeDecl,
1729 (pairsetter)XML_SetDoctypeDeclHandler);
1730}
1731
1732#endif
1733
Fred Drake0582df92000-07-12 04:49:00 +00001734statichere struct HandlerInfo handler_info[] = {
1735 {"StartElementHandler",
1736 pyxml_SetStartElementHandler,
1737 (xmlhandler)my_StartElementHandler},
1738 {"EndElementHandler",
1739 pyxml_SetEndElementHandler,
1740 (xmlhandler)my_EndElementHandler},
1741 {"ProcessingInstructionHandler",
1742 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1743 (xmlhandler)my_ProcessingInstructionHandler},
1744 {"CharacterDataHandler",
1745 (xmlhandlersetter)XML_SetCharacterDataHandler,
1746 (xmlhandler)my_CharacterDataHandler},
1747 {"UnparsedEntityDeclHandler",
1748 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1749 (xmlhandler)my_UnparsedEntityDeclHandler },
1750 {"NotationDeclHandler",
1751 (xmlhandlersetter)XML_SetNotationDeclHandler,
1752 (xmlhandler)my_NotationDeclHandler },
1753 {"StartNamespaceDeclHandler",
1754 pyxml_SetStartNamespaceDeclHandler,
1755 (xmlhandler)my_StartNamespaceDeclHandler },
1756 {"EndNamespaceDeclHandler",
1757 pyxml_SetEndNamespaceDeclHandler,
1758 (xmlhandler)my_EndNamespaceDeclHandler },
1759 {"CommentHandler",
1760 (xmlhandlersetter)XML_SetCommentHandler,
1761 (xmlhandler)my_CommentHandler},
1762 {"StartCdataSectionHandler",
1763 pyxml_SetStartCdataSection,
1764 (xmlhandler)my_StartCdataSectionHandler},
1765 {"EndCdataSectionHandler",
1766 pyxml_SetEndCdataSection,
1767 (xmlhandler)my_EndCdataSectionHandler},
1768 {"DefaultHandler",
1769 (xmlhandlersetter)XML_SetDefaultHandler,
1770 (xmlhandler)my_DefaultHandler},
1771 {"DefaultHandlerExpand",
1772 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1773 (xmlhandler)my_DefaultHandlerExpandHandler},
1774 {"NotStandaloneHandler",
1775 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1776 (xmlhandler)my_NotStandaloneHandler},
1777 {"ExternalEntityRefHandler",
1778 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1779 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001780#if EXPAT_VERSION >= 0x010200
1781 {"StartDoctypeDeclHandler",
1782 pyxml_SetStartDoctypeDeclHandler,
1783 (xmlhandler)my_StartDoctypeDeclHandler},
1784 {"EndDoctypeDeclHandler",
1785 pyxml_SetEndDoctypeDeclHandler,
1786 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001787#endif
1788#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001789 {"ExternalParsedEntityDeclHandler",
1790 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1791 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1792 {"InternalParsedEntityDeclHandler",
1793 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1794 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001795#endif
1796#if EXPAT_VERSION >= 0x015f00
1797 {"EntityDeclHandler",
1798 (xmlhandlersetter)XML_SetEntityDeclHandler,
1799 (xmlhandler)my_EntityDeclHandler},
1800 {"XmlDeclHandler",
1801 (xmlhandlersetter)XML_SetXmlDeclHandler,
1802 (xmlhandler)my_XmlDeclHandler},
1803 {"ElementDeclHandler",
1804 (xmlhandlersetter)XML_SetElementDeclHandler,
1805 (xmlhandler)my_ElementDeclHandler},
1806 {"AttlistDeclHandler",
1807 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1808 (xmlhandler)my_AttlistDeclHandler},
1809#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001810
Fred Drake0582df92000-07-12 04:49:00 +00001811 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001812};