blob: 3ea022a778d648c4f3ad3f51588e132ee4184ccb [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#ifdef HAVE_EXPAT_H
10#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000011#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +000012#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
13 + 0x100 * XML_MINOR_VERSION \
14 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000015#else
Fred Drake85d835f2001-02-08 15:39:08 +000016/* Assume the oldest Expat that used expat.h and did not have version info */
17#define EXPAT_VERSION 0x015f00
18#endif
19#else /* !defined(HAVE_EXPAT_H) */
20#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000021/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000022#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000023#define EXPAT_VERSION 0x010100
24#endif
Fred Drake85d835f2001-02-08 15:39:08 +000025#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000026
27#ifndef PyGC_HEAD_SIZE
28#define PyGC_HEAD_SIZE 0
29#define PyObject_GC_Init(x)
30#define PyObject_GC_Fini(m)
31#define Py_TPFLAGS_GC 0
32#endif
33
Martin v. Löwis339d0f72001-08-17 18:39:25 +000034#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
35/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
36#define Py_USING_UNICODE
37#endif
38
Fred Drake0582df92000-07-12 04:49:00 +000039enum HandlerTypes {
40 StartElement,
41 EndElement,
42 ProcessingInstruction,
43 CharacterData,
44 UnparsedEntityDecl,
45 NotationDecl,
46 StartNamespaceDecl,
47 EndNamespaceDecl,
48 Comment,
49 StartCdataSection,
50 EndCdataSection,
51 Default,
52 DefaultHandlerExpand,
53 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000054 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000055#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000056 StartDoctypeDecl,
57 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000058#endif
59#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000060 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000061 InternalParsedEntityDecl,
62#endif
63#if EXPAT_VERSION >= 0x015f00
64 EntityDecl,
65 XmlDecl,
66 ElementDecl,
67 AttlistDecl,
68#endif
69 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070};
71
72static PyObject *ErrorObject;
73
74/* ----------------------------------------------------- */
75
76/* Declarations for objects of type xmlparser */
77
78typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000079 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080
Fred Drake0582df92000-07-12 04:49:00 +000081 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000082 int returns_unicode; /* True if Unicode strings are returned;
83 if false, UTF-8 strings are returned */
84 int ordered_attributes; /* Return attributes as a list. */
85 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000086 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000087 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000088} xmlparseobject;
89
90staticforward PyTypeObject Xmlparsetype;
91
Fred Drake6f987622000-08-25 18:03:30 +000092typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000093typedef void* xmlhandler;
94
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000095struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000096 const char *name;
97 xmlhandlersetter setter;
98 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000099 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000100};
101
Andrew M. Kuchling637f6642000-07-04 14:53:43 +0000102staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000103
Fred Drakebd6101c2001-02-14 18:29:45 +0000104/* Set an integer attribute on the error object; return true on success,
105 * false on an exception.
106 */
107static int
108set_error_attr(PyObject *err, char *name, int value)
109{
110 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000111
Fred Drakebd6101c2001-02-14 18:29:45 +0000112 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
113 Py_DECREF(v);
114 return 0;
115 }
116 return 1;
117}
118
119/* Build and set an Expat exception, including positioning
120 * information. Always returns NULL.
121 */
Fred Drake85d835f2001-02-08 15:39:08 +0000122static PyObject *
123set_error(xmlparseobject *self)
124{
125 PyObject *err;
126 char buffer[256];
127 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000128 int lineno = XML_GetErrorLineNumber(parser);
129 int column = XML_GetErrorColumnNumber(parser);
130 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000131
Tim Peters885d4572001-11-28 20:27:42 +0000132 PyOS_snprintf(buffer, sizeof(buffer), "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000133 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000134 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000135 if ( err != NULL
136 && set_error_attr(err, "code", code)
137 && set_error_attr(err, "offset", column)
138 && set_error_attr(err, "lineno", lineno)) {
139 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000140 }
141 return NULL;
142}
143
144
145#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000146/* Convert an array of attributes and their values into a Python dict */
147
Fred Drake0582df92000-07-12 04:49:00 +0000148static PyObject *
149conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000150{
Fred Drake0582df92000-07-12 04:49:00 +0000151 PyObject *attrs_obj = NULL;
152 XML_Char **attrs_p, **attrs_k = NULL;
153 int attrs_len;
154 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000155
Fred Drake0582df92000-07-12 04:49:00 +0000156 if ((attrs_obj = PyDict_New()) == NULL)
157 goto finally;
158 for (attrs_len = 0, attrs_p = atts;
159 *attrs_p;
160 attrs_p++, attrs_len++) {
161 if (attrs_len % 2) {
162 rv = PyString_FromString(*attrs_p);
163 if (!rv) {
164 Py_DECREF(attrs_obj);
165 attrs_obj = NULL;
166 goto finally;
167 }
168 if (PyDict_SetItemString(attrs_obj,
169 (char*)*attrs_k, rv) < 0) {
170 Py_DECREF(attrs_obj);
171 attrs_obj = NULL;
172 goto finally;
173 }
174 Py_DECREF(rv);
175 }
176 else
177 attrs_k = attrs_p;
178 }
179 finally:
180 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000181}
Fred Drake85d835f2001-02-08 15:39:08 +0000182#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000183
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000184#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000185#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000186static PyObject *
187conv_atts_using_unicode(XML_Char **atts)
188{
Fred Drakeca1f4262000-09-21 20:10:23 +0000189 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000190 XML_Char **attrs_p, **attrs_k = NULL;
191 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000192
Fred Drake0582df92000-07-12 04:49:00 +0000193 if ((attrs_obj = PyDict_New()) == NULL)
194 goto finally;
195 for (attrs_len = 0, attrs_p = atts;
196 *attrs_p;
197 attrs_p++, attrs_len++) {
198 if (attrs_len % 2) {
199 PyObject *attr_str, *value_str;
200 const char *p = (const char *) (*attrs_k);
201 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
202 if (!attr_str) {
203 Py_DECREF(attrs_obj);
204 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000205 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000206 }
207 p = (const char *) *attrs_p;
208 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
209 if (!value_str) {
210 Py_DECREF(attrs_obj);
211 Py_DECREF(attr_str);
212 attrs_obj = NULL;
213 goto finally;
214 }
215 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
216 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000217 Py_DECREF(attr_str);
218 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000219 attrs_obj = NULL;
220 goto finally;
221 }
222 Py_DECREF(attr_str);
223 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000224 }
Fred Drake0582df92000-07-12 04:49:00 +0000225 else
226 attrs_k = attrs_p;
227 }
228 finally:
229 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000230}
Fred Drake85d835f2001-02-08 15:39:08 +0000231#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000232
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000233/* Convert a string of XML_Chars into a Unicode string.
234 Returns None if str is a null pointer. */
235
Fred Drake0582df92000-07-12 04:49:00 +0000236static PyObject *
237conv_string_to_unicode(XML_Char *str)
238{
239 /* XXX currently this code assumes that XML_Char is 8-bit,
240 and hence in UTF-8. */
241 /* UTF-8 from Expat, Unicode desired */
242 if (str == NULL) {
243 Py_INCREF(Py_None);
244 return Py_None;
245 }
246 return PyUnicode_DecodeUTF8((const char *)str,
247 strlen((const char *)str),
248 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000249}
250
Fred Drake0582df92000-07-12 04:49:00 +0000251static PyObject *
252conv_string_len_to_unicode(const XML_Char *str, int len)
253{
254 /* XXX currently this code assumes that XML_Char is 8-bit,
255 and hence in UTF-8. */
256 /* UTF-8 from Expat, Unicode desired */
257 if (str == NULL) {
258 Py_INCREF(Py_None);
259 return Py_None;
260 }
Fred Drake6f987622000-08-25 18:03:30 +0000261 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000262}
263#endif
264
265/* Convert a string of XML_Chars into an 8-bit Python string.
266 Returns None if str is a null pointer. */
267
Fred Drake6f987622000-08-25 18:03:30 +0000268static PyObject *
269conv_string_to_utf8(XML_Char *str)
270{
271 /* XXX currently this code assumes that XML_Char is 8-bit,
272 and hence in UTF-8. */
273 /* UTF-8 from Expat, UTF-8 desired */
274 if (str == NULL) {
275 Py_INCREF(Py_None);
276 return Py_None;
277 }
278 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000279}
280
Fred Drake6f987622000-08-25 18:03:30 +0000281static PyObject *
282conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000283{
Fred Drake6f987622000-08-25 18:03:30 +0000284 /* XXX currently this code assumes that XML_Char is 8-bit,
285 and hence in UTF-8. */
286 /* UTF-8 from Expat, UTF-8 desired */
287 if (str == NULL) {
288 Py_INCREF(Py_None);
289 return Py_None;
290 }
291 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000292}
293
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000294/* Callback routines */
295
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000296static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000297
Fred Drake6f987622000-08-25 18:03:30 +0000298static void
299flag_error(xmlparseobject *self)
300{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000301 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000302}
303
304static PyCodeObject*
305getcode(enum HandlerTypes slot, char* func_name, int lineno)
306{
Fred Drakebd6101c2001-02-14 18:29:45 +0000307 PyObject *code = NULL;
308 PyObject *name = NULL;
309 PyObject *nulltuple = NULL;
310 PyObject *filename = NULL;
311
312 if (handler_info[slot].tb_code == NULL) {
313 code = PyString_FromString("");
314 if (code == NULL)
315 goto failed;
316 name = PyString_FromString(func_name);
317 if (name == NULL)
318 goto failed;
319 nulltuple = PyTuple_New(0);
320 if (nulltuple == NULL)
321 goto failed;
322 filename = PyString_FromString(__FILE__);
323 handler_info[slot].tb_code =
324 PyCode_New(0, /* argcount */
325 0, /* nlocals */
326 0, /* stacksize */
327 0, /* flags */
328 code, /* code */
329 nulltuple, /* consts */
330 nulltuple, /* names */
331 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000332#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000333 nulltuple, /* freevars */
334 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000335#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000336 filename, /* filename */
337 name, /* name */
338 lineno, /* firstlineno */
339 code /* lnotab */
340 );
341 if (handler_info[slot].tb_code == NULL)
342 goto failed;
343 Py_DECREF(code);
344 Py_DECREF(nulltuple);
345 Py_DECREF(filename);
346 Py_DECREF(name);
347 }
348 return handler_info[slot].tb_code;
349 failed:
350 Py_XDECREF(code);
351 Py_XDECREF(name);
352 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000353}
354
355static PyObject*
356call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
357{
Fred Drakebd6101c2001-02-14 18:29:45 +0000358 PyThreadState *tstate = PyThreadState_GET();
359 PyFrameObject *f;
360 PyObject *res;
361
362 if (c == NULL)
363 return NULL;
364 f = PyFrame_New(
365 tstate, /*back*/
366 c, /*code*/
367 tstate->frame->f_globals, /*globals*/
368 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000369 );
370 if (f == NULL)
371 return NULL;
372 tstate->frame = f;
373 res = PyEval_CallObject(func, args);
374 if (res == NULL && tstate->curexc_traceback == NULL)
375 PyTraceBack_Here(f);
376 tstate->frame = f->f_back;
377 Py_DECREF(f);
378 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000379}
380
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000381#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000382#define STRING_CONV_FUNC conv_string_to_utf8
383#else
384/* Python 1.6 and later versions */
385#define STRING_CONV_FUNC (self->returns_unicode \
386 ? conv_string_to_unicode : conv_string_to_utf8)
387#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000388
Fred Drake85d835f2001-02-08 15:39:08 +0000389static void
390my_StartElementHandler(void *userData,
391 const XML_Char *name, const XML_Char **atts)
392{
393 xmlparseobject *self = (xmlparseobject *)userData;
394
395 if (self->handlers[StartElement]
396 && self->handlers[StartElement] != Py_None) {
397 PyObject *container, *rv, *args;
398 int i, max;
399
400 /* Set max to the number of slots filled in atts[]; max/2 is
401 * the number of attributes we need to process.
402 */
403 if (self->specified_attributes) {
404 max = XML_GetSpecifiedAttributeCount(self->itself);
405 }
406 else {
407 max = 0;
408 while (atts[max] != NULL)
409 max += 2;
410 }
411 /* Build the container. */
412 if (self->ordered_attributes)
413 container = PyList_New(max);
414 else
415 container = PyDict_New();
416 if (container == NULL) {
417 flag_error(self);
418 return;
419 }
420 for (i = 0; i < max; i += 2) {
421 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
422 PyObject *v;
423 if (n == NULL) {
424 flag_error(self);
425 Py_DECREF(container);
426 return;
427 }
428 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
429 if (v == NULL) {
430 flag_error(self);
431 Py_DECREF(container);
432 Py_DECREF(n);
433 return;
434 }
435 if (self->ordered_attributes) {
436 PyList_SET_ITEM(container, i, n);
437 PyList_SET_ITEM(container, i+1, v);
438 }
439 else if (PyDict_SetItem(container, n, v)) {
440 flag_error(self);
441 Py_DECREF(n);
442 Py_DECREF(v);
443 return;
444 }
445 else {
446 Py_DECREF(n);
447 Py_DECREF(v);
448 }
449 }
450 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
451 if (args == NULL) {
452 Py_DECREF(container);
453 return;
454 }
455 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000456 self->in_callback = 1;
457 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000458 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000459 self->in_callback = 0;
460 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000461 if (rv == NULL) {
462 flag_error(self);
463 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000464 }
Fred Drake85d835f2001-02-08 15:39:08 +0000465 Py_DECREF(rv);
466 }
467}
468
469#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
470 RETURN, GETUSERDATA) \
471static RC \
472my_##NAME##Handler PARAMS {\
473 xmlparseobject *self = GETUSERDATA ; \
474 PyObject *args = NULL; \
475 PyObject *rv = NULL; \
476 INIT \
477\
478 if (self->handlers[NAME] \
479 && self->handlers[NAME] != Py_None) { \
480 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000481 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000482 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000483 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
484 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000485 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000486 Py_DECREF(args); \
487 if (rv == NULL) { \
488 flag_error(self); \
489 return RETURN; \
490 } \
491 CONVERSION \
492 Py_DECREF(rv); \
493 } \
494 return RETURN; \
495}
496
Fred Drake6f987622000-08-25 18:03:30 +0000497#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
498 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
499 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000500
Fred Drake6f987622000-08-25 18:03:30 +0000501#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
502 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
503 rc = PyInt_AsLong(rv);, rc, \
504 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000505
Fred Drake6f987622000-08-25 18:03:30 +0000506VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000507 (void *userData, const XML_Char *name),
508 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000509
Fred Drake6f987622000-08-25 18:03:30 +0000510VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000511 (void *userData,
512 const XML_Char *target,
513 const XML_Char *data),
514 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000515
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000516#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000517VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000518 (void *userData, const XML_Char *data, int len),
519 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000520#else
Fred Drake6f987622000-08-25 18:03:30 +0000521VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000522 (void *userData, const XML_Char *data, int len),
523 ("(N)", (self->returns_unicode
524 ? conv_string_len_to_unicode(data,len)
525 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000526#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000527
Fred Drake6f987622000-08-25 18:03:30 +0000528VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000529 (void *userData,
530 const XML_Char *entityName,
531 const XML_Char *base,
532 const XML_Char *systemId,
533 const XML_Char *publicId,
534 const XML_Char *notationName),
535 ("(O&O&O&O&O&)",
536 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
537 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
538 STRING_CONV_FUNC,notationName))
539
540#if EXPAT_VERSION >= 0x015f00
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000541#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000542VOID_HANDLER(EntityDecl,
543 (void *userData,
544 const XML_Char *entityName,
545 int is_parameter_entity,
546 const XML_Char *value,
547 int value_length,
548 const XML_Char *base,
549 const XML_Char *systemId,
550 const XML_Char *publicId,
551 const XML_Char *notationName),
552 ("O&iNO&O&O&O&",
553 STRING_CONV_FUNC,entityName, is_parameter_entity,
554 conv_string_len_to_utf8(value, value_length),
555 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
556 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
557#else
558VOID_HANDLER(EntityDecl,
559 (void *userData,
560 const XML_Char *entityName,
561 int is_parameter_entity,
562 const XML_Char *value,
563 int value_length,
564 const XML_Char *base,
565 const XML_Char *systemId,
566 const XML_Char *publicId,
567 const XML_Char *notationName),
568 ("O&iNO&O&O&O&",
569 STRING_CONV_FUNC,entityName, is_parameter_entity,
570 (self->returns_unicode
571 ? conv_string_len_to_unicode(value, value_length)
572 : conv_string_len_to_utf8(value, value_length)),
573 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
574 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
575#endif
576
577VOID_HANDLER(XmlDecl,
578 (void *userData,
579 const XML_Char *version,
580 const XML_Char *encoding,
581 int standalone),
582 ("(O&O&i)",
583 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
584 standalone))
585
586static PyObject *
587conv_content_model(XML_Content * const model,
588 PyObject *(*conv_string)(XML_Char *))
589{
590 PyObject *result = NULL;
591 PyObject *children = PyTuple_New(model->numchildren);
592 int i;
593
594 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000595 assert(model->numchildren < INT_MAX);
596 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000597 PyObject *child = conv_content_model(&model->children[i],
598 conv_string);
599 if (child == NULL) {
600 Py_XDECREF(children);
601 return NULL;
602 }
603 PyTuple_SET_ITEM(children, i, child);
604 }
605 result = Py_BuildValue("(iiO&N)",
606 model->type, model->quant,
607 conv_string,model->name, children);
608 }
609 return result;
610}
611
612static PyObject *
613conv_content_model_utf8(XML_Content * const model)
614{
615 return conv_content_model(model, conv_string_to_utf8);
616}
617
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000618#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000619static PyObject *
620conv_content_model_unicode(XML_Content * const model)
621{
622 return conv_content_model(model, conv_string_to_unicode);
623}
624
625VOID_HANDLER(ElementDecl,
626 (void *userData,
627 const XML_Char *name,
628 XML_Content *model),
629 ("O&O&",
630 STRING_CONV_FUNC,name,
631 (self->returns_unicode ? conv_content_model_unicode
632 : conv_content_model_utf8),model))
633#else
634VOID_HANDLER(ElementDecl,
635 (void *userData,
636 const XML_Char *name,
637 XML_Content *model),
638 ("O&O&",
639 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
640#endif
641
642VOID_HANDLER(AttlistDecl,
643 (void *userData,
644 const XML_Char *elname,
645 const XML_Char *attname,
646 const XML_Char *att_type,
647 const XML_Char *dflt,
648 int isrequired),
649 ("(O&O&O&O&i)",
650 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
651 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
652 isrequired))
653#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000654
Fred Drake6f987622000-08-25 18:03:30 +0000655VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000656 (void *userData,
657 const XML_Char *notationName,
658 const XML_Char *base,
659 const XML_Char *systemId,
660 const XML_Char *publicId),
661 ("(O&O&O&O&)",
662 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
663 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000664
Fred Drake6f987622000-08-25 18:03:30 +0000665VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000666 (void *userData,
667 const XML_Char *prefix,
668 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000669 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000670
Fred Drake6f987622000-08-25 18:03:30 +0000671VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000672 (void *userData,
673 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000674 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000675
Fred Drake6f987622000-08-25 18:03:30 +0000676VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000677 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000678 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000679
Fred Drake6f987622000-08-25 18:03:30 +0000680VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000681 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000682 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000683
Fred Drake6f987622000-08-25 18:03:30 +0000684VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000686 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000687
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000688#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000689VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000690 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000691 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000692
Fred Drake6f987622000-08-25 18:03:30 +0000693VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000695 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000696#else
Fred Drake6f987622000-08-25 18:03:30 +0000697VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000698 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000699 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000700 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000701 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000702
Fred Drake6f987622000-08-25 18:03:30 +0000703VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000705 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000706 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000707 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000708#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000709
Fred Drake6f987622000-08-25 18:03:30 +0000710INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000711 (void *userData),
712 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000713
Fred Drake6f987622000-08-25 18:03:30 +0000714RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000715 (XML_Parser parser,
716 const XML_Char *context,
717 const XML_Char *base,
718 const XML_Char *systemId,
719 const XML_Char *publicId),
720 int rc=0;,
721 ("(O&O&O&O&)",
722 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000723 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
724 rc = PyInt_AsLong(rv);, rc,
725 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000726
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000727/* XXX UnknownEncodingHandler */
728
Fred Drake85d835f2001-02-08 15:39:08 +0000729#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000730VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000731 (void *userData, const XML_Char *doctypeName),
732 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
733 Py_None, Py_None, -1))
734#elif EXPAT_VERSION >= 0x015f00
735VOID_HANDLER(StartDoctypeDecl,
736 (void *userData, const XML_Char *doctypeName,
737 const XML_Char *sysid, const XML_Char *pubid,
738 int has_internal_subset),
739 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
740 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
741 has_internal_subset))
742#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000743
Fred Drake85d835f2001-02-08 15:39:08 +0000744#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000745VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000746#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000747
Fred Drake85d835f2001-02-08 15:39:08 +0000748#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000749VOID_HANDLER(ExternalParsedEntityDecl,
750 (void *userData, const XML_Char *entityName,
751 const XML_Char *base, const XML_Char *systemId,
752 const XML_Char *publicId),
753 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
754 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
755 STRING_CONV_FUNC, publicId))
756
757VOID_HANDLER(InternalParsedEntityDecl,
758 (void *userData, const XML_Char *entityName,
759 const XML_Char *replacementText, int replacementTextLength),
760 ("(O&O&i)", STRING_CONV_FUNC, entityName,
761 STRING_CONV_FUNC, replacementText, replacementTextLength))
762
Fred Drake85d835f2001-02-08 15:39:08 +0000763#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000765/* ---------------------------------------------------------------- */
766
767static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000768"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000769Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770
771static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000772xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000773{
Fred Drake0582df92000-07-12 04:49:00 +0000774 char *s;
775 int slen;
776 int isFinal = 0;
777 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000778
Fred Drake0582df92000-07-12 04:49:00 +0000779 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
780 return NULL;
781 rv = XML_Parse(self->itself, s, slen, isFinal);
782 if (PyErr_Occurred()) {
783 return NULL;
784 }
785 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000786 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000787 }
788 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789}
790
Fred Drakeca1f4262000-09-21 20:10:23 +0000791/* File reading copied from cPickle */
792
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000793#define BUF_SIZE 2048
794
Fred Drake0582df92000-07-12 04:49:00 +0000795static int
796readinst(char *buf, int buf_size, PyObject *meth)
797{
798 PyObject *arg = NULL;
799 PyObject *bytes = NULL;
800 PyObject *str = NULL;
801 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000802
Fred Drake676940b2000-09-22 15:21:31 +0000803 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000804 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000805
Fred Drakeca1f4262000-09-21 20:10:23 +0000806 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000807 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000808
Tim Peters954eef72000-09-22 06:01:11 +0000809 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000810
Fred Drakeca1f4262000-09-21 20:10:23 +0000811 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000812 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000813
Fred Drake0582df92000-07-12 04:49:00 +0000814 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000815 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000816 PyErr_Format(PyExc_TypeError,
817 "read() did not return a string object (type=%.400s)",
818 str->ob_type->tp_name);
819 goto finally;
820 }
821 len = PyString_GET_SIZE(str);
822 if (len > buf_size) {
823 PyErr_Format(PyExc_ValueError,
824 "read() returned too much data: "
825 "%i bytes requested, %i returned",
826 buf_size, len);
827 Py_DECREF(str);
828 goto finally;
829 }
830 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000831finally:
Fred Drake0582df92000-07-12 04:49:00 +0000832 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000833 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000834 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000835}
836
837static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000838"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000839Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840
841static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000842xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000843{
Fred Drake0582df92000-07-12 04:49:00 +0000844 int rv = 1;
845 PyObject *f;
846 FILE *fp;
847 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848
Fred Drake0582df92000-07-12 04:49:00 +0000849 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
850 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000851
Fred Drake0582df92000-07-12 04:49:00 +0000852 if (PyFile_Check(f)) {
853 fp = PyFile_AsFile(f);
854 }
855 else{
856 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000857 readmethod = PyObject_GetAttrString(f, "read");
858 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000859 PyErr_Clear();
860 PyErr_SetString(PyExc_TypeError,
861 "argument must have 'read' attribute");
862 return 0;
863 }
864 }
865 for (;;) {
866 int bytes_read;
867 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
868 if (buf == NULL)
869 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000870
Fred Drake0582df92000-07-12 04:49:00 +0000871 if (fp) {
872 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
873 if (bytes_read < 0) {
874 PyErr_SetFromErrno(PyExc_IOError);
875 return NULL;
876 }
877 }
878 else {
879 bytes_read = readinst(buf, BUF_SIZE, readmethod);
880 if (bytes_read < 0)
881 return NULL;
882 }
883 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
884 if (PyErr_Occurred())
885 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886
Fred Drake0582df92000-07-12 04:49:00 +0000887 if (!rv || bytes_read == 0)
888 break;
889 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000890 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000891 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000892 }
Fred Drake0582df92000-07-12 04:49:00 +0000893 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000894}
895
896static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000897"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000898Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899
900static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000901xmlparse_SetBase(xmlparseobject *self, PyObject *args)
902{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903 char *base;
904
Fred Drake0582df92000-07-12 04:49:00 +0000905 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000906 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000907 if (!XML_SetBase(self->itself, base)) {
908 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000909 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000910 Py_INCREF(Py_None);
911 return Py_None;
912}
913
914static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000915"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000916Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000917
918static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000919xmlparse_GetBase(xmlparseobject *self, PyObject *args)
920{
921 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000922 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000923
Fred Drake0582df92000-07-12 04:49:00 +0000924 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000925}
926
Fred Drakebd6101c2001-02-14 18:29:45 +0000927#if EXPAT_VERSION >= 0x015f00
928static char xmlparse_GetInputContext__doc__[] =
929"GetInputContext() -> string\n\
930Return the untranslated text of the input that caused the current event.\n\
931If the event was generated by a large amount of text (such as a start tag\n\
932for an element with many attributes), not all of the text may be available.";
933
934static PyObject *
935xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
936{
937 PyObject *result = NULL;
938
939 if (PyArg_ParseTuple(args, ":GetInputContext")) {
940 if (self->in_callback) {
941 int offset, size;
942 const char *buffer
943 = XML_GetInputContext(self->itself, &offset, &size);
944
945 if (buffer != NULL)
946 result = PyString_FromStringAndSize(buffer + offset, size);
947 else {
948 result = Py_None;
949 Py_INCREF(result);
950 }
951 }
952 else {
953 result = Py_None;
954 Py_INCREF(result);
955 }
956 }
957 return result;
958}
959#endif
960
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000961static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000962"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000963Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000964information passed to the ExternalEntityRefHandler.";
965
966static PyObject *
967xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
968{
969 char *context;
970 char *encoding = NULL;
971 xmlparseobject *new_parser;
972 int i;
973
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000974 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000975 &context, &encoding)) {
976 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000977 }
978
979#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
980 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000981#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000982#ifndef Py_TPFLAGS_HAVE_GC
983 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000984 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000985#else
986 /* Python versions 2.2 and later */
987 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
988#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000989#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000990
991 if (new_parser == NULL)
992 return NULL;
993 new_parser->returns_unicode = self->returns_unicode;
994 new_parser->ordered_attributes = self->ordered_attributes;
995 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000996 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000997 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000998 encoding);
999 new_parser->handlers = 0;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001000#ifdef Py_TPFLAGS_HAVE_GC
1001 PyObject_GC_Track(new_parser);
1002#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001003 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001004#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001005
1006 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001007 Py_DECREF(new_parser);
1008 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001009 }
1010
1011 XML_SetUserData(new_parser->itself, (void *)new_parser);
1012
1013 /* allocate and clear handlers first */
1014 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001015 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016
1017 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001018 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001019 Py_DECREF(new_parser);
1020 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001021 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001022 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001023
1024 /* then copy handlers from self */
1025 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001026 if (self->handlers[i]) {
1027 Py_INCREF(self->handlers[i]);
1028 new_parser->handlers[i] = self->handlers[i];
1029 handler_info[i].setter(new_parser->itself,
1030 handler_info[i].handler);
1031 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001032 }
Fred Drake28adf522000-09-24 22:07:59 +00001033 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001034}
1035
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001036#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001037
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038static char xmlparse_SetParamEntityParsing__doc__[] =
1039"SetParamEntityParsing(flag) -> success\n\
1040Controls parsing of parameter entities (including the external DTD\n\
1041subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1042XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1043XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1044was successful.";
1045
1046static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001047xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001048{
Fred Drake85d835f2001-02-08 15:39:08 +00001049 int flag;
1050 if (!PyArg_ParseTuple(args, "i", &flag))
1051 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001052 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001053 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001054}
1055
Fred Drake85d835f2001-02-08 15:39:08 +00001056#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001057
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001058static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001059 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001060 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001061 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001062 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001063 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001064 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001065 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001066 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001067 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1068 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001069#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001070 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1071 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1072#endif
1073#if EXPAT_VERSION >= 0x015f00
1074 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1075 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001076#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001077 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001078};
1079
1080/* ---------- */
1081
1082
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001083#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001084
1085/*
1086 pyexpat international encoding support.
1087 Make it as simple as possible.
1088*/
1089
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001090static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001091PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092
1093static void
1094init_template_buffer(void)
1095{
1096 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001097 for (i = 0; i < 256; i++) {
1098 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001099 }
Fred Drakebb66a202001-03-01 20:48:17 +00001100 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001101}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102
1103int
1104PyUnknownEncodingHandler(void *encodingHandlerData,
1105const XML_Char *name,
1106XML_Encoding * info)
1107{
Fred Drakebb66a202001-03-01 20:48:17 +00001108 PyUnicodeObject *_u_string = NULL;
1109 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110 int i;
1111
Fred Drakebb66a202001-03-01 20:48:17 +00001112 /* Yes, supports only 8bit encodings */
1113 _u_string = (PyUnicodeObject *)
1114 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001115
Fred Drakebb66a202001-03-01 20:48:17 +00001116 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001117 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001118
Fred Drakebb66a202001-03-01 20:48:17 +00001119 for (i = 0; i < 256; i++) {
1120 /* Stupid to access directly, but fast */
1121 Py_UNICODE c = _u_string->str[i];
1122 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001123 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001124 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001125 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001126 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001127
1128 info->data = NULL;
1129 info->convert = NULL;
1130 info->release = NULL;
1131 result=1;
1132
1133 Py_DECREF(_u_string);
1134 return result;
1135}
1136
1137#endif
1138
1139static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001140newxmlparseobject(char *encoding, char *namespace_separator)
1141{
1142 int i;
1143 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001144
1145#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001146 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1147 if (self == NULL)
1148 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001149
Fred Drake0582df92000-07-12 04:49:00 +00001150 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001151#else
Fred Drake0582df92000-07-12 04:49:00 +00001152 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001153#ifdef Py_TPFLAGS_HAVE_GC
1154 /* Code for versions 2.2 and later */
1155 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1156#else
Fred Drake0582df92000-07-12 04:49:00 +00001157 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001158#endif
Fred Drake0582df92000-07-12 04:49:00 +00001159 if (self == NULL)
1160 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001161
Fred Drake0582df92000-07-12 04:49:00 +00001162 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001163#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001164 self->ordered_attributes = 0;
1165 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001166 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001167 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001168 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001169 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1170 }
Fred Drake85d835f2001-02-08 15:39:08 +00001171 else {
Fred Drake0582df92000-07-12 04:49:00 +00001172 self->itself = XML_ParserCreate(encoding);
1173 }
Martin v. Löwis894258c2001-09-23 10:20:10 +00001174#ifdef Py_TPFLAGS_HAVE_GC
1175 PyObject_GC_Track(self);
1176#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001177 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001178#endif
Fred Drake0582df92000-07-12 04:49:00 +00001179 if (self->itself == NULL) {
1180 PyErr_SetString(PyExc_RuntimeError,
1181 "XML_ParserCreate failed");
1182 Py_DECREF(self);
1183 return NULL;
1184 }
1185 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001186#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001187 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1188#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189
Fred Drake0582df92000-07-12 04:49:00 +00001190 for(i = 0; handler_info[i].name != NULL; i++)
1191 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001192
Fred Drake0582df92000-07-12 04:49:00 +00001193 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001194 if (!self->handlers){
1195 Py_DECREF(self);
1196 return PyErr_NoMemory();
1197 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001198 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001199
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001200 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001201}
1202
1203
1204static void
Fred Drake0582df92000-07-12 04:49:00 +00001205xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001206{
Fred Drake0582df92000-07-12 04:49:00 +00001207 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001208#ifdef Py_TPFLAGS_HAVE_GC
1209 PyObject_GC_UnTrack(self);
1210#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001211 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001212#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001213 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001214 XML_ParserFree(self->itself);
1215 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001216
Fred Drake85d835f2001-02-08 15:39:08 +00001217 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001218 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001219 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001220 temp = self->handlers[i];
1221 self->handlers[i] = NULL;
1222 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001223 }
1224 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001225 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001226#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001227 /* Code for versions before 1.6 */
1228 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001229#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001230#ifndef Py_TPFLAGS_HAVE_GC
1231 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001232 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001233#else
1234 /* Code for versions 2.2 and later. */
1235 PyObject_GC_Del(self);
1236#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001237#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001238}
1239
Fred Drake0582df92000-07-12 04:49:00 +00001240static int
1241handlername2int(const char *name)
1242{
1243 int i;
1244 for (i=0; handler_info[i].name != NULL; i++) {
1245 if (strcmp(name, handler_info[i].name) == 0) {
1246 return i;
1247 }
1248 }
1249 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001250}
1251
1252static PyObject *
1253xmlparse_getattr(xmlparseobject *self, char *name)
1254{
Fred Drake0582df92000-07-12 04:49:00 +00001255 int handlernum;
1256 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001257 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001258 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001259 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001260 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001261 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001262 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001263 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1264 if (strcmp(name, "ordered_attributes") == 0)
1265 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001266 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001267 return PyInt_FromLong((long) self->returns_unicode);
1268 if (strcmp(name, "specified_attributes") == 0)
1269 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001270
Fred Drake0582df92000-07-12 04:49:00 +00001271 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272
Fred Drake0582df92000-07-12 04:49:00 +00001273 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1274 Py_INCREF(self->handlers[handlernum]);
1275 return self->handlers[handlernum];
1276 }
1277 if (strcmp(name, "__members__") == 0) {
1278 int i;
1279 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001280 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001281 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001282 }
1283 PyList_Append(rc, PyString_FromString("ErrorCode"));
1284 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1285 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1286 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001287 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001288 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001289 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001290
Fred Drake0582df92000-07-12 04:49:00 +00001291 return rc;
1292 }
1293 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001294}
1295
Fred Drake6f987622000-08-25 18:03:30 +00001296static int
1297sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001298{
1299 int handlernum = handlername2int(name);
1300 if (handlernum != -1) {
1301 Py_INCREF(v);
1302 Py_XDECREF(self->handlers[handlernum]);
1303 self->handlers[handlernum] = v;
1304 handler_info[handlernum].setter(self->itself,
1305 handler_info[handlernum].handler);
1306 return 1;
1307 }
1308 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001309}
1310
1311static int
Fred Drake6f987622000-08-25 18:03:30 +00001312xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001313{
Fred Drake6f987622000-08-25 18:03:30 +00001314 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001315 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001316 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1317 return -1;
1318 }
Fred Drake85d835f2001-02-08 15:39:08 +00001319 if (strcmp(name, "ordered_attributes") == 0) {
1320 if (PyObject_IsTrue(v))
1321 self->ordered_attributes = 1;
1322 else
1323 self->ordered_attributes = 0;
1324 return 0;
1325 }
Fred Drake6f987622000-08-25 18:03:30 +00001326 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001327 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001328#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001329 PyErr_SetString(PyExc_ValueError,
1330 "Cannot return Unicode strings in Python 1.5");
1331 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001332#else
Fred Drake6f987622000-08-25 18:03:30 +00001333 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001334#endif
Fred Drake6f987622000-08-25 18:03:30 +00001335 }
1336 else
1337 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001338 return 0;
1339 }
1340 if (strcmp(name, "specified_attributes") == 0) {
1341 if (PyObject_IsTrue(v))
1342 self->specified_attributes = 1;
1343 else
1344 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001345 return 0;
1346 }
1347 if (sethandler(self, name, v)) {
1348 return 0;
1349 }
1350 PyErr_SetString(PyExc_AttributeError, name);
1351 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001352}
1353
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001354#ifdef WITH_CYCLE_GC
1355static int
1356xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1357{
Fred Drakecde79132001-04-25 16:01:30 +00001358 int i, err;
1359 for (i = 0; handler_info[i].name != NULL; i++) {
1360 if (!op->handlers[i])
1361 continue;
1362 err = visit(op->handlers[i], arg);
1363 if (err)
1364 return err;
1365 }
1366 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001367}
1368
1369static int
1370xmlparse_clear(xmlparseobject *op)
1371{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001372 clear_handlers(op, 0);
Fred Drakecde79132001-04-25 16:01:30 +00001373 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001374}
1375#endif
1376
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001378"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001379
1380static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001381 PyObject_HEAD_INIT(NULL)
1382 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001383 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001384 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001385 0, /*tp_itemsize*/
1386 /* methods */
1387 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1388 (printfunc)0, /*tp_print*/
1389 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1390 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1391 (cmpfunc)0, /*tp_compare*/
1392 (reprfunc)0, /*tp_repr*/
1393 0, /*tp_as_number*/
1394 0, /*tp_as_sequence*/
1395 0, /*tp_as_mapping*/
1396 (hashfunc)0, /*tp_hash*/
1397 (ternaryfunc)0, /*tp_call*/
1398 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001399 0, /* tp_getattro */
1400 0, /* tp_setattro */
1401 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001402#ifdef Py_TPFLAGS_HAVE_GC
1403 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1404#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001405 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001406#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001407 Xmlparsetype__doc__, /* Documentation string */
1408#ifdef WITH_CYCLE_GC
1409 (traverseproc)xmlparse_traverse, /* tp_traverse */
1410 (inquiry)xmlparse_clear /* tp_clear */
1411#else
1412 0, 0
1413#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001414};
1415
1416/* End of code for xmlparser objects */
1417/* -------------------------------------------------------- */
1418
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001419static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001420"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1421Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001422
1423static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001424pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1425{
Fred Drakecde79132001-04-25 16:01:30 +00001426 char *encoding = NULL;
1427 char *namespace_separator = NULL;
1428 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001429
Fred Drakecde79132001-04-25 16:01:30 +00001430 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1431 &encoding, &namespace_separator))
1432 return NULL;
1433 if (namespace_separator != NULL
1434 && strlen(namespace_separator) > 1) {
1435 PyErr_SetString(PyExc_ValueError,
1436 "namespace_separator must be at most one"
1437 " character, omitted, or None");
1438 return NULL;
1439 }
1440 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001441}
1442
1443static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001444"ErrorString(errno) -> string\n\
1445Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001446
1447static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001448pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001449{
Fred Drake0582df92000-07-12 04:49:00 +00001450 long code = 0;
1451
1452 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1453 return NULL;
1454 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001455}
1456
1457/* List of methods defined in the module */
1458
1459static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001460 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1461 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1462 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1463 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001464
Fred Drake0582df92000-07-12 04:49:00 +00001465 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001466};
1467
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001468/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001469
1470static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001471"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001472
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001473#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001474
1475/* 1.5 compatibility: PyModule_AddObject */
1476static int
1477PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1478{
Fred Drakecde79132001-04-25 16:01:30 +00001479 PyObject *dict;
1480 if (!PyModule_Check(m) || o == NULL)
1481 return -1;
1482 dict = PyModule_GetDict(m);
1483 if (dict == NULL)
1484 return -1;
1485 if (PyDict_SetItemString(dict, name, o))
1486 return -1;
1487 Py_DECREF(o);
1488 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001489}
1490
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001491int
1492PyModule_AddIntConstant(PyObject *m, char *name, long value)
1493{
Fred Drakecde79132001-04-25 16:01:30 +00001494 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001495}
1496
Fred Drakea77254a2000-09-29 19:23:29 +00001497static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001498PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1499{
Fred Drakecde79132001-04-25 16:01:30 +00001500 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001501}
1502
1503#endif
1504
Fred Drake4113b132001-03-24 19:58:26 +00001505
1506/* Return a Python string that represents the version number without the
1507 * extra cruft added by revision control, even if the right options were
1508 * given to the "cvs export" command to make it not include the extra
1509 * cruft.
1510 */
1511static PyObject *
1512get_version_string(void)
1513{
1514 static char *rcsid = "$Revision$";
1515 char *rev = rcsid;
1516 int i = 0;
1517
1518 while (!isdigit(*rev))
1519 ++rev;
1520 while (rev[i] != ' ' && rev[i] != '\0')
1521 ++i;
1522
1523 return PyString_FromStringAndSize(rev, i);
1524}
1525
Fred Drakecde79132001-04-25 16:01:30 +00001526/* Initialization function for the module */
1527
1528#ifndef MODULE_NAME
1529#define MODULE_NAME "pyexpat"
1530#endif
1531
1532#ifndef MODULE_INITFUNC
1533#define MODULE_INITFUNC initpyexpat
1534#endif
1535
1536void MODULE_INITFUNC(void); /* avoid compiler warnings */
1537
Fred Drake6f987622000-08-25 18:03:30 +00001538DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001539MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001540{
1541 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001542 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001543 PyObject *errors_module;
1544 PyObject *modelmod_name;
1545 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001546 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001547
Fred Drake6f987622000-08-25 18:03:30 +00001548 if (errmod_name == NULL)
1549 return;
Fred Drakecde79132001-04-25 16:01:30 +00001550 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001551 if (modelmod_name == NULL)
1552 return;
Fred Drake6f987622000-08-25 18:03:30 +00001553
Fred Drake0582df92000-07-12 04:49:00 +00001554 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001555
Fred Drake0582df92000-07-12 04:49:00 +00001556 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001557 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001558 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001559
Fred Drake0582df92000-07-12 04:49:00 +00001560 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001561 if (ErrorObject == NULL) {
1562 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001563 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001564 if (ErrorObject == NULL)
1565 return;
1566 }
1567 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001568 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001569 Py_INCREF(ErrorObject);
1570 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001571 Py_INCREF(&Xmlparsetype);
1572 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001573
Fred Drake4113b132001-03-24 19:58:26 +00001574 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001575#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001576 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1577 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001578 {
1579 XML_Expat_Version info = XML_ExpatVersionInfo();
1580 PyModule_AddObject(m, "version_info",
1581 Py_BuildValue("(iii)", info.major,
1582 info.minor, info.micro));
1583 }
Fred Drake738293d2000-12-21 17:25:07 +00001584#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001585#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001586 init_template_buffer();
1587#endif
Fred Drake0582df92000-07-12 04:49:00 +00001588 /* XXX When Expat supports some way of figuring out how it was
1589 compiled, this should check and set native_encoding
1590 appropriately.
1591 */
Fred Drake93adb692000-09-23 04:55:48 +00001592 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001593
Fred Drake85d835f2001-02-08 15:39:08 +00001594 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001595 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001596 errors_module = PyDict_GetItem(d, errmod_name);
1597 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001598 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001599 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001600 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001601 /* gives away the reference to errors_module */
1602 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001603 }
1604 }
Fred Drake6f987622000-08-25 18:03:30 +00001605 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001606 model_module = PyDict_GetItem(d, modelmod_name);
1607 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001608 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001609 if (model_module != NULL) {
1610 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1611 /* gives away the reference to model_module */
1612 PyModule_AddObject(m, "model", model_module);
1613 }
1614 }
1615 Py_DECREF(modelmod_name);
1616 if (errors_module == NULL || model_module == NULL)
1617 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001618 return;
1619
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001620#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001621 PyModule_AddStringConstant(errors_module, #name, \
1622 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001623
Fred Drake0582df92000-07-12 04:49:00 +00001624 MYCONST(XML_ERROR_NO_MEMORY);
1625 MYCONST(XML_ERROR_SYNTAX);
1626 MYCONST(XML_ERROR_NO_ELEMENTS);
1627 MYCONST(XML_ERROR_INVALID_TOKEN);
1628 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1629 MYCONST(XML_ERROR_PARTIAL_CHAR);
1630 MYCONST(XML_ERROR_TAG_MISMATCH);
1631 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1632 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1633 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1634 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1635 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1636 MYCONST(XML_ERROR_ASYNC_ENTITY);
1637 MYCONST(XML_ERROR_BAD_CHAR_REF);
1638 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1639 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1640 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1641 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1642 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001643 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1644 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1645 MYCONST(XML_ERROR_NOT_STANDALONE);
1646
Fred Drake85d835f2001-02-08 15:39:08 +00001647 PyModule_AddStringConstant(errors_module, "__doc__",
1648 "Constants used to describe error conditions.");
1649
Fred Drake93adb692000-09-23 04:55:48 +00001650#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001651
1652#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001653#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001654 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1655 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1656 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001657#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001658#endif
1659
Fred Drake85d835f2001-02-08 15:39:08 +00001660#if EXPAT_VERSION >= 0x015f00
1661#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1662 PyModule_AddStringConstant(model_module, "__doc__",
1663 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001664
Fred Drake85d835f2001-02-08 15:39:08 +00001665 MYCONST(XML_CTYPE_EMPTY);
1666 MYCONST(XML_CTYPE_ANY);
1667 MYCONST(XML_CTYPE_MIXED);
1668 MYCONST(XML_CTYPE_NAME);
1669 MYCONST(XML_CTYPE_CHOICE);
1670 MYCONST(XML_CTYPE_SEQ);
1671
1672 MYCONST(XML_CQUANT_NONE);
1673 MYCONST(XML_CQUANT_OPT);
1674 MYCONST(XML_CQUANT_REP);
1675 MYCONST(XML_CQUANT_PLUS);
1676#undef MYCONST
1677#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001678}
1679
Fred Drake6f987622000-08-25 18:03:30 +00001680static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001681clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001682{
Fred Drakecde79132001-04-25 16:01:30 +00001683 int i = 0;
1684 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001685
Fred Drakecde79132001-04-25 16:01:30 +00001686 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001687 if (initial)
1688 self->handlers[i]=NULL;
1689 else {
Fred Drakecde79132001-04-25 16:01:30 +00001690 temp = self->handlers[i];
1691 self->handlers[i] = NULL;
1692 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001693 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001694 }
Fred Drakecde79132001-04-25 16:01:30 +00001695 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001696}
1697
Fred Drake6f987622000-08-25 18:03:30 +00001698typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001699
Fred Drake6f987622000-08-25 18:03:30 +00001700static void
1701pyxml_UpdatePairedHandlers(xmlparseobject *self,
1702 int startHandler,
1703 int endHandler,
1704 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001705{
Fred Drakecde79132001-04-25 16:01:30 +00001706 void *start_handler = NULL;
1707 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001708
Fred Drake0582df92000-07-12 04:49:00 +00001709 if (self->handlers[startHandler]
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001710 && self->handlers[startHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001711 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001712 }
Martin v. Löwis42ba08f2001-11-10 13:59:16 +00001713 if (self->handlers[endHandler]
1714 && self->handlers[endHandler] != Py_None) {
Fred Drakecde79132001-04-25 16:01:30 +00001715 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001716 }
1717 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001718}
1719
Fred Drake6f987622000-08-25 18:03:30 +00001720static void
1721pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001722{
1723 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1724 StartElement, EndElement,
1725 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001726}
1727
Fred Drake6f987622000-08-25 18:03:30 +00001728static void
1729pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001730{
1731 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1732 StartElement, EndElement,
1733 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001734}
1735
Fred Drake6f987622000-08-25 18:03:30 +00001736static void
1737pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001738{
1739 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1740 StartNamespaceDecl, EndNamespaceDecl,
1741 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001742}
1743
Fred Drake6f987622000-08-25 18:03:30 +00001744static void
1745pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001746{
1747 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1748 StartNamespaceDecl, EndNamespaceDecl,
1749 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001750}
1751
Fred Drake6f987622000-08-25 18:03:30 +00001752static void
1753pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001754{
1755 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1756 StartCdataSection, EndCdataSection,
1757 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001758}
1759
Fred Drake6f987622000-08-25 18:03:30 +00001760static void
1761pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001762{
1763 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1764 StartCdataSection, EndCdataSection,
1765 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001766}
1767
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001768#if EXPAT_VERSION >= 0x010200
1769
1770static void
1771pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1772{
1773 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1774 StartDoctypeDecl, EndDoctypeDecl,
1775 (pairsetter)XML_SetDoctypeDeclHandler);
1776}
1777
1778static void
1779pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1780{
1781 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1782 StartDoctypeDecl, EndDoctypeDecl,
1783 (pairsetter)XML_SetDoctypeDeclHandler);
1784}
1785
1786#endif
1787
Fred Drake0582df92000-07-12 04:49:00 +00001788statichere struct HandlerInfo handler_info[] = {
1789 {"StartElementHandler",
1790 pyxml_SetStartElementHandler,
1791 (xmlhandler)my_StartElementHandler},
1792 {"EndElementHandler",
1793 pyxml_SetEndElementHandler,
1794 (xmlhandler)my_EndElementHandler},
1795 {"ProcessingInstructionHandler",
1796 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1797 (xmlhandler)my_ProcessingInstructionHandler},
1798 {"CharacterDataHandler",
1799 (xmlhandlersetter)XML_SetCharacterDataHandler,
1800 (xmlhandler)my_CharacterDataHandler},
1801 {"UnparsedEntityDeclHandler",
1802 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1803 (xmlhandler)my_UnparsedEntityDeclHandler },
1804 {"NotationDeclHandler",
1805 (xmlhandlersetter)XML_SetNotationDeclHandler,
1806 (xmlhandler)my_NotationDeclHandler },
1807 {"StartNamespaceDeclHandler",
1808 pyxml_SetStartNamespaceDeclHandler,
1809 (xmlhandler)my_StartNamespaceDeclHandler },
1810 {"EndNamespaceDeclHandler",
1811 pyxml_SetEndNamespaceDeclHandler,
1812 (xmlhandler)my_EndNamespaceDeclHandler },
1813 {"CommentHandler",
1814 (xmlhandlersetter)XML_SetCommentHandler,
1815 (xmlhandler)my_CommentHandler},
1816 {"StartCdataSectionHandler",
1817 pyxml_SetStartCdataSection,
1818 (xmlhandler)my_StartCdataSectionHandler},
1819 {"EndCdataSectionHandler",
1820 pyxml_SetEndCdataSection,
1821 (xmlhandler)my_EndCdataSectionHandler},
1822 {"DefaultHandler",
1823 (xmlhandlersetter)XML_SetDefaultHandler,
1824 (xmlhandler)my_DefaultHandler},
1825 {"DefaultHandlerExpand",
1826 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1827 (xmlhandler)my_DefaultHandlerExpandHandler},
1828 {"NotStandaloneHandler",
1829 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1830 (xmlhandler)my_NotStandaloneHandler},
1831 {"ExternalEntityRefHandler",
1832 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1833 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001834#if EXPAT_VERSION >= 0x010200
1835 {"StartDoctypeDeclHandler",
1836 pyxml_SetStartDoctypeDeclHandler,
1837 (xmlhandler)my_StartDoctypeDeclHandler},
1838 {"EndDoctypeDeclHandler",
1839 pyxml_SetEndDoctypeDeclHandler,
1840 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001841#endif
1842#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001843 {"ExternalParsedEntityDeclHandler",
1844 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1845 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1846 {"InternalParsedEntityDeclHandler",
1847 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1848 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001849#endif
1850#if EXPAT_VERSION >= 0x015f00
1851 {"EntityDeclHandler",
1852 (xmlhandlersetter)XML_SetEntityDeclHandler,
1853 (xmlhandler)my_EntityDeclHandler},
1854 {"XmlDeclHandler",
1855 (xmlhandlersetter)XML_SetXmlDeclHandler,
1856 (xmlhandler)my_XmlDeclHandler},
1857 {"ElementDeclHandler",
1858 (xmlhandlersetter)XML_SetElementDeclHandler,
1859 (xmlhandler)my_ElementDeclHandler},
1860 {"AttlistDeclHandler",
1861 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1862 (xmlhandler)my_AttlistDeclHandler},
1863#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001864
Fred Drake0582df92000-07-12 04:49:00 +00001865 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001866};