blob: 045b13a1d40302b0721aef5ec6967dce80ff7ff0 [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
9#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010#endif
11
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000012#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
13/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000014#define Py_USING_UNICODE
15#endif
16
Fred Drake0582df92000-07-12 04:49:00 +000017enum HandlerTypes {
18 StartElement,
19 EndElement,
20 ProcessingInstruction,
21 CharacterData,
22 UnparsedEntityDecl,
23 NotationDecl,
24 StartNamespaceDecl,
25 EndNamespaceDecl,
26 Comment,
27 StartCdataSection,
28 EndCdataSection,
29 Default,
30 DefaultHandlerExpand,
31 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000032 ExternalEntityRef,
33 StartDoctypeDecl,
34 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000035 EntityDecl,
36 XmlDecl,
37 ElementDecl,
38 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000039 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000040};
41
42static PyObject *ErrorObject;
43
44/* ----------------------------------------------------- */
45
46/* Declarations for objects of type xmlparser */
47
48typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000049 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000050
Fred Drake0582df92000-07-12 04:49:00 +000051 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000052 int returns_unicode; /* True if Unicode strings are returned;
53 if false, UTF-8 strings are returned */
54 int ordered_attributes; /* Return attributes as a list. */
55 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000056 int in_callback; /* Is a callback active? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000057 XML_Char *buffer; /* Buffer used when accumulating characters */
58 /* NULL if not enabled */
59 int buffer_size; /* Size of buffer, in XML_Char units */
60 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000061 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000062 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000063} xmlparseobject;
64
Fred Drake2a3d7db2002-06-28 22:56:48 +000065#define CHARACTER_DATA_BUFFER_SIZE 8192
66
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000067staticforward PyTypeObject Xmlparsetype;
68
Fred Drake6f987622000-08-25 18:03:30 +000069typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070typedef void* xmlhandler;
71
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000072struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000073 const char *name;
74 xmlhandlersetter setter;
75 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000076 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000077 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078};
79
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000080staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000081
Fred Drakebd6101c2001-02-14 18:29:45 +000082/* Set an integer attribute on the error object; return true on success,
83 * false on an exception.
84 */
85static int
86set_error_attr(PyObject *err, char *name, int value)
87{
88 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000089
Fred Drakebd6101c2001-02-14 18:29:45 +000090 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
91 Py_DECREF(v);
92 return 0;
93 }
94 return 1;
95}
96
97/* Build and set an Expat exception, including positioning
98 * information. Always returns NULL.
99 */
Fred Drake85d835f2001-02-08 15:39:08 +0000100static PyObject *
101set_error(xmlparseobject *self)
102{
103 PyObject *err;
104 char buffer[256];
105 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000106 int lineno = XML_GetErrorLineNumber(parser);
107 int column = XML_GetErrorColumnNumber(parser);
108 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000109
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000110 /* There is no risk of overflowing this buffer, since
111 even for 64-bit integers, there is sufficient space. */
112 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000114 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000115 if ( err != NULL
116 && set_error_attr(err, "code", code)
117 && set_error_attr(err, "offset", column)
118 && set_error_attr(err, "lineno", lineno)) {
119 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000120 }
121 return NULL;
122}
123
Fred Drake71b63ff2002-06-28 22:29:01 +0000124static int
125have_handler(xmlparseobject *self, int type)
126{
127 PyObject *handler = self->handlers[type];
128 return handler != NULL;
129}
130
131static PyObject *
132get_handler_name(struct HandlerInfo *hinfo)
133{
134 PyObject *name = hinfo->nameobj;
135 if (name == NULL) {
136 name = PyString_FromString(hinfo->name);
137 hinfo->nameobj = name;
138 }
139 Py_XINCREF(name);
140 return name;
141}
142
Fred Drake85d835f2001-02-08 15:39:08 +0000143
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000144#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000145/* Convert a string of XML_Chars into a Unicode string.
146 Returns None if str is a null pointer. */
147
Fred Drake0582df92000-07-12 04:49:00 +0000148static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000149conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000150{
Fred Drake71b63ff2002-06-28 22:29:01 +0000151 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000152 and hence in UTF-8. */
153 /* UTF-8 from Expat, Unicode desired */
154 if (str == NULL) {
155 Py_INCREF(Py_None);
156 return Py_None;
157 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000158 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000159}
160
Fred Drake0582df92000-07-12 04:49:00 +0000161static PyObject *
162conv_string_len_to_unicode(const XML_Char *str, int len)
163{
Fred Drake71b63ff2002-06-28 22:29:01 +0000164 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000165 and hence in UTF-8. */
166 /* UTF-8 from Expat, Unicode desired */
167 if (str == NULL) {
168 Py_INCREF(Py_None);
169 return Py_None;
170 }
Fred Drake6f987622000-08-25 18:03:30 +0000171 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000172}
173#endif
174
175/* Convert a string of XML_Chars into an 8-bit Python string.
176 Returns None if str is a null pointer. */
177
Fred Drake6f987622000-08-25 18:03:30 +0000178static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000179conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000180{
Fred Drake71b63ff2002-06-28 22:29:01 +0000181 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000182 and hence in UTF-8. */
183 /* UTF-8 from Expat, UTF-8 desired */
184 if (str == NULL) {
185 Py_INCREF(Py_None);
186 return Py_None;
187 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000188 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000189}
190
Fred Drake6f987622000-08-25 18:03:30 +0000191static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000192conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000193{
Fred Drake71b63ff2002-06-28 22:29:01 +0000194 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000195 and hence in UTF-8. */
196 /* UTF-8 from Expat, UTF-8 desired */
197 if (str == NULL) {
198 Py_INCREF(Py_None);
199 return Py_None;
200 }
201 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000202}
203
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000204/* Callback routines */
205
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000206static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000207
Fred Drake6f987622000-08-25 18:03:30 +0000208static void
209flag_error(xmlparseobject *self)
210{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000211 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000212}
213
214static PyCodeObject*
215getcode(enum HandlerTypes slot, char* func_name, int lineno)
216{
Fred Drakebd6101c2001-02-14 18:29:45 +0000217 PyObject *code = NULL;
218 PyObject *name = NULL;
219 PyObject *nulltuple = NULL;
220 PyObject *filename = NULL;
221
222 if (handler_info[slot].tb_code == NULL) {
223 code = PyString_FromString("");
224 if (code == NULL)
225 goto failed;
226 name = PyString_FromString(func_name);
227 if (name == NULL)
228 goto failed;
229 nulltuple = PyTuple_New(0);
230 if (nulltuple == NULL)
231 goto failed;
232 filename = PyString_FromString(__FILE__);
233 handler_info[slot].tb_code =
234 PyCode_New(0, /* argcount */
235 0, /* nlocals */
236 0, /* stacksize */
237 0, /* flags */
238 code, /* code */
239 nulltuple, /* consts */
240 nulltuple, /* names */
241 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000242#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000243 nulltuple, /* freevars */
244 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000245#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000246 filename, /* filename */
247 name, /* name */
248 lineno, /* firstlineno */
249 code /* lnotab */
250 );
251 if (handler_info[slot].tb_code == NULL)
252 goto failed;
253 Py_DECREF(code);
254 Py_DECREF(nulltuple);
255 Py_DECREF(filename);
256 Py_DECREF(name);
257 }
258 return handler_info[slot].tb_code;
259 failed:
260 Py_XDECREF(code);
261 Py_XDECREF(name);
262 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000263}
264
265static PyObject*
266call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
267{
Fred Drakebd6101c2001-02-14 18:29:45 +0000268 PyThreadState *tstate = PyThreadState_GET();
269 PyFrameObject *f;
270 PyObject *res;
271
272 if (c == NULL)
273 return NULL;
274 f = PyFrame_New(
275 tstate, /*back*/
276 c, /*code*/
277 tstate->frame->f_globals, /*globals*/
278 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000279 );
280 if (f == NULL)
281 return NULL;
282 tstate->frame = f;
283 res = PyEval_CallObject(func, args);
284 if (res == NULL && tstate->curexc_traceback == NULL)
285 PyTraceBack_Here(f);
286 tstate->frame = f->f_back;
287 Py_DECREF(f);
288 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000289}
290
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000291#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000292#define STRING_CONV_FUNC conv_string_to_utf8
293#else
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000294/* Python 2.0 and later versions */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000295#define STRING_CONV_FUNC (self->returns_unicode \
296 ? conv_string_to_unicode : conv_string_to_utf8)
297#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000298
Fred Drakeb91a36b2002-06-27 19:40:48 +0000299static PyObject*
300string_intern(xmlparseobject *self, const char* str)
301{
302 PyObject *result = STRING_CONV_FUNC(str);
303 PyObject *value;
304 if (!self->intern)
305 return result;
306 value = PyDict_GetItem(self->intern, result);
307 if (!value) {
308 if (PyDict_SetItem(self->intern, result, result) == 0)
309 return result;
310 else
311 return NULL;
312 }
313 Py_INCREF(value);
314 Py_DECREF(result);
315 return value;
316}
317
Fred Drake2a3d7db2002-06-28 22:56:48 +0000318/* Return 0 on success, -1 on exception.
319 * flag_error() will be called before return if needed.
320 */
321static int
322call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
323{
324 PyObject *args;
325 PyObject *temp;
326
327 args = PyTuple_New(1);
328 if (args == NULL)
329 return -1;
330#ifdef Py_USING_UNICODE
331 temp = (self->returns_unicode
332 ? conv_string_len_to_unicode(buffer, len)
333 : conv_string_len_to_utf8(buffer, len));
334#else
335 temp = conv_string_len_to_utf8(buffer, len);
336#endif
337 if (temp == NULL) {
338 Py_DECREF(args);
339 flag_error(self);
340 return -1;
341 }
342 PyTuple_SET_ITEM(args, 0, temp);
343 /* temp is now a borrowed reference; consider it unused. */
344 self->in_callback = 1;
345 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
346 self->handlers[CharacterData], args);
347 /* temp is an owned reference again, or NULL */
348 self->in_callback = 0;
349 Py_DECREF(args);
350 if (temp == NULL) {
351 flag_error(self);
352 return -1;
353 }
354 Py_DECREF(temp);
355 return 0;
356}
357
358static int
359flush_character_buffer(xmlparseobject *self)
360{
361 int rc;
362 if (self->buffer == NULL || self->buffer_used == 0)
363 return 0;
364 rc = call_character_handler(self, self->buffer, self->buffer_used);
365 self->buffer_used = 0;
366 return rc;
367}
368
369static void
370my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
371{
372 xmlparseobject *self = (xmlparseobject *) userData;
373 if (self->buffer == NULL)
374 call_character_handler(self, data, len);
375 else {
376 if ((self->buffer_used + len) > self->buffer_size) {
377 if (flush_character_buffer(self) < 0)
378 return;
379 /* handler might have changed; drop the rest on the floor
380 * if there isn't a handler anymore
381 */
382 if (!have_handler(self, CharacterData))
383 return;
384 }
385 if (len > self->buffer_size) {
386 call_character_handler(self, data, len);
387 self->buffer_used = 0;
388 }
389 else {
390 memcpy(self->buffer + self->buffer_used,
391 data, len * sizeof(XML_Char));
392 self->buffer_used += len;
393 }
394 }
395}
396
Fred Drake85d835f2001-02-08 15:39:08 +0000397static void
398my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000399 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000400{
401 xmlparseobject *self = (xmlparseobject *)userData;
402
Fred Drake71b63ff2002-06-28 22:29:01 +0000403 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000404 PyObject *container, *rv, *args;
405 int i, max;
406
Fred Drake2a3d7db2002-06-28 22:56:48 +0000407 if (flush_character_buffer(self) < 0)
408 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000409 /* Set max to the number of slots filled in atts[]; max/2 is
410 * the number of attributes we need to process.
411 */
412 if (self->specified_attributes) {
413 max = XML_GetSpecifiedAttributeCount(self->itself);
414 }
415 else {
416 max = 0;
417 while (atts[max] != NULL)
418 max += 2;
419 }
420 /* Build the container. */
421 if (self->ordered_attributes)
422 container = PyList_New(max);
423 else
424 container = PyDict_New();
425 if (container == NULL) {
426 flag_error(self);
427 return;
428 }
429 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000430 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000431 PyObject *v;
432 if (n == NULL) {
433 flag_error(self);
434 Py_DECREF(container);
435 return;
436 }
437 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
438 if (v == NULL) {
439 flag_error(self);
440 Py_DECREF(container);
441 Py_DECREF(n);
442 return;
443 }
444 if (self->ordered_attributes) {
445 PyList_SET_ITEM(container, i, n);
446 PyList_SET_ITEM(container, i+1, v);
447 }
448 else if (PyDict_SetItem(container, n, v)) {
449 flag_error(self);
450 Py_DECREF(n);
451 Py_DECREF(v);
452 return;
453 }
454 else {
455 Py_DECREF(n);
456 Py_DECREF(v);
457 }
458 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000459 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000460 if (args == NULL) {
461 Py_DECREF(container);
462 return;
463 }
464 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000465 self->in_callback = 1;
466 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000467 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000468 self->in_callback = 0;
469 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000470 if (rv == NULL) {
471 flag_error(self);
472 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000473 }
Fred Drake85d835f2001-02-08 15:39:08 +0000474 Py_DECREF(rv);
475 }
476}
477
478#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
479 RETURN, GETUSERDATA) \
480static RC \
481my_##NAME##Handler PARAMS {\
482 xmlparseobject *self = GETUSERDATA ; \
483 PyObject *args = NULL; \
484 PyObject *rv = NULL; \
485 INIT \
486\
Fred Drake71b63ff2002-06-28 22:29:01 +0000487 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000488 if (flush_character_buffer(self) < 0) \
489 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000490 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000491 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000492 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000493 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
494 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000495 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000496 Py_DECREF(args); \
497 if (rv == NULL) { \
498 flag_error(self); \
499 return RETURN; \
500 } \
501 CONVERSION \
502 Py_DECREF(rv); \
503 } \
504 return RETURN; \
505}
506
Fred Drake6f987622000-08-25 18:03:30 +0000507#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
508 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
509 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000510
Fred Drake6f987622000-08-25 18:03:30 +0000511#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
512 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
513 rc = PyInt_AsLong(rv);, rc, \
514 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000515
Fred Drake71b63ff2002-06-28 22:29:01 +0000516VOID_HANDLER(EndElement,
517 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000518 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000519
Fred Drake6f987622000-08-25 18:03:30 +0000520VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000521 (void *userData,
522 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000523 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000524 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000525
Fred Drake6f987622000-08-25 18:03:30 +0000526VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000527 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000528 const XML_Char *entityName,
529 const XML_Char *base,
530 const XML_Char *systemId,
531 const XML_Char *publicId,
532 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000533 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000534 string_intern(self, entityName), string_intern(self, base),
535 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000536 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000537
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000538#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000539VOID_HANDLER(EntityDecl,
540 (void *userData,
541 const XML_Char *entityName,
542 int is_parameter_entity,
543 const XML_Char *value,
544 int value_length,
545 const XML_Char *base,
546 const XML_Char *systemId,
547 const XML_Char *publicId,
548 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000549 ("NiNNNNN",
550 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000551 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000552 string_intern(self, base), string_intern(self, systemId),
553 string_intern(self, publicId),
554 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000555#else
556VOID_HANDLER(EntityDecl,
557 (void *userData,
558 const XML_Char *entityName,
559 int is_parameter_entity,
560 const XML_Char *value,
561 int value_length,
562 const XML_Char *base,
563 const XML_Char *systemId,
564 const XML_Char *publicId,
565 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000566 ("NiNNNNN",
567 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000568 (self->returns_unicode
569 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000570 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000571 string_intern(self, base), string_intern(self, systemId),
572 string_intern(self, publicId),
573 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000574#endif
575
576VOID_HANDLER(XmlDecl,
577 (void *userData,
578 const XML_Char *version,
579 const XML_Char *encoding,
580 int standalone),
581 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000582 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000583 standalone))
584
585static PyObject *
586conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000587 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000588{
589 PyObject *result = NULL;
590 PyObject *children = PyTuple_New(model->numchildren);
591 int i;
592
593 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000594 assert(model->numchildren < INT_MAX);
595 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000596 PyObject *child = conv_content_model(&model->children[i],
597 conv_string);
598 if (child == NULL) {
599 Py_XDECREF(children);
600 return NULL;
601 }
602 PyTuple_SET_ITEM(children, i, child);
603 }
604 result = Py_BuildValue("(iiO&N)",
605 model->type, model->quant,
606 conv_string,model->name, children);
607 }
608 return result;
609}
610
611static PyObject *
612conv_content_model_utf8(XML_Content * const model)
613{
614 return conv_content_model(model, conv_string_to_utf8);
615}
616
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000617#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000618static PyObject *
619conv_content_model_unicode(XML_Content * const model)
620{
621 return conv_content_model(model, conv_string_to_unicode);
622}
623
624VOID_HANDLER(ElementDecl,
625 (void *userData,
626 const XML_Char *name,
627 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000628 ("NO&",
629 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000630 (self->returns_unicode ? conv_content_model_unicode
631 : conv_content_model_utf8),model))
632#else
633VOID_HANDLER(ElementDecl,
634 (void *userData,
635 const XML_Char *name,
636 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000637 ("NO&",
638 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000639#endif
640
641VOID_HANDLER(AttlistDecl,
642 (void *userData,
643 const XML_Char *elname,
644 const XML_Char *attname,
645 const XML_Char *att_type,
646 const XML_Char *dflt,
647 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000648 ("(NNO&O&i)",
649 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000650 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
651 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000652
Fred Drake71b63ff2002-06-28 22:29:01 +0000653VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000654 (void *userData,
655 const XML_Char *notationName,
656 const XML_Char *base,
657 const XML_Char *systemId,
658 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000659 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000660 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000661 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000662
Fred Drake6f987622000-08-25 18:03:30 +0000663VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000664 (void *userData,
665 const XML_Char *prefix,
666 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000667 ("(NN)",
668 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000669
Fred Drake6f987622000-08-25 18:03:30 +0000670VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000671 (void *userData,
672 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000673 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674
Fred Drake6f987622000-08-25 18:03:30 +0000675VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000676 (void *userData, const XML_Char *data),
677 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678
Fred Drake6f987622000-08-25 18:03:30 +0000679VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000681 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000682
Fred Drake6f987622000-08-25 18:03:30 +0000683VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000684 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000685 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000686
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000687#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000688VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000689 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000690 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000691
Fred Drake6f987622000-08-25 18:03:30 +0000692VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000693 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000694 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000695#else
Fred Drake6f987622000-08-25 18:03:30 +0000696VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000697 (void *userData, const XML_Char *s, int len),
698 ("(N)", (self->returns_unicode
699 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000700 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701
Fred Drake6f987622000-08-25 18:03:30 +0000702VOID_HANDLER(DefaultHandlerExpand,
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#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000708
Fred Drake71b63ff2002-06-28 22:29:01 +0000709INT_HANDLER(NotStandalone,
710 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000711 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000712
Fred Drake6f987622000-08-25 18:03:30 +0000713RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000714 (XML_Parser parser,
715 const XML_Char *context,
716 const XML_Char *base,
717 const XML_Char *systemId,
718 const XML_Char *publicId),
719 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000720 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000721 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000722 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000723 rc = PyInt_AsLong(rv);, rc,
724 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000725
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000726/* XXX UnknownEncodingHandler */
727
Fred Drake85d835f2001-02-08 15:39:08 +0000728VOID_HANDLER(StartDoctypeDecl,
729 (void *userData, const XML_Char *doctypeName,
730 const XML_Char *sysid, const XML_Char *pubid,
731 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000732 ("(NNNi)", string_intern(self, doctypeName),
733 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000734 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000735
736VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000737
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000738/* ---------------------------------------------------------------- */
739
Fred Drake71b63ff2002-06-28 22:29:01 +0000740static PyObject *
741get_parse_result(xmlparseobject *self, int rv)
742{
743 if (PyErr_Occurred()) {
744 return NULL;
745 }
746 if (rv == 0) {
747 return set_error(self);
748 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000749 if (flush_character_buffer(self) < 0) {
750 return NULL;
751 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000752 return PyInt_FromLong(rv);
753}
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000756"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758
759static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000760xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000761{
Fred Drake0582df92000-07-12 04:49:00 +0000762 char *s;
763 int slen;
764 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000765
Fred Drake0582df92000-07-12 04:49:00 +0000766 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
767 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000768
769 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770}
771
Fred Drakeca1f4262000-09-21 20:10:23 +0000772/* File reading copied from cPickle */
773
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000774#define BUF_SIZE 2048
775
Fred Drake0582df92000-07-12 04:49:00 +0000776static int
777readinst(char *buf, int buf_size, PyObject *meth)
778{
779 PyObject *arg = NULL;
780 PyObject *bytes = NULL;
781 PyObject *str = NULL;
782 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783
Fred Drake676940b2000-09-22 15:21:31 +0000784 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000785 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000786
Fred Drakeca1f4262000-09-21 20:10:23 +0000787 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000788 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789
Tim Peters954eef72000-09-22 06:01:11 +0000790 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000791
Fred Drakeca1f4262000-09-21 20:10:23 +0000792 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000793 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000794
Fred Drake0582df92000-07-12 04:49:00 +0000795 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000796 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000797 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000798 "read() did not return a string object (type=%.400s)",
799 str->ob_type->tp_name);
800 goto finally;
801 }
802 len = PyString_GET_SIZE(str);
803 if (len > buf_size) {
804 PyErr_Format(PyExc_ValueError,
805 "read() returned too much data: "
806 "%i bytes requested, %i returned",
807 buf_size, len);
808 Py_DECREF(str);
809 goto finally;
810 }
811 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000812finally:
Fred Drake0582df92000-07-12 04:49:00 +0000813 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000814 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000815 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000816}
817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000819"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000821
822static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000823xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000824{
Fred Drake0582df92000-07-12 04:49:00 +0000825 int rv = 1;
826 PyObject *f;
827 FILE *fp;
828 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829
Fred Drake0582df92000-07-12 04:49:00 +0000830 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
831 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000832
Fred Drake0582df92000-07-12 04:49:00 +0000833 if (PyFile_Check(f)) {
834 fp = PyFile_AsFile(f);
835 }
836 else{
837 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000838 readmethod = PyObject_GetAttrString(f, "read");
839 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000840 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000841 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000842 "argument must have 'read' attribute");
843 return 0;
844 }
845 }
846 for (;;) {
847 int bytes_read;
848 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
849 if (buf == NULL)
850 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000851
Fred Drake0582df92000-07-12 04:49:00 +0000852 if (fp) {
853 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
854 if (bytes_read < 0) {
855 PyErr_SetFromErrno(PyExc_IOError);
856 return NULL;
857 }
858 }
859 else {
860 bytes_read = readinst(buf, BUF_SIZE, readmethod);
861 if (bytes_read < 0)
862 return NULL;
863 }
864 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
865 if (PyErr_Occurred())
866 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000867
Fred Drake0582df92000-07-12 04:49:00 +0000868 if (!rv || bytes_read == 0)
869 break;
870 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000871 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000872}
873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000874PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000875"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000877
878static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000879xmlparse_SetBase(xmlparseobject *self, PyObject *args)
880{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000881 char *base;
882
Fred Drake0582df92000-07-12 04:49:00 +0000883 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000884 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000885 if (!XML_SetBase(self->itself, base)) {
886 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000887 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000888 Py_INCREF(Py_None);
889 return Py_None;
890}
891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000893"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000895
896static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000897xmlparse_GetBase(xmlparseobject *self, PyObject *args)
898{
899 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000900 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000901
Fred Drake0582df92000-07-12 04:49:00 +0000902 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903}
904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000905PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000906"GetInputContext() -> string\n\
907Return the untranslated text of the input that caused the current event.\n\
908If 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 +0000909for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000910
911static PyObject *
912xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
913{
914 PyObject *result = NULL;
915
916 if (PyArg_ParseTuple(args, ":GetInputContext")) {
917 if (self->in_callback) {
918 int offset, size;
919 const char *buffer
920 = XML_GetInputContext(self->itself, &offset, &size);
921
922 if (buffer != NULL)
923 result = PyString_FromStringAndSize(buffer + offset, size);
924 else {
925 result = Py_None;
926 Py_INCREF(result);
927 }
928 }
929 else {
930 result = Py_None;
931 Py_INCREF(result);
932 }
933 }
934 return result;
935}
Fred Drakebd6101c2001-02-14 18:29:45 +0000936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000937PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000938"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000939Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000941
942static PyObject *
943xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
944{
945 char *context;
946 char *encoding = NULL;
947 xmlparseobject *new_parser;
948 int i;
949
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000950 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000951 &context, &encoding)) {
952 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000953 }
954
Martin v. Löwis894258c2001-09-23 10:20:10 +0000955#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000956 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000957 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000958#else
959 /* Python versions 2.2 and later */
960 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
961#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000962
963 if (new_parser == NULL)
964 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000965 new_parser->buffer_size = self->buffer_size;
966 new_parser->buffer_used = 0;
967 if (self->buffer != NULL) {
968 new_parser->buffer = malloc(new_parser->buffer_size);
969 if (new_parser->buffer == NULL) {
970 PyObject_GC_Del(new_parser);
971 return PyErr_NoMemory();
972 }
973 }
974 else
975 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000976 new_parser->returns_unicode = self->returns_unicode;
977 new_parser->ordered_attributes = self->ordered_attributes;
978 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000979 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000980 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000981 encoding);
982 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000983 new_parser->intern = self->intern;
984 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000985#ifdef Py_TPFLAGS_HAVE_GC
986 PyObject_GC_Track(new_parser);
987#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000988 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000989#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000990
991 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000992 Py_DECREF(new_parser);
993 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000994 }
995
996 XML_SetUserData(new_parser->itself, (void *)new_parser);
997
998 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +0000999 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001000 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001001
Fred Drake2a3d7db2002-06-28 22:56:48 +00001002 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001003 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001004 Py_DECREF(new_parser);
1005 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001006 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001007 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001008
1009 /* then copy handlers from self */
1010 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001011 PyObject *handler = self->handlers[i];
1012 if (handler != NULL) {
1013 Py_INCREF(handler);
1014 new_parser->handlers[i] = handler;
1015 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001016 handler_info[i].handler);
1017 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001018 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001019 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001020}
1021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001023"SetParamEntityParsing(flag) -> success\n\
1024Controls parsing of parameter entities (including the external DTD\n\
1025subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1026XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1027XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001029
1030static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001031xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001032{
Fred Drake85d835f2001-02-08 15:39:08 +00001033 int flag;
1034 if (!PyArg_ParseTuple(args, "i", &flag))
1035 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001036 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001037 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038}
1039
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001040static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001041 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001042 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001043 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001044 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001045 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001046 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001047 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001048 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001049 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1050 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001051 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1052 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001053 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1054 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001055 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001056};
1057
1058/* ---------- */
1059
1060
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001061#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001062
Fred Drake71b63ff2002-06-28 22:29:01 +00001063/* pyexpat international encoding support.
1064 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001065*/
1066
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001067static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001068PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001069
Fred Drake71b63ff2002-06-28 22:29:01 +00001070static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001071init_template_buffer(void)
1072{
1073 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001074 for (i = 0; i < 256; i++) {
1075 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001076 }
Fred Drakebb66a202001-03-01 20:48:17 +00001077 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001078}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001079
Fred Drake71b63ff2002-06-28 22:29:01 +00001080static int
1081PyUnknownEncodingHandler(void *encodingHandlerData,
1082 const XML_Char *name,
1083 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001084{
Fred Drakebb66a202001-03-01 20:48:17 +00001085 PyUnicodeObject *_u_string = NULL;
1086 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001087 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001088
Fred Drakebb66a202001-03-01 20:48:17 +00001089 /* Yes, supports only 8bit encodings */
1090 _u_string = (PyUnicodeObject *)
1091 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001092
Fred Drakebb66a202001-03-01 20:48:17 +00001093 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001094 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001095
Fred Drakebb66a202001-03-01 20:48:17 +00001096 for (i = 0; i < 256; i++) {
1097 /* Stupid to access directly, but fast */
1098 Py_UNICODE c = _u_string->str[i];
1099 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001100 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001101 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001103 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001104 info->data = NULL;
1105 info->convert = NULL;
1106 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001107 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108 Py_DECREF(_u_string);
1109 return result;
1110}
1111
1112#endif
1113
1114static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001115newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001116{
1117 int i;
1118 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001119
Martin v. Löwis894258c2001-09-23 10:20:10 +00001120#ifdef Py_TPFLAGS_HAVE_GC
1121 /* Code for versions 2.2 and later */
1122 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1123#else
Fred Drake0582df92000-07-12 04:49:00 +00001124 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001125#endif
Fred Drake0582df92000-07-12 04:49:00 +00001126 if (self == NULL)
1127 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001128
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001129#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001130 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001131#else
1132 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001133#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001134
Fred Drake2a3d7db2002-06-28 22:56:48 +00001135 self->buffer = NULL;
1136 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1137 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001138 self->ordered_attributes = 0;
1139 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001140 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001141 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001142 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001143 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1144 }
Fred Drake85d835f2001-02-08 15:39:08 +00001145 else {
Fred Drake0582df92000-07-12 04:49:00 +00001146 self->itself = XML_ParserCreate(encoding);
1147 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001148 self->intern = intern;
1149 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001150#ifdef Py_TPFLAGS_HAVE_GC
1151 PyObject_GC_Track(self);
1152#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001153 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001154#endif
Fred Drake0582df92000-07-12 04:49:00 +00001155 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001156 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001157 "XML_ParserCreate failed");
1158 Py_DECREF(self);
1159 return NULL;
1160 }
1161 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001162#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001163 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1164#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001165
Fred Drake2a3d7db2002-06-28 22:56:48 +00001166 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001167 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001168
Fred Drake0582df92000-07-12 04:49:00 +00001169 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001170 if (!self->handlers){
Fred Drake71b63ff2002-06-28 22:29:01 +00001171 Py_DECREF(self);
1172 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001173 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001174 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001175
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001176 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001177}
1178
1179
1180static void
Fred Drake0582df92000-07-12 04:49:00 +00001181xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001182{
Fred Drake0582df92000-07-12 04:49:00 +00001183 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001184#ifdef Py_TPFLAGS_HAVE_GC
1185 PyObject_GC_UnTrack(self);
1186#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001187 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001188#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001189 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001190 XML_ParserFree(self->itself);
1191 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001192
Fred Drake85d835f2001-02-08 15:39:08 +00001193 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001194 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001195 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001196 temp = self->handlers[i];
1197 self->handlers[i] = NULL;
1198 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001199 }
1200 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001201 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001202 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001203 if (self->buffer != NULL) {
1204 free(self->buffer);
1205 self->buffer = NULL;
1206 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001207 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001208#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001209 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001210 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001211#else
1212 /* Code for versions 2.2 and later. */
1213 PyObject_GC_Del(self);
1214#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001215}
1216
Fred Drake0582df92000-07-12 04:49:00 +00001217static int
1218handlername2int(const char *name)
1219{
1220 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001221 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001222 if (strcmp(name, handler_info[i].name) == 0) {
1223 return i;
1224 }
1225 }
1226 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001227}
1228
1229static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001230get_pybool(int istrue)
1231{
1232 PyObject *result = istrue ? Py_True : Py_False;
1233 Py_INCREF(result);
1234 return result;
1235}
1236
1237static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001238xmlparse_getattr(xmlparseobject *self, char *name)
1239{
Fred Drake71b63ff2002-06-28 22:29:01 +00001240 int handlernum = handlername2int(name);
1241
1242 if (handlernum != -1) {
1243 PyObject *result = self->handlers[handlernum];
1244 if (result == NULL)
1245 result = Py_None;
1246 Py_INCREF(result);
1247 return result;
1248 }
1249 if (name[0] == 'E') {
1250 if (strcmp(name, "ErrorCode") == 0)
1251 return PyInt_FromLong((long)
1252 XML_GetErrorCode(self->itself));
1253 if (strcmp(name, "ErrorLineNumber") == 0)
1254 return PyInt_FromLong((long)
1255 XML_GetErrorLineNumber(self->itself));
1256 if (strcmp(name, "ErrorColumnNumber") == 0)
1257 return PyInt_FromLong((long)
1258 XML_GetErrorColumnNumber(self->itself));
1259 if (strcmp(name, "ErrorByteIndex") == 0)
1260 return PyInt_FromLong((long)
1261 XML_GetErrorByteIndex(self->itself));
1262 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001263 if (name[0] == 'b') {
1264 if (strcmp(name, "buffer_size") == 0)
1265 return PyInt_FromLong((long) self->buffer_size);
1266 if (strcmp(name, "buffer_text") == 0)
1267 return get_pybool(self->buffer != NULL);
1268 if (strcmp(name, "buffer_used") == 0)
1269 return PyInt_FromLong((long) self->buffer_used);
1270 }
Fred Drake85d835f2001-02-08 15:39:08 +00001271 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001272 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001273 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001274 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001275 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001276 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001277 if (strcmp(name, "intern") == 0) {
1278 if (self->intern == NULL) {
1279 Py_INCREF(Py_None);
1280 return Py_None;
1281 }
1282 else {
1283 Py_INCREF(self->intern);
1284 return self->intern;
1285 }
1286 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001287
Fred Drake0582df92000-07-12 04:49:00 +00001288 if (strcmp(name, "__members__") == 0) {
1289 int i;
1290 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001291 for (i = 0; handler_info[i].name != NULL; i++) {
1292 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001293 }
1294 PyList_Append(rc, PyString_FromString("ErrorCode"));
1295 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1296 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1297 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake2a3d7db2002-06-28 22:56:48 +00001298 PyList_Append(rc, PyString_FromString("buffer_size"));
1299 PyList_Append(rc, PyString_FromString("buffer_text"));
1300 PyList_Append(rc, PyString_FromString("buffer_used"));
Fred Drake85d835f2001-02-08 15:39:08 +00001301 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001302 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001303 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001304 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001305
Fred Drake0582df92000-07-12 04:49:00 +00001306 return rc;
1307 }
1308 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001309}
1310
Fred Drake6f987622000-08-25 18:03:30 +00001311static int
1312sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001313{
1314 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001315 if (handlernum >= 0) {
1316 xmlhandler c_handler = NULL;
1317 PyObject *temp = self->handlers[handlernum];
1318
1319 if (v == Py_None)
1320 v = NULL;
1321 else if (v != NULL) {
1322 Py_INCREF(v);
1323 c_handler = handler_info[handlernum].handler;
1324 }
Fred Drake0582df92000-07-12 04:49:00 +00001325 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001326 Py_XDECREF(temp);
1327 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001328 return 1;
1329 }
1330 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001331}
1332
1333static int
Fred Drake6f987622000-08-25 18:03:30 +00001334xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001335{
Fred Drake6f987622000-08-25 18:03:30 +00001336 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001337 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001338 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1339 return -1;
1340 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001341 if (strcmp(name, "buffer_text") == 0) {
1342 if (PyObject_IsTrue(v)) {
1343 if (self->buffer == NULL) {
1344 self->buffer = malloc(self->buffer_size);
1345 if (self->buffer == NULL) {
1346 PyErr_NoMemory();
1347 return -1;
1348 }
1349 self->buffer_used = 0;
1350 }
1351 }
1352 else if (self->buffer != NULL) {
1353 if (flush_character_buffer(self) < 0)
1354 return -1;
1355 free(self->buffer);
1356 self->buffer = NULL;
1357 }
1358 return 0;
1359 }
Fred Drake85d835f2001-02-08 15:39:08 +00001360 if (strcmp(name, "ordered_attributes") == 0) {
1361 if (PyObject_IsTrue(v))
1362 self->ordered_attributes = 1;
1363 else
1364 self->ordered_attributes = 0;
1365 return 0;
1366 }
Fred Drake6f987622000-08-25 18:03:30 +00001367 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001368 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001369#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001370 PyErr_SetString(PyExc_ValueError,
1371 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001372 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001373#else
Fred Drake6f987622000-08-25 18:03:30 +00001374 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001375#endif
Fred Drake6f987622000-08-25 18:03:30 +00001376 }
1377 else
1378 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001379 return 0;
1380 }
1381 if (strcmp(name, "specified_attributes") == 0) {
1382 if (PyObject_IsTrue(v))
1383 self->specified_attributes = 1;
1384 else
1385 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001386 return 0;
1387 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001388 if (strcmp(name, "CharacterDataHandler") == 0) {
1389 /* If we're changing the character data handler, flush all
1390 * cached data with the old handler. Not sure there's a
1391 * "right" thing to do, though, but this probably won't
1392 * happen.
1393 */
1394 if (flush_character_buffer(self) < 0)
1395 return -1;
1396 }
Fred Drake6f987622000-08-25 18:03:30 +00001397 if (sethandler(self, name, v)) {
1398 return 0;
1399 }
1400 PyErr_SetString(PyExc_AttributeError, name);
1401 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001402}
1403
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001404#ifdef WITH_CYCLE_GC
1405static int
1406xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1407{
Fred Drakecde79132001-04-25 16:01:30 +00001408 int i, err;
1409 for (i = 0; handler_info[i].name != NULL; i++) {
1410 if (!op->handlers[i])
1411 continue;
1412 err = visit(op->handlers[i], arg);
1413 if (err)
1414 return err;
1415 }
1416 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001417}
1418
1419static int
1420xmlparse_clear(xmlparseobject *op)
1421{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001422 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001423 Py_XDECREF(op->intern);
1424 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001425 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001426}
1427#endif
1428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001429PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001430
1431static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001432 PyObject_HEAD_INIT(NULL)
1433 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001434 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001435 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001436 0, /*tp_itemsize*/
1437 /* methods */
1438 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1439 (printfunc)0, /*tp_print*/
1440 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1441 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1442 (cmpfunc)0, /*tp_compare*/
1443 (reprfunc)0, /*tp_repr*/
1444 0, /*tp_as_number*/
1445 0, /*tp_as_sequence*/
1446 0, /*tp_as_mapping*/
1447 (hashfunc)0, /*tp_hash*/
1448 (ternaryfunc)0, /*tp_call*/
1449 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001450 0, /* tp_getattro */
1451 0, /* tp_setattro */
1452 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001453#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001454 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001455#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001456 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001457#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001458 Xmlparsetype__doc__, /* Documentation string */
1459#ifdef WITH_CYCLE_GC
1460 (traverseproc)xmlparse_traverse, /* tp_traverse */
1461 (inquiry)xmlparse_clear /* tp_clear */
1462#else
1463 0, 0
1464#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001465};
1466
1467/* End of code for xmlparser objects */
1468/* -------------------------------------------------------- */
1469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001471"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001473
1474static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001475pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1476{
Fred Drakecde79132001-04-25 16:01:30 +00001477 char *encoding = NULL;
1478 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001479 PyObject *intern = NULL;
1480 PyObject *result;
1481 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001482 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001483 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001484
Fred Drakeb91a36b2002-06-27 19:40:48 +00001485 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1486 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001487 return NULL;
1488 if (namespace_separator != NULL
1489 && strlen(namespace_separator) > 1) {
1490 PyErr_SetString(PyExc_ValueError,
1491 "namespace_separator must be at most one"
1492 " character, omitted, or None");
1493 return NULL;
1494 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001495 /* Explicitly passing None means no interning is desired.
1496 Not passing anything means that a new dictionary is used. */
1497 if (intern == Py_None)
1498 intern = NULL;
1499 else if (intern == NULL) {
1500 intern = PyDict_New();
1501 if (!intern)
1502 return NULL;
1503 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001504 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001505 else if (!PyDict_Check(intern)) {
1506 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1507 return NULL;
1508 }
1509
1510 result = newxmlparseobject(encoding, namespace_separator, intern);
1511 if (intern_decref) {
1512 Py_DECREF(intern);
1513 }
1514 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001515}
1516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001518"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001520
1521static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001522pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001523{
Fred Drake0582df92000-07-12 04:49:00 +00001524 long code = 0;
1525
1526 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1527 return NULL;
1528 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001529}
1530
1531/* List of methods defined in the module */
1532
1533static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001534 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1535 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1536 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1537 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001538
Fred Drake0582df92000-07-12 04:49:00 +00001539 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001540};
1541
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001542/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001544PyDoc_STRVAR(pyexpat_module_documentation,
1545"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001546
Fred Drake4113b132001-03-24 19:58:26 +00001547/* Return a Python string that represents the version number without the
1548 * extra cruft added by revision control, even if the right options were
1549 * given to the "cvs export" command to make it not include the extra
1550 * cruft.
1551 */
1552static PyObject *
1553get_version_string(void)
1554{
1555 static char *rcsid = "$Revision$";
1556 char *rev = rcsid;
1557 int i = 0;
1558
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001559 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001560 ++rev;
1561 while (rev[i] != ' ' && rev[i] != '\0')
1562 ++i;
1563
1564 return PyString_FromStringAndSize(rev, i);
1565}
1566
Fred Drakecde79132001-04-25 16:01:30 +00001567/* Initialization function for the module */
1568
1569#ifndef MODULE_NAME
1570#define MODULE_NAME "pyexpat"
1571#endif
1572
1573#ifndef MODULE_INITFUNC
1574#define MODULE_INITFUNC initpyexpat
1575#endif
1576
1577void MODULE_INITFUNC(void); /* avoid compiler warnings */
1578
Fred Drake6f987622000-08-25 18:03:30 +00001579DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001580MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001581{
1582 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001583 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001584 PyObject *errors_module;
1585 PyObject *modelmod_name;
1586 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001587 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001588
Fred Drake6f987622000-08-25 18:03:30 +00001589 if (errmod_name == NULL)
1590 return;
Fred Drakecde79132001-04-25 16:01:30 +00001591 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001592 if (modelmod_name == NULL)
1593 return;
Fred Drake6f987622000-08-25 18:03:30 +00001594
Fred Drake0582df92000-07-12 04:49:00 +00001595 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001596
Fred Drake0582df92000-07-12 04:49:00 +00001597 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001598 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001599 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001600
Fred Drake0582df92000-07-12 04:49:00 +00001601 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001602 if (ErrorObject == NULL) {
1603 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001604 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001605 if (ErrorObject == NULL)
1606 return;
1607 }
1608 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001609 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001610 Py_INCREF(ErrorObject);
1611 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001612 Py_INCREF(&Xmlparsetype);
1613 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001614
Fred Drake4113b132001-03-24 19:58:26 +00001615 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001616 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1617 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001618 {
1619 XML_Expat_Version info = XML_ExpatVersionInfo();
1620 PyModule_AddObject(m, "version_info",
1621 Py_BuildValue("(iii)", info.major,
1622 info.minor, info.micro));
1623 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001624#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001625 init_template_buffer();
1626#endif
Fred Drake0582df92000-07-12 04:49:00 +00001627 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001628 compiled, this should check and set native_encoding
1629 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001630 */
Fred Drake93adb692000-09-23 04:55:48 +00001631 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001632
Fred Drake85d835f2001-02-08 15:39:08 +00001633 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001634 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001635 errors_module = PyDict_GetItem(d, errmod_name);
1636 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001637 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001638 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001639 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001640 /* gives away the reference to errors_module */
1641 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001642 }
1643 }
Fred Drake6f987622000-08-25 18:03:30 +00001644 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001645 model_module = PyDict_GetItem(d, modelmod_name);
1646 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001647 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001648 if (model_module != NULL) {
1649 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1650 /* gives away the reference to model_module */
1651 PyModule_AddObject(m, "model", model_module);
1652 }
1653 }
1654 Py_DECREF(modelmod_name);
1655 if (errors_module == NULL || model_module == NULL)
1656 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001657 return;
1658
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001659#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001660 PyModule_AddStringConstant(errors_module, #name, \
1661 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001662
Fred Drake0582df92000-07-12 04:49:00 +00001663 MYCONST(XML_ERROR_NO_MEMORY);
1664 MYCONST(XML_ERROR_SYNTAX);
1665 MYCONST(XML_ERROR_NO_ELEMENTS);
1666 MYCONST(XML_ERROR_INVALID_TOKEN);
1667 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1668 MYCONST(XML_ERROR_PARTIAL_CHAR);
1669 MYCONST(XML_ERROR_TAG_MISMATCH);
1670 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1671 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1672 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1673 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1674 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1675 MYCONST(XML_ERROR_ASYNC_ENTITY);
1676 MYCONST(XML_ERROR_BAD_CHAR_REF);
1677 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1678 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1679 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1680 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1681 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001682 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1683 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1684 MYCONST(XML_ERROR_NOT_STANDALONE);
1685
Fred Drake85d835f2001-02-08 15:39:08 +00001686 PyModule_AddStringConstant(errors_module, "__doc__",
1687 "Constants used to describe error conditions.");
1688
Fred Drake93adb692000-09-23 04:55:48 +00001689#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001690
Fred Drake85d835f2001-02-08 15:39:08 +00001691#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001692 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1693 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1694 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001695#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001696
Fred Drake85d835f2001-02-08 15:39:08 +00001697#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1698 PyModule_AddStringConstant(model_module, "__doc__",
1699 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001700
Fred Drake85d835f2001-02-08 15:39:08 +00001701 MYCONST(XML_CTYPE_EMPTY);
1702 MYCONST(XML_CTYPE_ANY);
1703 MYCONST(XML_CTYPE_MIXED);
1704 MYCONST(XML_CTYPE_NAME);
1705 MYCONST(XML_CTYPE_CHOICE);
1706 MYCONST(XML_CTYPE_SEQ);
1707
1708 MYCONST(XML_CQUANT_NONE);
1709 MYCONST(XML_CQUANT_OPT);
1710 MYCONST(XML_CQUANT_REP);
1711 MYCONST(XML_CQUANT_PLUS);
1712#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001713}
1714
Fred Drake6f987622000-08-25 18:03:30 +00001715static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001716clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001717{
Fred Drakecde79132001-04-25 16:01:30 +00001718 int i = 0;
1719 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001720
Fred Drake71b63ff2002-06-28 22:29:01 +00001721 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001722 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001723 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001724 else {
Fred Drakecde79132001-04-25 16:01:30 +00001725 temp = self->handlers[i];
1726 self->handlers[i] = NULL;
1727 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001728 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001729 }
Fred Drakecde79132001-04-25 16:01:30 +00001730 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001731}
1732
Fred Drake0582df92000-07-12 04:49:00 +00001733statichere struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001734 {"StartElementHandler",
1735 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001736 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001737 {"EndElementHandler",
1738 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001739 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001740 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001741 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1742 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001743 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001744 (xmlhandlersetter)XML_SetCharacterDataHandler,
1745 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001746 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001747 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001748 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001749 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001750 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001751 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001752 {"StartNamespaceDeclHandler",
1753 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001754 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001755 {"EndNamespaceDeclHandler",
1756 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001757 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001758 {"CommentHandler",
1759 (xmlhandlersetter)XML_SetCommentHandler,
1760 (xmlhandler)my_CommentHandler},
1761 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001762 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001763 (xmlhandler)my_StartCdataSectionHandler},
1764 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001765 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001766 (xmlhandler)my_EndCdataSectionHandler},
1767 {"DefaultHandler",
1768 (xmlhandlersetter)XML_SetDefaultHandler,
1769 (xmlhandler)my_DefaultHandler},
1770 {"DefaultHandlerExpand",
1771 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1772 (xmlhandler)my_DefaultHandlerExpandHandler},
1773 {"NotStandaloneHandler",
1774 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1775 (xmlhandler)my_NotStandaloneHandler},
1776 {"ExternalEntityRefHandler",
1777 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001778 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001779 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001780 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001781 (xmlhandler)my_StartDoctypeDeclHandler},
1782 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001783 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001784 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001785 {"EntityDeclHandler",
1786 (xmlhandlersetter)XML_SetEntityDeclHandler,
1787 (xmlhandler)my_EntityDeclHandler},
1788 {"XmlDeclHandler",
1789 (xmlhandlersetter)XML_SetXmlDeclHandler,
1790 (xmlhandler)my_XmlDeclHandler},
1791 {"ElementDeclHandler",
1792 (xmlhandlersetter)XML_SetElementDeclHandler,
1793 (xmlhandler)my_ElementDeclHandler},
1794 {"AttlistDeclHandler",
1795 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1796 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001797
Fred Drake0582df92000-07-12 04:49:00 +00001798 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001799};