blob: 69105bdf457f4a8e822979315f7bfc2000797465 [file] [log] [blame]
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001#include "Python.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002#include "compile.h"
3#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00004#ifdef HAVE_EXPAT_H
5#include "expat.h"
6#else
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00007#include "xmlparse.h"
Fred Drakea77254a2000-09-29 19:23:29 +00008#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00009
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010#ifdef XML_MAJOR_VERSION
11#define EXPAT_VERSION (0x10000*XML_MAJOR_VERSION+0x100*XML_MINOR_VERSION+XML_MINOR_VERSION)
12#else
13#ifndef EXPAT_VERSION
14/* Assume Expat 1.1 unless told otherwise */
15#define EXPAT_VERSION 0x010100
16#endif
17#endif
18
19#ifndef PyGC_HEAD_SIZE
20#define PyGC_HEAD_SIZE 0
21#define PyObject_GC_Init(x)
22#define PyObject_GC_Fini(m)
23#define Py_TPFLAGS_GC 0
24#endif
25
Fred Drake0582df92000-07-12 04:49:00 +000026enum HandlerTypes {
27 StartElement,
28 EndElement,
29 ProcessingInstruction,
30 CharacterData,
31 UnparsedEntityDecl,
32 NotationDecl,
33 StartNamespaceDecl,
34 EndNamespaceDecl,
35 Comment,
36 StartCdataSection,
37 EndCdataSection,
38 Default,
39 DefaultHandlerExpand,
40 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000041 ExternalEntityRef,
42 StartDoctypeDecl,
43 EndDoctypeDecl,
44 ExternalParsedEntityDecl,
45 InternalParsedEntityDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000046};
47
48static PyObject *ErrorObject;
49
50/* ----------------------------------------------------- */
51
52/* Declarations for objects of type xmlparser */
53
54typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000055 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056
Fred Drake0582df92000-07-12 04:49:00 +000057 XML_Parser itself;
58 int returns_unicode; /* True if Unicode strings are returned;
59 if false, UTF-8 strings are returned */
60 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000061} xmlparseobject;
62
63staticforward PyTypeObject Xmlparsetype;
64
Fred Drake6f987622000-08-25 18:03:30 +000065typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000066typedef void* xmlhandler;
67
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000068struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000069 const char *name;
70 xmlhandlersetter setter;
71 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000072 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000073};
74
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000075staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000077/* Convert an array of attributes and their values into a Python dict */
78
Fred Drake0582df92000-07-12 04:49:00 +000079static PyObject *
80conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +000081{
Fred Drake0582df92000-07-12 04:49:00 +000082 PyObject *attrs_obj = NULL;
83 XML_Char **attrs_p, **attrs_k = NULL;
84 int attrs_len;
85 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000086
Fred Drake0582df92000-07-12 04:49:00 +000087 if ((attrs_obj = PyDict_New()) == NULL)
88 goto finally;
89 for (attrs_len = 0, attrs_p = atts;
90 *attrs_p;
91 attrs_p++, attrs_len++) {
92 if (attrs_len % 2) {
93 rv = PyString_FromString(*attrs_p);
94 if (!rv) {
95 Py_DECREF(attrs_obj);
96 attrs_obj = NULL;
97 goto finally;
98 }
99 if (PyDict_SetItemString(attrs_obj,
100 (char*)*attrs_k, rv) < 0) {
101 Py_DECREF(attrs_obj);
102 attrs_obj = NULL;
103 goto finally;
104 }
105 Py_DECREF(rv);
106 }
107 else
108 attrs_k = attrs_p;
109 }
110 finally:
111 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000112}
113
114#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake0582df92000-07-12 04:49:00 +0000115static PyObject *
116conv_atts_using_unicode(XML_Char **atts)
117{
Fred Drakeca1f4262000-09-21 20:10:23 +0000118 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000119 XML_Char **attrs_p, **attrs_k = NULL;
120 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000121
Fred Drake0582df92000-07-12 04:49:00 +0000122 if ((attrs_obj = PyDict_New()) == NULL)
123 goto finally;
124 for (attrs_len = 0, attrs_p = atts;
125 *attrs_p;
126 attrs_p++, attrs_len++) {
127 if (attrs_len % 2) {
128 PyObject *attr_str, *value_str;
129 const char *p = (const char *) (*attrs_k);
130 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
131 if (!attr_str) {
132 Py_DECREF(attrs_obj);
133 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000134 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000135 }
136 p = (const char *) *attrs_p;
137 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
138 if (!value_str) {
139 Py_DECREF(attrs_obj);
140 Py_DECREF(attr_str);
141 attrs_obj = NULL;
142 goto finally;
143 }
144 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
145 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000146 Py_DECREF(attr_str);
147 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000148 attrs_obj = NULL;
149 goto finally;
150 }
151 Py_DECREF(attr_str);
152 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000153 }
Fred Drake0582df92000-07-12 04:49:00 +0000154 else
155 attrs_k = attrs_p;
156 }
157 finally:
158 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000159}
160
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000161/* Convert a string of XML_Chars into a Unicode string.
162 Returns None if str is a null pointer. */
163
Fred Drake0582df92000-07-12 04:49:00 +0000164static PyObject *
165conv_string_to_unicode(XML_Char *str)
166{
167 /* XXX currently this code assumes that XML_Char is 8-bit,
168 and hence in UTF-8. */
169 /* UTF-8 from Expat, Unicode desired */
170 if (str == NULL) {
171 Py_INCREF(Py_None);
172 return Py_None;
173 }
174 return PyUnicode_DecodeUTF8((const char *)str,
175 strlen((const char *)str),
176 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000177}
178
Fred Drake0582df92000-07-12 04:49:00 +0000179static PyObject *
180conv_string_len_to_unicode(const XML_Char *str, int len)
181{
182 /* XXX currently this code assumes that XML_Char is 8-bit,
183 and hence in UTF-8. */
184 /* UTF-8 from Expat, Unicode desired */
185 if (str == NULL) {
186 Py_INCREF(Py_None);
187 return Py_None;
188 }
Fred Drake6f987622000-08-25 18:03:30 +0000189 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000190}
191#endif
192
193/* Convert a string of XML_Chars into an 8-bit Python string.
194 Returns None if str is a null pointer. */
195
Fred Drake6f987622000-08-25 18:03:30 +0000196static PyObject *
197conv_string_to_utf8(XML_Char *str)
198{
199 /* XXX currently this code assumes that XML_Char is 8-bit,
200 and hence in UTF-8. */
201 /* UTF-8 from Expat, UTF-8 desired */
202 if (str == NULL) {
203 Py_INCREF(Py_None);
204 return Py_None;
205 }
206 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000207}
208
Fred Drake6f987622000-08-25 18:03:30 +0000209static PyObject *
210conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000211{
Fred Drake6f987622000-08-25 18:03:30 +0000212 /* XXX currently this code assumes that XML_Char is 8-bit,
213 and hence in UTF-8. */
214 /* UTF-8 from Expat, UTF-8 desired */
215 if (str == NULL) {
216 Py_INCREF(Py_None);
217 return Py_None;
218 }
219 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000220}
221
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222/* Callback routines */
223
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000224static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000225
Fred Drake6f987622000-08-25 18:03:30 +0000226static void
227flag_error(xmlparseobject *self)
228{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000229 clear_handlers(self, 1);
230}
231
232static PyCodeObject*
233getcode(enum HandlerTypes slot, char* func_name, int lineno)
234{
235 PyObject *code = NULL;
236 PyObject *name = NULL;
237 PyObject *nulltuple = NULL;
238 PyObject *filename = NULL;
239 if (handler_info[slot].tb_code == NULL) {
240 code = PyString_FromString("");
241 if (code == NULL)
242 goto failed;
243 name = PyString_FromString(func_name);
244 if (name == NULL)
245 goto failed;
246 nulltuple = PyTuple_New(0);
247 if (nulltuple == NULL)
248 goto failed;
249 filename = PyString_FromString("pyexpat.c");
250 handler_info[slot].tb_code = PyCode_New(
251 0, /* argcount */
252 0, /* nlocals */
253 0, /* stacksize */
254 0, /* flags */
255 code, /* code */
256 nulltuple, /* consts */
257 nulltuple, /* names */
258 nulltuple, /* varnames */
259 filename, /* filename */
260 name, /* name */
261 lineno, /* firstlineno */
262 code /* lnotab */
263 );
264 if (handler_info[slot].tb_code == NULL)
265 goto failed;
266 Py_DECREF(code);
267 Py_DECREF(nulltuple);
268 Py_DECREF(filename);
269 Py_DECREF(name);
270 }
271 return handler_info[slot].tb_code;
272 failed:
273 Py_XDECREF(code);
274 Py_XDECREF(name);
275 return NULL;
276}
277
278static PyObject*
279call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
280{
281 PyThreadState *tstate = PyThreadState_GET();
282 PyFrameObject *f;
283 PyObject *res;
284 if (c == NULL)
285 return NULL;
286 f = PyFrame_New(
287 tstate, /*back*/
288 c, /*code*/
289 tstate->frame->f_globals, /*globals*/
290 NULL); /*locals*/
291 if (f == NULL)
292 return NULL;
293 tstate->frame = f;
294 res = PyEval_CallObject(func, args);
295 if (res == NULL && tstate->curexc_traceback == NULL)
296 PyTraceBack_Here(f);
297 tstate->frame = f->f_back;
298 Py_DECREF(f);
299 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000300}
301
Fred Drake6f987622000-08-25 18:03:30 +0000302#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000303 RETURN, GETUSERDATA) \
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000304\
305static RC my_##NAME##Handler PARAMS {\
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000306 xmlparseobject *self = GETUSERDATA ; \
307 PyObject *args=NULL; \
308 PyObject *rv=NULL; \
309 INIT \
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000310\
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000311 if (self->handlers[NAME] \
312 && self->handlers[NAME] != Py_None) { \
313 args = Py_BuildValue PARAM_FORMAT ;\
314 if (!args) return RETURN; \
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000315 rv = call_with_frame(getcode(NAME,#NAME,__LINE__),self->handlers[NAME], args); \
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000316 Py_DECREF(args); \
317 if (rv == NULL) { \
Fred Drake6f987622000-08-25 18:03:30 +0000318 flag_error(self); \
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000319 return RETURN; \
320 } \
321 CONVERSION \
322 Py_DECREF(rv); \
323 } \
324 return RETURN; \
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000325}
326
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000327#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
328#define STRING_CONV_FUNC conv_string_to_utf8
329#else
330/* Python 1.6 and later versions */
331#define STRING_CONV_FUNC (self->returns_unicode \
332 ? conv_string_to_unicode : conv_string_to_utf8)
333#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000334
Fred Drake6f987622000-08-25 18:03:30 +0000335#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
336 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
337 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000338
Fred Drake6f987622000-08-25 18:03:30 +0000339#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
340 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
341 rc = PyInt_AsLong(rv);, rc, \
342 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000343
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000344#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000345VOID_HANDLER(StartElement,
346 (void *userData, const XML_Char *name, const XML_Char **atts),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000347 ("(O&O&)", STRING_CONV_FUNC, name,
348 conv_atts_using_string, atts ) )
349#else
350/* Python 1.6 and later */
Fred Drake6f987622000-08-25 18:03:30 +0000351VOID_HANDLER(StartElement,
352 (void *userData, const XML_Char *name, const XML_Char **atts),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000353 ("(O&O&)", STRING_CONV_FUNC, name,
354 (self->returns_unicode
355 ? conv_atts_using_unicode
Fred Drake6f987622000-08-25 18:03:30 +0000356 : conv_atts_using_string), atts))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000357#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000358
Fred Drake6f987622000-08-25 18:03:30 +0000359VOID_HANDLER(EndElement,
360 (void *userData, const XML_Char *name),
361 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000362
Fred Drake6f987622000-08-25 18:03:30 +0000363VOID_HANDLER(ProcessingInstruction,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000364 (void *userData,
365 const XML_Char *target,
366 const XML_Char *data),
Fred Drake6f987622000-08-25 18:03:30 +0000367 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000368
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000369#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000370VOID_HANDLER(CharacterData,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000371 (void *userData, const XML_Char *data, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000372 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000373#else
Fred Drake6f987622000-08-25 18:03:30 +0000374VOID_HANDLER(CharacterData,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000375 (void *userData, const XML_Char *data, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000376 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000377 ? conv_string_len_to_unicode(data,len)
Fred Drake6f987622000-08-25 18:03:30 +0000378 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000379#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000380
Fred Drake6f987622000-08-25 18:03:30 +0000381VOID_HANDLER(UnparsedEntityDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000382 (void *userData,
383 const XML_Char *entityName,
384 const XML_Char *base,
385 const XML_Char *systemId,
386 const XML_Char *publicId,
387 const XML_Char *notationName),
388 ("(O&O&O&O&O&)",
389 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
390 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
391 STRING_CONV_FUNC,notationName))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000392
Fred Drake6f987622000-08-25 18:03:30 +0000393VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000394 (void *userData,
395 const XML_Char *notationName,
396 const XML_Char *base,
397 const XML_Char *systemId,
398 const XML_Char *publicId),
399 ("(O&O&O&O&)",
400 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
401 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000402
Fred Drake6f987622000-08-25 18:03:30 +0000403VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000404 (void *userData,
405 const XML_Char *prefix,
406 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000407 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000408
Fred Drake6f987622000-08-25 18:03:30 +0000409VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000410 (void *userData,
411 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000412 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000413
Fred Drake6f987622000-08-25 18:03:30 +0000414VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000415 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000416 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000417
Fred Drake6f987622000-08-25 18:03:30 +0000418VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000419 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000420 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000421
Fred Drake6f987622000-08-25 18:03:30 +0000422VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000423 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000424 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000425
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000426#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000427VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000428 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000429 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000430
Fred Drake6f987622000-08-25 18:03:30 +0000431VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000432 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000433 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000434#else
Fred Drake6f987622000-08-25 18:03:30 +0000435VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000436 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000437 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000438 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000439 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000440
Fred Drake6f987622000-08-25 18:03:30 +0000441VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000442 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000443 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000444 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000445 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000446#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000447
Fred Drake6f987622000-08-25 18:03:30 +0000448INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000449 (void *userData),
450 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000451
Fred Drake6f987622000-08-25 18:03:30 +0000452RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000453 (XML_Parser parser,
454 const XML_Char *context,
455 const XML_Char *base,
456 const XML_Char *systemId,
457 const XML_Char *publicId),
458 int rc=0;,
459 ("(O&O&O&O&)",
460 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000461 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
462 rc = PyInt_AsLong(rv);, rc,
463 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000464
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000465/* XXX UnknownEncodingHandler */
466
467#if EXPAT_VERSION >= 0x010200
468
469VOID_HANDLER(StartDoctypeDecl,
470 (void *userData, const XML_Char *doctypeName),
471 ("(O&)", STRING_CONV_FUNC, doctypeName))
472
473VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
474
475VOID_HANDLER(ExternalParsedEntityDecl,
476 (void *userData, const XML_Char *entityName,
477 const XML_Char *base, const XML_Char *systemId,
478 const XML_Char *publicId),
479 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
480 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
481 STRING_CONV_FUNC, publicId))
482
483VOID_HANDLER(InternalParsedEntityDecl,
484 (void *userData, const XML_Char *entityName,
485 const XML_Char *replacementText, int replacementTextLength),
486 ("(O&O&i)", STRING_CONV_FUNC, entityName,
487 STRING_CONV_FUNC, replacementText, replacementTextLength))
488
489#endif /* EXPAT_VERSION >= 0x010200 */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000490
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000491/* ---------------------------------------------------------------- */
492
493static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000494"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000495Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000496
497static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000498xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000499{
Fred Drake0582df92000-07-12 04:49:00 +0000500 char *s;
501 int slen;
502 int isFinal = 0;
503 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000504
Fred Drake0582df92000-07-12 04:49:00 +0000505 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
506 return NULL;
507 rv = XML_Parse(self->itself, s, slen, isFinal);
508 if (PyErr_Occurred()) {
509 return NULL;
510 }
511 else if (rv == 0) {
512 PyErr_Format(ErrorObject, "%.200s: line %i, column %i",
513 XML_ErrorString(XML_GetErrorCode(self->itself)),
514 XML_GetErrorLineNumber(self->itself),
515 XML_GetErrorColumnNumber(self->itself));
516 return NULL;
517 }
518 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000519}
520
Fred Drakeca1f4262000-09-21 20:10:23 +0000521/* File reading copied from cPickle */
522
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000523#define BUF_SIZE 2048
524
Fred Drake0582df92000-07-12 04:49:00 +0000525static int
526readinst(char *buf, int buf_size, PyObject *meth)
527{
528 PyObject *arg = NULL;
529 PyObject *bytes = NULL;
530 PyObject *str = NULL;
531 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000532
Fred Drake676940b2000-09-22 15:21:31 +0000533 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000534 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000535
Fred Drakeca1f4262000-09-21 20:10:23 +0000536 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000537 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000538
Tim Peters954eef72000-09-22 06:01:11 +0000539 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000540
Fred Drakeca1f4262000-09-21 20:10:23 +0000541 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000542 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000543
Fred Drake0582df92000-07-12 04:49:00 +0000544 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000545 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000546 PyErr_Format(PyExc_TypeError,
547 "read() did not return a string object (type=%.400s)",
548 str->ob_type->tp_name);
549 goto finally;
550 }
551 len = PyString_GET_SIZE(str);
552 if (len > buf_size) {
553 PyErr_Format(PyExc_ValueError,
554 "read() returned too much data: "
555 "%i bytes requested, %i returned",
556 buf_size, len);
557 Py_DECREF(str);
558 goto finally;
559 }
560 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000561finally:
Fred Drake0582df92000-07-12 04:49:00 +0000562 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000563 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000564 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000565}
566
567static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000568"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000569Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000570
571static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000572xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000573{
Fred Drake0582df92000-07-12 04:49:00 +0000574 int rv = 1;
575 PyObject *f;
576 FILE *fp;
577 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000578
Fred Drake0582df92000-07-12 04:49:00 +0000579 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
580 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000581
Fred Drake0582df92000-07-12 04:49:00 +0000582 if (PyFile_Check(f)) {
583 fp = PyFile_AsFile(f);
584 }
585 else{
586 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000587 readmethod = PyObject_GetAttrString(f, "read");
588 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000589 PyErr_Clear();
590 PyErr_SetString(PyExc_TypeError,
591 "argument must have 'read' attribute");
592 return 0;
593 }
594 }
595 for (;;) {
596 int bytes_read;
597 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
598 if (buf == NULL)
599 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000600
Fred Drake0582df92000-07-12 04:49:00 +0000601 if (fp) {
602 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
603 if (bytes_read < 0) {
604 PyErr_SetFromErrno(PyExc_IOError);
605 return NULL;
606 }
607 }
608 else {
609 bytes_read = readinst(buf, BUF_SIZE, readmethod);
610 if (bytes_read < 0)
611 return NULL;
612 }
613 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
614 if (PyErr_Occurred())
615 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000616
Fred Drake0582df92000-07-12 04:49:00 +0000617 if (!rv || bytes_read == 0)
618 break;
619 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000620 if (rv == 0) {
621 PyErr_Format(ErrorObject, "%.200s: line %i, column %i",
622 XML_ErrorString(XML_GetErrorCode(self->itself)),
623 XML_GetErrorLineNumber(self->itself),
624 XML_GetErrorColumnNumber(self->itself));
625 return NULL;
626 }
Fred Drake0582df92000-07-12 04:49:00 +0000627 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000628}
629
630static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000631"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000632Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000633
634static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000635xmlparse_SetBase(xmlparseobject *self, PyObject *args)
636{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000637 char *base;
638
Fred Drake0582df92000-07-12 04:49:00 +0000639 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000640 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000641 if (!XML_SetBase(self->itself, base)) {
642 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000643 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000644 Py_INCREF(Py_None);
645 return Py_None;
646}
647
648static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000649"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000650Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000651
652static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000653xmlparse_GetBase(xmlparseobject *self, PyObject *args)
654{
655 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000657
Fred Drake0582df92000-07-12 04:49:00 +0000658 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000659}
660
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000661static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000662"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000663Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000664information passed to the ExternalEntityRefHandler.";
665
666static PyObject *
667xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
668{
669 char *context;
670 char *encoding = NULL;
671 xmlparseobject *new_parser;
672 int i;
673
674 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
675 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000676 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000677 }
678
679#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
680 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
681 if (new_parser == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000682 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000683
684 new_parser->returns_unicode = 0;
685#else
686 /* Code for versions 1.6 and later */
687 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
688 if (new_parser == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000689 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000690
691 new_parser->returns_unicode = 1;
692#endif
693
694 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000695 encoding);
696 new_parser->handlers = 0;
697 PyObject_GC_Init(new_parser);
698
699 if (!new_parser->itself) {
700 Py_DECREF(new_parser);
701 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000702 }
703
704 XML_SetUserData(new_parser->itself, (void *)new_parser);
705
706 /* allocate and clear handlers first */
707 for(i = 0; handler_info[i].name != NULL; i++)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000708 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000709
710 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000711 if (!new_parser->handlers) {
712 Py_DECREF(new_parser);
713 return PyErr_NoMemory();
714 }
715 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000716
717 /* then copy handlers from self */
718 for (i = 0; handler_info[i].name != NULL; i++) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000719 if (self->handlers[i]) {
720 Py_INCREF(self->handlers[i]);
721 new_parser->handlers[i] = self->handlers[i];
722 handler_info[i].setter(new_parser->itself,
723 handler_info[i].handler);
724 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000725 }
726
Fred Drake28adf522000-09-24 22:07:59 +0000727 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000728}
729
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000730#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000731
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000732static char xmlparse_SetParamEntityParsing__doc__[] =
733"SetParamEntityParsing(flag) -> success\n\
734Controls parsing of parameter entities (including the external DTD\n\
735subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
736XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
737XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
738was successful.";
739
740static PyObject*
741xmlparse_SetParamEntityParsing(PyObject *p, PyObject* args)
742{
743 int flag;
744 if (!PyArg_ParseTuple(args, "i", &flag))
745 return NULL;
746 flag = XML_SetParamEntityParsing(((xmlparseobject*)p)->itself,
747 flag);
748 return PyInt_FromLong(flag);
749}
750
751#endif /* EXPAT_VERSION >= 0x010200 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000752
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000753static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +0000754 {"Parse", (PyCFunction)xmlparse_Parse,
755 METH_VARARGS, xmlparse_Parse__doc__},
756 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
757 METH_VARARGS, xmlparse_ParseFile__doc__},
758 {"SetBase", (PyCFunction)xmlparse_SetBase,
759 METH_VARARGS, xmlparse_SetBase__doc__},
760 {"GetBase", (PyCFunction)xmlparse_GetBase,
761 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000762 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
763 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000764#if EXPAT_VERSION >= 0x010200
765 {"SetParamEntityParsing", xmlparse_SetParamEntityParsing,
766 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
767#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000768 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000769};
770
771/* ---------- */
772
773
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000774#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
775
776/*
777 pyexpat international encoding support.
778 Make it as simple as possible.
779*/
780
781static char template_buffer[256];
782PyObject * template_string=NULL;
783
784static void
785init_template_buffer(void)
786{
787 int i;
788 for (i=0;i<256;i++) {
789 template_buffer[i]=i;
790 };
791 template_buffer[256]=0;
792};
793
794int
795PyUnknownEncodingHandler(void *encodingHandlerData,
796const XML_Char *name,
797XML_Encoding * info)
798{
799 PyUnicodeObject * _u_string=NULL;
800 int result=0;
801 int i;
802
803 _u_string=(PyUnicodeObject *) PyUnicode_Decode(template_buffer, 256, name, "replace"); // Yes, supports only 8bit encodings
804
805 if (_u_string==NULL) {
806 return result;
807 };
808
809 for (i=0; i<256; i++) {
810 Py_UNICODE c = _u_string->str[i] ; // Stupid to access directly, but fast
811 if (c==Py_UNICODE_REPLACEMENT_CHARACTER) {
812 info->map[i] = -1;
813 } else {
814 info->map[i] = c;
815 };
816 };
817
818 info->data = NULL;
819 info->convert = NULL;
820 info->release = NULL;
821 result=1;
822
823 Py_DECREF(_u_string);
824 return result;
825}
826
827#endif
828
829static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000830newxmlparseobject(char *encoding, char *namespace_separator)
831{
832 int i;
833 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000834
835#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +0000836 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
837 if (self == NULL)
838 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000839
Fred Drake0582df92000-07-12 04:49:00 +0000840 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000841#else
Fred Drake0582df92000-07-12 04:49:00 +0000842 /* Code for versions 1.6 and later */
843 self = PyObject_New(xmlparseobject, &Xmlparsetype);
844 if (self == NULL)
845 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000846
Fred Drake0582df92000-07-12 04:49:00 +0000847 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000848#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000849 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000850 if (namespace_separator) {
851 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
852 }
853 else{
854 self->itself = XML_ParserCreate(encoding);
855 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000856 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +0000857 if (self->itself == NULL) {
858 PyErr_SetString(PyExc_RuntimeError,
859 "XML_ParserCreate failed");
860 Py_DECREF(self);
861 return NULL;
862 }
863 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000864#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
865#else
866 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
867#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000868
Fred Drake0582df92000-07-12 04:49:00 +0000869 for(i = 0; handler_info[i].name != NULL; i++)
870 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000871
Fred Drake0582df92000-07-12 04:49:00 +0000872 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000873 if (!self->handlers){
874 Py_DECREF(self);
875 return PyErr_NoMemory();
876 }
877 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000879 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000880}
881
882
883static void
Fred Drake0582df92000-07-12 04:49:00 +0000884xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000885{
Fred Drake0582df92000-07-12 04:49:00 +0000886 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000887 PyObject_GC_Fini(self);
Fred Drake0582df92000-07-12 04:49:00 +0000888 if (self->itself)
889 XML_ParserFree(self->itself);
890 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000892 if(self->handlers){
893 for (i=0; handler_info[i].name != NULL; i++) {
894 Py_XDECREF(self->handlers[i]);
895 }
896 free (self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +0000897 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000898#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +0000899 /* Code for versions before 1.6 */
900 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000901#else
Fred Drake0582df92000-07-12 04:49:00 +0000902 /* Code for versions 1.6 and later */
903 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000904#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000905}
906
Fred Drake0582df92000-07-12 04:49:00 +0000907static int
908handlername2int(const char *name)
909{
910 int i;
911 for (i=0; handler_info[i].name != NULL; i++) {
912 if (strcmp(name, handler_info[i].name) == 0) {
913 return i;
914 }
915 }
916 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000917}
918
919static PyObject *
920xmlparse_getattr(xmlparseobject *self, char *name)
921{
Fred Drake0582df92000-07-12 04:49:00 +0000922 int handlernum;
923 if (strcmp(name, "ErrorCode") == 0)
924 return Py_BuildValue("l",
925 (long)XML_GetErrorCode(self->itself));
926 if (strcmp(name, "ErrorLineNumber") == 0)
927 return Py_BuildValue("l",
928 (long)XML_GetErrorLineNumber(self->itself));
929 if (strcmp(name, "ErrorColumnNumber") == 0)
930 return Py_BuildValue("l",
931 (long)XML_GetErrorColumnNumber(self->itself));
932 if (strcmp(name, "ErrorByteIndex") == 0)
933 return Py_BuildValue("l",
934 XML_GetErrorByteIndex(self->itself));
935 if (strcmp(name, "returns_unicode") == 0)
936 return Py_BuildValue("i", self->returns_unicode);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000937
Fred Drake0582df92000-07-12 04:49:00 +0000938 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000939
Fred Drake0582df92000-07-12 04:49:00 +0000940 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
941 Py_INCREF(self->handlers[handlernum]);
942 return self->handlers[handlernum];
943 }
944 if (strcmp(name, "__members__") == 0) {
945 int i;
946 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +0000947 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +0000948 PyList_Append(rc,
949 PyString_FromString(handler_info[i].name));
950 }
951 PyList_Append(rc, PyString_FromString("ErrorCode"));
952 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
953 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
954 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drakee8f3ad52000-12-16 01:48:29 +0000955 PyList_Append(rc, PyString_FromString("returns_unicode"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000956
Fred Drake0582df92000-07-12 04:49:00 +0000957 return rc;
958 }
959 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000960}
961
Fred Drake6f987622000-08-25 18:03:30 +0000962static int
963sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +0000964{
965 int handlernum = handlername2int(name);
966 if (handlernum != -1) {
967 Py_INCREF(v);
968 Py_XDECREF(self->handlers[handlernum]);
969 self->handlers[handlernum] = v;
970 handler_info[handlernum].setter(self->itself,
971 handler_info[handlernum].handler);
972 return 1;
973 }
974 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000975}
976
977static int
Fred Drake6f987622000-08-25 18:03:30 +0000978xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000979{
Fred Drake6f987622000-08-25 18:03:30 +0000980 /* Set attribute 'name' to value 'v'. v==NULL means delete */
981 if (v==NULL) {
982 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
983 return -1;
984 }
985 if (strcmp(name, "returns_unicode") == 0) {
986 PyObject *intobj = PyNumber_Int(v);
987 if (intobj == NULL) return -1;
988 if (PyInt_AsLong(intobj)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000989#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000990 PyErr_SetString(PyExc_ValueError,
991 "Cannot return Unicode strings in Python 1.5");
992 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000993#else
Fred Drake6f987622000-08-25 18:03:30 +0000994 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000995#endif
Fred Drake6f987622000-08-25 18:03:30 +0000996 }
997 else
998 self->returns_unicode = 0;
999 Py_DECREF(intobj);
1000 return 0;
1001 }
1002 if (sethandler(self, name, v)) {
1003 return 0;
1004 }
1005 PyErr_SetString(PyExc_AttributeError, name);
1006 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001007}
1008
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001009#ifdef WITH_CYCLE_GC
1010static int
1011xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1012{
1013 int i, err;
1014 for (i = 0; handler_info[i].name != NULL; i++) {
1015 if (!op->handlers[i])
1016 continue;
1017 err = visit(op->handlers[i], arg);
1018 if (err)
1019 return err;
1020 }
1021 return 0;
1022}
1023
1024static int
1025xmlparse_clear(xmlparseobject *op)
1026{
1027 clear_handlers(op, 1);
1028 return 0;
1029}
1030#endif
1031
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001032static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001033"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001034
1035static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001036 PyObject_HEAD_INIT(NULL)
1037 0, /*ob_size*/
1038 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001039 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001040 0, /*tp_itemsize*/
1041 /* methods */
1042 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1043 (printfunc)0, /*tp_print*/
1044 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1045 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1046 (cmpfunc)0, /*tp_compare*/
1047 (reprfunc)0, /*tp_repr*/
1048 0, /*tp_as_number*/
1049 0, /*tp_as_sequence*/
1050 0, /*tp_as_mapping*/
1051 (hashfunc)0, /*tp_hash*/
1052 (ternaryfunc)0, /*tp_call*/
1053 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001054 0, /* tp_getattro */
1055 0, /* tp_setattro */
1056 0, /* tp_as_buffer */
1057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1058 Xmlparsetype__doc__, /* Documentation string */
1059#ifdef WITH_CYCLE_GC
1060 (traverseproc)xmlparse_traverse, /* tp_traverse */
1061 (inquiry)xmlparse_clear /* tp_clear */
1062#else
1063 0, 0
1064#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001065};
1066
1067/* End of code for xmlparser objects */
1068/* -------------------------------------------------------- */
1069
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001070static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001071"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1072Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001073
1074static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001075pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1076{
1077 char *encoding = NULL;
1078 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001079 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001080
Fred Drake0582df92000-07-12 04:49:00 +00001081 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001082 &encoding, &namespace_separator))
1083 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001084 if (namespace_separator != NULL
1085 && strlen(namespace_separator) != 1) {
1086 PyErr_SetString(PyExc_ValueError,
1087 "namespace_separator must be one character,"
1088 " omitted, or None");
1089 return NULL;
1090 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001091 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001092}
1093
1094static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001095"ErrorString(errno) -> string\n\
1096Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001097
1098static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001099pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001100{
Fred Drake0582df92000-07-12 04:49:00 +00001101 long code = 0;
1102
1103 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1104 return NULL;
1105 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001106}
1107
1108/* List of methods defined in the module */
1109
1110static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001111 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1112 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1113 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1114 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001115
Fred Drake0582df92000-07-12 04:49:00 +00001116 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001117};
1118
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001119/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001120
1121static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001122"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001123
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001124/* Initialization function for the module */
1125
Fred Drake93adb692000-09-23 04:55:48 +00001126void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001127
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001128#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001129
1130/* 1.5 compatibility: PyModule_AddObject */
1131static int
1132PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1133{
1134 PyObject *dict;
1135 if (!PyModule_Check(m) || o == NULL)
1136 return -1;
1137 dict = PyModule_GetDict(m);
1138 if (dict == NULL)
1139 return -1;
1140 if (PyDict_SetItemString(dict, name, o))
1141 return -1;
1142 Py_DECREF(o);
1143 return 0;
1144}
1145
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001146int
1147PyModule_AddIntConstant(PyObject *m, char *name, long value)
1148{
1149 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1150}
1151
Fred Drakea77254a2000-09-29 19:23:29 +00001152static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001153PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1154{
1155 return PyModule_AddObject(m, name, PyString_FromString(value));
1156}
1157
1158#endif
1159
Fred Drake6f987622000-08-25 18:03:30 +00001160DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001161initpyexpat(void)
1162{
1163 PyObject *m, *d;
1164 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001165 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake0582df92000-07-12 04:49:00 +00001166 PyObject *errors_module, *errors_dict;
1167 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001168
Fred Drake6f987622000-08-25 18:03:30 +00001169 if (errmod_name == NULL)
1170 return;
1171
Fred Drake0582df92000-07-12 04:49:00 +00001172 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001173
Fred Drake0582df92000-07-12 04:49:00 +00001174 /* Create the module and add the functions */
1175 m = Py_InitModule4("pyexpat", pyexpat_methods,
1176 pyexpat_module_documentation,
1177 (PyObject*)NULL, PYTHON_API_VERSION);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001178
Fred Drake0582df92000-07-12 04:49:00 +00001179 /* Add some symbolic constants to the module */
Fred Drakec23b5232000-08-24 21:57:43 +00001180 if (ErrorObject == NULL)
Fred Drake93adb692000-09-23 04:55:48 +00001181 ErrorObject = PyErr_NewException("xml.parsers.expat.error",
1182 NULL, NULL);
1183 PyModule_AddObject(m, "error", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001184 Py_INCREF(&Xmlparsetype);
1185 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001186
Fred Drake93adb692000-09-23 04:55:48 +00001187 PyModule_AddObject(m, "__version__",
1188 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake738293d2000-12-21 17:25:07 +00001189#ifdef XML_MAJOR_VERSION
1190 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1191 (char *) XML_ExpatVersion());
1192 PyModule_AddObject(m, "version_info",
1193 Py_BuildValue("(iii)", XML_MAJOR_VERSION,
1194 XML_MINOR_VERSION, XML_MICRO_VERSION));
1195#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001196
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001197#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1198#else
1199 init_template_buffer();
1200#endif
Fred Drake0582df92000-07-12 04:49:00 +00001201 /* XXX When Expat supports some way of figuring out how it was
1202 compiled, this should check and set native_encoding
1203 appropriately.
1204 */
Fred Drake93adb692000-09-23 04:55:48 +00001205 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001206
Fred Drake93adb692000-09-23 04:55:48 +00001207 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001208 errors_module = PyDict_GetItem(d, errmod_name);
1209 if (errors_module == NULL) {
1210 errors_module = PyModule_New("pyexpat.errors");
1211 if (errors_module != NULL) {
1212 sys_modules = PySys_GetObject("modules");
Fred Drake6f987622000-08-25 18:03:30 +00001213 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001214 /* gives away the reference to errors_module */
1215 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001216 }
1217 }
Fred Drake6f987622000-08-25 18:03:30 +00001218 Py_DECREF(errmod_name);
1219 if (errors_module == NULL)
1220 /* Don't code dump later! */
1221 return;
1222
Fred Drake0582df92000-07-12 04:49:00 +00001223 errors_dict = PyModule_GetDict(errors_module);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001224
1225#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001226 PyModule_AddStringConstant(errors_module, #name, \
1227 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001228
Fred Drake0582df92000-07-12 04:49:00 +00001229 MYCONST(XML_ERROR_NO_MEMORY);
1230 MYCONST(XML_ERROR_SYNTAX);
1231 MYCONST(XML_ERROR_NO_ELEMENTS);
1232 MYCONST(XML_ERROR_INVALID_TOKEN);
1233 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1234 MYCONST(XML_ERROR_PARTIAL_CHAR);
1235 MYCONST(XML_ERROR_TAG_MISMATCH);
1236 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1237 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1238 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1239 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1240 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1241 MYCONST(XML_ERROR_ASYNC_ENTITY);
1242 MYCONST(XML_ERROR_BAD_CHAR_REF);
1243 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1244 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1245 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1246 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1247 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001248 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1249 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1250 MYCONST(XML_ERROR_NOT_STANDALONE);
1251
Fred Drake93adb692000-09-23 04:55:48 +00001252#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001253#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
1254
1255#if EXPAT_VERSION >= 0x010200
1256 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1257 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1258 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
1259#endif
1260
1261#undef MYCONST
1262
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001263}
1264
Fred Drake6f987622000-08-25 18:03:30 +00001265static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001266clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001267{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001268 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001270 for (; handler_info[i].name!=NULL; i++) {
1271 if (decref){
1272 Py_XDECREF(self->handlers[i]);
1273 }
1274 self->handlers[i]=NULL;
1275 handler_info[i].setter(self->itself, NULL);
1276 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001277}
1278
Fred Drake6f987622000-08-25 18:03:30 +00001279typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001280
Fred Drake6f987622000-08-25 18:03:30 +00001281static void
1282pyxml_UpdatePairedHandlers(xmlparseobject *self,
1283 int startHandler,
1284 int endHandler,
1285 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001286{
1287 void *start_handler=NULL;
1288 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001289
Fred Drake0582df92000-07-12 04:49:00 +00001290 if (self->handlers[startHandler]
1291 && self->handlers[endHandler]!=Py_None) {
1292 start_handler=handler_info[startHandler].handler;
1293 }
1294 if (self->handlers[EndElement]
1295 && self->handlers[EndElement] !=Py_None) {
1296 end_handler=handler_info[endHandler].handler;
1297 }
1298 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001299}
1300
Fred Drake6f987622000-08-25 18:03:30 +00001301static void
1302pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001303{
1304 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1305 StartElement, EndElement,
1306 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001307}
1308
Fred Drake6f987622000-08-25 18:03:30 +00001309static void
1310pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001311{
1312 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1313 StartElement, EndElement,
1314 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001315}
1316
Fred Drake6f987622000-08-25 18:03:30 +00001317static void
1318pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001319{
1320 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1321 StartNamespaceDecl, EndNamespaceDecl,
1322 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001323}
1324
Fred Drake6f987622000-08-25 18:03:30 +00001325static void
1326pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001327{
1328 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1329 StartNamespaceDecl, EndNamespaceDecl,
1330 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001331}
1332
Fred Drake6f987622000-08-25 18:03:30 +00001333static void
1334pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001335{
1336 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1337 StartCdataSection, EndCdataSection,
1338 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001339}
1340
Fred Drake6f987622000-08-25 18:03:30 +00001341static void
1342pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001343{
1344 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1345 StartCdataSection, EndCdataSection,
1346 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001347}
1348
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001349#if EXPAT_VERSION >= 0x010200
1350
1351static void
1352pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1353{
1354 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1355 StartDoctypeDecl, EndDoctypeDecl,
1356 (pairsetter)XML_SetDoctypeDeclHandler);
1357}
1358
1359static void
1360pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1361{
1362 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1363 StartDoctypeDecl, EndDoctypeDecl,
1364 (pairsetter)XML_SetDoctypeDeclHandler);
1365}
1366
1367#endif
1368
Fred Drake0582df92000-07-12 04:49:00 +00001369statichere struct HandlerInfo handler_info[] = {
1370 {"StartElementHandler",
1371 pyxml_SetStartElementHandler,
1372 (xmlhandler)my_StartElementHandler},
1373 {"EndElementHandler",
1374 pyxml_SetEndElementHandler,
1375 (xmlhandler)my_EndElementHandler},
1376 {"ProcessingInstructionHandler",
1377 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1378 (xmlhandler)my_ProcessingInstructionHandler},
1379 {"CharacterDataHandler",
1380 (xmlhandlersetter)XML_SetCharacterDataHandler,
1381 (xmlhandler)my_CharacterDataHandler},
1382 {"UnparsedEntityDeclHandler",
1383 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1384 (xmlhandler)my_UnparsedEntityDeclHandler },
1385 {"NotationDeclHandler",
1386 (xmlhandlersetter)XML_SetNotationDeclHandler,
1387 (xmlhandler)my_NotationDeclHandler },
1388 {"StartNamespaceDeclHandler",
1389 pyxml_SetStartNamespaceDeclHandler,
1390 (xmlhandler)my_StartNamespaceDeclHandler },
1391 {"EndNamespaceDeclHandler",
1392 pyxml_SetEndNamespaceDeclHandler,
1393 (xmlhandler)my_EndNamespaceDeclHandler },
1394 {"CommentHandler",
1395 (xmlhandlersetter)XML_SetCommentHandler,
1396 (xmlhandler)my_CommentHandler},
1397 {"StartCdataSectionHandler",
1398 pyxml_SetStartCdataSection,
1399 (xmlhandler)my_StartCdataSectionHandler},
1400 {"EndCdataSectionHandler",
1401 pyxml_SetEndCdataSection,
1402 (xmlhandler)my_EndCdataSectionHandler},
1403 {"DefaultHandler",
1404 (xmlhandlersetter)XML_SetDefaultHandler,
1405 (xmlhandler)my_DefaultHandler},
1406 {"DefaultHandlerExpand",
1407 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1408 (xmlhandler)my_DefaultHandlerExpandHandler},
1409 {"NotStandaloneHandler",
1410 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1411 (xmlhandler)my_NotStandaloneHandler},
1412 {"ExternalEntityRefHandler",
1413 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1414 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001415#if EXPAT_VERSION >= 0x010200
1416 {"StartDoctypeDeclHandler",
1417 pyxml_SetStartDoctypeDeclHandler,
1418 (xmlhandler)my_StartDoctypeDeclHandler},
1419 {"EndDoctypeDeclHandler",
1420 pyxml_SetEndDoctypeDeclHandler,
1421 (xmlhandler)my_EndDoctypeDeclHandler},
1422 {"ExternalParsedEntityDeclHandler",
1423 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1424 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1425 {"InternalParsedEntityDeclHandler",
1426 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1427 (xmlhandler)my_InternalParsedEntityDeclHandler},
1428#endif /* EXPAT_VERSION >=0x010200 */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001429
Fred Drake0582df92000-07-12 04:49:00 +00001430 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431};