blob: d5929b699655c417ec100b284beee391023d4b96 [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00004#include "compile.h"
5#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00006#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00007
Martin v. Löwisc847f402003-01-21 11:09:21 +00008#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
9
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000010#ifndef PyDoc_STRVAR
Martin v. Löwis069dde22003-01-21 10:58:18 +000011
12/*
13 * fdrake says:
14 * Don't change the PyDoc_STR macro definition to (str), because
15 * '''the parentheses cause compile failures
16 * ("non-constant static initializer" or something like that)
17 * on some platforms (Irix?)'''
18 */
Fred Drakef57b22a2002-09-02 15:54:06 +000019#define PyDoc_STR(str) str
Fred Drake7c75bf22002-07-01 14:02:31 +000020#define PyDoc_VAR(name) static char name[]
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000021#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000022#endif
23
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000024#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
25/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000026#define Py_USING_UNICODE
Jeremy Hylton9263f572003-06-27 16:13:17 +000027#else
28#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000029#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,
47 StartDoctypeDecl,
48 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000049 EntityDecl,
50 XmlDecl,
51 ElementDecl,
52 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000053#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000054 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000055#endif
Fred Drake85d835f2001-02-08 15:39:08 +000056 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000057};
58
59static PyObject *ErrorObject;
60
61/* ----------------------------------------------------- */
62
63/* Declarations for objects of type xmlparser */
64
65typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000066 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000067
Fred Drake0582df92000-07-12 04:49:00 +000068 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000069 int returns_unicode; /* True if Unicode strings are returned;
70 if false, UTF-8 strings are returned */
71 int ordered_attributes; /* Return attributes as a list. */
72 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000073 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000074 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000075 XML_Char *buffer; /* Buffer used when accumulating characters */
76 /* NULL if not enabled */
77 int buffer_size; /* Size of buffer, in XML_Char units */
78 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000079 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000080 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000081} xmlparseobject;
82
Fred Drake2a3d7db2002-06-28 22:56:48 +000083#define CHARACTER_DATA_BUFFER_SIZE 8192
84
Jeremy Hylton938ace62002-07-17 16:30:39 +000085static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000086
Fred Drake117ac852002-09-24 16:24:54 +000087typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000088typedef void* xmlhandler;
89
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000090struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000091 const char *name;
92 xmlhandlersetter setter;
93 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000094 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000095 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000096};
97
Jeremy Hylton938ace62002-07-17 16:30:39 +000098static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000099
Fred Drakebd6101c2001-02-14 18:29:45 +0000100/* Set an integer attribute on the error object; return true on success,
101 * false on an exception.
102 */
103static int
104set_error_attr(PyObject *err, char *name, int value)
105{
106 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000107
Fred Drakebd6101c2001-02-14 18:29:45 +0000108 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
109 Py_DECREF(v);
110 return 0;
111 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000112 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 return 1;
114}
115
116/* Build and set an Expat exception, including positioning
117 * information. Always returns NULL.
118 */
Fred Drake85d835f2001-02-08 15:39:08 +0000119static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000120set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000121{
122 PyObject *err;
123 char buffer[256];
124 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 int lineno = XML_GetErrorLineNumber(parser);
126 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000127
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000128 /* There is no risk of overflowing this buffer, since
129 even for 64-bit integers, there is sufficient space. */
130 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000131 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000132 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000133 if ( err != NULL
134 && set_error_attr(err, "code", code)
135 && set_error_attr(err, "offset", column)
136 && set_error_attr(err, "lineno", lineno)) {
137 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000138 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000139 Py_DECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000140 return NULL;
141}
142
Fred Drake71b63ff2002-06-28 22:29:01 +0000143static int
144have_handler(xmlparseobject *self, int type)
145{
146 PyObject *handler = self->handlers[type];
147 return handler != NULL;
148}
149
150static PyObject *
151get_handler_name(struct HandlerInfo *hinfo)
152{
153 PyObject *name = hinfo->nameobj;
154 if (name == NULL) {
155 name = PyString_FromString(hinfo->name);
156 hinfo->nameobj = name;
157 }
158 Py_XINCREF(name);
159 return name;
160}
161
Fred Drake85d835f2001-02-08 15:39:08 +0000162
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000163#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000164/* Convert a string of XML_Chars into a Unicode string.
165 Returns None if str is a null pointer. */
166
Fred Drake0582df92000-07-12 04:49:00 +0000167static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000168conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000169{
Fred Drake71b63ff2002-06-28 22:29:01 +0000170 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000171 and hence in UTF-8. */
172 /* UTF-8 from Expat, Unicode desired */
173 if (str == NULL) {
174 Py_INCREF(Py_None);
175 return Py_None;
176 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000177 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000178}
179
Fred Drake0582df92000-07-12 04:49:00 +0000180static PyObject *
181conv_string_len_to_unicode(const XML_Char *str, int len)
182{
Fred Drake71b63ff2002-06-28 22:29:01 +0000183 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000184 and hence in UTF-8. */
185 /* UTF-8 from Expat, Unicode desired */
186 if (str == NULL) {
187 Py_INCREF(Py_None);
188 return Py_None;
189 }
Fred Drake6f987622000-08-25 18:03:30 +0000190 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000191}
192#endif
193
194/* Convert a string of XML_Chars into an 8-bit Python string.
195 Returns None if str is a null pointer. */
196
Fred Drake6f987622000-08-25 18:03:30 +0000197static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000198conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000199{
Fred Drake71b63ff2002-06-28 22:29:01 +0000200 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000201 and hence in UTF-8. */
202 /* UTF-8 from Expat, UTF-8 desired */
203 if (str == NULL) {
204 Py_INCREF(Py_None);
205 return Py_None;
206 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000207 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000208}
209
Fred Drake6f987622000-08-25 18:03:30 +0000210static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000211conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000212{
Fred Drake71b63ff2002-06-28 22:29:01 +0000213 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000214 and hence in UTF-8. */
215 /* UTF-8 from Expat, UTF-8 desired */
216 if (str == NULL) {
217 Py_INCREF(Py_None);
218 return Py_None;
219 }
220 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000221}
222
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000223/* Callback routines */
224
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000225static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000226
Martin v. Löwis069dde22003-01-21 10:58:18 +0000227/* This handler is used when an error has been detected, in the hope
228 that actual parsing can be terminated early. This will only help
229 if an external entity reference is encountered. */
230static int
231error_external_entity_ref_handler(XML_Parser parser,
232 const XML_Char *context,
233 const XML_Char *base,
234 const XML_Char *systemId,
235 const XML_Char *publicId)
236{
237 return 0;
238}
239
Fred Drake6f987622000-08-25 18:03:30 +0000240static void
241flag_error(xmlparseobject *self)
242{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000243 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000244 XML_SetExternalEntityRefHandler(self->itself,
245 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000246}
247
248static PyCodeObject*
249getcode(enum HandlerTypes slot, char* func_name, int lineno)
250{
Fred Drakebd6101c2001-02-14 18:29:45 +0000251 PyObject *code = NULL;
252 PyObject *name = NULL;
253 PyObject *nulltuple = NULL;
254 PyObject *filename = NULL;
255
256 if (handler_info[slot].tb_code == NULL) {
257 code = PyString_FromString("");
258 if (code == NULL)
259 goto failed;
260 name = PyString_FromString(func_name);
261 if (name == NULL)
262 goto failed;
263 nulltuple = PyTuple_New(0);
264 if (nulltuple == NULL)
265 goto failed;
266 filename = PyString_FromString(__FILE__);
267 handler_info[slot].tb_code =
268 PyCode_New(0, /* argcount */
269 0, /* nlocals */
270 0, /* stacksize */
271 0, /* flags */
272 code, /* code */
273 nulltuple, /* consts */
274 nulltuple, /* names */
275 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000276#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000277 nulltuple, /* freevars */
278 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000279#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000280 filename, /* filename */
281 name, /* name */
282 lineno, /* firstlineno */
283 code /* lnotab */
284 );
285 if (handler_info[slot].tb_code == NULL)
286 goto failed;
287 Py_DECREF(code);
288 Py_DECREF(nulltuple);
289 Py_DECREF(filename);
290 Py_DECREF(name);
291 }
292 return handler_info[slot].tb_code;
293 failed:
294 Py_XDECREF(code);
295 Py_XDECREF(name);
296 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000297}
298
Jeremy Hylton9263f572003-06-27 16:13:17 +0000299#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000300static int
301trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
302{
303 int result = 0;
304 if (!tstate->use_tracing || tstate->tracing)
305 return 0;
306 if (tstate->c_profilefunc != NULL) {
307 tstate->tracing++;
308 result = tstate->c_profilefunc(tstate->c_profileobj,
309 f, code , val);
310 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
311 || (tstate->c_profilefunc != NULL));
312 tstate->tracing--;
313 if (result)
314 return result;
315 }
316 if (tstate->c_tracefunc != NULL) {
317 tstate->tracing++;
318 result = tstate->c_tracefunc(tstate->c_traceobj,
319 f, code , val);
320 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
321 || (tstate->c_profilefunc != NULL));
322 tstate->tracing--;
323 }
324 return result;
325}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000326
327static int
328trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
329{
330 PyObject *type, *value, *traceback, *arg;
331 int err;
332
333 if (tstate->c_tracefunc == NULL)
334 return 0;
335
336 PyErr_Fetch(&type, &value, &traceback);
337 if (value == NULL) {
338 value = Py_None;
339 Py_INCREF(value);
340 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000341 arg = PyTuple_Pack(3, type, value, traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000342 if (arg == NULL) {
343 PyErr_Restore(type, value, traceback);
344 return 0;
345 }
346 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
347 Py_DECREF(arg);
348 if (err == 0)
349 PyErr_Restore(type, value, traceback);
350 else {
351 Py_XDECREF(type);
352 Py_XDECREF(value);
353 Py_XDECREF(traceback);
354 }
355 return err;
356}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000357#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000358
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000359static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000360call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
361 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000362{
Fred Drakebd6101c2001-02-14 18:29:45 +0000363 PyThreadState *tstate = PyThreadState_GET();
364 PyFrameObject *f;
365 PyObject *res;
366
367 if (c == NULL)
368 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000369
Jeremy Hylton9263f572003-06-27 16:13:17 +0000370 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000371 if (f == NULL)
372 return NULL;
373 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000374#ifdef FIX_TRACE
375 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000376 return NULL;
377 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000378#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000379 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000380 if (res == NULL) {
381 if (tstate->curexc_traceback == NULL)
382 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000383 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000384#ifdef FIX_TRACE
385 if (trace_frame_exc(tstate, f) < 0) {
386 return NULL;
387 }
388 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000389 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000390 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000391 Py_XDECREF(res);
392 res = NULL;
393 }
394 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000395#else
396 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000397#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000398 tstate->frame = f->f_back;
399 Py_DECREF(f);
400 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000401}
402
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000403#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000404#define STRING_CONV_FUNC conv_string_to_utf8
405#else
Martin v. Löwis069dde22003-01-21 10:58:18 +0000406/* Python 2.0 and later versions, when built with Unicode support */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000407#define STRING_CONV_FUNC (self->returns_unicode \
408 ? conv_string_to_unicode : conv_string_to_utf8)
409#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000410
Fred Drakeb91a36b2002-06-27 19:40:48 +0000411static PyObject*
412string_intern(xmlparseobject *self, const char* str)
413{
414 PyObject *result = STRING_CONV_FUNC(str);
415 PyObject *value;
416 if (!self->intern)
417 return result;
418 value = PyDict_GetItem(self->intern, result);
419 if (!value) {
420 if (PyDict_SetItem(self->intern, result, result) == 0)
421 return result;
422 else
423 return NULL;
424 }
425 Py_INCREF(value);
426 Py_DECREF(result);
427 return value;
428}
429
Fred Drake2a3d7db2002-06-28 22:56:48 +0000430/* Return 0 on success, -1 on exception.
431 * flag_error() will be called before return if needed.
432 */
433static int
434call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
435{
436 PyObject *args;
437 PyObject *temp;
438
439 args = PyTuple_New(1);
440 if (args == NULL)
441 return -1;
442#ifdef Py_USING_UNICODE
443 temp = (self->returns_unicode
444 ? conv_string_len_to_unicode(buffer, len)
445 : conv_string_len_to_utf8(buffer, len));
446#else
447 temp = conv_string_len_to_utf8(buffer, len);
448#endif
449 if (temp == NULL) {
450 Py_DECREF(args);
451 flag_error(self);
452 return -1;
453 }
454 PyTuple_SET_ITEM(args, 0, temp);
455 /* temp is now a borrowed reference; consider it unused. */
456 self->in_callback = 1;
457 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000458 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000459 /* temp is an owned reference again, or NULL */
460 self->in_callback = 0;
461 Py_DECREF(args);
462 if (temp == NULL) {
463 flag_error(self);
464 return -1;
465 }
466 Py_DECREF(temp);
467 return 0;
468}
469
470static int
471flush_character_buffer(xmlparseobject *self)
472{
473 int rc;
474 if (self->buffer == NULL || self->buffer_used == 0)
475 return 0;
476 rc = call_character_handler(self, self->buffer, self->buffer_used);
477 self->buffer_used = 0;
478 return rc;
479}
480
481static void
482my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
483{
484 xmlparseobject *self = (xmlparseobject *) userData;
485 if (self->buffer == NULL)
486 call_character_handler(self, data, len);
487 else {
488 if ((self->buffer_used + len) > self->buffer_size) {
489 if (flush_character_buffer(self) < 0)
490 return;
491 /* handler might have changed; drop the rest on the floor
492 * if there isn't a handler anymore
493 */
494 if (!have_handler(self, CharacterData))
495 return;
496 }
497 if (len > self->buffer_size) {
498 call_character_handler(self, data, len);
499 self->buffer_used = 0;
500 }
501 else {
502 memcpy(self->buffer + self->buffer_used,
503 data, len * sizeof(XML_Char));
504 self->buffer_used += len;
505 }
506 }
507}
508
Fred Drake85d835f2001-02-08 15:39:08 +0000509static void
510my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000511 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000512{
513 xmlparseobject *self = (xmlparseobject *)userData;
514
Fred Drake71b63ff2002-06-28 22:29:01 +0000515 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000516 PyObject *container, *rv, *args;
517 int i, max;
518
Fred Drake2a3d7db2002-06-28 22:56:48 +0000519 if (flush_character_buffer(self) < 0)
520 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000521 /* Set max to the number of slots filled in atts[]; max/2 is
522 * the number of attributes we need to process.
523 */
524 if (self->specified_attributes) {
525 max = XML_GetSpecifiedAttributeCount(self->itself);
526 }
527 else {
528 max = 0;
529 while (atts[max] != NULL)
530 max += 2;
531 }
532 /* Build the container. */
533 if (self->ordered_attributes)
534 container = PyList_New(max);
535 else
536 container = PyDict_New();
537 if (container == NULL) {
538 flag_error(self);
539 return;
540 }
541 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000542 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000543 PyObject *v;
544 if (n == NULL) {
545 flag_error(self);
546 Py_DECREF(container);
547 return;
548 }
549 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
550 if (v == NULL) {
551 flag_error(self);
552 Py_DECREF(container);
553 Py_DECREF(n);
554 return;
555 }
556 if (self->ordered_attributes) {
557 PyList_SET_ITEM(container, i, n);
558 PyList_SET_ITEM(container, i+1, v);
559 }
560 else if (PyDict_SetItem(container, n, v)) {
561 flag_error(self);
562 Py_DECREF(n);
563 Py_DECREF(v);
564 return;
565 }
566 else {
567 Py_DECREF(n);
568 Py_DECREF(v);
569 }
570 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000571 args = Py_BuildValue("(NN)", string_intern(self, name), container);
Fred Drake85d835f2001-02-08 15:39:08 +0000572 if (args == NULL) {
573 Py_DECREF(container);
574 return;
575 }
576 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000577 self->in_callback = 1;
578 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000579 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000580 self->in_callback = 0;
581 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000582 if (rv == NULL) {
583 flag_error(self);
584 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000585 }
Fred Drake85d835f2001-02-08 15:39:08 +0000586 Py_DECREF(rv);
587 }
588}
589
590#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
591 RETURN, GETUSERDATA) \
592static RC \
593my_##NAME##Handler PARAMS {\
594 xmlparseobject *self = GETUSERDATA ; \
595 PyObject *args = NULL; \
596 PyObject *rv = NULL; \
597 INIT \
598\
Fred Drake71b63ff2002-06-28 22:29:01 +0000599 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000600 if (flush_character_buffer(self) < 0) \
601 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000602 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000603 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000604 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000605 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000606 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000607 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000608 Py_DECREF(args); \
609 if (rv == NULL) { \
610 flag_error(self); \
611 return RETURN; \
612 } \
613 CONVERSION \
614 Py_DECREF(rv); \
615 } \
616 return RETURN; \
617}
618
Fred Drake6f987622000-08-25 18:03:30 +0000619#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
620 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
621 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000622
Fred Drake6f987622000-08-25 18:03:30 +0000623#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
624 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
625 rc = PyInt_AsLong(rv);, rc, \
626 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000627
Fred Drake71b63ff2002-06-28 22:29:01 +0000628VOID_HANDLER(EndElement,
629 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000630 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000631
Fred Drake6f987622000-08-25 18:03:30 +0000632VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000633 (void *userData,
634 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000635 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000636 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000637
Fred Drake6f987622000-08-25 18:03:30 +0000638VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000639 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000640 const XML_Char *entityName,
641 const XML_Char *base,
642 const XML_Char *systemId,
643 const XML_Char *publicId,
644 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000645 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000646 string_intern(self, entityName), string_intern(self, base),
647 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000648 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000649
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000650#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000651VOID_HANDLER(EntityDecl,
652 (void *userData,
653 const XML_Char *entityName,
654 int is_parameter_entity,
655 const XML_Char *value,
656 int value_length,
657 const XML_Char *base,
658 const XML_Char *systemId,
659 const XML_Char *publicId,
660 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000661 ("NiNNNNN",
662 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000663 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000664 string_intern(self, base), string_intern(self, systemId),
665 string_intern(self, publicId),
666 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000667#else
668VOID_HANDLER(EntityDecl,
669 (void *userData,
670 const XML_Char *entityName,
671 int is_parameter_entity,
672 const XML_Char *value,
673 int value_length,
674 const XML_Char *base,
675 const XML_Char *systemId,
676 const XML_Char *publicId,
677 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000678 ("NiNNNNN",
679 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000680 (self->returns_unicode
681 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000682 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000683 string_intern(self, base), string_intern(self, systemId),
684 string_intern(self, publicId),
685 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000686#endif
687
688VOID_HANDLER(XmlDecl,
689 (void *userData,
690 const XML_Char *version,
691 const XML_Char *encoding,
692 int standalone),
693 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000694 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000695 standalone))
696
697static PyObject *
698conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000699 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000700{
701 PyObject *result = NULL;
702 PyObject *children = PyTuple_New(model->numchildren);
703 int i;
704
705 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000706 assert(model->numchildren < INT_MAX);
707 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000708 PyObject *child = conv_content_model(&model->children[i],
709 conv_string);
710 if (child == NULL) {
711 Py_XDECREF(children);
712 return NULL;
713 }
714 PyTuple_SET_ITEM(children, i, child);
715 }
716 result = Py_BuildValue("(iiO&N)",
717 model->type, model->quant,
718 conv_string,model->name, children);
719 }
720 return result;
721}
722
Fred Drake06dd8cf2003-02-02 03:54:17 +0000723static void
724my_ElementDeclHandler(void *userData,
725 const XML_Char *name,
726 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000727{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000728 xmlparseobject *self = (xmlparseobject *)userData;
729 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000730
Fred Drake06dd8cf2003-02-02 03:54:17 +0000731 if (have_handler(self, ElementDecl)) {
732 PyObject *rv = NULL;
733 PyObject *modelobj, *nameobj;
734
735 if (flush_character_buffer(self) < 0)
736 goto finally;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000737#ifdef Py_USING_UNICODE
Fred Drake06dd8cf2003-02-02 03:54:17 +0000738 modelobj = conv_content_model(model,
739 (self->returns_unicode
740 ? conv_string_to_unicode
741 : conv_string_to_utf8));
Fred Drake85d835f2001-02-08 15:39:08 +0000742#else
Fred Drake06dd8cf2003-02-02 03:54:17 +0000743 modelobj = conv_content_model(model, conv_string_to_utf8);
Fred Drake85d835f2001-02-08 15:39:08 +0000744#endif
Fred Drake06dd8cf2003-02-02 03:54:17 +0000745 if (modelobj == NULL) {
746 flag_error(self);
747 goto finally;
748 }
749 nameobj = string_intern(self, name);
750 if (nameobj == NULL) {
751 Py_DECREF(modelobj);
752 flag_error(self);
753 goto finally;
754 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000755 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000756 if (args == NULL) {
757 Py_DECREF(modelobj);
758 flag_error(self);
759 goto finally;
760 }
761 self->in_callback = 1;
762 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000763 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000764 self->in_callback = 0;
765 if (rv == NULL) {
766 flag_error(self);
767 goto finally;
768 }
769 Py_DECREF(rv);
770 }
771 finally:
772 Py_XDECREF(args);
773 XML_FreeContentModel(self->itself, model);
774 return;
775}
Fred Drake85d835f2001-02-08 15:39:08 +0000776
777VOID_HANDLER(AttlistDecl,
778 (void *userData,
779 const XML_Char *elname,
780 const XML_Char *attname,
781 const XML_Char *att_type,
782 const XML_Char *dflt,
783 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000784 ("(NNO&O&i)",
785 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000786 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
787 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000788
Martin v. Löwisc847f402003-01-21 11:09:21 +0000789#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000790VOID_HANDLER(SkippedEntity,
791 (void *userData,
792 const XML_Char *entityName,
793 int is_parameter_entity),
794 ("Ni",
795 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000796#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000797
Fred Drake71b63ff2002-06-28 22:29:01 +0000798VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000799 (void *userData,
800 const XML_Char *notationName,
801 const XML_Char *base,
802 const XML_Char *systemId,
803 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000804 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000805 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000806 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000807
Fred Drake6f987622000-08-25 18:03:30 +0000808VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000809 (void *userData,
810 const XML_Char *prefix,
811 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000812 ("(NN)",
813 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000814
Fred Drake6f987622000-08-25 18:03:30 +0000815VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000816 (void *userData,
817 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000818 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000819
Fred Drake6f987622000-08-25 18:03:30 +0000820VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000821 (void *userData, const XML_Char *data),
822 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823
Fred Drake6f987622000-08-25 18:03:30 +0000824VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000826 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000827
Fred Drake6f987622000-08-25 18:03:30 +0000828VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000830 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000831
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000832#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000833VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000834 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000835 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836
Fred Drake6f987622000-08-25 18:03:30 +0000837VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000838 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000839 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000840#else
Fred Drake6f987622000-08-25 18:03:30 +0000841VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000842 (void *userData, const XML_Char *s, int len),
843 ("(N)", (self->returns_unicode
844 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000845 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000846
Fred Drake6f987622000-08-25 18:03:30 +0000847VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000848 (void *userData, const XML_Char *s, int len),
849 ("(N)", (self->returns_unicode
850 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000851 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000852#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000853
Fred Drake71b63ff2002-06-28 22:29:01 +0000854INT_HANDLER(NotStandalone,
855 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000856 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000857
Fred Drake6f987622000-08-25 18:03:30 +0000858RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000859 (XML_Parser parser,
860 const XML_Char *context,
861 const XML_Char *base,
862 const XML_Char *systemId,
863 const XML_Char *publicId),
864 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000865 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000866 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000867 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000868 rc = PyInt_AsLong(rv);, rc,
869 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000870
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000871/* XXX UnknownEncodingHandler */
872
Fred Drake85d835f2001-02-08 15:39:08 +0000873VOID_HANDLER(StartDoctypeDecl,
874 (void *userData, const XML_Char *doctypeName,
875 const XML_Char *sysid, const XML_Char *pubid,
876 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000877 ("(NNNi)", string_intern(self, doctypeName),
878 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000879 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000880
881VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000882
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000883/* ---------------------------------------------------------------- */
884
Fred Drake71b63ff2002-06-28 22:29:01 +0000885static PyObject *
886get_parse_result(xmlparseobject *self, int rv)
887{
888 if (PyErr_Occurred()) {
889 return NULL;
890 }
891 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000892 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000893 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000894 if (flush_character_buffer(self) < 0) {
895 return NULL;
896 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000897 return PyInt_FromLong(rv);
898}
899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000901"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903
904static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000905xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000906{
Fred Drake0582df92000-07-12 04:49:00 +0000907 char *s;
908 int slen;
909 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000910
Fred Drake0582df92000-07-12 04:49:00 +0000911 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
912 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000913
914 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915}
916
Fred Drakeca1f4262000-09-21 20:10:23 +0000917/* File reading copied from cPickle */
918
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000919#define BUF_SIZE 2048
920
Fred Drake0582df92000-07-12 04:49:00 +0000921static int
922readinst(char *buf, int buf_size, PyObject *meth)
923{
924 PyObject *arg = NULL;
925 PyObject *bytes = NULL;
926 PyObject *str = NULL;
927 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000928
Fred Drake676940b2000-09-22 15:21:31 +0000929 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000930 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000931
Fred Drake7b6caff2003-07-21 17:05:56 +0000932 if ((arg = PyTuple_New(1)) == NULL) {
933 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000934 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000935 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000936
Tim Peters954eef72000-09-22 06:01:11 +0000937 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000938
Guido van Rossum84b2bed2002-08-16 17:01:09 +0000939 if ((str = PyObject_Call(meth, arg, NULL)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000940 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000941
Fred Drake0582df92000-07-12 04:49:00 +0000942 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000943 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000944 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000945 "read() did not return a string object (type=%.400s)",
946 str->ob_type->tp_name);
947 goto finally;
948 }
949 len = PyString_GET_SIZE(str);
950 if (len > buf_size) {
951 PyErr_Format(PyExc_ValueError,
952 "read() returned too much data: "
953 "%i bytes requested, %i returned",
954 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000955 goto finally;
956 }
957 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000958finally:
Fred Drake0582df92000-07-12 04:49:00 +0000959 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000960 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000961 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000962}
963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000964PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000965"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000966Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000967
968static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000969xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000970{
Fred Drake0582df92000-07-12 04:49:00 +0000971 int rv = 1;
972 PyObject *f;
973 FILE *fp;
974 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000975
Fred Drake0582df92000-07-12 04:49:00 +0000976 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
977 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000978
Fred Drake0582df92000-07-12 04:49:00 +0000979 if (PyFile_Check(f)) {
980 fp = PyFile_AsFile(f);
981 }
982 else{
983 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000984 readmethod = PyObject_GetAttrString(f, "read");
985 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000986 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000987 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000988 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +0000989 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000990 }
991 }
992 for (;;) {
993 int bytes_read;
994 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000995 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000996 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000997 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000998 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000999
Fred Drake0582df92000-07-12 04:49:00 +00001000 if (fp) {
1001 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
1002 if (bytes_read < 0) {
1003 PyErr_SetFromErrno(PyExc_IOError);
1004 return NULL;
1005 }
1006 }
1007 else {
1008 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +00001009 if (bytes_read < 0) {
1010 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001011 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001012 }
Fred Drake0582df92000-07-12 04:49:00 +00001013 }
1014 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +00001015 if (PyErr_Occurred()) {
1016 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001017 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001018 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001019
Fred Drake0582df92000-07-12 04:49:00 +00001020 if (!rv || bytes_read == 0)
1021 break;
1022 }
Fred Drake7b6caff2003-07-21 17:05:56 +00001023 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +00001024 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001025}
1026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001027PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001028"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001029Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001030
1031static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001032xmlparse_SetBase(xmlparseobject *self, PyObject *args)
1033{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001034 char *base;
1035
Fred Drake0582df92000-07-12 04:49:00 +00001036 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001037 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001038 if (!XML_SetBase(self->itself, base)) {
1039 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001040 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001041 Py_INCREF(Py_None);
1042 return Py_None;
1043}
1044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001046"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001047Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001048
1049static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001050xmlparse_GetBase(xmlparseobject *self, PyObject *args)
1051{
1052 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001053 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001054
Fred Drake0582df92000-07-12 04:49:00 +00001055 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001056}
1057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +00001059"GetInputContext() -> string\n\
1060Return the untranslated text of the input that caused the current event.\n\
1061If 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 +00001062for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +00001063
1064static PyObject *
1065xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
1066{
1067 PyObject *result = NULL;
1068
1069 if (PyArg_ParseTuple(args, ":GetInputContext")) {
1070 if (self->in_callback) {
1071 int offset, size;
1072 const char *buffer
1073 = XML_GetInputContext(self->itself, &offset, &size);
1074
1075 if (buffer != NULL)
1076 result = PyString_FromStringAndSize(buffer + offset, size);
1077 else {
1078 result = Py_None;
1079 Py_INCREF(result);
1080 }
1081 }
1082 else {
1083 result = Py_None;
1084 Py_INCREF(result);
1085 }
1086 }
1087 return result;
1088}
Fred Drakebd6101c2001-02-14 18:29:45 +00001089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001090PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001091"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001092Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001093information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001094
1095static PyObject *
1096xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1097{
1098 char *context;
1099 char *encoding = NULL;
1100 xmlparseobject *new_parser;
1101 int i;
1102
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001103 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001104 &context, &encoding)) {
1105 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001106 }
1107
Martin v. Löwis894258c2001-09-23 10:20:10 +00001108#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001109 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001110 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001111#else
1112 /* Python versions 2.2 and later */
1113 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1114#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001115
1116 if (new_parser == NULL)
1117 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001118 new_parser->buffer_size = self->buffer_size;
1119 new_parser->buffer_used = 0;
1120 if (self->buffer != NULL) {
1121 new_parser->buffer = malloc(new_parser->buffer_size);
1122 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001123#ifndef Py_TPFLAGS_HAVE_GC
1124 /* Code for versions 2.0 and 2.1 */
1125 PyObject_Del(new_parser);
1126#else
1127 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001128 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001129#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001130 return PyErr_NoMemory();
1131 }
1132 }
1133 else
1134 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001135 new_parser->returns_unicode = self->returns_unicode;
1136 new_parser->ordered_attributes = self->ordered_attributes;
1137 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001138 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001139 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001140 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001141 encoding);
1142 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001143 new_parser->intern = self->intern;
1144 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001145#ifdef Py_TPFLAGS_HAVE_GC
1146 PyObject_GC_Track(new_parser);
1147#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001148 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001149#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001150
1151 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001152 Py_DECREF(new_parser);
1153 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001154 }
1155
1156 XML_SetUserData(new_parser->itself, (void *)new_parser);
1157
1158 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001159 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001160 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001161
Fred Drake2a3d7db2002-06-28 22:56:48 +00001162 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001163 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001164 Py_DECREF(new_parser);
1165 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001166 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001167 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001168
1169 /* then copy handlers from self */
1170 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001171 PyObject *handler = self->handlers[i];
1172 if (handler != NULL) {
1173 Py_INCREF(handler);
1174 new_parser->handlers[i] = handler;
1175 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001176 handler_info[i].handler);
1177 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001178 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001179 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001180}
1181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183"SetParamEntityParsing(flag) -> success\n\
1184Controls parsing of parameter entities (including the external DTD\n\
1185subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1186XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1187XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001189
1190static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001191xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001192{
Fred Drake85d835f2001-02-08 15:39:08 +00001193 int flag;
1194 if (!PyArg_ParseTuple(args, "i", &flag))
1195 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001196 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001197 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001198}
1199
Martin v. Löwisc847f402003-01-21 11:09:21 +00001200
1201#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001202PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1203"UseForeignDTD([flag])\n\
1204Allows the application to provide an artificial external subset if one is\n\
1205not specified as part of the document instance. This readily allows the\n\
1206use of a 'default' document type controlled by the application, while still\n\
1207getting the advantage of providing document type information to the parser.\n\
1208'flag' defaults to True if not provided.");
1209
1210static PyObject *
1211xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1212{
1213 PyObject *flagobj = NULL;
1214 XML_Bool flag = XML_TRUE;
1215 enum XML_Error rc;
1216 if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj))
1217 return NULL;
1218 if (flagobj != NULL)
1219 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1220 rc = XML_UseForeignDTD(self->itself, flag);
1221 if (rc != XML_ERROR_NONE) {
1222 return set_error(self, rc);
1223 }
1224 Py_INCREF(Py_None);
1225 return Py_None;
1226}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001227#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001228
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001229static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001230 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001231 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001232 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001233 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001234 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001235 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001236 {"GetBase", (PyCFunction)xmlparse_GetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001237 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001238 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001239 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001240 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1241 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001242 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1243 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001244#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001245 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1246 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001247#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001248 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001249};
1250
1251/* ---------- */
1252
1253
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001254#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001255
Fred Drake71b63ff2002-06-28 22:29:01 +00001256/* pyexpat international encoding support.
1257 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001258*/
1259
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001260static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001261PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001262
Fred Drake71b63ff2002-06-28 22:29:01 +00001263static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001264init_template_buffer(void)
1265{
1266 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001267 for (i = 0; i < 256; i++) {
1268 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001269 }
Fred Drakebb66a202001-03-01 20:48:17 +00001270 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001271}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001272
Fred Drake71b63ff2002-06-28 22:29:01 +00001273static int
1274PyUnknownEncodingHandler(void *encodingHandlerData,
1275 const XML_Char *name,
1276 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001277{
Fred Drakebb66a202001-03-01 20:48:17 +00001278 PyUnicodeObject *_u_string = NULL;
1279 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001280 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001281
Fred Drakebb66a202001-03-01 20:48:17 +00001282 /* Yes, supports only 8bit encodings */
1283 _u_string = (PyUnicodeObject *)
1284 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001285
Fred Drakebb66a202001-03-01 20:48:17 +00001286 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001287 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001288
Fred Drakebb66a202001-03-01 20:48:17 +00001289 for (i = 0; i < 256; i++) {
1290 /* Stupid to access directly, but fast */
1291 Py_UNICODE c = _u_string->str[i];
1292 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001293 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001294 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001295 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001296 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001297 info->data = NULL;
1298 info->convert = NULL;
1299 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001300 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001301 Py_DECREF(_u_string);
1302 return result;
1303}
1304
1305#endif
1306
1307static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001308newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001309{
1310 int i;
1311 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001312
Martin v. Löwis894258c2001-09-23 10:20:10 +00001313#ifdef Py_TPFLAGS_HAVE_GC
1314 /* Code for versions 2.2 and later */
1315 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1316#else
Fred Drake0582df92000-07-12 04:49:00 +00001317 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001318#endif
Fred Drake0582df92000-07-12 04:49:00 +00001319 if (self == NULL)
1320 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001321
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001322#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001323 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001324#else
1325 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001326#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001327
Fred Drake2a3d7db2002-06-28 22:56:48 +00001328 self->buffer = NULL;
1329 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1330 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001331 self->ordered_attributes = 0;
1332 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001333 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001334 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001335 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001336 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001337 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1338 }
Fred Drake85d835f2001-02-08 15:39:08 +00001339 else {
Fred Drake0582df92000-07-12 04:49:00 +00001340 self->itself = XML_ParserCreate(encoding);
1341 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001342 self->intern = intern;
1343 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001344#ifdef Py_TPFLAGS_HAVE_GC
1345 PyObject_GC_Track(self);
1346#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001347 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001348#endif
Fred Drake0582df92000-07-12 04:49:00 +00001349 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001350 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001351 "XML_ParserCreate failed");
1352 Py_DECREF(self);
1353 return NULL;
1354 }
1355 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001356#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001357 XML_SetUnknownEncodingHandler(self->itself,
1358 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001359#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001360
Fred Drake2a3d7db2002-06-28 22:56:48 +00001361 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001362 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001363
Fred Drake7c75bf22002-07-01 14:02:31 +00001364 self->handlers = malloc(sizeof(PyObject *) * i);
1365 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001366 Py_DECREF(self);
1367 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001368 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001369 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001370
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001371 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001372}
1373
1374
1375static void
Fred Drake0582df92000-07-12 04:49:00 +00001376xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377{
Fred Drake0582df92000-07-12 04:49:00 +00001378 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001379#ifdef Py_TPFLAGS_HAVE_GC
1380 PyObject_GC_UnTrack(self);
1381#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001382 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001383#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001384 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001385 XML_ParserFree(self->itself);
1386 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001387
Fred Drake85d835f2001-02-08 15:39:08 +00001388 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001389 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001390 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001391 temp = self->handlers[i];
1392 self->handlers[i] = NULL;
1393 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001394 }
1395 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001396 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001397 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001398 if (self->buffer != NULL) {
1399 free(self->buffer);
1400 self->buffer = NULL;
1401 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001402 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001403#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001404 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001405 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001406#else
1407 /* Code for versions 2.2 and later. */
1408 PyObject_GC_Del(self);
1409#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001410}
1411
Fred Drake0582df92000-07-12 04:49:00 +00001412static int
1413handlername2int(const char *name)
1414{
1415 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001416 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001417 if (strcmp(name, handler_info[i].name) == 0) {
1418 return i;
1419 }
1420 }
1421 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001422}
1423
1424static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001425get_pybool(int istrue)
1426{
1427 PyObject *result = istrue ? Py_True : Py_False;
1428 Py_INCREF(result);
1429 return result;
1430}
1431
1432static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001433xmlparse_getattr(xmlparseobject *self, char *name)
1434{
Fred Drake71b63ff2002-06-28 22:29:01 +00001435 int handlernum = handlername2int(name);
1436
1437 if (handlernum != -1) {
1438 PyObject *result = self->handlers[handlernum];
1439 if (result == NULL)
1440 result = Py_None;
1441 Py_INCREF(result);
1442 return result;
1443 }
1444 if (name[0] == 'E') {
1445 if (strcmp(name, "ErrorCode") == 0)
1446 return PyInt_FromLong((long)
1447 XML_GetErrorCode(self->itself));
1448 if (strcmp(name, "ErrorLineNumber") == 0)
1449 return PyInt_FromLong((long)
1450 XML_GetErrorLineNumber(self->itself));
1451 if (strcmp(name, "ErrorColumnNumber") == 0)
1452 return PyInt_FromLong((long)
1453 XML_GetErrorColumnNumber(self->itself));
1454 if (strcmp(name, "ErrorByteIndex") == 0)
1455 return PyInt_FromLong((long)
1456 XML_GetErrorByteIndex(self->itself));
1457 }
Dave Cole3203efb2004-08-26 00:37:31 +00001458 if (name[0] == 'C') {
1459 if (strcmp(name, "CurrentLineNumber") == 0)
1460 return PyInt_FromLong((long)
1461 XML_GetCurrentLineNumber(self->itself));
1462 if (strcmp(name, "CurrentColumnNumber") == 0)
1463 return PyInt_FromLong((long)
1464 XML_GetCurrentColumnNumber(self->itself));
1465 if (strcmp(name, "CurrentByteIndex") == 0)
1466 return PyInt_FromLong((long)
1467 XML_GetCurrentByteIndex(self->itself));
1468 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001469 if (name[0] == 'b') {
1470 if (strcmp(name, "buffer_size") == 0)
1471 return PyInt_FromLong((long) self->buffer_size);
1472 if (strcmp(name, "buffer_text") == 0)
1473 return get_pybool(self->buffer != NULL);
1474 if (strcmp(name, "buffer_used") == 0)
1475 return PyInt_FromLong((long) self->buffer_used);
1476 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001477 if (strcmp(name, "namespace_prefixes") == 0)
1478 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001479 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001480 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001481 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001482 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001483 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001484 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001485 if (strcmp(name, "intern") == 0) {
1486 if (self->intern == NULL) {
1487 Py_INCREF(Py_None);
1488 return Py_None;
1489 }
1490 else {
1491 Py_INCREF(self->intern);
1492 return self->intern;
1493 }
1494 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001495
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001496#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001497 do { \
1498 PyObject *o = PyString_FromString(str); \
1499 if (o != NULL) \
1500 PyList_Append(list, o); \
1501 Py_XDECREF(o); \
1502 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001503
Fred Drake0582df92000-07-12 04:49:00 +00001504 if (strcmp(name, "__members__") == 0) {
1505 int i;
1506 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001507 for (i = 0; handler_info[i].name != NULL; i++) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001508 PyObject *o = get_handler_name(&handler_info[i]);
1509 if (o != NULL)
1510 PyList_Append(rc, o);
1511 Py_XDECREF(o);
Fred Drake0582df92000-07-12 04:49:00 +00001512 }
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001513 APPEND(rc, "ErrorCode");
1514 APPEND(rc, "ErrorLineNumber");
1515 APPEND(rc, "ErrorColumnNumber");
1516 APPEND(rc, "ErrorByteIndex");
Dave Cole3203efb2004-08-26 00:37:31 +00001517 APPEND(rc, "CurrentLineNumber");
1518 APPEND(rc, "CurrentColumnNumber");
1519 APPEND(rc, "CurrentByteIndex");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001520 APPEND(rc, "buffer_size");
1521 APPEND(rc, "buffer_text");
1522 APPEND(rc, "buffer_used");
Martin v. Löwis069dde22003-01-21 10:58:18 +00001523 APPEND(rc, "namespace_prefixes");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001524 APPEND(rc, "ordered_attributes");
1525 APPEND(rc, "returns_unicode");
1526 APPEND(rc, "specified_attributes");
1527 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001528
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001529#undef APPEND
Fred Drake0582df92000-07-12 04:49:00 +00001530 return rc;
1531 }
1532 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001533}
1534
Fred Drake6f987622000-08-25 18:03:30 +00001535static int
1536sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001537{
1538 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001539 if (handlernum >= 0) {
1540 xmlhandler c_handler = NULL;
1541 PyObject *temp = self->handlers[handlernum];
1542
1543 if (v == Py_None)
1544 v = NULL;
1545 else if (v != NULL) {
1546 Py_INCREF(v);
1547 c_handler = handler_info[handlernum].handler;
1548 }
Fred Drake0582df92000-07-12 04:49:00 +00001549 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001550 Py_XDECREF(temp);
1551 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001552 return 1;
1553 }
1554 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001555}
1556
1557static int
Fred Drake6f987622000-08-25 18:03:30 +00001558xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001559{
Fred Drake6f987622000-08-25 18:03:30 +00001560 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001561 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001562 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1563 return -1;
1564 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001565 if (strcmp(name, "buffer_text") == 0) {
1566 if (PyObject_IsTrue(v)) {
1567 if (self->buffer == NULL) {
1568 self->buffer = malloc(self->buffer_size);
1569 if (self->buffer == NULL) {
1570 PyErr_NoMemory();
1571 return -1;
1572 }
1573 self->buffer_used = 0;
1574 }
1575 }
1576 else if (self->buffer != NULL) {
1577 if (flush_character_buffer(self) < 0)
1578 return -1;
1579 free(self->buffer);
1580 self->buffer = NULL;
1581 }
1582 return 0;
1583 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001584 if (strcmp(name, "namespace_prefixes") == 0) {
1585 if (PyObject_IsTrue(v))
1586 self->ns_prefixes = 1;
1587 else
1588 self->ns_prefixes = 0;
1589 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1590 return 0;
1591 }
Fred Drake85d835f2001-02-08 15:39:08 +00001592 if (strcmp(name, "ordered_attributes") == 0) {
1593 if (PyObject_IsTrue(v))
1594 self->ordered_attributes = 1;
1595 else
1596 self->ordered_attributes = 0;
1597 return 0;
1598 }
Fred Drake6f987622000-08-25 18:03:30 +00001599 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001600 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001601#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001602 PyErr_SetString(PyExc_ValueError,
1603 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001604 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001605#else
Fred Drake6f987622000-08-25 18:03:30 +00001606 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001607#endif
Fred Drake6f987622000-08-25 18:03:30 +00001608 }
1609 else
1610 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001611 return 0;
1612 }
1613 if (strcmp(name, "specified_attributes") == 0) {
1614 if (PyObject_IsTrue(v))
1615 self->specified_attributes = 1;
1616 else
1617 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001618 return 0;
1619 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001620 if (strcmp(name, "CharacterDataHandler") == 0) {
1621 /* If we're changing the character data handler, flush all
1622 * cached data with the old handler. Not sure there's a
1623 * "right" thing to do, though, but this probably won't
1624 * happen.
1625 */
1626 if (flush_character_buffer(self) < 0)
1627 return -1;
1628 }
Fred Drake6f987622000-08-25 18:03:30 +00001629 if (sethandler(self, name, v)) {
1630 return 0;
1631 }
1632 PyErr_SetString(PyExc_AttributeError, name);
1633 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001634}
1635
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001636#ifdef WITH_CYCLE_GC
1637static int
1638xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1639{
Fred Drakecde79132001-04-25 16:01:30 +00001640 int i, err;
1641 for (i = 0; handler_info[i].name != NULL; i++) {
1642 if (!op->handlers[i])
1643 continue;
1644 err = visit(op->handlers[i], arg);
1645 if (err)
1646 return err;
1647 }
1648 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001649}
1650
1651static int
1652xmlparse_clear(xmlparseobject *op)
1653{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001654 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001655 Py_XDECREF(op->intern);
1656 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001657 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001658}
1659#endif
1660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001661PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001662
1663static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001664 PyObject_HEAD_INIT(NULL)
1665 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001666 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001667 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001668 0, /*tp_itemsize*/
1669 /* methods */
1670 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1671 (printfunc)0, /*tp_print*/
1672 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1673 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1674 (cmpfunc)0, /*tp_compare*/
1675 (reprfunc)0, /*tp_repr*/
1676 0, /*tp_as_number*/
1677 0, /*tp_as_sequence*/
1678 0, /*tp_as_mapping*/
1679 (hashfunc)0, /*tp_hash*/
1680 (ternaryfunc)0, /*tp_call*/
1681 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001682 0, /* tp_getattro */
1683 0, /* tp_setattro */
1684 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001685#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001686 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001687#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001688 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001689#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001690 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001691#ifdef WITH_CYCLE_GC
1692 (traverseproc)xmlparse_traverse, /* tp_traverse */
1693 (inquiry)xmlparse_clear /* tp_clear */
1694#else
1695 0, 0
1696#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001697};
1698
1699/* End of code for xmlparser objects */
1700/* -------------------------------------------------------- */
1701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001702PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001703"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001704Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001705
1706static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001707pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1708{
Fred Drakecde79132001-04-25 16:01:30 +00001709 char *encoding = NULL;
1710 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001711 PyObject *intern = NULL;
1712 PyObject *result;
1713 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001714 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001715 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001716
Fred Drakeb91a36b2002-06-27 19:40:48 +00001717 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1718 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001719 return NULL;
1720 if (namespace_separator != NULL
1721 && strlen(namespace_separator) > 1) {
1722 PyErr_SetString(PyExc_ValueError,
1723 "namespace_separator must be at most one"
1724 " character, omitted, or None");
1725 return NULL;
1726 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001727 /* Explicitly passing None means no interning is desired.
1728 Not passing anything means that a new dictionary is used. */
1729 if (intern == Py_None)
1730 intern = NULL;
1731 else if (intern == NULL) {
1732 intern = PyDict_New();
1733 if (!intern)
1734 return NULL;
1735 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001736 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001737 else if (!PyDict_Check(intern)) {
1738 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1739 return NULL;
1740 }
1741
1742 result = newxmlparseobject(encoding, namespace_separator, intern);
1743 if (intern_decref) {
1744 Py_DECREF(intern);
1745 }
1746 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001747}
1748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001750"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001752
1753static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001754pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001755{
Fred Drake0582df92000-07-12 04:49:00 +00001756 long code = 0;
1757
1758 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1759 return NULL;
1760 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001761}
1762
1763/* List of methods defined in the module */
1764
1765static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001766 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1767 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1768 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1769 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001770
Fred Drake0582df92000-07-12 04:49:00 +00001771 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001772};
1773
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001774/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776PyDoc_STRVAR(pyexpat_module_documentation,
1777"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001778
Fred Drake4113b132001-03-24 19:58:26 +00001779/* Return a Python string that represents the version number without the
1780 * extra cruft added by revision control, even if the right options were
1781 * given to the "cvs export" command to make it not include the extra
1782 * cruft.
1783 */
1784static PyObject *
1785get_version_string(void)
1786{
1787 static char *rcsid = "$Revision$";
1788 char *rev = rcsid;
1789 int i = 0;
1790
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001791 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001792 ++rev;
1793 while (rev[i] != ' ' && rev[i] != '\0')
1794 ++i;
1795
1796 return PyString_FromStringAndSize(rev, i);
1797}
1798
Fred Drakecde79132001-04-25 16:01:30 +00001799/* Initialization function for the module */
1800
1801#ifndef MODULE_NAME
1802#define MODULE_NAME "pyexpat"
1803#endif
1804
1805#ifndef MODULE_INITFUNC
1806#define MODULE_INITFUNC initpyexpat
1807#endif
1808
Martin v. Löwis069dde22003-01-21 10:58:18 +00001809#ifndef PyMODINIT_FUNC
1810# ifdef MS_WINDOWS
1811# define PyMODINIT_FUNC __declspec(dllexport) void
1812# else
1813# define PyMODINIT_FUNC void
1814# endif
1815#endif
1816
Mark Hammond8235ea12002-07-19 06:55:41 +00001817PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001818
Martin v. Löwis069dde22003-01-21 10:58:18 +00001819PyMODINIT_FUNC
1820MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001821{
1822 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001823 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001824 PyObject *errors_module;
1825 PyObject *modelmod_name;
1826 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001827 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001828
Fred Drake6f987622000-08-25 18:03:30 +00001829 if (errmod_name == NULL)
1830 return;
Fred Drakecde79132001-04-25 16:01:30 +00001831 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001832 if (modelmod_name == NULL)
1833 return;
Fred Drake6f987622000-08-25 18:03:30 +00001834
Fred Drake0582df92000-07-12 04:49:00 +00001835 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001836
Fred Drake0582df92000-07-12 04:49:00 +00001837 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001838 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001839 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001840
Fred Drake0582df92000-07-12 04:49:00 +00001841 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001842 if (ErrorObject == NULL) {
1843 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001844 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001845 if (ErrorObject == NULL)
1846 return;
1847 }
1848 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001849 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001850 Py_INCREF(ErrorObject);
1851 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001852 Py_INCREF(&Xmlparsetype);
1853 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001854
Fred Drake4113b132001-03-24 19:58:26 +00001855 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001856 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1857 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001858 {
1859 XML_Expat_Version info = XML_ExpatVersionInfo();
1860 PyModule_AddObject(m, "version_info",
1861 Py_BuildValue("(iii)", info.major,
1862 info.minor, info.micro));
1863 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001864#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001865 init_template_buffer();
1866#endif
Fred Drake0582df92000-07-12 04:49:00 +00001867 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001868 compiled, this should check and set native_encoding
1869 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001870 */
Fred Drake93adb692000-09-23 04:55:48 +00001871 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001872
Fred Drake85d835f2001-02-08 15:39:08 +00001873 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001874 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001875 errors_module = PyDict_GetItem(d, errmod_name);
1876 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001877 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001878 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001879 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001880 /* gives away the reference to errors_module */
1881 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001882 }
1883 }
Fred Drake6f987622000-08-25 18:03:30 +00001884 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001885 model_module = PyDict_GetItem(d, modelmod_name);
1886 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001887 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001888 if (model_module != NULL) {
1889 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1890 /* gives away the reference to model_module */
1891 PyModule_AddObject(m, "model", model_module);
1892 }
1893 }
1894 Py_DECREF(modelmod_name);
1895 if (errors_module == NULL || model_module == NULL)
1896 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001897 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001898
Martin v. Löwisc847f402003-01-21 11:09:21 +00001899#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001900 {
1901 const XML_Feature *features = XML_GetFeatureList();
1902 PyObject *list = PyList_New(0);
1903 if (list == NULL)
1904 /* just ignore it */
1905 PyErr_Clear();
1906 else {
1907 int i = 0;
1908 for (; features[i].feature != XML_FEATURE_END; ++i) {
1909 int ok;
1910 PyObject *item = Py_BuildValue("si", features[i].name,
1911 features[i].value);
1912 if (item == NULL) {
1913 Py_DECREF(list);
1914 list = NULL;
1915 break;
1916 }
1917 ok = PyList_Append(list, item);
1918 Py_DECREF(item);
1919 if (ok < 0) {
1920 PyErr_Clear();
1921 break;
1922 }
1923 }
1924 if (list != NULL)
1925 PyModule_AddObject(m, "features", list);
1926 }
1927 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001928#endif
Fred Drake6f987622000-08-25 18:03:30 +00001929
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001930#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001931 PyModule_AddStringConstant(errors_module, #name, \
1932 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001933
Fred Drake0582df92000-07-12 04:49:00 +00001934 MYCONST(XML_ERROR_NO_MEMORY);
1935 MYCONST(XML_ERROR_SYNTAX);
1936 MYCONST(XML_ERROR_NO_ELEMENTS);
1937 MYCONST(XML_ERROR_INVALID_TOKEN);
1938 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1939 MYCONST(XML_ERROR_PARTIAL_CHAR);
1940 MYCONST(XML_ERROR_TAG_MISMATCH);
1941 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1942 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1943 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1944 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1945 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1946 MYCONST(XML_ERROR_ASYNC_ENTITY);
1947 MYCONST(XML_ERROR_BAD_CHAR_REF);
1948 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1949 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1950 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1951 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1952 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001953 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1954 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1955 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001956 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1957 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1958 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1959 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1960 /* Added in Expat 1.95.7. */
1961 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1962 /* Added in Expat 1.95.8. */
1963 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1964 MYCONST(XML_ERROR_INCOMPLETE_PE);
1965 MYCONST(XML_ERROR_XML_DECL);
1966 MYCONST(XML_ERROR_TEXT_DECL);
1967 MYCONST(XML_ERROR_PUBLICID);
1968 MYCONST(XML_ERROR_SUSPENDED);
1969 MYCONST(XML_ERROR_NOT_SUSPENDED);
1970 MYCONST(XML_ERROR_ABORTED);
1971 MYCONST(XML_ERROR_FINISHED);
1972 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001973
Fred Drake85d835f2001-02-08 15:39:08 +00001974 PyModule_AddStringConstant(errors_module, "__doc__",
1975 "Constants used to describe error conditions.");
1976
Fred Drake93adb692000-09-23 04:55:48 +00001977#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001978
Fred Drake85d835f2001-02-08 15:39:08 +00001979#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001980 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1981 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1982 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001983#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001984
Fred Drake85d835f2001-02-08 15:39:08 +00001985#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1986 PyModule_AddStringConstant(model_module, "__doc__",
1987 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001988
Fred Drake85d835f2001-02-08 15:39:08 +00001989 MYCONST(XML_CTYPE_EMPTY);
1990 MYCONST(XML_CTYPE_ANY);
1991 MYCONST(XML_CTYPE_MIXED);
1992 MYCONST(XML_CTYPE_NAME);
1993 MYCONST(XML_CTYPE_CHOICE);
1994 MYCONST(XML_CTYPE_SEQ);
1995
1996 MYCONST(XML_CQUANT_NONE);
1997 MYCONST(XML_CQUANT_OPT);
1998 MYCONST(XML_CQUANT_REP);
1999 MYCONST(XML_CQUANT_PLUS);
2000#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002001}
2002
Fred Drake6f987622000-08-25 18:03:30 +00002003static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002004clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00002005{
Fred Drakecde79132001-04-25 16:01:30 +00002006 int i = 0;
2007 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002008
Fred Drake71b63ff2002-06-28 22:29:01 +00002009 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002010 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00002011 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002012 else {
Fred Drakecde79132001-04-25 16:01:30 +00002013 temp = self->handlers[i];
2014 self->handlers[i] = NULL;
2015 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002016 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002017 }
Fred Drakecde79132001-04-25 16:01:30 +00002018 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002019}
2020
Tim Peters0c322792002-07-17 16:49:03 +00002021static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002022 {"StartElementHandler",
2023 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002024 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002025 {"EndElementHandler",
2026 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002027 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002028 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002029 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2030 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002031 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002032 (xmlhandlersetter)XML_SetCharacterDataHandler,
2033 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002034 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002035 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002036 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002037 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002038 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002039 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002040 {"StartNamespaceDeclHandler",
2041 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002042 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002043 {"EndNamespaceDeclHandler",
2044 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002045 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002046 {"CommentHandler",
2047 (xmlhandlersetter)XML_SetCommentHandler,
2048 (xmlhandler)my_CommentHandler},
2049 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002050 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002051 (xmlhandler)my_StartCdataSectionHandler},
2052 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002053 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002054 (xmlhandler)my_EndCdataSectionHandler},
2055 {"DefaultHandler",
2056 (xmlhandlersetter)XML_SetDefaultHandler,
2057 (xmlhandler)my_DefaultHandler},
2058 {"DefaultHandlerExpand",
2059 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2060 (xmlhandler)my_DefaultHandlerExpandHandler},
2061 {"NotStandaloneHandler",
2062 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2063 (xmlhandler)my_NotStandaloneHandler},
2064 {"ExternalEntityRefHandler",
2065 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002066 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002067 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002068 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002069 (xmlhandler)my_StartDoctypeDeclHandler},
2070 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002071 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002072 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002073 {"EntityDeclHandler",
2074 (xmlhandlersetter)XML_SetEntityDeclHandler,
2075 (xmlhandler)my_EntityDeclHandler},
2076 {"XmlDeclHandler",
2077 (xmlhandlersetter)XML_SetXmlDeclHandler,
2078 (xmlhandler)my_XmlDeclHandler},
2079 {"ElementDeclHandler",
2080 (xmlhandlersetter)XML_SetElementDeclHandler,
2081 (xmlhandler)my_ElementDeclHandler},
2082 {"AttlistDeclHandler",
2083 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2084 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002085#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002086 {"SkippedEntityHandler",
2087 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2088 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002089#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002090
Fred Drake0582df92000-07-12 04:49:00 +00002091 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002092};