blob: 7c70f1b0586fa56804d3e282f6e969bb681bfece [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake8188e792001-11-18 02:36:07 +00002#if PY_VERSION_HEX < 0x020000B1
3#include <assert.h>
4#endif
Fred Drake4113b132001-03-24 19:58:26 +00005#include <ctype.h>
6
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007#include "compile.h"
8#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00009#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010
11#ifndef PyGC_HEAD_SIZE
12#define PyGC_HEAD_SIZE 0
13#define PyObject_GC_Init(x)
14#define PyObject_GC_Fini(m)
15#define Py_TPFLAGS_GC 0
16#endif
17
Martin v. Löwis339d0f72001-08-17 18:39:25 +000018#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
19/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
20#define Py_USING_UNICODE
21#endif
22
Fred Drake0582df92000-07-12 04:49:00 +000023enum HandlerTypes {
24 StartElement,
25 EndElement,
26 ProcessingInstruction,
27 CharacterData,
28 UnparsedEntityDecl,
29 NotationDecl,
30 StartNamespaceDecl,
31 EndNamespaceDecl,
32 Comment,
33 StartCdataSection,
34 EndCdataSection,
35 Default,
36 DefaultHandlerExpand,
37 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000038 ExternalEntityRef,
39 StartDoctypeDecl,
40 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000041 EntityDecl,
42 XmlDecl,
43 ElementDecl,
44 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000045 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000046};
47
48static PyObject *ErrorObject;
49
50/* ----------------------------------------------------- */
51
52/* Declarations for objects of type xmlparser */
53
54typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000055 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056
Fred Drake0582df92000-07-12 04:49:00 +000057 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000058 int returns_unicode; /* True if Unicode strings are returned;
59 if false, UTF-8 strings are returned */
60 int ordered_attributes; /* Return attributes as a list. */
61 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000062 int in_callback; /* Is a callback active? */
Fred Drakeb91a36b2002-06-27 19:40:48 +000063 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000064 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000065} xmlparseobject;
66
67staticforward PyTypeObject Xmlparsetype;
68
Fred Drake6f987622000-08-25 18:03:30 +000069typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070typedef void* xmlhandler;
71
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000072struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000073 const char *name;
74 xmlhandlersetter setter;
75 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000076 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000077};
78
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000079staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080
Fred Drakebd6101c2001-02-14 18:29:45 +000081/* Set an integer attribute on the error object; return true on success,
82 * false on an exception.
83 */
84static int
85set_error_attr(PyObject *err, char *name, int value)
86{
87 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000088
Fred Drakebd6101c2001-02-14 18:29:45 +000089 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
90 Py_DECREF(v);
91 return 0;
92 }
93 return 1;
94}
95
96/* Build and set an Expat exception, including positioning
97 * information. Always returns NULL.
98 */
Fred Drake85d835f2001-02-08 15:39:08 +000099static PyObject *
100set_error(xmlparseobject *self)
101{
102 PyObject *err;
103 char buffer[256];
104 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000105 int lineno = XML_GetErrorLineNumber(parser);
106 int column = XML_GetErrorColumnNumber(parser);
107 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000108
Tim Peters885d4572001-11-28 20:27:42 +0000109 PyOS_snprintf(buffer, sizeof(buffer), "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000110 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000111 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000112 if ( err != NULL
113 && set_error_attr(err, "code", code)
114 && set_error_attr(err, "offset", column)
115 && set_error_attr(err, "lineno", lineno)) {
116 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000117 }
118 return NULL;
119}
120
121
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000122#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000123/* Convert a string of XML_Chars into a Unicode string.
124 Returns None if str is a null pointer. */
125
Fred Drake0582df92000-07-12 04:49:00 +0000126static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000127conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000128{
129 /* XXX currently this code assumes that XML_Char is 8-bit,
130 and hence in UTF-8. */
131 /* UTF-8 from Expat, Unicode desired */
132 if (str == NULL) {
133 Py_INCREF(Py_None);
134 return Py_None;
135 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000136 return PyUnicode_DecodeUTF8(str, strlen(str),
Fred Drake0582df92000-07-12 04:49:00 +0000137 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000138}
139
Fred Drake0582df92000-07-12 04:49:00 +0000140static PyObject *
141conv_string_len_to_unicode(const XML_Char *str, int len)
142{
143 /* XXX currently this code assumes that XML_Char is 8-bit,
144 and hence in UTF-8. */
145 /* UTF-8 from Expat, Unicode desired */
146 if (str == NULL) {
147 Py_INCREF(Py_None);
148 return Py_None;
149 }
Fred Drake6f987622000-08-25 18:03:30 +0000150 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000151}
152#endif
153
154/* Convert a string of XML_Chars into an 8-bit Python string.
155 Returns None if str is a null pointer. */
156
Fred Drake6f987622000-08-25 18:03:30 +0000157static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000158conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000159{
160 /* XXX currently this code assumes that XML_Char is 8-bit,
161 and hence in UTF-8. */
162 /* UTF-8 from Expat, UTF-8 desired */
163 if (str == NULL) {
164 Py_INCREF(Py_None);
165 return Py_None;
166 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000167 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000168}
169
Fred Drake6f987622000-08-25 18:03:30 +0000170static PyObject *
171conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000172{
Fred Drake6f987622000-08-25 18:03:30 +0000173 /* XXX currently this code assumes that XML_Char is 8-bit,
174 and hence in UTF-8. */
175 /* UTF-8 from Expat, UTF-8 desired */
176 if (str == NULL) {
177 Py_INCREF(Py_None);
178 return Py_None;
179 }
180 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000181}
182
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000183/* Callback routines */
184
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000185static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000186
Fred Drake6f987622000-08-25 18:03:30 +0000187static void
188flag_error(xmlparseobject *self)
189{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000190 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000191}
192
193static PyCodeObject*
194getcode(enum HandlerTypes slot, char* func_name, int lineno)
195{
Fred Drakebd6101c2001-02-14 18:29:45 +0000196 PyObject *code = NULL;
197 PyObject *name = NULL;
198 PyObject *nulltuple = NULL;
199 PyObject *filename = NULL;
200
201 if (handler_info[slot].tb_code == NULL) {
202 code = PyString_FromString("");
203 if (code == NULL)
204 goto failed;
205 name = PyString_FromString(func_name);
206 if (name == NULL)
207 goto failed;
208 nulltuple = PyTuple_New(0);
209 if (nulltuple == NULL)
210 goto failed;
211 filename = PyString_FromString(__FILE__);
212 handler_info[slot].tb_code =
213 PyCode_New(0, /* argcount */
214 0, /* nlocals */
215 0, /* stacksize */
216 0, /* flags */
217 code, /* code */
218 nulltuple, /* consts */
219 nulltuple, /* names */
220 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000221#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000222 nulltuple, /* freevars */
223 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000224#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000225 filename, /* filename */
226 name, /* name */
227 lineno, /* firstlineno */
228 code /* lnotab */
229 );
230 if (handler_info[slot].tb_code == NULL)
231 goto failed;
232 Py_DECREF(code);
233 Py_DECREF(nulltuple);
234 Py_DECREF(filename);
235 Py_DECREF(name);
236 }
237 return handler_info[slot].tb_code;
238 failed:
239 Py_XDECREF(code);
240 Py_XDECREF(name);
241 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000242}
243
244static PyObject*
245call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
246{
Fred Drakebd6101c2001-02-14 18:29:45 +0000247 PyThreadState *tstate = PyThreadState_GET();
248 PyFrameObject *f;
249 PyObject *res;
250
251 if (c == NULL)
252 return NULL;
253 f = PyFrame_New(
254 tstate, /*back*/
255 c, /*code*/
256 tstate->frame->f_globals, /*globals*/
257 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000258 );
259 if (f == NULL)
260 return NULL;
261 tstate->frame = f;
262 res = PyEval_CallObject(func, args);
263 if (res == NULL && tstate->curexc_traceback == NULL)
264 PyTraceBack_Here(f);
265 tstate->frame = f->f_back;
266 Py_DECREF(f);
267 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000268}
269
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000270#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000271#define STRING_CONV_FUNC conv_string_to_utf8
272#else
273/* Python 1.6 and later versions */
274#define STRING_CONV_FUNC (self->returns_unicode \
275 ? conv_string_to_unicode : conv_string_to_utf8)
276#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000277
Fred Drakeb91a36b2002-06-27 19:40:48 +0000278static PyObject*
279string_intern(xmlparseobject *self, const char* str)
280{
281 PyObject *result = STRING_CONV_FUNC(str);
282 PyObject *value;
283 if (!self->intern)
284 return result;
285 value = PyDict_GetItem(self->intern, result);
286 if (!value) {
287 if (PyDict_SetItem(self->intern, result, result) == 0)
288 return result;
289 else
290 return NULL;
291 }
292 Py_INCREF(value);
293 Py_DECREF(result);
294 return value;
295}
296
Fred Drake85d835f2001-02-08 15:39:08 +0000297static void
298my_StartElementHandler(void *userData,
299 const XML_Char *name, const XML_Char **atts)
300{
301 xmlparseobject *self = (xmlparseobject *)userData;
302
303 if (self->handlers[StartElement]
304 && self->handlers[StartElement] != Py_None) {
305 PyObject *container, *rv, *args;
306 int i, max;
307
308 /* Set max to the number of slots filled in atts[]; max/2 is
309 * the number of attributes we need to process.
310 */
311 if (self->specified_attributes) {
312 max = XML_GetSpecifiedAttributeCount(self->itself);
313 }
314 else {
315 max = 0;
316 while (atts[max] != NULL)
317 max += 2;
318 }
319 /* Build the container. */
320 if (self->ordered_attributes)
321 container = PyList_New(max);
322 else
323 container = PyDict_New();
324 if (container == NULL) {
325 flag_error(self);
326 return;
327 }
328 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000329 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000330 PyObject *v;
331 if (n == NULL) {
332 flag_error(self);
333 Py_DECREF(container);
334 return;
335 }
336 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
337 if (v == NULL) {
338 flag_error(self);
339 Py_DECREF(container);
340 Py_DECREF(n);
341 return;
342 }
343 if (self->ordered_attributes) {
344 PyList_SET_ITEM(container, i, n);
345 PyList_SET_ITEM(container, i+1, v);
346 }
347 else if (PyDict_SetItem(container, n, v)) {
348 flag_error(self);
349 Py_DECREF(n);
350 Py_DECREF(v);
351 return;
352 }
353 else {
354 Py_DECREF(n);
355 Py_DECREF(v);
356 }
357 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000358 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000359 if (args == NULL) {
360 Py_DECREF(container);
361 return;
362 }
363 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000364 self->in_callback = 1;
365 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000366 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000367 self->in_callback = 0;
368 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000369 if (rv == NULL) {
370 flag_error(self);
371 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000372 }
Fred Drake85d835f2001-02-08 15:39:08 +0000373 Py_DECREF(rv);
374 }
375}
376
377#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
378 RETURN, GETUSERDATA) \
379static RC \
380my_##NAME##Handler PARAMS {\
381 xmlparseobject *self = GETUSERDATA ; \
382 PyObject *args = NULL; \
383 PyObject *rv = NULL; \
384 INIT \
385\
386 if (self->handlers[NAME] \
387 && self->handlers[NAME] != Py_None) { \
388 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000389 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000390 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000391 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
392 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000393 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000394 Py_DECREF(args); \
395 if (rv == NULL) { \
396 flag_error(self); \
397 return RETURN; \
398 } \
399 CONVERSION \
400 Py_DECREF(rv); \
401 } \
402 return RETURN; \
403}
404
Fred Drake6f987622000-08-25 18:03:30 +0000405#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
406 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
407 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000408
Fred Drake6f987622000-08-25 18:03:30 +0000409#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
410 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
411 rc = PyInt_AsLong(rv);, rc, \
412 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000413
Fred Drake6f987622000-08-25 18:03:30 +0000414VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000415 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000416 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000417
Fred Drake6f987622000-08-25 18:03:30 +0000418VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000419 (void *userData,
420 const XML_Char *target,
421 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000422 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000423
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000424#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000425VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000426 (void *userData, const XML_Char *data, int len),
427 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000428#else
Fred Drake6f987622000-08-25 18:03:30 +0000429VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000430 (void *userData, const XML_Char *data, int len),
431 ("(N)", (self->returns_unicode
432 ? conv_string_len_to_unicode(data,len)
433 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000434#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000435
Fred Drake6f987622000-08-25 18:03:30 +0000436VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000437 (void *userData,
438 const XML_Char *entityName,
439 const XML_Char *base,
440 const XML_Char *systemId,
441 const XML_Char *publicId,
442 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000443 ("(NNNNN)",
444 string_intern(self, entityName), string_intern(self, base),
445 string_intern(self, systemId), string_intern(self, publicId),
446 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000447
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000448#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000449VOID_HANDLER(EntityDecl,
450 (void *userData,
451 const XML_Char *entityName,
452 int is_parameter_entity,
453 const XML_Char *value,
454 int value_length,
455 const XML_Char *base,
456 const XML_Char *systemId,
457 const XML_Char *publicId,
458 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000459 ("NiNNNNN",
460 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000461 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000462 string_intern(self, base), string_intern(self, systemId),
463 string_intern(self, publicId),
464 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000465#else
466VOID_HANDLER(EntityDecl,
467 (void *userData,
468 const XML_Char *entityName,
469 int is_parameter_entity,
470 const XML_Char *value,
471 int value_length,
472 const XML_Char *base,
473 const XML_Char *systemId,
474 const XML_Char *publicId,
475 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000476 ("NiNNNNN",
477 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000478 (self->returns_unicode
479 ? conv_string_len_to_unicode(value, value_length)
480 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000481 string_intern(self, base), string_intern(self, systemId),
482 string_intern(self, publicId),
483 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000484#endif
485
486VOID_HANDLER(XmlDecl,
487 (void *userData,
488 const XML_Char *version,
489 const XML_Char *encoding,
490 int standalone),
491 ("(O&O&i)",
492 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
493 standalone))
494
495static PyObject *
496conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000497 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000498{
499 PyObject *result = NULL;
500 PyObject *children = PyTuple_New(model->numchildren);
501 int i;
502
503 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000504 assert(model->numchildren < INT_MAX);
505 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000506 PyObject *child = conv_content_model(&model->children[i],
507 conv_string);
508 if (child == NULL) {
509 Py_XDECREF(children);
510 return NULL;
511 }
512 PyTuple_SET_ITEM(children, i, child);
513 }
514 result = Py_BuildValue("(iiO&N)",
515 model->type, model->quant,
516 conv_string,model->name, children);
517 }
518 return result;
519}
520
521static PyObject *
522conv_content_model_utf8(XML_Content * const model)
523{
524 return conv_content_model(model, conv_string_to_utf8);
525}
526
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000527#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000528static PyObject *
529conv_content_model_unicode(XML_Content * const model)
530{
531 return conv_content_model(model, conv_string_to_unicode);
532}
533
534VOID_HANDLER(ElementDecl,
535 (void *userData,
536 const XML_Char *name,
537 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000538 ("NO&",
539 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000540 (self->returns_unicode ? conv_content_model_unicode
541 : conv_content_model_utf8),model))
542#else
543VOID_HANDLER(ElementDecl,
544 (void *userData,
545 const XML_Char *name,
546 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000547 ("NO&",
548 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000549#endif
550
551VOID_HANDLER(AttlistDecl,
552 (void *userData,
553 const XML_Char *elname,
554 const XML_Char *attname,
555 const XML_Char *att_type,
556 const XML_Char *dflt,
557 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000558 ("(NNO&O&i)",
559 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000560 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
561 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000562
Fred Drake6f987622000-08-25 18:03:30 +0000563VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000564 (void *userData,
565 const XML_Char *notationName,
566 const XML_Char *base,
567 const XML_Char *systemId,
568 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000569 ("(NNNN)",
570 string_intern(self, notationName), string_intern(self, base),
571 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000572
Fred Drake6f987622000-08-25 18:03:30 +0000573VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000574 (void *userData,
575 const XML_Char *prefix,
576 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000577 ("(NN)",
578 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000579
Fred Drake6f987622000-08-25 18:03:30 +0000580VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000581 (void *userData,
582 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000583 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000584
Fred Drake6f987622000-08-25 18:03:30 +0000585VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000586 (void *userData, const XML_Char *data),
587 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000588
Fred Drake6f987622000-08-25 18:03:30 +0000589VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000590 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000591 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000592
Fred Drake6f987622000-08-25 18:03:30 +0000593VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000594 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000595 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000596
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000597#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000598VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000599 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000600 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000601
Fred Drake6f987622000-08-25 18:03:30 +0000602VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000603 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000604 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000605#else
Fred Drake6f987622000-08-25 18:03:30 +0000606VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000607 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000608 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000609 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000610 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000611
Fred Drake6f987622000-08-25 18:03:30 +0000612VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000613 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000614 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000615 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000616 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000617#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000618
Fred Drake6f987622000-08-25 18:03:30 +0000619INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000620 (void *userData),
621 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000622
Fred Drake6f987622000-08-25 18:03:30 +0000623RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000624 (XML_Parser parser,
625 const XML_Char *context,
626 const XML_Char *base,
627 const XML_Char *systemId,
628 const XML_Char *publicId),
629 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000630 ("(O&NNN)",
631 STRING_CONV_FUNC,context, string_intern(self, base),
632 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000633 rc = PyInt_AsLong(rv);, rc,
634 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000635
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000636/* XXX UnknownEncodingHandler */
637
Fred Drake85d835f2001-02-08 15:39:08 +0000638VOID_HANDLER(StartDoctypeDecl,
639 (void *userData, const XML_Char *doctypeName,
640 const XML_Char *sysid, const XML_Char *pubid,
641 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000642 ("(NNNi)", string_intern(self, doctypeName),
643 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000644 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000645
646VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000647
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000648/* ---------------------------------------------------------------- */
649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000650PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000651"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000653
654static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000655xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656{
Fred Drake0582df92000-07-12 04:49:00 +0000657 char *s;
658 int slen;
659 int isFinal = 0;
660 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000661
Fred Drake0582df92000-07-12 04:49:00 +0000662 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
663 return NULL;
664 rv = XML_Parse(self->itself, s, slen, isFinal);
665 if (PyErr_Occurred()) {
666 return NULL;
667 }
668 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000669 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000670 }
671 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000672}
673
Fred Drakeca1f4262000-09-21 20:10:23 +0000674/* File reading copied from cPickle */
675
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000676#define BUF_SIZE 2048
677
Fred Drake0582df92000-07-12 04:49:00 +0000678static int
679readinst(char *buf, int buf_size, PyObject *meth)
680{
681 PyObject *arg = NULL;
682 PyObject *bytes = NULL;
683 PyObject *str = NULL;
684 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685
Fred Drake676940b2000-09-22 15:21:31 +0000686 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000687 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000688
Fred Drakeca1f4262000-09-21 20:10:23 +0000689 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000690 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000691
Tim Peters954eef72000-09-22 06:01:11 +0000692 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000693
Fred Drakeca1f4262000-09-21 20:10:23 +0000694 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000695 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000696
Fred Drake0582df92000-07-12 04:49:00 +0000697 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000698 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000699 PyErr_Format(PyExc_TypeError,
700 "read() did not return a string object (type=%.400s)",
701 str->ob_type->tp_name);
702 goto finally;
703 }
704 len = PyString_GET_SIZE(str);
705 if (len > buf_size) {
706 PyErr_Format(PyExc_ValueError,
707 "read() returned too much data: "
708 "%i bytes requested, %i returned",
709 buf_size, len);
710 Py_DECREF(str);
711 goto finally;
712 }
713 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000714finally:
Fred Drake0582df92000-07-12 04:49:00 +0000715 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000716 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000717 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718}
719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000720PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000721"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000723
724static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000725xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000726{
Fred Drake0582df92000-07-12 04:49:00 +0000727 int rv = 1;
728 PyObject *f;
729 FILE *fp;
730 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000731
Fred Drake0582df92000-07-12 04:49:00 +0000732 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
733 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000734
Fred Drake0582df92000-07-12 04:49:00 +0000735 if (PyFile_Check(f)) {
736 fp = PyFile_AsFile(f);
737 }
738 else{
739 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000740 readmethod = PyObject_GetAttrString(f, "read");
741 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000742 PyErr_Clear();
743 PyErr_SetString(PyExc_TypeError,
744 "argument must have 'read' attribute");
745 return 0;
746 }
747 }
748 for (;;) {
749 int bytes_read;
750 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
751 if (buf == NULL)
752 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000753
Fred Drake0582df92000-07-12 04:49:00 +0000754 if (fp) {
755 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
756 if (bytes_read < 0) {
757 PyErr_SetFromErrno(PyExc_IOError);
758 return NULL;
759 }
760 }
761 else {
762 bytes_read = readinst(buf, BUF_SIZE, readmethod);
763 if (bytes_read < 0)
764 return NULL;
765 }
766 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
767 if (PyErr_Occurred())
768 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000769
Fred Drake0582df92000-07-12 04:49:00 +0000770 if (!rv || bytes_read == 0)
771 break;
772 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000773 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000774 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000775 }
Fred Drake0582df92000-07-12 04:49:00 +0000776 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000777}
778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000779PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000780"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000781Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000782
783static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000784xmlparse_SetBase(xmlparseobject *self, PyObject *args)
785{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786 char *base;
787
Fred Drake0582df92000-07-12 04:49:00 +0000788 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000790 if (!XML_SetBase(self->itself, base)) {
791 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000792 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000793 Py_INCREF(Py_None);
794 return Py_None;
795}
796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000798"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000799Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000800
801static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000802xmlparse_GetBase(xmlparseobject *self, PyObject *args)
803{
804 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000805 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000806
Fred Drake0582df92000-07-12 04:49:00 +0000807 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000808}
809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000811"GetInputContext() -> string\n\
812Return the untranslated text of the input that caused the current event.\n\
813If the event was generated by a large amount of text (such as a start tag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000814for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000815
816static PyObject *
817xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
818{
819 PyObject *result = NULL;
820
821 if (PyArg_ParseTuple(args, ":GetInputContext")) {
822 if (self->in_callback) {
823 int offset, size;
824 const char *buffer
825 = XML_GetInputContext(self->itself, &offset, &size);
826
827 if (buffer != NULL)
828 result = PyString_FromStringAndSize(buffer + offset, size);
829 else {
830 result = Py_None;
831 Py_INCREF(result);
832 }
833 }
834 else {
835 result = Py_None;
836 Py_INCREF(result);
837 }
838 }
839 return result;
840}
Fred Drakebd6101c2001-02-14 18:29:45 +0000841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000842PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000843"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000844Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000845information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000846
847static PyObject *
848xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
849{
850 char *context;
851 char *encoding = NULL;
852 xmlparseobject *new_parser;
853 int i;
854
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000855 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000856 &context, &encoding)) {
857 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000858 }
859
860#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
861 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000862#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000863#ifndef Py_TPFLAGS_HAVE_GC
864 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000865 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000866#else
867 /* Python versions 2.2 and later */
868 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
869#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000870#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000871
872 if (new_parser == NULL)
873 return NULL;
874 new_parser->returns_unicode = self->returns_unicode;
875 new_parser->ordered_attributes = self->ordered_attributes;
876 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000877 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000878 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000879 encoding);
880 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000881 new_parser->intern = self->intern;
882 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000883#ifdef Py_TPFLAGS_HAVE_GC
884 PyObject_GC_Track(new_parser);
885#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000886 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000887#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000888
889 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000890 Py_DECREF(new_parser);
891 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000892 }
893
894 XML_SetUserData(new_parser->itself, (void *)new_parser);
895
896 /* allocate and clear handlers first */
897 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000898 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000899
900 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000901 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000902 Py_DECREF(new_parser);
903 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000904 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000905 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000906
907 /* then copy handlers from self */
908 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +0000909 if (self->handlers[i]) {
910 Py_INCREF(self->handlers[i]);
911 new_parser->handlers[i] = self->handlers[i];
912 handler_info[i].setter(new_parser->itself,
913 handler_info[i].handler);
914 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000915 }
Fred Drake28adf522000-09-24 22:07:59 +0000916 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000917}
918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000919PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000920"SetParamEntityParsing(flag) -> success\n\
921Controls parsing of parameter entities (including the external DTD\n\
922subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
923XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
924XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000925was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000926
927static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +0000928xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000929{
Fred Drake85d835f2001-02-08 15:39:08 +0000930 int flag;
931 if (!PyArg_ParseTuple(args, "i", &flag))
932 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +0000933 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +0000934 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000935}
936
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000937static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +0000938 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +0000939 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000940 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +0000941 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000942 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000943 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000944 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000945 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000946 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
947 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000948 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
949 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000950 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
951 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000952 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000953};
954
955/* ---------- */
956
957
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000958#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000959
960/*
961 pyexpat international encoding support.
962 Make it as simple as possible.
963*/
964
Martin v. Löwis3af7cc02001-01-22 08:19:10 +0000965static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +0000966PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000967
968static void
969init_template_buffer(void)
970{
971 int i;
Fred Drakebb66a202001-03-01 20:48:17 +0000972 for (i = 0; i < 256; i++) {
973 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +0000974 }
Fred Drakebb66a202001-03-01 20:48:17 +0000975 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +0000976}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000977
978int
979PyUnknownEncodingHandler(void *encodingHandlerData,
980const XML_Char *name,
981XML_Encoding * info)
982{
Fred Drakebb66a202001-03-01 20:48:17 +0000983 PyUnicodeObject *_u_string = NULL;
984 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000985 int i;
986
Fred Drakebb66a202001-03-01 20:48:17 +0000987 /* Yes, supports only 8bit encodings */
988 _u_string = (PyUnicodeObject *)
989 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000990
Fred Drakebb66a202001-03-01 20:48:17 +0000991 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000992 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000993
Fred Drakebb66a202001-03-01 20:48:17 +0000994 for (i = 0; i < 256; i++) {
995 /* Stupid to access directly, but fast */
996 Py_UNICODE c = _u_string->str[i];
997 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000998 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +0000999 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001000 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001001 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001002
1003 info->data = NULL;
1004 info->convert = NULL;
1005 info->release = NULL;
1006 result=1;
1007
1008 Py_DECREF(_u_string);
1009 return result;
1010}
1011
1012#endif
1013
1014static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001015newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001016{
1017 int i;
1018 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001019
1020#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001021 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1022 if (self == NULL)
1023 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001024
Fred Drake0582df92000-07-12 04:49:00 +00001025 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001026#else
Fred Drake0582df92000-07-12 04:49:00 +00001027 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001028#ifdef Py_TPFLAGS_HAVE_GC
1029 /* Code for versions 2.2 and later */
1030 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1031#else
Fred Drake0582df92000-07-12 04:49:00 +00001032 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001033#endif
Fred Drake0582df92000-07-12 04:49:00 +00001034 if (self == NULL)
1035 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001036
Fred Drake0582df92000-07-12 04:49:00 +00001037 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001038#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001039 self->ordered_attributes = 0;
1040 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001041 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001042 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001043 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001044 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1045 }
Fred Drake85d835f2001-02-08 15:39:08 +00001046 else {
Fred Drake0582df92000-07-12 04:49:00 +00001047 self->itself = XML_ParserCreate(encoding);
1048 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001049 self->intern = intern;
1050 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001051#ifdef Py_TPFLAGS_HAVE_GC
1052 PyObject_GC_Track(self);
1053#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001054 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001055#endif
Fred Drake0582df92000-07-12 04:49:00 +00001056 if (self->itself == NULL) {
1057 PyErr_SetString(PyExc_RuntimeError,
1058 "XML_ParserCreate failed");
1059 Py_DECREF(self);
1060 return NULL;
1061 }
1062 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001063#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001064 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1065#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001066
Fred Drake0582df92000-07-12 04:49:00 +00001067 for(i = 0; handler_info[i].name != NULL; i++)
1068 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001069
Fred Drake0582df92000-07-12 04:49:00 +00001070 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001071 if (!self->handlers){
1072 Py_DECREF(self);
1073 return PyErr_NoMemory();
1074 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001075 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001076
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001078}
1079
1080
1081static void
Fred Drake0582df92000-07-12 04:49:00 +00001082xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001083{
Fred Drake0582df92000-07-12 04:49:00 +00001084 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001085#ifdef Py_TPFLAGS_HAVE_GC
1086 PyObject_GC_UnTrack(self);
1087#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001088 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001089#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001090 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001091 XML_ParserFree(self->itself);
1092 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001093
Fred Drake85d835f2001-02-08 15:39:08 +00001094 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001095 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001096 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001097 temp = self->handlers[i];
1098 self->handlers[i] = NULL;
1099 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001100 }
1101 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001102 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001103 Py_XDECREF(self->intern);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001104#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001105 /* Code for versions before 1.6 */
1106 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001107#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001108#ifndef Py_TPFLAGS_HAVE_GC
1109 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001110 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001111#else
1112 /* Code for versions 2.2 and later. */
1113 PyObject_GC_Del(self);
1114#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001115#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001116}
1117
Fred Drake0582df92000-07-12 04:49:00 +00001118static int
1119handlername2int(const char *name)
1120{
1121 int i;
1122 for (i=0; handler_info[i].name != NULL; i++) {
1123 if (strcmp(name, handler_info[i].name) == 0) {
1124 return i;
1125 }
1126 }
1127 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001128}
1129
1130static PyObject *
1131xmlparse_getattr(xmlparseobject *self, char *name)
1132{
Fred Drake0582df92000-07-12 04:49:00 +00001133 int handlernum;
1134 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001135 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001136 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001137 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001138 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001139 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001140 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001141 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1142 if (strcmp(name, "ordered_attributes") == 0)
1143 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001144 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001145 return PyInt_FromLong((long) self->returns_unicode);
1146 if (strcmp(name, "specified_attributes") == 0)
1147 return PyInt_FromLong((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001148 if (strcmp(name, "intern") == 0) {
1149 if (self->intern == NULL) {
1150 Py_INCREF(Py_None);
1151 return Py_None;
1152 }
1153 else {
1154 Py_INCREF(self->intern);
1155 return self->intern;
1156 }
1157 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001158
Fred Drake0582df92000-07-12 04:49:00 +00001159 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001160
Fred Drake0582df92000-07-12 04:49:00 +00001161 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1162 Py_INCREF(self->handlers[handlernum]);
1163 return self->handlers[handlernum];
1164 }
1165 if (strcmp(name, "__members__") == 0) {
1166 int i;
1167 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001168 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001169 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001170 }
1171 PyList_Append(rc, PyString_FromString("ErrorCode"));
1172 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1173 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1174 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001175 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001176 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001177 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001178 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001179
Fred Drake0582df92000-07-12 04:49:00 +00001180 return rc;
1181 }
1182 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001183}
1184
Fred Drake6f987622000-08-25 18:03:30 +00001185static int
1186sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001187{
1188 int handlernum = handlername2int(name);
1189 if (handlernum != -1) {
1190 Py_INCREF(v);
1191 Py_XDECREF(self->handlers[handlernum]);
1192 self->handlers[handlernum] = v;
1193 handler_info[handlernum].setter(self->itself,
1194 handler_info[handlernum].handler);
1195 return 1;
1196 }
1197 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001198}
1199
1200static int
Fred Drake6f987622000-08-25 18:03:30 +00001201xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001202{
Fred Drake6f987622000-08-25 18:03:30 +00001203 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001204 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001205 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1206 return -1;
1207 }
Fred Drake85d835f2001-02-08 15:39:08 +00001208 if (strcmp(name, "ordered_attributes") == 0) {
1209 if (PyObject_IsTrue(v))
1210 self->ordered_attributes = 1;
1211 else
1212 self->ordered_attributes = 0;
1213 return 0;
1214 }
Fred Drake6f987622000-08-25 18:03:30 +00001215 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001216 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001217#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001218 PyErr_SetString(PyExc_ValueError,
1219 "Cannot return Unicode strings in Python 1.5");
1220 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001221#else
Fred Drake6f987622000-08-25 18:03:30 +00001222 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001223#endif
Fred Drake6f987622000-08-25 18:03:30 +00001224 }
1225 else
1226 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001227 return 0;
1228 }
1229 if (strcmp(name, "specified_attributes") == 0) {
1230 if (PyObject_IsTrue(v))
1231 self->specified_attributes = 1;
1232 else
1233 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001234 return 0;
1235 }
1236 if (sethandler(self, name, v)) {
1237 return 0;
1238 }
1239 PyErr_SetString(PyExc_AttributeError, name);
1240 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001241}
1242
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001243#ifdef WITH_CYCLE_GC
1244static int
1245xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1246{
Fred Drakecde79132001-04-25 16:01:30 +00001247 int i, err;
1248 for (i = 0; handler_info[i].name != NULL; i++) {
1249 if (!op->handlers[i])
1250 continue;
1251 err = visit(op->handlers[i], arg);
1252 if (err)
1253 return err;
1254 }
1255 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001256}
1257
1258static int
1259xmlparse_clear(xmlparseobject *op)
1260{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001261 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001262 Py_XDECREF(op->intern);
1263 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001264 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001265}
1266#endif
1267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269
1270static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001271 PyObject_HEAD_INIT(NULL)
1272 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001273 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001274 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001275 0, /*tp_itemsize*/
1276 /* methods */
1277 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1278 (printfunc)0, /*tp_print*/
1279 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1280 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1281 (cmpfunc)0, /*tp_compare*/
1282 (reprfunc)0, /*tp_repr*/
1283 0, /*tp_as_number*/
1284 0, /*tp_as_sequence*/
1285 0, /*tp_as_mapping*/
1286 (hashfunc)0, /*tp_hash*/
1287 (ternaryfunc)0, /*tp_call*/
1288 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001289 0, /* tp_getattro */
1290 0, /* tp_setattro */
1291 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001292#ifdef Py_TPFLAGS_HAVE_GC
1293 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1294#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001295 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001296#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001297 Xmlparsetype__doc__, /* Documentation string */
1298#ifdef WITH_CYCLE_GC
1299 (traverseproc)xmlparse_traverse, /* tp_traverse */
1300 (inquiry)xmlparse_clear /* tp_clear */
1301#else
1302 0, 0
1303#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001304};
1305
1306/* End of code for xmlparser objects */
1307/* -------------------------------------------------------- */
1308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001309PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001310"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001312
1313static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001314pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1315{
Fred Drakecde79132001-04-25 16:01:30 +00001316 char *encoding = NULL;
1317 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001318 PyObject *intern = NULL;
1319 PyObject *result;
1320 int intern_decref = 0;
1321 static char *kwlist[] = {"encoding", "namespace_separator",
1322 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001323
Fred Drakeb91a36b2002-06-27 19:40:48 +00001324 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1325 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001326 return NULL;
1327 if (namespace_separator != NULL
1328 && strlen(namespace_separator) > 1) {
1329 PyErr_SetString(PyExc_ValueError,
1330 "namespace_separator must be at most one"
1331 " character, omitted, or None");
1332 return NULL;
1333 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001334 /* Explicitly passing None means no interning is desired.
1335 Not passing anything means that a new dictionary is used. */
1336 if (intern == Py_None)
1337 intern = NULL;
1338 else if (intern == NULL) {
1339 intern = PyDict_New();
1340 if (!intern)
1341 return NULL;
1342 intern_decref = 1;
1343 }
1344 else if (!PyDict_Check(intern)) {
1345 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1346 return NULL;
1347 }
1348
1349 result = newxmlparseobject(encoding, namespace_separator, intern);
1350 if (intern_decref) {
1351 Py_DECREF(intern);
1352 }
1353 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001354}
1355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001356PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001357"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001359
1360static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001361pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001362{
Fred Drake0582df92000-07-12 04:49:00 +00001363 long code = 0;
1364
1365 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1366 return NULL;
1367 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001368}
1369
1370/* List of methods defined in the module */
1371
1372static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001373 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1374 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1375 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1376 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377
Fred Drake0582df92000-07-12 04:49:00 +00001378 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001379};
1380
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001381/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001383PyDoc_STRVAR(pyexpat_module_documentation,
1384"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001386#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001387
1388/* 1.5 compatibility: PyModule_AddObject */
1389static int
1390PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1391{
Fred Drakecde79132001-04-25 16:01:30 +00001392 PyObject *dict;
1393 if (!PyModule_Check(m) || o == NULL)
1394 return -1;
1395 dict = PyModule_GetDict(m);
1396 if (dict == NULL)
1397 return -1;
1398 if (PyDict_SetItemString(dict, name, o))
1399 return -1;
1400 Py_DECREF(o);
1401 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001402}
1403
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001404int
1405PyModule_AddIntConstant(PyObject *m, char *name, long value)
1406{
Fred Drakecde79132001-04-25 16:01:30 +00001407 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001408}
1409
Fred Drakea77254a2000-09-29 19:23:29 +00001410static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001411PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1412{
Fred Drakecde79132001-04-25 16:01:30 +00001413 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001414}
1415
1416#endif
1417
Fred Drake4113b132001-03-24 19:58:26 +00001418
1419/* Return a Python string that represents the version number without the
1420 * extra cruft added by revision control, even if the right options were
1421 * given to the "cvs export" command to make it not include the extra
1422 * cruft.
1423 */
1424static PyObject *
1425get_version_string(void)
1426{
1427 static char *rcsid = "$Revision$";
1428 char *rev = rcsid;
1429 int i = 0;
1430
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001431 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001432 ++rev;
1433 while (rev[i] != ' ' && rev[i] != '\0')
1434 ++i;
1435
1436 return PyString_FromStringAndSize(rev, i);
1437}
1438
Fred Drakecde79132001-04-25 16:01:30 +00001439/* Initialization function for the module */
1440
1441#ifndef MODULE_NAME
1442#define MODULE_NAME "pyexpat"
1443#endif
1444
1445#ifndef MODULE_INITFUNC
1446#define MODULE_INITFUNC initpyexpat
1447#endif
1448
1449void MODULE_INITFUNC(void); /* avoid compiler warnings */
1450
Fred Drake6f987622000-08-25 18:03:30 +00001451DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001452MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001453{
1454 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001455 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001456 PyObject *errors_module;
1457 PyObject *modelmod_name;
1458 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001459 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001460
Fred Drake6f987622000-08-25 18:03:30 +00001461 if (errmod_name == NULL)
1462 return;
Fred Drakecde79132001-04-25 16:01:30 +00001463 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001464 if (modelmod_name == NULL)
1465 return;
Fred Drake6f987622000-08-25 18:03:30 +00001466
Fred Drake0582df92000-07-12 04:49:00 +00001467 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001468
Fred Drake0582df92000-07-12 04:49:00 +00001469 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001470 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001471 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001472
Fred Drake0582df92000-07-12 04:49:00 +00001473 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001474 if (ErrorObject == NULL) {
1475 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001476 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001477 if (ErrorObject == NULL)
1478 return;
1479 }
1480 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001481 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001482 Py_INCREF(ErrorObject);
1483 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001484 Py_INCREF(&Xmlparsetype);
1485 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001486
Fred Drake4113b132001-03-24 19:58:26 +00001487 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001488 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1489 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001490 {
1491 XML_Expat_Version info = XML_ExpatVersionInfo();
1492 PyModule_AddObject(m, "version_info",
1493 Py_BuildValue("(iii)", info.major,
1494 info.minor, info.micro));
1495 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001496#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001497 init_template_buffer();
1498#endif
Fred Drake0582df92000-07-12 04:49:00 +00001499 /* XXX When Expat supports some way of figuring out how it was
1500 compiled, this should check and set native_encoding
1501 appropriately.
1502 */
Fred Drake93adb692000-09-23 04:55:48 +00001503 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001504
Fred Drake85d835f2001-02-08 15:39:08 +00001505 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001506 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001507 errors_module = PyDict_GetItem(d, errmod_name);
1508 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001509 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001510 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001511 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001512 /* gives away the reference to errors_module */
1513 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001514 }
1515 }
Fred Drake6f987622000-08-25 18:03:30 +00001516 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001517 model_module = PyDict_GetItem(d, modelmod_name);
1518 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001519 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001520 if (model_module != NULL) {
1521 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1522 /* gives away the reference to model_module */
1523 PyModule_AddObject(m, "model", model_module);
1524 }
1525 }
1526 Py_DECREF(modelmod_name);
1527 if (errors_module == NULL || model_module == NULL)
1528 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001529 return;
1530
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001531#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001532 PyModule_AddStringConstant(errors_module, #name, \
1533 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001534
Fred Drake0582df92000-07-12 04:49:00 +00001535 MYCONST(XML_ERROR_NO_MEMORY);
1536 MYCONST(XML_ERROR_SYNTAX);
1537 MYCONST(XML_ERROR_NO_ELEMENTS);
1538 MYCONST(XML_ERROR_INVALID_TOKEN);
1539 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1540 MYCONST(XML_ERROR_PARTIAL_CHAR);
1541 MYCONST(XML_ERROR_TAG_MISMATCH);
1542 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1543 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1544 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1545 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1546 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1547 MYCONST(XML_ERROR_ASYNC_ENTITY);
1548 MYCONST(XML_ERROR_BAD_CHAR_REF);
1549 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1550 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1551 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1552 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1553 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001554 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1555 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1556 MYCONST(XML_ERROR_NOT_STANDALONE);
1557
Fred Drake85d835f2001-02-08 15:39:08 +00001558 PyModule_AddStringConstant(errors_module, "__doc__",
1559 "Constants used to describe error conditions.");
1560
Fred Drake93adb692000-09-23 04:55:48 +00001561#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001562
Fred Drake85d835f2001-02-08 15:39:08 +00001563#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001564 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1565 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1566 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001567#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001568
Fred Drake85d835f2001-02-08 15:39:08 +00001569#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1570 PyModule_AddStringConstant(model_module, "__doc__",
1571 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001572
Fred Drake85d835f2001-02-08 15:39:08 +00001573 MYCONST(XML_CTYPE_EMPTY);
1574 MYCONST(XML_CTYPE_ANY);
1575 MYCONST(XML_CTYPE_MIXED);
1576 MYCONST(XML_CTYPE_NAME);
1577 MYCONST(XML_CTYPE_CHOICE);
1578 MYCONST(XML_CTYPE_SEQ);
1579
1580 MYCONST(XML_CQUANT_NONE);
1581 MYCONST(XML_CQUANT_OPT);
1582 MYCONST(XML_CQUANT_REP);
1583 MYCONST(XML_CQUANT_PLUS);
1584#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001585}
1586
Fred Drake6f987622000-08-25 18:03:30 +00001587static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001588clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001589{
Fred Drakecde79132001-04-25 16:01:30 +00001590 int i = 0;
1591 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001592
Fred Drakecde79132001-04-25 16:01:30 +00001593 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001594 if (initial)
1595 self->handlers[i]=NULL;
1596 else {
Fred Drakecde79132001-04-25 16:01:30 +00001597 temp = self->handlers[i];
1598 self->handlers[i] = NULL;
1599 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001600 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001601 }
Fred Drakecde79132001-04-25 16:01:30 +00001602 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001603}
1604
Fred Drake6f987622000-08-25 18:03:30 +00001605typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001606
Fred Drake6f987622000-08-25 18:03:30 +00001607static void
1608pyxml_UpdatePairedHandlers(xmlparseobject *self,
1609 int startHandler,
1610 int endHandler,
1611 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001612{
Fred Drakecde79132001-04-25 16:01:30 +00001613 void *start_handler = NULL;
1614 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001615
Fred Drake0582df92000-07-12 04:49:00 +00001616 if (self->handlers[startHandler]
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001617 && self->handlers[startHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001618 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001619 }
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001620 if (self->handlers[endHandler]
1621 && self->handlers[endHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001622 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001623 }
1624 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001625}
1626
Fred Drake6f987622000-08-25 18:03:30 +00001627static void
1628pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001629{
1630 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1631 StartElement, EndElement,
1632 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001633}
1634
Fred Drake6f987622000-08-25 18:03:30 +00001635static void
1636pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001637{
1638 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1639 StartElement, EndElement,
1640 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001641}
1642
Fred Drake6f987622000-08-25 18:03:30 +00001643static void
1644pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001645{
1646 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1647 StartNamespaceDecl, EndNamespaceDecl,
1648 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001649}
1650
Fred Drake6f987622000-08-25 18:03:30 +00001651static void
1652pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001653{
1654 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1655 StartNamespaceDecl, EndNamespaceDecl,
1656 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001657}
1658
Fred Drake6f987622000-08-25 18:03:30 +00001659static void
1660pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001661{
1662 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1663 StartCdataSection, EndCdataSection,
1664 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001665}
1666
Fred Drake6f987622000-08-25 18:03:30 +00001667static void
1668pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001669{
1670 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1671 StartCdataSection, EndCdataSection,
1672 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001673}
1674
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001675static void
1676pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1677{
1678 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1679 StartDoctypeDecl, EndDoctypeDecl,
1680 (pairsetter)XML_SetDoctypeDeclHandler);
1681}
1682
1683static void
1684pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1685{
1686 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1687 StartDoctypeDecl, EndDoctypeDecl,
1688 (pairsetter)XML_SetDoctypeDeclHandler);
1689}
1690
Fred Drake0582df92000-07-12 04:49:00 +00001691statichere struct HandlerInfo handler_info[] = {
1692 {"StartElementHandler",
1693 pyxml_SetStartElementHandler,
1694 (xmlhandler)my_StartElementHandler},
1695 {"EndElementHandler",
1696 pyxml_SetEndElementHandler,
1697 (xmlhandler)my_EndElementHandler},
1698 {"ProcessingInstructionHandler",
1699 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1700 (xmlhandler)my_ProcessingInstructionHandler},
1701 {"CharacterDataHandler",
1702 (xmlhandlersetter)XML_SetCharacterDataHandler,
1703 (xmlhandler)my_CharacterDataHandler},
1704 {"UnparsedEntityDeclHandler",
1705 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1706 (xmlhandler)my_UnparsedEntityDeclHandler },
1707 {"NotationDeclHandler",
1708 (xmlhandlersetter)XML_SetNotationDeclHandler,
1709 (xmlhandler)my_NotationDeclHandler },
1710 {"StartNamespaceDeclHandler",
1711 pyxml_SetStartNamespaceDeclHandler,
1712 (xmlhandler)my_StartNamespaceDeclHandler },
1713 {"EndNamespaceDeclHandler",
1714 pyxml_SetEndNamespaceDeclHandler,
1715 (xmlhandler)my_EndNamespaceDeclHandler },
1716 {"CommentHandler",
1717 (xmlhandlersetter)XML_SetCommentHandler,
1718 (xmlhandler)my_CommentHandler},
1719 {"StartCdataSectionHandler",
1720 pyxml_SetStartCdataSection,
1721 (xmlhandler)my_StartCdataSectionHandler},
1722 {"EndCdataSectionHandler",
1723 pyxml_SetEndCdataSection,
1724 (xmlhandler)my_EndCdataSectionHandler},
1725 {"DefaultHandler",
1726 (xmlhandlersetter)XML_SetDefaultHandler,
1727 (xmlhandler)my_DefaultHandler},
1728 {"DefaultHandlerExpand",
1729 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1730 (xmlhandler)my_DefaultHandlerExpandHandler},
1731 {"NotStandaloneHandler",
1732 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1733 (xmlhandler)my_NotStandaloneHandler},
1734 {"ExternalEntityRefHandler",
1735 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1736 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001737 {"StartDoctypeDeclHandler",
1738 pyxml_SetStartDoctypeDeclHandler,
1739 (xmlhandler)my_StartDoctypeDeclHandler},
1740 {"EndDoctypeDeclHandler",
1741 pyxml_SetEndDoctypeDeclHandler,
1742 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001743 {"EntityDeclHandler",
1744 (xmlhandlersetter)XML_SetEntityDeclHandler,
1745 (xmlhandler)my_EntityDeclHandler},
1746 {"XmlDeclHandler",
1747 (xmlhandlersetter)XML_SetXmlDeclHandler,
1748 (xmlhandler)my_XmlDeclHandler},
1749 {"ElementDeclHandler",
1750 (xmlhandlersetter)XML_SetElementDeclHandler,
1751 (xmlhandler)my_ElementDeclHandler},
1752 {"AttlistDeclHandler",
1753 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1754 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001755
Fred Drake0582df92000-07-12 04:49:00 +00001756 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001757};