blob: 39caab99a1144e24090403ed815e6f4d055eb8e9 [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];
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001075PyObject * template_string=NULL;
1076
1077static void
1078init_template_buffer(void)
1079{
1080 int i;
1081 for (i=0;i<256;i++) {
1082 template_buffer[i]=i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001083 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +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{
1092 PyUnicodeObject * _u_string=NULL;
1093 int result=0;
1094 int i;
1095
1096 _u_string=(PyUnicodeObject *) PyUnicode_Decode(template_buffer, 256, name, "replace"); // Yes, supports only 8bit encodings
1097
1098 if (_u_string==NULL) {
1099 return result;
Tim Peters63cb99e2001-02-17 18:12:50 +00001100 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101
1102 for (i=0; i<256; i++) {
1103 Py_UNICODE c = _u_string->str[i] ; // Stupid to access directly, but fast
1104 if (c==Py_UNICODE_REPLACEMENT_CHARACTER) {
1105 info->map[i] = -1;
1106 } else {
1107 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001108 }
1109 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110
1111 info->data = NULL;
1112 info->convert = NULL;
1113 info->release = NULL;
1114 result=1;
1115
1116 Py_DECREF(_u_string);
1117 return result;
1118}
1119
1120#endif
1121
1122static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001123newxmlparseobject(char *encoding, char *namespace_separator)
1124{
1125 int i;
1126 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001127
1128#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001129 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1130 if (self == NULL)
1131 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001132
Fred Drake0582df92000-07-12 04:49:00 +00001133 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001134#else
Fred Drake0582df92000-07-12 04:49:00 +00001135 /* Code for versions 1.6 and later */
1136 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1137 if (self == NULL)
1138 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001139
Fred Drake0582df92000-07-12 04:49:00 +00001140 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001141#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001142 self->ordered_attributes = 0;
1143 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001144 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001145 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001146 if (namespace_separator) {
1147 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1148 }
Fred Drake85d835f2001-02-08 15:39:08 +00001149 else {
Fred Drake0582df92000-07-12 04:49:00 +00001150 self->itself = XML_ParserCreate(encoding);
1151 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001152 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001153 if (self->itself == NULL) {
1154 PyErr_SetString(PyExc_RuntimeError,
1155 "XML_ParserCreate failed");
1156 Py_DECREF(self);
1157 return NULL;
1158 }
1159 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001160#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1161#else
1162 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1163#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001164
Fred Drake0582df92000-07-12 04:49:00 +00001165 for(i = 0; handler_info[i].name != NULL; i++)
1166 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001167
Fred Drake0582df92000-07-12 04:49:00 +00001168 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001169 if (!self->handlers){
1170 Py_DECREF(self);
1171 return PyErr_NoMemory();
1172 }
1173 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001174
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001175 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001176}
1177
1178
1179static void
Fred Drake0582df92000-07-12 04:49:00 +00001180xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001181{
Fred Drake0582df92000-07-12 04:49:00 +00001182 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001184 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001185 XML_ParserFree(self->itself);
1186 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001187
Fred Drake85d835f2001-02-08 15:39:08 +00001188 if (self->handlers != NULL) {
1189 for (i = 0; handler_info[i].name != NULL; i++) {
1190 Py_XDECREF(self->handlers[i]);
1191 }
1192 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001193 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001194#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001195 /* Code for versions before 1.6 */
1196 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001197#else
Fred Drake0582df92000-07-12 04:49:00 +00001198 /* Code for versions 1.6 and later */
1199 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001200#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001201}
1202
Fred Drake0582df92000-07-12 04:49:00 +00001203static int
1204handlername2int(const char *name)
1205{
1206 int i;
1207 for (i=0; handler_info[i].name != NULL; i++) {
1208 if (strcmp(name, handler_info[i].name) == 0) {
1209 return i;
1210 }
1211 }
1212 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001213}
1214
1215static PyObject *
1216xmlparse_getattr(xmlparseobject *self, char *name)
1217{
Fred Drake0582df92000-07-12 04:49:00 +00001218 int handlernum;
1219 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001220 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001221 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001222 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001223 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001224 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001225 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001226 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1227 if (strcmp(name, "ordered_attributes") == 0)
1228 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001230 return PyInt_FromLong((long) self->returns_unicode);
1231 if (strcmp(name, "specified_attributes") == 0)
1232 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001233
Fred Drake0582df92000-07-12 04:49:00 +00001234 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001235
Fred Drake0582df92000-07-12 04:49:00 +00001236 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1237 Py_INCREF(self->handlers[handlernum]);
1238 return self->handlers[handlernum];
1239 }
1240 if (strcmp(name, "__members__") == 0) {
1241 int i;
1242 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001243 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001244 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001245 }
1246 PyList_Append(rc, PyString_FromString("ErrorCode"));
1247 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1248 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1249 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001250 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001251 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001252 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001253
Fred Drake0582df92000-07-12 04:49:00 +00001254 return rc;
1255 }
1256 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001257}
1258
Fred Drake6f987622000-08-25 18:03:30 +00001259static int
1260sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001261{
1262 int handlernum = handlername2int(name);
1263 if (handlernum != -1) {
1264 Py_INCREF(v);
1265 Py_XDECREF(self->handlers[handlernum]);
1266 self->handlers[handlernum] = v;
1267 handler_info[handlernum].setter(self->itself,
1268 handler_info[handlernum].handler);
1269 return 1;
1270 }
1271 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272}
1273
1274static int
Fred Drake6f987622000-08-25 18:03:30 +00001275xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001276{
Fred Drake6f987622000-08-25 18:03:30 +00001277 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001278 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001279 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1280 return -1;
1281 }
Fred Drake85d835f2001-02-08 15:39:08 +00001282 if (strcmp(name, "ordered_attributes") == 0) {
1283 if (PyObject_IsTrue(v))
1284 self->ordered_attributes = 1;
1285 else
1286 self->ordered_attributes = 0;
1287 return 0;
1288 }
Fred Drake6f987622000-08-25 18:03:30 +00001289 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001290 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001291#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001292 PyErr_SetString(PyExc_ValueError,
1293 "Cannot return Unicode strings in Python 1.5");
1294 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001295#else
Fred Drake6f987622000-08-25 18:03:30 +00001296 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001297#endif
Fred Drake6f987622000-08-25 18:03:30 +00001298 }
1299 else
1300 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001301 return 0;
1302 }
1303 if (strcmp(name, "specified_attributes") == 0) {
1304 if (PyObject_IsTrue(v))
1305 self->specified_attributes = 1;
1306 else
1307 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001308 return 0;
1309 }
1310 if (sethandler(self, name, v)) {
1311 return 0;
1312 }
1313 PyErr_SetString(PyExc_AttributeError, name);
1314 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001315}
1316
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001317#ifdef WITH_CYCLE_GC
1318static int
1319xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1320{
1321 int i, err;
1322 for (i = 0; handler_info[i].name != NULL; i++) {
1323 if (!op->handlers[i])
1324 continue;
1325 err = visit(op->handlers[i], arg);
1326 if (err)
1327 return err;
1328 }
1329 return 0;
1330}
1331
1332static int
1333xmlparse_clear(xmlparseobject *op)
1334{
1335 clear_handlers(op, 1);
1336 return 0;
1337}
1338#endif
1339
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001340static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001341"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001342
1343static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001344 PyObject_HEAD_INIT(NULL)
1345 0, /*ob_size*/
1346 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001347 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001348 0, /*tp_itemsize*/
1349 /* methods */
1350 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1351 (printfunc)0, /*tp_print*/
1352 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1353 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1354 (cmpfunc)0, /*tp_compare*/
1355 (reprfunc)0, /*tp_repr*/
1356 0, /*tp_as_number*/
1357 0, /*tp_as_sequence*/
1358 0, /*tp_as_mapping*/
1359 (hashfunc)0, /*tp_hash*/
1360 (ternaryfunc)0, /*tp_call*/
1361 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001362 0, /* tp_getattro */
1363 0, /* tp_setattro */
1364 0, /* tp_as_buffer */
1365 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1366 Xmlparsetype__doc__, /* Documentation string */
1367#ifdef WITH_CYCLE_GC
1368 (traverseproc)xmlparse_traverse, /* tp_traverse */
1369 (inquiry)xmlparse_clear /* tp_clear */
1370#else
1371 0, 0
1372#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001373};
1374
1375/* End of code for xmlparser objects */
1376/* -------------------------------------------------------- */
1377
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001378static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001379"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1380Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001381
1382static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001383pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1384{
1385 char *encoding = NULL;
1386 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001387 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001388
Fred Drake0582df92000-07-12 04:49:00 +00001389 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001390 &encoding, &namespace_separator))
1391 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001392 if (namespace_separator != NULL
1393 && strlen(namespace_separator) != 1) {
1394 PyErr_SetString(PyExc_ValueError,
1395 "namespace_separator must be one character,"
1396 " omitted, or None");
1397 return NULL;
1398 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001399 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001400}
1401
1402static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001403"ErrorString(errno) -> string\n\
1404Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001405
1406static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001407pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001408{
Fred Drake0582df92000-07-12 04:49:00 +00001409 long code = 0;
1410
1411 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1412 return NULL;
1413 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001414}
1415
1416/* List of methods defined in the module */
1417
1418static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001419 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1420 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1421 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1422 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423
Fred Drake0582df92000-07-12 04:49:00 +00001424 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001425};
1426
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001427/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001428
1429static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001430"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001432/* Initialization function for the module */
1433
Fred Drake93adb692000-09-23 04:55:48 +00001434void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001435
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001436#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001437
1438/* 1.5 compatibility: PyModule_AddObject */
1439static int
1440PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1441{
1442 PyObject *dict;
1443 if (!PyModule_Check(m) || o == NULL)
1444 return -1;
1445 dict = PyModule_GetDict(m);
1446 if (dict == NULL)
1447 return -1;
1448 if (PyDict_SetItemString(dict, name, o))
1449 return -1;
1450 Py_DECREF(o);
1451 return 0;
1452}
1453
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001454int
1455PyModule_AddIntConstant(PyObject *m, char *name, long value)
1456{
1457 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1458}
1459
Fred Drakea77254a2000-09-29 19:23:29 +00001460static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001461PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1462{
1463 return PyModule_AddObject(m, name, PyString_FromString(value));
1464}
1465
1466#endif
1467
Fred Drake6f987622000-08-25 18:03:30 +00001468DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001469initpyexpat(void)
1470{
1471 PyObject *m, *d;
1472 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001473 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001474 PyObject *errors_module;
1475 PyObject *modelmod_name;
1476 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001477 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001478
Fred Drake6f987622000-08-25 18:03:30 +00001479 if (errmod_name == NULL)
1480 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001481 modelmod_name = PyString_FromString("pyexpat.model");
1482 if (modelmod_name == NULL)
1483 return;
Fred Drake6f987622000-08-25 18:03:30 +00001484
Fred Drake0582df92000-07-12 04:49:00 +00001485 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001486
Fred Drake0582df92000-07-12 04:49:00 +00001487 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001488 m = Py_InitModule3("pyexpat", pyexpat_methods,
1489 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001490
Fred Drake0582df92000-07-12 04:49:00 +00001491 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001492 if (ErrorObject == NULL) {
1493 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001494 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001495 if (ErrorObject == NULL)
1496 return;
1497 }
1498 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001499 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001500 Py_INCREF(ErrorObject);
1501 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001502 Py_INCREF(&Xmlparsetype);
1503 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001504
Fred Drake93adb692000-09-23 04:55:48 +00001505 PyModule_AddObject(m, "__version__",
1506 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake85d835f2001-02-08 15:39:08 +00001507#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001508 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1509 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001510 {
1511 XML_Expat_Version info = XML_ExpatVersionInfo();
1512 PyModule_AddObject(m, "version_info",
1513 Py_BuildValue("(iii)", info.major,
1514 info.minor, info.micro));
1515 }
Fred Drake738293d2000-12-21 17:25:07 +00001516#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001517#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1518#else
1519 init_template_buffer();
1520#endif
Fred Drake0582df92000-07-12 04:49:00 +00001521 /* XXX When Expat supports some way of figuring out how it was
1522 compiled, this should check and set native_encoding
1523 appropriately.
1524 */
Fred Drake93adb692000-09-23 04:55:48 +00001525 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001526
Fred Drake85d835f2001-02-08 15:39:08 +00001527 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001528 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001529 errors_module = PyDict_GetItem(d, errmod_name);
1530 if (errors_module == NULL) {
1531 errors_module = PyModule_New("pyexpat.errors");
1532 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001533 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001534 /* gives away the reference to errors_module */
1535 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001536 }
1537 }
Fred Drake6f987622000-08-25 18:03:30 +00001538 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001539 model_module = PyDict_GetItem(d, modelmod_name);
1540 if (model_module == NULL) {
1541 model_module = PyModule_New("pyexpat.model");
1542 if (model_module != NULL) {
1543 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1544 /* gives away the reference to model_module */
1545 PyModule_AddObject(m, "model", model_module);
1546 }
1547 }
1548 Py_DECREF(modelmod_name);
1549 if (errors_module == NULL || model_module == NULL)
1550 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001551 return;
1552
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001553#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001554 PyModule_AddStringConstant(errors_module, #name, \
1555 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001556
Fred Drake0582df92000-07-12 04:49:00 +00001557 MYCONST(XML_ERROR_NO_MEMORY);
1558 MYCONST(XML_ERROR_SYNTAX);
1559 MYCONST(XML_ERROR_NO_ELEMENTS);
1560 MYCONST(XML_ERROR_INVALID_TOKEN);
1561 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1562 MYCONST(XML_ERROR_PARTIAL_CHAR);
1563 MYCONST(XML_ERROR_TAG_MISMATCH);
1564 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1565 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1566 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1567 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1568 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1569 MYCONST(XML_ERROR_ASYNC_ENTITY);
1570 MYCONST(XML_ERROR_BAD_CHAR_REF);
1571 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1572 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1573 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1574 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1575 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001576 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1577 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1578 MYCONST(XML_ERROR_NOT_STANDALONE);
1579
Fred Drake85d835f2001-02-08 15:39:08 +00001580 PyModule_AddStringConstant(errors_module, "__doc__",
1581 "Constants used to describe error conditions.");
1582
Fred Drake93adb692000-09-23 04:55:48 +00001583#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001584
1585#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001586#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001587 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1588 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1589 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001590#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001591#endif
1592
Fred Drake85d835f2001-02-08 15:39:08 +00001593#if EXPAT_VERSION >= 0x015f00
1594#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1595 PyModule_AddStringConstant(model_module, "__doc__",
1596 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001597
Fred Drake85d835f2001-02-08 15:39:08 +00001598 MYCONST(XML_CTYPE_EMPTY);
1599 MYCONST(XML_CTYPE_ANY);
1600 MYCONST(XML_CTYPE_MIXED);
1601 MYCONST(XML_CTYPE_NAME);
1602 MYCONST(XML_CTYPE_CHOICE);
1603 MYCONST(XML_CTYPE_SEQ);
1604
1605 MYCONST(XML_CQUANT_NONE);
1606 MYCONST(XML_CQUANT_OPT);
1607 MYCONST(XML_CQUANT_REP);
1608 MYCONST(XML_CQUANT_PLUS);
1609#undef MYCONST
1610#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001611}
1612
Fred Drake6f987622000-08-25 18:03:30 +00001613static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001614clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001615{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001616 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001617
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001618 for (; handler_info[i].name!=NULL; i++) {
1619 if (decref){
1620 Py_XDECREF(self->handlers[i]);
1621 }
1622 self->handlers[i]=NULL;
1623 handler_info[i].setter(self->itself, NULL);
1624 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001625}
1626
Fred Drake6f987622000-08-25 18:03:30 +00001627typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001628
Fred Drake6f987622000-08-25 18:03:30 +00001629static void
1630pyxml_UpdatePairedHandlers(xmlparseobject *self,
1631 int startHandler,
1632 int endHandler,
1633 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001634{
1635 void *start_handler=NULL;
1636 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001637
Fred Drake0582df92000-07-12 04:49:00 +00001638 if (self->handlers[startHandler]
1639 && self->handlers[endHandler]!=Py_None) {
1640 start_handler=handler_info[startHandler].handler;
1641 }
1642 if (self->handlers[EndElement]
1643 && self->handlers[EndElement] !=Py_None) {
1644 end_handler=handler_info[endHandler].handler;
1645 }
1646 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001647}
1648
Fred Drake6f987622000-08-25 18:03:30 +00001649static void
1650pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001651{
1652 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1653 StartElement, EndElement,
1654 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001655}
1656
Fred Drake6f987622000-08-25 18:03:30 +00001657static void
1658pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001659{
1660 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1661 StartElement, EndElement,
1662 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001663}
1664
Fred Drake6f987622000-08-25 18:03:30 +00001665static void
1666pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001667{
1668 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1669 StartNamespaceDecl, EndNamespaceDecl,
1670 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001671}
1672
Fred Drake6f987622000-08-25 18:03:30 +00001673static void
1674pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001675{
1676 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1677 StartNamespaceDecl, EndNamespaceDecl,
1678 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001679}
1680
Fred Drake6f987622000-08-25 18:03:30 +00001681static void
1682pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001683{
1684 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1685 StartCdataSection, EndCdataSection,
1686 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001687}
1688
Fred Drake6f987622000-08-25 18:03:30 +00001689static void
1690pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001691{
1692 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1693 StartCdataSection, EndCdataSection,
1694 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001695}
1696
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001697#if EXPAT_VERSION >= 0x010200
1698
1699static void
1700pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1701{
1702 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1703 StartDoctypeDecl, EndDoctypeDecl,
1704 (pairsetter)XML_SetDoctypeDeclHandler);
1705}
1706
1707static void
1708pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1709{
1710 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1711 StartDoctypeDecl, EndDoctypeDecl,
1712 (pairsetter)XML_SetDoctypeDeclHandler);
1713}
1714
1715#endif
1716
Fred Drake0582df92000-07-12 04:49:00 +00001717statichere struct HandlerInfo handler_info[] = {
1718 {"StartElementHandler",
1719 pyxml_SetStartElementHandler,
1720 (xmlhandler)my_StartElementHandler},
1721 {"EndElementHandler",
1722 pyxml_SetEndElementHandler,
1723 (xmlhandler)my_EndElementHandler},
1724 {"ProcessingInstructionHandler",
1725 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1726 (xmlhandler)my_ProcessingInstructionHandler},
1727 {"CharacterDataHandler",
1728 (xmlhandlersetter)XML_SetCharacterDataHandler,
1729 (xmlhandler)my_CharacterDataHandler},
1730 {"UnparsedEntityDeclHandler",
1731 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1732 (xmlhandler)my_UnparsedEntityDeclHandler },
1733 {"NotationDeclHandler",
1734 (xmlhandlersetter)XML_SetNotationDeclHandler,
1735 (xmlhandler)my_NotationDeclHandler },
1736 {"StartNamespaceDeclHandler",
1737 pyxml_SetStartNamespaceDeclHandler,
1738 (xmlhandler)my_StartNamespaceDeclHandler },
1739 {"EndNamespaceDeclHandler",
1740 pyxml_SetEndNamespaceDeclHandler,
1741 (xmlhandler)my_EndNamespaceDeclHandler },
1742 {"CommentHandler",
1743 (xmlhandlersetter)XML_SetCommentHandler,
1744 (xmlhandler)my_CommentHandler},
1745 {"StartCdataSectionHandler",
1746 pyxml_SetStartCdataSection,
1747 (xmlhandler)my_StartCdataSectionHandler},
1748 {"EndCdataSectionHandler",
1749 pyxml_SetEndCdataSection,
1750 (xmlhandler)my_EndCdataSectionHandler},
1751 {"DefaultHandler",
1752 (xmlhandlersetter)XML_SetDefaultHandler,
1753 (xmlhandler)my_DefaultHandler},
1754 {"DefaultHandlerExpand",
1755 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1756 (xmlhandler)my_DefaultHandlerExpandHandler},
1757 {"NotStandaloneHandler",
1758 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1759 (xmlhandler)my_NotStandaloneHandler},
1760 {"ExternalEntityRefHandler",
1761 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1762 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001763#if EXPAT_VERSION >= 0x010200
1764 {"StartDoctypeDeclHandler",
1765 pyxml_SetStartDoctypeDeclHandler,
1766 (xmlhandler)my_StartDoctypeDeclHandler},
1767 {"EndDoctypeDeclHandler",
1768 pyxml_SetEndDoctypeDeclHandler,
1769 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001770#endif
1771#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001772 {"ExternalParsedEntityDeclHandler",
1773 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1774 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1775 {"InternalParsedEntityDeclHandler",
1776 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1777 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001778#endif
1779#if EXPAT_VERSION >= 0x015f00
1780 {"EntityDeclHandler",
1781 (xmlhandlersetter)XML_SetEntityDeclHandler,
1782 (xmlhandler)my_EntityDeclHandler},
1783 {"XmlDeclHandler",
1784 (xmlhandlersetter)XML_SetXmlDeclHandler,
1785 (xmlhandler)my_XmlDeclHandler},
1786 {"ElementDeclHandler",
1787 (xmlhandlersetter)XML_SetElementDeclHandler,
1788 (xmlhandler)my_ElementDeclHandler},
1789 {"AttlistDeclHandler",
1790 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1791 (xmlhandler)my_AttlistDeclHandler},
1792#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001793
Fred Drake0582df92000-07-12 04:49:00 +00001794 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001795};