blob: ce820142a761540f9ccc8100d1fba21b52272f2f [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake8188e792001-11-18 02:36:07 +00002#if PY_VERSION_HEX < 0x020000B1
3#include <assert.h>
4#endif
Fred Drake4113b132001-03-24 19:58:26 +00005#include <ctype.h>
6
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007#include "compile.h"
8#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00009#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010
11#ifndef PyGC_HEAD_SIZE
12#define PyGC_HEAD_SIZE 0
13#define PyObject_GC_Init(x)
14#define PyObject_GC_Fini(m)
15#define Py_TPFLAGS_GC 0
16#endif
17
Martin v. Löwis339d0f72001-08-17 18:39:25 +000018#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
19/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
20#define Py_USING_UNICODE
21#endif
22
Fred Drake0582df92000-07-12 04:49:00 +000023enum HandlerTypes {
24 StartElement,
25 EndElement,
26 ProcessingInstruction,
27 CharacterData,
28 UnparsedEntityDecl,
29 NotationDecl,
30 StartNamespaceDecl,
31 EndNamespaceDecl,
32 Comment,
33 StartCdataSection,
34 EndCdataSection,
35 Default,
36 DefaultHandlerExpand,
37 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000038 ExternalEntityRef,
39 StartDoctypeDecl,
40 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000041 EntityDecl,
42 XmlDecl,
43 ElementDecl,
44 AttlistDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000045 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000046};
47
48static PyObject *ErrorObject;
49
50/* ----------------------------------------------------- */
51
52/* Declarations for objects of type xmlparser */
53
54typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000055 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056
Fred Drake0582df92000-07-12 04:49:00 +000057 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000058 int returns_unicode; /* True if Unicode strings are returned;
59 if false, UTF-8 strings are returned */
60 int ordered_attributes; /* Return attributes as a list. */
61 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000062 int in_callback; /* Is a callback active? */
Fred Drakeb91a36b2002-06-27 19:40:48 +000063 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000064 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000065} xmlparseobject;
66
67staticforward PyTypeObject Xmlparsetype;
68
Fred Drake6f987622000-08-25 18:03:30 +000069typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070typedef void* xmlhandler;
71
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000072struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000073 const char *name;
74 xmlhandlersetter setter;
75 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000076 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000077 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078};
79
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000080staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000081
Fred Drakebd6101c2001-02-14 18:29:45 +000082/* Set an integer attribute on the error object; return true on success,
83 * false on an exception.
84 */
85static int
86set_error_attr(PyObject *err, char *name, int value)
87{
88 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000089
Fred Drakebd6101c2001-02-14 18:29:45 +000090 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
91 Py_DECREF(v);
92 return 0;
93 }
94 return 1;
95}
96
97/* Build and set an Expat exception, including positioning
98 * information. Always returns NULL.
99 */
Fred Drake85d835f2001-02-08 15:39:08 +0000100static PyObject *
101set_error(xmlparseobject *self)
102{
103 PyObject *err;
104 char buffer[256];
105 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000106 int lineno = XML_GetErrorLineNumber(parser);
107 int column = XML_GetErrorColumnNumber(parser);
108 enum XML_Error code = XML_GetErrorCode(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000109
Tim Peters885d4572001-11-28 20:27:42 +0000110 PyOS_snprintf(buffer, sizeof(buffer), "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000111 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000112 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 if ( err != NULL
114 && set_error_attr(err, "code", code)
115 && set_error_attr(err, "offset", column)
116 && set_error_attr(err, "lineno", lineno)) {
117 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000118 }
119 return NULL;
120}
121
Fred Drake71b63ff2002-06-28 22:29:01 +0000122static int
123have_handler(xmlparseobject *self, int type)
124{
125 PyObject *handler = self->handlers[type];
126 return handler != NULL;
127}
128
129static PyObject *
130get_handler_name(struct HandlerInfo *hinfo)
131{
132 PyObject *name = hinfo->nameobj;
133 if (name == NULL) {
134 name = PyString_FromString(hinfo->name);
135 hinfo->nameobj = name;
136 }
137 Py_XINCREF(name);
138 return name;
139}
140
Fred Drake85d835f2001-02-08 15:39:08 +0000141
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000142#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000143/* Convert a string of XML_Chars into a Unicode string.
144 Returns None if str is a null pointer. */
145
Fred Drake0582df92000-07-12 04:49:00 +0000146static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000147conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000148{
Fred Drake71b63ff2002-06-28 22:29:01 +0000149 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000150 and hence in UTF-8. */
151 /* UTF-8 from Expat, Unicode desired */
152 if (str == NULL) {
153 Py_INCREF(Py_None);
154 return Py_None;
155 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000156 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000157}
158
Fred Drake0582df92000-07-12 04:49:00 +0000159static PyObject *
160conv_string_len_to_unicode(const XML_Char *str, int len)
161{
Fred Drake71b63ff2002-06-28 22:29:01 +0000162 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000163 and hence in UTF-8. */
164 /* UTF-8 from Expat, Unicode desired */
165 if (str == NULL) {
166 Py_INCREF(Py_None);
167 return Py_None;
168 }
Fred Drake6f987622000-08-25 18:03:30 +0000169 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000170}
171#endif
172
173/* Convert a string of XML_Chars into an 8-bit Python string.
174 Returns None if str is a null pointer. */
175
Fred Drake6f987622000-08-25 18:03:30 +0000176static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000177conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000178{
Fred Drake71b63ff2002-06-28 22:29:01 +0000179 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000180 and hence in UTF-8. */
181 /* UTF-8 from Expat, UTF-8 desired */
182 if (str == NULL) {
183 Py_INCREF(Py_None);
184 return Py_None;
185 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000186 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000187}
188
Fred Drake6f987622000-08-25 18:03:30 +0000189static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000190conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000191{
Fred Drake71b63ff2002-06-28 22:29:01 +0000192 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000193 and hence in UTF-8. */
194 /* UTF-8 from Expat, UTF-8 desired */
195 if (str == NULL) {
196 Py_INCREF(Py_None);
197 return Py_None;
198 }
199 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000200}
201
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000202/* Callback routines */
203
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000204static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000205
Fred Drake6f987622000-08-25 18:03:30 +0000206static void
207flag_error(xmlparseobject *self)
208{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000209 clear_handlers(self, 0);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000210}
211
212static PyCodeObject*
213getcode(enum HandlerTypes slot, char* func_name, int lineno)
214{
Fred Drakebd6101c2001-02-14 18:29:45 +0000215 PyObject *code = NULL;
216 PyObject *name = NULL;
217 PyObject *nulltuple = NULL;
218 PyObject *filename = NULL;
219
220 if (handler_info[slot].tb_code == NULL) {
221 code = PyString_FromString("");
222 if (code == NULL)
223 goto failed;
224 name = PyString_FromString(func_name);
225 if (name == NULL)
226 goto failed;
227 nulltuple = PyTuple_New(0);
228 if (nulltuple == NULL)
229 goto failed;
230 filename = PyString_FromString(__FILE__);
231 handler_info[slot].tb_code =
232 PyCode_New(0, /* argcount */
233 0, /* nlocals */
234 0, /* stacksize */
235 0, /* flags */
236 code, /* code */
237 nulltuple, /* consts */
238 nulltuple, /* names */
239 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000240#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000241 nulltuple, /* freevars */
242 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000243#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000244 filename, /* filename */
245 name, /* name */
246 lineno, /* firstlineno */
247 code /* lnotab */
248 );
249 if (handler_info[slot].tb_code == NULL)
250 goto failed;
251 Py_DECREF(code);
252 Py_DECREF(nulltuple);
253 Py_DECREF(filename);
254 Py_DECREF(name);
255 }
256 return handler_info[slot].tb_code;
257 failed:
258 Py_XDECREF(code);
259 Py_XDECREF(name);
260 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000261}
262
263static PyObject*
264call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
265{
Fred Drakebd6101c2001-02-14 18:29:45 +0000266 PyThreadState *tstate = PyThreadState_GET();
267 PyFrameObject *f;
268 PyObject *res;
269
270 if (c == NULL)
271 return NULL;
272 f = PyFrame_New(
273 tstate, /*back*/
274 c, /*code*/
275 tstate->frame->f_globals, /*globals*/
276 NULL /*locals*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000277 );
278 if (f == NULL)
279 return NULL;
280 tstate->frame = f;
281 res = PyEval_CallObject(func, args);
282 if (res == NULL && tstate->curexc_traceback == NULL)
283 PyTraceBack_Here(f);
284 tstate->frame = f->f_back;
285 Py_DECREF(f);
286 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000287}
288
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000289#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000290#define STRING_CONV_FUNC conv_string_to_utf8
291#else
292/* Python 1.6 and later versions */
293#define STRING_CONV_FUNC (self->returns_unicode \
294 ? conv_string_to_unicode : conv_string_to_utf8)
295#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000296
Fred Drakeb91a36b2002-06-27 19:40:48 +0000297static PyObject*
298string_intern(xmlparseobject *self, const char* str)
299{
300 PyObject *result = STRING_CONV_FUNC(str);
301 PyObject *value;
302 if (!self->intern)
303 return result;
304 value = PyDict_GetItem(self->intern, result);
305 if (!value) {
306 if (PyDict_SetItem(self->intern, result, result) == 0)
307 return result;
308 else
309 return NULL;
310 }
311 Py_INCREF(value);
312 Py_DECREF(result);
313 return value;
314}
315
Fred Drake85d835f2001-02-08 15:39:08 +0000316static void
317my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000318 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000319{
320 xmlparseobject *self = (xmlparseobject *)userData;
321
Fred Drake71b63ff2002-06-28 22:29:01 +0000322 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000323 PyObject *container, *rv, *args;
324 int i, max;
325
326 /* Set max to the number of slots filled in atts[]; max/2 is
327 * the number of attributes we need to process.
328 */
329 if (self->specified_attributes) {
330 max = XML_GetSpecifiedAttributeCount(self->itself);
331 }
332 else {
333 max = 0;
334 while (atts[max] != NULL)
335 max += 2;
336 }
337 /* Build the container. */
338 if (self->ordered_attributes)
339 container = PyList_New(max);
340 else
341 container = PyDict_New();
342 if (container == NULL) {
343 flag_error(self);
344 return;
345 }
346 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000347 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000348 PyObject *v;
349 if (n == NULL) {
350 flag_error(self);
351 Py_DECREF(container);
352 return;
353 }
354 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
355 if (v == NULL) {
356 flag_error(self);
357 Py_DECREF(container);
358 Py_DECREF(n);
359 return;
360 }
361 if (self->ordered_attributes) {
362 PyList_SET_ITEM(container, i, n);
363 PyList_SET_ITEM(container, i+1, v);
364 }
365 else if (PyDict_SetItem(container, n, v)) {
366 flag_error(self);
367 Py_DECREF(n);
368 Py_DECREF(v);
369 return;
370 }
371 else {
372 Py_DECREF(n);
373 Py_DECREF(v);
374 }
375 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000376 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000377 if (args == NULL) {
378 Py_DECREF(container);
379 return;
380 }
381 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000382 self->in_callback = 1;
383 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake85d835f2001-02-08 15:39:08 +0000384 self->handlers[StartElement], args);
Fred Drakebd6101c2001-02-14 18:29:45 +0000385 self->in_callback = 0;
386 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000387 if (rv == NULL) {
388 flag_error(self);
389 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000390 }
Fred Drake85d835f2001-02-08 15:39:08 +0000391 Py_DECREF(rv);
392 }
393}
394
395#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
396 RETURN, GETUSERDATA) \
397static RC \
398my_##NAME##Handler PARAMS {\
399 xmlparseobject *self = GETUSERDATA ; \
400 PyObject *args = NULL; \
401 PyObject *rv = NULL; \
402 INIT \
403\
Fred Drake71b63ff2002-06-28 22:29:01 +0000404 if (have_handler(self, NAME)) { \
Fred Drake85d835f2001-02-08 15:39:08 +0000405 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000406 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000407 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000408 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
409 self->handlers[NAME], args); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000410 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000411 Py_DECREF(args); \
412 if (rv == NULL) { \
413 flag_error(self); \
414 return RETURN; \
415 } \
416 CONVERSION \
417 Py_DECREF(rv); \
418 } \
419 return RETURN; \
420}
421
Fred Drake6f987622000-08-25 18:03:30 +0000422#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
423 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
424 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000425
Fred Drake6f987622000-08-25 18:03:30 +0000426#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
427 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
428 rc = PyInt_AsLong(rv);, rc, \
429 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000430
Fred Drake71b63ff2002-06-28 22:29:01 +0000431VOID_HANDLER(EndElement,
432 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000433 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000434
Fred Drake6f987622000-08-25 18:03:30 +0000435VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000436 (void *userData,
437 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000438 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000439 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000440
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000441#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +0000442VOID_HANDLER(CharacterData,
443 (void *userData, const XML_Char *data, int len),
Fred Drake85d835f2001-02-08 15:39:08 +0000444 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000445#else
Fred Drake71b63ff2002-06-28 22:29:01 +0000446VOID_HANDLER(CharacterData,
447 (void *userData, const XML_Char *data, int len),
448 ("(N)", (self->returns_unicode
449 ? conv_string_len_to_unicode(data,len)
Fred Drake85d835f2001-02-08 15:39:08 +0000450 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000451#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000452
Fred Drake6f987622000-08-25 18:03:30 +0000453VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000454 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000455 const XML_Char *entityName,
456 const XML_Char *base,
457 const XML_Char *systemId,
458 const XML_Char *publicId,
459 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000460 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000461 string_intern(self, entityName), string_intern(self, base),
462 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000463 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000464
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000465#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000466VOID_HANDLER(EntityDecl,
467 (void *userData,
468 const XML_Char *entityName,
469 int is_parameter_entity,
470 const XML_Char *value,
471 int value_length,
472 const XML_Char *base,
473 const XML_Char *systemId,
474 const XML_Char *publicId,
475 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000476 ("NiNNNNN",
477 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000478 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000479 string_intern(self, base), string_intern(self, systemId),
480 string_intern(self, publicId),
481 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000482#else
483VOID_HANDLER(EntityDecl,
484 (void *userData,
485 const XML_Char *entityName,
486 int is_parameter_entity,
487 const XML_Char *value,
488 int value_length,
489 const XML_Char *base,
490 const XML_Char *systemId,
491 const XML_Char *publicId,
492 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000493 ("NiNNNNN",
494 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000495 (self->returns_unicode
496 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000497 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000498 string_intern(self, base), string_intern(self, systemId),
499 string_intern(self, publicId),
500 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000501#endif
502
503VOID_HANDLER(XmlDecl,
504 (void *userData,
505 const XML_Char *version,
506 const XML_Char *encoding,
507 int standalone),
508 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000509 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000510 standalone))
511
512static PyObject *
513conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000514 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000515{
516 PyObject *result = NULL;
517 PyObject *children = PyTuple_New(model->numchildren);
518 int i;
519
520 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000521 assert(model->numchildren < INT_MAX);
522 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000523 PyObject *child = conv_content_model(&model->children[i],
524 conv_string);
525 if (child == NULL) {
526 Py_XDECREF(children);
527 return NULL;
528 }
529 PyTuple_SET_ITEM(children, i, child);
530 }
531 result = Py_BuildValue("(iiO&N)",
532 model->type, model->quant,
533 conv_string,model->name, children);
534 }
535 return result;
536}
537
538static PyObject *
539conv_content_model_utf8(XML_Content * const model)
540{
541 return conv_content_model(model, conv_string_to_utf8);
542}
543
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000544#ifdef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000545static PyObject *
546conv_content_model_unicode(XML_Content * const model)
547{
548 return conv_content_model(model, conv_string_to_unicode);
549}
550
551VOID_HANDLER(ElementDecl,
552 (void *userData,
553 const XML_Char *name,
554 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000555 ("NO&",
556 string_intern(self, name),
Fred Drake85d835f2001-02-08 15:39:08 +0000557 (self->returns_unicode ? conv_content_model_unicode
558 : conv_content_model_utf8),model))
559#else
560VOID_HANDLER(ElementDecl,
561 (void *userData,
562 const XML_Char *name,
563 XML_Content *model),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000564 ("NO&",
565 string_intern(self, name), conv_content_model_utf8,model))
Fred Drake85d835f2001-02-08 15:39:08 +0000566#endif
567
568VOID_HANDLER(AttlistDecl,
569 (void *userData,
570 const XML_Char *elname,
571 const XML_Char *attname,
572 const XML_Char *att_type,
573 const XML_Char *dflt,
574 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000575 ("(NNO&O&i)",
576 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000577 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
578 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000579
Fred Drake71b63ff2002-06-28 22:29:01 +0000580VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000581 (void *userData,
582 const XML_Char *notationName,
583 const XML_Char *base,
584 const XML_Char *systemId,
585 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000586 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000587 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000588 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000589
Fred Drake6f987622000-08-25 18:03:30 +0000590VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000591 (void *userData,
592 const XML_Char *prefix,
593 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000594 ("(NN)",
595 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000596
Fred Drake6f987622000-08-25 18:03:30 +0000597VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000598 (void *userData,
599 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000600 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000601
Fred Drake6f987622000-08-25 18:03:30 +0000602VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000603 (void *userData, const XML_Char *data),
604 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000605
Fred Drake6f987622000-08-25 18:03:30 +0000606VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000607 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000608 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000609
Fred Drake6f987622000-08-25 18:03:30 +0000610VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000611 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000612 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000613
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000614#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000615VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000616 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000617 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000618
Fred Drake6f987622000-08-25 18:03:30 +0000619VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000620 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000621 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000622#else
Fred Drake6f987622000-08-25 18:03:30 +0000623VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000624 (void *userData, const XML_Char *s, int len),
625 ("(N)", (self->returns_unicode
626 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000627 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000628
Fred Drake6f987622000-08-25 18:03:30 +0000629VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000630 (void *userData, const XML_Char *s, int len),
631 ("(N)", (self->returns_unicode
632 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000633 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000634#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000635
Fred Drake71b63ff2002-06-28 22:29:01 +0000636INT_HANDLER(NotStandalone,
637 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000638 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000639
Fred Drake6f987622000-08-25 18:03:30 +0000640RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000641 (XML_Parser parser,
642 const XML_Char *context,
643 const XML_Char *base,
644 const XML_Char *systemId,
645 const XML_Char *publicId),
646 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000647 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000648 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000649 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000650 rc = PyInt_AsLong(rv);, rc,
651 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000652
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000653/* XXX UnknownEncodingHandler */
654
Fred Drake85d835f2001-02-08 15:39:08 +0000655VOID_HANDLER(StartDoctypeDecl,
656 (void *userData, const XML_Char *doctypeName,
657 const XML_Char *sysid, const XML_Char *pubid,
658 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000659 ("(NNNi)", string_intern(self, doctypeName),
660 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000661 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000662
663VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000664
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000665/* ---------------------------------------------------------------- */
666
Fred Drake71b63ff2002-06-28 22:29:01 +0000667static PyObject *
668get_parse_result(xmlparseobject *self, int rv)
669{
670 if (PyErr_Occurred()) {
671 return NULL;
672 }
673 if (rv == 0) {
674 return set_error(self);
675 }
676 return PyInt_FromLong(rv);
677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000680"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000681Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682
683static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000684xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000685{
Fred Drake0582df92000-07-12 04:49:00 +0000686 char *s;
687 int slen;
688 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000689
Fred Drake0582df92000-07-12 04:49:00 +0000690 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
691 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000692
693 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000694}
695
Fred Drakeca1f4262000-09-21 20:10:23 +0000696/* File reading copied from cPickle */
697
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000698#define BUF_SIZE 2048
699
Fred Drake0582df92000-07-12 04:49:00 +0000700static int
701readinst(char *buf, int buf_size, PyObject *meth)
702{
703 PyObject *arg = NULL;
704 PyObject *bytes = NULL;
705 PyObject *str = NULL;
706 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000707
Fred Drake676940b2000-09-22 15:21:31 +0000708 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000709 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000710
Fred Drakeca1f4262000-09-21 20:10:23 +0000711 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000712 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000713
Tim Peters954eef72000-09-22 06:01:11 +0000714 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000715
Fred Drakeca1f4262000-09-21 20:10:23 +0000716 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000717 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718
Fred Drake0582df92000-07-12 04:49:00 +0000719 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000720 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000721 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000722 "read() did not return a string object (type=%.400s)",
723 str->ob_type->tp_name);
724 goto finally;
725 }
726 len = PyString_GET_SIZE(str);
727 if (len > buf_size) {
728 PyErr_Format(PyExc_ValueError,
729 "read() returned too much data: "
730 "%i bytes requested, %i returned",
731 buf_size, len);
732 Py_DECREF(str);
733 goto finally;
734 }
735 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000736finally:
Fred Drake0582df92000-07-12 04:49:00 +0000737 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000738 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000739 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000740}
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000743"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000744Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000745
746static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000747xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000748{
Fred Drake0582df92000-07-12 04:49:00 +0000749 int rv = 1;
750 PyObject *f;
751 FILE *fp;
752 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000753
Fred Drake0582df92000-07-12 04:49:00 +0000754 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
755 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756
Fred Drake0582df92000-07-12 04:49:00 +0000757 if (PyFile_Check(f)) {
758 fp = PyFile_AsFile(f);
759 }
760 else{
761 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000762 readmethod = PyObject_GetAttrString(f, "read");
763 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000764 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000765 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000766 "argument must have 'read' attribute");
767 return 0;
768 }
769 }
770 for (;;) {
771 int bytes_read;
772 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
773 if (buf == NULL)
774 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000775
Fred Drake0582df92000-07-12 04:49:00 +0000776 if (fp) {
777 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
778 if (bytes_read < 0) {
779 PyErr_SetFromErrno(PyExc_IOError);
780 return NULL;
781 }
782 }
783 else {
784 bytes_read = readinst(buf, BUF_SIZE, readmethod);
785 if (bytes_read < 0)
786 return NULL;
787 }
788 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
789 if (PyErr_Occurred())
790 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000791
Fred Drake0582df92000-07-12 04:49:00 +0000792 if (!rv || bytes_read == 0)
793 break;
794 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000795 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000796}
797
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000798PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000799"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000800Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
802static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000803xmlparse_SetBase(xmlparseobject *self, PyObject *args)
804{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000805 char *base;
806
Fred Drake0582df92000-07-12 04:49:00 +0000807 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000808 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000809 if (!XML_SetBase(self->itself, base)) {
810 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000811 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000812 Py_INCREF(Py_None);
813 return Py_None;
814}
815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000816PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000817"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000819
820static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000821xmlparse_GetBase(xmlparseobject *self, PyObject *args)
822{
823 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000824 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825
Fred Drake0582df92000-07-12 04:49:00 +0000826 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000827}
828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000829PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000830"GetInputContext() -> string\n\
831Return the untranslated text of the input that caused the current event.\n\
832If the event was generated by a large amount of text (such as a start tag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000834
835static PyObject *
836xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
837{
838 PyObject *result = NULL;
839
840 if (PyArg_ParseTuple(args, ":GetInputContext")) {
841 if (self->in_callback) {
842 int offset, size;
843 const char *buffer
844 = XML_GetInputContext(self->itself, &offset, &size);
845
846 if (buffer != NULL)
847 result = PyString_FromStringAndSize(buffer + offset, size);
848 else {
849 result = Py_None;
850 Py_INCREF(result);
851 }
852 }
853 else {
854 result = Py_None;
855 Py_INCREF(result);
856 }
857 }
858 return result;
859}
Fred Drakebd6101c2001-02-14 18:29:45 +0000860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000862"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000863Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000864information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000865
866static PyObject *
867xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
868{
869 char *context;
870 char *encoding = NULL;
871 xmlparseobject *new_parser;
872 int i;
873
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000874 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000875 &context, &encoding)) {
876 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000877 }
878
879#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
880 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000881#else
Martin v. Löwis894258c2001-09-23 10:20:10 +0000882#ifndef Py_TPFLAGS_HAVE_GC
883 /* Python versions 1.6 to 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000884 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000885#else
886 /* Python versions 2.2 and later */
887 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
888#endif
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000889#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000890
891 if (new_parser == NULL)
892 return NULL;
893 new_parser->returns_unicode = self->returns_unicode;
894 new_parser->ordered_attributes = self->ordered_attributes;
895 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000896 new_parser->in_callback = 0;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000897 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000898 encoding);
899 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000900 new_parser->intern = self->intern;
901 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000902#ifdef Py_TPFLAGS_HAVE_GC
903 PyObject_GC_Track(new_parser);
904#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000905 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000906#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000907
908 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000909 Py_DECREF(new_parser);
910 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000911 }
912
913 XML_SetUserData(new_parser->itself, (void *)new_parser);
914
915 /* allocate and clear handlers first */
916 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000917 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000918
919 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000920 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000921 Py_DECREF(new_parser);
922 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000923 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000924 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000925
926 /* then copy handlers from self */
927 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000928 PyObject *handler = self->handlers[i];
929 if (handler != NULL) {
930 Py_INCREF(handler);
931 new_parser->handlers[i] = handler;
932 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +0000933 handler_info[i].handler);
934 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000935 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000936 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000937}
938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000940"SetParamEntityParsing(flag) -> success\n\
941Controls parsing of parameter entities (including the external DTD\n\
942subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
943XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
944XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000945was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000946
947static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +0000948xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000949{
Fred Drake85d835f2001-02-08 15:39:08 +0000950 int flag;
951 if (!PyArg_ParseTuple(args, "i", &flag))
952 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +0000953 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +0000954 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000955}
956
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000957static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +0000958 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +0000959 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000960 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +0000961 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000962 {"SetBase", (PyCFunction)xmlparse_SetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000963 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +0000964 {"GetBase", (PyCFunction)xmlparse_GetBase,
Fred Drakebd6101c2001-02-14 18:29:45 +0000965 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000966 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
967 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000968 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
969 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000970 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
971 METH_VARARGS, xmlparse_GetInputContext__doc__},
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000972 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000973};
974
975/* ---------- */
976
977
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000978#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000979
Fred Drake71b63ff2002-06-28 22:29:01 +0000980/* pyexpat international encoding support.
981 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000982*/
983
Martin v. Löwis3af7cc02001-01-22 08:19:10 +0000984static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +0000985PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000986
Fred Drake71b63ff2002-06-28 22:29:01 +0000987static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000988init_template_buffer(void)
989{
990 int i;
Fred Drakebb66a202001-03-01 20:48:17 +0000991 for (i = 0; i < 256; i++) {
992 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +0000993 }
Fred Drakebb66a202001-03-01 20:48:17 +0000994 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +0000995}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000996
Fred Drake71b63ff2002-06-28 22:29:01 +0000997static int
998PyUnknownEncodingHandler(void *encodingHandlerData,
999 const XML_Char *name,
1000 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001001{
Fred Drakebb66a202001-03-01 20:48:17 +00001002 PyUnicodeObject *_u_string = NULL;
1003 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001004 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001005
Fred Drakebb66a202001-03-01 20:48:17 +00001006 /* Yes, supports only 8bit encodings */
1007 _u_string = (PyUnicodeObject *)
1008 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001009
Fred Drakebb66a202001-03-01 20:48:17 +00001010 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001011 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001012
Fred Drakebb66a202001-03-01 20:48:17 +00001013 for (i = 0; i < 256; i++) {
1014 /* Stupid to access directly, but fast */
1015 Py_UNICODE c = _u_string->str[i];
1016 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001017 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001018 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001019 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001020 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001021 info->data = NULL;
1022 info->convert = NULL;
1023 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001024 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001025 Py_DECREF(_u_string);
1026 return result;
1027}
1028
1029#endif
1030
1031static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001032newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001033{
1034 int i;
1035 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001036
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001037#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001038 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1039 if (self == NULL)
1040 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001041
Fred Drake0582df92000-07-12 04:49:00 +00001042 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001043#else
Fred Drake0582df92000-07-12 04:49:00 +00001044 /* Code for versions 1.6 and later */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001045#ifdef Py_TPFLAGS_HAVE_GC
1046 /* Code for versions 2.2 and later */
1047 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1048#else
Fred Drake0582df92000-07-12 04:49:00 +00001049 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001050#endif
Fred Drake0582df92000-07-12 04:49:00 +00001051 if (self == NULL)
1052 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001053
Fred Drake0582df92000-07-12 04:49:00 +00001054 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001055#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001056 self->ordered_attributes = 0;
1057 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001058 self->in_callback = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001059 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001060 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001061 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1062 }
Fred Drake85d835f2001-02-08 15:39:08 +00001063 else {
Fred Drake0582df92000-07-12 04:49:00 +00001064 self->itself = XML_ParserCreate(encoding);
1065 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001066 self->intern = intern;
1067 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001068#ifdef Py_TPFLAGS_HAVE_GC
1069 PyObject_GC_Track(self);
1070#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001071 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001072#endif
Fred Drake0582df92000-07-12 04:49:00 +00001073 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001074 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001075 "XML_ParserCreate failed");
1076 Py_DECREF(self);
1077 return NULL;
1078 }
1079 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001080#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001081 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1082#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001083
Fred Drake0582df92000-07-12 04:49:00 +00001084 for(i = 0; handler_info[i].name != NULL; i++)
1085 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001086
Fred Drake0582df92000-07-12 04:49:00 +00001087 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001088 if (!self->handlers){
Fred Drake71b63ff2002-06-28 22:29:01 +00001089 Py_DECREF(self);
1090 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001091 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001092 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001093
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001094 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001095}
1096
1097
1098static void
Fred Drake0582df92000-07-12 04:49:00 +00001099xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001100{
Fred Drake0582df92000-07-12 04:49:00 +00001101 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001102#ifdef Py_TPFLAGS_HAVE_GC
1103 PyObject_GC_UnTrack(self);
1104#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001105 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001106#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001107 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001108 XML_ParserFree(self->itself);
1109 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001110
Fred Drake85d835f2001-02-08 15:39:08 +00001111 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001112 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001113 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001114 temp = self->handlers[i];
1115 self->handlers[i] = NULL;
1116 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001117 }
1118 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001119 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001120 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001121 Py_XDECREF(self->intern);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001122#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001123 /* Code for versions before 1.6 */
1124 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001125#else
Martin v. Löwis894258c2001-09-23 10:20:10 +00001126#ifndef Py_TPFLAGS_HAVE_GC
1127 /* Code for versions 1.6 to 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001128 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001129#else
1130 /* Code for versions 2.2 and later. */
1131 PyObject_GC_Del(self);
1132#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001133#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001134}
1135
Fred Drake0582df92000-07-12 04:49:00 +00001136static int
1137handlername2int(const char *name)
1138{
1139 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001140 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001141 if (strcmp(name, handler_info[i].name) == 0) {
1142 return i;
1143 }
1144 }
1145 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001146}
1147
1148static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001149get_pybool(int istrue)
1150{
1151 PyObject *result = istrue ? Py_True : Py_False;
1152 Py_INCREF(result);
1153 return result;
1154}
1155
1156static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001157xmlparse_getattr(xmlparseobject *self, char *name)
1158{
Fred Drake71b63ff2002-06-28 22:29:01 +00001159 int handlernum = handlername2int(name);
1160
1161 if (handlernum != -1) {
1162 PyObject *result = self->handlers[handlernum];
1163 if (result == NULL)
1164 result = Py_None;
1165 Py_INCREF(result);
1166 return result;
1167 }
1168 if (name[0] == 'E') {
1169 if (strcmp(name, "ErrorCode") == 0)
1170 return PyInt_FromLong((long)
1171 XML_GetErrorCode(self->itself));
1172 if (strcmp(name, "ErrorLineNumber") == 0)
1173 return PyInt_FromLong((long)
1174 XML_GetErrorLineNumber(self->itself));
1175 if (strcmp(name, "ErrorColumnNumber") == 0)
1176 return PyInt_FromLong((long)
1177 XML_GetErrorColumnNumber(self->itself));
1178 if (strcmp(name, "ErrorByteIndex") == 0)
1179 return PyInt_FromLong((long)
1180 XML_GetErrorByteIndex(self->itself));
1181 }
Fred Drake85d835f2001-02-08 15:39:08 +00001182 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001183 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001184 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001185 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001186 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001187 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001188 if (strcmp(name, "intern") == 0) {
1189 if (self->intern == NULL) {
1190 Py_INCREF(Py_None);
1191 return Py_None;
1192 }
1193 else {
1194 Py_INCREF(self->intern);
1195 return self->intern;
1196 }
1197 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001198
Fred Drake0582df92000-07-12 04:49:00 +00001199 if (strcmp(name, "__members__") == 0) {
1200 int i;
1201 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001202 for (i = 0; handler_info[i].name != NULL; i++) {
1203 PyList_Append(rc, get_handler_name(&handler_info[i]));
Fred Drake0582df92000-07-12 04:49:00 +00001204 }
1205 PyList_Append(rc, PyString_FromString("ErrorCode"));
1206 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1207 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1208 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001209 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001210 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001211 PyList_Append(rc, PyString_FromString("specified_attributes"));
Fred Drakeb91a36b2002-06-27 19:40:48 +00001212 PyList_Append(rc, PyString_FromString("intern"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001213
Fred Drake0582df92000-07-12 04:49:00 +00001214 return rc;
1215 }
1216 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001217}
1218
Fred Drake6f987622000-08-25 18:03:30 +00001219static int
1220sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001221{
1222 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001223 if (handlernum >= 0) {
1224 xmlhandler c_handler = NULL;
1225 PyObject *temp = self->handlers[handlernum];
1226
1227 if (v == Py_None)
1228 v = NULL;
1229 else if (v != NULL) {
1230 Py_INCREF(v);
1231 c_handler = handler_info[handlernum].handler;
1232 }
Fred Drake0582df92000-07-12 04:49:00 +00001233 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001234 Py_XDECREF(temp);
1235 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001236 return 1;
1237 }
1238 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001239}
1240
1241static int
Fred Drake6f987622000-08-25 18:03:30 +00001242xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001243{
Fred Drake6f987622000-08-25 18:03:30 +00001244 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001245 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001246 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1247 return -1;
1248 }
Fred Drake85d835f2001-02-08 15:39:08 +00001249 if (strcmp(name, "ordered_attributes") == 0) {
1250 if (PyObject_IsTrue(v))
1251 self->ordered_attributes = 1;
1252 else
1253 self->ordered_attributes = 0;
1254 return 0;
1255 }
Fred Drake6f987622000-08-25 18:03:30 +00001256 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001257 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001258#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001259 PyErr_SetString(PyExc_ValueError,
1260 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001261 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001262#else
Fred Drake6f987622000-08-25 18:03:30 +00001263 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001264#endif
Fred Drake6f987622000-08-25 18:03:30 +00001265 }
1266 else
1267 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001268 return 0;
1269 }
1270 if (strcmp(name, "specified_attributes") == 0) {
1271 if (PyObject_IsTrue(v))
1272 self->specified_attributes = 1;
1273 else
1274 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001275 return 0;
1276 }
1277 if (sethandler(self, name, v)) {
1278 return 0;
1279 }
1280 PyErr_SetString(PyExc_AttributeError, name);
1281 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001282}
1283
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001284#ifdef WITH_CYCLE_GC
1285static int
1286xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1287{
Fred Drakecde79132001-04-25 16:01:30 +00001288 int i, err;
1289 for (i = 0; handler_info[i].name != NULL; i++) {
1290 if (!op->handlers[i])
1291 continue;
1292 err = visit(op->handlers[i], arg);
1293 if (err)
1294 return err;
1295 }
1296 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001297}
1298
1299static int
1300xmlparse_clear(xmlparseobject *op)
1301{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001302 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001303 Py_XDECREF(op->intern);
1304 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001305 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001306}
1307#endif
1308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001309PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001310
1311static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001312 PyObject_HEAD_INIT(NULL)
1313 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001314 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001315 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001316 0, /*tp_itemsize*/
1317 /* methods */
1318 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1319 (printfunc)0, /*tp_print*/
1320 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1321 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1322 (cmpfunc)0, /*tp_compare*/
1323 (reprfunc)0, /*tp_repr*/
1324 0, /*tp_as_number*/
1325 0, /*tp_as_sequence*/
1326 0, /*tp_as_mapping*/
1327 (hashfunc)0, /*tp_hash*/
1328 (ternaryfunc)0, /*tp_call*/
1329 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001330 0, /* tp_getattro */
1331 0, /* tp_setattro */
1332 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001333#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001334 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001335#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001336 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001337#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001338 Xmlparsetype__doc__, /* Documentation string */
1339#ifdef WITH_CYCLE_GC
1340 (traverseproc)xmlparse_traverse, /* tp_traverse */
1341 (inquiry)xmlparse_clear /* tp_clear */
1342#else
1343 0, 0
1344#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001345};
1346
1347/* End of code for xmlparser objects */
1348/* -------------------------------------------------------- */
1349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001351"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001352Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001353
1354static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001355pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1356{
Fred Drakecde79132001-04-25 16:01:30 +00001357 char *encoding = NULL;
1358 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001359 PyObject *intern = NULL;
1360 PyObject *result;
1361 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001362 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001363 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001364
Fred Drakeb91a36b2002-06-27 19:40:48 +00001365 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1366 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001367 return NULL;
1368 if (namespace_separator != NULL
1369 && strlen(namespace_separator) > 1) {
1370 PyErr_SetString(PyExc_ValueError,
1371 "namespace_separator must be at most one"
1372 " character, omitted, or None");
1373 return NULL;
1374 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001375 /* Explicitly passing None means no interning is desired.
1376 Not passing anything means that a new dictionary is used. */
1377 if (intern == Py_None)
1378 intern = NULL;
1379 else if (intern == NULL) {
1380 intern = PyDict_New();
1381 if (!intern)
1382 return NULL;
1383 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001384 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001385 else if (!PyDict_Check(intern)) {
1386 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1387 return NULL;
1388 }
1389
1390 result = newxmlparseobject(encoding, namespace_separator, intern);
1391 if (intern_decref) {
1392 Py_DECREF(intern);
1393 }
1394 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001395}
1396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001397PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001398"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001399Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001400
1401static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001402pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001403{
Fred Drake0582df92000-07-12 04:49:00 +00001404 long code = 0;
1405
1406 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1407 return NULL;
1408 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001409}
1410
1411/* List of methods defined in the module */
1412
1413static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001414 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1415 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1416 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1417 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001418
Fred Drake0582df92000-07-12 04:49:00 +00001419 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001420};
1421
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001422/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001424PyDoc_STRVAR(pyexpat_module_documentation,
1425"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001426
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001427#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001428
1429/* 1.5 compatibility: PyModule_AddObject */
1430static int
1431PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1432{
Fred Drakecde79132001-04-25 16:01:30 +00001433 PyObject *dict;
1434 if (!PyModule_Check(m) || o == NULL)
1435 return -1;
1436 dict = PyModule_GetDict(m);
1437 if (dict == NULL)
1438 return -1;
1439 if (PyDict_SetItemString(dict, name, o))
1440 return -1;
1441 Py_DECREF(o);
1442 return 0;
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001443}
1444
Fred Drake71b63ff2002-06-28 22:29:01 +00001445static int
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001446PyModule_AddIntConstant(PyObject *m, char *name, long value)
1447{
Fred Drakecde79132001-04-25 16:01:30 +00001448 return PyModule_AddObject(m, name, PyInt_FromLong(value));
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001449}
1450
Fred Drake71b63ff2002-06-28 22:29:01 +00001451static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001452PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1453{
Fred Drakecde79132001-04-25 16:01:30 +00001454 return PyModule_AddObject(m, name, PyString_FromString(value));
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001455}
1456
1457#endif
1458
Fred Drake4113b132001-03-24 19:58:26 +00001459
1460/* Return a Python string that represents the version number without the
1461 * extra cruft added by revision control, even if the right options were
1462 * given to the "cvs export" command to make it not include the extra
1463 * cruft.
1464 */
1465static PyObject *
1466get_version_string(void)
1467{
1468 static char *rcsid = "$Revision$";
1469 char *rev = rcsid;
1470 int i = 0;
1471
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001472 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001473 ++rev;
1474 while (rev[i] != ' ' && rev[i] != '\0')
1475 ++i;
1476
1477 return PyString_FromStringAndSize(rev, i);
1478}
1479
Fred Drakecde79132001-04-25 16:01:30 +00001480/* Initialization function for the module */
1481
1482#ifndef MODULE_NAME
1483#define MODULE_NAME "pyexpat"
1484#endif
1485
1486#ifndef MODULE_INITFUNC
1487#define MODULE_INITFUNC initpyexpat
1488#endif
1489
1490void MODULE_INITFUNC(void); /* avoid compiler warnings */
1491
Fred Drake6f987622000-08-25 18:03:30 +00001492DL_EXPORT(void)
Fred Drakecde79132001-04-25 16:01:30 +00001493MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001494{
1495 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001496 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001497 PyObject *errors_module;
1498 PyObject *modelmod_name;
1499 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001500 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001501
Fred Drake6f987622000-08-25 18:03:30 +00001502 if (errmod_name == NULL)
1503 return;
Fred Drakecde79132001-04-25 16:01:30 +00001504 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001505 if (modelmod_name == NULL)
1506 return;
Fred Drake6f987622000-08-25 18:03:30 +00001507
Fred Drake0582df92000-07-12 04:49:00 +00001508 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001509
Fred Drake0582df92000-07-12 04:49:00 +00001510 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001511 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001512 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001513
Fred Drake0582df92000-07-12 04:49:00 +00001514 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001515 if (ErrorObject == NULL) {
1516 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001517 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001518 if (ErrorObject == NULL)
1519 return;
1520 }
1521 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001522 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001523 Py_INCREF(ErrorObject);
1524 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001525 Py_INCREF(&Xmlparsetype);
1526 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001527
Fred Drake4113b132001-03-24 19:58:26 +00001528 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001529 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1530 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001531 {
1532 XML_Expat_Version info = XML_ExpatVersionInfo();
1533 PyModule_AddObject(m, "version_info",
1534 Py_BuildValue("(iii)", info.major,
1535 info.minor, info.micro));
1536 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001537#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001538 init_template_buffer();
1539#endif
Fred Drake0582df92000-07-12 04:49:00 +00001540 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001541 compiled, this should check and set native_encoding
1542 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001543 */
Fred Drake93adb692000-09-23 04:55:48 +00001544 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001545
Fred Drake85d835f2001-02-08 15:39:08 +00001546 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001547 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001548 errors_module = PyDict_GetItem(d, errmod_name);
1549 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001550 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001551 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001552 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001553 /* gives away the reference to errors_module */
1554 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001555 }
1556 }
Fred Drake6f987622000-08-25 18:03:30 +00001557 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001558 model_module = PyDict_GetItem(d, modelmod_name);
1559 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001560 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001561 if (model_module != NULL) {
1562 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1563 /* gives away the reference to model_module */
1564 PyModule_AddObject(m, "model", model_module);
1565 }
1566 }
1567 Py_DECREF(modelmod_name);
1568 if (errors_module == NULL || model_module == NULL)
1569 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001570 return;
1571
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001572#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001573 PyModule_AddStringConstant(errors_module, #name, \
1574 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001575
Fred Drake0582df92000-07-12 04:49:00 +00001576 MYCONST(XML_ERROR_NO_MEMORY);
1577 MYCONST(XML_ERROR_SYNTAX);
1578 MYCONST(XML_ERROR_NO_ELEMENTS);
1579 MYCONST(XML_ERROR_INVALID_TOKEN);
1580 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1581 MYCONST(XML_ERROR_PARTIAL_CHAR);
1582 MYCONST(XML_ERROR_TAG_MISMATCH);
1583 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1584 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1585 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1586 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1587 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1588 MYCONST(XML_ERROR_ASYNC_ENTITY);
1589 MYCONST(XML_ERROR_BAD_CHAR_REF);
1590 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1591 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1592 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1593 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1594 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001595 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1596 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1597 MYCONST(XML_ERROR_NOT_STANDALONE);
1598
Fred Drake85d835f2001-02-08 15:39:08 +00001599 PyModule_AddStringConstant(errors_module, "__doc__",
1600 "Constants used to describe error conditions.");
1601
Fred Drake93adb692000-09-23 04:55:48 +00001602#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001603
Fred Drake85d835f2001-02-08 15:39:08 +00001604#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001605 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1606 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1607 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001608#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001609
Fred Drake85d835f2001-02-08 15:39:08 +00001610#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1611 PyModule_AddStringConstant(model_module, "__doc__",
1612 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001613
Fred Drake85d835f2001-02-08 15:39:08 +00001614 MYCONST(XML_CTYPE_EMPTY);
1615 MYCONST(XML_CTYPE_ANY);
1616 MYCONST(XML_CTYPE_MIXED);
1617 MYCONST(XML_CTYPE_NAME);
1618 MYCONST(XML_CTYPE_CHOICE);
1619 MYCONST(XML_CTYPE_SEQ);
1620
1621 MYCONST(XML_CQUANT_NONE);
1622 MYCONST(XML_CQUANT_OPT);
1623 MYCONST(XML_CQUANT_REP);
1624 MYCONST(XML_CQUANT_PLUS);
1625#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001626}
1627
Fred Drake6f987622000-08-25 18:03:30 +00001628static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001629clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001630{
Fred Drakecde79132001-04-25 16:01:30 +00001631 int i = 0;
1632 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001633
Fred Drake71b63ff2002-06-28 22:29:01 +00001634 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001635 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001636 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001637 else {
Fred Drakecde79132001-04-25 16:01:30 +00001638 temp = self->handlers[i];
1639 self->handlers[i] = NULL;
1640 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001641 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001642 }
Fred Drakecde79132001-04-25 16:01:30 +00001643 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001644}
1645
Fred Drake0582df92000-07-12 04:49:00 +00001646statichere struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001647 {"StartElementHandler",
1648 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001649 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001650 {"EndElementHandler",
1651 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001652 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001653 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001654 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1655 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001656 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001657 (xmlhandlersetter)XML_SetCharacterDataHandler,
1658 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001659 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001660 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1661 (xmlhandler)my_UnparsedEntityDeclHandler },
Fred Drake71b63ff2002-06-28 22:29:01 +00001662 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001663 (xmlhandlersetter)XML_SetNotationDeclHandler,
1664 (xmlhandler)my_NotationDeclHandler },
Fred Drake71b63ff2002-06-28 22:29:01 +00001665 {"StartNamespaceDeclHandler",
1666 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001667 (xmlhandler)my_StartNamespaceDeclHandler },
Fred Drake71b63ff2002-06-28 22:29:01 +00001668 {"EndNamespaceDeclHandler",
1669 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001670 (xmlhandler)my_EndNamespaceDeclHandler },
1671 {"CommentHandler",
1672 (xmlhandlersetter)XML_SetCommentHandler,
1673 (xmlhandler)my_CommentHandler},
1674 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001675 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001676 (xmlhandler)my_StartCdataSectionHandler},
1677 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001678 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001679 (xmlhandler)my_EndCdataSectionHandler},
1680 {"DefaultHandler",
1681 (xmlhandlersetter)XML_SetDefaultHandler,
1682 (xmlhandler)my_DefaultHandler},
1683 {"DefaultHandlerExpand",
1684 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1685 (xmlhandler)my_DefaultHandlerExpandHandler},
1686 {"NotStandaloneHandler",
1687 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1688 (xmlhandler)my_NotStandaloneHandler},
1689 {"ExternalEntityRefHandler",
1690 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1691 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001692 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001693 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001694 (xmlhandler)my_StartDoctypeDeclHandler},
1695 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001696 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001697 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001698 {"EntityDeclHandler",
1699 (xmlhandlersetter)XML_SetEntityDeclHandler,
1700 (xmlhandler)my_EntityDeclHandler},
1701 {"XmlDeclHandler",
1702 (xmlhandlersetter)XML_SetXmlDeclHandler,
1703 (xmlhandler)my_XmlDeclHandler},
1704 {"ElementDeclHandler",
1705 (xmlhandlersetter)XML_SetElementDeclHandler,
1706 (xmlhandler)my_ElementDeclHandler},
1707 {"AttlistDeclHandler",
1708 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1709 (xmlhandler)my_AttlistDeclHandler},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001710
Fred Drake0582df92000-07-12 04:49:00 +00001711 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001712};