blob: 96cb79d74408a70fd0aa7b912f4419af683198ed [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00004#include "compile.h"
5#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00006#ifdef HAVE_EXPAT_H
7#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00008#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +00009#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
10 + 0x100 * XML_MINOR_VERSION \
11 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000012#else
Fred Drake85d835f2001-02-08 15:39:08 +000013/* Assume the oldest Expat that used expat.h and did not have version info */
14#define EXPAT_VERSION 0x015f00
15#endif
16#else /* !defined(HAVE_EXPAT_H) */
17#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000018/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000019#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000020#define EXPAT_VERSION 0x010100
21#endif
Fred Drake85d835f2001-02-08 15:39:08 +000022#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000023
24#ifndef PyGC_HEAD_SIZE
25#define PyGC_HEAD_SIZE 0
26#define PyObject_GC_Init(x)
27#define PyObject_GC_Fini(m)
28#define Py_TPFLAGS_GC 0
29#endif
30
Martin v. Löwis339d0f72001-08-17 18:39:25 +000031#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
32/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
33#define Py_USING_UNICODE
34#endif
35
Fred Drake0582df92000-07-12 04:49:00 +000036enum HandlerTypes {
37 StartElement,
38 EndElement,
39 ProcessingInstruction,
40 CharacterData,
41 UnparsedEntityDecl,
42 NotationDecl,
43 StartNamespaceDecl,
44 EndNamespaceDecl,
45 Comment,
46 StartCdataSection,
47 EndCdataSection,
48 Default,
49 DefaultHandlerExpand,
50 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000051 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000052#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000053 StartDoctypeDecl,
54 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000055#endif
56#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000057 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000058 InternalParsedEntityDecl,
59#endif
60#if EXPAT_VERSION >= 0x015f00
61 EntityDecl,
62 XmlDecl,
63 ElementDecl,
64 AttlistDecl,
65#endif
66 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000067};
68
69static PyObject *ErrorObject;
70
71/* ----------------------------------------------------- */
72
73/* Declarations for objects of type xmlparser */
74
75typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000076 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000077
Fred Drake0582df92000-07-12 04:49:00 +000078 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000079 int returns_unicode; /* True if Unicode strings are returned;
80 if false, UTF-8 strings are returned */
81 int ordered_attributes; /* Return attributes as a list. */
82 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000083 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000084 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000085} xmlparseobject;
86
87staticforward PyTypeObject Xmlparsetype;
88
Fred Drake6f987622000-08-25 18:03:30 +000089typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090typedef void* xmlhandler;
91
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000092struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000093 const char *name;
94 xmlhandlersetter setter;
95 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000096 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000097};
98
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000099staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000100
Fred Drakebd6101c2001-02-14 18:29:45 +0000101/* Set an integer attribute on the error object; return true on success,
102 * false on an exception.
103 */
104static int
105set_error_attr(PyObject *err, char *name, int value)
106{
107 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000108
Fred Drakebd6101c2001-02-14 18:29:45 +0000109 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
110 Py_DECREF(v);
111 return 0;
112 }
113 return 1;
114}
115
116/* Build and set an Expat exception, including positioning
117 * information. Always returns NULL.
118 */
Fred Drake85d835f2001-02-08 15:39:08 +0000119static PyObject *
120set_error(xmlparseobject *self)
121{
122 PyObject *err;
123 char buffer[256];
124 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 int lineno = XML_GetErrorLineNumber(parser);
126 int column = XML_GetErrorColumnNumber(parser);
127 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000128
129 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000130 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000131 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000132 if ( err != NULL
133 && set_error_attr(err, "code", code)
134 && set_error_attr(err, "offset", column)
135 && set_error_attr(err, "lineno", lineno)) {
136 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000137 }
138 return NULL;
139}
140
141
142#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000143/* Convert an array of attributes and their values into a Python dict */
144
Fred Drake0582df92000-07-12 04:49:00 +0000145static PyObject *
146conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000147{
Fred Drake0582df92000-07-12 04:49:00 +0000148 PyObject *attrs_obj = NULL;
149 XML_Char **attrs_p, **attrs_k = NULL;
150 int attrs_len;
151 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000152
Fred Drake0582df92000-07-12 04:49:00 +0000153 if ((attrs_obj = PyDict_New()) == NULL)
154 goto finally;
155 for (attrs_len = 0, attrs_p = atts;
156 *attrs_p;
157 attrs_p++, attrs_len++) {
158 if (attrs_len % 2) {
159 rv = PyString_FromString(*attrs_p);
160 if (!rv) {
161 Py_DECREF(attrs_obj);
162 attrs_obj = NULL;
163 goto finally;
164 }
165 if (PyDict_SetItemString(attrs_obj,
166 (char*)*attrs_k, rv) < 0) {
167 Py_DECREF(attrs_obj);
168 attrs_obj = NULL;
169 goto finally;
170 }
171 Py_DECREF(rv);
172 }
173 else
174 attrs_k = attrs_p;
175 }
176 finally:
177 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000178}
Fred Drake85d835f2001-02-08 15:39:08 +0000179#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000180
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000181#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000182#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000183static PyObject *
184conv_atts_using_unicode(XML_Char **atts)
185{
Fred Drakeca1f4262000-09-21 20:10:23 +0000186 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000187 XML_Char **attrs_p, **attrs_k = NULL;
188 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000189
Fred Drake0582df92000-07-12 04:49:00 +0000190 if ((attrs_obj = PyDict_New()) == NULL)
191 goto finally;
192 for (attrs_len = 0, attrs_p = atts;
193 *attrs_p;
194 attrs_p++, attrs_len++) {
195 if (attrs_len % 2) {
196 PyObject *attr_str, *value_str;
197 const char *p = (const char *) (*attrs_k);
198 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
199 if (!attr_str) {
200 Py_DECREF(attrs_obj);
201 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000202 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000203 }
204 p = (const char *) *attrs_p;
205 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
206 if (!value_str) {
207 Py_DECREF(attrs_obj);
208 Py_DECREF(attr_str);
209 attrs_obj = NULL;
210 goto finally;
211 }
212 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
213 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000214 Py_DECREF(attr_str);
215 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000216 attrs_obj = NULL;
217 goto finally;
218 }
219 Py_DECREF(attr_str);
220 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000221 }
Fred Drake0582df92000-07-12 04:49:00 +0000222 else
223 attrs_k = attrs_p;
224 }
225 finally:
226 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000227}
Fred Drake85d835f2001-02-08 15:39:08 +0000228#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000229
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000230/* Convert a string of XML_Chars into a Unicode string.
231 Returns None if str is a null pointer. */
232
Fred Drake0582df92000-07-12 04:49:00 +0000233static PyObject *
234conv_string_to_unicode(XML_Char *str)
235{
236 /* XXX currently this code assumes that XML_Char is 8-bit,
237 and hence in UTF-8. */
238 /* UTF-8 from Expat, Unicode desired */
239 if (str == NULL) {
240 Py_INCREF(Py_None);
241 return Py_None;
242 }
243 return PyUnicode_DecodeUTF8((const char *)str,
244 strlen((const char *)str),
245 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000246}
247
Fred Drake0582df92000-07-12 04:49:00 +0000248static PyObject *
249conv_string_len_to_unicode(const XML_Char *str, int len)
250{
251 /* XXX currently this code assumes that XML_Char is 8-bit,
252 and hence in UTF-8. */
253 /* UTF-8 from Expat, Unicode desired */
254 if (str == NULL) {
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
Fred Drake6f987622000-08-25 18:03:30 +0000258 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000259}
260#endif
261
262/* Convert a string of XML_Chars into an 8-bit Python string.
263 Returns None if str is a null pointer. */
264
Fred Drake6f987622000-08-25 18:03:30 +0000265static PyObject *
266conv_string_to_utf8(XML_Char *str)
267{
268 /* XXX currently this code assumes that XML_Char is 8-bit,
269 and hence in UTF-8. */
270 /* UTF-8 from Expat, UTF-8 desired */
271 if (str == NULL) {
272 Py_INCREF(Py_None);
273 return Py_None;
274 }
275 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000276}
277
Fred Drake6f987622000-08-25 18:03:30 +0000278static PyObject *
279conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000280{
Fred Drake6f987622000-08-25 18:03:30 +0000281 /* XXX currently this code assumes that XML_Char is 8-bit,
282 and hence in UTF-8. */
283 /* UTF-8 from Expat, UTF-8 desired */
284 if (str == NULL) {
285 Py_INCREF(Py_None);
286 return Py_None;
287 }
288 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000289}
290
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000291/* Callback routines */
292
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000293static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000294
Fred Drake6f987622000-08-25 18:03:30 +0000295static void
296flag_error(xmlparseobject *self)
297{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000298 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000299}
300
301static PyCodeObject*
302getcode(enum HandlerTypes slot, char* func_name, int lineno)
303{
Fred Drakebd6101c2001-02-14 18:29:45 +0000304 PyObject *code = NULL;
305 PyObject *name = NULL;
306 PyObject *nulltuple = NULL;
307 PyObject *filename = NULL;
308
309 if (handler_info[slot].tb_code == NULL) {
310 code = PyString_FromString("");
311 if (code == NULL)
312 goto failed;
313 name = PyString_FromString(func_name);
314 if (name == NULL)
315 goto failed;
316 nulltuple = PyTuple_New(0);
317 if (nulltuple == NULL)
318 goto failed;
319 filename = PyString_FromString(__FILE__);
320 handler_info[slot].tb_code =
321 PyCode_New(0, /* argcount */
322 0, /* nlocals */
323 0, /* stacksize */
324 0, /* flags */
325 code, /* code */
326 nulltuple, /* consts */
327 nulltuple, /* names */
328 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000329#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000330 nulltuple, /* freevars */
331 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000332#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000333 filename, /* filename */
334 name, /* name */
335 lineno, /* firstlineno */
336 code /* lnotab */
337 );
338 if (handler_info[slot].tb_code == NULL)
339 goto failed;
340 Py_DECREF(code);
341 Py_DECREF(nulltuple);
342 Py_DECREF(filename);
343 Py_DECREF(name);
344 }
345 return handler_info[slot].tb_code;
346 failed:
347 Py_XDECREF(code);
348 Py_XDECREF(name);
349 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000350}
351
352static PyObject*
353call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
354{
Fred Drakebd6101c2001-02-14 18:29:45 +0000355 PyThreadState *tstate = PyThreadState_GET();
356 PyFrameObject *f;
357 PyObject *res;
358
359 if (c == NULL)
360 return NULL;
361 f = PyFrame_New(
362 tstate, /*back*/
363 c, /*code*/
364 tstate->frame->f_globals, /*globals*/
365 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000366 );
367 if (f == NULL)
368 return NULL;
369 tstate->frame = f;
370 res = PyEval_CallObject(func, args);
371 if (res == NULL && tstate->curexc_traceback == NULL)
372 PyTraceBack_Here(f);
373 tstate->frame = f->f_back;
374 Py_DECREF(f);
375 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000376}
377
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000378#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000379#define STRING_CONV_FUNC conv_string_to_utf8
380#else
381/* Python 1.6 and later versions */
382#define STRING_CONV_FUNC (self->returns_unicode \
383 ? conv_string_to_unicode : conv_string_to_utf8)
384#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000385
Fred Drake85d835f2001-02-08 15:39:08 +0000386static void
387my_StartElementHandler(void *userData,
388 const XML_Char *name, const XML_Char **atts)
389{
390 xmlparseobject *self = (xmlparseobject *)userData;
391
392 if (self->handlers[StartElement]
393 && self->handlers[StartElement] != Py_None) {
394 PyObject *container, *rv, *args;
395 int i, max;
396
397 /* Set max to the number of slots filled in atts[]; max/2 is
398 * the number of attributes we need to process.
399 */
400 if (self->specified_attributes) {
401 max = XML_GetSpecifiedAttributeCount(self->itself);
402 }
403 else {
404 max = 0;
405 while (atts[max] != NULL)
406 max += 2;
407 }
408 /* Build the container. */
409 if (self->ordered_attributes)
410 container = PyList_New(max);
411 else
412 container = PyDict_New();
413 if (container == NULL) {
414 flag_error(self);
415 return;
416 }
417 for (i = 0; i < max; i += 2) {
418 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
419 PyObject *v;
420 if (n == NULL) {
421 flag_error(self);
422 Py_DECREF(container);
423 return;
424 }
425 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
426 if (v == NULL) {
427 flag_error(self);
428 Py_DECREF(container);
429 Py_DECREF(n);
430 return;
431 }
432 if (self->ordered_attributes) {
433 PyList_SET_ITEM(container, i, n);
434 PyList_SET_ITEM(container, i+1, v);
435 }
436 else if (PyDict_SetItem(container, n, v)) {
437 flag_error(self);
438 Py_DECREF(n);
439 Py_DECREF(v);
440 return;
441 }
442 else {
443 Py_DECREF(n);
444 Py_DECREF(v);
445 }
446 }
447 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
448 if (args == NULL) {
449 Py_DECREF(container);
450 return;
451 }
452 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000453 self->in_callback = 1;
454 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000455 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000456 self->in_callback = 0;
457 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000458 if (rv == NULL) {
459 flag_error(self);
460 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000461 }
Fred Drake85d835f2001-02-08 15:39:08 +0000462 Py_DECREF(rv);
463 }
464}
465
466#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
467 RETURN, GETUSERDATA) \
468static RC \
469my_##NAME##Handler PARAMS {\
470 xmlparseobject *self = GETUSERDATA ; \
471 PyObject *args = NULL; \
472 PyObject *rv = NULL; \
473 INIT \
474\
475 if (self->handlers[NAME] \
476 && self->handlers[NAME] != Py_None) { \
477 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000478 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000479 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000480 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
481 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000482 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000483 Py_DECREF(args); \
484 if (rv == NULL) { \
485 flag_error(self); \
486 return RETURN; \
487 } \
488 CONVERSION \
489 Py_DECREF(rv); \
490 } \
491 return RETURN; \
492}
493
Fred Drake6f987622000-08-25 18:03:30 +0000494#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
495 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
496 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000497
Fred Drake6f987622000-08-25 18:03:30 +0000498#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
499 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
500 rc = PyInt_AsLong(rv);, rc, \
501 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000502
Fred Drake6f987622000-08-25 18:03:30 +0000503VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000504 (void *userData, const XML_Char *name),
505 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000506
Fred Drake6f987622000-08-25 18:03:30 +0000507VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000508 (void *userData,
509 const XML_Char *target,
510 const XML_Char *data),
511 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000512
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000513#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000514VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000515 (void *userData, const XML_Char *data, int len),
516 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000517#else
Fred Drake6f987622000-08-25 18:03:30 +0000518VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000519 (void *userData, const XML_Char *data, int len),
520 ("(N)", (self->returns_unicode
521 ? conv_string_len_to_unicode(data,len)
522 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000523#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000524
Fred Drake6f987622000-08-25 18:03:30 +0000525VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000526 (void *userData,
527 const XML_Char *entityName,
528 const XML_Char *base,
529 const XML_Char *systemId,
530 const XML_Char *publicId,
531 const XML_Char *notationName),
532 ("(O&O&O&O&O&)",
533 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
534 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
535 STRING_CONV_FUNC,notationName))
536
537#if EXPAT_VERSION >= 0x015f00
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000538#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000539VOID_HANDLER(EntityDecl,
540 (void *userData,
541 const XML_Char *entityName,
542 int is_parameter_entity,
543 const XML_Char *value,
544 int value_length,
545 const XML_Char *base,
546 const XML_Char *systemId,
547 const XML_Char *publicId,
548 const XML_Char *notationName),
549 ("O&iNO&O&O&O&",
550 STRING_CONV_FUNC,entityName, is_parameter_entity,
551 conv_string_len_to_utf8(value, value_length),
552 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
553 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
554#else
555VOID_HANDLER(EntityDecl,
556 (void *userData,
557 const XML_Char *entityName,
558 int is_parameter_entity,
559 const XML_Char *value,
560 int value_length,
561 const XML_Char *base,
562 const XML_Char *systemId,
563 const XML_Char *publicId,
564 const XML_Char *notationName),
565 ("O&iNO&O&O&O&",
566 STRING_CONV_FUNC,entityName, is_parameter_entity,
567 (self->returns_unicode
568 ? conv_string_len_to_unicode(value, value_length)
569 : conv_string_len_to_utf8(value, value_length)),
570 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
571 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
572#endif
573
574VOID_HANDLER(XmlDecl,
575 (void *userData,
576 const XML_Char *version,
577 const XML_Char *encoding,
578 int standalone),
579 ("(O&O&i)",
580 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
581 standalone))
582
583static PyObject *
584conv_content_model(XML_Content * const model,
585 PyObject *(*conv_string)(XML_Char *))
586{
587 PyObject *result = NULL;
588 PyObject *children = PyTuple_New(model->numchildren);
589 int i;
590
591 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000592 assert(model->numchildren < INT_MAX);
593 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000594 PyObject *child = conv_content_model(&model->children[i],
595 conv_string);
596 if (child == NULL) {
597 Py_XDECREF(children);
598 return NULL;
599 }
600 PyTuple_SET_ITEM(children, i, child);
601 }
602 result = Py_BuildValue("(iiO&N)",
603 model->type, model->quant,
604 conv_string,model->name, children);
605 }
606 return result;
607}
608
609static PyObject *
610conv_content_model_utf8(XML_Content * const model)
611{
612 return conv_content_model(model, conv_string_to_utf8);
613}
614
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000615#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000616static PyObject *
617conv_content_model_unicode(XML_Content * const model)
618{
619 return conv_content_model(model, conv_string_to_unicode);
620}
621
622VOID_HANDLER(ElementDecl,
623 (void *userData,
624 const XML_Char *name,
625 XML_Content *model),
626 ("O&O&",
627 STRING_CONV_FUNC,name,
628 (self->returns_unicode ? conv_content_model_unicode
629 : conv_content_model_utf8),model))
630#else
631VOID_HANDLER(ElementDecl,
632 (void *userData,
633 const XML_Char *name,
634 XML_Content *model),
635 ("O&O&",
636 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
637#endif
638
639VOID_HANDLER(AttlistDecl,
640 (void *userData,
641 const XML_Char *elname,
642 const XML_Char *attname,
643 const XML_Char *att_type,
644 const XML_Char *dflt,
645 int isrequired),
646 ("(O&O&O&O&i)",
647 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
648 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
649 isrequired))
650#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000651
Fred Drake6f987622000-08-25 18:03:30 +0000652VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000653 (void *userData,
654 const XML_Char *notationName,
655 const XML_Char *base,
656 const XML_Char *systemId,
657 const XML_Char *publicId),
658 ("(O&O&O&O&)",
659 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
660 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000661
Fred Drake6f987622000-08-25 18:03:30 +0000662VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000663 (void *userData,
664 const XML_Char *prefix,
665 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000666 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000667
Fred Drake6f987622000-08-25 18:03:30 +0000668VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000669 (void *userData,
670 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000671 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000672
Fred Drake6f987622000-08-25 18:03:30 +0000673VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000675 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000676
Fred Drake6f987622000-08-25 18:03:30 +0000677VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000679 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000680
Fred Drake6f987622000-08-25 18:03:30 +0000681VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000683 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000684
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000685#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000686VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000687 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000688 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000689
Fred Drake6f987622000-08-25 18:03:30 +0000690VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000691 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000692 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000693#else
Fred Drake6f987622000-08-25 18:03:30 +0000694VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000695 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000696 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000697 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000698 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000699
Fred Drake6f987622000-08-25 18:03:30 +0000700VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000702 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000703 ? 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#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000706
Fred Drake6f987622000-08-25 18:03:30 +0000707INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000708 (void *userData),
709 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000710
Fred Drake6f987622000-08-25 18:03:30 +0000711RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000712 (XML_Parser parser,
713 const XML_Char *context,
714 const XML_Char *base,
715 const XML_Char *systemId,
716 const XML_Char *publicId),
717 int rc=0;,
718 ("(O&O&O&O&)",
719 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000720 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
721 rc = PyInt_AsLong(rv);, rc,
722 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000723
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000724/* XXX UnknownEncodingHandler */
725
Fred Drake85d835f2001-02-08 15:39:08 +0000726#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000727VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000728 (void *userData, const XML_Char *doctypeName),
729 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
730 Py_None, Py_None, -1))
731#elif EXPAT_VERSION >= 0x015f00
732VOID_HANDLER(StartDoctypeDecl,
733 (void *userData, const XML_Char *doctypeName,
734 const XML_Char *sysid, const XML_Char *pubid,
735 int has_internal_subset),
736 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
737 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
738 has_internal_subset))
739#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000740
Fred Drake85d835f2001-02-08 15:39:08 +0000741#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000742VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000743#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000744
Fred Drake85d835f2001-02-08 15:39:08 +0000745#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000746VOID_HANDLER(ExternalParsedEntityDecl,
747 (void *userData, const XML_Char *entityName,
748 const XML_Char *base, const XML_Char *systemId,
749 const XML_Char *publicId),
750 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
751 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
752 STRING_CONV_FUNC, publicId))
753
754VOID_HANDLER(InternalParsedEntityDecl,
755 (void *userData, const XML_Char *entityName,
756 const XML_Char *replacementText, int replacementTextLength),
757 ("(O&O&i)", STRING_CONV_FUNC, entityName,
758 STRING_CONV_FUNC, replacementText, replacementTextLength))
759
Fred Drake85d835f2001-02-08 15:39:08 +0000760#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000761
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762/* ---------------------------------------------------------------- */
763
764static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000765"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000766Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000767
768static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000769xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770{
Fred Drake0582df92000-07-12 04:49:00 +0000771 char *s;
772 int slen;
773 int isFinal = 0;
774 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000775
Fred Drake0582df92000-07-12 04:49:00 +0000776 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
777 return NULL;
778 rv = XML_Parse(self->itself, s, slen, isFinal);
779 if (PyErr_Occurred()) {
780 return NULL;
781 }
782 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000783 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000784 }
785 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786}
787
Fred Drakeca1f4262000-09-21 20:10:23 +0000788/* File reading copied from cPickle */
789
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000790#define BUF_SIZE 2048
791
Fred Drake0582df92000-07-12 04:49:00 +0000792static int
793readinst(char *buf, int buf_size, PyObject *meth)
794{
795 PyObject *arg = NULL;
796 PyObject *bytes = NULL;
797 PyObject *str = NULL;
798 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000799
Fred Drake676940b2000-09-22 15:21:31 +0000800 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000801 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000802
Fred Drakeca1f4262000-09-21 20:10:23 +0000803 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000804 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000805
Tim Peters954eef72000-09-22 06:01:11 +0000806 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000807
Fred Drakeca1f4262000-09-21 20:10:23 +0000808 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000809 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000810
Fred Drake0582df92000-07-12 04:49:00 +0000811 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000812 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000813 PyErr_Format(PyExc_TypeError,
814 "read() did not return a string object (type=%.400s)",
815 str->ob_type->tp_name);
816 goto finally;
817 }
818 len = PyString_GET_SIZE(str);
819 if (len > buf_size) {
820 PyErr_Format(PyExc_ValueError,
821 "read() returned too much data: "
822 "%i bytes requested, %i returned",
823 buf_size, len);
824 Py_DECREF(str);
825 goto finally;
826 }
827 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828finally:
Fred Drake0582df92000-07-12 04:49:00 +0000829 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000830 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000831 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000832}
833
834static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000835"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000836Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000837
838static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000839xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840{
Fred Drake0582df92000-07-12 04:49:00 +0000841 int rv = 1;
842 PyObject *f;
843 FILE *fp;
844 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000845
Fred Drake0582df92000-07-12 04:49:00 +0000846 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
847 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848
Fred Drake0582df92000-07-12 04:49:00 +0000849 if (PyFile_Check(f)) {
850 fp = PyFile_AsFile(f);
851 }
852 else{
853 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000854 readmethod = PyObject_GetAttrString(f, "read");
855 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000856 PyErr_Clear();
857 PyErr_SetString(PyExc_TypeError,
858 "argument must have 'read' attribute");
859 return 0;
860 }
861 }
862 for (;;) {
863 int bytes_read;
864 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
865 if (buf == NULL)
866 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000867
Fred Drake0582df92000-07-12 04:49:00 +0000868 if (fp) {
869 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
870 if (bytes_read < 0) {
871 PyErr_SetFromErrno(PyExc_IOError);
872 return NULL;
873 }
874 }
875 else {
876 bytes_read = readinst(buf, BUF_SIZE, readmethod);
877 if (bytes_read < 0)
878 return NULL;
879 }
880 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
881 if (PyErr_Occurred())
882 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000883
Fred Drake0582df92000-07-12 04:49:00 +0000884 if (!rv || bytes_read == 0)
885 break;
886 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000887 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000888 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000889 }
Fred Drake0582df92000-07-12 04:49:00 +0000890 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891}
892
893static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000894"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000895Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896
897static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000898xmlparse_SetBase(xmlparseobject *self, PyObject *args)
899{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000900 char *base;
901
Fred Drake0582df92000-07-12 04:49:00 +0000902 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000904 if (!XML_SetBase(self->itself, base)) {
905 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000906 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907 Py_INCREF(Py_None);
908 return Py_None;
909}
910
911static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000912"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000913Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000914
915static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000916xmlparse_GetBase(xmlparseobject *self, PyObject *args)
917{
918 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000919 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920
Fred Drake0582df92000-07-12 04:49:00 +0000921 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000922}
923
Fred Drakebd6101c2001-02-14 18:29:45 +0000924#if EXPAT_VERSION >= 0x015f00
925static char xmlparse_GetInputContext__doc__[] =
926"GetInputContext() -> string\n\
927Return the untranslated text of the input that caused the current event.\n\
928If the event was generated by a large amount of text (such as a start tag\n\
929for an element with many attributes), not all of the text may be available.";
930
931static PyObject *
932xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
933{
934 PyObject *result = NULL;
935
936 if (PyArg_ParseTuple(args, ":GetInputContext")) {
937 if (self->in_callback) {
938 int offset, size;
939 const char *buffer
940 = XML_GetInputContext(self->itself, &offset, &size);
941
942 if (buffer != NULL)
943 result = PyString_FromStringAndSize(buffer + offset, size);
944 else {
945 result = Py_None;
946 Py_INCREF(result);
947 }
948 }
949 else {
950 result = Py_None;
951 Py_INCREF(result);
952 }
953 }
954 return result;
955}
956#endif
957
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000958static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000959"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000960Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000961information passed to the ExternalEntityRefHandler.";
962
963static PyObject *
964xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
965{
966 char *context;
967 char *encoding = NULL;
968 xmlparseobject *new_parser;
969 int i;
970
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000971 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000972 &context, &encoding)) {
973 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000974 }
975
976#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
977 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000978#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000979#ifndef Py_TPFLAGS_HAVE_GC
980 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000981 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000982#else
983 /* Python versions 2.2 and later */
984 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
985#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000986#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000987
988 if (new_parser == NULL)
989 return NULL;
990 new_parser->returns_unicode = self->returns_unicode;
991 new_parser->ordered_attributes = self->ordered_attributes;
992 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000993 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000994 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000995 encoding);
996 new_parser->handlers = 0;
Martin v. Löwis894258c2001-09-23 10:20:10 +0000997#ifdef Py_TPFLAGS_HAVE_GC
998 PyObject_GC_Track(new_parser);
999#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001000 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001001#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001002
1003 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001004 Py_DECREF(new_parser);
1005 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001006 }
1007
1008 XML_SetUserData(new_parser->itself, (void *)new_parser);
1009
1010 /* allocate and clear handlers first */
1011 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001012 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001013
1014 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001015 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001016 Py_DECREF(new_parser);
1017 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001018 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001019 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001020
1021 /* then copy handlers from self */
1022 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001023 if (self->handlers[i]) {
1024 Py_INCREF(self->handlers[i]);
1025 new_parser->handlers[i] = self->handlers[i];
1026 handler_info[i].setter(new_parser->itself,
1027 handler_info[i].handler);
1028 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001029 }
Fred Drake28adf522000-09-24 22:07:59 +00001030 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001031}
1032
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001033#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001034
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001035static char xmlparse_SetParamEntityParsing__doc__[] =
1036"SetParamEntityParsing(flag) -> success\n\
1037Controls parsing of parameter entities (including the external DTD\n\
1038subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1039XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1040XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1041was successful.";
1042
1043static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001044xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001045{
Fred Drake85d835f2001-02-08 15:39:08 +00001046 int flag;
1047 if (!PyArg_ParseTuple(args, "i", &flag))
1048 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001049 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001050 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001051}
1052
Fred Drake85d835f2001-02-08 15:39:08 +00001053#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001054
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001055static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001056 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001057 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001058 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001059 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001060 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001061 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001062 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001063 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001064 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1065 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001066#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001067 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1068 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1069#endif
1070#if EXPAT_VERSION >= 0x015f00
1071 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1072 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001074 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001075};
1076
1077/* ---------- */
1078
1079
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001080#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001081
1082/*
1083 pyexpat international encoding support.
1084 Make it as simple as possible.
1085*/
1086
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001087static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001088PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001089
1090static void
1091init_template_buffer(void)
1092{
1093 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001094 for (i = 0; i < 256; i++) {
1095 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001096 }
Fred Drakebb66a202001-03-01 20:48:17 +00001097 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001098}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099
1100int
1101PyUnknownEncodingHandler(void *encodingHandlerData,
1102const XML_Char *name,
1103XML_Encoding * info)
1104{
Fred Drakebb66a202001-03-01 20:48:17 +00001105 PyUnicodeObject *_u_string = NULL;
1106 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107 int i;
1108
Fred Drakebb66a202001-03-01 20:48:17 +00001109 /* Yes, supports only 8bit encodings */
1110 _u_string = (PyUnicodeObject *)
1111 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001112
Fred Drakebb66a202001-03-01 20:48:17 +00001113 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001114 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001115
Fred Drakebb66a202001-03-01 20:48:17 +00001116 for (i = 0; i < 256; i++) {
1117 /* Stupid to access directly, but fast */
1118 Py_UNICODE c = _u_string->str[i];
1119 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001120 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001121 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001122 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001123 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001124
1125 info->data = NULL;
1126 info->convert = NULL;
1127 info->release = NULL;
1128 result=1;
1129
1130 Py_DECREF(_u_string);
1131 return result;
1132}
1133
1134#endif
1135
1136static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001137newxmlparseobject(char *encoding, char *namespace_separator)
1138{
1139 int i;
1140 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001141
1142#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001143 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1144 if (self == NULL)
1145 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001146
Fred Drake0582df92000-07-12 04:49:00 +00001147 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001148#else
Fred Drake0582df92000-07-12 04:49:00 +00001149 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001150#ifdef Py_TPFLAGS_HAVE_GC
1151 /* Code for versions 2.2 and later */
1152 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1153#else
Fred Drake0582df92000-07-12 04:49:00 +00001154 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001155#endif
Fred Drake0582df92000-07-12 04:49:00 +00001156 if (self == NULL)
1157 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001158
Fred Drake0582df92000-07-12 04:49:00 +00001159 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001160#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001161 self->ordered_attributes = 0;
1162 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001163 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001164 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001165 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001166 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1167 }
Fred Drake85d835f2001-02-08 15:39:08 +00001168 else {
Fred Drake0582df92000-07-12 04:49:00 +00001169 self->itself = XML_ParserCreate(encoding);
1170 }
Martin v. Löwis894258c2001-09-23 10:20:10 +00001171#ifdef Py_TPFLAGS_HAVE_GC
1172 PyObject_GC_Track(self);
1173#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001174 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001175#endif
Fred Drake0582df92000-07-12 04:49:00 +00001176 if (self->itself == NULL) {
1177 PyErr_SetString(PyExc_RuntimeError,
1178 "XML_ParserCreate failed");
1179 Py_DECREF(self);
1180 return NULL;
1181 }
1182 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001183#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001184 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1185#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001186
Fred Drake0582df92000-07-12 04:49:00 +00001187 for(i = 0; handler_info[i].name != NULL; i++)
1188 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189
Fred Drake0582df92000-07-12 04:49:00 +00001190 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001191 if (!self->handlers){
1192 Py_DECREF(self);
1193 return PyErr_NoMemory();
1194 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001195 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001196
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001197 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001198}
1199
1200
1201static void
Fred Drake0582df92000-07-12 04:49:00 +00001202xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001203{
Fred Drake0582df92000-07-12 04:49:00 +00001204 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001205#ifdef Py_TPFLAGS_HAVE_GC
1206 PyObject_GC_UnTrack(self);
1207#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001208 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001209#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001210 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001211 XML_ParserFree(self->itself);
1212 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001213
Fred Drake85d835f2001-02-08 15:39:08 +00001214 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001215 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001216 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001217 temp = self->handlers[i];
1218 self->handlers[i] = NULL;
1219 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001220 }
1221 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001222 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001223#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001224 /* Code for versions before 1.6 */
1225 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001226#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001227#ifndef Py_TPFLAGS_HAVE_GC
1228 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001229 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001230#else
1231 /* Code for versions 2.2 and later. */
1232 PyObject_GC_Del(self);
1233#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001234#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001235}
1236
Fred Drake0582df92000-07-12 04:49:00 +00001237static int
1238handlername2int(const char *name)
1239{
1240 int i;
1241 for (i=0; handler_info[i].name != NULL; i++) {
1242 if (strcmp(name, handler_info[i].name) == 0) {
1243 return i;
1244 }
1245 }
1246 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001247}
1248
1249static PyObject *
1250xmlparse_getattr(xmlparseobject *self, char *name)
1251{
Fred Drake0582df92000-07-12 04:49:00 +00001252 int handlernum;
1253 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001254 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001255 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001256 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001257 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001258 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001259 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001260 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1261 if (strcmp(name, "ordered_attributes") == 0)
1262 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001263 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001264 return PyInt_FromLong((long) self->returns_unicode);
1265 if (strcmp(name, "specified_attributes") == 0)
1266 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001267
Fred Drake0582df92000-07-12 04:49:00 +00001268 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269
Fred Drake0582df92000-07-12 04:49:00 +00001270 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1271 Py_INCREF(self->handlers[handlernum]);
1272 return self->handlers[handlernum];
1273 }
1274 if (strcmp(name, "__members__") == 0) {
1275 int i;
1276 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001277 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001278 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001279 }
1280 PyList_Append(rc, PyString_FromString("ErrorCode"));
1281 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1282 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1283 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001284 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001285 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001286 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001287
Fred Drake0582df92000-07-12 04:49:00 +00001288 return rc;
1289 }
1290 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001291}
1292
Fred Drake6f987622000-08-25 18:03:30 +00001293static int
1294sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001295{
1296 int handlernum = handlername2int(name);
1297 if (handlernum != -1) {
1298 Py_INCREF(v);
1299 Py_XDECREF(self->handlers[handlernum]);
1300 self->handlers[handlernum] = v;
1301 handler_info[handlernum].setter(self->itself,
1302 handler_info[handlernum].handler);
1303 return 1;
1304 }
1305 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001306}
1307
1308static int
Fred Drake6f987622000-08-25 18:03:30 +00001309xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001310{
Fred Drake6f987622000-08-25 18:03:30 +00001311 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001312 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001313 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1314 return -1;
1315 }
Fred Drake85d835f2001-02-08 15:39:08 +00001316 if (strcmp(name, "ordered_attributes") == 0) {
1317 if (PyObject_IsTrue(v))
1318 self->ordered_attributes = 1;
1319 else
1320 self->ordered_attributes = 0;
1321 return 0;
1322 }
Fred Drake6f987622000-08-25 18:03:30 +00001323 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001324 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001325#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +00001326 PyErr_SetString(PyExc_ValueError,
1327 "Cannot return Unicode strings in Python 1.5");
1328 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001329#else
Fred Drake6f987622000-08-25 18:03:30 +00001330 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001331#endif
Fred Drake6f987622000-08-25 18:03:30 +00001332 }
1333 else
1334 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001335 return 0;
1336 }
1337 if (strcmp(name, "specified_attributes") == 0) {
1338 if (PyObject_IsTrue(v))
1339 self->specified_attributes = 1;
1340 else
1341 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001342 return 0;
1343 }
1344 if (sethandler(self, name, v)) {
1345 return 0;
1346 }
1347 PyErr_SetString(PyExc_AttributeError, name);
1348 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001349}
1350
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001351#ifdef WITH_CYCLE_GC
1352static int
1353xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1354{
Fred Drakecde79132001-04-25 16:01:30 +00001355 int i, err;
1356 for (i = 0; handler_info[i].name != NULL; i++) {
1357 if (!op->handlers[i])
1358 continue;
1359 err = visit(op->handlers[i], arg);
1360 if (err)
1361 return err;
1362 }
1363 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001364}
1365
1366static int
1367xmlparse_clear(xmlparseobject *op)
1368{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001369 clear_handlers(op, 0);
Fred Drakecde79132001-04-25 16:01:30 +00001370 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001371}
1372#endif
1373
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001374static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001375"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001376
1377static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001378 PyObject_HEAD_INIT(NULL)
1379 0, /*ob_size*/
1380 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001381 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001382 0, /*tp_itemsize*/
1383 /* methods */
1384 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1385 (printfunc)0, /*tp_print*/
1386 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1387 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1388 (cmpfunc)0, /*tp_compare*/
1389 (reprfunc)0, /*tp_repr*/
1390 0, /*tp_as_number*/
1391 0, /*tp_as_sequence*/
1392 0, /*tp_as_mapping*/
1393 (hashfunc)0, /*tp_hash*/
1394 (ternaryfunc)0, /*tp_call*/
1395 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001396 0, /* tp_getattro */
1397 0, /* tp_setattro */
1398 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001399#ifdef Py_TPFLAGS_HAVE_GC
1400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1401#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001403#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001404 Xmlparsetype__doc__, /* Documentation string */
1405#ifdef WITH_CYCLE_GC
1406 (traverseproc)xmlparse_traverse, /* tp_traverse */
1407 (inquiry)xmlparse_clear /* tp_clear */
1408#else
1409 0, 0
1410#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001411};
1412
1413/* End of code for xmlparser objects */
1414/* -------------------------------------------------------- */
1415
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001416static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001417"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1418Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001419
1420static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001421pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1422{
Fred Drakecde79132001-04-25 16:01:30 +00001423 char *encoding = NULL;
1424 char *namespace_separator = NULL;
1425 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001426
Fred Drakecde79132001-04-25 16:01:30 +00001427 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1428 &encoding, &namespace_separator))
1429 return NULL;
1430 if (namespace_separator != NULL
1431 && strlen(namespace_separator) > 1) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "namespace_separator must be at most one"
1434 " character, omitted, or None");
1435 return NULL;
1436 }
1437 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001438}
1439
1440static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001441"ErrorString(errno) -> string\n\
1442Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001443
1444static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001445pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001446{
Fred Drake0582df92000-07-12 04:49:00 +00001447 long code = 0;
1448
1449 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1450 return NULL;
1451 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001452}
1453
1454/* List of methods defined in the module */
1455
1456static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001457 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1458 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1459 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1460 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001461
Fred Drake0582df92000-07-12 04:49:00 +00001462 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001463};
1464
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001465/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001466
1467static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001468"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001469
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001470#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001471
1472/* 1.5 compatibility: PyModule_AddObject */
1473static int
1474PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1475{
Fred Drakecde79132001-04-25 16:01:30 +00001476 PyObject *dict;
1477 if (!PyModule_Check(m) || o == NULL)
1478 return -1;
1479 dict = PyModule_GetDict(m);
1480 if (dict == NULL)
1481 return -1;
1482 if (PyDict_SetItemString(dict, name, o))
1483 return -1;
1484 Py_DECREF(o);
1485 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001486}
1487
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001488int
1489PyModule_AddIntConstant(PyObject *m, char *name, long value)
1490{
Fred Drakecde79132001-04-25 16:01:30 +00001491 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001492}
1493
Fred Drakea77254a2000-09-29 19:23:29 +00001494static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001495PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1496{
Fred Drakecde79132001-04-25 16:01:30 +00001497 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001498}
1499
1500#endif
1501
Fred Drake4113b132001-03-24 19:58:26 +00001502
1503/* Return a Python string that represents the version number without the
1504 * extra cruft added by revision control, even if the right options were
1505 * given to the "cvs export" command to make it not include the extra
1506 * cruft.
1507 */
1508static PyObject *
1509get_version_string(void)
1510{
1511 static char *rcsid = "$Revision$";
1512 char *rev = rcsid;
1513 int i = 0;
1514
1515 while (!isdigit(*rev))
1516 ++rev;
1517 while (rev[i] != ' ' && rev[i] != '\0')
1518 ++i;
1519
1520 return PyString_FromStringAndSize(rev, i);
1521}
1522
Fred Drakecde79132001-04-25 16:01:30 +00001523/* Initialization function for the module */
1524
1525#ifndef MODULE_NAME
1526#define MODULE_NAME "pyexpat"
1527#endif
1528
1529#ifndef MODULE_INITFUNC
1530#define MODULE_INITFUNC initpyexpat
1531#endif
1532
1533void MODULE_INITFUNC(void); /* avoid compiler warnings */
1534
Fred Drake6f987622000-08-25 18:03:30 +00001535DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001536MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001537{
1538 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001539 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001540 PyObject *errors_module;
1541 PyObject *modelmod_name;
1542 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001543 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001544
Fred Drake6f987622000-08-25 18:03:30 +00001545 if (errmod_name == NULL)
1546 return;
Fred Drakecde79132001-04-25 16:01:30 +00001547 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001548 if (modelmod_name == NULL)
1549 return;
Fred Drake6f987622000-08-25 18:03:30 +00001550
Fred Drake0582df92000-07-12 04:49:00 +00001551 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001552
Fred Drake0582df92000-07-12 04:49:00 +00001553 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001554 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001555 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001556
Fred Drake0582df92000-07-12 04:49:00 +00001557 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001558 if (ErrorObject == NULL) {
1559 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001560 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001561 if (ErrorObject == NULL)
1562 return;
1563 }
1564 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001565 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001566 Py_INCREF(ErrorObject);
1567 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001568 Py_INCREF(&Xmlparsetype);
1569 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570
Fred Drake4113b132001-03-24 19:58:26 +00001571 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001572#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001573 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1574 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001575 {
1576 XML_Expat_Version info = XML_ExpatVersionInfo();
1577 PyModule_AddObject(m, "version_info",
1578 Py_BuildValue("(iii)", info.major,
1579 info.minor, info.micro));
1580 }
Fred Drake738293d2000-12-21 17:25:07 +00001581#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001582#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001583 init_template_buffer();
1584#endif
Fred Drake0582df92000-07-12 04:49:00 +00001585 /* XXX When Expat supports some way of figuring out how it was
1586 compiled, this should check and set native_encoding
1587 appropriately.
1588 */
Fred Drake93adb692000-09-23 04:55:48 +00001589 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001590
Fred Drake85d835f2001-02-08 15:39:08 +00001591 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001592 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001593 errors_module = PyDict_GetItem(d, errmod_name);
1594 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001595 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001596 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001597 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001598 /* gives away the reference to errors_module */
1599 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001600 }
1601 }
Fred Drake6f987622000-08-25 18:03:30 +00001602 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001603 model_module = PyDict_GetItem(d, modelmod_name);
1604 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001605 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001606 if (model_module != NULL) {
1607 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1608 /* gives away the reference to model_module */
1609 PyModule_AddObject(m, "model", model_module);
1610 }
1611 }
1612 Py_DECREF(modelmod_name);
1613 if (errors_module == NULL || model_module == NULL)
1614 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001615 return;
1616
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001617#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001618 PyModule_AddStringConstant(errors_module, #name, \
1619 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001620
Fred Drake0582df92000-07-12 04:49:00 +00001621 MYCONST(XML_ERROR_NO_MEMORY);
1622 MYCONST(XML_ERROR_SYNTAX);
1623 MYCONST(XML_ERROR_NO_ELEMENTS);
1624 MYCONST(XML_ERROR_INVALID_TOKEN);
1625 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1626 MYCONST(XML_ERROR_PARTIAL_CHAR);
1627 MYCONST(XML_ERROR_TAG_MISMATCH);
1628 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1629 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1630 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1631 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1632 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1633 MYCONST(XML_ERROR_ASYNC_ENTITY);
1634 MYCONST(XML_ERROR_BAD_CHAR_REF);
1635 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1636 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1637 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1638 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1639 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001640 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1641 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1642 MYCONST(XML_ERROR_NOT_STANDALONE);
1643
Fred Drake85d835f2001-02-08 15:39:08 +00001644 PyModule_AddStringConstant(errors_module, "__doc__",
1645 "Constants used to describe error conditions.");
1646
Fred Drake93adb692000-09-23 04:55:48 +00001647#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001648
1649#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001650#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001651 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1652 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1653 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001654#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001655#endif
1656
Fred Drake85d835f2001-02-08 15:39:08 +00001657#if EXPAT_VERSION >= 0x015f00
1658#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1659 PyModule_AddStringConstant(model_module, "__doc__",
1660 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001661
Fred Drake85d835f2001-02-08 15:39:08 +00001662 MYCONST(XML_CTYPE_EMPTY);
1663 MYCONST(XML_CTYPE_ANY);
1664 MYCONST(XML_CTYPE_MIXED);
1665 MYCONST(XML_CTYPE_NAME);
1666 MYCONST(XML_CTYPE_CHOICE);
1667 MYCONST(XML_CTYPE_SEQ);
1668
1669 MYCONST(XML_CQUANT_NONE);
1670 MYCONST(XML_CQUANT_OPT);
1671 MYCONST(XML_CQUANT_REP);
1672 MYCONST(XML_CQUANT_PLUS);
1673#undef MYCONST
1674#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001675}
1676
Fred Drake6f987622000-08-25 18:03:30 +00001677static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001678clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001679{
Fred Drakecde79132001-04-25 16:01:30 +00001680 int i = 0;
1681 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001682
Fred Drakecde79132001-04-25 16:01:30 +00001683 for (; handler_info[i].name!=NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001684 if (initial)
1685 self->handlers[i]=NULL;
1686 else {
Fred Drakecde79132001-04-25 16:01:30 +00001687 temp = self->handlers[i];
1688 self->handlers[i] = NULL;
1689 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001690 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001691 }
Fred Drakecde79132001-04-25 16:01:30 +00001692 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001693}
1694
Fred Drake6f987622000-08-25 18:03:30 +00001695typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001696
Fred Drake6f987622000-08-25 18:03:30 +00001697static void
1698pyxml_UpdatePairedHandlers(xmlparseobject *self,
1699 int startHandler,
1700 int endHandler,
1701 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001702{
Fred Drakecde79132001-04-25 16:01:30 +00001703 void *start_handler = NULL;
1704 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001705
Fred Drake0582df92000-07-12 04:49:00 +00001706 if (self->handlers[startHandler]
Fred Drakecde79132001-04-25 16:01:30 +00001707 && self->handlers[endHandler] != Py_None) {
1708 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001709 }
1710 if (self->handlers[EndElement]
Fred Drakecde79132001-04-25 16:01:30 +00001711 && self->handlers[EndElement] != Py_None) {
1712 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001713 }
1714 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001715}
1716
Fred Drake6f987622000-08-25 18:03:30 +00001717static void
1718pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001719{
1720 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1721 StartElement, EndElement,
1722 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001723}
1724
Fred Drake6f987622000-08-25 18:03:30 +00001725static void
1726pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001727{
1728 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1729 StartElement, EndElement,
1730 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001731}
1732
Fred Drake6f987622000-08-25 18:03:30 +00001733static void
1734pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001735{
1736 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1737 StartNamespaceDecl, EndNamespaceDecl,
1738 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001739}
1740
Fred Drake6f987622000-08-25 18:03:30 +00001741static void
1742pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001743{
1744 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1745 StartNamespaceDecl, EndNamespaceDecl,
1746 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001747}
1748
Fred Drake6f987622000-08-25 18:03:30 +00001749static void
1750pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001751{
1752 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1753 StartCdataSection, EndCdataSection,
1754 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001755}
1756
Fred Drake6f987622000-08-25 18:03:30 +00001757static void
1758pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001759{
1760 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1761 StartCdataSection, EndCdataSection,
1762 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001763}
1764
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001765#if EXPAT_VERSION >= 0x010200
1766
1767static void
1768pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1769{
1770 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1771 StartDoctypeDecl, EndDoctypeDecl,
1772 (pairsetter)XML_SetDoctypeDeclHandler);
1773}
1774
1775static void
1776pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1777{
1778 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1779 StartDoctypeDecl, EndDoctypeDecl,
1780 (pairsetter)XML_SetDoctypeDeclHandler);
1781}
1782
1783#endif
1784
Fred Drake0582df92000-07-12 04:49:00 +00001785statichere struct HandlerInfo handler_info[] = {
1786 {"StartElementHandler",
1787 pyxml_SetStartElementHandler,
1788 (xmlhandler)my_StartElementHandler},
1789 {"EndElementHandler",
1790 pyxml_SetEndElementHandler,
1791 (xmlhandler)my_EndElementHandler},
1792 {"ProcessingInstructionHandler",
1793 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1794 (xmlhandler)my_ProcessingInstructionHandler},
1795 {"CharacterDataHandler",
1796 (xmlhandlersetter)XML_SetCharacterDataHandler,
1797 (xmlhandler)my_CharacterDataHandler},
1798 {"UnparsedEntityDeclHandler",
1799 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1800 (xmlhandler)my_UnparsedEntityDeclHandler },
1801 {"NotationDeclHandler",
1802 (xmlhandlersetter)XML_SetNotationDeclHandler,
1803 (xmlhandler)my_NotationDeclHandler },
1804 {"StartNamespaceDeclHandler",
1805 pyxml_SetStartNamespaceDeclHandler,
1806 (xmlhandler)my_StartNamespaceDeclHandler },
1807 {"EndNamespaceDeclHandler",
1808 pyxml_SetEndNamespaceDeclHandler,
1809 (xmlhandler)my_EndNamespaceDeclHandler },
1810 {"CommentHandler",
1811 (xmlhandlersetter)XML_SetCommentHandler,
1812 (xmlhandler)my_CommentHandler},
1813 {"StartCdataSectionHandler",
1814 pyxml_SetStartCdataSection,
1815 (xmlhandler)my_StartCdataSectionHandler},
1816 {"EndCdataSectionHandler",
1817 pyxml_SetEndCdataSection,
1818 (xmlhandler)my_EndCdataSectionHandler},
1819 {"DefaultHandler",
1820 (xmlhandlersetter)XML_SetDefaultHandler,
1821 (xmlhandler)my_DefaultHandler},
1822 {"DefaultHandlerExpand",
1823 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1824 (xmlhandler)my_DefaultHandlerExpandHandler},
1825 {"NotStandaloneHandler",
1826 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1827 (xmlhandler)my_NotStandaloneHandler},
1828 {"ExternalEntityRefHandler",
1829 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1830 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001831#if EXPAT_VERSION >= 0x010200
1832 {"StartDoctypeDeclHandler",
1833 pyxml_SetStartDoctypeDeclHandler,
1834 (xmlhandler)my_StartDoctypeDeclHandler},
1835 {"EndDoctypeDeclHandler",
1836 pyxml_SetEndDoctypeDeclHandler,
1837 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001838#endif
1839#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001840 {"ExternalParsedEntityDeclHandler",
1841 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1842 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1843 {"InternalParsedEntityDeclHandler",
1844 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1845 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001846#endif
1847#if EXPAT_VERSION >= 0x015f00
1848 {"EntityDeclHandler",
1849 (xmlhandlersetter)XML_SetEntityDeclHandler,
1850 (xmlhandler)my_EntityDeclHandler},
1851 {"XmlDeclHandler",
1852 (xmlhandlersetter)XML_SetXmlDeclHandler,
1853 (xmlhandler)my_XmlDeclHandler},
1854 {"ElementDeclHandler",
1855 (xmlhandlersetter)XML_SetElementDeclHandler,
1856 (xmlhandler)my_ElementDeclHandler},
1857 {"AttlistDeclHandler",
1858 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1859 (xmlhandler)my_AttlistDeclHandler},
1860#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001861
Fred Drake0582df92000-07-12 04:49:00 +00001862 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001863};