blob: 4bddc4691034b22dd0137f71eeabe941475226d8 [file] [log] [blame]
Fred Drake4113b132001-03-24 19:58:26 +00001#include <ctype.h>
2
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00003#include "Python.h"
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
Fred Drake0582df92000-07-12 04:49:00 +000031enum HandlerTypes {
32 StartElement,
33 EndElement,
34 ProcessingInstruction,
35 CharacterData,
36 UnparsedEntityDecl,
37 NotationDecl,
38 StartNamespaceDecl,
39 EndNamespaceDecl,
40 Comment,
41 StartCdataSection,
42 EndCdataSection,
43 Default,
44 DefaultHandlerExpand,
45 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000046 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000047#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000048 StartDoctypeDecl,
49 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000050#endif
51#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000052 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000053 InternalParsedEntityDecl,
54#endif
55#if EXPAT_VERSION >= 0x015f00
56 EntityDecl,
57 XmlDecl,
58 ElementDecl,
59 AttlistDecl,
60#endif
61 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000062};
63
64static PyObject *ErrorObject;
65
66/* ----------------------------------------------------- */
67
68/* Declarations for objects of type xmlparser */
69
70typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000071 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000072
Fred Drake0582df92000-07-12 04:49:00 +000073 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000074 int returns_unicode; /* True if Unicode strings are returned;
75 if false, UTF-8 strings are returned */
76 int ordered_attributes; /* Return attributes as a list. */
77 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000078 int in_callback; /* Is a callback active? */
Fred Drake0582df92000-07-12 04:49:00 +000079 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080} xmlparseobject;
81
82staticforward PyTypeObject Xmlparsetype;
83
Fred Drake6f987622000-08-25 18:03:30 +000084typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000085typedef void* xmlhandler;
86
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000087struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000088 const char *name;
89 xmlhandlersetter setter;
90 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000091 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000092};
93
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000094staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000095
Fred Drakebd6101c2001-02-14 18:29:45 +000096/* Set an integer attribute on the error object; return true on success,
97 * false on an exception.
98 */
99static int
100set_error_attr(PyObject *err, char *name, int value)
101{
102 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000103
Fred Drakebd6101c2001-02-14 18:29:45 +0000104 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
105 Py_DECREF(v);
106 return 0;
107 }
108 return 1;
109}
110
111/* Build and set an Expat exception, including positioning
112 * information. Always returns NULL.
113 */
Fred Drake85d835f2001-02-08 15:39:08 +0000114static PyObject *
115set_error(xmlparseobject *self)
116{
117 PyObject *err;
118 char buffer[256];
119 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000120 int lineno = XML_GetErrorLineNumber(parser);
121 int column = XML_GetErrorColumnNumber(parser);
122 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000123
124 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000126 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000127 if ( err != NULL
128 && set_error_attr(err, "code", code)
129 && set_error_attr(err, "offset", column)
130 && set_error_attr(err, "lineno", lineno)) {
131 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000132 }
133 return NULL;
134}
135
136
137#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000138/* Convert an array of attributes and their values into a Python dict */
139
Fred Drake0582df92000-07-12 04:49:00 +0000140static PyObject *
141conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000142{
Fred Drake0582df92000-07-12 04:49:00 +0000143 PyObject *attrs_obj = NULL;
144 XML_Char **attrs_p, **attrs_k = NULL;
145 int attrs_len;
146 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000147
Fred Drake0582df92000-07-12 04:49:00 +0000148 if ((attrs_obj = PyDict_New()) == NULL)
149 goto finally;
150 for (attrs_len = 0, attrs_p = atts;
151 *attrs_p;
152 attrs_p++, attrs_len++) {
153 if (attrs_len % 2) {
154 rv = PyString_FromString(*attrs_p);
155 if (!rv) {
156 Py_DECREF(attrs_obj);
157 attrs_obj = NULL;
158 goto finally;
159 }
160 if (PyDict_SetItemString(attrs_obj,
161 (char*)*attrs_k, rv) < 0) {
162 Py_DECREF(attrs_obj);
163 attrs_obj = NULL;
164 goto finally;
165 }
166 Py_DECREF(rv);
167 }
168 else
169 attrs_k = attrs_p;
170 }
171 finally:
172 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000173}
Fred Drake85d835f2001-02-08 15:39:08 +0000174#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000175
176#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake85d835f2001-02-08 15:39:08 +0000177#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000178static PyObject *
179conv_atts_using_unicode(XML_Char **atts)
180{
Fred Drakeca1f4262000-09-21 20:10:23 +0000181 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000182 XML_Char **attrs_p, **attrs_k = NULL;
183 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000184
Fred Drake0582df92000-07-12 04:49:00 +0000185 if ((attrs_obj = PyDict_New()) == NULL)
186 goto finally;
187 for (attrs_len = 0, attrs_p = atts;
188 *attrs_p;
189 attrs_p++, attrs_len++) {
190 if (attrs_len % 2) {
191 PyObject *attr_str, *value_str;
192 const char *p = (const char *) (*attrs_k);
193 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
194 if (!attr_str) {
195 Py_DECREF(attrs_obj);
196 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000197 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000198 }
199 p = (const char *) *attrs_p;
200 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
201 if (!value_str) {
202 Py_DECREF(attrs_obj);
203 Py_DECREF(attr_str);
204 attrs_obj = NULL;
205 goto finally;
206 }
207 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
208 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000209 Py_DECREF(attr_str);
210 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000211 attrs_obj = NULL;
212 goto finally;
213 }
214 Py_DECREF(attr_str);
215 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000216 }
Fred Drake0582df92000-07-12 04:49:00 +0000217 else
218 attrs_k = attrs_p;
219 }
220 finally:
221 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222}
Fred Drake85d835f2001-02-08 15:39:08 +0000223#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000224
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000225/* Convert a string of XML_Chars into a Unicode string.
226 Returns None if str is a null pointer. */
227
Fred Drake0582df92000-07-12 04:49:00 +0000228static PyObject *
229conv_string_to_unicode(XML_Char *str)
230{
231 /* XXX currently this code assumes that XML_Char is 8-bit,
232 and hence in UTF-8. */
233 /* UTF-8 from Expat, Unicode desired */
234 if (str == NULL) {
235 Py_INCREF(Py_None);
236 return Py_None;
237 }
238 return PyUnicode_DecodeUTF8((const char *)str,
239 strlen((const char *)str),
240 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000241}
242
Fred Drake0582df92000-07-12 04:49:00 +0000243static PyObject *
244conv_string_len_to_unicode(const XML_Char *str, int len)
245{
246 /* XXX currently this code assumes that XML_Char is 8-bit,
247 and hence in UTF-8. */
248 /* UTF-8 from Expat, Unicode desired */
249 if (str == NULL) {
250 Py_INCREF(Py_None);
251 return Py_None;
252 }
Fred Drake6f987622000-08-25 18:03:30 +0000253 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000254}
255#endif
256
257/* Convert a string of XML_Chars into an 8-bit Python string.
258 Returns None if str is a null pointer. */
259
Fred Drake6f987622000-08-25 18:03:30 +0000260static PyObject *
261conv_string_to_utf8(XML_Char *str)
262{
263 /* XXX currently this code assumes that XML_Char is 8-bit,
264 and hence in UTF-8. */
265 /* UTF-8 from Expat, UTF-8 desired */
266 if (str == NULL) {
267 Py_INCREF(Py_None);
268 return Py_None;
269 }
270 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000271}
272
Fred Drake6f987622000-08-25 18:03:30 +0000273static PyObject *
274conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000275{
Fred Drake6f987622000-08-25 18:03:30 +0000276 /* XXX currently this code assumes that XML_Char is 8-bit,
277 and hence in UTF-8. */
278 /* UTF-8 from Expat, UTF-8 desired */
279 if (str == NULL) {
280 Py_INCREF(Py_None);
281 return Py_None;
282 }
283 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000284}
285
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000286/* Callback routines */
287
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000288static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000289
Fred Drake6f987622000-08-25 18:03:30 +0000290static void
291flag_error(xmlparseobject *self)
292{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000293 clear_handlers(self, 1);
294}
295
296static PyCodeObject*
297getcode(enum HandlerTypes slot, char* func_name, int lineno)
298{
Fred Drakebd6101c2001-02-14 18:29:45 +0000299 PyObject *code = NULL;
300 PyObject *name = NULL;
301 PyObject *nulltuple = NULL;
302 PyObject *filename = NULL;
303
304 if (handler_info[slot].tb_code == NULL) {
305 code = PyString_FromString("");
306 if (code == NULL)
307 goto failed;
308 name = PyString_FromString(func_name);
309 if (name == NULL)
310 goto failed;
311 nulltuple = PyTuple_New(0);
312 if (nulltuple == NULL)
313 goto failed;
314 filename = PyString_FromString(__FILE__);
315 handler_info[slot].tb_code =
316 PyCode_New(0, /* argcount */
317 0, /* nlocals */
318 0, /* stacksize */
319 0, /* flags */
320 code, /* code */
321 nulltuple, /* consts */
322 nulltuple, /* names */
323 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000324#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000325 nulltuple, /* freevars */
326 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000327#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000328 filename, /* filename */
329 name, /* name */
330 lineno, /* firstlineno */
331 code /* lnotab */
332 );
333 if (handler_info[slot].tb_code == NULL)
334 goto failed;
335 Py_DECREF(code);
336 Py_DECREF(nulltuple);
337 Py_DECREF(filename);
338 Py_DECREF(name);
339 }
340 return handler_info[slot].tb_code;
341 failed:
342 Py_XDECREF(code);
343 Py_XDECREF(name);
344 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000345}
346
347static PyObject*
348call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
349{
Fred Drakebd6101c2001-02-14 18:29:45 +0000350 PyThreadState *tstate = PyThreadState_GET();
351 PyFrameObject *f;
352 PyObject *res;
353
354 if (c == NULL)
355 return NULL;
356 f = PyFrame_New(
357 tstate, /*back*/
358 c, /*code*/
359 tstate->frame->f_globals, /*globals*/
360 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000361 );
362 if (f == NULL)
363 return NULL;
364 tstate->frame = f;
365 res = PyEval_CallObject(func, args);
366 if (res == NULL && tstate->curexc_traceback == NULL)
367 PyTraceBack_Here(f);
368 tstate->frame = f->f_back;
369 Py_DECREF(f);
370 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000371}
372
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000373#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
374#define STRING_CONV_FUNC conv_string_to_utf8
375#else
376/* Python 1.6 and later versions */
377#define STRING_CONV_FUNC (self->returns_unicode \
378 ? conv_string_to_unicode : conv_string_to_utf8)
379#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000380
Fred Drake85d835f2001-02-08 15:39:08 +0000381static void
382my_StartElementHandler(void *userData,
383 const XML_Char *name, const XML_Char **atts)
384{
385 xmlparseobject *self = (xmlparseobject *)userData;
386
387 if (self->handlers[StartElement]
388 && self->handlers[StartElement] != Py_None) {
389 PyObject *container, *rv, *args;
390 int i, max;
391
392 /* Set max to the number of slots filled in atts[]; max/2 is
393 * the number of attributes we need to process.
394 */
395 if (self->specified_attributes) {
396 max = XML_GetSpecifiedAttributeCount(self->itself);
397 }
398 else {
399 max = 0;
400 while (atts[max] != NULL)
401 max += 2;
402 }
403 /* Build the container. */
404 if (self->ordered_attributes)
405 container = PyList_New(max);
406 else
407 container = PyDict_New();
408 if (container == NULL) {
409 flag_error(self);
410 return;
411 }
412 for (i = 0; i < max; i += 2) {
413 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
414 PyObject *v;
415 if (n == NULL) {
416 flag_error(self);
417 Py_DECREF(container);
418 return;
419 }
420 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
421 if (v == NULL) {
422 flag_error(self);
423 Py_DECREF(container);
424 Py_DECREF(n);
425 return;
426 }
427 if (self->ordered_attributes) {
428 PyList_SET_ITEM(container, i, n);
429 PyList_SET_ITEM(container, i+1, v);
430 }
431 else if (PyDict_SetItem(container, n, v)) {
432 flag_error(self);
433 Py_DECREF(n);
434 Py_DECREF(v);
435 return;
436 }
437 else {
438 Py_DECREF(n);
439 Py_DECREF(v);
440 }
441 }
442 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
443 if (args == NULL) {
444 Py_DECREF(container);
445 return;
446 }
447 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000448 self->in_callback = 1;
449 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000450 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000451 self->in_callback = 0;
452 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000453 if (rv == NULL) {
454 flag_error(self);
455 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000456 }
Fred Drake85d835f2001-02-08 15:39:08 +0000457 Py_DECREF(rv);
458 }
459}
460
461#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
462 RETURN, GETUSERDATA) \
463static RC \
464my_##NAME##Handler PARAMS {\
465 xmlparseobject *self = GETUSERDATA ; \
466 PyObject *args = NULL; \
467 PyObject *rv = NULL; \
468 INIT \
469\
470 if (self->handlers[NAME] \
471 && self->handlers[NAME] != Py_None) { \
472 args = Py_BuildValue PARAM_FORMAT ;\
473 if (!args) \
474 return RETURN; \
Fred Drakebd6101c2001-02-14 18:29:45 +0000475 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000476 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
477 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000478 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000479 Py_DECREF(args); \
480 if (rv == NULL) { \
481 flag_error(self); \
482 return RETURN; \
483 } \
484 CONVERSION \
485 Py_DECREF(rv); \
486 } \
487 return RETURN; \
488}
489
Fred Drake6f987622000-08-25 18:03:30 +0000490#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
491 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
492 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000493
Fred Drake6f987622000-08-25 18:03:30 +0000494#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
495 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
496 rc = PyInt_AsLong(rv);, rc, \
497 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000498
Fred Drake6f987622000-08-25 18:03:30 +0000499VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000500 (void *userData, const XML_Char *name),
501 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000502
Fred Drake6f987622000-08-25 18:03:30 +0000503VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000504 (void *userData,
505 const XML_Char *target,
506 const XML_Char *data),
507 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000508
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000509#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000510VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000511 (void *userData, const XML_Char *data, int len),
512 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000513#else
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)", (self->returns_unicode
517 ? conv_string_len_to_unicode(data,len)
518 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000519#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000520
Fred Drake6f987622000-08-25 18:03:30 +0000521VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000522 (void *userData,
523 const XML_Char *entityName,
524 const XML_Char *base,
525 const XML_Char *systemId,
526 const XML_Char *publicId,
527 const XML_Char *notationName),
528 ("(O&O&O&O&O&)",
529 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
530 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
531 STRING_CONV_FUNC,notationName))
532
533#if EXPAT_VERSION >= 0x015f00
534#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
535VOID_HANDLER(EntityDecl,
536 (void *userData,
537 const XML_Char *entityName,
538 int is_parameter_entity,
539 const XML_Char *value,
540 int value_length,
541 const XML_Char *base,
542 const XML_Char *systemId,
543 const XML_Char *publicId,
544 const XML_Char *notationName),
545 ("O&iNO&O&O&O&",
546 STRING_CONV_FUNC,entityName, is_parameter_entity,
547 conv_string_len_to_utf8(value, value_length),
548 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
549 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
550#else
551VOID_HANDLER(EntityDecl,
552 (void *userData,
553 const XML_Char *entityName,
554 int is_parameter_entity,
555 const XML_Char *value,
556 int value_length,
557 const XML_Char *base,
558 const XML_Char *systemId,
559 const XML_Char *publicId,
560 const XML_Char *notationName),
561 ("O&iNO&O&O&O&",
562 STRING_CONV_FUNC,entityName, is_parameter_entity,
563 (self->returns_unicode
564 ? conv_string_len_to_unicode(value, value_length)
565 : conv_string_len_to_utf8(value, value_length)),
566 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
567 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
568#endif
569
570VOID_HANDLER(XmlDecl,
571 (void *userData,
572 const XML_Char *version,
573 const XML_Char *encoding,
574 int standalone),
575 ("(O&O&i)",
576 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
577 standalone))
578
579static PyObject *
580conv_content_model(XML_Content * const model,
581 PyObject *(*conv_string)(XML_Char *))
582{
583 PyObject *result = NULL;
584 PyObject *children = PyTuple_New(model->numchildren);
585 int i;
586
587 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000588 assert(model->numchildren < INT_MAX);
589 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000590 PyObject *child = conv_content_model(&model->children[i],
591 conv_string);
592 if (child == NULL) {
593 Py_XDECREF(children);
594 return NULL;
595 }
596 PyTuple_SET_ITEM(children, i, child);
597 }
598 result = Py_BuildValue("(iiO&N)",
599 model->type, model->quant,
600 conv_string,model->name, children);
601 }
602 return result;
603}
604
605static PyObject *
606conv_content_model_utf8(XML_Content * const model)
607{
608 return conv_content_model(model, conv_string_to_utf8);
609}
610
611#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
612static PyObject *
613conv_content_model_unicode(XML_Content * const model)
614{
615 return conv_content_model(model, conv_string_to_unicode);
616}
617
618VOID_HANDLER(ElementDecl,
619 (void *userData,
620 const XML_Char *name,
621 XML_Content *model),
622 ("O&O&",
623 STRING_CONV_FUNC,name,
624 (self->returns_unicode ? conv_content_model_unicode
625 : conv_content_model_utf8),model))
626#else
627VOID_HANDLER(ElementDecl,
628 (void *userData,
629 const XML_Char *name,
630 XML_Content *model),
631 ("O&O&",
632 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
633#endif
634
635VOID_HANDLER(AttlistDecl,
636 (void *userData,
637 const XML_Char *elname,
638 const XML_Char *attname,
639 const XML_Char *att_type,
640 const XML_Char *dflt,
641 int isrequired),
642 ("(O&O&O&O&i)",
643 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
644 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
645 isrequired))
646#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000647
Fred Drake6f987622000-08-25 18:03:30 +0000648VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000649 (void *userData,
650 const XML_Char *notationName,
651 const XML_Char *base,
652 const XML_Char *systemId,
653 const XML_Char *publicId),
654 ("(O&O&O&O&)",
655 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
656 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000657
Fred Drake6f987622000-08-25 18:03:30 +0000658VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000659 (void *userData,
660 const XML_Char *prefix,
661 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000662 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000663
Fred Drake6f987622000-08-25 18:03:30 +0000664VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000665 (void *userData,
666 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000667 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000668
Fred Drake6f987622000-08-25 18:03:30 +0000669VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000670 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33: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(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000674 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000675 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000676
Fred Drake6f987622000-08-25 18:03:30 +0000677VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000679 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000681#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000682VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000683 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000684 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685
Fred Drake6f987622000-08-25 18:03:30 +0000686VOID_HANDLER(DefaultHandlerExpand,
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. Kuchlingbeba0562000-06-27 00:33:30 +0000689#else
Fred Drake6f987622000-08-25 18:03:30 +0000690VOID_HANDLER(Default,
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)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000693 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000694 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000695
Fred Drake6f987622000-08-25 18:03:30 +0000696VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000697 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000698 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000699 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000700 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000702
Fred Drake6f987622000-08-25 18:03:30 +0000703INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000704 (void *userData),
705 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000706
Fred Drake6f987622000-08-25 18:03:30 +0000707RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000708 (XML_Parser parser,
709 const XML_Char *context,
710 const XML_Char *base,
711 const XML_Char *systemId,
712 const XML_Char *publicId),
713 int rc=0;,
714 ("(O&O&O&O&)",
715 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000716 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
717 rc = PyInt_AsLong(rv);, rc,
718 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000719
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000720/* XXX UnknownEncodingHandler */
721
Fred Drake85d835f2001-02-08 15:39:08 +0000722#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000723VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000724 (void *userData, const XML_Char *doctypeName),
725 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
726 Py_None, Py_None, -1))
727#elif EXPAT_VERSION >= 0x015f00
728VOID_HANDLER(StartDoctypeDecl,
729 (void *userData, const XML_Char *doctypeName,
730 const XML_Char *sysid, const XML_Char *pubid,
731 int has_internal_subset),
732 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
733 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
734 has_internal_subset))
735#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000736
Fred Drake85d835f2001-02-08 15:39:08 +0000737#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000738VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000739#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(ExternalParsedEntityDecl,
743 (void *userData, const XML_Char *entityName,
744 const XML_Char *base, const XML_Char *systemId,
745 const XML_Char *publicId),
746 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
747 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
748 STRING_CONV_FUNC, publicId))
749
750VOID_HANDLER(InternalParsedEntityDecl,
751 (void *userData, const XML_Char *entityName,
752 const XML_Char *replacementText, int replacementTextLength),
753 ("(O&O&i)", STRING_CONV_FUNC, entityName,
754 STRING_CONV_FUNC, replacementText, replacementTextLength))
755
Fred Drake85d835f2001-02-08 15:39:08 +0000756#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000757
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758/* ---------------------------------------------------------------- */
759
760static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000761"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000762Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000763
764static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000765xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000766{
Fred Drake0582df92000-07-12 04:49:00 +0000767 char *s;
768 int slen;
769 int isFinal = 0;
770 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000771
Fred Drake0582df92000-07-12 04:49:00 +0000772 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
773 return NULL;
774 rv = XML_Parse(self->itself, s, slen, isFinal);
775 if (PyErr_Occurred()) {
776 return NULL;
777 }
778 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000779 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000780 }
781 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000782}
783
Fred Drakeca1f4262000-09-21 20:10:23 +0000784/* File reading copied from cPickle */
785
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786#define BUF_SIZE 2048
787
Fred Drake0582df92000-07-12 04:49:00 +0000788static int
789readinst(char *buf, int buf_size, PyObject *meth)
790{
791 PyObject *arg = NULL;
792 PyObject *bytes = NULL;
793 PyObject *str = NULL;
794 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000795
Fred Drake676940b2000-09-22 15:21:31 +0000796 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000797 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000798
Fred Drakeca1f4262000-09-21 20:10:23 +0000799 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000800 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
Tim Peters954eef72000-09-22 06:01:11 +0000802 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000803
Fred Drakeca1f4262000-09-21 20:10:23 +0000804 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000805 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000806
Fred Drake0582df92000-07-12 04:49:00 +0000807 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000808 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000809 PyErr_Format(PyExc_TypeError,
810 "read() did not return a string object (type=%.400s)",
811 str->ob_type->tp_name);
812 goto finally;
813 }
814 len = PyString_GET_SIZE(str);
815 if (len > buf_size) {
816 PyErr_Format(PyExc_ValueError,
817 "read() returned too much data: "
818 "%i bytes requested, %i returned",
819 buf_size, len);
820 Py_DECREF(str);
821 goto finally;
822 }
823 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000824finally:
Fred Drake0582df92000-07-12 04:49:00 +0000825 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000826 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000827 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828}
829
830static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000831"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000832Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833
834static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000835xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836{
Fred Drake0582df92000-07-12 04:49:00 +0000837 int rv = 1;
838 PyObject *f;
839 FILE *fp;
840 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841
Fred Drake0582df92000-07-12 04:49:00 +0000842 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
843 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000844
Fred Drake0582df92000-07-12 04:49:00 +0000845 if (PyFile_Check(f)) {
846 fp = PyFile_AsFile(f);
847 }
848 else{
849 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000850 readmethod = PyObject_GetAttrString(f, "read");
851 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000852 PyErr_Clear();
853 PyErr_SetString(PyExc_TypeError,
854 "argument must have 'read' attribute");
855 return 0;
856 }
857 }
858 for (;;) {
859 int bytes_read;
860 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
861 if (buf == NULL)
862 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000863
Fred Drake0582df92000-07-12 04:49:00 +0000864 if (fp) {
865 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
866 if (bytes_read < 0) {
867 PyErr_SetFromErrno(PyExc_IOError);
868 return NULL;
869 }
870 }
871 else {
872 bytes_read = readinst(buf, BUF_SIZE, readmethod);
873 if (bytes_read < 0)
874 return NULL;
875 }
876 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
877 if (PyErr_Occurred())
878 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000879
Fred Drake0582df92000-07-12 04:49:00 +0000880 if (!rv || bytes_read == 0)
881 break;
882 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000883 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000884 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000885 }
Fred Drake0582df92000-07-12 04:49:00 +0000886 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000887}
888
889static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000890"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000891Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892
893static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000894xmlparse_SetBase(xmlparseobject *self, PyObject *args)
895{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896 char *base;
897
Fred Drake0582df92000-07-12 04:49:00 +0000898 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000900 if (!XML_SetBase(self->itself, base)) {
901 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903 Py_INCREF(Py_None);
904 return Py_None;
905}
906
907static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000908"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000909Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000910
911static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000912xmlparse_GetBase(xmlparseobject *self, PyObject *args)
913{
914 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000916
Fred Drake0582df92000-07-12 04:49:00 +0000917 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000918}
919
Fred Drakebd6101c2001-02-14 18:29:45 +0000920#if EXPAT_VERSION >= 0x015f00
921static char xmlparse_GetInputContext__doc__[] =
922"GetInputContext() -> string\n\
923Return the untranslated text of the input that caused the current event.\n\
924If the event was generated by a large amount of text (such as a start tag\n\
925for an element with many attributes), not all of the text may be available.";
926
927static PyObject *
928xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
929{
930 PyObject *result = NULL;
931
932 if (PyArg_ParseTuple(args, ":GetInputContext")) {
933 if (self->in_callback) {
934 int offset, size;
935 const char *buffer
936 = XML_GetInputContext(self->itself, &offset, &size);
937
938 if (buffer != NULL)
939 result = PyString_FromStringAndSize(buffer + offset, size);
940 else {
941 result = Py_None;
942 Py_INCREF(result);
943 }
944 }
945 else {
946 result = Py_None;
947 Py_INCREF(result);
948 }
949 }
950 return result;
951}
952#endif
953
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000954static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000955"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000956Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000957information passed to the ExternalEntityRefHandler.";
958
959static PyObject *
960xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
961{
962 char *context;
963 char *encoding = NULL;
964 xmlparseobject *new_parser;
965 int i;
966
Fred Drakecde79132001-04-25 16:01:30 +0000967 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate",
968 &context, &encoding)) {
969 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000970 }
971
972#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
973 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000974#else
Fred Drake85d835f2001-02-08 15:39:08 +0000975 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000976 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000977#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000978
979 if (new_parser == NULL)
980 return NULL;
981 new_parser->returns_unicode = self->returns_unicode;
982 new_parser->ordered_attributes = self->ordered_attributes;
983 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000984 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000985 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000986 encoding);
987 new_parser->handlers = 0;
988 PyObject_GC_Init(new_parser);
989
990 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000991 Py_DECREF(new_parser);
992 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000993 }
994
995 XML_SetUserData(new_parser->itself, (void *)new_parser);
996
997 /* allocate and clear handlers first */
998 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000999 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001000
1001 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001002 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001003 Py_DECREF(new_parser);
1004 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001005 }
1006 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001007
1008 /* then copy handlers from self */
1009 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001010 if (self->handlers[i]) {
1011 Py_INCREF(self->handlers[i]);
1012 new_parser->handlers[i] = self->handlers[i];
1013 handler_info[i].setter(new_parser->itself,
1014 handler_info[i].handler);
1015 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016 }
Fred Drake28adf522000-09-24 22:07:59 +00001017 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001018}
1019
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001020#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001021
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001022static char xmlparse_SetParamEntityParsing__doc__[] =
1023"SetParamEntityParsing(flag) -> success\n\
1024Controls parsing of parameter entities (including the external DTD\n\
1025subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1026XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1027XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1028was successful.";
1029
1030static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001031xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001032{
Fred Drake85d835f2001-02-08 15:39:08 +00001033 int flag;
1034 if (!PyArg_ParseTuple(args, "i", &flag))
1035 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001036 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001037 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001038}
1039
Fred Drake85d835f2001-02-08 15:39:08 +00001040#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001041
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001042static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001043 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001044 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001045 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001046 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001047 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001048 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001049 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +00001050 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001051 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1052 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001053#if EXPAT_VERSION >= 0x010200
Fred Drakebd6101c2001-02-14 18:29:45 +00001054 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1055 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1056#endif
1057#if EXPAT_VERSION >= 0x015f00
1058 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1059 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001060#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001061 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001062};
1063
1064/* ---------- */
1065
1066
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001067#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1068
1069/*
1070 pyexpat international encoding support.
1071 Make it as simple as possible.
1072*/
1073
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001074static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001075PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001076
1077static void
1078init_template_buffer(void)
1079{
1080 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001081 for (i = 0; i < 256; i++) {
1082 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001083 }
Fred Drakebb66a202001-03-01 20:48:17 +00001084 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001085}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001086
1087int
1088PyUnknownEncodingHandler(void *encodingHandlerData,
1089const XML_Char *name,
1090XML_Encoding * info)
1091{
Fred Drakebb66a202001-03-01 20:48:17 +00001092 PyUnicodeObject *_u_string = NULL;
1093 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001094 int i;
1095
Fred Drakebb66a202001-03-01 20:48:17 +00001096 /* Yes, supports only 8bit encodings */
1097 _u_string = (PyUnicodeObject *)
1098 PyUnicode_Decode(template_buffer, 256, name, "replace");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099
Fred Drakebb66a202001-03-01 20:48:17 +00001100 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101 return result;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102
Fred Drakebb66a202001-03-01 20:48:17 +00001103 for (i = 0; i < 256; i++) {
1104 /* Stupid to access directly, but fast */
1105 Py_UNICODE c = _u_string->str[i];
1106 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001108 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001109 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001110 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001111
1112 info->data = NULL;
1113 info->convert = NULL;
1114 info->release = NULL;
1115 result=1;
1116
1117 Py_DECREF(_u_string);
1118 return result;
1119}
1120
1121#endif
1122
1123static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001124newxmlparseobject(char *encoding, char *namespace_separator)
1125{
1126 int i;
1127 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001128
1129#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001130 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1131 if (self == NULL)
1132 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001133
Fred Drake0582df92000-07-12 04:49:00 +00001134 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001135#else
Fred Drake0582df92000-07-12 04:49:00 +00001136 /* Code for versions 1.6 and later */
1137 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1138 if (self == NULL)
1139 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001140
Fred Drake0582df92000-07-12 04:49:00 +00001141 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001142#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001143 self->ordered_attributes = 0;
1144 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001145 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001146 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001147 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001148 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1149 }
Fred Drake85d835f2001-02-08 15:39:08 +00001150 else {
Fred Drake0582df92000-07-12 04:49:00 +00001151 self->itself = XML_ParserCreate(encoding);
1152 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001153 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001154 if (self->itself == NULL) {
1155 PyErr_SetString(PyExc_RuntimeError,
1156 "XML_ParserCreate failed");
1157 Py_DECREF(self);
1158 return NULL;
1159 }
1160 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001161#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1162#else
1163 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1164#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001165
Fred Drake0582df92000-07-12 04:49:00 +00001166 for(i = 0; handler_info[i].name != NULL; i++)
1167 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001168
Fred Drake0582df92000-07-12 04:49:00 +00001169 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001170 if (!self->handlers){
1171 Py_DECREF(self);
1172 return PyErr_NoMemory();
1173 }
1174 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001175
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001176 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001177}
1178
1179
1180static void
Fred Drake0582df92000-07-12 04:49:00 +00001181xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001182{
Fred Drake0582df92000-07-12 04:49:00 +00001183 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001184 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001185 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001186 XML_ParserFree(self->itself);
1187 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001188
Fred Drake85d835f2001-02-08 15:39:08 +00001189 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001190 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001191 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001192 temp = self->handlers[i];
1193 self->handlers[i] = NULL;
1194 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001195 }
1196 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001197 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001198#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001199 /* Code for versions before 1.6 */
1200 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001201#else
Fred Drake0582df92000-07-12 04:49:00 +00001202 /* Code for versions 1.6 and later */
1203 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001204#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001205}
1206
Fred Drake0582df92000-07-12 04:49:00 +00001207static int
1208handlername2int(const char *name)
1209{
1210 int i;
1211 for (i=0; handler_info[i].name != NULL; i++) {
1212 if (strcmp(name, handler_info[i].name) == 0) {
1213 return i;
1214 }
1215 }
1216 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001217}
1218
1219static PyObject *
1220xmlparse_getattr(xmlparseobject *self, char *name)
1221{
Fred Drake0582df92000-07-12 04:49:00 +00001222 int handlernum;
1223 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001224 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001225 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001226 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001227 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001228 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001230 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1231 if (strcmp(name, "ordered_attributes") == 0)
1232 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001233 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001234 return PyInt_FromLong((long) self->returns_unicode);
1235 if (strcmp(name, "specified_attributes") == 0)
1236 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001237
Fred Drake0582df92000-07-12 04:49:00 +00001238 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001239
Fred Drake0582df92000-07-12 04:49:00 +00001240 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1241 Py_INCREF(self->handlers[handlernum]);
1242 return self->handlers[handlernum];
1243 }
1244 if (strcmp(name, "__members__") == 0) {
1245 int i;
1246 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001247 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001248 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001249 }
1250 PyList_Append(rc, PyString_FromString("ErrorCode"));
1251 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1252 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1253 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001254 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001255 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001256 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001257
Fred Drake0582df92000-07-12 04:49:00 +00001258 return rc;
1259 }
1260 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001261}
1262
Fred Drake6f987622000-08-25 18:03:30 +00001263static int
1264sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001265{
1266 int handlernum = handlername2int(name);
1267 if (handlernum != -1) {
1268 Py_INCREF(v);
1269 Py_XDECREF(self->handlers[handlernum]);
1270 self->handlers[handlernum] = v;
1271 handler_info[handlernum].setter(self->itself,
1272 handler_info[handlernum].handler);
1273 return 1;
1274 }
1275 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001276}
1277
1278static int
Fred Drake6f987622000-08-25 18:03:30 +00001279xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001280{
Fred Drake6f987622000-08-25 18:03:30 +00001281 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001282 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001283 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1284 return -1;
1285 }
Fred Drake85d835f2001-02-08 15:39:08 +00001286 if (strcmp(name, "ordered_attributes") == 0) {
1287 if (PyObject_IsTrue(v))
1288 self->ordered_attributes = 1;
1289 else
1290 self->ordered_attributes = 0;
1291 return 0;
1292 }
Fred Drake6f987622000-08-25 18:03:30 +00001293 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001294 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001295#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001296 PyErr_SetString(PyExc_ValueError,
1297 "Cannot return Unicode strings in Python 1.5");
1298 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001299#else
Fred Drake6f987622000-08-25 18:03:30 +00001300 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001301#endif
Fred Drake6f987622000-08-25 18:03:30 +00001302 }
1303 else
1304 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001305 return 0;
1306 }
1307 if (strcmp(name, "specified_attributes") == 0) {
1308 if (PyObject_IsTrue(v))
1309 self->specified_attributes = 1;
1310 else
1311 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001312 return 0;
1313 }
1314 if (sethandler(self, name, v)) {
1315 return 0;
1316 }
1317 PyErr_SetString(PyExc_AttributeError, name);
1318 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001319}
1320
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001321#ifdef WITH_CYCLE_GC
1322static int
1323xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1324{
Fred Drakecde79132001-04-25 16:01:30 +00001325 int i, err;
1326 for (i = 0; handler_info[i].name != NULL; i++) {
1327 if (!op->handlers[i])
1328 continue;
1329 err = visit(op->handlers[i], arg);
1330 if (err)
1331 return err;
1332 }
1333 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001334}
1335
1336static int
1337xmlparse_clear(xmlparseobject *op)
1338{
Fred Drakecde79132001-04-25 16:01:30 +00001339 clear_handlers(op, 1);
1340 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001341}
1342#endif
1343
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001344static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001345"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001346
1347static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001348 PyObject_HEAD_INIT(NULL)
1349 0, /*ob_size*/
1350 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001351 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001352 0, /*tp_itemsize*/
1353 /* methods */
1354 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1355 (printfunc)0, /*tp_print*/
1356 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1357 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1358 (cmpfunc)0, /*tp_compare*/
1359 (reprfunc)0, /*tp_repr*/
1360 0, /*tp_as_number*/
1361 0, /*tp_as_sequence*/
1362 0, /*tp_as_mapping*/
1363 (hashfunc)0, /*tp_hash*/
1364 (ternaryfunc)0, /*tp_call*/
1365 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001366 0, /* tp_getattro */
1367 0, /* tp_setattro */
1368 0, /* tp_as_buffer */
1369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1370 Xmlparsetype__doc__, /* Documentation string */
1371#ifdef WITH_CYCLE_GC
1372 (traverseproc)xmlparse_traverse, /* tp_traverse */
1373 (inquiry)xmlparse_clear /* tp_clear */
1374#else
1375 0, 0
1376#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377};
1378
1379/* End of code for xmlparser objects */
1380/* -------------------------------------------------------- */
1381
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001382static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001383"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1384Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385
1386static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001387pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1388{
Fred Drakecde79132001-04-25 16:01:30 +00001389 char *encoding = NULL;
1390 char *namespace_separator = NULL;
1391 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001392
Fred Drakecde79132001-04-25 16:01:30 +00001393 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
1394 &encoding, &namespace_separator))
1395 return NULL;
1396 if (namespace_separator != NULL
1397 && strlen(namespace_separator) > 1) {
1398 PyErr_SetString(PyExc_ValueError,
1399 "namespace_separator must be at most one"
1400 " character, omitted, or None");
1401 return NULL;
1402 }
1403 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001404}
1405
1406static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001407"ErrorString(errno) -> string\n\
1408Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001409
1410static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001411pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001412{
Fred Drake0582df92000-07-12 04:49:00 +00001413 long code = 0;
1414
1415 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1416 return NULL;
1417 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001418}
1419
1420/* List of methods defined in the module */
1421
1422static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001423 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1424 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1425 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1426 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001427
Fred Drake0582df92000-07-12 04:49:00 +00001428 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001429};
1430
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001431/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001432
1433static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001434"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001435
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001436#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001437
1438/* 1.5 compatibility: PyModule_AddObject */
1439static int
1440PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1441{
Fred Drakecde79132001-04-25 16:01:30 +00001442 PyObject *dict;
1443 if (!PyModule_Check(m) || o == NULL)
1444 return -1;
1445 dict = PyModule_GetDict(m);
1446 if (dict == NULL)
1447 return -1;
1448 if (PyDict_SetItemString(dict, name, o))
1449 return -1;
1450 Py_DECREF(o);
1451 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001452}
1453
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001454int
1455PyModule_AddIntConstant(PyObject *m, char *name, long value)
1456{
Fred Drakecde79132001-04-25 16:01:30 +00001457 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001458}
1459
Fred Drakea77254a2000-09-29 19:23:29 +00001460static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001461PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1462{
Fred Drakecde79132001-04-25 16:01:30 +00001463 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001464}
1465
1466#endif
1467
Fred Drake4113b132001-03-24 19:58:26 +00001468
1469/* Return a Python string that represents the version number without the
1470 * extra cruft added by revision control, even if the right options were
1471 * given to the "cvs export" command to make it not include the extra
1472 * cruft.
1473 */
1474static PyObject *
1475get_version_string(void)
1476{
1477 static char *rcsid = "$Revision$";
1478 char *rev = rcsid;
1479 int i = 0;
1480
1481 while (!isdigit(*rev))
1482 ++rev;
1483 while (rev[i] != ' ' && rev[i] != '\0')
1484 ++i;
1485
1486 return PyString_FromStringAndSize(rev, i);
1487}
1488
Fred Drakecde79132001-04-25 16:01:30 +00001489/* Initialization function for the module */
1490
1491#ifndef MODULE_NAME
1492#define MODULE_NAME "pyexpat"
1493#endif
1494
1495#ifndef MODULE_INITFUNC
1496#define MODULE_INITFUNC initpyexpat
1497#endif
1498
1499void MODULE_INITFUNC(void); /* avoid compiler warnings */
1500
Fred Drake6f987622000-08-25 18:03:30 +00001501DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001502MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001503{
1504 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001505 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001506 PyObject *errors_module;
1507 PyObject *modelmod_name;
1508 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001509 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001510
Fred Drake6f987622000-08-25 18:03:30 +00001511 if (errmod_name == NULL)
1512 return;
Fred Drakecde79132001-04-25 16:01:30 +00001513 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001514 if (modelmod_name == NULL)
1515 return;
Fred Drake6f987622000-08-25 18:03:30 +00001516
Fred Drake0582df92000-07-12 04:49:00 +00001517 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001518
Fred Drake0582df92000-07-12 04:49:00 +00001519 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001520 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001521 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001522
Fred Drake0582df92000-07-12 04:49:00 +00001523 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001524 if (ErrorObject == NULL) {
1525 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001526 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001527 if (ErrorObject == NULL)
1528 return;
1529 }
1530 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001531 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001532 Py_INCREF(ErrorObject);
1533 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001534 Py_INCREF(&Xmlparsetype);
1535 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001536
Fred Drake4113b132001-03-24 19:58:26 +00001537 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake85d835f2001-02-08 15:39:08 +00001538#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001539 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1540 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001541 {
1542 XML_Expat_Version info = XML_ExpatVersionInfo();
1543 PyModule_AddObject(m, "version_info",
1544 Py_BuildValue("(iii)", info.major,
1545 info.minor, info.micro));
1546 }
Fred Drake738293d2000-12-21 17:25:07 +00001547#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001548#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1549#else
1550 init_template_buffer();
1551#endif
Fred Drake0582df92000-07-12 04:49:00 +00001552 /* XXX When Expat supports some way of figuring out how it was
1553 compiled, this should check and set native_encoding
1554 appropriately.
1555 */
Fred Drake93adb692000-09-23 04:55:48 +00001556 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001557
Fred Drake85d835f2001-02-08 15:39:08 +00001558 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001559 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001560 errors_module = PyDict_GetItem(d, errmod_name);
1561 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001562 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001563 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001564 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001565 /* gives away the reference to errors_module */
1566 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001567 }
1568 }
Fred Drake6f987622000-08-25 18:03:30 +00001569 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001570 model_module = PyDict_GetItem(d, modelmod_name);
1571 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001572 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001573 if (model_module != NULL) {
1574 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1575 /* gives away the reference to model_module */
1576 PyModule_AddObject(m, "model", model_module);
1577 }
1578 }
1579 Py_DECREF(modelmod_name);
1580 if (errors_module == NULL || model_module == NULL)
1581 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001582 return;
1583
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001584#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001585 PyModule_AddStringConstant(errors_module, #name, \
1586 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001587
Fred Drake0582df92000-07-12 04:49:00 +00001588 MYCONST(XML_ERROR_NO_MEMORY);
1589 MYCONST(XML_ERROR_SYNTAX);
1590 MYCONST(XML_ERROR_NO_ELEMENTS);
1591 MYCONST(XML_ERROR_INVALID_TOKEN);
1592 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1593 MYCONST(XML_ERROR_PARTIAL_CHAR);
1594 MYCONST(XML_ERROR_TAG_MISMATCH);
1595 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1596 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1597 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1598 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1599 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1600 MYCONST(XML_ERROR_ASYNC_ENTITY);
1601 MYCONST(XML_ERROR_BAD_CHAR_REF);
1602 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1603 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1604 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1605 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1606 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001607 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1608 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1609 MYCONST(XML_ERROR_NOT_STANDALONE);
1610
Fred Drake85d835f2001-02-08 15:39:08 +00001611 PyModule_AddStringConstant(errors_module, "__doc__",
1612 "Constants used to describe error conditions.");
1613
Fred Drake93adb692000-09-23 04:55:48 +00001614#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001615
1616#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001617#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001618 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1619 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1620 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001621#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001622#endif
1623
Fred Drake85d835f2001-02-08 15:39:08 +00001624#if EXPAT_VERSION >= 0x015f00
1625#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1626 PyModule_AddStringConstant(model_module, "__doc__",
1627 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001628
Fred Drake85d835f2001-02-08 15:39:08 +00001629 MYCONST(XML_CTYPE_EMPTY);
1630 MYCONST(XML_CTYPE_ANY);
1631 MYCONST(XML_CTYPE_MIXED);
1632 MYCONST(XML_CTYPE_NAME);
1633 MYCONST(XML_CTYPE_CHOICE);
1634 MYCONST(XML_CTYPE_SEQ);
1635
1636 MYCONST(XML_CQUANT_NONE);
1637 MYCONST(XML_CQUANT_OPT);
1638 MYCONST(XML_CQUANT_REP);
1639 MYCONST(XML_CQUANT_PLUS);
1640#undef MYCONST
1641#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001642}
1643
Fred Drake6f987622000-08-25 18:03:30 +00001644static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001645clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001646{
Fred Drakecde79132001-04-25 16:01:30 +00001647 int i = 0;
1648 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001649
Fred Drakecde79132001-04-25 16:01:30 +00001650 for (; handler_info[i].name!=NULL; i++) {
1651 if (decref) {
1652 temp = self->handlers[i];
1653 self->handlers[i] = NULL;
1654 Py_XDECREF(temp);
1655 }
1656 self->handlers[i]=NULL;
1657 handler_info[i].setter(self->itself, NULL);
1658 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001659}
1660
Fred Drake6f987622000-08-25 18:03:30 +00001661typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001662
Fred Drake6f987622000-08-25 18:03:30 +00001663static void
1664pyxml_UpdatePairedHandlers(xmlparseobject *self,
1665 int startHandler,
1666 int endHandler,
1667 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001668{
Fred Drakecde79132001-04-25 16:01:30 +00001669 void *start_handler = NULL;
1670 void *end_handler = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001671
Fred Drake0582df92000-07-12 04:49:00 +00001672 if (self->handlers[startHandler]
Fred Drakecde79132001-04-25 16:01:30 +00001673 && self->handlers[endHandler] != Py_None) {
1674 start_handler = handler_info[startHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001675 }
1676 if (self->handlers[EndElement]
Fred Drakecde79132001-04-25 16:01:30 +00001677 && self->handlers[EndElement] != Py_None) {
1678 end_handler = handler_info[endHandler].handler;
Fred Drake0582df92000-07-12 04:49:00 +00001679 }
1680 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001681}
1682
Fred Drake6f987622000-08-25 18:03:30 +00001683static void
1684pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001685{
1686 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1687 StartElement, EndElement,
1688 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001689}
1690
Fred Drake6f987622000-08-25 18:03:30 +00001691static void
1692pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001693{
1694 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1695 StartElement, EndElement,
1696 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001697}
1698
Fred Drake6f987622000-08-25 18:03:30 +00001699static void
1700pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001701{
1702 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1703 StartNamespaceDecl, EndNamespaceDecl,
1704 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001705}
1706
Fred Drake6f987622000-08-25 18:03:30 +00001707static void
1708pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001709{
1710 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1711 StartNamespaceDecl, EndNamespaceDecl,
1712 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001713}
1714
Fred Drake6f987622000-08-25 18:03:30 +00001715static void
1716pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001717{
1718 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1719 StartCdataSection, EndCdataSection,
1720 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001721}
1722
Fred Drake6f987622000-08-25 18:03:30 +00001723static void
1724pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001725{
1726 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1727 StartCdataSection, EndCdataSection,
1728 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001729}
1730
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001731#if EXPAT_VERSION >= 0x010200
1732
1733static void
1734pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1735{
1736 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1737 StartDoctypeDecl, EndDoctypeDecl,
1738 (pairsetter)XML_SetDoctypeDeclHandler);
1739}
1740
1741static void
1742pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1743{
1744 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1745 StartDoctypeDecl, EndDoctypeDecl,
1746 (pairsetter)XML_SetDoctypeDeclHandler);
1747}
1748
1749#endif
1750
Fred Drake0582df92000-07-12 04:49:00 +00001751statichere struct HandlerInfo handler_info[] = {
1752 {"StartElementHandler",
1753 pyxml_SetStartElementHandler,
1754 (xmlhandler)my_StartElementHandler},
1755 {"EndElementHandler",
1756 pyxml_SetEndElementHandler,
1757 (xmlhandler)my_EndElementHandler},
1758 {"ProcessingInstructionHandler",
1759 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1760 (xmlhandler)my_ProcessingInstructionHandler},
1761 {"CharacterDataHandler",
1762 (xmlhandlersetter)XML_SetCharacterDataHandler,
1763 (xmlhandler)my_CharacterDataHandler},
1764 {"UnparsedEntityDeclHandler",
1765 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1766 (xmlhandler)my_UnparsedEntityDeclHandler },
1767 {"NotationDeclHandler",
1768 (xmlhandlersetter)XML_SetNotationDeclHandler,
1769 (xmlhandler)my_NotationDeclHandler },
1770 {"StartNamespaceDeclHandler",
1771 pyxml_SetStartNamespaceDeclHandler,
1772 (xmlhandler)my_StartNamespaceDeclHandler },
1773 {"EndNamespaceDeclHandler",
1774 pyxml_SetEndNamespaceDeclHandler,
1775 (xmlhandler)my_EndNamespaceDeclHandler },
1776 {"CommentHandler",
1777 (xmlhandlersetter)XML_SetCommentHandler,
1778 (xmlhandler)my_CommentHandler},
1779 {"StartCdataSectionHandler",
1780 pyxml_SetStartCdataSection,
1781 (xmlhandler)my_StartCdataSectionHandler},
1782 {"EndCdataSectionHandler",
1783 pyxml_SetEndCdataSection,
1784 (xmlhandler)my_EndCdataSectionHandler},
1785 {"DefaultHandler",
1786 (xmlhandlersetter)XML_SetDefaultHandler,
1787 (xmlhandler)my_DefaultHandler},
1788 {"DefaultHandlerExpand",
1789 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1790 (xmlhandler)my_DefaultHandlerExpandHandler},
1791 {"NotStandaloneHandler",
1792 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1793 (xmlhandler)my_NotStandaloneHandler},
1794 {"ExternalEntityRefHandler",
1795 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1796 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001797#if EXPAT_VERSION >= 0x010200
1798 {"StartDoctypeDeclHandler",
1799 pyxml_SetStartDoctypeDeclHandler,
1800 (xmlhandler)my_StartDoctypeDeclHandler},
1801 {"EndDoctypeDeclHandler",
1802 pyxml_SetEndDoctypeDeclHandler,
1803 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001804#endif
1805#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001806 {"ExternalParsedEntityDeclHandler",
1807 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1808 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1809 {"InternalParsedEntityDeclHandler",
1810 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1811 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001812#endif
1813#if EXPAT_VERSION >= 0x015f00
1814 {"EntityDeclHandler",
1815 (xmlhandlersetter)XML_SetEntityDeclHandler,
1816 (xmlhandler)my_EntityDeclHandler},
1817 {"XmlDeclHandler",
1818 (xmlhandlersetter)XML_SetXmlDeclHandler,
1819 (xmlhandler)my_XmlDeclHandler},
1820 {"ElementDeclHandler",
1821 (xmlhandlersetter)XML_SetElementDeclHandler,
1822 (xmlhandler)my_ElementDeclHandler},
1823 {"AttlistDeclHandler",
1824 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1825 (xmlhandler)my_AttlistDeclHandler},
1826#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001827
Fred Drake0582df92000-07-12 04:49:00 +00001828 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001829};