blob: 93107baba1ba80ee4ab73f55ca048b5899c05f3e [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00004#include "compile.h"
5#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00006#ifdef HAVE_EXPAT_H
7#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00008#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +00009#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
10 + 0x100 * XML_MINOR_VERSION \
11 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000012#else
Fred Drake85d835f2001-02-08 15:39:08 +000013/* Assume the oldest Expat that used expat.h and did not have version info */
14#define EXPAT_VERSION 0x015f00
15#endif
16#else /* !defined(HAVE_EXPAT_H) */
17#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000018/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000019#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000020#define EXPAT_VERSION 0x010100
21#endif
Fred Drake85d835f2001-02-08 15:39:08 +000022#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000023
24#ifndef PyGC_HEAD_SIZE
25#define PyGC_HEAD_SIZE 0
26#define PyObject_GC_Init(x)
27#define PyObject_GC_Fini(m)
28#define Py_TPFLAGS_GC 0
29#endif
30
Martin v. Löwis339d0f72001-08-17 18:39:25 +000031#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
32/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
33#define Py_USING_UNICODE
34#endif
35
Fred Drake0582df92000-07-12 04:49:00 +000036enum HandlerTypes {
37 StartElement,
38 EndElement,
39 ProcessingInstruction,
40 CharacterData,
41 UnparsedEntityDecl,
42 NotationDecl,
43 StartNamespaceDecl,
44 EndNamespaceDecl,
45 Comment,
46 StartCdataSection,
47 EndCdataSection,
48 Default,
49 DefaultHandlerExpand,
50 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000051 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000052#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000053 StartDoctypeDecl,
54 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000055#endif
56#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000057 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000058 InternalParsedEntityDecl,
59#endif
60#if EXPAT_VERSION >= 0x015f00
61 EntityDecl,
62 XmlDecl,
63 ElementDecl,
64 AttlistDecl,
65#endif
66 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000067};
68
69static PyObject *ErrorObject;
70
71/* ----------------------------------------------------- */
72
73/* Declarations for objects of type xmlparser */
74
75typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000076 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000077
Fred Drake0582df92000-07-12 04:49:00 +000078 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000079 int returns_unicode; /* True if Unicode strings are returned;
80 if false, UTF-8 strings are returned */
81 int ordered_attributes; /* Return attributes as a list. */
82 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000083 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000084 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000085} xmlparseobject;
86
87staticforward PyTypeObject Xmlparsetype;
88
Fred Drake6f987622000-08-25 18:03:30 +000089typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090typedef void* xmlhandler;
91
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000092struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000093 const char *name;
94 xmlhandlersetter setter;
95 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000096 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000097};
98
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000099staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000100
Fred Drakebd6101c2001-02-14 18:29:45 +0000101/* Set an integer attribute on the error object; return true on success,
102 * false on an exception.
103 */
104static int
105set_error_attr(PyObject *err, char *name, int value)
106{
107 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000108
Fred Drakebd6101c2001-02-14 18:29:45 +0000109 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
110 Py_DECREF(v);
111 return 0;
112 }
113 return 1;
114}
115
116/* Build and set an Expat exception, including positioning
117 * information. Always returns NULL.
118 */
Fred Drake85d835f2001-02-08 15:39:08 +0000119static PyObject *
120set_error(xmlparseobject *self)
121{
122 PyObject *err;
123 char buffer[256];
124 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 int lineno = XML_GetErrorLineNumber(parser);
126 int column = XML_GetErrorColumnNumber(parser);
127 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000128
129 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000130 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000131 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000132 if ( err != NULL
133 && set_error_attr(err, "code", code)
134 && set_error_attr(err, "offset", column)
135 && set_error_attr(err, "lineno", lineno)) {
136 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000137 }
138 return NULL;
139}
140
141
142#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000143/* Convert an array of attributes and their values into a Python dict */
144
Fred Drake0582df92000-07-12 04:49:00 +0000145static PyObject *
146conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000147{
Fred Drake0582df92000-07-12 04:49:00 +0000148 PyObject *attrs_obj = NULL;
149 XML_Char **attrs_p, **attrs_k = NULL;
150 int attrs_len;
151 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000152
Fred Drake0582df92000-07-12 04:49:00 +0000153 if ((attrs_obj = PyDict_New()) == NULL)
154 goto finally;
155 for (attrs_len = 0, attrs_p = atts;
156 *attrs_p;
157 attrs_p++, attrs_len++) {
158 if (attrs_len % 2) {
159 rv = PyString_FromString(*attrs_p);
160 if (!rv) {
161 Py_DECREF(attrs_obj);
162 attrs_obj = NULL;
163 goto finally;
164 }
165 if (PyDict_SetItemString(attrs_obj,
166 (char*)*attrs_k, rv) < 0) {
167 Py_DECREF(attrs_obj);
168 attrs_obj = NULL;
169 goto finally;
170 }
171 Py_DECREF(rv);
172 }
173 else
174 attrs_k = attrs_p;
175 }
176 finally:
177 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000178}
Fred Drake85d835f2001-02-08 15:39:08 +0000179#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000180
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000181#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000182#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000183static PyObject *
184conv_atts_using_unicode(XML_Char **atts)
185{
Fred Drakeca1f4262000-09-21 20:10:23 +0000186 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000187 XML_Char **attrs_p, **attrs_k = NULL;
188 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000189
Fred Drake0582df92000-07-12 04:49:00 +0000190 if ((attrs_obj = PyDict_New()) == NULL)
191 goto finally;
192 for (attrs_len = 0, attrs_p = atts;
193 *attrs_p;
194 attrs_p++, attrs_len++) {
195 if (attrs_len % 2) {
196 PyObject *attr_str, *value_str;
197 const char *p = (const char *) (*attrs_k);
198 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
199 if (!attr_str) {
200 Py_DECREF(attrs_obj);
201 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000202 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000203 }
204 p = (const char *) *attrs_p;
205 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
206 if (!value_str) {
207 Py_DECREF(attrs_obj);
208 Py_DECREF(attr_str);
209 attrs_obj = NULL;
210 goto finally;
211 }
212 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
213 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000214 Py_DECREF(attr_str);
215 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000216 attrs_obj = NULL;
217 goto finally;
218 }
219 Py_DECREF(attr_str);
220 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000221 }
Fred Drake0582df92000-07-12 04:49:00 +0000222 else
223 attrs_k = attrs_p;
224 }
225 finally:
226 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000227}
Fred Drake85d835f2001-02-08 15:39:08 +0000228#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000229
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000230/* Convert a string of XML_Chars into a Unicode string.
231 Returns None if str is a null pointer. */
232
Fred Drake0582df92000-07-12 04:49:00 +0000233static PyObject *
234conv_string_to_unicode(XML_Char *str)
235{
236 /* XXX currently this code assumes that XML_Char is 8-bit,
237 and hence in UTF-8. */
238 /* UTF-8 from Expat, Unicode desired */
239 if (str == NULL) {
240 Py_INCREF(Py_None);
241 return Py_None;
242 }
243 return PyUnicode_DecodeUTF8((const char *)str,
244 strlen((const char *)str),
245 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000246}
247
Fred Drake0582df92000-07-12 04:49:00 +0000248static PyObject *
249conv_string_len_to_unicode(const XML_Char *str, int len)
250{
251 /* XXX currently this code assumes that XML_Char is 8-bit,
252 and hence in UTF-8. */
253 /* UTF-8 from Expat, Unicode desired */
254 if (str == NULL) {
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
Fred Drake6f987622000-08-25 18:03:30 +0000258 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000259}
260#endif
261
262/* Convert a string of XML_Chars into an 8-bit Python string.
263 Returns None if str is a null pointer. */
264
Fred Drake6f987622000-08-25 18:03:30 +0000265static PyObject *
266conv_string_to_utf8(XML_Char *str)
267{
268 /* XXX currently this code assumes that XML_Char is 8-bit,
269 and hence in UTF-8. */
270 /* UTF-8 from Expat, UTF-8 desired */
271 if (str == NULL) {
272 Py_INCREF(Py_None);
273 return Py_None;
274 }
275 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000276}
277
Fred Drake6f987622000-08-25 18:03:30 +0000278static PyObject *
279conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000280{
Fred Drake6f987622000-08-25 18:03:30 +0000281 /* XXX currently this code assumes that XML_Char is 8-bit,
282 and hence in UTF-8. */
283 /* UTF-8 from Expat, UTF-8 desired */
284 if (str == NULL) {
285 Py_INCREF(Py_None);
286 return Py_None;
287 }
288 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000289}
290
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000291/* Callback routines */
292
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000293static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000294
Fred Drake6f987622000-08-25 18:03:30 +0000295static void
296flag_error(xmlparseobject *self)
297{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000298 clear_handlers(self, 1);
299}
300
301static PyCodeObject*
302getcode(enum HandlerTypes slot, char* func_name, int lineno)
303{
Fred Drakebd6101c2001-02-14 18:29:45 +0000304 PyObject *code = NULL;
305 PyObject *name = NULL;
306 PyObject *nulltuple = NULL;
307 PyObject *filename = NULL;
308
309 if (handler_info[slot].tb_code == NULL) {
310 code = PyString_FromString("");
311 if (code == NULL)
312 goto failed;
313 name = PyString_FromString(func_name);
314 if (name == NULL)
315 goto failed;
316 nulltuple = PyTuple_New(0);
317 if (nulltuple == NULL)
318 goto failed;
319 filename = PyString_FromString(__FILE__);
320 handler_info[slot].tb_code =
321 PyCode_New(0, /* argcount */
322 0, /* nlocals */
323 0, /* stacksize */
324 0, /* flags */
325 code, /* code */
326 nulltuple, /* consts */
327 nulltuple, /* names */
328 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000329#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000330 nulltuple, /* freevars */
331 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000332#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000333 filename, /* filename */
334 name, /* name */
335 lineno, /* firstlineno */
336 code /* lnotab */
337 );
338 if (handler_info[slot].tb_code == NULL)
339 goto failed;
340 Py_DECREF(code);
341 Py_DECREF(nulltuple);
342 Py_DECREF(filename);
343 Py_DECREF(name);
344 }
345 return handler_info[slot].tb_code;
346 failed:
347 Py_XDECREF(code);
348 Py_XDECREF(name);
349 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000350}
351
352static PyObject*
353call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
354{
Fred Drakebd6101c2001-02-14 18:29:45 +0000355 PyThreadState *tstate = PyThreadState_GET();
356 PyFrameObject *f;
357 PyObject *res;
358
359 if (c == NULL)
360 return NULL;
361 f = PyFrame_New(
362 tstate, /*back*/
363 c, /*code*/
364 tstate->frame->f_globals, /*globals*/
365 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000366 );
367 if (f == NULL)
368 return NULL;
369 tstate->frame = f;
370 res = PyEval_CallObject(func, args);
371 if (res == NULL && tstate->curexc_traceback == NULL)
372 PyTraceBack_Here(f);
373 tstate->frame = f->f_back;
374 Py_DECREF(f);
375 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000376}
377
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000378#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000379#define STRING_CONV_FUNC conv_string_to_utf8
380#else
381/* Python 1.6 and later versions */
382#define STRING_CONV_FUNC (self->returns_unicode \
383 ? conv_string_to_unicode : conv_string_to_utf8)
384#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000385
Fred Drake85d835f2001-02-08 15:39:08 +0000386static void
387my_StartElementHandler(void *userData,
388 const XML_Char *name, const XML_Char **atts)
389{
390 xmlparseobject *self = (xmlparseobject *)userData;
391
392 if (self->handlers[StartElement]
393 && self->handlers[StartElement] != Py_None) {
394 PyObject *container, *rv, *args;
395 int i, max;
396
397 /* Set max to the number of slots filled in atts[]; max/2 is
398 * the number of attributes we need to process.
399 */
400 if (self->specified_attributes) {
401 max = XML_GetSpecifiedAttributeCount(self->itself);
402 }
403 else {
404 max = 0;
405 while (atts[max] != NULL)
406 max += 2;
407 }
408 /* Build the container. */
409 if (self->ordered_attributes)
410 container = PyList_New(max);
411 else
412 container = PyDict_New();
413 if (container == NULL) {
414 flag_error(self);
415 return;
416 }
417 for (i = 0; i < max; i += 2) {
418 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
419 PyObject *v;
420 if (n == NULL) {
421 flag_error(self);
422 Py_DECREF(container);
423 return;
424 }
425 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
426 if (v == NULL) {
427 flag_error(self);
428 Py_DECREF(container);
429 Py_DECREF(n);
430 return;
431 }
432 if (self->ordered_attributes) {
433 PyList_SET_ITEM(container, i, n);
434 PyList_SET_ITEM(container, i+1, v);
435 }
436 else if (PyDict_SetItem(container, n, v)) {
437 flag_error(self);
438 Py_DECREF(n);
439 Py_DECREF(v);
440 return;
441 }
442 else {
443 Py_DECREF(n);
444 Py_DECREF(v);
445 }
446 }
447 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
448 if (args == NULL) {
449 Py_DECREF(container);
450 return;
451 }
452 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000453 self->in_callback = 1;
454 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000455 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000456 self->in_callback = 0;
457 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000458 if (rv == NULL) {
459 flag_error(self);
460 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000461 }
Fred Drake85d835f2001-02-08 15:39:08 +0000462 Py_DECREF(rv);
463 }
464}
465
466#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
467 RETURN, GETUSERDATA) \
468static RC \
469my_##NAME##Handler PARAMS {\
470 xmlparseobject *self = GETUSERDATA ; \
471 PyObject *args = NULL; \
472 PyObject *rv = NULL; \
473 INIT \
474\
475 if (self->handlers[NAME] \
476 && self->handlers[NAME] != Py_None) { \
477 args = Py_BuildValue PARAM_FORMAT ;\
478 if (!args) \
479 return RETURN; \
Fred Drakebd6101c2001-02-14 18:29:45 +0000480 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000481 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
482 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000483 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000484 Py_DECREF(args); \
485 if (rv == NULL) { \
486 flag_error(self); \
487 return RETURN; \
488 } \
489 CONVERSION \
490 Py_DECREF(rv); \
491 } \
492 return RETURN; \
493}
494
Fred Drake6f987622000-08-25 18:03:30 +0000495#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
496 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
497 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000498
Fred Drake6f987622000-08-25 18:03:30 +0000499#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
500 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
501 rc = PyInt_AsLong(rv);, rc, \
502 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000503
Fred Drake6f987622000-08-25 18:03:30 +0000504VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000505 (void *userData, const XML_Char *name),
506 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000507
Fred Drake6f987622000-08-25 18:03:30 +0000508VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000509 (void *userData,
510 const XML_Char *target,
511 const XML_Char *data),
512 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000513
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000514#ifndef Py_USING_UNICODE
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)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000518#else
Fred Drake6f987622000-08-25 18:03:30 +0000519VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000520 (void *userData, const XML_Char *data, int len),
521 ("(N)", (self->returns_unicode
522 ? conv_string_len_to_unicode(data,len)
523 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000524#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000525
Fred Drake6f987622000-08-25 18:03:30 +0000526VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000527 (void *userData,
528 const XML_Char *entityName,
529 const XML_Char *base,
530 const XML_Char *systemId,
531 const XML_Char *publicId,
532 const XML_Char *notationName),
533 ("(O&O&O&O&O&)",
534 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
535 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
536 STRING_CONV_FUNC,notationName))
537
538#if EXPAT_VERSION >= 0x015f00
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000539#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000540VOID_HANDLER(EntityDecl,
541 (void *userData,
542 const XML_Char *entityName,
543 int is_parameter_entity,
544 const XML_Char *value,
545 int value_length,
546 const XML_Char *base,
547 const XML_Char *systemId,
548 const XML_Char *publicId,
549 const XML_Char *notationName),
550 ("O&iNO&O&O&O&",
551 STRING_CONV_FUNC,entityName, is_parameter_entity,
552 conv_string_len_to_utf8(value, value_length),
553 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
554 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
555#else
556VOID_HANDLER(EntityDecl,
557 (void *userData,
558 const XML_Char *entityName,
559 int is_parameter_entity,
560 const XML_Char *value,
561 int value_length,
562 const XML_Char *base,
563 const XML_Char *systemId,
564 const XML_Char *publicId,
565 const XML_Char *notationName),
566 ("O&iNO&O&O&O&",
567 STRING_CONV_FUNC,entityName, is_parameter_entity,
568 (self->returns_unicode
569 ? conv_string_len_to_unicode(value, value_length)
570 : conv_string_len_to_utf8(value, value_length)),
571 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
572 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
573#endif
574
575VOID_HANDLER(XmlDecl,
576 (void *userData,
577 const XML_Char *version,
578 const XML_Char *encoding,
579 int standalone),
580 ("(O&O&i)",
581 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
582 standalone))
583
584static PyObject *
585conv_content_model(XML_Content * const model,
586 PyObject *(*conv_string)(XML_Char *))
587{
588 PyObject *result = NULL;
589 PyObject *children = PyTuple_New(model->numchildren);
590 int i;
591
592 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000593 assert(model->numchildren < INT_MAX);
594 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000595 PyObject *child = conv_content_model(&model->children[i],
596 conv_string);
597 if (child == NULL) {
598 Py_XDECREF(children);
599 return NULL;
600 }
601 PyTuple_SET_ITEM(children, i, child);
602 }
603 result = Py_BuildValue("(iiO&N)",
604 model->type, model->quant,
605 conv_string,model->name, children);
606 }
607 return result;
608}
609
610static PyObject *
611conv_content_model_utf8(XML_Content * const model)
612{
613 return conv_content_model(model, conv_string_to_utf8);
614}
615
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000616#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000617static PyObject *
618conv_content_model_unicode(XML_Content * const model)
619{
620 return conv_content_model(model, conv_string_to_unicode);
621}
622
623VOID_HANDLER(ElementDecl,
624 (void *userData,
625 const XML_Char *name,
626 XML_Content *model),
627 ("O&O&",
628 STRING_CONV_FUNC,name,
629 (self->returns_unicode ? conv_content_model_unicode
630 : conv_content_model_utf8),model))
631#else
632VOID_HANDLER(ElementDecl,
633 (void *userData,
634 const XML_Char *name,
635 XML_Content *model),
636 ("O&O&",
637 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
638#endif
639
640VOID_HANDLER(AttlistDecl,
641 (void *userData,
642 const XML_Char *elname,
643 const XML_Char *attname,
644 const XML_Char *att_type,
645 const XML_Char *dflt,
646 int isrequired),
647 ("(O&O&O&O&i)",
648 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
649 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
650 isrequired))
651#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000652
Fred Drake6f987622000-08-25 18:03:30 +0000653VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000654 (void *userData,
655 const XML_Char *notationName,
656 const XML_Char *base,
657 const XML_Char *systemId,
658 const XML_Char *publicId),
659 ("(O&O&O&O&)",
660 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
661 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000662
Fred Drake6f987622000-08-25 18:03:30 +0000663VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000664 (void *userData,
665 const XML_Char *prefix,
666 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000667 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000668
Fred Drake6f987622000-08-25 18:03:30 +0000669VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000670 (void *userData,
671 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000672 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000673
Fred Drake6f987622000-08-25 18:03:30 +0000674VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000675 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000676 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000677
Fred Drake6f987622000-08-25 18:03:30 +0000678VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000679 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000680 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000681
Fred Drake6f987622000-08-25 18:03:30 +0000682VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000683 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000684 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000686#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000687VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000688 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000689 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000690
Fred Drake6f987622000-08-25 18:03:30 +0000691VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000692 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000693 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694#else
Fred Drake6f987622000-08-25 18:03:30 +0000695VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000696 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000697 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000698 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000699 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000700
Fred Drake6f987622000-08-25 18:03:30 +0000701VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000702 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000703 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000705 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000706#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000707
Fred Drake6f987622000-08-25 18:03:30 +0000708INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000709 (void *userData),
710 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000711
Fred Drake6f987622000-08-25 18:03:30 +0000712RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000713 (XML_Parser parser,
714 const XML_Char *context,
715 const XML_Char *base,
716 const XML_Char *systemId,
717 const XML_Char *publicId),
718 int rc=0;,
719 ("(O&O&O&O&)",
720 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000721 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
722 rc = PyInt_AsLong(rv);, rc,
723 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000724
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000725/* XXX UnknownEncodingHandler */
726
Fred Drake85d835f2001-02-08 15:39:08 +0000727#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000728VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000729 (void *userData, const XML_Char *doctypeName),
730 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
731 Py_None, Py_None, -1))
732#elif EXPAT_VERSION >= 0x015f00
733VOID_HANDLER(StartDoctypeDecl,
734 (void *userData, const XML_Char *doctypeName,
735 const XML_Char *sysid, const XML_Char *pubid,
736 int has_internal_subset),
737 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
738 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
739 has_internal_subset))
740#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000741
Fred Drake85d835f2001-02-08 15:39:08 +0000742#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000743VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000744#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000745
Fred Drake85d835f2001-02-08 15:39:08 +0000746#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000747VOID_HANDLER(ExternalParsedEntityDecl,
748 (void *userData, const XML_Char *entityName,
749 const XML_Char *base, const XML_Char *systemId,
750 const XML_Char *publicId),
751 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
752 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
753 STRING_CONV_FUNC, publicId))
754
755VOID_HANDLER(InternalParsedEntityDecl,
756 (void *userData, const XML_Char *entityName,
757 const XML_Char *replacementText, int replacementTextLength),
758 ("(O&O&i)", STRING_CONV_FUNC, entityName,
759 STRING_CONV_FUNC, replacementText, replacementTextLength))
760
Fred Drake85d835f2001-02-08 15:39:08 +0000761#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000763/* ---------------------------------------------------------------- */
764
765static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000766"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000767Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000768
769static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000770xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000771{
Fred Drake0582df92000-07-12 04:49:00 +0000772 char *s;
773 int slen;
774 int isFinal = 0;
775 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776
Fred Drake0582df92000-07-12 04:49:00 +0000777 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
778 return NULL;
779 rv = XML_Parse(self->itself, s, slen, isFinal);
780 if (PyErr_Occurred()) {
781 return NULL;
782 }
783 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000784 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000785 }
786 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000787}
788
Fred Drakeca1f4262000-09-21 20:10:23 +0000789/* File reading copied from cPickle */
790
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000791#define BUF_SIZE 2048
792
Fred Drake0582df92000-07-12 04:49:00 +0000793static int
794readinst(char *buf, int buf_size, PyObject *meth)
795{
796 PyObject *arg = NULL;
797 PyObject *bytes = NULL;
798 PyObject *str = NULL;
799 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000800
Fred Drake676940b2000-09-22 15:21:31 +0000801 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000802 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000803
Fred Drakeca1f4262000-09-21 20:10:23 +0000804 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000805 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000806
Tim Peters954eef72000-09-22 06:01:11 +0000807 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000808
Fred Drakeca1f4262000-09-21 20:10:23 +0000809 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000810 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000811
Fred Drake0582df92000-07-12 04:49:00 +0000812 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000813 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000814 PyErr_Format(PyExc_TypeError,
815 "read() did not return a string object (type=%.400s)",
816 str->ob_type->tp_name);
817 goto finally;
818 }
819 len = PyString_GET_SIZE(str);
820 if (len > buf_size) {
821 PyErr_Format(PyExc_ValueError,
822 "read() returned too much data: "
823 "%i bytes requested, %i returned",
824 buf_size, len);
825 Py_DECREF(str);
826 goto finally;
827 }
828 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829finally:
Fred Drake0582df92000-07-12 04:49:00 +0000830 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000831 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000832 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833}
834
835static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000836"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000837Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838
839static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000840xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841{
Fred Drake0582df92000-07-12 04:49:00 +0000842 int rv = 1;
843 PyObject *f;
844 FILE *fp;
845 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000846
Fred Drake0582df92000-07-12 04:49:00 +0000847 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
848 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000849
Fred Drake0582df92000-07-12 04:49:00 +0000850 if (PyFile_Check(f)) {
851 fp = PyFile_AsFile(f);
852 }
853 else{
854 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000855 readmethod = PyObject_GetAttrString(f, "read");
856 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000857 PyErr_Clear();
858 PyErr_SetString(PyExc_TypeError,
859 "argument must have 'read' attribute");
860 return 0;
861 }
862 }
863 for (;;) {
864 int bytes_read;
865 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
866 if (buf == NULL)
867 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000868
Fred Drake0582df92000-07-12 04:49:00 +0000869 if (fp) {
870 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
871 if (bytes_read < 0) {
872 PyErr_SetFromErrno(PyExc_IOError);
873 return NULL;
874 }
875 }
876 else {
877 bytes_read = readinst(buf, BUF_SIZE, readmethod);
878 if (bytes_read < 0)
879 return NULL;
880 }
881 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
882 if (PyErr_Occurred())
883 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000884
Fred Drake0582df92000-07-12 04:49:00 +0000885 if (!rv || bytes_read == 0)
886 break;
887 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000888 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000889 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000890 }
Fred Drake0582df92000-07-12 04:49:00 +0000891 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892}
893
894static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000895"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000896Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000897
898static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000899xmlparse_SetBase(xmlparseobject *self, PyObject *args)
900{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000901 char *base;
902
Fred Drake0582df92000-07-12 04:49:00 +0000903 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000904 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000905 if (!XML_SetBase(self->itself, base)) {
906 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000908 Py_INCREF(Py_None);
909 return Py_None;
910}
911
912static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000913"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000914Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915
916static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000917xmlparse_GetBase(xmlparseobject *self, PyObject *args)
918{
919 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000921
Fred Drake0582df92000-07-12 04:49:00 +0000922 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000923}
924
Fred Drakebd6101c2001-02-14 18:29:45 +0000925#if EXPAT_VERSION >= 0x015f00
926static char xmlparse_GetInputContext__doc__[] =
927"GetInputContext() -> string\n\
928Return the untranslated text of the input that caused the current event.\n\
929If the event was generated by a large amount of text (such as a start tag\n\
930for an element with many attributes), not all of the text may be available.";
931
932static PyObject *
933xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
934{
935 PyObject *result = NULL;
936
937 if (PyArg_ParseTuple(args, ":GetInputContext")) {
938 if (self->in_callback) {
939 int offset, size;
940 const char *buffer
941 = XML_GetInputContext(self->itself, &offset, &size);
942
943 if (buffer != NULL)
944 result = PyString_FromStringAndSize(buffer + offset, size);
945 else {
946 result = Py_None;
947 Py_INCREF(result);
948 }
949 }
950 else {
951 result = Py_None;
952 Py_INCREF(result);
953 }
954 }
955 return result;
956}
957#endif
958
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000959static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000960"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000961Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000962information passed to the ExternalEntityRefHandler.";
963
964static PyObject *
965xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
966{
967 char *context;
968 char *encoding = NULL;
969 xmlparseobject *new_parser;
970 int i;
971
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000972 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000973 &context, &encoding)) {
974 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000975 }
976
977#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
978 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000979#else
Fred Drake85d835f2001-02-08 15:39:08 +0000980 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000981 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000982#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000983
984 if (new_parser == NULL)
985 return NULL;
986 new_parser->returns_unicode = self->returns_unicode;
987 new_parser->ordered_attributes = self->ordered_attributes;
988 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000989 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000990 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000991 encoding);
992 new_parser->handlers = 0;
993 PyObject_GC_Init(new_parser);
994
995 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000996 Py_DECREF(new_parser);
997 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000998 }
999
1000 XML_SetUserData(new_parser->itself, (void *)new_parser);
1001
1002 /* allocate and clear handlers first */
1003 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001004 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001005
1006 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001007 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001008 Py_DECREF(new_parser);
1009 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001010 }
1011 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001012
1013 /* then copy handlers from self */
1014 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001015 if (self->handlers[i]) {
1016 Py_INCREF(self->handlers[i]);
1017 new_parser->handlers[i] = self->handlers[i];
1018 handler_info[i].setter(new_parser->itself,
1019 handler_info[i].handler);
1020 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001021 }
Fred Drake28adf522000-09-24 22:07:59 +00001022 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001023}
1024
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001025#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001027static char xmlparse_SetParamEntityParsing__doc__[] =
1028"SetParamEntityParsing(flag) -> success\n\
1029Controls parsing of parameter entities (including the external DTD\n\
1030subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1031XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1032XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1033was successful.";
1034
1035static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001036xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001037{
Fred Drake85d835f2001-02-08 15:39:08 +00001038 int flag;
1039 if (!PyArg_ParseTuple(args, "i", &flag))
1040 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001041 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001042 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001043}
1044
Fred Drake85d835f2001-02-08 15:39:08 +00001045#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001046
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001047static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001048 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001049 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001050 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001051 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001052 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001053 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001054 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001055 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001056 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1057 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001058#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001059 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1060 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1061#endif
1062#if EXPAT_VERSION >= 0x015f00
1063 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1064 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001065#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001066 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001067};
1068
1069/* ---------- */
1070
1071
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001072#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073
1074/*
1075 pyexpat international encoding support.
1076 Make it as simple as possible.
1077*/
1078
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001079static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001080PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001081
1082static void
1083init_template_buffer(void)
1084{
1085 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001086 for (i = 0; i < 256; i++) {
1087 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001088 }
Fred Drakebb66a202001-03-01 20:48:17 +00001089 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001090}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001091
1092int
1093PyUnknownEncodingHandler(void *encodingHandlerData,
1094const XML_Char *name,
1095XML_Encoding * info)
1096{
Fred Drakebb66a202001-03-01 20:48:17 +00001097 PyUnicodeObject *_u_string = NULL;
1098 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099 int i;
1100
Fred Drakebb66a202001-03-01 20:48:17 +00001101 /* Yes, supports only 8bit encodings */
1102 _u_string = (PyUnicodeObject *)
1103 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001104
Fred Drakebb66a202001-03-01 20:48:17 +00001105 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001106 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107
Fred Drakebb66a202001-03-01 20:48:17 +00001108 for (i = 0; i < 256; i++) {
1109 /* Stupid to access directly, but fast */
1110 Py_UNICODE c = _u_string->str[i];
1111 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001112 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001113 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001114 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001115 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001116
1117 info->data = NULL;
1118 info->convert = NULL;
1119 info->release = NULL;
1120 result=1;
1121
1122 Py_DECREF(_u_string);
1123 return result;
1124}
1125
1126#endif
1127
1128static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001129newxmlparseobject(char *encoding, char *namespace_separator)
1130{
1131 int i;
1132 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001133
1134#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001135 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1136 if (self == NULL)
1137 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001138
Fred Drake0582df92000-07-12 04:49:00 +00001139 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001140#else
Fred Drake0582df92000-07-12 04:49:00 +00001141 /* Code for versions 1.6 and later */
1142 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1143 if (self == NULL)
1144 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001145
Fred Drake0582df92000-07-12 04:49:00 +00001146 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001147#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001148 self->ordered_attributes = 0;
1149 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001150 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001151 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001152 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001153 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1154 }
Fred Drake85d835f2001-02-08 15:39:08 +00001155 else {
Fred Drake0582df92000-07-12 04:49:00 +00001156 self->itself = XML_ParserCreate(encoding);
1157 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001158 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001159 if (self->itself == NULL) {
1160 PyErr_SetString(PyExc_RuntimeError,
1161 "XML_ParserCreate failed");
1162 Py_DECREF(self);
1163 return NULL;
1164 }
1165 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001166#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001167 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1168#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001169
Fred Drake0582df92000-07-12 04:49:00 +00001170 for(i = 0; handler_info[i].name != NULL; i++)
1171 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001172
Fred Drake0582df92000-07-12 04:49:00 +00001173 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001174 if (!self->handlers){
1175 Py_DECREF(self);
1176 return PyErr_NoMemory();
1177 }
1178 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001179
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001181}
1182
1183
1184static void
Fred Drake0582df92000-07-12 04:49:00 +00001185xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001186{
Fred Drake0582df92000-07-12 04:49:00 +00001187 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001188 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001189 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001190 XML_ParserFree(self->itself);
1191 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001192
Fred Drake85d835f2001-02-08 15:39:08 +00001193 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001194 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001195 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001196 temp = self->handlers[i];
1197 self->handlers[i] = NULL;
1198 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001199 }
1200 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001201 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001202#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001203 /* Code for versions before 1.6 */
1204 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001205#else
Fred Drake0582df92000-07-12 04:49:00 +00001206 /* Code for versions 1.6 and later */
1207 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001208#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001209}
1210
Fred Drake0582df92000-07-12 04:49:00 +00001211static int
1212handlername2int(const char *name)
1213{
1214 int i;
1215 for (i=0; handler_info[i].name != NULL; i++) {
1216 if (strcmp(name, handler_info[i].name) == 0) {
1217 return i;
1218 }
1219 }
1220 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001221}
1222
1223static PyObject *
1224xmlparse_getattr(xmlparseobject *self, char *name)
1225{
Fred Drake0582df92000-07-12 04:49:00 +00001226 int handlernum;
1227 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001228 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001230 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001231 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001232 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001233 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001234 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1235 if (strcmp(name, "ordered_attributes") == 0)
1236 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001237 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001238 return PyInt_FromLong((long) self->returns_unicode);
1239 if (strcmp(name, "specified_attributes") == 0)
1240 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001241
Fred Drake0582df92000-07-12 04:49:00 +00001242 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001243
Fred Drake0582df92000-07-12 04:49:00 +00001244 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1245 Py_INCREF(self->handlers[handlernum]);
1246 return self->handlers[handlernum];
1247 }
1248 if (strcmp(name, "__members__") == 0) {
1249 int i;
1250 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001251 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001252 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001253 }
1254 PyList_Append(rc, PyString_FromString("ErrorCode"));
1255 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1256 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1257 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001258 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001259 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001260 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001261
Fred Drake0582df92000-07-12 04:49:00 +00001262 return rc;
1263 }
1264 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001265}
1266
Fred Drake6f987622000-08-25 18:03:30 +00001267static int
1268sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001269{
1270 int handlernum = handlername2int(name);
1271 if (handlernum != -1) {
1272 Py_INCREF(v);
1273 Py_XDECREF(self->handlers[handlernum]);
1274 self->handlers[handlernum] = v;
1275 handler_info[handlernum].setter(self->itself,
1276 handler_info[handlernum].handler);
1277 return 1;
1278 }
1279 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001280}
1281
1282static int
Fred Drake6f987622000-08-25 18:03:30 +00001283xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001284{
Fred Drake6f987622000-08-25 18:03:30 +00001285 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001286 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001287 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1288 return -1;
1289 }
Fred Drake85d835f2001-02-08 15:39:08 +00001290 if (strcmp(name, "ordered_attributes") == 0) {
1291 if (PyObject_IsTrue(v))
1292 self->ordered_attributes = 1;
1293 else
1294 self->ordered_attributes = 0;
1295 return 0;
1296 }
Fred Drake6f987622000-08-25 18:03:30 +00001297 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001298 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001299#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001300 PyErr_SetString(PyExc_ValueError,
1301 "Cannot return Unicode strings in Python 1.5");
1302 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001303#else
Fred Drake6f987622000-08-25 18:03:30 +00001304 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001305#endif
Fred Drake6f987622000-08-25 18:03:30 +00001306 }
1307 else
1308 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001309 return 0;
1310 }
1311 if (strcmp(name, "specified_attributes") == 0) {
1312 if (PyObject_IsTrue(v))
1313 self->specified_attributes = 1;
1314 else
1315 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001316 return 0;
1317 }
1318 if (sethandler(self, name, v)) {
1319 return 0;
1320 }
1321 PyErr_SetString(PyExc_AttributeError, name);
1322 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001323}
1324
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001325#ifdef WITH_CYCLE_GC
1326static int
1327xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1328{
Fred Drakecde79132001-04-25 16:01:30 +00001329 int i, err;
1330 for (i = 0; handler_info[i].name != NULL; i++) {
1331 if (!op->handlers[i])
1332 continue;
1333 err = visit(op->handlers[i], arg);
1334 if (err)
1335 return err;
1336 }
1337 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001338}
1339
1340static int
1341xmlparse_clear(xmlparseobject *op)
1342{
Fred Drakecde79132001-04-25 16:01:30 +00001343 clear_handlers(op, 1);
1344 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001345}
1346#endif
1347
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001348static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001349"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001350
1351static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001352 PyObject_HEAD_INIT(NULL)
1353 0, /*ob_size*/
1354 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001355 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001356 0, /*tp_itemsize*/
1357 /* methods */
1358 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1359 (printfunc)0, /*tp_print*/
1360 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1361 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1362 (cmpfunc)0, /*tp_compare*/
1363 (reprfunc)0, /*tp_repr*/
1364 0, /*tp_as_number*/
1365 0, /*tp_as_sequence*/
1366 0, /*tp_as_mapping*/
1367 (hashfunc)0, /*tp_hash*/
1368 (ternaryfunc)0, /*tp_call*/
1369 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001370 0, /* tp_getattro */
1371 0, /* tp_setattro */
1372 0, /* tp_as_buffer */
1373 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1374 Xmlparsetype__doc__, /* Documentation string */
1375#ifdef WITH_CYCLE_GC
1376 (traverseproc)xmlparse_traverse, /* tp_traverse */
1377 (inquiry)xmlparse_clear /* tp_clear */
1378#else
1379 0, 0
1380#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001381};
1382
1383/* End of code for xmlparser objects */
1384/* -------------------------------------------------------- */
1385
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001386static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001387"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1388Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001389
1390static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001391pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1392{
Fred Drakecde79132001-04-25 16:01:30 +00001393 char *encoding = NULL;
1394 char *namespace_separator = NULL;
1395 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001396
Fred Drakecde79132001-04-25 16:01:30 +00001397 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1398 &encoding, &namespace_separator))
1399 return NULL;
1400 if (namespace_separator != NULL
1401 && strlen(namespace_separator) > 1) {
1402 PyErr_SetString(PyExc_ValueError,
1403 "namespace_separator must be at most one"
1404 " character, omitted, or None");
1405 return NULL;
1406 }
1407 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001408}
1409
1410static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001411"ErrorString(errno) -> string\n\
1412Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001413
1414static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001415pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001416{
Fred Drake0582df92000-07-12 04:49:00 +00001417 long code = 0;
1418
1419 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1420 return NULL;
1421 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001422}
1423
1424/* List of methods defined in the module */
1425
1426static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001427 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1428 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1429 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1430 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431
Fred Drake0582df92000-07-12 04:49:00 +00001432 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001433};
1434
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001435/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001436
1437static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001438"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001439
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001440#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001441
1442/* 1.5 compatibility: PyModule_AddObject */
1443static int
1444PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1445{
Fred Drakecde79132001-04-25 16:01:30 +00001446 PyObject *dict;
1447 if (!PyModule_Check(m) || o == NULL)
1448 return -1;
1449 dict = PyModule_GetDict(m);
1450 if (dict == NULL)
1451 return -1;
1452 if (PyDict_SetItemString(dict, name, o))
1453 return -1;
1454 Py_DECREF(o);
1455 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001456}
1457
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001458int
1459PyModule_AddIntConstant(PyObject *m, char *name, long value)
1460{
Fred Drakecde79132001-04-25 16:01:30 +00001461 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001462}
1463
Fred Drakea77254a2000-09-29 19:23:29 +00001464static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001465PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1466{
Fred Drakecde79132001-04-25 16:01:30 +00001467 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001468}
1469
1470#endif
1471
Fred Drake4113b132001-03-24 19:58:26 +00001472
1473/* Return a Python string that represents the version number without the
1474 * extra cruft added by revision control, even if the right options were
1475 * given to the "cvs export" command to make it not include the extra
1476 * cruft.
1477 */
1478static PyObject *
1479get_version_string(void)
1480{
1481 static char *rcsid = "$Revision$";
1482 char *rev = rcsid;
1483 int i = 0;
1484
1485 while (!isdigit(*rev))
1486 ++rev;
1487 while (rev[i] != ' ' && rev[i] != '\0')
1488 ++i;
1489
1490 return PyString_FromStringAndSize(rev, i);
1491}
1492
Fred Drakecde79132001-04-25 16:01:30 +00001493/* Initialization function for the module */
1494
1495#ifndef MODULE_NAME
1496#define MODULE_NAME "pyexpat"
1497#endif
1498
1499#ifndef MODULE_INITFUNC
1500#define MODULE_INITFUNC initpyexpat
1501#endif
1502
1503void MODULE_INITFUNC(void); /* avoid compiler warnings */
1504
Fred Drake6f987622000-08-25 18:03:30 +00001505DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001506MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001507{
1508 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001509 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001510 PyObject *errors_module;
1511 PyObject *modelmod_name;
1512 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001513 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001514
Fred Drake6f987622000-08-25 18:03:30 +00001515 if (errmod_name == NULL)
1516 return;
Fred Drakecde79132001-04-25 16:01:30 +00001517 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001518 if (modelmod_name == NULL)
1519 return;
Fred Drake6f987622000-08-25 18:03:30 +00001520
Fred Drake0582df92000-07-12 04:49:00 +00001521 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001522
Fred Drake0582df92000-07-12 04:49:00 +00001523 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001524 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001525 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001526
Fred Drake0582df92000-07-12 04:49:00 +00001527 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001528 if (ErrorObject == NULL) {
1529 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001530 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001531 if (ErrorObject == NULL)
1532 return;
1533 }
1534 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001535 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001536 Py_INCREF(ErrorObject);
1537 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001538 Py_INCREF(&Xmlparsetype);
1539 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001540
Fred Drake4113b132001-03-24 19:58:26 +00001541 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001542#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001543 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1544 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001545 {
1546 XML_Expat_Version info = XML_ExpatVersionInfo();
1547 PyModule_AddObject(m, "version_info",
1548 Py_BuildValue("(iii)", info.major,
1549 info.minor, info.micro));
1550 }
Fred Drake738293d2000-12-21 17:25:07 +00001551#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001552#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001553 init_template_buffer();
1554#endif
Fred Drake0582df92000-07-12 04:49:00 +00001555 /* XXX When Expat supports some way of figuring out how it was
1556 compiled, this should check and set native_encoding
1557 appropriately.
1558 */
Fred Drake93adb692000-09-23 04:55:48 +00001559 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001560
Fred Drake85d835f2001-02-08 15:39:08 +00001561 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001562 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001563 errors_module = PyDict_GetItem(d, errmod_name);
1564 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001565 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001566 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001567 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001568 /* gives away the reference to errors_module */
1569 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001570 }
1571 }
Fred Drake6f987622000-08-25 18:03:30 +00001572 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001573 model_module = PyDict_GetItem(d, modelmod_name);
1574 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001575 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001576 if (model_module != NULL) {
1577 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1578 /* gives away the reference to model_module */
1579 PyModule_AddObject(m, "model", model_module);
1580 }
1581 }
1582 Py_DECREF(modelmod_name);
1583 if (errors_module == NULL || model_module == NULL)
1584 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001585 return;
1586
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001587#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001588 PyModule_AddStringConstant(errors_module, #name, \
1589 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001590
Fred Drake0582df92000-07-12 04:49:00 +00001591 MYCONST(XML_ERROR_NO_MEMORY);
1592 MYCONST(XML_ERROR_SYNTAX);
1593 MYCONST(XML_ERROR_NO_ELEMENTS);
1594 MYCONST(XML_ERROR_INVALID_TOKEN);
1595 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1596 MYCONST(XML_ERROR_PARTIAL_CHAR);
1597 MYCONST(XML_ERROR_TAG_MISMATCH);
1598 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1599 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1600 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1601 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1602 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1603 MYCONST(XML_ERROR_ASYNC_ENTITY);
1604 MYCONST(XML_ERROR_BAD_CHAR_REF);
1605 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1606 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1607 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1608 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1609 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001610 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1611 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1612 MYCONST(XML_ERROR_NOT_STANDALONE);
1613
Fred Drake85d835f2001-02-08 15:39:08 +00001614 PyModule_AddStringConstant(errors_module, "__doc__",
1615 "Constants used to describe error conditions.");
1616
Fred Drake93adb692000-09-23 04:55:48 +00001617#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001618
1619#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001620#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001621 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1622 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1623 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001624#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001625#endif
1626
Fred Drake85d835f2001-02-08 15:39:08 +00001627#if EXPAT_VERSION >= 0x015f00
1628#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1629 PyModule_AddStringConstant(model_module, "__doc__",
1630 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001631
Fred Drake85d835f2001-02-08 15:39:08 +00001632 MYCONST(XML_CTYPE_EMPTY);
1633 MYCONST(XML_CTYPE_ANY);
1634 MYCONST(XML_CTYPE_MIXED);
1635 MYCONST(XML_CTYPE_NAME);
1636 MYCONST(XML_CTYPE_CHOICE);
1637 MYCONST(XML_CTYPE_SEQ);
1638
1639 MYCONST(XML_CQUANT_NONE);
1640 MYCONST(XML_CQUANT_OPT);
1641 MYCONST(XML_CQUANT_REP);
1642 MYCONST(XML_CQUANT_PLUS);
1643#undef MYCONST
1644#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001645}
1646
Fred Drake6f987622000-08-25 18:03:30 +00001647static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001648clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001649{
Fred Drakecde79132001-04-25 16:01:30 +00001650 int i = 0;
1651 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001652
Fred Drakecde79132001-04-25 16:01:30 +00001653 for (; handler_info[i].name!=NULL; i++) {
1654 if (decref) {
1655 temp = self->handlers[i];
1656 self->handlers[i] = NULL;
1657 Py_XDECREF(temp);
1658 }
1659 self->handlers[i]=NULL;
1660 handler_info[i].setter(self->itself, NULL);
1661 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001662}
1663
Fred Drake6f987622000-08-25 18:03:30 +00001664typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001665
Fred Drake6f987622000-08-25 18:03:30 +00001666static void
1667pyxml_UpdatePairedHandlers(xmlparseobject *self,
1668 int startHandler,
1669 int endHandler,
1670 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001671{
Fred Drakecde79132001-04-25 16:01:30 +00001672 void *start_handler = NULL;
1673 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001674
Fred Drake0582df92000-07-12 04:49:00 +00001675 if (self->handlers[startHandler]
Fred Drakecde79132001-04-25 16:01:30 +00001676 && self->handlers[endHandler] != Py_None) {
1677 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001678 }
1679 if (self->handlers[EndElement]
Fred Drakecde79132001-04-25 16:01:30 +00001680 && self->handlers[EndElement] != Py_None) {
1681 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001682 }
1683 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001684}
1685
Fred Drake6f987622000-08-25 18:03:30 +00001686static void
1687pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001688{
1689 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1690 StartElement, EndElement,
1691 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001692}
1693
Fred Drake6f987622000-08-25 18:03:30 +00001694static void
1695pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001696{
1697 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1698 StartElement, EndElement,
1699 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001700}
1701
Fred Drake6f987622000-08-25 18:03:30 +00001702static void
1703pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001704{
1705 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1706 StartNamespaceDecl, EndNamespaceDecl,
1707 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001708}
1709
Fred Drake6f987622000-08-25 18:03:30 +00001710static void
1711pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001712{
1713 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1714 StartNamespaceDecl, EndNamespaceDecl,
1715 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001716}
1717
Fred Drake6f987622000-08-25 18:03:30 +00001718static void
1719pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001720{
1721 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1722 StartCdataSection, EndCdataSection,
1723 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001724}
1725
Fred Drake6f987622000-08-25 18:03:30 +00001726static void
1727pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001728{
1729 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1730 StartCdataSection, EndCdataSection,
1731 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001732}
1733
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001734#if EXPAT_VERSION >= 0x010200
1735
1736static void
1737pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1738{
1739 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1740 StartDoctypeDecl, EndDoctypeDecl,
1741 (pairsetter)XML_SetDoctypeDeclHandler);
1742}
1743
1744static void
1745pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1746{
1747 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1748 StartDoctypeDecl, EndDoctypeDecl,
1749 (pairsetter)XML_SetDoctypeDeclHandler);
1750}
1751
1752#endif
1753
Fred Drake0582df92000-07-12 04:49:00 +00001754statichere struct HandlerInfo handler_info[] = {
1755 {"StartElementHandler",
1756 pyxml_SetStartElementHandler,
1757 (xmlhandler)my_StartElementHandler},
1758 {"EndElementHandler",
1759 pyxml_SetEndElementHandler,
1760 (xmlhandler)my_EndElementHandler},
1761 {"ProcessingInstructionHandler",
1762 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1763 (xmlhandler)my_ProcessingInstructionHandler},
1764 {"CharacterDataHandler",
1765 (xmlhandlersetter)XML_SetCharacterDataHandler,
1766 (xmlhandler)my_CharacterDataHandler},
1767 {"UnparsedEntityDeclHandler",
1768 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1769 (xmlhandler)my_UnparsedEntityDeclHandler },
1770 {"NotationDeclHandler",
1771 (xmlhandlersetter)XML_SetNotationDeclHandler,
1772 (xmlhandler)my_NotationDeclHandler },
1773 {"StartNamespaceDeclHandler",
1774 pyxml_SetStartNamespaceDeclHandler,
1775 (xmlhandler)my_StartNamespaceDeclHandler },
1776 {"EndNamespaceDeclHandler",
1777 pyxml_SetEndNamespaceDeclHandler,
1778 (xmlhandler)my_EndNamespaceDeclHandler },
1779 {"CommentHandler",
1780 (xmlhandlersetter)XML_SetCommentHandler,
1781 (xmlhandler)my_CommentHandler},
1782 {"StartCdataSectionHandler",
1783 pyxml_SetStartCdataSection,
1784 (xmlhandler)my_StartCdataSectionHandler},
1785 {"EndCdataSectionHandler",
1786 pyxml_SetEndCdataSection,
1787 (xmlhandler)my_EndCdataSectionHandler},
1788 {"DefaultHandler",
1789 (xmlhandlersetter)XML_SetDefaultHandler,
1790 (xmlhandler)my_DefaultHandler},
1791 {"DefaultHandlerExpand",
1792 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1793 (xmlhandler)my_DefaultHandlerExpandHandler},
1794 {"NotStandaloneHandler",
1795 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1796 (xmlhandler)my_NotStandaloneHandler},
1797 {"ExternalEntityRefHandler",
1798 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1799 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001800#if EXPAT_VERSION >= 0x010200
1801 {"StartDoctypeDeclHandler",
1802 pyxml_SetStartDoctypeDeclHandler,
1803 (xmlhandler)my_StartDoctypeDeclHandler},
1804 {"EndDoctypeDeclHandler",
1805 pyxml_SetEndDoctypeDeclHandler,
1806 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001807#endif
1808#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001809 {"ExternalParsedEntityDeclHandler",
1810 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1811 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1812 {"InternalParsedEntityDeclHandler",
1813 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1814 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001815#endif
1816#if EXPAT_VERSION >= 0x015f00
1817 {"EntityDeclHandler",
1818 (xmlhandlersetter)XML_SetEntityDeclHandler,
1819 (xmlhandler)my_EntityDeclHandler},
1820 {"XmlDeclHandler",
1821 (xmlhandlersetter)XML_SetXmlDeclHandler,
1822 (xmlhandler)my_XmlDeclHandler},
1823 {"ElementDeclHandler",
1824 (xmlhandlersetter)XML_SetElementDeclHandler,
1825 (xmlhandler)my_ElementDeclHandler},
1826 {"AttlistDeclHandler",
1827 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1828 (xmlhandler)my_AttlistDeclHandler},
1829#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001830
Fred Drake0582df92000-07-12 04:49:00 +00001831 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001832};