blob: eac92cb4a1f48013f5dd6d8797c7ba4914f99376 [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Fred Drake7c75bf22002-07-01 14:02:31 +00004#ifdef HAVE_PYMEMCOMPAT_H
5#include "pymemcompat.h"
6#endif
7
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00008#include "compile.h"
9#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +000010#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000011
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000012#ifndef PyDoc_STRVAR
Fred Drake7c75bf22002-07-01 14:02:31 +000013#define PyDoc_STR(str) (str)
14#define PyDoc_VAR(name) static char name[]
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000015#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000016#endif
17
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000018#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
19/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000020#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 Drake2a3d7db2002-06-28 22:56:48 +000063 XML_Char *buffer; /* Buffer used when accumulating characters */
64 /* NULL if not enabled */
65 int buffer_size; /* Size of buffer, in XML_Char units */
66 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000067 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000068 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000069} xmlparseobject;
70
Fred Drake2a3d7db2002-06-28 22:56:48 +000071#define CHARACTER_DATA_BUFFER_SIZE 8192
72
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000073staticforward PyTypeObject Xmlparsetype;
74
Fred Drake6f987622000-08-25 18:03:30 +000075typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076typedef void* xmlhandler;
77
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000078struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000079 const char *name;
80 xmlhandlersetter setter;
81 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000082 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000083 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000084};
85
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000086staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000087
Fred Drakebd6101c2001-02-14 18:29:45 +000088/* Set an integer attribute on the error object; return true on success,
89 * false on an exception.
90 */
91static int
92set_error_attr(PyObject *err, char *name, int value)
93{
94 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000095
Fred Drakebd6101c2001-02-14 18:29:45 +000096 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
97 Py_DECREF(v);
98 return 0;
99 }
100 return 1;
101}
102
103/* Build and set an Expat exception, including positioning
104 * information. Always returns NULL.
105 */
Fred Drake85d835f2001-02-08 15:39:08 +0000106static PyObject *
107set_error(xmlparseobject *self)
108{
109 PyObject *err;
110 char buffer[256];
111 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000112 int lineno = XML_GetErrorLineNumber(parser);
113 int column = XML_GetErrorColumnNumber(parser);
114 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000115
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000116 /* There is no risk of overflowing this buffer, since
117 even for 64-bit integers, there is sufficient space. */
118 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000119 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000120 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000121 if ( err != NULL
122 && set_error_attr(err, "code", code)
123 && set_error_attr(err, "offset", column)
124 && set_error_attr(err, "lineno", lineno)) {
125 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000126 }
127 return NULL;
128}
129
Fred Drake71b63ff2002-06-28 22:29:01 +0000130static int
131have_handler(xmlparseobject *self, int type)
132{
133 PyObject *handler = self->handlers[type];
134 return handler != NULL;
135}
136
137static PyObject *
138get_handler_name(struct HandlerInfo *hinfo)
139{
140 PyObject *name = hinfo->nameobj;
141 if (name == NULL) {
142 name = PyString_FromString(hinfo->name);
143 hinfo->nameobj = name;
144 }
145 Py_XINCREF(name);
146 return name;
147}
148
Fred Drake85d835f2001-02-08 15:39:08 +0000149
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000150#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000151/* Convert a string of XML_Chars into a Unicode string.
152 Returns None if str is a null pointer. */
153
Fred Drake0582df92000-07-12 04:49:00 +0000154static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000155conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000156{
Fred Drake71b63ff2002-06-28 22:29:01 +0000157 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000158 and hence in UTF-8. */
159 /* UTF-8 from Expat, Unicode desired */
160 if (str == NULL) {
161 Py_INCREF(Py_None);
162 return Py_None;
163 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000164 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000165}
166
Fred Drake0582df92000-07-12 04:49:00 +0000167static PyObject *
168conv_string_len_to_unicode(const XML_Char *str, int len)
169{
Fred Drake71b63ff2002-06-28 22:29:01 +0000170 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000171 and hence in UTF-8. */
172 /* UTF-8 from Expat, Unicode desired */
173 if (str == NULL) {
174 Py_INCREF(Py_None);
175 return Py_None;
176 }
Fred Drake6f987622000-08-25 18:03:30 +0000177 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000178}
179#endif
180
181/* Convert a string of XML_Chars into an 8-bit Python string.
182 Returns None if str is a null pointer. */
183
Fred Drake6f987622000-08-25 18:03:30 +0000184static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000185conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000186{
Fred Drake71b63ff2002-06-28 22:29:01 +0000187 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000188 and hence in UTF-8. */
189 /* UTF-8 from Expat, UTF-8 desired */
190 if (str == NULL) {
191 Py_INCREF(Py_None);
192 return Py_None;
193 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000194 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000195}
196
Fred Drake6f987622000-08-25 18:03:30 +0000197static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000198conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000199{
Fred Drake71b63ff2002-06-28 22:29:01 +0000200 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000201 and hence in UTF-8. */
202 /* UTF-8 from Expat, UTF-8 desired */
203 if (str == NULL) {
204 Py_INCREF(Py_None);
205 return Py_None;
206 }
207 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000208}
209
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000210/* Callback routines */
211
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000212static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000213
Fred Drake6f987622000-08-25 18:03:30 +0000214static void
215flag_error(xmlparseobject *self)
216{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000217 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000218}
219
220static PyCodeObject*
221getcode(enum HandlerTypes slot, char* func_name, int lineno)
222{
Fred Drakebd6101c2001-02-14 18:29:45 +0000223 PyObject *code = NULL;
224 PyObject *name = NULL;
225 PyObject *nulltuple = NULL;
226 PyObject *filename = NULL;
227
228 if (handler_info[slot].tb_code == NULL) {
229 code = PyString_FromString("");
230 if (code == NULL)
231 goto failed;
232 name = PyString_FromString(func_name);
233 if (name == NULL)
234 goto failed;
235 nulltuple = PyTuple_New(0);
236 if (nulltuple == NULL)
237 goto failed;
238 filename = PyString_FromString(__FILE__);
239 handler_info[slot].tb_code =
240 PyCode_New(0, /* argcount */
241 0, /* nlocals */
242 0, /* stacksize */
243 0, /* flags */
244 code, /* code */
245 nulltuple, /* consts */
246 nulltuple, /* names */
247 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000248#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000249 nulltuple, /* freevars */
250 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000251#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000252 filename, /* filename */
253 name, /* name */
254 lineno, /* firstlineno */
255 code /* lnotab */
256 );
257 if (handler_info[slot].tb_code == NULL)
258 goto failed;
259 Py_DECREF(code);
260 Py_DECREF(nulltuple);
261 Py_DECREF(filename);
262 Py_DECREF(name);
263 }
264 return handler_info[slot].tb_code;
265 failed:
266 Py_XDECREF(code);
267 Py_XDECREF(name);
268 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000269}
270
271static PyObject*
272call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
273{
Fred Drakebd6101c2001-02-14 18:29:45 +0000274 PyThreadState *tstate = PyThreadState_GET();
275 PyFrameObject *f;
276 PyObject *res;
277
278 if (c == NULL)
279 return NULL;
280 f = PyFrame_New(
281 tstate, /*back*/
282 c, /*code*/
283 tstate->frame->f_globals, /*globals*/
284 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000285 );
286 if (f == NULL)
287 return NULL;
288 tstate->frame = f;
289 res = PyEval_CallObject(func, args);
290 if (res == NULL && tstate->curexc_traceback == NULL)
291 PyTraceBack_Here(f);
292 tstate->frame = f->f_back;
293 Py_DECREF(f);
294 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000295}
296
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000297#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000298#define STRING_CONV_FUNC conv_string_to_utf8
299#else
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000300/* Python 2.0 and later versions */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000301#define STRING_CONV_FUNC (self->returns_unicode \
302 ? conv_string_to_unicode : conv_string_to_utf8)
303#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000304
Fred Drakeb91a36b2002-06-27 19:40:48 +0000305static PyObject*
306string_intern(xmlparseobject *self, const char* str)
307{
308 PyObject *result = STRING_CONV_FUNC(str);
309 PyObject *value;
310 if (!self->intern)
311 return result;
312 value = PyDict_GetItem(self->intern, result);
313 if (!value) {
314 if (PyDict_SetItem(self->intern, result, result) == 0)
315 return result;
316 else
317 return NULL;
318 }
319 Py_INCREF(value);
320 Py_DECREF(result);
321 return value;
322}
323
Fred Drake2a3d7db2002-06-28 22:56:48 +0000324/* Return 0 on success, -1 on exception.
325 * flag_error() will be called before return if needed.
326 */
327static int
328call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
329{
330 PyObject *args;
331 PyObject *temp;
332
333 args = PyTuple_New(1);
334 if (args == NULL)
335 return -1;
336#ifdef Py_USING_UNICODE
337 temp = (self->returns_unicode
338 ? conv_string_len_to_unicode(buffer, len)
339 : conv_string_len_to_utf8(buffer, len));
340#else
341 temp = conv_string_len_to_utf8(buffer, len);
342#endif
343 if (temp == NULL) {
344 Py_DECREF(args);
345 flag_error(self);
346 return -1;
347 }
348 PyTuple_SET_ITEM(args, 0, temp);
349 /* temp is now a borrowed reference; consider it unused. */
350 self->in_callback = 1;
351 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
352 self->handlers[CharacterData], args);
353 /* temp is an owned reference again, or NULL */
354 self->in_callback = 0;
355 Py_DECREF(args);
356 if (temp == NULL) {
357 flag_error(self);
358 return -1;
359 }
360 Py_DECREF(temp);
361 return 0;
362}
363
364static int
365flush_character_buffer(xmlparseobject *self)
366{
367 int rc;
368 if (self->buffer == NULL || self->buffer_used == 0)
369 return 0;
370 rc = call_character_handler(self, self->buffer, self->buffer_used);
371 self->buffer_used = 0;
372 return rc;
373}
374
375static void
376my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
377{
378 xmlparseobject *self = (xmlparseobject *) userData;
379 if (self->buffer == NULL)
380 call_character_handler(self, data, len);
381 else {
382 if ((self->buffer_used + len) > self->buffer_size) {
383 if (flush_character_buffer(self) < 0)
384 return;
385 /* handler might have changed; drop the rest on the floor
386 * if there isn't a handler anymore
387 */
388 if (!have_handler(self, CharacterData))
389 return;
390 }
391 if (len > self->buffer_size) {
392 call_character_handler(self, data, len);
393 self->buffer_used = 0;
394 }
395 else {
396 memcpy(self->buffer + self->buffer_used,
397 data, len * sizeof(XML_Char));
398 self->buffer_used += len;
399 }
400 }
401}
402
Fred Drake85d835f2001-02-08 15:39:08 +0000403static void
404my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000405 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000406{
407 xmlparseobject *self = (xmlparseobject *)userData;
408
Fred Drake71b63ff2002-06-28 22:29:01 +0000409 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000410 PyObject *container, *rv, *args;
411 int i, max;
412
Fred Drake2a3d7db2002-06-28 22:56:48 +0000413 if (flush_character_buffer(self) < 0)
414 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000415 /* Set max to the number of slots filled in atts[]; max/2 is
416 * the number of attributes we need to process.
417 */
418 if (self->specified_attributes) {
419 max = XML_GetSpecifiedAttributeCount(self->itself);
420 }
421 else {
422 max = 0;
423 while (atts[max] != NULL)
424 max += 2;
425 }
426 /* Build the container. */
427 if (self->ordered_attributes)
428 container = PyList_New(max);
429 else
430 container = PyDict_New();
431 if (container == NULL) {
432 flag_error(self);
433 return;
434 }
435 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000436 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000437 PyObject *v;
438 if (n == NULL) {
439 flag_error(self);
440 Py_DECREF(container);
441 return;
442 }
443 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
444 if (v == NULL) {
445 flag_error(self);
446 Py_DECREF(container);
447 Py_DECREF(n);
448 return;
449 }
450 if (self->ordered_attributes) {
451 PyList_SET_ITEM(container, i, n);
452 PyList_SET_ITEM(container, i+1, v);
453 }
454 else if (PyDict_SetItem(container, n, v)) {
455 flag_error(self);
456 Py_DECREF(n);
457 Py_DECREF(v);
458 return;
459 }
460 else {
461 Py_DECREF(n);
462 Py_DECREF(v);
463 }
464 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000465 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000466 if (args == NULL) {
467 Py_DECREF(container);
468 return;
469 }
470 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000471 self->in_callback = 1;
472 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000473 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000474 self->in_callback = 0;
475 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000476 if (rv == NULL) {
477 flag_error(self);
478 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000479 }
Fred Drake85d835f2001-02-08 15:39:08 +0000480 Py_DECREF(rv);
481 }
482}
483
484#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
485 RETURN, GETUSERDATA) \
486static RC \
487my_##NAME##Handler PARAMS {\
488 xmlparseobject *self = GETUSERDATA ; \
489 PyObject *args = NULL; \
490 PyObject *rv = NULL; \
491 INIT \
492\
Fred Drake71b63ff2002-06-28 22:29:01 +0000493 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000494 if (flush_character_buffer(self) < 0) \
495 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000496 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000497 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000498 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000499 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
500 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000501 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000502 Py_DECREF(args); \
503 if (rv == NULL) { \
504 flag_error(self); \
505 return RETURN; \
506 } \
507 CONVERSION \
508 Py_DECREF(rv); \
509 } \
510 return RETURN; \
511}
512
Fred Drake6f987622000-08-25 18:03:30 +0000513#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
514 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
515 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000516
Fred Drake6f987622000-08-25 18:03:30 +0000517#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
518 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
519 rc = PyInt_AsLong(rv);, rc, \
520 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000521
Fred Drake71b63ff2002-06-28 22:29:01 +0000522VOID_HANDLER(EndElement,
523 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000524 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000525
Fred Drake6f987622000-08-25 18:03:30 +0000526VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000527 (void *userData,
528 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000529 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000530 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000531
Fred Drake6f987622000-08-25 18:03:30 +0000532VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000533 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000534 const XML_Char *entityName,
535 const XML_Char *base,
536 const XML_Char *systemId,
537 const XML_Char *publicId,
538 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000539 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000540 string_intern(self, entityName), string_intern(self, base),
541 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000542 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000543
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000544#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000545VOID_HANDLER(EntityDecl,
546 (void *userData,
547 const XML_Char *entityName,
548 int is_parameter_entity,
549 const XML_Char *value,
550 int value_length,
551 const XML_Char *base,
552 const XML_Char *systemId,
553 const XML_Char *publicId,
554 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000555 ("NiNNNNN",
556 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000557 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000558 string_intern(self, base), string_intern(self, systemId),
559 string_intern(self, publicId),
560 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000561#else
562VOID_HANDLER(EntityDecl,
563 (void *userData,
564 const XML_Char *entityName,
565 int is_parameter_entity,
566 const XML_Char *value,
567 int value_length,
568 const XML_Char *base,
569 const XML_Char *systemId,
570 const XML_Char *publicId,
571 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000572 ("NiNNNNN",
573 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000574 (self->returns_unicode
575 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000576 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000577 string_intern(self, base), string_intern(self, systemId),
578 string_intern(self, publicId),
579 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000580#endif
581
582VOID_HANDLER(XmlDecl,
583 (void *userData,
584 const XML_Char *version,
585 const XML_Char *encoding,
586 int standalone),
587 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000588 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000589 standalone))
590
591static PyObject *
592conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000593 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000594{
595 PyObject *result = NULL;
596 PyObject *children = PyTuple_New(model->numchildren);
597 int i;
598
599 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000600 assert(model->numchildren < INT_MAX);
601 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000602 PyObject *child = conv_content_model(&model->children[i],
603 conv_string);
604 if (child == NULL) {
605 Py_XDECREF(children);
606 return NULL;
607 }
608 PyTuple_SET_ITEM(children, i, child);
609 }
610 result = Py_BuildValue("(iiO&N)",
611 model->type, model->quant,
612 conv_string,model->name, children);
613 }
614 return result;
615}
616
617static PyObject *
618conv_content_model_utf8(XML_Content * const model)
619{
620 return conv_content_model(model, conv_string_to_utf8);
621}
622
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000623#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000624static PyObject *
625conv_content_model_unicode(XML_Content * const model)
626{
627 return conv_content_model(model, conv_string_to_unicode);
628}
629
630VOID_HANDLER(ElementDecl,
631 (void *userData,
632 const XML_Char *name,
633 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000634 ("NO&",
635 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000636 (self->returns_unicode ? conv_content_model_unicode
637 : conv_content_model_utf8),model))
638#else
639VOID_HANDLER(ElementDecl,
640 (void *userData,
641 const XML_Char *name,
642 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000643 ("NO&",
644 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000645#endif
646
647VOID_HANDLER(AttlistDecl,
648 (void *userData,
649 const XML_Char *elname,
650 const XML_Char *attname,
651 const XML_Char *att_type,
652 const XML_Char *dflt,
653 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000654 ("(NNO&O&i)",
655 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000656 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
657 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000658
Fred Drake71b63ff2002-06-28 22:29:01 +0000659VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000660 (void *userData,
661 const XML_Char *notationName,
662 const XML_Char *base,
663 const XML_Char *systemId,
664 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000665 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000666 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000667 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000668
Fred Drake6f987622000-08-25 18:03:30 +0000669VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000670 (void *userData,
671 const XML_Char *prefix,
672 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000673 ("(NN)",
674 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000675
Fred Drake6f987622000-08-25 18:03:30 +0000676VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000677 (void *userData,
678 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000679 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680
Fred Drake6f987622000-08-25 18:03:30 +0000681VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000682 (void *userData, const XML_Char *data),
683 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000684
Fred Drake6f987622000-08-25 18:03:30 +0000685VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000686 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000687 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000688
Fred Drake6f987622000-08-25 18:03:30 +0000689VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000690 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000691 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000692
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000693#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000694VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000695 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000696 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000697
Fred Drake6f987622000-08-25 18:03:30 +0000698VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000699 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000700 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701#else
Fred Drake6f987622000-08-25 18:03:30 +0000702VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000703 (void *userData, const XML_Char *s, int len),
704 ("(N)", (self->returns_unicode
705 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000706 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000707
Fred Drake6f987622000-08-25 18:03:30 +0000708VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000709 (void *userData, const XML_Char *s, int len),
710 ("(N)", (self->returns_unicode
711 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000712 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000713#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000714
Fred Drake71b63ff2002-06-28 22:29:01 +0000715INT_HANDLER(NotStandalone,
716 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000717 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718
Fred Drake6f987622000-08-25 18:03:30 +0000719RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000720 (XML_Parser parser,
721 const XML_Char *context,
722 const XML_Char *base,
723 const XML_Char *systemId,
724 const XML_Char *publicId),
725 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000726 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000727 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000728 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000729 rc = PyInt_AsLong(rv);, rc,
730 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000731
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000732/* XXX UnknownEncodingHandler */
733
Fred Drake85d835f2001-02-08 15:39:08 +0000734VOID_HANDLER(StartDoctypeDecl,
735 (void *userData, const XML_Char *doctypeName,
736 const XML_Char *sysid, const XML_Char *pubid,
737 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000738 ("(NNNi)", string_intern(self, doctypeName),
739 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000740 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000741
742VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000743
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000744/* ---------------------------------------------------------------- */
745
Fred Drake71b63ff2002-06-28 22:29:01 +0000746static PyObject *
747get_parse_result(xmlparseobject *self, int rv)
748{
749 if (PyErr_Occurred()) {
750 return NULL;
751 }
752 if (rv == 0) {
753 return set_error(self);
754 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000755 if (flush_character_buffer(self) < 0) {
756 return NULL;
757 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000758 return PyInt_FromLong(rv);
759}
760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000761PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000762"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764
765static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000766xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000767{
Fred Drake0582df92000-07-12 04:49:00 +0000768 char *s;
769 int slen;
770 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000771
Fred Drake0582df92000-07-12 04:49:00 +0000772 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
773 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000774
775 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776}
777
Fred Drakeca1f4262000-09-21 20:10:23 +0000778/* File reading copied from cPickle */
779
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000780#define BUF_SIZE 2048
781
Fred Drake0582df92000-07-12 04:49:00 +0000782static int
783readinst(char *buf, int buf_size, PyObject *meth)
784{
785 PyObject *arg = NULL;
786 PyObject *bytes = NULL;
787 PyObject *str = NULL;
788 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789
Fred Drake676940b2000-09-22 15:21:31 +0000790 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000791 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000792
Fred Drakeca1f4262000-09-21 20:10:23 +0000793 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000794 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000795
Tim Peters954eef72000-09-22 06:01:11 +0000796 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000797
Fred Drakeca1f4262000-09-21 20:10:23 +0000798 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000799 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000800
Fred Drake0582df92000-07-12 04:49:00 +0000801 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000802 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000803 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000804 "read() did not return a string object (type=%.400s)",
805 str->ob_type->tp_name);
806 goto finally;
807 }
808 len = PyString_GET_SIZE(str);
809 if (len > buf_size) {
810 PyErr_Format(PyExc_ValueError,
811 "read() returned too much data: "
812 "%i bytes requested, %i returned",
813 buf_size, len);
814 Py_DECREF(str);
815 goto finally;
816 }
817 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000818finally:
Fred Drake0582df92000-07-12 04:49:00 +0000819 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000820 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000821 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000822}
823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000824PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000825"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000826Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000827
828static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000829xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000830{
Fred Drake0582df92000-07-12 04:49:00 +0000831 int rv = 1;
832 PyObject *f;
833 FILE *fp;
834 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000835
Fred Drake0582df92000-07-12 04:49:00 +0000836 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
837 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838
Fred Drake0582df92000-07-12 04:49:00 +0000839 if (PyFile_Check(f)) {
840 fp = PyFile_AsFile(f);
841 }
842 else{
843 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000844 readmethod = PyObject_GetAttrString(f, "read");
845 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000846 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000847 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000848 "argument must have 'read' attribute");
849 return 0;
850 }
851 }
852 for (;;) {
853 int bytes_read;
854 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
855 if (buf == NULL)
856 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000857
Fred Drake0582df92000-07-12 04:49:00 +0000858 if (fp) {
859 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
860 if (bytes_read < 0) {
861 PyErr_SetFromErrno(PyExc_IOError);
862 return NULL;
863 }
864 }
865 else {
866 bytes_read = readinst(buf, BUF_SIZE, readmethod);
867 if (bytes_read < 0)
868 return NULL;
869 }
870 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
871 if (PyErr_Occurred())
872 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000873
Fred Drake0582df92000-07-12 04:49:00 +0000874 if (!rv || bytes_read == 0)
875 break;
876 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000877 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878}
879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000881"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000883
884static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000885xmlparse_SetBase(xmlparseobject *self, PyObject *args)
886{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000887 char *base;
888
Fred Drake0582df92000-07-12 04:49:00 +0000889 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000891 if (!XML_SetBase(self->itself, base)) {
892 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000893 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000894 Py_INCREF(Py_None);
895 return Py_None;
896}
897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000898PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000899"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000901
902static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000903xmlparse_GetBase(xmlparseobject *self, PyObject *args)
904{
905 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000906 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907
Fred Drake0582df92000-07-12 04:49:00 +0000908 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000909}
910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000911PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000912"GetInputContext() -> string\n\
913Return the untranslated text of the input that caused the current event.\n\
914If 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 +0000915for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000916
917static PyObject *
918xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
919{
920 PyObject *result = NULL;
921
922 if (PyArg_ParseTuple(args, ":GetInputContext")) {
923 if (self->in_callback) {
924 int offset, size;
925 const char *buffer
926 = XML_GetInputContext(self->itself, &offset, &size);
927
928 if (buffer != NULL)
929 result = PyString_FromStringAndSize(buffer + offset, size);
930 else {
931 result = Py_None;
932 Py_INCREF(result);
933 }
934 }
935 else {
936 result = Py_None;
937 Py_INCREF(result);
938 }
939 }
940 return result;
941}
Fred Drakebd6101c2001-02-14 18:29:45 +0000942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000943PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000944"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000945Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000946information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000947
948static PyObject *
949xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
950{
951 char *context;
952 char *encoding = NULL;
953 xmlparseobject *new_parser;
954 int i;
955
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000956 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000957 &context, &encoding)) {
958 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000959 }
960
Martin v. Löwis894258c2001-09-23 10:20:10 +0000961#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000962 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000963 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000964#else
965 /* Python versions 2.2 and later */
966 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
967#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000968
969 if (new_parser == NULL)
970 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000971 new_parser->buffer_size = self->buffer_size;
972 new_parser->buffer_used = 0;
973 if (self->buffer != NULL) {
974 new_parser->buffer = malloc(new_parser->buffer_size);
975 if (new_parser->buffer == NULL) {
976 PyObject_GC_Del(new_parser);
977 return PyErr_NoMemory();
978 }
979 }
980 else
981 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000982 new_parser->returns_unicode = self->returns_unicode;
983 new_parser->ordered_attributes = self->ordered_attributes;
984 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000985 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000986 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000987 encoding);
988 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000989 new_parser->intern = self->intern;
990 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000991#ifdef Py_TPFLAGS_HAVE_GC
992 PyObject_GC_Track(new_parser);
993#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000994 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000995#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000996
997 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000998 Py_DECREF(new_parser);
999 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001000 }
1001
1002 XML_SetUserData(new_parser->itself, (void *)new_parser);
1003
1004 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001005 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001006 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001007
Fred Drake2a3d7db2002-06-28 22:56:48 +00001008 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001009 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001010 Py_DECREF(new_parser);
1011 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001012 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001013 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001014
1015 /* then copy handlers from self */
1016 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001017 PyObject *handler = self->handlers[i];
1018 if (handler != NULL) {
1019 Py_INCREF(handler);
1020 new_parser->handlers[i] = handler;
1021 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001022 handler_info[i].handler);
1023 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001024 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001025 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026}
1027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001029"SetParamEntityParsing(flag) -> success\n\
1030Controls parsing of parameter entities (including the external DTD\n\
1031subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1032XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1033XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001034was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001035
1036static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001037xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038{
Fred Drake85d835f2001-02-08 15:39:08 +00001039 int flag;
1040 if (!PyArg_ParseTuple(args, "i", &flag))
1041 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001042 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001043 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001044}
1045
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001046static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001047 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001048 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001049 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001051 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001052 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001053 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001054 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001055 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1056 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001057 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1058 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001059 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1060 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001061 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001062};
1063
1064/* ---------- */
1065
1066
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001067#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001068
Fred Drake71b63ff2002-06-28 22:29:01 +00001069/* pyexpat international encoding support.
1070 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001071*/
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
Fred Drake71b63ff2002-06-28 22:29:01 +00001076static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077init_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
Fred Drake71b63ff2002-06-28 22:29:01 +00001086static int
1087PyUnknownEncodingHandler(void *encodingHandlerData,
1088 const XML_Char *name,
1089 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001090{
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;
Fred Drake71b63ff2002-06-28 22:29:01 +00001094
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");
Fred Drake71b63ff2002-06-28 22:29:01 +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;
Fred Drake71b63ff2002-06-28 22:29:01 +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 info->data = NULL;
1111 info->convert = NULL;
1112 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001113 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001114 Py_DECREF(_u_string);
1115 return result;
1116}
1117
1118#endif
1119
1120static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001121newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001122{
1123 int i;
1124 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001125
Martin v. Löwis894258c2001-09-23 10:20:10 +00001126#ifdef Py_TPFLAGS_HAVE_GC
1127 /* Code for versions 2.2 and later */
1128 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1129#else
Fred Drake0582df92000-07-12 04:49:00 +00001130 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001131#endif
Fred Drake0582df92000-07-12 04:49:00 +00001132 if (self == NULL)
1133 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001134
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001135#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001136 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001137#else
1138 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001139#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001140
Fred Drake2a3d7db2002-06-28 22:56:48 +00001141 self->buffer = NULL;
1142 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1143 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001144 self->ordered_attributes = 0;
1145 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001146 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001147 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001148 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001149 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1150 }
Fred Drake85d835f2001-02-08 15:39:08 +00001151 else {
Fred Drake0582df92000-07-12 04:49:00 +00001152 self->itself = XML_ParserCreate(encoding);
1153 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001154 self->intern = intern;
1155 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001156#ifdef Py_TPFLAGS_HAVE_GC
1157 PyObject_GC_Track(self);
1158#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001159 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001160#endif
Fred Drake0582df92000-07-12 04:49:00 +00001161 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001162 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001163 "XML_ParserCreate failed");
1164 Py_DECREF(self);
1165 return NULL;
1166 }
1167 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001168#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001169 XML_SetUnknownEncodingHandler(self->itself,
1170 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001171#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001172
Fred Drake2a3d7db2002-06-28 22:56:48 +00001173 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001174 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001175
Fred Drake7c75bf22002-07-01 14:02:31 +00001176 self->handlers = malloc(sizeof(PyObject *) * i);
1177 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001178 Py_DECREF(self);
1179 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001181 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001182
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001184}
1185
1186
1187static void
Fred Drake0582df92000-07-12 04:49:00 +00001188xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189{
Fred Drake0582df92000-07-12 04:49:00 +00001190 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001191#ifdef Py_TPFLAGS_HAVE_GC
1192 PyObject_GC_UnTrack(self);
1193#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001194 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001195#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001196 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001197 XML_ParserFree(self->itself);
1198 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001199
Fred Drake85d835f2001-02-08 15:39:08 +00001200 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001201 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001202 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001203 temp = self->handlers[i];
1204 self->handlers[i] = NULL;
1205 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001206 }
1207 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001208 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001209 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001210 if (self->buffer != NULL) {
1211 free(self->buffer);
1212 self->buffer = NULL;
1213 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001214 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001215#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001216 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001217 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001218#else
1219 /* Code for versions 2.2 and later. */
1220 PyObject_GC_Del(self);
1221#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001222}
1223
Fred Drake0582df92000-07-12 04:49:00 +00001224static int
1225handlername2int(const char *name)
1226{
1227 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001228 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (strcmp(name, handler_info[i].name) == 0) {
1230 return i;
1231 }
1232 }
1233 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001234}
1235
1236static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001237get_pybool(int istrue)
1238{
1239 PyObject *result = istrue ? Py_True : Py_False;
1240 Py_INCREF(result);
1241 return result;
1242}
1243
1244static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001245xmlparse_getattr(xmlparseobject *self, char *name)
1246{
Fred Drake71b63ff2002-06-28 22:29:01 +00001247 int handlernum = handlername2int(name);
1248
1249 if (handlernum != -1) {
1250 PyObject *result = self->handlers[handlernum];
1251 if (result == NULL)
1252 result = Py_None;
1253 Py_INCREF(result);
1254 return result;
1255 }
1256 if (name[0] == 'E') {
1257 if (strcmp(name, "ErrorCode") == 0)
1258 return PyInt_FromLong((long)
1259 XML_GetErrorCode(self->itself));
1260 if (strcmp(name, "ErrorLineNumber") == 0)
1261 return PyInt_FromLong((long)
1262 XML_GetErrorLineNumber(self->itself));
1263 if (strcmp(name, "ErrorColumnNumber") == 0)
1264 return PyInt_FromLong((long)
1265 XML_GetErrorColumnNumber(self->itself));
1266 if (strcmp(name, "ErrorByteIndex") == 0)
1267 return PyInt_FromLong((long)
1268 XML_GetErrorByteIndex(self->itself));
1269 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001270 if (name[0] == 'b') {
1271 if (strcmp(name, "buffer_size") == 0)
1272 return PyInt_FromLong((long) self->buffer_size);
1273 if (strcmp(name, "buffer_text") == 0)
1274 return get_pybool(self->buffer != NULL);
1275 if (strcmp(name, "buffer_used") == 0)
1276 return PyInt_FromLong((long) self->buffer_used);
1277 }
Fred Drake85d835f2001-02-08 15:39:08 +00001278 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001279 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001280 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001281 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001282 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001283 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001284 if (strcmp(name, "intern") == 0) {
1285 if (self->intern == NULL) {
1286 Py_INCREF(Py_None);
1287 return Py_None;
1288 }
1289 else {
1290 Py_INCREF(self->intern);
1291 return self->intern;
1292 }
1293 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001294
Fred Drake0582df92000-07-12 04:49:00 +00001295 if (strcmp(name, "__members__") == 0) {
1296 int i;
1297 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001298 for (i = 0; handler_info[i].name != NULL; i++) {
1299 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001300 }
1301 PyList_Append(rc, PyString_FromString("ErrorCode"));
1302 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1303 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1304 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake2a3d7db2002-06-28 22:56:48 +00001305 PyList_Append(rc, PyString_FromString("buffer_size"));
1306 PyList_Append(rc, PyString_FromString("buffer_text"));
1307 PyList_Append(rc, PyString_FromString("buffer_used"));
Fred Drake85d835f2001-02-08 15:39:08 +00001308 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001309 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001310 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001311 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001312
Fred Drake0582df92000-07-12 04:49:00 +00001313 return rc;
1314 }
1315 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001316}
1317
Fred Drake6f987622000-08-25 18:03:30 +00001318static int
1319sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001320{
1321 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001322 if (handlernum >= 0) {
1323 xmlhandler c_handler = NULL;
1324 PyObject *temp = self->handlers[handlernum];
1325
1326 if (v == Py_None)
1327 v = NULL;
1328 else if (v != NULL) {
1329 Py_INCREF(v);
1330 c_handler = handler_info[handlernum].handler;
1331 }
Fred Drake0582df92000-07-12 04:49:00 +00001332 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001333 Py_XDECREF(temp);
1334 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001335 return 1;
1336 }
1337 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001338}
1339
1340static int
Fred Drake6f987622000-08-25 18:03:30 +00001341xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001342{
Fred Drake6f987622000-08-25 18:03:30 +00001343 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001344 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001345 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1346 return -1;
1347 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001348 if (strcmp(name, "buffer_text") == 0) {
1349 if (PyObject_IsTrue(v)) {
1350 if (self->buffer == NULL) {
1351 self->buffer = malloc(self->buffer_size);
1352 if (self->buffer == NULL) {
1353 PyErr_NoMemory();
1354 return -1;
1355 }
1356 self->buffer_used = 0;
1357 }
1358 }
1359 else if (self->buffer != NULL) {
1360 if (flush_character_buffer(self) < 0)
1361 return -1;
1362 free(self->buffer);
1363 self->buffer = NULL;
1364 }
1365 return 0;
1366 }
Fred Drake85d835f2001-02-08 15:39:08 +00001367 if (strcmp(name, "ordered_attributes") == 0) {
1368 if (PyObject_IsTrue(v))
1369 self->ordered_attributes = 1;
1370 else
1371 self->ordered_attributes = 0;
1372 return 0;
1373 }
Fred Drake6f987622000-08-25 18:03:30 +00001374 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001375 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001376#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001377 PyErr_SetString(PyExc_ValueError,
1378 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001379 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001380#else
Fred Drake6f987622000-08-25 18:03:30 +00001381 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001382#endif
Fred Drake6f987622000-08-25 18:03:30 +00001383 }
1384 else
1385 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001386 return 0;
1387 }
1388 if (strcmp(name, "specified_attributes") == 0) {
1389 if (PyObject_IsTrue(v))
1390 self->specified_attributes = 1;
1391 else
1392 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001393 return 0;
1394 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001395 if (strcmp(name, "CharacterDataHandler") == 0) {
1396 /* If we're changing the character data handler, flush all
1397 * cached data with the old handler. Not sure there's a
1398 * "right" thing to do, though, but this probably won't
1399 * happen.
1400 */
1401 if (flush_character_buffer(self) < 0)
1402 return -1;
1403 }
Fred Drake6f987622000-08-25 18:03:30 +00001404 if (sethandler(self, name, v)) {
1405 return 0;
1406 }
1407 PyErr_SetString(PyExc_AttributeError, name);
1408 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001409}
1410
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001411#ifdef WITH_CYCLE_GC
1412static int
1413xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1414{
Fred Drakecde79132001-04-25 16:01:30 +00001415 int i, err;
1416 for (i = 0; handler_info[i].name != NULL; i++) {
1417 if (!op->handlers[i])
1418 continue;
1419 err = visit(op->handlers[i], arg);
1420 if (err)
1421 return err;
1422 }
1423 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001424}
1425
1426static int
1427xmlparse_clear(xmlparseobject *op)
1428{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001429 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001430 Py_XDECREF(op->intern);
1431 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001432 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001433}
1434#endif
1435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001436PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001437
1438static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001439 PyObject_HEAD_INIT(NULL)
1440 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001441 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001442 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001443 0, /*tp_itemsize*/
1444 /* methods */
1445 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1446 (printfunc)0, /*tp_print*/
1447 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1448 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1449 (cmpfunc)0, /*tp_compare*/
1450 (reprfunc)0, /*tp_repr*/
1451 0, /*tp_as_number*/
1452 0, /*tp_as_sequence*/
1453 0, /*tp_as_mapping*/
1454 (hashfunc)0, /*tp_hash*/
1455 (ternaryfunc)0, /*tp_call*/
1456 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001457 0, /* tp_getattro */
1458 0, /* tp_setattro */
1459 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001460#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001461 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001462#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001464#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001465 Xmlparsetype__doc__, /* Documentation string */
1466#ifdef WITH_CYCLE_GC
1467 (traverseproc)xmlparse_traverse, /* tp_traverse */
1468 (inquiry)xmlparse_clear /* tp_clear */
1469#else
1470 0, 0
1471#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001472};
1473
1474/* End of code for xmlparser objects */
1475/* -------------------------------------------------------- */
1476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001477PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001478"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001480
1481static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001482pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1483{
Fred Drakecde79132001-04-25 16:01:30 +00001484 char *encoding = NULL;
1485 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001486 PyObject *intern = NULL;
1487 PyObject *result;
1488 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001489 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001490 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001491
Fred Drakeb91a36b2002-06-27 19:40:48 +00001492 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1493 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001494 return NULL;
1495 if (namespace_separator != NULL
1496 && strlen(namespace_separator) > 1) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "namespace_separator must be at most one"
1499 " character, omitted, or None");
1500 return NULL;
1501 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001502 /* Explicitly passing None means no interning is desired.
1503 Not passing anything means that a new dictionary is used. */
1504 if (intern == Py_None)
1505 intern = NULL;
1506 else if (intern == NULL) {
1507 intern = PyDict_New();
1508 if (!intern)
1509 return NULL;
1510 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001511 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001512 else if (!PyDict_Check(intern)) {
1513 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1514 return NULL;
1515 }
1516
1517 result = newxmlparseobject(encoding, namespace_separator, intern);
1518 if (intern_decref) {
1519 Py_DECREF(intern);
1520 }
1521 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001522}
1523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001525"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001527
1528static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001529pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001530{
Fred Drake0582df92000-07-12 04:49:00 +00001531 long code = 0;
1532
1533 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1534 return NULL;
1535 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001536}
1537
1538/* List of methods defined in the module */
1539
1540static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001541 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1542 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1543 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1544 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001545
Fred Drake0582df92000-07-12 04:49:00 +00001546 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001547};
1548
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001549/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001551PyDoc_STRVAR(pyexpat_module_documentation,
1552"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001553
Fred Drake4113b132001-03-24 19:58:26 +00001554/* Return a Python string that represents the version number without the
1555 * extra cruft added by revision control, even if the right options were
1556 * given to the "cvs export" command to make it not include the extra
1557 * cruft.
1558 */
1559static PyObject *
1560get_version_string(void)
1561{
1562 static char *rcsid = "$Revision$";
1563 char *rev = rcsid;
1564 int i = 0;
1565
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001566 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001567 ++rev;
1568 while (rev[i] != ' ' && rev[i] != '\0')
1569 ++i;
1570
1571 return PyString_FromStringAndSize(rev, i);
1572}
1573
Fred Drakecde79132001-04-25 16:01:30 +00001574/* Initialization function for the module */
1575
1576#ifndef MODULE_NAME
1577#define MODULE_NAME "pyexpat"
1578#endif
1579
1580#ifndef MODULE_INITFUNC
1581#define MODULE_INITFUNC initpyexpat
1582#endif
1583
1584void MODULE_INITFUNC(void); /* avoid compiler warnings */
1585
Fred Drake6f987622000-08-25 18:03:30 +00001586DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001587MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001588{
1589 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001590 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001591 PyObject *errors_module;
1592 PyObject *modelmod_name;
1593 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001594 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001595
Fred Drake6f987622000-08-25 18:03:30 +00001596 if (errmod_name == NULL)
1597 return;
Fred Drakecde79132001-04-25 16:01:30 +00001598 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001599 if (modelmod_name == NULL)
1600 return;
Fred Drake6f987622000-08-25 18:03:30 +00001601
Fred Drake0582df92000-07-12 04:49:00 +00001602 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001603
Fred Drake0582df92000-07-12 04:49:00 +00001604 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001605 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001606 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001607
Fred Drake0582df92000-07-12 04:49:00 +00001608 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001609 if (ErrorObject == NULL) {
1610 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001611 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001612 if (ErrorObject == NULL)
1613 return;
1614 }
1615 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001616 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001617 Py_INCREF(ErrorObject);
1618 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001619 Py_INCREF(&Xmlparsetype);
1620 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001621
Fred Drake4113b132001-03-24 19:58:26 +00001622 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001623 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1624 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001625 {
1626 XML_Expat_Version info = XML_ExpatVersionInfo();
1627 PyModule_AddObject(m, "version_info",
1628 Py_BuildValue("(iii)", info.major,
1629 info.minor, info.micro));
1630 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001631#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001632 init_template_buffer();
1633#endif
Fred Drake0582df92000-07-12 04:49:00 +00001634 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001635 compiled, this should check and set native_encoding
1636 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001637 */
Fred Drake93adb692000-09-23 04:55:48 +00001638 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001639
Fred Drake85d835f2001-02-08 15:39:08 +00001640 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001641 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001642 errors_module = PyDict_GetItem(d, errmod_name);
1643 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001644 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001645 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001646 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001647 /* gives away the reference to errors_module */
1648 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001649 }
1650 }
Fred Drake6f987622000-08-25 18:03:30 +00001651 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001652 model_module = PyDict_GetItem(d, modelmod_name);
1653 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001654 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001655 if (model_module != NULL) {
1656 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1657 /* gives away the reference to model_module */
1658 PyModule_AddObject(m, "model", model_module);
1659 }
1660 }
1661 Py_DECREF(modelmod_name);
1662 if (errors_module == NULL || model_module == NULL)
1663 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001664 return;
1665
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001666#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001667 PyModule_AddStringConstant(errors_module, #name, \
1668 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001669
Fred Drake0582df92000-07-12 04:49:00 +00001670 MYCONST(XML_ERROR_NO_MEMORY);
1671 MYCONST(XML_ERROR_SYNTAX);
1672 MYCONST(XML_ERROR_NO_ELEMENTS);
1673 MYCONST(XML_ERROR_INVALID_TOKEN);
1674 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1675 MYCONST(XML_ERROR_PARTIAL_CHAR);
1676 MYCONST(XML_ERROR_TAG_MISMATCH);
1677 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1678 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1679 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1680 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1681 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1682 MYCONST(XML_ERROR_ASYNC_ENTITY);
1683 MYCONST(XML_ERROR_BAD_CHAR_REF);
1684 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1685 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1686 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1687 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1688 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001689 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1690 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1691 MYCONST(XML_ERROR_NOT_STANDALONE);
1692
Fred Drake85d835f2001-02-08 15:39:08 +00001693 PyModule_AddStringConstant(errors_module, "__doc__",
1694 "Constants used to describe error conditions.");
1695
Fred Drake93adb692000-09-23 04:55:48 +00001696#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001697
Fred Drake85d835f2001-02-08 15:39:08 +00001698#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001699 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1700 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1701 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001702#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001703
Fred Drake85d835f2001-02-08 15:39:08 +00001704#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1705 PyModule_AddStringConstant(model_module, "__doc__",
1706 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001707
Fred Drake85d835f2001-02-08 15:39:08 +00001708 MYCONST(XML_CTYPE_EMPTY);
1709 MYCONST(XML_CTYPE_ANY);
1710 MYCONST(XML_CTYPE_MIXED);
1711 MYCONST(XML_CTYPE_NAME);
1712 MYCONST(XML_CTYPE_CHOICE);
1713 MYCONST(XML_CTYPE_SEQ);
1714
1715 MYCONST(XML_CQUANT_NONE);
1716 MYCONST(XML_CQUANT_OPT);
1717 MYCONST(XML_CQUANT_REP);
1718 MYCONST(XML_CQUANT_PLUS);
1719#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001720}
1721
Fred Drake6f987622000-08-25 18:03:30 +00001722static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001723clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001724{
Fred Drakecde79132001-04-25 16:01:30 +00001725 int i = 0;
1726 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001727
Fred Drake71b63ff2002-06-28 22:29:01 +00001728 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001729 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001730 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001731 else {
Fred Drakecde79132001-04-25 16:01:30 +00001732 temp = self->handlers[i];
1733 self->handlers[i] = NULL;
1734 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001735 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001736 }
Fred Drakecde79132001-04-25 16:01:30 +00001737 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001738}
1739
Fred Drake0582df92000-07-12 04:49:00 +00001740statichere struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001741 {"StartElementHandler",
1742 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001743 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001744 {"EndElementHandler",
1745 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001746 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001747 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001748 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1749 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001750 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001751 (xmlhandlersetter)XML_SetCharacterDataHandler,
1752 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001753 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001754 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001755 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001756 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001757 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001758 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001759 {"StartNamespaceDeclHandler",
1760 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001761 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001762 {"EndNamespaceDeclHandler",
1763 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001764 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001765 {"CommentHandler",
1766 (xmlhandlersetter)XML_SetCommentHandler,
1767 (xmlhandler)my_CommentHandler},
1768 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001769 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001770 (xmlhandler)my_StartCdataSectionHandler},
1771 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001772 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001773 (xmlhandler)my_EndCdataSectionHandler},
1774 {"DefaultHandler",
1775 (xmlhandlersetter)XML_SetDefaultHandler,
1776 (xmlhandler)my_DefaultHandler},
1777 {"DefaultHandlerExpand",
1778 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1779 (xmlhandler)my_DefaultHandlerExpandHandler},
1780 {"NotStandaloneHandler",
1781 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1782 (xmlhandler)my_NotStandaloneHandler},
1783 {"ExternalEntityRefHandler",
1784 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001785 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001786 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001787 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001788 (xmlhandler)my_StartDoctypeDeclHandler},
1789 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001790 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001791 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001792 {"EntityDeclHandler",
1793 (xmlhandlersetter)XML_SetEntityDeclHandler,
1794 (xmlhandler)my_EntityDeclHandler},
1795 {"XmlDeclHandler",
1796 (xmlhandlersetter)XML_SetXmlDeclHandler,
1797 (xmlhandler)my_XmlDeclHandler},
1798 {"ElementDeclHandler",
1799 (xmlhandlersetter)XML_SetElementDeclHandler,
1800 (xmlhandler)my_ElementDeclHandler},
1801 {"AttlistDeclHandler",
1802 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1803 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001804
Fred Drake0582df92000-07-12 04:49:00 +00001805 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001806};