blob: 0442acacb0470ffe3f976ff90d9e739720514012 [file] [log] [blame]
Fred Drake4113b132001-03-24 19:58:26 +00001#include <ctype.h>
2
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00003#include "Python.h"
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
Fred Drake0582df92000-07-12 04:49:00 +000031enum HandlerTypes {
32 StartElement,
33 EndElement,
34 ProcessingInstruction,
35 CharacterData,
36 UnparsedEntityDecl,
37 NotationDecl,
38 StartNamespaceDecl,
39 EndNamespaceDecl,
40 Comment,
41 StartCdataSection,
42 EndCdataSection,
43 Default,
44 DefaultHandlerExpand,
45 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000046 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000047#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000048 StartDoctypeDecl,
49 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000050#endif
51#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000052 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000053 InternalParsedEntityDecl,
54#endif
55#if EXPAT_VERSION >= 0x015f00
56 EntityDecl,
57 XmlDecl,
58 ElementDecl,
59 AttlistDecl,
60#endif
61 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000062};
63
64static PyObject *ErrorObject;
65
66/* ----------------------------------------------------- */
67
68/* Declarations for objects of type xmlparser */
69
70typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000071 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000072
Fred Drake0582df92000-07-12 04:49:00 +000073 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000074 int returns_unicode; /* True if Unicode strings are returned;
75 if false, UTF-8 strings are returned */
76 int ordered_attributes; /* Return attributes as a list. */
77 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000078 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000079 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080} xmlparseobject;
81
82staticforward PyTypeObject Xmlparsetype;
83
Fred Drake6f987622000-08-25 18:03:30 +000084typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000085typedef void* xmlhandler;
86
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000087struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000088 const char *name;
89 xmlhandlersetter setter;
90 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000091 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000092};
93
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000094staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000095
Fred Drakebd6101c2001-02-14 18:29:45 +000096/* Set an integer attribute on the error object; return true on success,
97 * false on an exception.
98 */
99static int
100set_error_attr(PyObject *err, char *name, int value)
101{
102 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000103
Fred Drakebd6101c2001-02-14 18:29:45 +0000104 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
105 Py_DECREF(v);
106 return 0;
107 }
108 return 1;
109}
110
111/* Build and set an Expat exception, including positioning
112 * information. Always returns NULL.
113 */
Fred Drake85d835f2001-02-08 15:39:08 +0000114static PyObject *
115set_error(xmlparseobject *self)
116{
117 PyObject *err;
118 char buffer[256];
119 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000120 int lineno = XML_GetErrorLineNumber(parser);
121 int column = XML_GetErrorColumnNumber(parser);
122 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000123
124 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000126 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000127 if ( err != NULL
128 && set_error_attr(err, "code", code)
129 && set_error_attr(err, "offset", column)
130 && set_error_attr(err, "lineno", lineno)) {
131 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000132 }
133 return NULL;
134}
135
136
137#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000138/* Convert an array of attributes and their values into a Python dict */
139
Fred Drake0582df92000-07-12 04:49:00 +0000140static PyObject *
141conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000142{
Fred Drake0582df92000-07-12 04:49:00 +0000143 PyObject *attrs_obj = NULL;
144 XML_Char **attrs_p, **attrs_k = NULL;
145 int attrs_len;
146 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000147
Fred Drake0582df92000-07-12 04:49:00 +0000148 if ((attrs_obj = PyDict_New()) == NULL)
149 goto finally;
150 for (attrs_len = 0, attrs_p = atts;
151 *attrs_p;
152 attrs_p++, attrs_len++) {
153 if (attrs_len % 2) {
154 rv = PyString_FromString(*attrs_p);
155 if (!rv) {
156 Py_DECREF(attrs_obj);
157 attrs_obj = NULL;
158 goto finally;
159 }
160 if (PyDict_SetItemString(attrs_obj,
161 (char*)*attrs_k, rv) < 0) {
162 Py_DECREF(attrs_obj);
163 attrs_obj = NULL;
164 goto finally;
165 }
166 Py_DECREF(rv);
167 }
168 else
169 attrs_k = attrs_p;
170 }
171 finally:
172 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000173}
Fred Drake85d835f2001-02-08 15:39:08 +0000174#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000175
176#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake85d835f2001-02-08 15:39:08 +0000177#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000178static PyObject *
179conv_atts_using_unicode(XML_Char **atts)
180{
Fred Drakeca1f4262000-09-21 20:10:23 +0000181 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000182 XML_Char **attrs_p, **attrs_k = NULL;
183 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000184
Fred Drake0582df92000-07-12 04:49:00 +0000185 if ((attrs_obj = PyDict_New()) == NULL)
186 goto finally;
187 for (attrs_len = 0, attrs_p = atts;
188 *attrs_p;
189 attrs_p++, attrs_len++) {
190 if (attrs_len % 2) {
191 PyObject *attr_str, *value_str;
192 const char *p = (const char *) (*attrs_k);
193 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
194 if (!attr_str) {
195 Py_DECREF(attrs_obj);
196 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000197 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000198 }
199 p = (const char *) *attrs_p;
200 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
201 if (!value_str) {
202 Py_DECREF(attrs_obj);
203 Py_DECREF(attr_str);
204 attrs_obj = NULL;
205 goto finally;
206 }
207 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
208 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000209 Py_DECREF(attr_str);
210 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000211 attrs_obj = NULL;
212 goto finally;
213 }
214 Py_DECREF(attr_str);
215 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000216 }
Fred Drake0582df92000-07-12 04:49:00 +0000217 else
218 attrs_k = attrs_p;
219 }
220 finally:
221 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222}
Fred Drake85d835f2001-02-08 15:39:08 +0000223#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000224
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000225/* Convert a string of XML_Chars into a Unicode string.
226 Returns None if str is a null pointer. */
227
Fred Drake0582df92000-07-12 04:49:00 +0000228static PyObject *
229conv_string_to_unicode(XML_Char *str)
230{
231 /* XXX currently this code assumes that XML_Char is 8-bit,
232 and hence in UTF-8. */
233 /* UTF-8 from Expat, Unicode desired */
234 if (str == NULL) {
235 Py_INCREF(Py_None);
236 return Py_None;
237 }
238 return PyUnicode_DecodeUTF8((const char *)str,
239 strlen((const char *)str),
240 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000241}
242
Fred Drake0582df92000-07-12 04:49:00 +0000243static PyObject *
244conv_string_len_to_unicode(const XML_Char *str, int len)
245{
246 /* XXX currently this code assumes that XML_Char is 8-bit,
247 and hence in UTF-8. */
248 /* UTF-8 from Expat, Unicode desired */
249 if (str == NULL) {
250 Py_INCREF(Py_None);
251 return Py_None;
252 }
Fred Drake6f987622000-08-25 18:03:30 +0000253 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000254}
255#endif
256
257/* Convert a string of XML_Chars into an 8-bit Python string.
258 Returns None if str is a null pointer. */
259
Fred Drake6f987622000-08-25 18:03:30 +0000260static PyObject *
261conv_string_to_utf8(XML_Char *str)
262{
263 /* XXX currently this code assumes that XML_Char is 8-bit,
264 and hence in UTF-8. */
265 /* UTF-8 from Expat, UTF-8 desired */
266 if (str == NULL) {
267 Py_INCREF(Py_None);
268 return Py_None;
269 }
270 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000271}
272
Fred Drake6f987622000-08-25 18:03:30 +0000273static PyObject *
274conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000275{
Fred Drake6f987622000-08-25 18:03:30 +0000276 /* XXX currently this code assumes that XML_Char is 8-bit,
277 and hence in UTF-8. */
278 /* UTF-8 from Expat, UTF-8 desired */
279 if (str == NULL) {
280 Py_INCREF(Py_None);
281 return Py_None;
282 }
283 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000284}
285
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000286/* Callback routines */
287
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000288static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000289
Fred Drake6f987622000-08-25 18:03:30 +0000290static void
291flag_error(xmlparseobject *self)
292{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000293 clear_handlers(self, 1);
294}
295
296static PyCodeObject*
297getcode(enum HandlerTypes slot, char* func_name, int lineno)
298{
Fred Drakebd6101c2001-02-14 18:29:45 +0000299 PyObject *code = NULL;
300 PyObject *name = NULL;
301 PyObject *nulltuple = NULL;
302 PyObject *filename = NULL;
303
304 if (handler_info[slot].tb_code == NULL) {
305 code = PyString_FromString("");
306 if (code == NULL)
307 goto failed;
308 name = PyString_FromString(func_name);
309 if (name == NULL)
310 goto failed;
311 nulltuple = PyTuple_New(0);
312 if (nulltuple == NULL)
313 goto failed;
314 filename = PyString_FromString(__FILE__);
315 handler_info[slot].tb_code =
316 PyCode_New(0, /* argcount */
317 0, /* nlocals */
318 0, /* stacksize */
319 0, /* flags */
320 code, /* code */
321 nulltuple, /* consts */
322 nulltuple, /* names */
323 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000324#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000325 nulltuple, /* freevars */
326 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000327#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000328 filename, /* filename */
329 name, /* name */
330 lineno, /* firstlineno */
331 code /* lnotab */
332 );
333 if (handler_info[slot].tb_code == NULL)
334 goto failed;
335 Py_DECREF(code);
336 Py_DECREF(nulltuple);
337 Py_DECREF(filename);
338 Py_DECREF(name);
339 }
340 return handler_info[slot].tb_code;
341 failed:
342 Py_XDECREF(code);
343 Py_XDECREF(name);
344 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000345}
346
347static PyObject*
348call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
349{
Fred Drakebd6101c2001-02-14 18:29:45 +0000350 PyThreadState *tstate = PyThreadState_GET();
351 PyFrameObject *f;
352 PyObject *res;
353
354 if (c == NULL)
355 return NULL;
356 f = PyFrame_New(
357 tstate, /*back*/
358 c, /*code*/
359 tstate->frame->f_globals, /*globals*/
360 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000361 );
362 if (f == NULL)
363 return NULL;
364 tstate->frame = f;
365 res = PyEval_CallObject(func, args);
366 if (res == NULL && tstate->curexc_traceback == NULL)
367 PyTraceBack_Here(f);
368 tstate->frame = f->f_back;
369 Py_DECREF(f);
370 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000371}
372
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000373#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
374#define STRING_CONV_FUNC conv_string_to_utf8
375#else
376/* Python 1.6 and later versions */
377#define STRING_CONV_FUNC (self->returns_unicode \
378 ? conv_string_to_unicode : conv_string_to_utf8)
379#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000380
Fred Drake85d835f2001-02-08 15:39:08 +0000381static void
382my_StartElementHandler(void *userData,
383 const XML_Char *name, const XML_Char **atts)
384{
385 xmlparseobject *self = (xmlparseobject *)userData;
386
387 if (self->handlers[StartElement]
388 && self->handlers[StartElement] != Py_None) {
389 PyObject *container, *rv, *args;
390 int i, max;
391
392 /* Set max to the number of slots filled in atts[]; max/2 is
393 * the number of attributes we need to process.
394 */
395 if (self->specified_attributes) {
396 max = XML_GetSpecifiedAttributeCount(self->itself);
397 }
398 else {
399 max = 0;
400 while (atts[max] != NULL)
401 max += 2;
402 }
403 /* Build the container. */
404 if (self->ordered_attributes)
405 container = PyList_New(max);
406 else
407 container = PyDict_New();
408 if (container == NULL) {
409 flag_error(self);
410 return;
411 }
412 for (i = 0; i < max; i += 2) {
413 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
414 PyObject *v;
415 if (n == NULL) {
416 flag_error(self);
417 Py_DECREF(container);
418 return;
419 }
420 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
421 if (v == NULL) {
422 flag_error(self);
423 Py_DECREF(container);
424 Py_DECREF(n);
425 return;
426 }
427 if (self->ordered_attributes) {
428 PyList_SET_ITEM(container, i, n);
429 PyList_SET_ITEM(container, i+1, v);
430 }
431 else if (PyDict_SetItem(container, n, v)) {
432 flag_error(self);
433 Py_DECREF(n);
434 Py_DECREF(v);
435 return;
436 }
437 else {
438 Py_DECREF(n);
439 Py_DECREF(v);
440 }
441 }
442 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
443 if (args == NULL) {
444 Py_DECREF(container);
445 return;
446 }
447 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000448 self->in_callback = 1;
449 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000450 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000451 self->in_callback = 0;
452 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000453 if (rv == NULL) {
454 flag_error(self);
455 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000456 }
Fred Drake85d835f2001-02-08 15:39:08 +0000457 Py_DECREF(rv);
458 }
459}
460
461#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
462 RETURN, GETUSERDATA) \
463static RC \
464my_##NAME##Handler PARAMS {\
465 xmlparseobject *self = GETUSERDATA ; \
466 PyObject *args = NULL; \
467 PyObject *rv = NULL; \
468 INIT \
469\
470 if (self->handlers[NAME] \
471 && self->handlers[NAME] != Py_None) { \
472 args = Py_BuildValue PARAM_FORMAT ;\
473 if (!args) \
474 return RETURN; \
Fred Drakebd6101c2001-02-14 18:29:45 +0000475 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000476 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
477 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000478 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000479 Py_DECREF(args); \
480 if (rv == NULL) { \
481 flag_error(self); \
482 return RETURN; \
483 } \
484 CONVERSION \
485 Py_DECREF(rv); \
486 } \
487 return RETURN; \
488}
489
Fred Drake6f987622000-08-25 18:03:30 +0000490#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
491 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
492 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000493
Fred Drake6f987622000-08-25 18:03:30 +0000494#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
495 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
496 rc = PyInt_AsLong(rv);, rc, \
497 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000498
Fred Drake6f987622000-08-25 18:03:30 +0000499VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000500 (void *userData, const XML_Char *name),
501 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000502
Fred Drake6f987622000-08-25 18:03:30 +0000503VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000504 (void *userData,
505 const XML_Char *target,
506 const XML_Char *data),
507 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000508
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000509#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000510VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000511 (void *userData, const XML_Char *data, int len),
512 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000513#else
Fred Drake6f987622000-08-25 18:03:30 +0000514VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000515 (void *userData, const XML_Char *data, int len),
516 ("(N)", (self->returns_unicode
517 ? conv_string_len_to_unicode(data,len)
518 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000519#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000520
Fred Drake6f987622000-08-25 18:03:30 +0000521VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000522 (void *userData,
523 const XML_Char *entityName,
524 const XML_Char *base,
525 const XML_Char *systemId,
526 const XML_Char *publicId,
527 const XML_Char *notationName),
528 ("(O&O&O&O&O&)",
529 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
530 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
531 STRING_CONV_FUNC,notationName))
532
533#if EXPAT_VERSION >= 0x015f00
534#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
535VOID_HANDLER(EntityDecl,
536 (void *userData,
537 const XML_Char *entityName,
538 int is_parameter_entity,
539 const XML_Char *value,
540 int value_length,
541 const XML_Char *base,
542 const XML_Char *systemId,
543 const XML_Char *publicId,
544 const XML_Char *notationName),
545 ("O&iNO&O&O&O&",
546 STRING_CONV_FUNC,entityName, is_parameter_entity,
547 conv_string_len_to_utf8(value, value_length),
548 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
549 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
550#else
551VOID_HANDLER(EntityDecl,
552 (void *userData,
553 const XML_Char *entityName,
554 int is_parameter_entity,
555 const XML_Char *value,
556 int value_length,
557 const XML_Char *base,
558 const XML_Char *systemId,
559 const XML_Char *publicId,
560 const XML_Char *notationName),
561 ("O&iNO&O&O&O&",
562 STRING_CONV_FUNC,entityName, is_parameter_entity,
563 (self->returns_unicode
564 ? conv_string_len_to_unicode(value, value_length)
565 : conv_string_len_to_utf8(value, value_length)),
566 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
567 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
568#endif
569
570VOID_HANDLER(XmlDecl,
571 (void *userData,
572 const XML_Char *version,
573 const XML_Char *encoding,
574 int standalone),
575 ("(O&O&i)",
576 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
577 standalone))
578
579static PyObject *
580conv_content_model(XML_Content * const model,
581 PyObject *(*conv_string)(XML_Char *))
582{
583 PyObject *result = NULL;
584 PyObject *children = PyTuple_New(model->numchildren);
585 int i;
586
587 if (children != NULL) {
588 for (i = 0; i < model->numchildren; ++i) {
589 PyObject *child = conv_content_model(&model->children[i],
590 conv_string);
591 if (child == NULL) {
592 Py_XDECREF(children);
593 return NULL;
594 }
595 PyTuple_SET_ITEM(children, i, child);
596 }
597 result = Py_BuildValue("(iiO&N)",
598 model->type, model->quant,
599 conv_string,model->name, children);
600 }
601 return result;
602}
603
604static PyObject *
605conv_content_model_utf8(XML_Content * const model)
606{
607 return conv_content_model(model, conv_string_to_utf8);
608}
609
610#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
611static PyObject *
612conv_content_model_unicode(XML_Content * const model)
613{
614 return conv_content_model(model, conv_string_to_unicode);
615}
616
617VOID_HANDLER(ElementDecl,
618 (void *userData,
619 const XML_Char *name,
620 XML_Content *model),
621 ("O&O&",
622 STRING_CONV_FUNC,name,
623 (self->returns_unicode ? conv_content_model_unicode
624 : conv_content_model_utf8),model))
625#else
626VOID_HANDLER(ElementDecl,
627 (void *userData,
628 const XML_Char *name,
629 XML_Content *model),
630 ("O&O&",
631 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
632#endif
633
634VOID_HANDLER(AttlistDecl,
635 (void *userData,
636 const XML_Char *elname,
637 const XML_Char *attname,
638 const XML_Char *att_type,
639 const XML_Char *dflt,
640 int isrequired),
641 ("(O&O&O&O&i)",
642 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
643 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
644 isrequired))
645#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000646
Fred Drake6f987622000-08-25 18:03:30 +0000647VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000648 (void *userData,
649 const XML_Char *notationName,
650 const XML_Char *base,
651 const XML_Char *systemId,
652 const XML_Char *publicId),
653 ("(O&O&O&O&)",
654 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
655 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656
Fred Drake6f987622000-08-25 18:03:30 +0000657VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000658 (void *userData,
659 const XML_Char *prefix,
660 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000661 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000662
Fred Drake6f987622000-08-25 18:03:30 +0000663VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000664 (void *userData,
665 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000666 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000667
Fred Drake6f987622000-08-25 18:03:30 +0000668VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000669 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000670 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000671
Fred Drake6f987622000-08-25 18:03:30 +0000672VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000673 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000674 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000675
Fred Drake6f987622000-08-25 18:03:30 +0000676VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000677 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000678 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000679
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000680#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000681VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000682 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000683 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000684
Fred Drake6f987622000-08-25 18:03:30 +0000685VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000686 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000687 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000688#else
Fred Drake6f987622000-08-25 18:03:30 +0000689VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000690 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000691 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000692 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000693 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694
Fred Drake6f987622000-08-25 18:03:30 +0000695VOID_HANDLER(DefaultHandlerExpand,
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#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000701
Fred Drake6f987622000-08-25 18:03:30 +0000702INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000703 (void *userData),
704 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000705
Fred Drake6f987622000-08-25 18:03:30 +0000706RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000707 (XML_Parser parser,
708 const XML_Char *context,
709 const XML_Char *base,
710 const XML_Char *systemId,
711 const XML_Char *publicId),
712 int rc=0;,
713 ("(O&O&O&O&)",
714 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000715 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
716 rc = PyInt_AsLong(rv);, rc,
717 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000719/* XXX UnknownEncodingHandler */
720
Fred Drake85d835f2001-02-08 15:39:08 +0000721#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000722VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000723 (void *userData, const XML_Char *doctypeName),
724 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
725 Py_None, Py_None, -1))
726#elif EXPAT_VERSION >= 0x015f00
727VOID_HANDLER(StartDoctypeDecl,
728 (void *userData, const XML_Char *doctypeName,
729 const XML_Char *sysid, const XML_Char *pubid,
730 int has_internal_subset),
731 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
732 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
733 has_internal_subset))
734#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000735
Fred Drake85d835f2001-02-08 15:39:08 +0000736#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000737VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000738#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000739
Fred Drake85d835f2001-02-08 15:39:08 +0000740#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000741VOID_HANDLER(ExternalParsedEntityDecl,
742 (void *userData, const XML_Char *entityName,
743 const XML_Char *base, const XML_Char *systemId,
744 const XML_Char *publicId),
745 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
746 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
747 STRING_CONV_FUNC, publicId))
748
749VOID_HANDLER(InternalParsedEntityDecl,
750 (void *userData, const XML_Char *entityName,
751 const XML_Char *replacementText, int replacementTextLength),
752 ("(O&O&i)", STRING_CONV_FUNC, entityName,
753 STRING_CONV_FUNC, replacementText, replacementTextLength))
754
Fred Drake85d835f2001-02-08 15:39:08 +0000755#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000757/* ---------------------------------------------------------------- */
758
759static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000760"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000761Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762
763static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000764xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000765{
Fred Drake0582df92000-07-12 04:49:00 +0000766 char *s;
767 int slen;
768 int isFinal = 0;
769 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770
Fred Drake0582df92000-07-12 04:49:00 +0000771 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
772 return NULL;
773 rv = XML_Parse(self->itself, s, slen, isFinal);
774 if (PyErr_Occurred()) {
775 return NULL;
776 }
777 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000778 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000779 }
780 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000781}
782
Fred Drakeca1f4262000-09-21 20:10:23 +0000783/* File reading copied from cPickle */
784
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000785#define BUF_SIZE 2048
786
Fred Drake0582df92000-07-12 04:49:00 +0000787static int
788readinst(char *buf, int buf_size, PyObject *meth)
789{
790 PyObject *arg = NULL;
791 PyObject *bytes = NULL;
792 PyObject *str = NULL;
793 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000794
Fred Drake676940b2000-09-22 15:21:31 +0000795 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000796 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000797
Fred Drakeca1f4262000-09-21 20:10:23 +0000798 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000799 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000800
Tim Peters954eef72000-09-22 06:01:11 +0000801 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000802
Fred Drakeca1f4262000-09-21 20:10:23 +0000803 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000804 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000805
Fred Drake0582df92000-07-12 04:49:00 +0000806 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000807 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000808 PyErr_Format(PyExc_TypeError,
809 "read() did not return a string object (type=%.400s)",
810 str->ob_type->tp_name);
811 goto finally;
812 }
813 len = PyString_GET_SIZE(str);
814 if (len > buf_size) {
815 PyErr_Format(PyExc_ValueError,
816 "read() returned too much data: "
817 "%i bytes requested, %i returned",
818 buf_size, len);
819 Py_DECREF(str);
820 goto finally;
821 }
822 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823finally:
Fred Drake0582df92000-07-12 04:49:00 +0000824 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000825 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000826 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000827}
828
829static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000830"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000831Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000832
833static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000834xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000835{
Fred Drake0582df92000-07-12 04:49:00 +0000836 int rv = 1;
837 PyObject *f;
838 FILE *fp;
839 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840
Fred Drake0582df92000-07-12 04:49:00 +0000841 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
842 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000843
Fred Drake0582df92000-07-12 04:49:00 +0000844 if (PyFile_Check(f)) {
845 fp = PyFile_AsFile(f);
846 }
847 else{
848 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000849 readmethod = PyObject_GetAttrString(f, "read");
850 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000851 PyErr_Clear();
852 PyErr_SetString(PyExc_TypeError,
853 "argument must have 'read' attribute");
854 return 0;
855 }
856 }
857 for (;;) {
858 int bytes_read;
859 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
860 if (buf == NULL)
861 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000862
Fred Drake0582df92000-07-12 04:49:00 +0000863 if (fp) {
864 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
865 if (bytes_read < 0) {
866 PyErr_SetFromErrno(PyExc_IOError);
867 return NULL;
868 }
869 }
870 else {
871 bytes_read = readinst(buf, BUF_SIZE, readmethod);
872 if (bytes_read < 0)
873 return NULL;
874 }
875 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
876 if (PyErr_Occurred())
877 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878
Fred Drake0582df92000-07-12 04:49:00 +0000879 if (!rv || bytes_read == 0)
880 break;
881 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000882 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000883 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000884 }
Fred Drake0582df92000-07-12 04:49:00 +0000885 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886}
887
888static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000889"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000890Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891
892static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000893xmlparse_SetBase(xmlparseobject *self, PyObject *args)
894{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000895 char *base;
896
Fred Drake0582df92000-07-12 04:49:00 +0000897 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000898 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000899 if (!XML_SetBase(self->itself, base)) {
900 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000901 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902 Py_INCREF(Py_None);
903 return Py_None;
904}
905
906static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000907"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000908Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000909
910static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000911xmlparse_GetBase(xmlparseobject *self, PyObject *args)
912{
913 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000914 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915
Fred Drake0582df92000-07-12 04:49:00 +0000916 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000917}
918
Fred Drakebd6101c2001-02-14 18:29:45 +0000919#if EXPAT_VERSION >= 0x015f00
920static char xmlparse_GetInputContext__doc__[] =
921"GetInputContext() -> string\n\
922Return the untranslated text of the input that caused the current event.\n\
923If the event was generated by a large amount of text (such as a start tag\n\
924for an element with many attributes), not all of the text may be available.";
925
926static PyObject *
927xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
928{
929 PyObject *result = NULL;
930
931 if (PyArg_ParseTuple(args, ":GetInputContext")) {
932 if (self->in_callback) {
933 int offset, size;
934 const char *buffer
935 = XML_GetInputContext(self->itself, &offset, &size);
936
937 if (buffer != NULL)
938 result = PyString_FromStringAndSize(buffer + offset, size);
939 else {
940 result = Py_None;
941 Py_INCREF(result);
942 }
943 }
944 else {
945 result = Py_None;
946 Py_INCREF(result);
947 }
948 }
949 return result;
950}
951#endif
952
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000953static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000954"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000955Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000956information passed to the ExternalEntityRefHandler.";
957
958static PyObject *
959xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
960{
961 char *context;
962 char *encoding = NULL;
963 xmlparseobject *new_parser;
964 int i;
965
966 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
967 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000968 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000969 }
970
971#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
972 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000973#else
Fred Drake85d835f2001-02-08 15:39:08 +0000974 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000975 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000976#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000977
978 if (new_parser == NULL)
979 return NULL;
980 new_parser->returns_unicode = self->returns_unicode;
981 new_parser->ordered_attributes = self->ordered_attributes;
982 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000983 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000984 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000985 encoding);
986 new_parser->handlers = 0;
987 PyObject_GC_Init(new_parser);
988
989 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000990 Py_DECREF(new_parser);
991 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000992 }
993
994 XML_SetUserData(new_parser->itself, (void *)new_parser);
995
996 /* allocate and clear handlers first */
997 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000998 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000999
1000 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001001 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001002 Py_DECREF(new_parser);
1003 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001004 }
1005 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001006
1007 /* then copy handlers from self */
1008 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001009 if (self->handlers[i]) {
1010 Py_INCREF(self->handlers[i]);
1011 new_parser->handlers[i] = self->handlers[i];
1012 handler_info[i].setter(new_parser->itself,
1013 handler_info[i].handler);
1014 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001015 }
Fred Drake28adf522000-09-24 22:07:59 +00001016 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001017}
1018
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001019#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001020
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001021static char xmlparse_SetParamEntityParsing__doc__[] =
1022"SetParamEntityParsing(flag) -> success\n\
1023Controls parsing of parameter entities (including the external DTD\n\
1024subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1025XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1026XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1027was successful.";
1028
1029static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001030xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001031{
Fred Drake85d835f2001-02-08 15:39:08 +00001032 int flag;
1033 if (!PyArg_ParseTuple(args, "i", &flag))
1034 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001035 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001036 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001037}
1038
Fred Drake85d835f2001-02-08 15:39:08 +00001039#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001040
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001041static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001042 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001043 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001044 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001045 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001046 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001047 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001048 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001049 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001050 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1051 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001052#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001053 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1054 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1055#endif
1056#if EXPAT_VERSION >= 0x015f00
1057 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1058 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001059#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001060 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001061};
1062
1063/* ---------- */
1064
1065
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001066#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1067
1068/*
1069 pyexpat international encoding support.
1070 Make it as simple as possible.
1071*/
1072
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001073static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001074PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001075
1076static void
1077init_template_buffer(void)
1078{
1079 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001080 for (i = 0; i < 256; i++) {
1081 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001082 }
Fred Drakebb66a202001-03-01 20:48:17 +00001083 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001084}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001085
1086int
1087PyUnknownEncodingHandler(void *encodingHandlerData,
1088const XML_Char *name,
1089XML_Encoding * info)
1090{
Fred Drakebb66a202001-03-01 20:48:17 +00001091 PyUnicodeObject *_u_string = NULL;
1092 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001093 int i;
1094
Fred Drakebb66a202001-03-01 20:48:17 +00001095 /* Yes, supports only 8bit encodings */
1096 _u_string = (PyUnicodeObject *)
1097 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001098
Fred Drakebb66a202001-03-01 20:48:17 +00001099 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001100 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101
Fred Drakebb66a202001-03-01 20:48:17 +00001102 for (i = 0; i < 256; i++) {
1103 /* Stupid to access directly, but fast */
1104 Py_UNICODE c = _u_string->str[i];
1105 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001106 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001107 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001109 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110
1111 info->data = NULL;
1112 info->convert = NULL;
1113 info->release = NULL;
1114 result=1;
1115
1116 Py_DECREF(_u_string);
1117 return result;
1118}
1119
1120#endif
1121
1122static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001123newxmlparseobject(char *encoding, char *namespace_separator)
1124{
1125 int i;
1126 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001127
1128#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001129 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1130 if (self == NULL)
1131 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001132
Fred Drake0582df92000-07-12 04:49:00 +00001133 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001134#else
Fred Drake0582df92000-07-12 04:49:00 +00001135 /* Code for versions 1.6 and later */
1136 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1137 if (self == NULL)
1138 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001139
Fred Drake0582df92000-07-12 04:49:00 +00001140 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001141#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001142 self->ordered_attributes = 0;
1143 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001144 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001145 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001146 if (namespace_separator) {
1147 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1148 }
Fred Drake85d835f2001-02-08 15:39:08 +00001149 else {
Fred Drake0582df92000-07-12 04:49:00 +00001150 self->itself = XML_ParserCreate(encoding);
1151 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001152 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001153 if (self->itself == NULL) {
1154 PyErr_SetString(PyExc_RuntimeError,
1155 "XML_ParserCreate failed");
1156 Py_DECREF(self);
1157 return NULL;
1158 }
1159 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001160#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1161#else
1162 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1163#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001164
Fred Drake0582df92000-07-12 04:49:00 +00001165 for(i = 0; handler_info[i].name != NULL; i++)
1166 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001167
Fred Drake0582df92000-07-12 04:49:00 +00001168 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001169 if (!self->handlers){
1170 Py_DECREF(self);
1171 return PyErr_NoMemory();
1172 }
1173 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001174
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001175 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001176}
1177
1178
1179static void
Fred Drake0582df92000-07-12 04:49:00 +00001180xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001181{
Fred Drake0582df92000-07-12 04:49:00 +00001182 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001184 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001185 XML_ParserFree(self->itself);
1186 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001187
Fred Drake85d835f2001-02-08 15:39:08 +00001188 if (self->handlers != NULL) {
1189 for (i = 0; handler_info[i].name != NULL; i++) {
1190 Py_XDECREF(self->handlers[i]);
1191 }
1192 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001193 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001194#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001195 /* Code for versions before 1.6 */
1196 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001197#else
Fred Drake0582df92000-07-12 04:49:00 +00001198 /* Code for versions 1.6 and later */
1199 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001200#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001201}
1202
Fred Drake0582df92000-07-12 04:49:00 +00001203static int
1204handlername2int(const char *name)
1205{
1206 int i;
1207 for (i=0; handler_info[i].name != NULL; i++) {
1208 if (strcmp(name, handler_info[i].name) == 0) {
1209 return i;
1210 }
1211 }
1212 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001213}
1214
1215static PyObject *
1216xmlparse_getattr(xmlparseobject *self, char *name)
1217{
Fred Drake0582df92000-07-12 04:49:00 +00001218 int handlernum;
1219 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001220 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001221 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001222 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001223 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001224 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001225 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001226 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1227 if (strcmp(name, "ordered_attributes") == 0)
1228 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001230 return PyInt_FromLong((long) self->returns_unicode);
1231 if (strcmp(name, "specified_attributes") == 0)
1232 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001233
Fred Drake0582df92000-07-12 04:49:00 +00001234 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001235
Fred Drake0582df92000-07-12 04:49:00 +00001236 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1237 Py_INCREF(self->handlers[handlernum]);
1238 return self->handlers[handlernum];
1239 }
1240 if (strcmp(name, "__members__") == 0) {
1241 int i;
1242 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001243 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001244 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001245 }
1246 PyList_Append(rc, PyString_FromString("ErrorCode"));
1247 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1248 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1249 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001250 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001251 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001252 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001253
Fred Drake0582df92000-07-12 04:49:00 +00001254 return rc;
1255 }
1256 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001257}
1258
Fred Drake6f987622000-08-25 18:03:30 +00001259static int
1260sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001261{
1262 int handlernum = handlername2int(name);
1263 if (handlernum != -1) {
1264 Py_INCREF(v);
1265 Py_XDECREF(self->handlers[handlernum]);
1266 self->handlers[handlernum] = v;
1267 handler_info[handlernum].setter(self->itself,
1268 handler_info[handlernum].handler);
1269 return 1;
1270 }
1271 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272}
1273
1274static int
Fred Drake6f987622000-08-25 18:03:30 +00001275xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001276{
Fred Drake6f987622000-08-25 18:03:30 +00001277 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001278 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001279 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1280 return -1;
1281 }
Fred Drake85d835f2001-02-08 15:39:08 +00001282 if (strcmp(name, "ordered_attributes") == 0) {
1283 if (PyObject_IsTrue(v))
1284 self->ordered_attributes = 1;
1285 else
1286 self->ordered_attributes = 0;
1287 return 0;
1288 }
Fred Drake6f987622000-08-25 18:03:30 +00001289 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001290 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001291#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001292 PyErr_SetString(PyExc_ValueError,
1293 "Cannot return Unicode strings in Python 1.5");
1294 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001295#else
Fred Drake6f987622000-08-25 18:03:30 +00001296 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001297#endif
Fred Drake6f987622000-08-25 18:03:30 +00001298 }
1299 else
1300 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001301 return 0;
1302 }
1303 if (strcmp(name, "specified_attributes") == 0) {
1304 if (PyObject_IsTrue(v))
1305 self->specified_attributes = 1;
1306 else
1307 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001308 return 0;
1309 }
1310 if (sethandler(self, name, v)) {
1311 return 0;
1312 }
1313 PyErr_SetString(PyExc_AttributeError, name);
1314 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001315}
1316
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001317#ifdef WITH_CYCLE_GC
1318static int
1319xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1320{
1321 int i, err;
1322 for (i = 0; handler_info[i].name != NULL; i++) {
1323 if (!op->handlers[i])
1324 continue;
1325 err = visit(op->handlers[i], arg);
1326 if (err)
1327 return err;
1328 }
1329 return 0;
1330}
1331
1332static int
1333xmlparse_clear(xmlparseobject *op)
1334{
1335 clear_handlers(op, 1);
1336 return 0;
1337}
1338#endif
1339
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001340static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001341"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001342
1343static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001344 PyObject_HEAD_INIT(NULL)
1345 0, /*ob_size*/
1346 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001347 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001348 0, /*tp_itemsize*/
1349 /* methods */
1350 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1351 (printfunc)0, /*tp_print*/
1352 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1353 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1354 (cmpfunc)0, /*tp_compare*/
1355 (reprfunc)0, /*tp_repr*/
1356 0, /*tp_as_number*/
1357 0, /*tp_as_sequence*/
1358 0, /*tp_as_mapping*/
1359 (hashfunc)0, /*tp_hash*/
1360 (ternaryfunc)0, /*tp_call*/
1361 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001362 0, /* tp_getattro */
1363 0, /* tp_setattro */
1364 0, /* tp_as_buffer */
1365 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1366 Xmlparsetype__doc__, /* Documentation string */
1367#ifdef WITH_CYCLE_GC
1368 (traverseproc)xmlparse_traverse, /* tp_traverse */
1369 (inquiry)xmlparse_clear /* tp_clear */
1370#else
1371 0, 0
1372#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001373};
1374
1375/* End of code for xmlparser objects */
1376/* -------------------------------------------------------- */
1377
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001378static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001379"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1380Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001381
1382static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001383pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1384{
1385 char *encoding = NULL;
1386 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001387 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001388
Fred Drake0582df92000-07-12 04:49:00 +00001389 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001390 &encoding, &namespace_separator))
1391 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001392 if (namespace_separator != NULL
1393 && strlen(namespace_separator) != 1) {
1394 PyErr_SetString(PyExc_ValueError,
1395 "namespace_separator must be one character,"
1396 " omitted, or None");
1397 return NULL;
1398 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001399 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001400}
1401
1402static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001403"ErrorString(errno) -> string\n\
1404Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001405
1406static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001407pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001408{
Fred Drake0582df92000-07-12 04:49:00 +00001409 long code = 0;
1410
1411 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1412 return NULL;
1413 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001414}
1415
1416/* List of methods defined in the module */
1417
1418static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001419 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1420 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1421 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1422 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423
Fred Drake0582df92000-07-12 04:49:00 +00001424 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001425};
1426
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001427/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001428
1429static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001430"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001432/* Initialization function for the module */
1433
Fred Drake93adb692000-09-23 04:55:48 +00001434void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001435
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001436#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001437
1438/* 1.5 compatibility: PyModule_AddObject */
1439static int
1440PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1441{
1442 PyObject *dict;
1443 if (!PyModule_Check(m) || o == NULL)
1444 return -1;
1445 dict = PyModule_GetDict(m);
1446 if (dict == NULL)
1447 return -1;
1448 if (PyDict_SetItemString(dict, name, o))
1449 return -1;
1450 Py_DECREF(o);
1451 return 0;
1452}
1453
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001454int
1455PyModule_AddIntConstant(PyObject *m, char *name, long value)
1456{
1457 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1458}
1459
Fred Drakea77254a2000-09-29 19:23:29 +00001460static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001461PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1462{
1463 return PyModule_AddObject(m, name, PyString_FromString(value));
1464}
1465
1466#endif
1467
Fred Drake4113b132001-03-24 19:58:26 +00001468
1469/* Return a Python string that represents the version number without the
1470 * extra cruft added by revision control, even if the right options were
1471 * given to the "cvs export" command to make it not include the extra
1472 * cruft.
1473 */
1474static PyObject *
1475get_version_string(void)
1476{
1477 static char *rcsid = "$Revision$";
1478 char *rev = rcsid;
1479 int i = 0;
1480
1481 while (!isdigit(*rev))
1482 ++rev;
1483 while (rev[i] != ' ' && rev[i] != '\0')
1484 ++i;
1485
1486 return PyString_FromStringAndSize(rev, i);
1487}
1488
Fred Drake6f987622000-08-25 18:03:30 +00001489DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001490initpyexpat(void)
1491{
1492 PyObject *m, *d;
Fred Drake6f987622000-08-25 18:03:30 +00001493 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001494 PyObject *errors_module;
1495 PyObject *modelmod_name;
1496 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001497 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001498
Fred Drake6f987622000-08-25 18:03:30 +00001499 if (errmod_name == NULL)
1500 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001501 modelmod_name = PyString_FromString("pyexpat.model");
1502 if (modelmod_name == NULL)
1503 return;
Fred Drake6f987622000-08-25 18:03:30 +00001504
Fred Drake0582df92000-07-12 04:49:00 +00001505 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001506
Fred Drake0582df92000-07-12 04:49:00 +00001507 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001508 m = Py_InitModule3("pyexpat", pyexpat_methods,
1509 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001510
Fred Drake0582df92000-07-12 04:49:00 +00001511 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001512 if (ErrorObject == NULL) {
1513 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001514 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001515 if (ErrorObject == NULL)
1516 return;
1517 }
1518 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001519 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001520 Py_INCREF(ErrorObject);
1521 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001522 Py_INCREF(&Xmlparsetype);
1523 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001524
Fred Drake4113b132001-03-24 19:58:26 +00001525 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001526#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001527 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1528 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001529 {
1530 XML_Expat_Version info = XML_ExpatVersionInfo();
1531 PyModule_AddObject(m, "version_info",
1532 Py_BuildValue("(iii)", info.major,
1533 info.minor, info.micro));
1534 }
Fred Drake738293d2000-12-21 17:25:07 +00001535#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001536#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1537#else
1538 init_template_buffer();
1539#endif
Fred Drake0582df92000-07-12 04:49:00 +00001540 /* XXX When Expat supports some way of figuring out how it was
1541 compiled, this should check and set native_encoding
1542 appropriately.
1543 */
Fred Drake93adb692000-09-23 04:55:48 +00001544 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001545
Fred Drake85d835f2001-02-08 15:39:08 +00001546 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001547 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001548 errors_module = PyDict_GetItem(d, errmod_name);
1549 if (errors_module == NULL) {
1550 errors_module = PyModule_New("pyexpat.errors");
1551 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001552 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001553 /* gives away the reference to errors_module */
1554 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001555 }
1556 }
Fred Drake6f987622000-08-25 18:03:30 +00001557 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001558 model_module = PyDict_GetItem(d, modelmod_name);
1559 if (model_module == NULL) {
1560 model_module = PyModule_New("pyexpat.model");
1561 if (model_module != NULL) {
1562 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1563 /* gives away the reference to model_module */
1564 PyModule_AddObject(m, "model", model_module);
1565 }
1566 }
1567 Py_DECREF(modelmod_name);
1568 if (errors_module == NULL || model_module == NULL)
1569 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001570 return;
1571
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001572#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001573 PyModule_AddStringConstant(errors_module, #name, \
1574 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001575
Fred Drake0582df92000-07-12 04:49:00 +00001576 MYCONST(XML_ERROR_NO_MEMORY);
1577 MYCONST(XML_ERROR_SYNTAX);
1578 MYCONST(XML_ERROR_NO_ELEMENTS);
1579 MYCONST(XML_ERROR_INVALID_TOKEN);
1580 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1581 MYCONST(XML_ERROR_PARTIAL_CHAR);
1582 MYCONST(XML_ERROR_TAG_MISMATCH);
1583 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1584 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1585 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1586 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1587 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1588 MYCONST(XML_ERROR_ASYNC_ENTITY);
1589 MYCONST(XML_ERROR_BAD_CHAR_REF);
1590 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1591 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1592 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1593 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1594 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001595 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1596 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1597 MYCONST(XML_ERROR_NOT_STANDALONE);
1598
Fred Drake85d835f2001-02-08 15:39:08 +00001599 PyModule_AddStringConstant(errors_module, "__doc__",
1600 "Constants used to describe error conditions.");
1601
Fred Drake93adb692000-09-23 04:55:48 +00001602#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001603
1604#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001605#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001606 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1607 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1608 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001609#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001610#endif
1611
Fred Drake85d835f2001-02-08 15:39:08 +00001612#if EXPAT_VERSION >= 0x015f00
1613#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1614 PyModule_AddStringConstant(model_module, "__doc__",
1615 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001616
Fred Drake85d835f2001-02-08 15:39:08 +00001617 MYCONST(XML_CTYPE_EMPTY);
1618 MYCONST(XML_CTYPE_ANY);
1619 MYCONST(XML_CTYPE_MIXED);
1620 MYCONST(XML_CTYPE_NAME);
1621 MYCONST(XML_CTYPE_CHOICE);
1622 MYCONST(XML_CTYPE_SEQ);
1623
1624 MYCONST(XML_CQUANT_NONE);
1625 MYCONST(XML_CQUANT_OPT);
1626 MYCONST(XML_CQUANT_REP);
1627 MYCONST(XML_CQUANT_PLUS);
1628#undef MYCONST
1629#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001630}
1631
Fred Drake6f987622000-08-25 18:03:30 +00001632static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001633clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001634{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001635 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001636
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001637 for (; handler_info[i].name!=NULL; i++) {
1638 if (decref){
1639 Py_XDECREF(self->handlers[i]);
1640 }
1641 self->handlers[i]=NULL;
1642 handler_info[i].setter(self->itself, NULL);
1643 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001644}
1645
Fred Drake6f987622000-08-25 18:03:30 +00001646typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001647
Fred Drake6f987622000-08-25 18:03:30 +00001648static void
1649pyxml_UpdatePairedHandlers(xmlparseobject *self,
1650 int startHandler,
1651 int endHandler,
1652 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001653{
1654 void *start_handler=NULL;
1655 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001656
Fred Drake0582df92000-07-12 04:49:00 +00001657 if (self->handlers[startHandler]
1658 && self->handlers[endHandler]!=Py_None) {
1659 start_handler=handler_info[startHandler].handler;
1660 }
1661 if (self->handlers[EndElement]
1662 && self->handlers[EndElement] !=Py_None) {
1663 end_handler=handler_info[endHandler].handler;
1664 }
1665 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001666}
1667
Fred Drake6f987622000-08-25 18:03:30 +00001668static void
1669pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001670{
1671 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1672 StartElement, EndElement,
1673 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001674}
1675
Fred Drake6f987622000-08-25 18:03:30 +00001676static void
1677pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001678{
1679 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1680 StartElement, EndElement,
1681 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001682}
1683
Fred Drake6f987622000-08-25 18:03:30 +00001684static void
1685pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001686{
1687 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1688 StartNamespaceDecl, EndNamespaceDecl,
1689 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001690}
1691
Fred Drake6f987622000-08-25 18:03:30 +00001692static void
1693pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001694{
1695 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1696 StartNamespaceDecl, EndNamespaceDecl,
1697 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001698}
1699
Fred Drake6f987622000-08-25 18:03:30 +00001700static void
1701pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001702{
1703 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1704 StartCdataSection, EndCdataSection,
1705 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001706}
1707
Fred Drake6f987622000-08-25 18:03:30 +00001708static void
1709pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001710{
1711 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1712 StartCdataSection, EndCdataSection,
1713 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001714}
1715
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001716#if EXPAT_VERSION >= 0x010200
1717
1718static void
1719pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1720{
1721 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1722 StartDoctypeDecl, EndDoctypeDecl,
1723 (pairsetter)XML_SetDoctypeDeclHandler);
1724}
1725
1726static void
1727pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1728{
1729 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1730 StartDoctypeDecl, EndDoctypeDecl,
1731 (pairsetter)XML_SetDoctypeDeclHandler);
1732}
1733
1734#endif
1735
Fred Drake0582df92000-07-12 04:49:00 +00001736statichere struct HandlerInfo handler_info[] = {
1737 {"StartElementHandler",
1738 pyxml_SetStartElementHandler,
1739 (xmlhandler)my_StartElementHandler},
1740 {"EndElementHandler",
1741 pyxml_SetEndElementHandler,
1742 (xmlhandler)my_EndElementHandler},
1743 {"ProcessingInstructionHandler",
1744 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1745 (xmlhandler)my_ProcessingInstructionHandler},
1746 {"CharacterDataHandler",
1747 (xmlhandlersetter)XML_SetCharacterDataHandler,
1748 (xmlhandler)my_CharacterDataHandler},
1749 {"UnparsedEntityDeclHandler",
1750 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1751 (xmlhandler)my_UnparsedEntityDeclHandler },
1752 {"NotationDeclHandler",
1753 (xmlhandlersetter)XML_SetNotationDeclHandler,
1754 (xmlhandler)my_NotationDeclHandler },
1755 {"StartNamespaceDeclHandler",
1756 pyxml_SetStartNamespaceDeclHandler,
1757 (xmlhandler)my_StartNamespaceDeclHandler },
1758 {"EndNamespaceDeclHandler",
1759 pyxml_SetEndNamespaceDeclHandler,
1760 (xmlhandler)my_EndNamespaceDeclHandler },
1761 {"CommentHandler",
1762 (xmlhandlersetter)XML_SetCommentHandler,
1763 (xmlhandler)my_CommentHandler},
1764 {"StartCdataSectionHandler",
1765 pyxml_SetStartCdataSection,
1766 (xmlhandler)my_StartCdataSectionHandler},
1767 {"EndCdataSectionHandler",
1768 pyxml_SetEndCdataSection,
1769 (xmlhandler)my_EndCdataSectionHandler},
1770 {"DefaultHandler",
1771 (xmlhandlersetter)XML_SetDefaultHandler,
1772 (xmlhandler)my_DefaultHandler},
1773 {"DefaultHandlerExpand",
1774 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1775 (xmlhandler)my_DefaultHandlerExpandHandler},
1776 {"NotStandaloneHandler",
1777 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1778 (xmlhandler)my_NotStandaloneHandler},
1779 {"ExternalEntityRefHandler",
1780 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1781 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001782#if EXPAT_VERSION >= 0x010200
1783 {"StartDoctypeDeclHandler",
1784 pyxml_SetStartDoctypeDeclHandler,
1785 (xmlhandler)my_StartDoctypeDeclHandler},
1786 {"EndDoctypeDeclHandler",
1787 pyxml_SetEndDoctypeDeclHandler,
1788 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001789#endif
1790#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001791 {"ExternalParsedEntityDeclHandler",
1792 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1793 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1794 {"InternalParsedEntityDeclHandler",
1795 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1796 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001797#endif
1798#if EXPAT_VERSION >= 0x015f00
1799 {"EntityDeclHandler",
1800 (xmlhandlersetter)XML_SetEntityDeclHandler,
1801 (xmlhandler)my_EntityDeclHandler},
1802 {"XmlDeclHandler",
1803 (xmlhandlersetter)XML_SetXmlDeclHandler,
1804 (xmlhandler)my_XmlDeclHandler},
1805 {"ElementDeclHandler",
1806 (xmlhandlersetter)XML_SetElementDeclHandler,
1807 (xmlhandler)my_ElementDeclHandler},
1808 {"AttlistDeclHandler",
1809 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1810 (xmlhandler)my_AttlistDeclHandler},
1811#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001812
Fred Drake0582df92000-07-12 04:49:00 +00001813 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001814};