blob: d65ede9e4ee4a34605a3206087f74b251ec9f66b [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00004#include "compile.h"
5#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00006#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00008#ifndef PyDoc_STRVAR
Fred Drake7c75bf22002-07-01 14:02:31 +00009#define PyDoc_STR(str) (str)
10#define PyDoc_VAR(name) static char name[]
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000011#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000012#endif
13
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000014#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
15/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000016#define Py_USING_UNICODE
17#endif
18
Fred Drake0582df92000-07-12 04:49:00 +000019enum HandlerTypes {
20 StartElement,
21 EndElement,
22 ProcessingInstruction,
23 CharacterData,
24 UnparsedEntityDecl,
25 NotationDecl,
26 StartNamespaceDecl,
27 EndNamespaceDecl,
28 Comment,
29 StartCdataSection,
30 EndCdataSection,
31 Default,
32 DefaultHandlerExpand,
33 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000034 ExternalEntityRef,
35 StartDoctypeDecl,
36 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000037 EntityDecl,
38 XmlDecl,
39 ElementDecl,
40 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000041 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000042};
43
44static PyObject *ErrorObject;
45
46/* ----------------------------------------------------- */
47
48/* Declarations for objects of type xmlparser */
49
50typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000051 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000052
Fred Drake0582df92000-07-12 04:49:00 +000053 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000054 int returns_unicode; /* True if Unicode strings are returned;
55 if false, UTF-8 strings are returned */
56 int ordered_attributes; /* Return attributes as a list. */
57 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000058 int in_callback; /* Is a callback active? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000059 XML_Char *buffer; /* Buffer used when accumulating characters */
60 /* NULL if not enabled */
61 int buffer_size; /* Size of buffer, in XML_Char units */
62 int buffer_used; /* Buffer units in use */
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
Fred Drake2a3d7db2002-06-28 22:56:48 +000067#define CHARACTER_DATA_BUFFER_SIZE 8192
68
Jeremy Hylton938ace62002-07-17 16:30:39 +000069static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070
Fred Drake6f987622000-08-25 18:03:30 +000071typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000072typedef void* xmlhandler;
73
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000074struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000075 const char *name;
76 xmlhandlersetter setter;
77 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000078 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000079 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080};
81
Jeremy Hylton938ace62002-07-17 16:30:39 +000082static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000083
Fred Drakebd6101c2001-02-14 18:29:45 +000084/* Set an integer attribute on the error object; return true on success,
85 * false on an exception.
86 */
87static int
88set_error_attr(PyObject *err, char *name, int value)
89{
90 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000091
Fred Drakebd6101c2001-02-14 18:29:45 +000092 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
93 Py_DECREF(v);
94 return 0;
95 }
96 return 1;
97}
98
99/* Build and set an Expat exception, including positioning
100 * information. Always returns NULL.
101 */
Fred Drake85d835f2001-02-08 15:39:08 +0000102static PyObject *
103set_error(xmlparseobject *self)
104{
105 PyObject *err;
106 char buffer[256];
107 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000108 int lineno = XML_GetErrorLineNumber(parser);
109 int column = XML_GetErrorColumnNumber(parser);
110 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000111
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000112 /* There is no risk of overflowing this buffer, since
113 even for 64-bit integers, there is sufficient space. */
114 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000115 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000116 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000117 if ( err != NULL
118 && set_error_attr(err, "code", code)
119 && set_error_attr(err, "offset", column)
120 && set_error_attr(err, "lineno", lineno)) {
121 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000122 }
123 return NULL;
124}
125
Fred Drake71b63ff2002-06-28 22:29:01 +0000126static int
127have_handler(xmlparseobject *self, int type)
128{
129 PyObject *handler = self->handlers[type];
130 return handler != NULL;
131}
132
133static PyObject *
134get_handler_name(struct HandlerInfo *hinfo)
135{
136 PyObject *name = hinfo->nameobj;
137 if (name == NULL) {
138 name = PyString_FromString(hinfo->name);
139 hinfo->nameobj = name;
140 }
141 Py_XINCREF(name);
142 return name;
143}
144
Fred Drake85d835f2001-02-08 15:39:08 +0000145
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000146#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000147/* Convert a string of XML_Chars into a Unicode string.
148 Returns None if str is a null pointer. */
149
Fred Drake0582df92000-07-12 04:49:00 +0000150static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000151conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000152{
Fred Drake71b63ff2002-06-28 22:29:01 +0000153 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000154 and hence in UTF-8. */
155 /* UTF-8 from Expat, Unicode desired */
156 if (str == NULL) {
157 Py_INCREF(Py_None);
158 return Py_None;
159 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000160 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000161}
162
Fred Drake0582df92000-07-12 04:49:00 +0000163static PyObject *
164conv_string_len_to_unicode(const XML_Char *str, int len)
165{
Fred Drake71b63ff2002-06-28 22:29:01 +0000166 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000167 and hence in UTF-8. */
168 /* UTF-8 from Expat, Unicode desired */
169 if (str == NULL) {
170 Py_INCREF(Py_None);
171 return Py_None;
172 }
Fred Drake6f987622000-08-25 18:03:30 +0000173 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000174}
175#endif
176
177/* Convert a string of XML_Chars into an 8-bit Python string.
178 Returns None if str is a null pointer. */
179
Fred Drake6f987622000-08-25 18:03:30 +0000180static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000181conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000182{
Fred Drake71b63ff2002-06-28 22:29:01 +0000183 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000184 and hence in UTF-8. */
185 /* UTF-8 from Expat, UTF-8 desired */
186 if (str == NULL) {
187 Py_INCREF(Py_None);
188 return Py_None;
189 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000190 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000191}
192
Fred Drake6f987622000-08-25 18:03:30 +0000193static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000194conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000195{
Fred Drake71b63ff2002-06-28 22:29:01 +0000196 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000197 and hence in UTF-8. */
198 /* UTF-8 from Expat, UTF-8 desired */
199 if (str == NULL) {
200 Py_INCREF(Py_None);
201 return Py_None;
202 }
203 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000204}
205
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000206/* Callback routines */
207
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000208static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000209
Fred Drake6f987622000-08-25 18:03:30 +0000210static void
211flag_error(xmlparseobject *self)
212{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000213 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000214}
215
216static PyCodeObject*
217getcode(enum HandlerTypes slot, char* func_name, int lineno)
218{
Fred Drakebd6101c2001-02-14 18:29:45 +0000219 PyObject *code = NULL;
220 PyObject *name = NULL;
221 PyObject *nulltuple = NULL;
222 PyObject *filename = NULL;
223
224 if (handler_info[slot].tb_code == NULL) {
225 code = PyString_FromString("");
226 if (code == NULL)
227 goto failed;
228 name = PyString_FromString(func_name);
229 if (name == NULL)
230 goto failed;
231 nulltuple = PyTuple_New(0);
232 if (nulltuple == NULL)
233 goto failed;
234 filename = PyString_FromString(__FILE__);
235 handler_info[slot].tb_code =
236 PyCode_New(0, /* argcount */
237 0, /* nlocals */
238 0, /* stacksize */
239 0, /* flags */
240 code, /* code */
241 nulltuple, /* consts */
242 nulltuple, /* names */
243 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000244#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000245 nulltuple, /* freevars */
246 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000247#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000248 filename, /* filename */
249 name, /* name */
250 lineno, /* firstlineno */
251 code /* lnotab */
252 );
253 if (handler_info[slot].tb_code == NULL)
254 goto failed;
255 Py_DECREF(code);
256 Py_DECREF(nulltuple);
257 Py_DECREF(filename);
258 Py_DECREF(name);
259 }
260 return handler_info[slot].tb_code;
261 failed:
262 Py_XDECREF(code);
263 Py_XDECREF(name);
264 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000265}
266
267static PyObject*
268call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
269{
Fred Drakebd6101c2001-02-14 18:29:45 +0000270 PyThreadState *tstate = PyThreadState_GET();
271 PyFrameObject *f;
272 PyObject *res;
273
274 if (c == NULL)
275 return NULL;
276 f = PyFrame_New(
277 tstate, /*back*/
278 c, /*code*/
279 tstate->frame->f_globals, /*globals*/
280 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000281 );
282 if (f == NULL)
283 return NULL;
284 tstate->frame = f;
285 res = PyEval_CallObject(func, args);
286 if (res == NULL && tstate->curexc_traceback == NULL)
287 PyTraceBack_Here(f);
288 tstate->frame = f->f_back;
289 Py_DECREF(f);
290 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000291}
292
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000293#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000294#define STRING_CONV_FUNC conv_string_to_utf8
295#else
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000296/* Python 2.0 and later versions */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000297#define STRING_CONV_FUNC (self->returns_unicode \
298 ? conv_string_to_unicode : conv_string_to_utf8)
299#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000300
Fred Drakeb91a36b2002-06-27 19:40:48 +0000301static PyObject*
302string_intern(xmlparseobject *self, const char* str)
303{
304 PyObject *result = STRING_CONV_FUNC(str);
305 PyObject *value;
306 if (!self->intern)
307 return result;
308 value = PyDict_GetItem(self->intern, result);
309 if (!value) {
310 if (PyDict_SetItem(self->intern, result, result) == 0)
311 return result;
312 else
313 return NULL;
314 }
315 Py_INCREF(value);
316 Py_DECREF(result);
317 return value;
318}
319
Fred Drake2a3d7db2002-06-28 22:56:48 +0000320/* Return 0 on success, -1 on exception.
321 * flag_error() will be called before return if needed.
322 */
323static int
324call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
325{
326 PyObject *args;
327 PyObject *temp;
328
329 args = PyTuple_New(1);
330 if (args == NULL)
331 return -1;
332#ifdef Py_USING_UNICODE
333 temp = (self->returns_unicode
334 ? conv_string_len_to_unicode(buffer, len)
335 : conv_string_len_to_utf8(buffer, len));
336#else
337 temp = conv_string_len_to_utf8(buffer, len);
338#endif
339 if (temp == NULL) {
340 Py_DECREF(args);
341 flag_error(self);
342 return -1;
343 }
344 PyTuple_SET_ITEM(args, 0, temp);
345 /* temp is now a borrowed reference; consider it unused. */
346 self->in_callback = 1;
347 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
348 self->handlers[CharacterData], args);
349 /* temp is an owned reference again, or NULL */
350 self->in_callback = 0;
351 Py_DECREF(args);
352 if (temp == NULL) {
353 flag_error(self);
354 return -1;
355 }
356 Py_DECREF(temp);
357 return 0;
358}
359
360static int
361flush_character_buffer(xmlparseobject *self)
362{
363 int rc;
364 if (self->buffer == NULL || self->buffer_used == 0)
365 return 0;
366 rc = call_character_handler(self, self->buffer, self->buffer_used);
367 self->buffer_used = 0;
368 return rc;
369}
370
371static void
372my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
373{
374 xmlparseobject *self = (xmlparseobject *) userData;
375 if (self->buffer == NULL)
376 call_character_handler(self, data, len);
377 else {
378 if ((self->buffer_used + len) > self->buffer_size) {
379 if (flush_character_buffer(self) < 0)
380 return;
381 /* handler might have changed; drop the rest on the floor
382 * if there isn't a handler anymore
383 */
384 if (!have_handler(self, CharacterData))
385 return;
386 }
387 if (len > self->buffer_size) {
388 call_character_handler(self, data, len);
389 self->buffer_used = 0;
390 }
391 else {
392 memcpy(self->buffer + self->buffer_used,
393 data, len * sizeof(XML_Char));
394 self->buffer_used += len;
395 }
396 }
397}
398
Fred Drake85d835f2001-02-08 15:39:08 +0000399static void
400my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000401 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000402{
403 xmlparseobject *self = (xmlparseobject *)userData;
404
Fred Drake71b63ff2002-06-28 22:29:01 +0000405 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000406 PyObject *container, *rv, *args;
407 int i, max;
408
Fred Drake2a3d7db2002-06-28 22:56:48 +0000409 if (flush_character_buffer(self) < 0)
410 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000411 /* Set max to the number of slots filled in atts[]; max/2 is
412 * the number of attributes we need to process.
413 */
414 if (self->specified_attributes) {
415 max = XML_GetSpecifiedAttributeCount(self->itself);
416 }
417 else {
418 max = 0;
419 while (atts[max] != NULL)
420 max += 2;
421 }
422 /* Build the container. */
423 if (self->ordered_attributes)
424 container = PyList_New(max);
425 else
426 container = PyDict_New();
427 if (container == NULL) {
428 flag_error(self);
429 return;
430 }
431 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000432 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000433 PyObject *v;
434 if (n == NULL) {
435 flag_error(self);
436 Py_DECREF(container);
437 return;
438 }
439 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
440 if (v == NULL) {
441 flag_error(self);
442 Py_DECREF(container);
443 Py_DECREF(n);
444 return;
445 }
446 if (self->ordered_attributes) {
447 PyList_SET_ITEM(container, i, n);
448 PyList_SET_ITEM(container, i+1, v);
449 }
450 else if (PyDict_SetItem(container, n, v)) {
451 flag_error(self);
452 Py_DECREF(n);
453 Py_DECREF(v);
454 return;
455 }
456 else {
457 Py_DECREF(n);
458 Py_DECREF(v);
459 }
460 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000461 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000462 if (args == NULL) {
463 Py_DECREF(container);
464 return;
465 }
466 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000467 self->in_callback = 1;
468 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000469 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000470 self->in_callback = 0;
471 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000472 if (rv == NULL) {
473 flag_error(self);
474 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000475 }
Fred Drake85d835f2001-02-08 15:39:08 +0000476 Py_DECREF(rv);
477 }
478}
479
480#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
481 RETURN, GETUSERDATA) \
482static RC \
483my_##NAME##Handler PARAMS {\
484 xmlparseobject *self = GETUSERDATA ; \
485 PyObject *args = NULL; \
486 PyObject *rv = NULL; \
487 INIT \
488\
Fred Drake71b63ff2002-06-28 22:29:01 +0000489 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000490 if (flush_character_buffer(self) < 0) \
491 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000492 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000493 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000494 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000495 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
496 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000497 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000498 Py_DECREF(args); \
499 if (rv == NULL) { \
500 flag_error(self); \
501 return RETURN; \
502 } \
503 CONVERSION \
504 Py_DECREF(rv); \
505 } \
506 return RETURN; \
507}
508
Fred Drake6f987622000-08-25 18:03:30 +0000509#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
510 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
511 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000512
Fred Drake6f987622000-08-25 18:03:30 +0000513#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
514 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
515 rc = PyInt_AsLong(rv);, rc, \
516 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000517
Fred Drake71b63ff2002-06-28 22:29:01 +0000518VOID_HANDLER(EndElement,
519 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000520 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000521
Fred Drake6f987622000-08-25 18:03:30 +0000522VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000523 (void *userData,
524 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000525 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000526 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000527
Fred Drake6f987622000-08-25 18:03:30 +0000528VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000529 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000530 const XML_Char *entityName,
531 const XML_Char *base,
532 const XML_Char *systemId,
533 const XML_Char *publicId,
534 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000535 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000536 string_intern(self, entityName), string_intern(self, base),
537 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000538 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000539
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000540#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000541VOID_HANDLER(EntityDecl,
542 (void *userData,
543 const XML_Char *entityName,
544 int is_parameter_entity,
545 const XML_Char *value,
546 int value_length,
547 const XML_Char *base,
548 const XML_Char *systemId,
549 const XML_Char *publicId,
550 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000551 ("NiNNNNN",
552 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000553 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000554 string_intern(self, base), string_intern(self, systemId),
555 string_intern(self, publicId),
556 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000557#else
558VOID_HANDLER(EntityDecl,
559 (void *userData,
560 const XML_Char *entityName,
561 int is_parameter_entity,
562 const XML_Char *value,
563 int value_length,
564 const XML_Char *base,
565 const XML_Char *systemId,
566 const XML_Char *publicId,
567 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000568 ("NiNNNNN",
569 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000570 (self->returns_unicode
571 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000572 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000573 string_intern(self, base), string_intern(self, systemId),
574 string_intern(self, publicId),
575 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000576#endif
577
578VOID_HANDLER(XmlDecl,
579 (void *userData,
580 const XML_Char *version,
581 const XML_Char *encoding,
582 int standalone),
583 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000584 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000585 standalone))
586
587static PyObject *
588conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000589 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000590{
591 PyObject *result = NULL;
592 PyObject *children = PyTuple_New(model->numchildren);
593 int i;
594
595 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000596 assert(model->numchildren < INT_MAX);
597 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000598 PyObject *child = conv_content_model(&model->children[i],
599 conv_string);
600 if (child == NULL) {
601 Py_XDECREF(children);
602 return NULL;
603 }
604 PyTuple_SET_ITEM(children, i, child);
605 }
606 result = Py_BuildValue("(iiO&N)",
607 model->type, model->quant,
608 conv_string,model->name, children);
609 }
610 return result;
611}
612
613static PyObject *
614conv_content_model_utf8(XML_Content * const model)
615{
616 return conv_content_model(model, conv_string_to_utf8);
617}
618
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000619#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000620static PyObject *
621conv_content_model_unicode(XML_Content * const model)
622{
623 return conv_content_model(model, conv_string_to_unicode);
624}
625
626VOID_HANDLER(ElementDecl,
627 (void *userData,
628 const XML_Char *name,
629 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000630 ("NO&",
631 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000632 (self->returns_unicode ? conv_content_model_unicode
633 : conv_content_model_utf8),model))
634#else
635VOID_HANDLER(ElementDecl,
636 (void *userData,
637 const XML_Char *name,
638 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000639 ("NO&",
640 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000641#endif
642
643VOID_HANDLER(AttlistDecl,
644 (void *userData,
645 const XML_Char *elname,
646 const XML_Char *attname,
647 const XML_Char *att_type,
648 const XML_Char *dflt,
649 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000650 ("(NNO&O&i)",
651 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000652 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
653 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000654
Fred Drake71b63ff2002-06-28 22:29:01 +0000655VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000656 (void *userData,
657 const XML_Char *notationName,
658 const XML_Char *base,
659 const XML_Char *systemId,
660 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000661 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000662 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000663 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000664
Fred Drake6f987622000-08-25 18:03:30 +0000665VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000666 (void *userData,
667 const XML_Char *prefix,
668 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000669 ("(NN)",
670 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000671
Fred Drake6f987622000-08-25 18:03:30 +0000672VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000673 (void *userData,
674 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000675 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000676
Fred Drake6f987622000-08-25 18:03:30 +0000677VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000678 (void *userData, const XML_Char *data),
679 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680
Fred Drake6f987622000-08-25 18:03:30 +0000681VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000683 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000684
Fred Drake6f987622000-08-25 18:03:30 +0000685VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000686 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000687 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000688
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000689#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000690VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000691 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000692 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000693
Fred Drake6f987622000-08-25 18:03:30 +0000694VOID_HANDLER(DefaultHandlerExpand,
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. Kuchlingbeba0562000-06-27 00:33:30 +0000697#else
Fred Drake6f987622000-08-25 18:03:30 +0000698VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000699 (void *userData, const XML_Char *s, int len),
700 ("(N)", (self->returns_unicode
701 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000702 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000703
Fred Drake6f987622000-08-25 18:03:30 +0000704VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000705 (void *userData, const XML_Char *s, int len),
706 ("(N)", (self->returns_unicode
707 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000708 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000709#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000710
Fred Drake71b63ff2002-06-28 22:29:01 +0000711INT_HANDLER(NotStandalone,
712 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000713 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000714
Fred Drake6f987622000-08-25 18:03:30 +0000715RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000716 (XML_Parser parser,
717 const XML_Char *context,
718 const XML_Char *base,
719 const XML_Char *systemId,
720 const XML_Char *publicId),
721 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000722 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000723 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000724 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000725 rc = PyInt_AsLong(rv);, rc,
726 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000727
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000728/* XXX UnknownEncodingHandler */
729
Fred Drake85d835f2001-02-08 15:39:08 +0000730VOID_HANDLER(StartDoctypeDecl,
731 (void *userData, const XML_Char *doctypeName,
732 const XML_Char *sysid, const XML_Char *pubid,
733 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000734 ("(NNNi)", string_intern(self, doctypeName),
735 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000736 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000737
738VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000739
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000740/* ---------------------------------------------------------------- */
741
Fred Drake71b63ff2002-06-28 22:29:01 +0000742static PyObject *
743get_parse_result(xmlparseobject *self, int rv)
744{
745 if (PyErr_Occurred()) {
746 return NULL;
747 }
748 if (rv == 0) {
749 return set_error(self);
750 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000751 if (flush_character_buffer(self) < 0) {
752 return NULL;
753 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000754 return PyInt_FromLong(rv);
755}
756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000758"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000760
761static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000762xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000763{
Fred Drake0582df92000-07-12 04:49:00 +0000764 char *s;
765 int slen;
766 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000767
Fred Drake0582df92000-07-12 04:49:00 +0000768 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
769 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000770
771 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000772}
773
Fred Drakeca1f4262000-09-21 20:10:23 +0000774/* File reading copied from cPickle */
775
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776#define BUF_SIZE 2048
777
Fred Drake0582df92000-07-12 04:49:00 +0000778static int
779readinst(char *buf, int buf_size, PyObject *meth)
780{
781 PyObject *arg = NULL;
782 PyObject *bytes = NULL;
783 PyObject *str = NULL;
784 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000785
Fred Drake676940b2000-09-22 15:21:31 +0000786 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000787 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000788
Fred Drakeca1f4262000-09-21 20:10:23 +0000789 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000790 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000791
Tim Peters954eef72000-09-22 06:01:11 +0000792 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000793
Fred Drakeca1f4262000-09-21 20:10:23 +0000794 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000795 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000796
Fred Drake0582df92000-07-12 04:49:00 +0000797 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000798 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000799 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000800 "read() did not return a string object (type=%.400s)",
801 str->ob_type->tp_name);
802 goto finally;
803 }
804 len = PyString_GET_SIZE(str);
805 if (len > buf_size) {
806 PyErr_Format(PyExc_ValueError,
807 "read() returned too much data: "
808 "%i bytes requested, %i returned",
809 buf_size, len);
810 Py_DECREF(str);
811 goto finally;
812 }
813 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000814finally:
Fred Drake0582df92000-07-12 04:49:00 +0000815 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000816 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000817 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000818}
819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000821"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823
824static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000825xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000826{
Fred Drake0582df92000-07-12 04:49:00 +0000827 int rv = 1;
828 PyObject *f;
829 FILE *fp;
830 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000831
Fred Drake0582df92000-07-12 04:49:00 +0000832 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
833 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834
Fred Drake0582df92000-07-12 04:49:00 +0000835 if (PyFile_Check(f)) {
836 fp = PyFile_AsFile(f);
837 }
838 else{
839 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000840 readmethod = PyObject_GetAttrString(f, "read");
841 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000842 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000843 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000844 "argument must have 'read' attribute");
845 return 0;
846 }
847 }
848 for (;;) {
849 int bytes_read;
850 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
851 if (buf == NULL)
852 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000853
Fred Drake0582df92000-07-12 04:49:00 +0000854 if (fp) {
855 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
856 if (bytes_read < 0) {
857 PyErr_SetFromErrno(PyExc_IOError);
858 return NULL;
859 }
860 }
861 else {
862 bytes_read = readinst(buf, BUF_SIZE, readmethod);
863 if (bytes_read < 0)
864 return NULL;
865 }
866 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
867 if (PyErr_Occurred())
868 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000869
Fred Drake0582df92000-07-12 04:49:00 +0000870 if (!rv || bytes_read == 0)
871 break;
872 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000873 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000874}
875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000877"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000878Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000879
880static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000881xmlparse_SetBase(xmlparseobject *self, PyObject *args)
882{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000883 char *base;
884
Fred Drake0582df92000-07-12 04:49:00 +0000885 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000887 if (!XML_SetBase(self->itself, base)) {
888 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000889 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890 Py_INCREF(Py_None);
891 return Py_None;
892}
893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000895"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000897
898static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000899xmlparse_GetBase(xmlparseobject *self, PyObject *args)
900{
901 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903
Fred Drake0582df92000-07-12 04:49:00 +0000904 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000905}
906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000907PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000908"GetInputContext() -> string\n\
909Return the untranslated text of the input that caused the current event.\n\
910If 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 +0000911for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000912
913static PyObject *
914xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
915{
916 PyObject *result = NULL;
917
918 if (PyArg_ParseTuple(args, ":GetInputContext")) {
919 if (self->in_callback) {
920 int offset, size;
921 const char *buffer
922 = XML_GetInputContext(self->itself, &offset, &size);
923
924 if (buffer != NULL)
925 result = PyString_FromStringAndSize(buffer + offset, size);
926 else {
927 result = Py_None;
928 Py_INCREF(result);
929 }
930 }
931 else {
932 result = Py_None;
933 Py_INCREF(result);
934 }
935 }
936 return result;
937}
Fred Drakebd6101c2001-02-14 18:29:45 +0000938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000940"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000941Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000942information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000943
944static PyObject *
945xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
946{
947 char *context;
948 char *encoding = NULL;
949 xmlparseobject *new_parser;
950 int i;
951
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000952 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000953 &context, &encoding)) {
954 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000955 }
956
Martin v. Löwis894258c2001-09-23 10:20:10 +0000957#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000958 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000959 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000960#else
961 /* Python versions 2.2 and later */
962 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
963#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000964
965 if (new_parser == NULL)
966 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000967 new_parser->buffer_size = self->buffer_size;
968 new_parser->buffer_used = 0;
969 if (self->buffer != NULL) {
970 new_parser->buffer = malloc(new_parser->buffer_size);
971 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +0000972#ifndef Py_TPFLAGS_HAVE_GC
973 /* Code for versions 2.0 and 2.1 */
974 PyObject_Del(new_parser);
975#else
976 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +0000977 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +0000978#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +0000979 return PyErr_NoMemory();
980 }
981 }
982 else
983 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000984 new_parser->returns_unicode = self->returns_unicode;
985 new_parser->ordered_attributes = self->ordered_attributes;
986 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000987 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000988 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000989 encoding);
990 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000991 new_parser->intern = self->intern;
992 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000993#ifdef Py_TPFLAGS_HAVE_GC
994 PyObject_GC_Track(new_parser);
995#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000996 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000997#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000998
999 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001000 Py_DECREF(new_parser);
1001 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001002 }
1003
1004 XML_SetUserData(new_parser->itself, (void *)new_parser);
1005
1006 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001007 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001008 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001009
Fred Drake2a3d7db2002-06-28 22:56:48 +00001010 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001011 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001012 Py_DECREF(new_parser);
1013 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001014 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001015 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016
1017 /* then copy handlers from self */
1018 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001019 PyObject *handler = self->handlers[i];
1020 if (handler != NULL) {
1021 Py_INCREF(handler);
1022 new_parser->handlers[i] = handler;
1023 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001024 handler_info[i].handler);
1025 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001027 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001028}
1029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001030PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001031"SetParamEntityParsing(flag) -> success\n\
1032Controls parsing of parameter entities (including the external DTD\n\
1033subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1034XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1035XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001037
1038static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001039xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001040{
Fred Drake85d835f2001-02-08 15:39:08 +00001041 int flag;
1042 if (!PyArg_ParseTuple(args, "i", &flag))
1043 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001044 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001045 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001046}
1047
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001048static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001049 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001051 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001052 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001053 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001054 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001055 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001056 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001057 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1058 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001059 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1060 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001061 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1062 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001063 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001064};
1065
1066/* ---------- */
1067
1068
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001069#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001070
Fred Drake71b63ff2002-06-28 22:29:01 +00001071/* pyexpat international encoding support.
1072 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073*/
1074
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001075static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001076PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077
Fred Drake71b63ff2002-06-28 22:29:01 +00001078static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001079init_template_buffer(void)
1080{
1081 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001082 for (i = 0; i < 256; i++) {
1083 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001084 }
Fred Drakebb66a202001-03-01 20:48:17 +00001085 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001086}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001087
Fred Drake71b63ff2002-06-28 22:29:01 +00001088static int
1089PyUnknownEncodingHandler(void *encodingHandlerData,
1090 const XML_Char *name,
1091 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092{
Fred Drakebb66a202001-03-01 20:48:17 +00001093 PyUnicodeObject *_u_string = NULL;
1094 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001095 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001096
Fred Drakebb66a202001-03-01 20:48:17 +00001097 /* Yes, supports only 8bit encodings */
1098 _u_string = (PyUnicodeObject *)
1099 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001100
Fred Drakebb66a202001-03-01 20:48:17 +00001101 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001103
Fred Drakebb66a202001-03-01 20:48:17 +00001104 for (i = 0; i < 256; i++) {
1105 /* Stupid to access directly, but fast */
1106 Py_UNICODE c = _u_string->str[i];
1107 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001109 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001111 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001112 info->data = NULL;
1113 info->convert = NULL;
1114 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001115 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001116 Py_DECREF(_u_string);
1117 return result;
1118}
1119
1120#endif
1121
1122static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001123newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001124{
1125 int i;
1126 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001127
Martin v. Löwis894258c2001-09-23 10:20:10 +00001128#ifdef Py_TPFLAGS_HAVE_GC
1129 /* Code for versions 2.2 and later */
1130 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1131#else
Fred Drake0582df92000-07-12 04:49:00 +00001132 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001133#endif
Fred Drake0582df92000-07-12 04:49:00 +00001134 if (self == NULL)
1135 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001136
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001137#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001138 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001139#else
1140 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001141#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001142
Fred Drake2a3d7db2002-06-28 22:56:48 +00001143 self->buffer = NULL;
1144 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1145 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001146 self->ordered_attributes = 0;
1147 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001148 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001149 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001150 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001151 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1152 }
Fred Drake85d835f2001-02-08 15:39:08 +00001153 else {
Fred Drake0582df92000-07-12 04:49:00 +00001154 self->itself = XML_ParserCreate(encoding);
1155 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001156 self->intern = intern;
1157 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001158#ifdef Py_TPFLAGS_HAVE_GC
1159 PyObject_GC_Track(self);
1160#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001161 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001162#endif
Fred Drake0582df92000-07-12 04:49:00 +00001163 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001164 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001165 "XML_ParserCreate failed");
1166 Py_DECREF(self);
1167 return NULL;
1168 }
1169 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001170#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001171 XML_SetUnknownEncodingHandler(self->itself,
1172 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001173#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001174
Fred Drake2a3d7db2002-06-28 22:56:48 +00001175 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001176 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001177
Fred Drake7c75bf22002-07-01 14:02:31 +00001178 self->handlers = malloc(sizeof(PyObject *) * i);
1179 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001180 Py_DECREF(self);
1181 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001182 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001183 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001184
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001185 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001186}
1187
1188
1189static void
Fred Drake0582df92000-07-12 04:49:00 +00001190xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001191{
Fred Drake0582df92000-07-12 04:49:00 +00001192 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001193#ifdef Py_TPFLAGS_HAVE_GC
1194 PyObject_GC_UnTrack(self);
1195#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001196 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001197#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001198 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001199 XML_ParserFree(self->itself);
1200 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001201
Fred Drake85d835f2001-02-08 15:39:08 +00001202 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001203 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001204 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001205 temp = self->handlers[i];
1206 self->handlers[i] = NULL;
1207 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001208 }
1209 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001210 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001211 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001212 if (self->buffer != NULL) {
1213 free(self->buffer);
1214 self->buffer = NULL;
1215 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001216 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001217#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001218 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001219 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001220#else
1221 /* Code for versions 2.2 and later. */
1222 PyObject_GC_Del(self);
1223#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001224}
1225
Fred Drake0582df92000-07-12 04:49:00 +00001226static int
1227handlername2int(const char *name)
1228{
1229 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001230 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001231 if (strcmp(name, handler_info[i].name) == 0) {
1232 return i;
1233 }
1234 }
1235 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001236}
1237
1238static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001239get_pybool(int istrue)
1240{
1241 PyObject *result = istrue ? Py_True : Py_False;
1242 Py_INCREF(result);
1243 return result;
1244}
1245
1246static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001247xmlparse_getattr(xmlparseobject *self, char *name)
1248{
Fred Drake71b63ff2002-06-28 22:29:01 +00001249 int handlernum = handlername2int(name);
1250
1251 if (handlernum != -1) {
1252 PyObject *result = self->handlers[handlernum];
1253 if (result == NULL)
1254 result = Py_None;
1255 Py_INCREF(result);
1256 return result;
1257 }
1258 if (name[0] == 'E') {
1259 if (strcmp(name, "ErrorCode") == 0)
1260 return PyInt_FromLong((long)
1261 XML_GetErrorCode(self->itself));
1262 if (strcmp(name, "ErrorLineNumber") == 0)
1263 return PyInt_FromLong((long)
1264 XML_GetErrorLineNumber(self->itself));
1265 if (strcmp(name, "ErrorColumnNumber") == 0)
1266 return PyInt_FromLong((long)
1267 XML_GetErrorColumnNumber(self->itself));
1268 if (strcmp(name, "ErrorByteIndex") == 0)
1269 return PyInt_FromLong((long)
1270 XML_GetErrorByteIndex(self->itself));
1271 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001272 if (name[0] == 'b') {
1273 if (strcmp(name, "buffer_size") == 0)
1274 return PyInt_FromLong((long) self->buffer_size);
1275 if (strcmp(name, "buffer_text") == 0)
1276 return get_pybool(self->buffer != NULL);
1277 if (strcmp(name, "buffer_used") == 0)
1278 return PyInt_FromLong((long) self->buffer_used);
1279 }
Fred Drake85d835f2001-02-08 15:39:08 +00001280 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001281 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001282 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001283 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001284 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001285 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001286 if (strcmp(name, "intern") == 0) {
1287 if (self->intern == NULL) {
1288 Py_INCREF(Py_None);
1289 return Py_None;
1290 }
1291 else {
1292 Py_INCREF(self->intern);
1293 return self->intern;
1294 }
1295 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001296
Fred Drake0582df92000-07-12 04:49:00 +00001297 if (strcmp(name, "__members__") == 0) {
1298 int i;
1299 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001300 for (i = 0; handler_info[i].name != NULL; i++) {
1301 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001302 }
1303 PyList_Append(rc, PyString_FromString("ErrorCode"));
1304 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1305 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1306 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake2a3d7db2002-06-28 22:56:48 +00001307 PyList_Append(rc, PyString_FromString("buffer_size"));
1308 PyList_Append(rc, PyString_FromString("buffer_text"));
1309 PyList_Append(rc, PyString_FromString("buffer_used"));
Fred Drake85d835f2001-02-08 15:39:08 +00001310 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001311 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001312 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001313 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001314
Fred Drake0582df92000-07-12 04:49:00 +00001315 return rc;
1316 }
1317 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001318}
1319
Fred Drake6f987622000-08-25 18:03:30 +00001320static int
1321sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001322{
1323 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001324 if (handlernum >= 0) {
1325 xmlhandler c_handler = NULL;
1326 PyObject *temp = self->handlers[handlernum];
1327
1328 if (v == Py_None)
1329 v = NULL;
1330 else if (v != NULL) {
1331 Py_INCREF(v);
1332 c_handler = handler_info[handlernum].handler;
1333 }
Fred Drake0582df92000-07-12 04:49:00 +00001334 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001335 Py_XDECREF(temp);
1336 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001337 return 1;
1338 }
1339 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001340}
1341
1342static int
Fred Drake6f987622000-08-25 18:03:30 +00001343xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001344{
Fred Drake6f987622000-08-25 18:03:30 +00001345 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001346 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001347 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1348 return -1;
1349 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001350 if (strcmp(name, "buffer_text") == 0) {
1351 if (PyObject_IsTrue(v)) {
1352 if (self->buffer == NULL) {
1353 self->buffer = malloc(self->buffer_size);
1354 if (self->buffer == NULL) {
1355 PyErr_NoMemory();
1356 return -1;
1357 }
1358 self->buffer_used = 0;
1359 }
1360 }
1361 else if (self->buffer != NULL) {
1362 if (flush_character_buffer(self) < 0)
1363 return -1;
1364 free(self->buffer);
1365 self->buffer = NULL;
1366 }
1367 return 0;
1368 }
Fred Drake85d835f2001-02-08 15:39:08 +00001369 if (strcmp(name, "ordered_attributes") == 0) {
1370 if (PyObject_IsTrue(v))
1371 self->ordered_attributes = 1;
1372 else
1373 self->ordered_attributes = 0;
1374 return 0;
1375 }
Fred Drake6f987622000-08-25 18:03:30 +00001376 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001377 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001378#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001379 PyErr_SetString(PyExc_ValueError,
1380 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001381 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001382#else
Fred Drake6f987622000-08-25 18:03:30 +00001383 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001384#endif
Fred Drake6f987622000-08-25 18:03:30 +00001385 }
1386 else
1387 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001388 return 0;
1389 }
1390 if (strcmp(name, "specified_attributes") == 0) {
1391 if (PyObject_IsTrue(v))
1392 self->specified_attributes = 1;
1393 else
1394 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001395 return 0;
1396 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001397 if (strcmp(name, "CharacterDataHandler") == 0) {
1398 /* If we're changing the character data handler, flush all
1399 * cached data with the old handler. Not sure there's a
1400 * "right" thing to do, though, but this probably won't
1401 * happen.
1402 */
1403 if (flush_character_buffer(self) < 0)
1404 return -1;
1405 }
Fred Drake6f987622000-08-25 18:03:30 +00001406 if (sethandler(self, name, v)) {
1407 return 0;
1408 }
1409 PyErr_SetString(PyExc_AttributeError, name);
1410 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001411}
1412
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001413#ifdef WITH_CYCLE_GC
1414static int
1415xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1416{
Fred Drakecde79132001-04-25 16:01:30 +00001417 int i, err;
1418 for (i = 0; handler_info[i].name != NULL; i++) {
1419 if (!op->handlers[i])
1420 continue;
1421 err = visit(op->handlers[i], arg);
1422 if (err)
1423 return err;
1424 }
1425 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001426}
1427
1428static int
1429xmlparse_clear(xmlparseobject *op)
1430{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001431 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001432 Py_XDECREF(op->intern);
1433 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001434 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001435}
1436#endif
1437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001439
1440static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001441 PyObject_HEAD_INIT(NULL)
1442 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001443 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001444 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001445 0, /*tp_itemsize*/
1446 /* methods */
1447 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1448 (printfunc)0, /*tp_print*/
1449 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1450 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1451 (cmpfunc)0, /*tp_compare*/
1452 (reprfunc)0, /*tp_repr*/
1453 0, /*tp_as_number*/
1454 0, /*tp_as_sequence*/
1455 0, /*tp_as_mapping*/
1456 (hashfunc)0, /*tp_hash*/
1457 (ternaryfunc)0, /*tp_call*/
1458 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001459 0, /* tp_getattro */
1460 0, /* tp_setattro */
1461 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001462#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001464#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001465 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001466#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001467 Xmlparsetype__doc__, /* Documentation string */
1468#ifdef WITH_CYCLE_GC
1469 (traverseproc)xmlparse_traverse, /* tp_traverse */
1470 (inquiry)xmlparse_clear /* tp_clear */
1471#else
1472 0, 0
1473#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001474};
1475
1476/* End of code for xmlparser objects */
1477/* -------------------------------------------------------- */
1478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001480"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001481Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001482
1483static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001484pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1485{
Fred Drakecde79132001-04-25 16:01:30 +00001486 char *encoding = NULL;
1487 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001488 PyObject *intern = NULL;
1489 PyObject *result;
1490 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001491 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001492 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001493
Fred Drakeb91a36b2002-06-27 19:40:48 +00001494 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1495 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001496 return NULL;
1497 if (namespace_separator != NULL
1498 && strlen(namespace_separator) > 1) {
1499 PyErr_SetString(PyExc_ValueError,
1500 "namespace_separator must be at most one"
1501 " character, omitted, or None");
1502 return NULL;
1503 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001504 /* Explicitly passing None means no interning is desired.
1505 Not passing anything means that a new dictionary is used. */
1506 if (intern == Py_None)
1507 intern = NULL;
1508 else if (intern == NULL) {
1509 intern = PyDict_New();
1510 if (!intern)
1511 return NULL;
1512 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001513 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001514 else if (!PyDict_Check(intern)) {
1515 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1516 return NULL;
1517 }
1518
1519 result = newxmlparseobject(encoding, namespace_separator, intern);
1520 if (intern_decref) {
1521 Py_DECREF(intern);
1522 }
1523 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001524}
1525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001527"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001529
1530static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001531pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001532{
Fred Drake0582df92000-07-12 04:49:00 +00001533 long code = 0;
1534
1535 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1536 return NULL;
1537 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001538}
1539
1540/* List of methods defined in the module */
1541
1542static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001543 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1544 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1545 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1546 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001547
Fred Drake0582df92000-07-12 04:49:00 +00001548 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001549};
1550
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001551/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STRVAR(pyexpat_module_documentation,
1554"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001555
Fred Drake4113b132001-03-24 19:58:26 +00001556/* Return a Python string that represents the version number without the
1557 * extra cruft added by revision control, even if the right options were
1558 * given to the "cvs export" command to make it not include the extra
1559 * cruft.
1560 */
1561static PyObject *
1562get_version_string(void)
1563{
1564 static char *rcsid = "$Revision$";
1565 char *rev = rcsid;
1566 int i = 0;
1567
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001568 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001569 ++rev;
1570 while (rev[i] != ' ' && rev[i] != '\0')
1571 ++i;
1572
1573 return PyString_FromStringAndSize(rev, i);
1574}
1575
Fred Drakecde79132001-04-25 16:01:30 +00001576/* Initialization function for the module */
1577
1578#ifndef MODULE_NAME
1579#define MODULE_NAME "pyexpat"
1580#endif
1581
1582#ifndef MODULE_INITFUNC
1583#define MODULE_INITFUNC initpyexpat
1584#endif
1585
1586void MODULE_INITFUNC(void); /* avoid compiler warnings */
1587
Fred Drake6f987622000-08-25 18:03:30 +00001588DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001589MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001590{
1591 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001592 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001593 PyObject *errors_module;
1594 PyObject *modelmod_name;
1595 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001596 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001597
Fred Drake6f987622000-08-25 18:03:30 +00001598 if (errmod_name == NULL)
1599 return;
Fred Drakecde79132001-04-25 16:01:30 +00001600 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001601 if (modelmod_name == NULL)
1602 return;
Fred Drake6f987622000-08-25 18:03:30 +00001603
Fred Drake0582df92000-07-12 04:49:00 +00001604 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001605
Fred Drake0582df92000-07-12 04:49:00 +00001606 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001607 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001608 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001609
Fred Drake0582df92000-07-12 04:49:00 +00001610 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001611 if (ErrorObject == NULL) {
1612 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001613 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001614 if (ErrorObject == NULL)
1615 return;
1616 }
1617 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001618 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001619 Py_INCREF(ErrorObject);
1620 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001621 Py_INCREF(&Xmlparsetype);
1622 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001623
Fred Drake4113b132001-03-24 19:58:26 +00001624 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001625 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1626 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001627 {
1628 XML_Expat_Version info = XML_ExpatVersionInfo();
1629 PyModule_AddObject(m, "version_info",
1630 Py_BuildValue("(iii)", info.major,
1631 info.minor, info.micro));
1632 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001633#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001634 init_template_buffer();
1635#endif
Fred Drake0582df92000-07-12 04:49:00 +00001636 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001637 compiled, this should check and set native_encoding
1638 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001639 */
Fred Drake93adb692000-09-23 04:55:48 +00001640 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001641
Fred Drake85d835f2001-02-08 15:39:08 +00001642 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001643 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001644 errors_module = PyDict_GetItem(d, errmod_name);
1645 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001646 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001647 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001648 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001649 /* gives away the reference to errors_module */
1650 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001651 }
1652 }
Fred Drake6f987622000-08-25 18:03:30 +00001653 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001654 model_module = PyDict_GetItem(d, modelmod_name);
1655 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001656 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001657 if (model_module != NULL) {
1658 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1659 /* gives away the reference to model_module */
1660 PyModule_AddObject(m, "model", model_module);
1661 }
1662 }
1663 Py_DECREF(modelmod_name);
1664 if (errors_module == NULL || model_module == NULL)
1665 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001666 return;
1667
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001668#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001669 PyModule_AddStringConstant(errors_module, #name, \
1670 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001671
Fred Drake0582df92000-07-12 04:49:00 +00001672 MYCONST(XML_ERROR_NO_MEMORY);
1673 MYCONST(XML_ERROR_SYNTAX);
1674 MYCONST(XML_ERROR_NO_ELEMENTS);
1675 MYCONST(XML_ERROR_INVALID_TOKEN);
1676 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1677 MYCONST(XML_ERROR_PARTIAL_CHAR);
1678 MYCONST(XML_ERROR_TAG_MISMATCH);
1679 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1680 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1681 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1682 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1683 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1684 MYCONST(XML_ERROR_ASYNC_ENTITY);
1685 MYCONST(XML_ERROR_BAD_CHAR_REF);
1686 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1687 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1688 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1689 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1690 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001691 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1692 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1693 MYCONST(XML_ERROR_NOT_STANDALONE);
1694
Fred Drake85d835f2001-02-08 15:39:08 +00001695 PyModule_AddStringConstant(errors_module, "__doc__",
1696 "Constants used to describe error conditions.");
1697
Fred Drake93adb692000-09-23 04:55:48 +00001698#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001699
Fred Drake85d835f2001-02-08 15:39:08 +00001700#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001701 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1702 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1703 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001704#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001705
Fred Drake85d835f2001-02-08 15:39:08 +00001706#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1707 PyModule_AddStringConstant(model_module, "__doc__",
1708 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001709
Fred Drake85d835f2001-02-08 15:39:08 +00001710 MYCONST(XML_CTYPE_EMPTY);
1711 MYCONST(XML_CTYPE_ANY);
1712 MYCONST(XML_CTYPE_MIXED);
1713 MYCONST(XML_CTYPE_NAME);
1714 MYCONST(XML_CTYPE_CHOICE);
1715 MYCONST(XML_CTYPE_SEQ);
1716
1717 MYCONST(XML_CQUANT_NONE);
1718 MYCONST(XML_CQUANT_OPT);
1719 MYCONST(XML_CQUANT_REP);
1720 MYCONST(XML_CQUANT_PLUS);
1721#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001722}
1723
Fred Drake6f987622000-08-25 18:03:30 +00001724static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001725clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001726{
Fred Drakecde79132001-04-25 16:01:30 +00001727 int i = 0;
1728 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001729
Fred Drake71b63ff2002-06-28 22:29:01 +00001730 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001731 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001732 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001733 else {
Fred Drakecde79132001-04-25 16:01:30 +00001734 temp = self->handlers[i];
1735 self->handlers[i] = NULL;
1736 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001737 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001738 }
Fred Drakecde79132001-04-25 16:01:30 +00001739 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001740}
1741
Tim Peters0c322792002-07-17 16:49:03 +00001742static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001743 {"StartElementHandler",
1744 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001745 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001746 {"EndElementHandler",
1747 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001748 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001749 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001750 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1751 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001752 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001753 (xmlhandlersetter)XML_SetCharacterDataHandler,
1754 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001755 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001756 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001757 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001758 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001759 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001760 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001761 {"StartNamespaceDeclHandler",
1762 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001763 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001764 {"EndNamespaceDeclHandler",
1765 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001766 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001767 {"CommentHandler",
1768 (xmlhandlersetter)XML_SetCommentHandler,
1769 (xmlhandler)my_CommentHandler},
1770 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001771 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001772 (xmlhandler)my_StartCdataSectionHandler},
1773 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001774 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001775 (xmlhandler)my_EndCdataSectionHandler},
1776 {"DefaultHandler",
1777 (xmlhandlersetter)XML_SetDefaultHandler,
1778 (xmlhandler)my_DefaultHandler},
1779 {"DefaultHandlerExpand",
1780 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1781 (xmlhandler)my_DefaultHandlerExpandHandler},
1782 {"NotStandaloneHandler",
1783 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1784 (xmlhandler)my_NotStandaloneHandler},
1785 {"ExternalEntityRefHandler",
1786 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001787 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001788 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001789 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001790 (xmlhandler)my_StartDoctypeDeclHandler},
1791 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001792 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001793 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001794 {"EntityDeclHandler",
1795 (xmlhandlersetter)XML_SetEntityDeclHandler,
1796 (xmlhandler)my_EntityDeclHandler},
1797 {"XmlDeclHandler",
1798 (xmlhandlersetter)XML_SetXmlDeclHandler,
1799 (xmlhandler)my_XmlDeclHandler},
1800 {"ElementDeclHandler",
1801 (xmlhandlersetter)XML_SetElementDeclHandler,
1802 (xmlhandler)my_ElementDeclHandler},
1803 {"AttlistDeclHandler",
1804 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1805 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001806
Fred Drake0582df92000-07-12 04:49:00 +00001807 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001808};