blob: e1dbf2f0276d4c05da3e22fc4f43aba35f0927fa [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake8188e792001-11-18 02:36:07 +00002#if PY_VERSION_HEX < 0x020000B1
3#include <assert.h>
4#endif
Fred Drake4113b132001-03-24 19:58:26 +00005#include <ctype.h>
6
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007#include "compile.h"
8#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00009#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010
11#ifndef PyGC_HEAD_SIZE
12#define PyGC_HEAD_SIZE 0
13#define PyObject_GC_Init(x)
14#define PyObject_GC_Fini(m)
15#define Py_TPFLAGS_GC 0
16#endif
17
Martin v. Löwis339d0f72001-08-17 18:39:25 +000018#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
19/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
20#define Py_USING_UNICODE
21#endif
22
Fred Drake0582df92000-07-12 04:49:00 +000023enum HandlerTypes {
24 StartElement,
25 EndElement,
26 ProcessingInstruction,
27 CharacterData,
28 UnparsedEntityDecl,
29 NotationDecl,
30 StartNamespaceDecl,
31 EndNamespaceDecl,
32 Comment,
33 StartCdataSection,
34 EndCdataSection,
35 Default,
36 DefaultHandlerExpand,
37 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000038 ExternalEntityRef,
39 StartDoctypeDecl,
40 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000041 EntityDecl,
42 XmlDecl,
43 ElementDecl,
44 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000045 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000046};
47
48static PyObject *ErrorObject;
49
50/* ----------------------------------------------------- */
51
52/* Declarations for objects of type xmlparser */
53
54typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000055 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056
Fred Drake0582df92000-07-12 04:49:00 +000057 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000058 int returns_unicode; /* True if Unicode strings are returned;
59 if false, UTF-8 strings are returned */
60 int ordered_attributes; /* Return attributes as a list. */
61 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000062 int in_callback; /* Is a callback active? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000063 XML_Char *buffer; /* Buffer used when accumulating characters */
64 /* NULL if not enabled */
65 int buffer_size; /* Size of buffer, in XML_Char units */
66 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000067 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000068 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000069} xmlparseobject;
70
Fred Drake2a3d7db2002-06-28 22:56:48 +000071#define CHARACTER_DATA_BUFFER_SIZE 8192
72
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000073staticforward PyTypeObject Xmlparsetype;
74
Fred Drake6f987622000-08-25 18:03:30 +000075typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076typedef void* xmlhandler;
77
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000078struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000079 const char *name;
80 xmlhandlersetter setter;
81 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000082 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000083 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000084};
85
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000086staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000087
Fred Drakebd6101c2001-02-14 18:29:45 +000088/* Set an integer attribute on the error object; return true on success,
89 * false on an exception.
90 */
91static int
92set_error_attr(PyObject *err, char *name, int value)
93{
94 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000095
Fred Drakebd6101c2001-02-14 18:29:45 +000096 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
97 Py_DECREF(v);
98 return 0;
99 }
100 return 1;
101}
102
103/* Build and set an Expat exception, including positioning
104 * information. Always returns NULL.
105 */
Fred Drake85d835f2001-02-08 15:39:08 +0000106static PyObject *
107set_error(xmlparseobject *self)
108{
109 PyObject *err;
110 char buffer[256];
111 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000112 int lineno = XML_GetErrorLineNumber(parser);
113 int column = XML_GetErrorColumnNumber(parser);
114 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000115
Tim Peters885d4572001-11-28 20:27:42 +0000116 PyOS_snprintf(buffer, sizeof(buffer), "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000117 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000118 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000119 if ( err != NULL
120 && set_error_attr(err, "code", code)
121 && set_error_attr(err, "offset", column)
122 && set_error_attr(err, "lineno", lineno)) {
123 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000124 }
125 return NULL;
126}
127
Fred Drake71b63ff2002-06-28 22:29:01 +0000128static int
129have_handler(xmlparseobject *self, int type)
130{
131 PyObject *handler = self->handlers[type];
132 return handler != NULL;
133}
134
135static PyObject *
136get_handler_name(struct HandlerInfo *hinfo)
137{
138 PyObject *name = hinfo->nameobj;
139 if (name == NULL) {
140 name = PyString_FromString(hinfo->name);
141 hinfo->nameobj = name;
142 }
143 Py_XINCREF(name);
144 return name;
145}
146
Fred Drake85d835f2001-02-08 15:39:08 +0000147
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000148#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000149/* Convert a string of XML_Chars into a Unicode string.
150 Returns None if str is a null pointer. */
151
Fred Drake0582df92000-07-12 04:49:00 +0000152static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000153conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000154{
Fred Drake71b63ff2002-06-28 22:29:01 +0000155 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000156 and hence in UTF-8. */
157 /* UTF-8 from Expat, Unicode desired */
158 if (str == NULL) {
159 Py_INCREF(Py_None);
160 return Py_None;
161 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000162 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000163}
164
Fred Drake0582df92000-07-12 04:49:00 +0000165static PyObject *
166conv_string_len_to_unicode(const XML_Char *str, int len)
167{
Fred Drake71b63ff2002-06-28 22:29:01 +0000168 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000169 and hence in UTF-8. */
170 /* UTF-8 from Expat, Unicode desired */
171 if (str == NULL) {
172 Py_INCREF(Py_None);
173 return Py_None;
174 }
Fred Drake6f987622000-08-25 18:03:30 +0000175 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000176}
177#endif
178
179/* Convert a string of XML_Chars into an 8-bit Python string.
180 Returns None if str is a null pointer. */
181
Fred Drake6f987622000-08-25 18:03:30 +0000182static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000183conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000184{
Fred Drake71b63ff2002-06-28 22:29:01 +0000185 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000186 and hence in UTF-8. */
187 /* UTF-8 from Expat, UTF-8 desired */
188 if (str == NULL) {
189 Py_INCREF(Py_None);
190 return Py_None;
191 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000192 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000193}
194
Fred Drake6f987622000-08-25 18:03:30 +0000195static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000196conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000197{
Fred Drake71b63ff2002-06-28 22:29:01 +0000198 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000199 and hence in UTF-8. */
200 /* UTF-8 from Expat, UTF-8 desired */
201 if (str == NULL) {
202 Py_INCREF(Py_None);
203 return Py_None;
204 }
205 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000206}
207
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000208/* Callback routines */
209
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000210static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000211
Fred Drake6f987622000-08-25 18:03:30 +0000212static void
213flag_error(xmlparseobject *self)
214{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000215 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000216}
217
218static PyCodeObject*
219getcode(enum HandlerTypes slot, char* func_name, int lineno)
220{
Fred Drakebd6101c2001-02-14 18:29:45 +0000221 PyObject *code = NULL;
222 PyObject *name = NULL;
223 PyObject *nulltuple = NULL;
224 PyObject *filename = NULL;
225
226 if (handler_info[slot].tb_code == NULL) {
227 code = PyString_FromString("");
228 if (code == NULL)
229 goto failed;
230 name = PyString_FromString(func_name);
231 if (name == NULL)
232 goto failed;
233 nulltuple = PyTuple_New(0);
234 if (nulltuple == NULL)
235 goto failed;
236 filename = PyString_FromString(__FILE__);
237 handler_info[slot].tb_code =
238 PyCode_New(0, /* argcount */
239 0, /* nlocals */
240 0, /* stacksize */
241 0, /* flags */
242 code, /* code */
243 nulltuple, /* consts */
244 nulltuple, /* names */
245 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000246#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000247 nulltuple, /* freevars */
248 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000249#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000250 filename, /* filename */
251 name, /* name */
252 lineno, /* firstlineno */
253 code /* lnotab */
254 );
255 if (handler_info[slot].tb_code == NULL)
256 goto failed;
257 Py_DECREF(code);
258 Py_DECREF(nulltuple);
259 Py_DECREF(filename);
260 Py_DECREF(name);
261 }
262 return handler_info[slot].tb_code;
263 failed:
264 Py_XDECREF(code);
265 Py_XDECREF(name);
266 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000267}
268
269static PyObject*
270call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
271{
Fred Drakebd6101c2001-02-14 18:29:45 +0000272 PyThreadState *tstate = PyThreadState_GET();
273 PyFrameObject *f;
274 PyObject *res;
275
276 if (c == NULL)
277 return NULL;
278 f = PyFrame_New(
279 tstate, /*back*/
280 c, /*code*/
281 tstate->frame->f_globals, /*globals*/
282 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000283 );
284 if (f == NULL)
285 return NULL;
286 tstate->frame = f;
287 res = PyEval_CallObject(func, args);
288 if (res == NULL && tstate->curexc_traceback == NULL)
289 PyTraceBack_Here(f);
290 tstate->frame = f->f_back;
291 Py_DECREF(f);
292 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000293}
294
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000295#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000296#define STRING_CONV_FUNC conv_string_to_utf8
297#else
298/* Python 1.6 and later versions */
299#define STRING_CONV_FUNC (self->returns_unicode \
300 ? conv_string_to_unicode : conv_string_to_utf8)
301#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000302
Fred Drakeb91a36b2002-06-27 19:40:48 +0000303static PyObject*
304string_intern(xmlparseobject *self, const char* str)
305{
306 PyObject *result = STRING_CONV_FUNC(str);
307 PyObject *value;
308 if (!self->intern)
309 return result;
310 value = PyDict_GetItem(self->intern, result);
311 if (!value) {
312 if (PyDict_SetItem(self->intern, result, result) == 0)
313 return result;
314 else
315 return NULL;
316 }
317 Py_INCREF(value);
318 Py_DECREF(result);
319 return value;
320}
321
Fred Drake2a3d7db2002-06-28 22:56:48 +0000322/* Return 0 on success, -1 on exception.
323 * flag_error() will be called before return if needed.
324 */
325static int
326call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
327{
328 PyObject *args;
329 PyObject *temp;
330
331 args = PyTuple_New(1);
332 if (args == NULL)
333 return -1;
334#ifdef Py_USING_UNICODE
335 temp = (self->returns_unicode
336 ? conv_string_len_to_unicode(buffer, len)
337 : conv_string_len_to_utf8(buffer, len));
338#else
339 temp = conv_string_len_to_utf8(buffer, len);
340#endif
341 if (temp == NULL) {
342 Py_DECREF(args);
343 flag_error(self);
344 return -1;
345 }
346 PyTuple_SET_ITEM(args, 0, temp);
347 /* temp is now a borrowed reference; consider it unused. */
348 self->in_callback = 1;
349 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
350 self->handlers[CharacterData], args);
351 /* temp is an owned reference again, or NULL */
352 self->in_callback = 0;
353 Py_DECREF(args);
354 if (temp == NULL) {
355 flag_error(self);
356 return -1;
357 }
358 Py_DECREF(temp);
359 return 0;
360}
361
362static int
363flush_character_buffer(xmlparseobject *self)
364{
365 int rc;
366 if (self->buffer == NULL || self->buffer_used == 0)
367 return 0;
368 rc = call_character_handler(self, self->buffer, self->buffer_used);
369 self->buffer_used = 0;
370 return rc;
371}
372
373static void
374my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
375{
376 xmlparseobject *self = (xmlparseobject *) userData;
377 if (self->buffer == NULL)
378 call_character_handler(self, data, len);
379 else {
380 if ((self->buffer_used + len) > self->buffer_size) {
381 if (flush_character_buffer(self) < 0)
382 return;
383 /* handler might have changed; drop the rest on the floor
384 * if there isn't a handler anymore
385 */
386 if (!have_handler(self, CharacterData))
387 return;
388 }
389 if (len > self->buffer_size) {
390 call_character_handler(self, data, len);
391 self->buffer_used = 0;
392 }
393 else {
394 memcpy(self->buffer + self->buffer_used,
395 data, len * sizeof(XML_Char));
396 self->buffer_used += len;
397 }
398 }
399}
400
Fred Drake85d835f2001-02-08 15:39:08 +0000401static void
402my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000403 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000404{
405 xmlparseobject *self = (xmlparseobject *)userData;
406
Fred Drake71b63ff2002-06-28 22:29:01 +0000407 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000408 PyObject *container, *rv, *args;
409 int i, max;
410
Fred Drake2a3d7db2002-06-28 22:56:48 +0000411 if (flush_character_buffer(self) < 0)
412 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000413 /* Set max to the number of slots filled in atts[]; max/2 is
414 * the number of attributes we need to process.
415 */
416 if (self->specified_attributes) {
417 max = XML_GetSpecifiedAttributeCount(self->itself);
418 }
419 else {
420 max = 0;
421 while (atts[max] != NULL)
422 max += 2;
423 }
424 /* Build the container. */
425 if (self->ordered_attributes)
426 container = PyList_New(max);
427 else
428 container = PyDict_New();
429 if (container == NULL) {
430 flag_error(self);
431 return;
432 }
433 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000434 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000435 PyObject *v;
436 if (n == NULL) {
437 flag_error(self);
438 Py_DECREF(container);
439 return;
440 }
441 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
442 if (v == NULL) {
443 flag_error(self);
444 Py_DECREF(container);
445 Py_DECREF(n);
446 return;
447 }
448 if (self->ordered_attributes) {
449 PyList_SET_ITEM(container, i, n);
450 PyList_SET_ITEM(container, i+1, v);
451 }
452 else if (PyDict_SetItem(container, n, v)) {
453 flag_error(self);
454 Py_DECREF(n);
455 Py_DECREF(v);
456 return;
457 }
458 else {
459 Py_DECREF(n);
460 Py_DECREF(v);
461 }
462 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000463 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000464 if (args == NULL) {
465 Py_DECREF(container);
466 return;
467 }
468 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000469 self->in_callback = 1;
470 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000471 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000472 self->in_callback = 0;
473 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000474 if (rv == NULL) {
475 flag_error(self);
476 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000477 }
Fred Drake85d835f2001-02-08 15:39:08 +0000478 Py_DECREF(rv);
479 }
480}
481
482#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
483 RETURN, GETUSERDATA) \
484static RC \
485my_##NAME##Handler PARAMS {\
486 xmlparseobject *self = GETUSERDATA ; \
487 PyObject *args = NULL; \
488 PyObject *rv = NULL; \
489 INIT \
490\
Fred Drake71b63ff2002-06-28 22:29:01 +0000491 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000492 if (flush_character_buffer(self) < 0) \
493 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000494 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000495 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000496 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000497 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
498 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000499 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000500 Py_DECREF(args); \
501 if (rv == NULL) { \
502 flag_error(self); \
503 return RETURN; \
504 } \
505 CONVERSION \
506 Py_DECREF(rv); \
507 } \
508 return RETURN; \
509}
510
Fred Drake6f987622000-08-25 18:03:30 +0000511#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
512 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
513 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000514
Fred Drake6f987622000-08-25 18:03:30 +0000515#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
516 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
517 rc = PyInt_AsLong(rv);, rc, \
518 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000519
Fred Drake71b63ff2002-06-28 22:29:01 +0000520VOID_HANDLER(EndElement,
521 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000522 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000523
Fred Drake6f987622000-08-25 18:03:30 +0000524VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000525 (void *userData,
526 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000527 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000528 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000529
Fred Drake6f987622000-08-25 18:03:30 +0000530VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000531 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000532 const XML_Char *entityName,
533 const XML_Char *base,
534 const XML_Char *systemId,
535 const XML_Char *publicId,
536 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000537 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000538 string_intern(self, entityName), string_intern(self, base),
539 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000540 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000541
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000542#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000543VOID_HANDLER(EntityDecl,
544 (void *userData,
545 const XML_Char *entityName,
546 int is_parameter_entity,
547 const XML_Char *value,
548 int value_length,
549 const XML_Char *base,
550 const XML_Char *systemId,
551 const XML_Char *publicId,
552 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000553 ("NiNNNNN",
554 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000555 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000556 string_intern(self, base), string_intern(self, systemId),
557 string_intern(self, publicId),
558 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000559#else
560VOID_HANDLER(EntityDecl,
561 (void *userData,
562 const XML_Char *entityName,
563 int is_parameter_entity,
564 const XML_Char *value,
565 int value_length,
566 const XML_Char *base,
567 const XML_Char *systemId,
568 const XML_Char *publicId,
569 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000570 ("NiNNNNN",
571 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000572 (self->returns_unicode
573 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000574 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000575 string_intern(self, base), string_intern(self, systemId),
576 string_intern(self, publicId),
577 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000578#endif
579
580VOID_HANDLER(XmlDecl,
581 (void *userData,
582 const XML_Char *version,
583 const XML_Char *encoding,
584 int standalone),
585 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000586 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000587 standalone))
588
589static PyObject *
590conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000591 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000592{
593 PyObject *result = NULL;
594 PyObject *children = PyTuple_New(model->numchildren);
595 int i;
596
597 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000598 assert(model->numchildren < INT_MAX);
599 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000600 PyObject *child = conv_content_model(&model->children[i],
601 conv_string);
602 if (child == NULL) {
603 Py_XDECREF(children);
604 return NULL;
605 }
606 PyTuple_SET_ITEM(children, i, child);
607 }
608 result = Py_BuildValue("(iiO&N)",
609 model->type, model->quant,
610 conv_string,model->name, children);
611 }
612 return result;
613}
614
615static PyObject *
616conv_content_model_utf8(XML_Content * const model)
617{
618 return conv_content_model(model, conv_string_to_utf8);
619}
620
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000621#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000622static PyObject *
623conv_content_model_unicode(XML_Content * const model)
624{
625 return conv_content_model(model, conv_string_to_unicode);
626}
627
628VOID_HANDLER(ElementDecl,
629 (void *userData,
630 const XML_Char *name,
631 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000632 ("NO&",
633 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000634 (self->returns_unicode ? conv_content_model_unicode
635 : conv_content_model_utf8),model))
636#else
637VOID_HANDLER(ElementDecl,
638 (void *userData,
639 const XML_Char *name,
640 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000641 ("NO&",
642 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000643#endif
644
645VOID_HANDLER(AttlistDecl,
646 (void *userData,
647 const XML_Char *elname,
648 const XML_Char *attname,
649 const XML_Char *att_type,
650 const XML_Char *dflt,
651 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000652 ("(NNO&O&i)",
653 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000654 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
655 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656
Fred Drake71b63ff2002-06-28 22:29:01 +0000657VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000658 (void *userData,
659 const XML_Char *notationName,
660 const XML_Char *base,
661 const XML_Char *systemId,
662 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000663 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000664 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000665 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000666
Fred Drake6f987622000-08-25 18:03:30 +0000667VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000668 (void *userData,
669 const XML_Char *prefix,
670 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000671 ("(NN)",
672 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000673
Fred Drake6f987622000-08-25 18:03:30 +0000674VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000675 (void *userData,
676 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000677 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678
Fred Drake6f987622000-08-25 18:03:30 +0000679VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000680 (void *userData, const XML_Char *data),
681 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682
Fred Drake6f987622000-08-25 18:03:30 +0000683VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000684 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000685 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000686
Fred Drake6f987622000-08-25 18:03:30 +0000687VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000688 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000689 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000690
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000691#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000692VOID_HANDLER(Default,
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. Kuchlingb7f10532000-03-31 15:43:31 +0000695
Fred Drake6f987622000-08-25 18:03:30 +0000696VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000697 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000698 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000699#else
Fred Drake6f987622000-08-25 18:03:30 +0000700VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000701 (void *userData, const XML_Char *s, int len),
702 ("(N)", (self->returns_unicode
703 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000704 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000705
Fred Drake6f987622000-08-25 18:03:30 +0000706VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000707 (void *userData, const XML_Char *s, int len),
708 ("(N)", (self->returns_unicode
709 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000710 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000711#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000712
Fred Drake71b63ff2002-06-28 22:29:01 +0000713INT_HANDLER(NotStandalone,
714 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000715 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000716
Fred Drake6f987622000-08-25 18:03:30 +0000717RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000718 (XML_Parser parser,
719 const XML_Char *context,
720 const XML_Char *base,
721 const XML_Char *systemId,
722 const XML_Char *publicId),
723 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000724 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000725 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000726 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000727 rc = PyInt_AsLong(rv);, rc,
728 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000729
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000730/* XXX UnknownEncodingHandler */
731
Fred Drake85d835f2001-02-08 15:39:08 +0000732VOID_HANDLER(StartDoctypeDecl,
733 (void *userData, const XML_Char *doctypeName,
734 const XML_Char *sysid, const XML_Char *pubid,
735 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000736 ("(NNNi)", string_intern(self, doctypeName),
737 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000738 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000739
740VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000741
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000742/* ---------------------------------------------------------------- */
743
Fred Drake71b63ff2002-06-28 22:29:01 +0000744static PyObject *
745get_parse_result(xmlparseobject *self, int rv)
746{
747 if (PyErr_Occurred()) {
748 return NULL;
749 }
750 if (rv == 0) {
751 return set_error(self);
752 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000753 if (flush_character_buffer(self) < 0) {
754 return NULL;
755 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000756 return PyInt_FromLong(rv);
757}
758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000760"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000761Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762
763static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000764xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000765{
Fred Drake0582df92000-07-12 04:49:00 +0000766 char *s;
767 int slen;
768 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000769
Fred Drake0582df92000-07-12 04:49:00 +0000770 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
771 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000772
773 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000774}
775
Fred Drakeca1f4262000-09-21 20:10:23 +0000776/* File reading copied from cPickle */
777
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000778#define BUF_SIZE 2048
779
Fred Drake0582df92000-07-12 04:49:00 +0000780static int
781readinst(char *buf, int buf_size, PyObject *meth)
782{
783 PyObject *arg = NULL;
784 PyObject *bytes = NULL;
785 PyObject *str = NULL;
786 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000787
Fred Drake676940b2000-09-22 15:21:31 +0000788 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000789 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000790
Fred Drakeca1f4262000-09-21 20:10:23 +0000791 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000792 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000793
Tim Peters954eef72000-09-22 06:01:11 +0000794 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000795
Fred Drakeca1f4262000-09-21 20:10:23 +0000796 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000797 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000798
Fred Drake0582df92000-07-12 04:49:00 +0000799 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000800 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000801 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000802 "read() did not return a string object (type=%.400s)",
803 str->ob_type->tp_name);
804 goto finally;
805 }
806 len = PyString_GET_SIZE(str);
807 if (len > buf_size) {
808 PyErr_Format(PyExc_ValueError,
809 "read() returned too much data: "
810 "%i bytes requested, %i returned",
811 buf_size, len);
812 Py_DECREF(str);
813 goto finally;
814 }
815 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000816finally:
Fred Drake0582df92000-07-12 04:49:00 +0000817 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000818 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000819 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000820}
821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000823"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000824Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825
826static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000827xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828{
Fred Drake0582df92000-07-12 04:49:00 +0000829 int rv = 1;
830 PyObject *f;
831 FILE *fp;
832 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833
Fred Drake0582df92000-07-12 04:49:00 +0000834 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
835 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836
Fred Drake0582df92000-07-12 04:49:00 +0000837 if (PyFile_Check(f)) {
838 fp = PyFile_AsFile(f);
839 }
840 else{
841 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000842 readmethod = PyObject_GetAttrString(f, "read");
843 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000844 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000845 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000846 "argument must have 'read' attribute");
847 return 0;
848 }
849 }
850 for (;;) {
851 int bytes_read;
852 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
853 if (buf == NULL)
854 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000855
Fred Drake0582df92000-07-12 04:49:00 +0000856 if (fp) {
857 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
858 if (bytes_read < 0) {
859 PyErr_SetFromErrno(PyExc_IOError);
860 return NULL;
861 }
862 }
863 else {
864 bytes_read = readinst(buf, BUF_SIZE, readmethod);
865 if (bytes_read < 0)
866 return NULL;
867 }
868 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
869 if (PyErr_Occurred())
870 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000871
Fred Drake0582df92000-07-12 04:49:00 +0000872 if (!rv || bytes_read == 0)
873 break;
874 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000875 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000876}
877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000878PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000879"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000881
882static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000883xmlparse_SetBase(xmlparseobject *self, PyObject *args)
884{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000885 char *base;
886
Fred Drake0582df92000-07-12 04:49:00 +0000887 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000888 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000889 if (!XML_SetBase(self->itself, base)) {
890 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892 Py_INCREF(Py_None);
893 return Py_None;
894}
895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000897"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000898Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899
900static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000901xmlparse_GetBase(xmlparseobject *self, PyObject *args)
902{
903 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000904 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000905
Fred Drake0582df92000-07-12 04:49:00 +0000906 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907}
908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000910"GetInputContext() -> string\n\
911Return the untranslated text of the input that caused the current event.\n\
912If 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 +0000913for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000914
915static PyObject *
916xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
917{
918 PyObject *result = NULL;
919
920 if (PyArg_ParseTuple(args, ":GetInputContext")) {
921 if (self->in_callback) {
922 int offset, size;
923 const char *buffer
924 = XML_GetInputContext(self->itself, &offset, &size);
925
926 if (buffer != NULL)
927 result = PyString_FromStringAndSize(buffer + offset, size);
928 else {
929 result = Py_None;
930 Py_INCREF(result);
931 }
932 }
933 else {
934 result = Py_None;
935 Py_INCREF(result);
936 }
937 }
938 return result;
939}
Fred Drakebd6101c2001-02-14 18:29:45 +0000940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000941PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000942"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000943Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000944information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000945
946static PyObject *
947xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
948{
949 char *context;
950 char *encoding = NULL;
951 xmlparseobject *new_parser;
952 int i;
953
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000954 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000955 &context, &encoding)) {
956 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000957 }
958
959#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
960 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000961#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000962#ifndef Py_TPFLAGS_HAVE_GC
963 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000964 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000965#else
966 /* Python versions 2.2 and later */
967 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
968#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000969#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000970
971 if (new_parser == NULL)
972 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000973 new_parser->buffer_size = self->buffer_size;
974 new_parser->buffer_used = 0;
975 if (self->buffer != NULL) {
976 new_parser->buffer = malloc(new_parser->buffer_size);
977 if (new_parser->buffer == NULL) {
978 PyObject_GC_Del(new_parser);
979 return PyErr_NoMemory();
980 }
981 }
982 else
983 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000984 new_parser->returns_unicode = self->returns_unicode;
985 new_parser->ordered_attributes = self->ordered_attributes;
986 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000987 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000988 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000989 encoding);
990 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000991 new_parser->intern = self->intern;
992 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000993#ifdef Py_TPFLAGS_HAVE_GC
994 PyObject_GC_Track(new_parser);
995#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000996 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000997#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000998
999 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001000 Py_DECREF(new_parser);
1001 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001002 }
1003
1004 XML_SetUserData(new_parser->itself, (void *)new_parser);
1005
1006 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001007 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001008 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001009
Fred Drake2a3d7db2002-06-28 22:56:48 +00001010 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001011 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001012 Py_DECREF(new_parser);
1013 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001014 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001015 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016
1017 /* then copy handlers from self */
1018 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001019 PyObject *handler = self->handlers[i];
1020 if (handler != NULL) {
1021 Py_INCREF(handler);
1022 new_parser->handlers[i] = handler;
1023 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001024 handler_info[i].handler);
1025 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001027 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001028}
1029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001030PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001031"SetParamEntityParsing(flag) -> success\n\
1032Controls parsing of parameter entities (including the external DTD\n\
1033subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1034XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1035XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001037
1038static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001039xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001040{
Fred Drake85d835f2001-02-08 15:39:08 +00001041 int flag;
1042 if (!PyArg_ParseTuple(args, "i", &flag))
1043 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001044 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001045 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001046}
1047
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001048static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001049 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001051 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001052 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001053 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001054 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001055 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001056 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001057 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1058 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001059 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1060 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001061 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1062 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001063 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001064};
1065
1066/* ---------- */
1067
1068
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001069#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001070
Fred Drake71b63ff2002-06-28 22:29:01 +00001071/* pyexpat international encoding support.
1072 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073*/
1074
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001075static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001076PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077
Fred Drake71b63ff2002-06-28 22:29:01 +00001078static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001079init_template_buffer(void)
1080{
1081 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001082 for (i = 0; i < 256; i++) {
1083 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001084 }
Fred Drakebb66a202001-03-01 20:48:17 +00001085 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001086}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001087
Fred Drake71b63ff2002-06-28 22:29:01 +00001088static int
1089PyUnknownEncodingHandler(void *encodingHandlerData,
1090 const XML_Char *name,
1091 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092{
Fred Drakebb66a202001-03-01 20:48:17 +00001093 PyUnicodeObject *_u_string = NULL;
1094 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001095 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001096
Fred Drakebb66a202001-03-01 20:48:17 +00001097 /* Yes, supports only 8bit encodings */
1098 _u_string = (PyUnicodeObject *)
1099 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001100
Fred Drakebb66a202001-03-01 20:48:17 +00001101 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001103
Fred Drakebb66a202001-03-01 20:48:17 +00001104 for (i = 0; i < 256; i++) {
1105 /* Stupid to access directly, but fast */
1106 Py_UNICODE c = _u_string->str[i];
1107 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001109 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001111 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001112 info->data = NULL;
1113 info->convert = NULL;
1114 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001115 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001116 Py_DECREF(_u_string);
1117 return result;
1118}
1119
1120#endif
1121
1122static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001123newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001124{
1125 int i;
1126 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001127
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001128#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001129 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1130 if (self == NULL)
1131 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001132
Fred Drake0582df92000-07-12 04:49:00 +00001133 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001134#else
Fred Drake0582df92000-07-12 04:49:00 +00001135 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001136#ifdef Py_TPFLAGS_HAVE_GC
1137 /* Code for versions 2.2 and later */
1138 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1139#else
Fred Drake0582df92000-07-12 04:49:00 +00001140 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001141#endif
Fred Drake0582df92000-07-12 04:49:00 +00001142 if (self == NULL)
1143 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001144
Fred Drake0582df92000-07-12 04:49:00 +00001145 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001146#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001147 self->buffer = NULL;
1148 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1149 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001150 self->ordered_attributes = 0;
1151 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001152 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001153 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001154 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001155 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1156 }
Fred Drake85d835f2001-02-08 15:39:08 +00001157 else {
Fred Drake0582df92000-07-12 04:49:00 +00001158 self->itself = XML_ParserCreate(encoding);
1159 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001160 self->intern = intern;
1161 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001162#ifdef Py_TPFLAGS_HAVE_GC
1163 PyObject_GC_Track(self);
1164#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001165 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001166#endif
Fred Drake0582df92000-07-12 04:49:00 +00001167 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001168 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001169 "XML_ParserCreate failed");
1170 Py_DECREF(self);
1171 return NULL;
1172 }
1173 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001174#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001175 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1176#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001177
Fred Drake2a3d7db2002-06-28 22:56:48 +00001178 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001179 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001180
Fred Drake0582df92000-07-12 04:49:00 +00001181 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001182 if (!self->handlers){
Fred Drake71b63ff2002-06-28 22:29:01 +00001183 Py_DECREF(self);
1184 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001185 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001186 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001187
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001188 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189}
1190
1191
1192static void
Fred Drake0582df92000-07-12 04:49:00 +00001193xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001194{
Fred Drake0582df92000-07-12 04:49:00 +00001195 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001196#ifdef Py_TPFLAGS_HAVE_GC
1197 PyObject_GC_UnTrack(self);
1198#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001199 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001200#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001201 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001202 XML_ParserFree(self->itself);
1203 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001204
Fred Drake85d835f2001-02-08 15:39:08 +00001205 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001206 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001207 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001208 temp = self->handlers[i];
1209 self->handlers[i] = NULL;
1210 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001211 }
1212 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001213 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001214 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001215 if (self->buffer != NULL) {
1216 free(self->buffer);
1217 self->buffer = NULL;
1218 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001219 Py_XDECREF(self->intern);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001220#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001221 /* Code for versions before 1.6 */
1222 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001223#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001224#ifndef Py_TPFLAGS_HAVE_GC
1225 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001226 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001227#else
1228 /* Code for versions 2.2 and later. */
1229 PyObject_GC_Del(self);
1230#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001231#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001232}
1233
Fred Drake0582df92000-07-12 04:49:00 +00001234static int
1235handlername2int(const char *name)
1236{
1237 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001238 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001239 if (strcmp(name, handler_info[i].name) == 0) {
1240 return i;
1241 }
1242 }
1243 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001244}
1245
1246static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001247get_pybool(int istrue)
1248{
1249 PyObject *result = istrue ? Py_True : Py_False;
1250 Py_INCREF(result);
1251 return result;
1252}
1253
1254static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001255xmlparse_getattr(xmlparseobject *self, char *name)
1256{
Fred Drake71b63ff2002-06-28 22:29:01 +00001257 int handlernum = handlername2int(name);
1258
1259 if (handlernum != -1) {
1260 PyObject *result = self->handlers[handlernum];
1261 if (result == NULL)
1262 result = Py_None;
1263 Py_INCREF(result);
1264 return result;
1265 }
1266 if (name[0] == 'E') {
1267 if (strcmp(name, "ErrorCode") == 0)
1268 return PyInt_FromLong((long)
1269 XML_GetErrorCode(self->itself));
1270 if (strcmp(name, "ErrorLineNumber") == 0)
1271 return PyInt_FromLong((long)
1272 XML_GetErrorLineNumber(self->itself));
1273 if (strcmp(name, "ErrorColumnNumber") == 0)
1274 return PyInt_FromLong((long)
1275 XML_GetErrorColumnNumber(self->itself));
1276 if (strcmp(name, "ErrorByteIndex") == 0)
1277 return PyInt_FromLong((long)
1278 XML_GetErrorByteIndex(self->itself));
1279 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001280 if (name[0] == 'b') {
1281 if (strcmp(name, "buffer_size") == 0)
1282 return PyInt_FromLong((long) self->buffer_size);
1283 if (strcmp(name, "buffer_text") == 0)
1284 return get_pybool(self->buffer != NULL);
1285 if (strcmp(name, "buffer_used") == 0)
1286 return PyInt_FromLong((long) self->buffer_used);
1287 }
Fred Drake85d835f2001-02-08 15:39:08 +00001288 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001289 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001290 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001291 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001292 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001293 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001294 if (strcmp(name, "intern") == 0) {
1295 if (self->intern == NULL) {
1296 Py_INCREF(Py_None);
1297 return Py_None;
1298 }
1299 else {
1300 Py_INCREF(self->intern);
1301 return self->intern;
1302 }
1303 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001304
Fred Drake0582df92000-07-12 04:49:00 +00001305 if (strcmp(name, "__members__") == 0) {
1306 int i;
1307 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001308 for (i = 0; handler_info[i].name != NULL; i++) {
1309 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001310 }
1311 PyList_Append(rc, PyString_FromString("ErrorCode"));
1312 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1313 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1314 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake2a3d7db2002-06-28 22:56:48 +00001315 PyList_Append(rc, PyString_FromString("buffer_size"));
1316 PyList_Append(rc, PyString_FromString("buffer_text"));
1317 PyList_Append(rc, PyString_FromString("buffer_used"));
Fred Drake85d835f2001-02-08 15:39:08 +00001318 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001319 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001320 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001321 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001322
Fred Drake0582df92000-07-12 04:49:00 +00001323 return rc;
1324 }
1325 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001326}
1327
Fred Drake6f987622000-08-25 18:03:30 +00001328static int
1329sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001330{
1331 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001332 if (handlernum >= 0) {
1333 xmlhandler c_handler = NULL;
1334 PyObject *temp = self->handlers[handlernum];
1335
1336 if (v == Py_None)
1337 v = NULL;
1338 else if (v != NULL) {
1339 Py_INCREF(v);
1340 c_handler = handler_info[handlernum].handler;
1341 }
Fred Drake0582df92000-07-12 04:49:00 +00001342 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001343 Py_XDECREF(temp);
1344 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001345 return 1;
1346 }
1347 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001348}
1349
1350static int
Fred Drake6f987622000-08-25 18:03:30 +00001351xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001352{
Fred Drake6f987622000-08-25 18:03:30 +00001353 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001354 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001355 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1356 return -1;
1357 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001358 if (strcmp(name, "buffer_text") == 0) {
1359 if (PyObject_IsTrue(v)) {
1360 if (self->buffer == NULL) {
1361 self->buffer = malloc(self->buffer_size);
1362 if (self->buffer == NULL) {
1363 PyErr_NoMemory();
1364 return -1;
1365 }
1366 self->buffer_used = 0;
1367 }
1368 }
1369 else if (self->buffer != NULL) {
1370 if (flush_character_buffer(self) < 0)
1371 return -1;
1372 free(self->buffer);
1373 self->buffer = NULL;
1374 }
1375 return 0;
1376 }
Fred Drake85d835f2001-02-08 15:39:08 +00001377 if (strcmp(name, "ordered_attributes") == 0) {
1378 if (PyObject_IsTrue(v))
1379 self->ordered_attributes = 1;
1380 else
1381 self->ordered_attributes = 0;
1382 return 0;
1383 }
Fred Drake6f987622000-08-25 18:03:30 +00001384 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001385 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001386#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001387 PyErr_SetString(PyExc_ValueError,
1388 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001389 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001390#else
Fred Drake6f987622000-08-25 18:03:30 +00001391 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001392#endif
Fred Drake6f987622000-08-25 18:03:30 +00001393 }
1394 else
1395 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001396 return 0;
1397 }
1398 if (strcmp(name, "specified_attributes") == 0) {
1399 if (PyObject_IsTrue(v))
1400 self->specified_attributes = 1;
1401 else
1402 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001403 return 0;
1404 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001405 if (strcmp(name, "CharacterDataHandler") == 0) {
1406 /* If we're changing the character data handler, flush all
1407 * cached data with the old handler. Not sure there's a
1408 * "right" thing to do, though, but this probably won't
1409 * happen.
1410 */
1411 if (flush_character_buffer(self) < 0)
1412 return -1;
1413 }
Fred Drake6f987622000-08-25 18:03:30 +00001414 if (sethandler(self, name, v)) {
1415 return 0;
1416 }
1417 PyErr_SetString(PyExc_AttributeError, name);
1418 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001419}
1420
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001421#ifdef WITH_CYCLE_GC
1422static int
1423xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1424{
Fred Drakecde79132001-04-25 16:01:30 +00001425 int i, err;
1426 for (i = 0; handler_info[i].name != NULL; i++) {
1427 if (!op->handlers[i])
1428 continue;
1429 err = visit(op->handlers[i], arg);
1430 if (err)
1431 return err;
1432 }
1433 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001434}
1435
1436static int
1437xmlparse_clear(xmlparseobject *op)
1438{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001439 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001440 Py_XDECREF(op->intern);
1441 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001442 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001443}
1444#endif
1445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001447
1448static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001449 PyObject_HEAD_INIT(NULL)
1450 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001451 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001452 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001453 0, /*tp_itemsize*/
1454 /* methods */
1455 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1456 (printfunc)0, /*tp_print*/
1457 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1458 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1459 (cmpfunc)0, /*tp_compare*/
1460 (reprfunc)0, /*tp_repr*/
1461 0, /*tp_as_number*/
1462 0, /*tp_as_sequence*/
1463 0, /*tp_as_mapping*/
1464 (hashfunc)0, /*tp_hash*/
1465 (ternaryfunc)0, /*tp_call*/
1466 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001467 0, /* tp_getattro */
1468 0, /* tp_setattro */
1469 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001470#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001471 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001472#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001473 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001474#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001475 Xmlparsetype__doc__, /* Documentation string */
1476#ifdef WITH_CYCLE_GC
1477 (traverseproc)xmlparse_traverse, /* tp_traverse */
1478 (inquiry)xmlparse_clear /* tp_clear */
1479#else
1480 0, 0
1481#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001482};
1483
1484/* End of code for xmlparser objects */
1485/* -------------------------------------------------------- */
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001488"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001490
1491static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001492pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1493{
Fred Drakecde79132001-04-25 16:01:30 +00001494 char *encoding = NULL;
1495 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001496 PyObject *intern = NULL;
1497 PyObject *result;
1498 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001499 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001500 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001501
Fred Drakeb91a36b2002-06-27 19:40:48 +00001502 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1503 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001504 return NULL;
1505 if (namespace_separator != NULL
1506 && strlen(namespace_separator) > 1) {
1507 PyErr_SetString(PyExc_ValueError,
1508 "namespace_separator must be at most one"
1509 " character, omitted, or None");
1510 return NULL;
1511 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001512 /* Explicitly passing None means no interning is desired.
1513 Not passing anything means that a new dictionary is used. */
1514 if (intern == Py_None)
1515 intern = NULL;
1516 else if (intern == NULL) {
1517 intern = PyDict_New();
1518 if (!intern)
1519 return NULL;
1520 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001521 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001522 else if (!PyDict_Check(intern)) {
1523 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1524 return NULL;
1525 }
1526
1527 result = newxmlparseobject(encoding, namespace_separator, intern);
1528 if (intern_decref) {
1529 Py_DECREF(intern);
1530 }
1531 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001532}
1533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001535"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001537
1538static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001539pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001540{
Fred Drake0582df92000-07-12 04:49:00 +00001541 long code = 0;
1542
1543 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1544 return NULL;
1545 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001546}
1547
1548/* List of methods defined in the module */
1549
1550static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001551 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1552 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1553 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1554 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001555
Fred Drake0582df92000-07-12 04:49:00 +00001556 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001557};
1558
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001559/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(pyexpat_module_documentation,
1562"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001563
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001564#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001565
1566/* 1.5 compatibility: PyModule_AddObject */
1567static int
1568PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1569{
Fred Drakecde79132001-04-25 16:01:30 +00001570 PyObject *dict;
1571 if (!PyModule_Check(m) || o == NULL)
1572 return -1;
1573 dict = PyModule_GetDict(m);
1574 if (dict == NULL)
1575 return -1;
1576 if (PyDict_SetItemString(dict, name, o))
1577 return -1;
1578 Py_DECREF(o);
1579 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001580}
1581
Fred Drake71b63ff2002-06-28 22:29:01 +00001582static int
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001583PyModule_AddIntConstant(PyObject *m, char *name, long value)
1584{
Fred Drakecde79132001-04-25 16:01:30 +00001585 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001586}
1587
Fred Drake71b63ff2002-06-28 22:29:01 +00001588static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001589PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1590{
Fred Drakecde79132001-04-25 16:01:30 +00001591 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001592}
1593
1594#endif
1595
Fred Drake4113b132001-03-24 19:58:26 +00001596
1597/* Return a Python string that represents the version number without the
1598 * extra cruft added by revision control, even if the right options were
1599 * given to the "cvs export" command to make it not include the extra
1600 * cruft.
1601 */
1602static PyObject *
1603get_version_string(void)
1604{
1605 static char *rcsid = "$Revision$";
1606 char *rev = rcsid;
1607 int i = 0;
1608
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001609 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001610 ++rev;
1611 while (rev[i] != ' ' && rev[i] != '\0')
1612 ++i;
1613
1614 return PyString_FromStringAndSize(rev, i);
1615}
1616
Fred Drakecde79132001-04-25 16:01:30 +00001617/* Initialization function for the module */
1618
1619#ifndef MODULE_NAME
1620#define MODULE_NAME "pyexpat"
1621#endif
1622
1623#ifndef MODULE_INITFUNC
1624#define MODULE_INITFUNC initpyexpat
1625#endif
1626
1627void MODULE_INITFUNC(void); /* avoid compiler warnings */
1628
Fred Drake6f987622000-08-25 18:03:30 +00001629DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001630MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001631{
1632 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001633 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001634 PyObject *errors_module;
1635 PyObject *modelmod_name;
1636 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001637 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001638
Fred Drake6f987622000-08-25 18:03:30 +00001639 if (errmod_name == NULL)
1640 return;
Fred Drakecde79132001-04-25 16:01:30 +00001641 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001642 if (modelmod_name == NULL)
1643 return;
Fred Drake6f987622000-08-25 18:03:30 +00001644
Fred Drake0582df92000-07-12 04:49:00 +00001645 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001646
Fred Drake0582df92000-07-12 04:49:00 +00001647 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001648 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001649 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001650
Fred Drake0582df92000-07-12 04:49:00 +00001651 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001652 if (ErrorObject == NULL) {
1653 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001654 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001655 if (ErrorObject == NULL)
1656 return;
1657 }
1658 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001659 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001660 Py_INCREF(ErrorObject);
1661 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001662 Py_INCREF(&Xmlparsetype);
1663 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001664
Fred Drake4113b132001-03-24 19:58:26 +00001665 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001666 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1667 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001668 {
1669 XML_Expat_Version info = XML_ExpatVersionInfo();
1670 PyModule_AddObject(m, "version_info",
1671 Py_BuildValue("(iii)", info.major,
1672 info.minor, info.micro));
1673 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001674#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001675 init_template_buffer();
1676#endif
Fred Drake0582df92000-07-12 04:49:00 +00001677 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001678 compiled, this should check and set native_encoding
1679 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001680 */
Fred Drake93adb692000-09-23 04:55:48 +00001681 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001682
Fred Drake85d835f2001-02-08 15:39:08 +00001683 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001684 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001685 errors_module = PyDict_GetItem(d, errmod_name);
1686 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001687 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001688 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001689 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001690 /* gives away the reference to errors_module */
1691 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001692 }
1693 }
Fred Drake6f987622000-08-25 18:03:30 +00001694 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001695 model_module = PyDict_GetItem(d, modelmod_name);
1696 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001697 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001698 if (model_module != NULL) {
1699 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1700 /* gives away the reference to model_module */
1701 PyModule_AddObject(m, "model", model_module);
1702 }
1703 }
1704 Py_DECREF(modelmod_name);
1705 if (errors_module == NULL || model_module == NULL)
1706 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001707 return;
1708
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001709#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001710 PyModule_AddStringConstant(errors_module, #name, \
1711 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001712
Fred Drake0582df92000-07-12 04:49:00 +00001713 MYCONST(XML_ERROR_NO_MEMORY);
1714 MYCONST(XML_ERROR_SYNTAX);
1715 MYCONST(XML_ERROR_NO_ELEMENTS);
1716 MYCONST(XML_ERROR_INVALID_TOKEN);
1717 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1718 MYCONST(XML_ERROR_PARTIAL_CHAR);
1719 MYCONST(XML_ERROR_TAG_MISMATCH);
1720 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1721 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1722 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1723 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1724 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1725 MYCONST(XML_ERROR_ASYNC_ENTITY);
1726 MYCONST(XML_ERROR_BAD_CHAR_REF);
1727 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1728 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1729 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1730 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1731 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001732 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1733 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1734 MYCONST(XML_ERROR_NOT_STANDALONE);
1735
Fred Drake85d835f2001-02-08 15:39:08 +00001736 PyModule_AddStringConstant(errors_module, "__doc__",
1737 "Constants used to describe error conditions.");
1738
Fred Drake93adb692000-09-23 04:55:48 +00001739#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001740
Fred Drake85d835f2001-02-08 15:39:08 +00001741#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001742 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1743 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1744 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001745#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001746
Fred Drake85d835f2001-02-08 15:39:08 +00001747#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1748 PyModule_AddStringConstant(model_module, "__doc__",
1749 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001750
Fred Drake85d835f2001-02-08 15:39:08 +00001751 MYCONST(XML_CTYPE_EMPTY);
1752 MYCONST(XML_CTYPE_ANY);
1753 MYCONST(XML_CTYPE_MIXED);
1754 MYCONST(XML_CTYPE_NAME);
1755 MYCONST(XML_CTYPE_CHOICE);
1756 MYCONST(XML_CTYPE_SEQ);
1757
1758 MYCONST(XML_CQUANT_NONE);
1759 MYCONST(XML_CQUANT_OPT);
1760 MYCONST(XML_CQUANT_REP);
1761 MYCONST(XML_CQUANT_PLUS);
1762#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001763}
1764
Fred Drake6f987622000-08-25 18:03:30 +00001765static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001766clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001767{
Fred Drakecde79132001-04-25 16:01:30 +00001768 int i = 0;
1769 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001770
Fred Drake71b63ff2002-06-28 22:29:01 +00001771 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001772 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001773 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001774 else {
Fred Drakecde79132001-04-25 16:01:30 +00001775 temp = self->handlers[i];
1776 self->handlers[i] = NULL;
1777 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001778 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001779 }
Fred Drakecde79132001-04-25 16:01:30 +00001780 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001781}
1782
Fred Drake0582df92000-07-12 04:49:00 +00001783statichere struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001784 {"StartElementHandler",
1785 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001786 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001787 {"EndElementHandler",
1788 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001789 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001790 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001791 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1792 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001793 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001794 (xmlhandlersetter)XML_SetCharacterDataHandler,
1795 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001796 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001797 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001798 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001799 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001800 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001801 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001802 {"StartNamespaceDeclHandler",
1803 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001804 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001805 {"EndNamespaceDeclHandler",
1806 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001807 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001808 {"CommentHandler",
1809 (xmlhandlersetter)XML_SetCommentHandler,
1810 (xmlhandler)my_CommentHandler},
1811 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001812 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001813 (xmlhandler)my_StartCdataSectionHandler},
1814 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001815 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001816 (xmlhandler)my_EndCdataSectionHandler},
1817 {"DefaultHandler",
1818 (xmlhandlersetter)XML_SetDefaultHandler,
1819 (xmlhandler)my_DefaultHandler},
1820 {"DefaultHandlerExpand",
1821 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1822 (xmlhandler)my_DefaultHandlerExpandHandler},
1823 {"NotStandaloneHandler",
1824 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1825 (xmlhandler)my_NotStandaloneHandler},
1826 {"ExternalEntityRefHandler",
1827 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001828 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001829 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001830 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001831 (xmlhandler)my_StartDoctypeDeclHandler},
1832 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001833 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001834 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001835 {"EntityDeclHandler",
1836 (xmlhandlersetter)XML_SetEntityDeclHandler,
1837 (xmlhandler)my_EntityDeclHandler},
1838 {"XmlDeclHandler",
1839 (xmlhandlersetter)XML_SetXmlDeclHandler,
1840 (xmlhandler)my_XmlDeclHandler},
1841 {"ElementDeclHandler",
1842 (xmlhandlersetter)XML_SetElementDeclHandler,
1843 (xmlhandler)my_ElementDeclHandler},
1844 {"AttlistDeclHandler",
1845 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1846 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001847
Fred Drake0582df92000-07-12 04:49:00 +00001848 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001849};