blob: db4333c7c6429b2b51b1108e1c2a397693e7659e [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öwis5b68ce32001-10-21 08:53:52 +0000293static void clear_handlers(xmlparseobject *self, int initial);
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öwis5b68ce32001-10-21 08:53:52 +0000298 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000299}
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
Martin v. Löwis894258c2001-09-23 10:20:10 +0000980#ifndef Py_TPFLAGS_HAVE_GC
981 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000982 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000983#else
984 /* Python versions 2.2 and later */
985 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
986#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000987#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000988
989 if (new_parser == NULL)
990 return NULL;
991 new_parser->returns_unicode = self->returns_unicode;
992 new_parser->ordered_attributes = self->ordered_attributes;
993 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000994 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000995 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000996 encoding);
997 new_parser->handlers = 0;
Martin v. Löwis894258c2001-09-23 10:20:10 +0000998#ifdef Py_TPFLAGS_HAVE_GC
999 PyObject_GC_Track(new_parser);
1000#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001001 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001002#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001003
1004 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001005 Py_DECREF(new_parser);
1006 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001007 }
1008
1009 XML_SetUserData(new_parser->itself, (void *)new_parser);
1010
1011 /* allocate and clear handlers first */
1012 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001013 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001014
1015 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001016 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001017 Py_DECREF(new_parser);
1018 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001019 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001020 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001021
1022 /* then copy handlers from self */
1023 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001024 if (self->handlers[i]) {
1025 Py_INCREF(self->handlers[i]);
1026 new_parser->handlers[i] = self->handlers[i];
1027 handler_info[i].setter(new_parser->itself,
1028 handler_info[i].handler);
1029 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001030 }
Fred Drake28adf522000-09-24 22:07:59 +00001031 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001032}
1033
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001034#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001035
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001036static char xmlparse_SetParamEntityParsing__doc__[] =
1037"SetParamEntityParsing(flag) -> success\n\
1038Controls parsing of parameter entities (including the external DTD\n\
1039subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1040XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1041XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1042was successful.";
1043
1044static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001045xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001046{
Fred Drake85d835f2001-02-08 15:39:08 +00001047 int flag;
1048 if (!PyArg_ParseTuple(args, "i", &flag))
1049 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001051 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001052}
1053
Fred Drake85d835f2001-02-08 15:39:08 +00001054#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001055
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001056static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001057 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001058 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001059 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001060 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001061 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001062 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001063 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001064 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001065 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1066 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001067#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001068 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1069 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1070#endif
1071#if EXPAT_VERSION >= 0x015f00
1072 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1073 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001074#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001075 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001076};
1077
1078/* ---------- */
1079
1080
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001081#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001082
1083/*
1084 pyexpat international encoding support.
1085 Make it as simple as possible.
1086*/
1087
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001088static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001089PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001090
1091static void
1092init_template_buffer(void)
1093{
1094 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001095 for (i = 0; i < 256; i++) {
1096 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001097 }
Fred Drakebb66a202001-03-01 20:48:17 +00001098 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001099}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001100
1101int
1102PyUnknownEncodingHandler(void *encodingHandlerData,
1103const XML_Char *name,
1104XML_Encoding * info)
1105{
Fred Drakebb66a202001-03-01 20:48:17 +00001106 PyUnicodeObject *_u_string = NULL;
1107 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108 int i;
1109
Fred Drakebb66a202001-03-01 20:48:17 +00001110 /* Yes, supports only 8bit encodings */
1111 _u_string = (PyUnicodeObject *)
1112 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001113
Fred Drakebb66a202001-03-01 20:48:17 +00001114 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001115 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001116
Fred Drakebb66a202001-03-01 20:48:17 +00001117 for (i = 0; i < 256; i++) {
1118 /* Stupid to access directly, but fast */
1119 Py_UNICODE c = _u_string->str[i];
1120 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001121 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001122 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001123 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001124 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001125
1126 info->data = NULL;
1127 info->convert = NULL;
1128 info->release = NULL;
1129 result=1;
1130
1131 Py_DECREF(_u_string);
1132 return result;
1133}
1134
1135#endif
1136
1137static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001138newxmlparseobject(char *encoding, char *namespace_separator)
1139{
1140 int i;
1141 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001142
1143#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001144 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1145 if (self == NULL)
1146 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001147
Fred Drake0582df92000-07-12 04:49:00 +00001148 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001149#else
Fred Drake0582df92000-07-12 04:49:00 +00001150 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001151#ifdef Py_TPFLAGS_HAVE_GC
1152 /* Code for versions 2.2 and later */
1153 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1154#else
Fred Drake0582df92000-07-12 04:49:00 +00001155 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001156#endif
Fred Drake0582df92000-07-12 04:49:00 +00001157 if (self == NULL)
1158 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001159
Fred Drake0582df92000-07-12 04:49:00 +00001160 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001161#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001162 self->ordered_attributes = 0;
1163 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001164 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001165 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001166 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001167 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1168 }
Fred Drake85d835f2001-02-08 15:39:08 +00001169 else {
Fred Drake0582df92000-07-12 04:49:00 +00001170 self->itself = XML_ParserCreate(encoding);
1171 }
Martin v. Löwis894258c2001-09-23 10:20:10 +00001172#ifdef Py_TPFLAGS_HAVE_GC
1173 PyObject_GC_Track(self);
1174#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001175 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001176#endif
Fred Drake0582df92000-07-12 04:49:00 +00001177 if (self->itself == NULL) {
1178 PyErr_SetString(PyExc_RuntimeError,
1179 "XML_ParserCreate failed");
1180 Py_DECREF(self);
1181 return NULL;
1182 }
1183 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001184#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001185 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1186#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001187
Fred Drake0582df92000-07-12 04:49:00 +00001188 for(i = 0; handler_info[i].name != NULL; i++)
1189 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001190
Fred Drake0582df92000-07-12 04:49:00 +00001191 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001192 if (!self->handlers){
1193 Py_DECREF(self);
1194 return PyErr_NoMemory();
1195 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001196 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001197
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001198 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001199}
1200
1201
1202static void
Fred Drake0582df92000-07-12 04:49:00 +00001203xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001204{
Fred Drake0582df92000-07-12 04:49:00 +00001205 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001206#ifdef Py_TPFLAGS_HAVE_GC
1207 PyObject_GC_UnTrack(self);
1208#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001209 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001210#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001211 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001212 XML_ParserFree(self->itself);
1213 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001214
Fred Drake85d835f2001-02-08 15:39:08 +00001215 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001216 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001217 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001218 temp = self->handlers[i];
1219 self->handlers[i] = NULL;
1220 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001221 }
1222 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001223 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001224#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001225 /* Code for versions before 1.6 */
1226 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001227#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001228#ifndef Py_TPFLAGS_HAVE_GC
1229 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001230 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001231#else
1232 /* Code for versions 2.2 and later. */
1233 PyObject_GC_Del(self);
1234#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001235#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001236}
1237
Fred Drake0582df92000-07-12 04:49:00 +00001238static int
1239handlername2int(const char *name)
1240{
1241 int i;
1242 for (i=0; handler_info[i].name != NULL; i++) {
1243 if (strcmp(name, handler_info[i].name) == 0) {
1244 return i;
1245 }
1246 }
1247 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001248}
1249
1250static PyObject *
1251xmlparse_getattr(xmlparseobject *self, char *name)
1252{
Fred Drake0582df92000-07-12 04:49:00 +00001253 int handlernum;
1254 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001255 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001256 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001257 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001258 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001259 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001260 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001261 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1262 if (strcmp(name, "ordered_attributes") == 0)
1263 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001264 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001265 return PyInt_FromLong((long) self->returns_unicode);
1266 if (strcmp(name, "specified_attributes") == 0)
1267 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001268
Fred Drake0582df92000-07-12 04:49:00 +00001269 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001270
Fred Drake0582df92000-07-12 04:49:00 +00001271 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1272 Py_INCREF(self->handlers[handlernum]);
1273 return self->handlers[handlernum];
1274 }
1275 if (strcmp(name, "__members__") == 0) {
1276 int i;
1277 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001278 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001279 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001280 }
1281 PyList_Append(rc, PyString_FromString("ErrorCode"));
1282 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1283 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1284 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001285 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001286 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001287 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001288
Fred Drake0582df92000-07-12 04:49:00 +00001289 return rc;
1290 }
1291 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001292}
1293
Fred Drake6f987622000-08-25 18:03:30 +00001294static int
1295sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001296{
1297 int handlernum = handlername2int(name);
1298 if (handlernum != -1) {
1299 Py_INCREF(v);
1300 Py_XDECREF(self->handlers[handlernum]);
1301 self->handlers[handlernum] = v;
1302 handler_info[handlernum].setter(self->itself,
1303 handler_info[handlernum].handler);
1304 return 1;
1305 }
1306 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001307}
1308
1309static int
Fred Drake6f987622000-08-25 18:03:30 +00001310xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001311{
Fred Drake6f987622000-08-25 18:03:30 +00001312 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001313 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001314 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1315 return -1;
1316 }
Fred Drake85d835f2001-02-08 15:39:08 +00001317 if (strcmp(name, "ordered_attributes") == 0) {
1318 if (PyObject_IsTrue(v))
1319 self->ordered_attributes = 1;
1320 else
1321 self->ordered_attributes = 0;
1322 return 0;
1323 }
Fred Drake6f987622000-08-25 18:03:30 +00001324 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001325 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001326#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001327 PyErr_SetString(PyExc_ValueError,
1328 "Cannot return Unicode strings in Python 1.5");
1329 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001330#else
Fred Drake6f987622000-08-25 18:03:30 +00001331 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001332#endif
Fred Drake6f987622000-08-25 18:03:30 +00001333 }
1334 else
1335 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001336 return 0;
1337 }
1338 if (strcmp(name, "specified_attributes") == 0) {
1339 if (PyObject_IsTrue(v))
1340 self->specified_attributes = 1;
1341 else
1342 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001343 return 0;
1344 }
1345 if (sethandler(self, name, v)) {
1346 return 0;
1347 }
1348 PyErr_SetString(PyExc_AttributeError, name);
1349 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001350}
1351
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001352#ifdef WITH_CYCLE_GC
1353static int
1354xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1355{
Fred Drakecde79132001-04-25 16:01:30 +00001356 int i, err;
1357 for (i = 0; handler_info[i].name != NULL; i++) {
1358 if (!op->handlers[i])
1359 continue;
1360 err = visit(op->handlers[i], arg);
1361 if (err)
1362 return err;
1363 }
1364 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001365}
1366
1367static int
1368xmlparse_clear(xmlparseobject *op)
1369{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001370 clear_handlers(op, 0);
Fred Drakecde79132001-04-25 16:01:30 +00001371 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001372}
1373#endif
1374
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001375static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001376"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377
1378static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001379 PyObject_HEAD_INIT(NULL)
1380 0, /*ob_size*/
1381 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001382 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001383 0, /*tp_itemsize*/
1384 /* methods */
1385 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1386 (printfunc)0, /*tp_print*/
1387 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1388 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1389 (cmpfunc)0, /*tp_compare*/
1390 (reprfunc)0, /*tp_repr*/
1391 0, /*tp_as_number*/
1392 0, /*tp_as_sequence*/
1393 0, /*tp_as_mapping*/
1394 (hashfunc)0, /*tp_hash*/
1395 (ternaryfunc)0, /*tp_call*/
1396 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001397 0, /* tp_getattro */
1398 0, /* tp_setattro */
1399 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001400#ifdef Py_TPFLAGS_HAVE_GC
1401 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1402#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001403 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001404#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001405 Xmlparsetype__doc__, /* Documentation string */
1406#ifdef WITH_CYCLE_GC
1407 (traverseproc)xmlparse_traverse, /* tp_traverse */
1408 (inquiry)xmlparse_clear /* tp_clear */
1409#else
1410 0, 0
1411#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001412};
1413
1414/* End of code for xmlparser objects */
1415/* -------------------------------------------------------- */
1416
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001417static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001418"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1419Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001420
1421static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001422pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1423{
Fred Drakecde79132001-04-25 16:01:30 +00001424 char *encoding = NULL;
1425 char *namespace_separator = NULL;
1426 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001427
Fred Drakecde79132001-04-25 16:01:30 +00001428 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1429 &encoding, &namespace_separator))
1430 return NULL;
1431 if (namespace_separator != NULL
1432 && strlen(namespace_separator) > 1) {
1433 PyErr_SetString(PyExc_ValueError,
1434 "namespace_separator must be at most one"
1435 " character, omitted, or None");
1436 return NULL;
1437 }
1438 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001439}
1440
1441static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001442"ErrorString(errno) -> string\n\
1443Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001444
1445static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001446pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001447{
Fred Drake0582df92000-07-12 04:49:00 +00001448 long code = 0;
1449
1450 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1451 return NULL;
1452 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001453}
1454
1455/* List of methods defined in the module */
1456
1457static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001458 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1459 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1460 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1461 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001462
Fred Drake0582df92000-07-12 04:49:00 +00001463 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001464};
1465
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001466/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001467
1468static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001469"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001470
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001471#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001472
1473/* 1.5 compatibility: PyModule_AddObject */
1474static int
1475PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1476{
Fred Drakecde79132001-04-25 16:01:30 +00001477 PyObject *dict;
1478 if (!PyModule_Check(m) || o == NULL)
1479 return -1;
1480 dict = PyModule_GetDict(m);
1481 if (dict == NULL)
1482 return -1;
1483 if (PyDict_SetItemString(dict, name, o))
1484 return -1;
1485 Py_DECREF(o);
1486 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001487}
1488
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001489int
1490PyModule_AddIntConstant(PyObject *m, char *name, long value)
1491{
Fred Drakecde79132001-04-25 16:01:30 +00001492 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001493}
1494
Fred Drakea77254a2000-09-29 19:23:29 +00001495static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001496PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1497{
Fred Drakecde79132001-04-25 16:01:30 +00001498 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001499}
1500
1501#endif
1502
Fred Drake4113b132001-03-24 19:58:26 +00001503
1504/* Return a Python string that represents the version number without the
1505 * extra cruft added by revision control, even if the right options were
1506 * given to the "cvs export" command to make it not include the extra
1507 * cruft.
1508 */
1509static PyObject *
1510get_version_string(void)
1511{
1512 static char *rcsid = "$Revision$";
1513 char *rev = rcsid;
1514 int i = 0;
1515
1516 while (!isdigit(*rev))
1517 ++rev;
1518 while (rev[i] != ' ' && rev[i] != '\0')
1519 ++i;
1520
1521 return PyString_FromStringAndSize(rev, i);
1522}
1523
Fred Drakecde79132001-04-25 16:01:30 +00001524/* Initialization function for the module */
1525
1526#ifndef MODULE_NAME
1527#define MODULE_NAME "pyexpat"
1528#endif
1529
1530#ifndef MODULE_INITFUNC
1531#define MODULE_INITFUNC initpyexpat
1532#endif
1533
1534void MODULE_INITFUNC(void); /* avoid compiler warnings */
1535
Fred Drake6f987622000-08-25 18:03:30 +00001536DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001537MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001538{
1539 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001540 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001541 PyObject *errors_module;
1542 PyObject *modelmod_name;
1543 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001544 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001545
Fred Drake6f987622000-08-25 18:03:30 +00001546 if (errmod_name == NULL)
1547 return;
Fred Drakecde79132001-04-25 16:01:30 +00001548 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001549 if (modelmod_name == NULL)
1550 return;
Fred Drake6f987622000-08-25 18:03:30 +00001551
Fred Drake0582df92000-07-12 04:49:00 +00001552 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001553
Fred Drake0582df92000-07-12 04:49:00 +00001554 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001555 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001556 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001557
Fred Drake0582df92000-07-12 04:49:00 +00001558 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001559 if (ErrorObject == NULL) {
1560 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001561 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001562 if (ErrorObject == NULL)
1563 return;
1564 }
1565 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001566 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001567 Py_INCREF(ErrorObject);
1568 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001569 Py_INCREF(&Xmlparsetype);
1570 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001571
Fred Drake4113b132001-03-24 19:58:26 +00001572 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001573#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001574 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1575 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001576 {
1577 XML_Expat_Version info = XML_ExpatVersionInfo();
1578 PyModule_AddObject(m, "version_info",
1579 Py_BuildValue("(iii)", info.major,
1580 info.minor, info.micro));
1581 }
Fred Drake738293d2000-12-21 17:25:07 +00001582#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001583#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001584 init_template_buffer();
1585#endif
Fred Drake0582df92000-07-12 04:49:00 +00001586 /* XXX When Expat supports some way of figuring out how it was
1587 compiled, this should check and set native_encoding
1588 appropriately.
1589 */
Fred Drake93adb692000-09-23 04:55:48 +00001590 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001591
Fred Drake85d835f2001-02-08 15:39:08 +00001592 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001593 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001594 errors_module = PyDict_GetItem(d, errmod_name);
1595 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001596 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001597 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001598 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001599 /* gives away the reference to errors_module */
1600 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001601 }
1602 }
Fred Drake6f987622000-08-25 18:03:30 +00001603 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001604 model_module = PyDict_GetItem(d, modelmod_name);
1605 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001606 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001607 if (model_module != NULL) {
1608 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1609 /* gives away the reference to model_module */
1610 PyModule_AddObject(m, "model", model_module);
1611 }
1612 }
1613 Py_DECREF(modelmod_name);
1614 if (errors_module == NULL || model_module == NULL)
1615 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001616 return;
1617
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001618#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001619 PyModule_AddStringConstant(errors_module, #name, \
1620 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001621
Fred Drake0582df92000-07-12 04:49:00 +00001622 MYCONST(XML_ERROR_NO_MEMORY);
1623 MYCONST(XML_ERROR_SYNTAX);
1624 MYCONST(XML_ERROR_NO_ELEMENTS);
1625 MYCONST(XML_ERROR_INVALID_TOKEN);
1626 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1627 MYCONST(XML_ERROR_PARTIAL_CHAR);
1628 MYCONST(XML_ERROR_TAG_MISMATCH);
1629 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1630 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1631 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1632 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1633 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1634 MYCONST(XML_ERROR_ASYNC_ENTITY);
1635 MYCONST(XML_ERROR_BAD_CHAR_REF);
1636 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1637 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1638 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1639 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1640 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001641 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1642 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1643 MYCONST(XML_ERROR_NOT_STANDALONE);
1644
Fred Drake85d835f2001-02-08 15:39:08 +00001645 PyModule_AddStringConstant(errors_module, "__doc__",
1646 "Constants used to describe error conditions.");
1647
Fred Drake93adb692000-09-23 04:55:48 +00001648#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001649
1650#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001651#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001652 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1653 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1654 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001655#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001656#endif
1657
Fred Drake85d835f2001-02-08 15:39:08 +00001658#if EXPAT_VERSION >= 0x015f00
1659#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1660 PyModule_AddStringConstant(model_module, "__doc__",
1661 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001662
Fred Drake85d835f2001-02-08 15:39:08 +00001663 MYCONST(XML_CTYPE_EMPTY);
1664 MYCONST(XML_CTYPE_ANY);
1665 MYCONST(XML_CTYPE_MIXED);
1666 MYCONST(XML_CTYPE_NAME);
1667 MYCONST(XML_CTYPE_CHOICE);
1668 MYCONST(XML_CTYPE_SEQ);
1669
1670 MYCONST(XML_CQUANT_NONE);
1671 MYCONST(XML_CQUANT_OPT);
1672 MYCONST(XML_CQUANT_REP);
1673 MYCONST(XML_CQUANT_PLUS);
1674#undef MYCONST
1675#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001676}
1677
Fred Drake6f987622000-08-25 18:03:30 +00001678static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001679clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001680{
Fred Drakecde79132001-04-25 16:01:30 +00001681 int i = 0;
1682 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001683
Fred Drakecde79132001-04-25 16:01:30 +00001684 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001685 if (initial)
1686 self->handlers[i]=NULL;
1687 else {
Fred Drakecde79132001-04-25 16:01:30 +00001688 temp = self->handlers[i];
1689 self->handlers[i] = NULL;
1690 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001691 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001692 }
Fred Drakecde79132001-04-25 16:01:30 +00001693 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001694}
1695
Fred Drake6f987622000-08-25 18:03:30 +00001696typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001697
Fred Drake6f987622000-08-25 18:03:30 +00001698static void
1699pyxml_UpdatePairedHandlers(xmlparseobject *self,
1700 int startHandler,
1701 int endHandler,
1702 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001703{
Fred Drakecde79132001-04-25 16:01:30 +00001704 void *start_handler = NULL;
1705 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001706
Fred Drake0582df92000-07-12 04:49:00 +00001707 if (self->handlers[startHandler]
Fred Drakecde79132001-04-25 16:01:30 +00001708 && self->handlers[endHandler] != Py_None) {
1709 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001710 }
1711 if (self->handlers[EndElement]
Fred Drakecde79132001-04-25 16:01:30 +00001712 && self->handlers[EndElement] != Py_None) {
1713 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001714 }
1715 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001716}
1717
Fred Drake6f987622000-08-25 18:03:30 +00001718static void
1719pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001720{
1721 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1722 StartElement, EndElement,
1723 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001724}
1725
Fred Drake6f987622000-08-25 18:03:30 +00001726static void
1727pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001728{
1729 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1730 StartElement, EndElement,
1731 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001732}
1733
Fred Drake6f987622000-08-25 18:03:30 +00001734static void
1735pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001736{
1737 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1738 StartNamespaceDecl, EndNamespaceDecl,
1739 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001740}
1741
Fred Drake6f987622000-08-25 18:03:30 +00001742static void
1743pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001744{
1745 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1746 StartNamespaceDecl, EndNamespaceDecl,
1747 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001748}
1749
Fred Drake6f987622000-08-25 18:03:30 +00001750static void
1751pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001752{
1753 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1754 StartCdataSection, EndCdataSection,
1755 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001756}
1757
Fred Drake6f987622000-08-25 18:03:30 +00001758static void
1759pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001760{
1761 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1762 StartCdataSection, EndCdataSection,
1763 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001764}
1765
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001766#if EXPAT_VERSION >= 0x010200
1767
1768static void
1769pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1770{
1771 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1772 StartDoctypeDecl, EndDoctypeDecl,
1773 (pairsetter)XML_SetDoctypeDeclHandler);
1774}
1775
1776static void
1777pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1778{
1779 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1780 StartDoctypeDecl, EndDoctypeDecl,
1781 (pairsetter)XML_SetDoctypeDeclHandler);
1782}
1783
1784#endif
1785
Fred Drake0582df92000-07-12 04:49:00 +00001786statichere struct HandlerInfo handler_info[] = {
1787 {"StartElementHandler",
1788 pyxml_SetStartElementHandler,
1789 (xmlhandler)my_StartElementHandler},
1790 {"EndElementHandler",
1791 pyxml_SetEndElementHandler,
1792 (xmlhandler)my_EndElementHandler},
1793 {"ProcessingInstructionHandler",
1794 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1795 (xmlhandler)my_ProcessingInstructionHandler},
1796 {"CharacterDataHandler",
1797 (xmlhandlersetter)XML_SetCharacterDataHandler,
1798 (xmlhandler)my_CharacterDataHandler},
1799 {"UnparsedEntityDeclHandler",
1800 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1801 (xmlhandler)my_UnparsedEntityDeclHandler },
1802 {"NotationDeclHandler",
1803 (xmlhandlersetter)XML_SetNotationDeclHandler,
1804 (xmlhandler)my_NotationDeclHandler },
1805 {"StartNamespaceDeclHandler",
1806 pyxml_SetStartNamespaceDeclHandler,
1807 (xmlhandler)my_StartNamespaceDeclHandler },
1808 {"EndNamespaceDeclHandler",
1809 pyxml_SetEndNamespaceDeclHandler,
1810 (xmlhandler)my_EndNamespaceDeclHandler },
1811 {"CommentHandler",
1812 (xmlhandlersetter)XML_SetCommentHandler,
1813 (xmlhandler)my_CommentHandler},
1814 {"StartCdataSectionHandler",
1815 pyxml_SetStartCdataSection,
1816 (xmlhandler)my_StartCdataSectionHandler},
1817 {"EndCdataSectionHandler",
1818 pyxml_SetEndCdataSection,
1819 (xmlhandler)my_EndCdataSectionHandler},
1820 {"DefaultHandler",
1821 (xmlhandlersetter)XML_SetDefaultHandler,
1822 (xmlhandler)my_DefaultHandler},
1823 {"DefaultHandlerExpand",
1824 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1825 (xmlhandler)my_DefaultHandlerExpandHandler},
1826 {"NotStandaloneHandler",
1827 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1828 (xmlhandler)my_NotStandaloneHandler},
1829 {"ExternalEntityRefHandler",
1830 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1831 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001832#if EXPAT_VERSION >= 0x010200
1833 {"StartDoctypeDeclHandler",
1834 pyxml_SetStartDoctypeDeclHandler,
1835 (xmlhandler)my_StartDoctypeDeclHandler},
1836 {"EndDoctypeDeclHandler",
1837 pyxml_SetEndDoctypeDeclHandler,
1838 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001839#endif
1840#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001841 {"ExternalParsedEntityDeclHandler",
1842 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1843 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1844 {"InternalParsedEntityDeclHandler",
1845 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1846 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001847#endif
1848#if EXPAT_VERSION >= 0x015f00
1849 {"EntityDeclHandler",
1850 (xmlhandlersetter)XML_SetEntityDeclHandler,
1851 (xmlhandler)my_EntityDeclHandler},
1852 {"XmlDeclHandler",
1853 (xmlhandlersetter)XML_SetXmlDeclHandler,
1854 (xmlhandler)my_XmlDeclHandler},
1855 {"ElementDeclHandler",
1856 (xmlhandlersetter)XML_SetElementDeclHandler,
1857 (xmlhandler)my_ElementDeclHandler},
1858 {"AttlistDeclHandler",
1859 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1860 (xmlhandler)my_AttlistDeclHandler},
1861#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001862
Fred Drake0582df92000-07-12 04:49:00 +00001863 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001864};