blob: 85cb602fb6315d7e849b09e64b572c5da0cee557 [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 Drakef57b22a2002-09-02 15:54:06 +00009#define PyDoc_STR(str) str
Fred Drake7c75bf22002-07-01 14:02:31 +000010#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
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000267static int
268trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
269{
270 int result = 0;
271 if (!tstate->use_tracing || tstate->tracing)
272 return 0;
273 if (tstate->c_profilefunc != NULL) {
274 tstate->tracing++;
275 result = tstate->c_profilefunc(tstate->c_profileobj,
276 f, code , val);
277 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
278 || (tstate->c_profilefunc != NULL));
279 tstate->tracing--;
280 if (result)
281 return result;
282 }
283 if (tstate->c_tracefunc != NULL) {
284 tstate->tracing++;
285 result = tstate->c_tracefunc(tstate->c_traceobj,
286 f, code , val);
287 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
288 || (tstate->c_profilefunc != NULL));
289 tstate->tracing--;
290 }
291 return result;
292}
293
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000294static PyObject*
295call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
296{
Fred Drakebd6101c2001-02-14 18:29:45 +0000297 PyThreadState *tstate = PyThreadState_GET();
298 PyFrameObject *f;
299 PyObject *res;
300
301 if (c == NULL)
302 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000303
Fred Drakebd6101c2001-02-14 18:29:45 +0000304 f = PyFrame_New(
305 tstate, /*back*/
306 c, /*code*/
307 tstate->frame->f_globals, /*globals*/
308 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000309 );
310 if (f == NULL)
311 return NULL;
312 tstate->frame = f;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000313 if (trace_frame(tstate, f, PyTrace_CALL, Py_None)) {
314 Py_DECREF(f);
315 return NULL;
316 }
Fred Drakebd6101c2001-02-14 18:29:45 +0000317 res = PyEval_CallObject(func, args);
318 if (res == NULL && tstate->curexc_traceback == NULL)
319 PyTraceBack_Here(f);
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000320 else {
321 if (trace_frame(tstate, f, PyTrace_RETURN, res)) {
322 Py_XDECREF(res);
323 res = NULL;
324 }
325 }
Fred Drakebd6101c2001-02-14 18:29:45 +0000326 tstate->frame = f->f_back;
327 Py_DECREF(f);
328 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000329}
330
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000331#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000332#define STRING_CONV_FUNC conv_string_to_utf8
333#else
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000334/* Python 2.0 and later versions */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000335#define STRING_CONV_FUNC (self->returns_unicode \
336 ? conv_string_to_unicode : conv_string_to_utf8)
337#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000338
Fred Drakeb91a36b2002-06-27 19:40:48 +0000339static PyObject*
340string_intern(xmlparseobject *self, const char* str)
341{
342 PyObject *result = STRING_CONV_FUNC(str);
343 PyObject *value;
344 if (!self->intern)
345 return result;
346 value = PyDict_GetItem(self->intern, result);
347 if (!value) {
348 if (PyDict_SetItem(self->intern, result, result) == 0)
349 return result;
350 else
351 return NULL;
352 }
353 Py_INCREF(value);
354 Py_DECREF(result);
355 return value;
356}
357
Fred Drake2a3d7db2002-06-28 22:56:48 +0000358/* Return 0 on success, -1 on exception.
359 * flag_error() will be called before return if needed.
360 */
361static int
362call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
363{
364 PyObject *args;
365 PyObject *temp;
366
367 args = PyTuple_New(1);
368 if (args == NULL)
369 return -1;
370#ifdef Py_USING_UNICODE
371 temp = (self->returns_unicode
372 ? conv_string_len_to_unicode(buffer, len)
373 : conv_string_len_to_utf8(buffer, len));
374#else
375 temp = conv_string_len_to_utf8(buffer, len);
376#endif
377 if (temp == NULL) {
378 Py_DECREF(args);
379 flag_error(self);
380 return -1;
381 }
382 PyTuple_SET_ITEM(args, 0, temp);
383 /* temp is now a borrowed reference; consider it unused. */
384 self->in_callback = 1;
385 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
386 self->handlers[CharacterData], args);
387 /* temp is an owned reference again, or NULL */
388 self->in_callback = 0;
389 Py_DECREF(args);
390 if (temp == NULL) {
391 flag_error(self);
392 return -1;
393 }
394 Py_DECREF(temp);
395 return 0;
396}
397
398static int
399flush_character_buffer(xmlparseobject *self)
400{
401 int rc;
402 if (self->buffer == NULL || self->buffer_used == 0)
403 return 0;
404 rc = call_character_handler(self, self->buffer, self->buffer_used);
405 self->buffer_used = 0;
406 return rc;
407}
408
409static void
410my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
411{
412 xmlparseobject *self = (xmlparseobject *) userData;
413 if (self->buffer == NULL)
414 call_character_handler(self, data, len);
415 else {
416 if ((self->buffer_used + len) > self->buffer_size) {
417 if (flush_character_buffer(self) < 0)
418 return;
419 /* handler might have changed; drop the rest on the floor
420 * if there isn't a handler anymore
421 */
422 if (!have_handler(self, CharacterData))
423 return;
424 }
425 if (len > self->buffer_size) {
426 call_character_handler(self, data, len);
427 self->buffer_used = 0;
428 }
429 else {
430 memcpy(self->buffer + self->buffer_used,
431 data, len * sizeof(XML_Char));
432 self->buffer_used += len;
433 }
434 }
435}
436
Fred Drake85d835f2001-02-08 15:39:08 +0000437static void
438my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000439 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000440{
441 xmlparseobject *self = (xmlparseobject *)userData;
442
Fred Drake71b63ff2002-06-28 22:29:01 +0000443 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000444 PyObject *container, *rv, *args;
445 int i, max;
446
Fred Drake2a3d7db2002-06-28 22:56:48 +0000447 if (flush_character_buffer(self) < 0)
448 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000449 /* Set max to the number of slots filled in atts[]; max/2 is
450 * the number of attributes we need to process.
451 */
452 if (self->specified_attributes) {
453 max = XML_GetSpecifiedAttributeCount(self->itself);
454 }
455 else {
456 max = 0;
457 while (atts[max] != NULL)
458 max += 2;
459 }
460 /* Build the container. */
461 if (self->ordered_attributes)
462 container = PyList_New(max);
463 else
464 container = PyDict_New();
465 if (container == NULL) {
466 flag_error(self);
467 return;
468 }
469 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000470 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000471 PyObject *v;
472 if (n == NULL) {
473 flag_error(self);
474 Py_DECREF(container);
475 return;
476 }
477 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
478 if (v == NULL) {
479 flag_error(self);
480 Py_DECREF(container);
481 Py_DECREF(n);
482 return;
483 }
484 if (self->ordered_attributes) {
485 PyList_SET_ITEM(container, i, n);
486 PyList_SET_ITEM(container, i+1, v);
487 }
488 else if (PyDict_SetItem(container, n, v)) {
489 flag_error(self);
490 Py_DECREF(n);
491 Py_DECREF(v);
492 return;
493 }
494 else {
495 Py_DECREF(n);
496 Py_DECREF(v);
497 }
498 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000499 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000500 if (args == NULL) {
501 Py_DECREF(container);
502 return;
503 }
504 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000505 self->in_callback = 1;
506 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000507 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000508 self->in_callback = 0;
509 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000510 if (rv == NULL) {
511 flag_error(self);
512 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000513 }
Fred Drake85d835f2001-02-08 15:39:08 +0000514 Py_DECREF(rv);
515 }
516}
517
518#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
519 RETURN, GETUSERDATA) \
520static RC \
521my_##NAME##Handler PARAMS {\
522 xmlparseobject *self = GETUSERDATA ; \
523 PyObject *args = NULL; \
524 PyObject *rv = NULL; \
525 INIT \
526\
Fred Drake71b63ff2002-06-28 22:29:01 +0000527 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000528 if (flush_character_buffer(self) < 0) \
529 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000530 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000531 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000532 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000533 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
534 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000535 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000536 Py_DECREF(args); \
537 if (rv == NULL) { \
538 flag_error(self); \
539 return RETURN; \
540 } \
541 CONVERSION \
542 Py_DECREF(rv); \
543 } \
544 return RETURN; \
545}
546
Fred Drake6f987622000-08-25 18:03:30 +0000547#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
548 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
549 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000550
Fred Drake6f987622000-08-25 18:03:30 +0000551#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
552 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
553 rc = PyInt_AsLong(rv);, rc, \
554 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000555
Fred Drake71b63ff2002-06-28 22:29:01 +0000556VOID_HANDLER(EndElement,
557 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000558 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000559
Fred Drake6f987622000-08-25 18:03:30 +0000560VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000561 (void *userData,
562 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000563 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000564 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000565
Fred Drake6f987622000-08-25 18:03:30 +0000566VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000567 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000568 const XML_Char *entityName,
569 const XML_Char *base,
570 const XML_Char *systemId,
571 const XML_Char *publicId,
572 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000573 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000574 string_intern(self, entityName), string_intern(self, base),
575 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000576 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000577
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000578#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000579VOID_HANDLER(EntityDecl,
580 (void *userData,
581 const XML_Char *entityName,
582 int is_parameter_entity,
583 const XML_Char *value,
584 int value_length,
585 const XML_Char *base,
586 const XML_Char *systemId,
587 const XML_Char *publicId,
588 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000589 ("NiNNNNN",
590 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000591 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000592 string_intern(self, base), string_intern(self, systemId),
593 string_intern(self, publicId),
594 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000595#else
596VOID_HANDLER(EntityDecl,
597 (void *userData,
598 const XML_Char *entityName,
599 int is_parameter_entity,
600 const XML_Char *value,
601 int value_length,
602 const XML_Char *base,
603 const XML_Char *systemId,
604 const XML_Char *publicId,
605 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000606 ("NiNNNNN",
607 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000608 (self->returns_unicode
609 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000610 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000611 string_intern(self, base), string_intern(self, systemId),
612 string_intern(self, publicId),
613 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000614#endif
615
616VOID_HANDLER(XmlDecl,
617 (void *userData,
618 const XML_Char *version,
619 const XML_Char *encoding,
620 int standalone),
621 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000622 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000623 standalone))
624
625static PyObject *
626conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000627 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000628{
629 PyObject *result = NULL;
630 PyObject *children = PyTuple_New(model->numchildren);
631 int i;
632
633 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000634 assert(model->numchildren < INT_MAX);
635 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000636 PyObject *child = conv_content_model(&model->children[i],
637 conv_string);
638 if (child == NULL) {
639 Py_XDECREF(children);
640 return NULL;
641 }
642 PyTuple_SET_ITEM(children, i, child);
643 }
644 result = Py_BuildValue("(iiO&N)",
645 model->type, model->quant,
646 conv_string,model->name, children);
647 }
648 return result;
649}
650
651static PyObject *
652conv_content_model_utf8(XML_Content * const model)
653{
654 return conv_content_model(model, conv_string_to_utf8);
655}
656
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000657#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000658static PyObject *
659conv_content_model_unicode(XML_Content * const model)
660{
661 return conv_content_model(model, conv_string_to_unicode);
662}
663
664VOID_HANDLER(ElementDecl,
665 (void *userData,
666 const XML_Char *name,
667 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000668 ("NO&",
669 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000670 (self->returns_unicode ? conv_content_model_unicode
671 : conv_content_model_utf8),model))
672#else
673VOID_HANDLER(ElementDecl,
674 (void *userData,
675 const XML_Char *name,
676 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000677 ("NO&",
678 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000679#endif
680
681VOID_HANDLER(AttlistDecl,
682 (void *userData,
683 const XML_Char *elname,
684 const XML_Char *attname,
685 const XML_Char *att_type,
686 const XML_Char *dflt,
687 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000688 ("(NNO&O&i)",
689 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000690 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
691 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000692
Fred Drake71b63ff2002-06-28 22:29:01 +0000693VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694 (void *userData,
695 const XML_Char *notationName,
696 const XML_Char *base,
697 const XML_Char *systemId,
698 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000699 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000700 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000701 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000702
Fred Drake6f987622000-08-25 18:03:30 +0000703VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 (void *userData,
705 const XML_Char *prefix,
706 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000707 ("(NN)",
708 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000709
Fred Drake6f987622000-08-25 18:03:30 +0000710VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000711 (void *userData,
712 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000713 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000714
Fred Drake6f987622000-08-25 18:03:30 +0000715VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000716 (void *userData, const XML_Char *data),
717 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718
Fred Drake6f987622000-08-25 18:03:30 +0000719VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000720 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000721 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000722
Fred Drake6f987622000-08-25 18:03:30 +0000723VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000724 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000725 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000726
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000727#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000728VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000729 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000730 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000731
Fred Drake6f987622000-08-25 18:03:30 +0000732VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000733 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000734 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000735#else
Fred Drake6f987622000-08-25 18:03:30 +0000736VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000737 (void *userData, const XML_Char *s, int len),
738 ("(N)", (self->returns_unicode
739 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000740 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000741
Fred Drake6f987622000-08-25 18:03:30 +0000742VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000743 (void *userData, const XML_Char *s, int len),
744 ("(N)", (self->returns_unicode
745 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000746 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000747#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000748
Fred Drake71b63ff2002-06-28 22:29:01 +0000749INT_HANDLER(NotStandalone,
750 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000751 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000752
Fred Drake6f987622000-08-25 18:03:30 +0000753RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000754 (XML_Parser parser,
755 const XML_Char *context,
756 const XML_Char *base,
757 const XML_Char *systemId,
758 const XML_Char *publicId),
759 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000760 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000761 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000762 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000763 rc = PyInt_AsLong(rv);, rc,
764 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000765
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000766/* XXX UnknownEncodingHandler */
767
Fred Drake85d835f2001-02-08 15:39:08 +0000768VOID_HANDLER(StartDoctypeDecl,
769 (void *userData, const XML_Char *doctypeName,
770 const XML_Char *sysid, const XML_Char *pubid,
771 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000772 ("(NNNi)", string_intern(self, doctypeName),
773 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000774 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000775
776VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000777
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000778/* ---------------------------------------------------------------- */
779
Fred Drake71b63ff2002-06-28 22:29:01 +0000780static PyObject *
781get_parse_result(xmlparseobject *self, int rv)
782{
783 if (PyErr_Occurred()) {
784 return NULL;
785 }
786 if (rv == 0) {
787 return set_error(self);
788 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000789 if (flush_character_buffer(self) < 0) {
790 return NULL;
791 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000792 return PyInt_FromLong(rv);
793}
794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000795PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000796"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000798
799static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000800xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801{
Fred Drake0582df92000-07-12 04:49:00 +0000802 char *s;
803 int slen;
804 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000805
Fred Drake0582df92000-07-12 04:49:00 +0000806 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
807 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000808
809 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000810}
811
Fred Drakeca1f4262000-09-21 20:10:23 +0000812/* File reading copied from cPickle */
813
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000814#define BUF_SIZE 2048
815
Fred Drake0582df92000-07-12 04:49:00 +0000816static int
817readinst(char *buf, int buf_size, PyObject *meth)
818{
819 PyObject *arg = NULL;
820 PyObject *bytes = NULL;
821 PyObject *str = NULL;
822 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823
Fred Drake676940b2000-09-22 15:21:31 +0000824 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000825 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000826
Fred Drakeca1f4262000-09-21 20:10:23 +0000827 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000828 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829
Tim Peters954eef72000-09-22 06:01:11 +0000830 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000831
Guido van Rossum84b2bed2002-08-16 17:01:09 +0000832 if ((str = PyObject_Call(meth, arg, NULL)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000833 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834
Fred Drake0582df92000-07-12 04:49:00 +0000835 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000836 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000837 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000838 "read() did not return a string object (type=%.400s)",
839 str->ob_type->tp_name);
840 goto finally;
841 }
842 len = PyString_GET_SIZE(str);
843 if (len > buf_size) {
844 PyErr_Format(PyExc_ValueError,
845 "read() returned too much data: "
846 "%i bytes requested, %i returned",
847 buf_size, len);
848 Py_DECREF(str);
849 goto finally;
850 }
851 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000852finally:
Fred Drake0582df92000-07-12 04:49:00 +0000853 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000854 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000855 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000856}
857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000858PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000859"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000860Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000861
862static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000863xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000864{
Fred Drake0582df92000-07-12 04:49:00 +0000865 int rv = 1;
866 PyObject *f;
867 FILE *fp;
868 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000869
Fred Drake0582df92000-07-12 04:49:00 +0000870 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
871 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000872
Fred Drake0582df92000-07-12 04:49:00 +0000873 if (PyFile_Check(f)) {
874 fp = PyFile_AsFile(f);
875 }
876 else{
877 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000878 readmethod = PyObject_GetAttrString(f, "read");
879 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000880 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000881 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000882 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +0000883 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000884 }
885 }
886 for (;;) {
887 int bytes_read;
888 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
889 if (buf == NULL)
890 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891
Fred Drake0582df92000-07-12 04:49:00 +0000892 if (fp) {
893 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
894 if (bytes_read < 0) {
895 PyErr_SetFromErrno(PyExc_IOError);
896 return NULL;
897 }
898 }
899 else {
900 bytes_read = readinst(buf, BUF_SIZE, readmethod);
901 if (bytes_read < 0)
902 return NULL;
903 }
904 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
905 if (PyErr_Occurred())
906 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907
Fred Drake0582df92000-07-12 04:49:00 +0000908 if (!rv || bytes_read == 0)
909 break;
910 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000911 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000912}
913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000915"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000916Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000917
918static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000919xmlparse_SetBase(xmlparseobject *self, PyObject *args)
920{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000921 char *base;
922
Fred Drake0582df92000-07-12 04:49:00 +0000923 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000924 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000925 if (!XML_SetBase(self->itself, base)) {
926 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000927 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000928 Py_INCREF(Py_None);
929 return Py_None;
930}
931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000933"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000934Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000935
936static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000937xmlparse_GetBase(xmlparseobject *self, PyObject *args)
938{
939 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000940 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000941
Fred Drake0582df92000-07-12 04:49:00 +0000942 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000943}
944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000945PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000946"GetInputContext() -> string\n\
947Return the untranslated text of the input that caused the current event.\n\
948If 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 +0000949for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000950
951static PyObject *
952xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
953{
954 PyObject *result = NULL;
955
956 if (PyArg_ParseTuple(args, ":GetInputContext")) {
957 if (self->in_callback) {
958 int offset, size;
959 const char *buffer
960 = XML_GetInputContext(self->itself, &offset, &size);
961
962 if (buffer != NULL)
963 result = PyString_FromStringAndSize(buffer + offset, size);
964 else {
965 result = Py_None;
966 Py_INCREF(result);
967 }
968 }
969 else {
970 result = Py_None;
971 Py_INCREF(result);
972 }
973 }
974 return result;
975}
Fred Drakebd6101c2001-02-14 18:29:45 +0000976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000978"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000979Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000981
982static PyObject *
983xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
984{
985 char *context;
986 char *encoding = NULL;
987 xmlparseobject *new_parser;
988 int i;
989
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000990 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000991 &context, &encoding)) {
992 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000993 }
994
Martin v. Löwis894258c2001-09-23 10:20:10 +0000995#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +0000996 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000997 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000998#else
999 /* Python versions 2.2 and later */
1000 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1001#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001002
1003 if (new_parser == NULL)
1004 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001005 new_parser->buffer_size = self->buffer_size;
1006 new_parser->buffer_used = 0;
1007 if (self->buffer != NULL) {
1008 new_parser->buffer = malloc(new_parser->buffer_size);
1009 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001010#ifndef Py_TPFLAGS_HAVE_GC
1011 /* Code for versions 2.0 and 2.1 */
1012 PyObject_Del(new_parser);
1013#else
1014 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001015 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001016#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001017 return PyErr_NoMemory();
1018 }
1019 }
1020 else
1021 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001022 new_parser->returns_unicode = self->returns_unicode;
1023 new_parser->ordered_attributes = self->ordered_attributes;
1024 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001025 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001027 encoding);
1028 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001029 new_parser->intern = self->intern;
1030 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001031#ifdef Py_TPFLAGS_HAVE_GC
1032 PyObject_GC_Track(new_parser);
1033#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001034 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001035#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001036
1037 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001038 Py_DECREF(new_parser);
1039 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001040 }
1041
1042 XML_SetUserData(new_parser->itself, (void *)new_parser);
1043
1044 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001045 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001046 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001047
Fred Drake2a3d7db2002-06-28 22:56:48 +00001048 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001049 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001050 Py_DECREF(new_parser);
1051 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001052 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001053 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001054
1055 /* then copy handlers from self */
1056 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001057 PyObject *handler = self->handlers[i];
1058 if (handler != NULL) {
1059 Py_INCREF(handler);
1060 new_parser->handlers[i] = handler;
1061 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001062 handler_info[i].handler);
1063 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001064 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001065 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001066}
1067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001069"SetParamEntityParsing(flag) -> success\n\
1070Controls parsing of parameter entities (including the external DTD\n\
1071subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1072XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1073XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001075
1076static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001077xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001078{
Fred Drake85d835f2001-02-08 15:39:08 +00001079 int flag;
1080 if (!PyArg_ParseTuple(args, "i", &flag))
1081 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001082 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001083 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001084}
1085
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001086static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001087 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001088 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001089 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001090 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001091 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001092 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001093 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001094 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001095 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1096 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001097 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1098 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001099 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1100 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001101 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001102};
1103
1104/* ---------- */
1105
1106
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001107#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108
Fred Drake71b63ff2002-06-28 22:29:01 +00001109/* pyexpat international encoding support.
1110 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001111*/
1112
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001113static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001114PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001115
Fred Drake71b63ff2002-06-28 22:29:01 +00001116static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001117init_template_buffer(void)
1118{
1119 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001120 for (i = 0; i < 256; i++) {
1121 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001122 }
Fred Drakebb66a202001-03-01 20:48:17 +00001123 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001124}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001125
Fred Drake71b63ff2002-06-28 22:29:01 +00001126static int
1127PyUnknownEncodingHandler(void *encodingHandlerData,
1128 const XML_Char *name,
1129 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001130{
Fred Drakebb66a202001-03-01 20:48:17 +00001131 PyUnicodeObject *_u_string = NULL;
1132 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001133 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001134
Fred Drakebb66a202001-03-01 20:48:17 +00001135 /* Yes, supports only 8bit encodings */
1136 _u_string = (PyUnicodeObject *)
1137 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001138
Fred Drakebb66a202001-03-01 20:48:17 +00001139 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001140 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001141
Fred Drakebb66a202001-03-01 20:48:17 +00001142 for (i = 0; i < 256; i++) {
1143 /* Stupid to access directly, but fast */
1144 Py_UNICODE c = _u_string->str[i];
1145 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001146 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001147 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001148 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001149 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001150 info->data = NULL;
1151 info->convert = NULL;
1152 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001153 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001154 Py_DECREF(_u_string);
1155 return result;
1156}
1157
1158#endif
1159
1160static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001161newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001162{
1163 int i;
1164 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001165
Martin v. Löwis894258c2001-09-23 10:20:10 +00001166#ifdef Py_TPFLAGS_HAVE_GC
1167 /* Code for versions 2.2 and later */
1168 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1169#else
Fred Drake0582df92000-07-12 04:49:00 +00001170 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001171#endif
Fred Drake0582df92000-07-12 04:49:00 +00001172 if (self == NULL)
1173 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001174
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001175#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001176 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001177#else
1178 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001179#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001180
Fred Drake2a3d7db2002-06-28 22:56:48 +00001181 self->buffer = NULL;
1182 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1183 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001184 self->ordered_attributes = 0;
1185 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001186 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001187 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001188 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001189 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1190 }
Fred Drake85d835f2001-02-08 15:39:08 +00001191 else {
Fred Drake0582df92000-07-12 04:49:00 +00001192 self->itself = XML_ParserCreate(encoding);
1193 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001194 self->intern = intern;
1195 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001196#ifdef Py_TPFLAGS_HAVE_GC
1197 PyObject_GC_Track(self);
1198#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001199 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001200#endif
Fred Drake0582df92000-07-12 04:49:00 +00001201 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001202 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001203 "XML_ParserCreate failed");
1204 Py_DECREF(self);
1205 return NULL;
1206 }
1207 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001208#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001209 XML_SetUnknownEncodingHandler(self->itself,
1210 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001211#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001212
Fred Drake2a3d7db2002-06-28 22:56:48 +00001213 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001214 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001215
Fred Drake7c75bf22002-07-01 14:02:31 +00001216 self->handlers = malloc(sizeof(PyObject *) * i);
1217 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001218 Py_DECREF(self);
1219 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001220 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001221 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001222
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001223 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001224}
1225
1226
1227static void
Fred Drake0582df92000-07-12 04:49:00 +00001228xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001229{
Fred Drake0582df92000-07-12 04:49:00 +00001230 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001231#ifdef Py_TPFLAGS_HAVE_GC
1232 PyObject_GC_UnTrack(self);
1233#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001234 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001235#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001236 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001237 XML_ParserFree(self->itself);
1238 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001239
Fred Drake85d835f2001-02-08 15:39:08 +00001240 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001241 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001242 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001243 temp = self->handlers[i];
1244 self->handlers[i] = NULL;
1245 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001246 }
1247 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001248 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001249 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001250 if (self->buffer != NULL) {
1251 free(self->buffer);
1252 self->buffer = NULL;
1253 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001254 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001255#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001256 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001257 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001258#else
1259 /* Code for versions 2.2 and later. */
1260 PyObject_GC_Del(self);
1261#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001262}
1263
Fred Drake0582df92000-07-12 04:49:00 +00001264static int
1265handlername2int(const char *name)
1266{
1267 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001268 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001269 if (strcmp(name, handler_info[i].name) == 0) {
1270 return i;
1271 }
1272 }
1273 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001274}
1275
1276static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001277get_pybool(int istrue)
1278{
1279 PyObject *result = istrue ? Py_True : Py_False;
1280 Py_INCREF(result);
1281 return result;
1282}
1283
1284static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001285xmlparse_getattr(xmlparseobject *self, char *name)
1286{
Fred Drake71b63ff2002-06-28 22:29:01 +00001287 int handlernum = handlername2int(name);
1288
1289 if (handlernum != -1) {
1290 PyObject *result = self->handlers[handlernum];
1291 if (result == NULL)
1292 result = Py_None;
1293 Py_INCREF(result);
1294 return result;
1295 }
1296 if (name[0] == 'E') {
1297 if (strcmp(name, "ErrorCode") == 0)
1298 return PyInt_FromLong((long)
1299 XML_GetErrorCode(self->itself));
1300 if (strcmp(name, "ErrorLineNumber") == 0)
1301 return PyInt_FromLong((long)
1302 XML_GetErrorLineNumber(self->itself));
1303 if (strcmp(name, "ErrorColumnNumber") == 0)
1304 return PyInt_FromLong((long)
1305 XML_GetErrorColumnNumber(self->itself));
1306 if (strcmp(name, "ErrorByteIndex") == 0)
1307 return PyInt_FromLong((long)
1308 XML_GetErrorByteIndex(self->itself));
1309 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001310 if (name[0] == 'b') {
1311 if (strcmp(name, "buffer_size") == 0)
1312 return PyInt_FromLong((long) self->buffer_size);
1313 if (strcmp(name, "buffer_text") == 0)
1314 return get_pybool(self->buffer != NULL);
1315 if (strcmp(name, "buffer_used") == 0)
1316 return PyInt_FromLong((long) self->buffer_used);
1317 }
Fred Drake85d835f2001-02-08 15:39:08 +00001318 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001319 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001320 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001321 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001322 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001323 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001324 if (strcmp(name, "intern") == 0) {
1325 if (self->intern == NULL) {
1326 Py_INCREF(Py_None);
1327 return Py_None;
1328 }
1329 else {
1330 Py_INCREF(self->intern);
1331 return self->intern;
1332 }
1333 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001334
Fred Drake0582df92000-07-12 04:49:00 +00001335 if (strcmp(name, "__members__") == 0) {
1336 int i;
1337 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001338 for (i = 0; handler_info[i].name != NULL; i++) {
1339 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001340 }
1341 PyList_Append(rc, PyString_FromString("ErrorCode"));
1342 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1343 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1344 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake2a3d7db2002-06-28 22:56:48 +00001345 PyList_Append(rc, PyString_FromString("buffer_size"));
1346 PyList_Append(rc, PyString_FromString("buffer_text"));
1347 PyList_Append(rc, PyString_FromString("buffer_used"));
Fred Drake85d835f2001-02-08 15:39:08 +00001348 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001349 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001350 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001351 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001352
Fred Drake0582df92000-07-12 04:49:00 +00001353 return rc;
1354 }
1355 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001356}
1357
Fred Drake6f987622000-08-25 18:03:30 +00001358static int
1359sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001360{
1361 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001362 if (handlernum >= 0) {
1363 xmlhandler c_handler = NULL;
1364 PyObject *temp = self->handlers[handlernum];
1365
1366 if (v == Py_None)
1367 v = NULL;
1368 else if (v != NULL) {
1369 Py_INCREF(v);
1370 c_handler = handler_info[handlernum].handler;
1371 }
Fred Drake0582df92000-07-12 04:49:00 +00001372 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001373 Py_XDECREF(temp);
1374 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001375 return 1;
1376 }
1377 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001378}
1379
1380static int
Fred Drake6f987622000-08-25 18:03:30 +00001381xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001382{
Fred Drake6f987622000-08-25 18:03:30 +00001383 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001384 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001385 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1386 return -1;
1387 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001388 if (strcmp(name, "buffer_text") == 0) {
1389 if (PyObject_IsTrue(v)) {
1390 if (self->buffer == NULL) {
1391 self->buffer = malloc(self->buffer_size);
1392 if (self->buffer == NULL) {
1393 PyErr_NoMemory();
1394 return -1;
1395 }
1396 self->buffer_used = 0;
1397 }
1398 }
1399 else if (self->buffer != NULL) {
1400 if (flush_character_buffer(self) < 0)
1401 return -1;
1402 free(self->buffer);
1403 self->buffer = NULL;
1404 }
1405 return 0;
1406 }
Fred Drake85d835f2001-02-08 15:39:08 +00001407 if (strcmp(name, "ordered_attributes") == 0) {
1408 if (PyObject_IsTrue(v))
1409 self->ordered_attributes = 1;
1410 else
1411 self->ordered_attributes = 0;
1412 return 0;
1413 }
Fred Drake6f987622000-08-25 18:03:30 +00001414 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001415 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001416#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001417 PyErr_SetString(PyExc_ValueError,
1418 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001419 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001420#else
Fred Drake6f987622000-08-25 18:03:30 +00001421 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001422#endif
Fred Drake6f987622000-08-25 18:03:30 +00001423 }
1424 else
1425 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001426 return 0;
1427 }
1428 if (strcmp(name, "specified_attributes") == 0) {
1429 if (PyObject_IsTrue(v))
1430 self->specified_attributes = 1;
1431 else
1432 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001433 return 0;
1434 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001435 if (strcmp(name, "CharacterDataHandler") == 0) {
1436 /* If we're changing the character data handler, flush all
1437 * cached data with the old handler. Not sure there's a
1438 * "right" thing to do, though, but this probably won't
1439 * happen.
1440 */
1441 if (flush_character_buffer(self) < 0)
1442 return -1;
1443 }
Fred Drake6f987622000-08-25 18:03:30 +00001444 if (sethandler(self, name, v)) {
1445 return 0;
1446 }
1447 PyErr_SetString(PyExc_AttributeError, name);
1448 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001449}
1450
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001451#ifdef WITH_CYCLE_GC
1452static int
1453xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1454{
Fred Drakecde79132001-04-25 16:01:30 +00001455 int i, err;
1456 for (i = 0; handler_info[i].name != NULL; i++) {
1457 if (!op->handlers[i])
1458 continue;
1459 err = visit(op->handlers[i], arg);
1460 if (err)
1461 return err;
1462 }
1463 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001464}
1465
1466static int
1467xmlparse_clear(xmlparseobject *op)
1468{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001469 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001470 Py_XDECREF(op->intern);
1471 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001472 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001473}
1474#endif
1475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001476PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001477
1478static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001479 PyObject_HEAD_INIT(NULL)
1480 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001481 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001482 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001483 0, /*tp_itemsize*/
1484 /* methods */
1485 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1486 (printfunc)0, /*tp_print*/
1487 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1488 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1489 (cmpfunc)0, /*tp_compare*/
1490 (reprfunc)0, /*tp_repr*/
1491 0, /*tp_as_number*/
1492 0, /*tp_as_sequence*/
1493 0, /*tp_as_mapping*/
1494 (hashfunc)0, /*tp_hash*/
1495 (ternaryfunc)0, /*tp_call*/
1496 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001497 0, /* tp_getattro */
1498 0, /* tp_setattro */
1499 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001500#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001502#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001503 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001504#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001505 Xmlparsetype__doc__, /* Documentation string */
1506#ifdef WITH_CYCLE_GC
1507 (traverseproc)xmlparse_traverse, /* tp_traverse */
1508 (inquiry)xmlparse_clear /* tp_clear */
1509#else
1510 0, 0
1511#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001512};
1513
1514/* End of code for xmlparser objects */
1515/* -------------------------------------------------------- */
1516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001518"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001520
1521static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001522pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1523{
Fred Drakecde79132001-04-25 16:01:30 +00001524 char *encoding = NULL;
1525 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001526 PyObject *intern = NULL;
1527 PyObject *result;
1528 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001529 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001530 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001531
Fred Drakeb91a36b2002-06-27 19:40:48 +00001532 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1533 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001534 return NULL;
1535 if (namespace_separator != NULL
1536 && strlen(namespace_separator) > 1) {
1537 PyErr_SetString(PyExc_ValueError,
1538 "namespace_separator must be at most one"
1539 " character, omitted, or None");
1540 return NULL;
1541 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001542 /* Explicitly passing None means no interning is desired.
1543 Not passing anything means that a new dictionary is used. */
1544 if (intern == Py_None)
1545 intern = NULL;
1546 else if (intern == NULL) {
1547 intern = PyDict_New();
1548 if (!intern)
1549 return NULL;
1550 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001551 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001552 else if (!PyDict_Check(intern)) {
1553 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1554 return NULL;
1555 }
1556
1557 result = newxmlparseobject(encoding, namespace_separator, intern);
1558 if (intern_decref) {
1559 Py_DECREF(intern);
1560 }
1561 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001562}
1563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001565"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001567
1568static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001569pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570{
Fred Drake0582df92000-07-12 04:49:00 +00001571 long code = 0;
1572
1573 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1574 return NULL;
1575 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001576}
1577
1578/* List of methods defined in the module */
1579
1580static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001581 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1582 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1583 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1584 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001585
Fred Drake0582df92000-07-12 04:49:00 +00001586 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001587};
1588
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001589/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(pyexpat_module_documentation,
1592"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001593
Fred Drake4113b132001-03-24 19:58:26 +00001594/* Return a Python string that represents the version number without the
1595 * extra cruft added by revision control, even if the right options were
1596 * given to the "cvs export" command to make it not include the extra
1597 * cruft.
1598 */
1599static PyObject *
1600get_version_string(void)
1601{
1602 static char *rcsid = "$Revision$";
1603 char *rev = rcsid;
1604 int i = 0;
1605
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001606 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001607 ++rev;
1608 while (rev[i] != ' ' && rev[i] != '\0')
1609 ++i;
1610
1611 return PyString_FromStringAndSize(rev, i);
1612}
1613
Fred Drakecde79132001-04-25 16:01:30 +00001614/* Initialization function for the module */
1615
1616#ifndef MODULE_NAME
1617#define MODULE_NAME "pyexpat"
1618#endif
1619
1620#ifndef MODULE_INITFUNC
1621#define MODULE_INITFUNC initpyexpat
1622#endif
1623
Mark Hammond8235ea12002-07-19 06:55:41 +00001624PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001625
Mark Hammond8235ea12002-07-19 06:55:41 +00001626PyMODINIT_FUNC MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001627{
1628 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001629 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001630 PyObject *errors_module;
1631 PyObject *modelmod_name;
1632 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001633 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001634
Fred Drake6f987622000-08-25 18:03:30 +00001635 if (errmod_name == NULL)
1636 return;
Fred Drakecde79132001-04-25 16:01:30 +00001637 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001638 if (modelmod_name == NULL)
1639 return;
Fred Drake6f987622000-08-25 18:03:30 +00001640
Fred Drake0582df92000-07-12 04:49:00 +00001641 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001642
Fred Drake0582df92000-07-12 04:49:00 +00001643 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001644 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001645 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001646
Fred Drake0582df92000-07-12 04:49:00 +00001647 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001648 if (ErrorObject == NULL) {
1649 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001650 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001651 if (ErrorObject == NULL)
1652 return;
1653 }
1654 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001655 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001656 Py_INCREF(ErrorObject);
1657 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001658 Py_INCREF(&Xmlparsetype);
1659 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001660
Fred Drake4113b132001-03-24 19:58:26 +00001661 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001662 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1663 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001664 {
1665 XML_Expat_Version info = XML_ExpatVersionInfo();
1666 PyModule_AddObject(m, "version_info",
1667 Py_BuildValue("(iii)", info.major,
1668 info.minor, info.micro));
1669 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001670#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001671 init_template_buffer();
1672#endif
Fred Drake0582df92000-07-12 04:49:00 +00001673 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001674 compiled, this should check and set native_encoding
1675 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001676 */
Fred Drake93adb692000-09-23 04:55:48 +00001677 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001678
Fred Drake85d835f2001-02-08 15:39:08 +00001679 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001680 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001681 errors_module = PyDict_GetItem(d, errmod_name);
1682 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001683 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001684 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001685 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001686 /* gives away the reference to errors_module */
1687 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001688 }
1689 }
Fred Drake6f987622000-08-25 18:03:30 +00001690 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001691 model_module = PyDict_GetItem(d, modelmod_name);
1692 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001693 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001694 if (model_module != NULL) {
1695 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1696 /* gives away the reference to model_module */
1697 PyModule_AddObject(m, "model", model_module);
1698 }
1699 }
1700 Py_DECREF(modelmod_name);
1701 if (errors_module == NULL || model_module == NULL)
1702 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001703 return;
1704
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001705#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001706 PyModule_AddStringConstant(errors_module, #name, \
1707 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001708
Fred Drake0582df92000-07-12 04:49:00 +00001709 MYCONST(XML_ERROR_NO_MEMORY);
1710 MYCONST(XML_ERROR_SYNTAX);
1711 MYCONST(XML_ERROR_NO_ELEMENTS);
1712 MYCONST(XML_ERROR_INVALID_TOKEN);
1713 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1714 MYCONST(XML_ERROR_PARTIAL_CHAR);
1715 MYCONST(XML_ERROR_TAG_MISMATCH);
1716 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1717 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1718 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1719 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1720 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1721 MYCONST(XML_ERROR_ASYNC_ENTITY);
1722 MYCONST(XML_ERROR_BAD_CHAR_REF);
1723 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1724 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1725 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1726 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1727 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001728 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1729 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1730 MYCONST(XML_ERROR_NOT_STANDALONE);
1731
Fred Drake85d835f2001-02-08 15:39:08 +00001732 PyModule_AddStringConstant(errors_module, "__doc__",
1733 "Constants used to describe error conditions.");
1734
Fred Drake93adb692000-09-23 04:55:48 +00001735#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001736
Fred Drake85d835f2001-02-08 15:39:08 +00001737#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001738 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1739 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1740 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001741#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001742
Fred Drake85d835f2001-02-08 15:39:08 +00001743#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1744 PyModule_AddStringConstant(model_module, "__doc__",
1745 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001746
Fred Drake85d835f2001-02-08 15:39:08 +00001747 MYCONST(XML_CTYPE_EMPTY);
1748 MYCONST(XML_CTYPE_ANY);
1749 MYCONST(XML_CTYPE_MIXED);
1750 MYCONST(XML_CTYPE_NAME);
1751 MYCONST(XML_CTYPE_CHOICE);
1752 MYCONST(XML_CTYPE_SEQ);
1753
1754 MYCONST(XML_CQUANT_NONE);
1755 MYCONST(XML_CQUANT_OPT);
1756 MYCONST(XML_CQUANT_REP);
1757 MYCONST(XML_CQUANT_PLUS);
1758#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001759}
1760
Fred Drake6f987622000-08-25 18:03:30 +00001761static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001762clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001763{
Fred Drakecde79132001-04-25 16:01:30 +00001764 int i = 0;
1765 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001766
Fred Drake71b63ff2002-06-28 22:29:01 +00001767 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001768 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001769 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001770 else {
Fred Drakecde79132001-04-25 16:01:30 +00001771 temp = self->handlers[i];
1772 self->handlers[i] = NULL;
1773 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001774 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001775 }
Fred Drakecde79132001-04-25 16:01:30 +00001776 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001777}
1778
Tim Peters0c322792002-07-17 16:49:03 +00001779static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001780 {"StartElementHandler",
1781 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001782 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001783 {"EndElementHandler",
1784 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001785 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001786 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001787 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1788 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001789 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001790 (xmlhandlersetter)XML_SetCharacterDataHandler,
1791 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001792 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001793 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001794 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001795 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001796 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001797 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001798 {"StartNamespaceDeclHandler",
1799 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001800 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001801 {"EndNamespaceDeclHandler",
1802 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001803 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001804 {"CommentHandler",
1805 (xmlhandlersetter)XML_SetCommentHandler,
1806 (xmlhandler)my_CommentHandler},
1807 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001808 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001809 (xmlhandler)my_StartCdataSectionHandler},
1810 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001811 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001812 (xmlhandler)my_EndCdataSectionHandler},
1813 {"DefaultHandler",
1814 (xmlhandlersetter)XML_SetDefaultHandler,
1815 (xmlhandler)my_DefaultHandler},
1816 {"DefaultHandlerExpand",
1817 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1818 (xmlhandler)my_DefaultHandlerExpandHandler},
1819 {"NotStandaloneHandler",
1820 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1821 (xmlhandler)my_NotStandaloneHandler},
1822 {"ExternalEntityRefHandler",
1823 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001824 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001825 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001826 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001827 (xmlhandler)my_StartDoctypeDeclHandler},
1828 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001829 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001830 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001831 {"EntityDeclHandler",
1832 (xmlhandlersetter)XML_SetEntityDeclHandler,
1833 (xmlhandler)my_EntityDeclHandler},
1834 {"XmlDeclHandler",
1835 (xmlhandlersetter)XML_SetXmlDeclHandler,
1836 (xmlhandler)my_XmlDeclHandler},
1837 {"ElementDeclHandler",
1838 (xmlhandlersetter)XML_SetElementDeclHandler,
1839 (xmlhandler)my_ElementDeclHandler},
1840 {"AttlistDeclHandler",
1841 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1842 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001843
Fred Drake0582df92000-07-12 04:49:00 +00001844 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001845};