blob: 438f7609cb2249fcc62b72bcd75abfe1b18090b3 [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 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000341#if PY_VERSION_HEX < 0x02040000
342 arg = Py_BuildValue("(OOO)", type, value, traceback);
343#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000344 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000345#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000346 if (arg == NULL) {
347 PyErr_Restore(type, value, traceback);
348 return 0;
349 }
350 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
351 Py_DECREF(arg);
352 if (err == 0)
353 PyErr_Restore(type, value, traceback);
354 else {
355 Py_XDECREF(type);
356 Py_XDECREF(value);
357 Py_XDECREF(traceback);
358 }
359 return err;
360}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000361#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000362
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000363static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000364call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
365 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000366{
Fred Drakebd6101c2001-02-14 18:29:45 +0000367 PyThreadState *tstate = PyThreadState_GET();
368 PyFrameObject *f;
369 PyObject *res;
370
371 if (c == NULL)
372 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000373
Jeremy Hylton9263f572003-06-27 16:13:17 +0000374 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000375 if (f == NULL)
376 return NULL;
377 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000378#ifdef FIX_TRACE
379 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000380 return NULL;
381 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000382#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000383 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000384 if (res == NULL) {
385 if (tstate->curexc_traceback == NULL)
386 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000387 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000388#ifdef FIX_TRACE
389 if (trace_frame_exc(tstate, f) < 0) {
390 return NULL;
391 }
392 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000393 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000394 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000395 Py_XDECREF(res);
396 res = NULL;
397 }
398 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000399#else
400 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000401#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000402 tstate->frame = f->f_back;
403 Py_DECREF(f);
404 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000405}
406
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000407#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000408#define STRING_CONV_FUNC conv_string_to_utf8
409#else
Martin v. Löwis069dde22003-01-21 10:58:18 +0000410/* Python 2.0 and later versions, when built with Unicode support */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000411#define STRING_CONV_FUNC (self->returns_unicode \
412 ? conv_string_to_unicode : conv_string_to_utf8)
413#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000414
Fred Drakeb91a36b2002-06-27 19:40:48 +0000415static PyObject*
416string_intern(xmlparseobject *self, const char* str)
417{
418 PyObject *result = STRING_CONV_FUNC(str);
419 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000420 /* result can be NULL if the unicode conversion failed. */
421 if (!result)
422 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000423 if (!self->intern)
424 return result;
425 value = PyDict_GetItem(self->intern, result);
426 if (!value) {
427 if (PyDict_SetItem(self->intern, result, result) == 0)
428 return result;
429 else
430 return NULL;
431 }
432 Py_INCREF(value);
433 Py_DECREF(result);
434 return value;
435}
436
Fred Drake2a3d7db2002-06-28 22:56:48 +0000437/* Return 0 on success, -1 on exception.
438 * flag_error() will be called before return if needed.
439 */
440static int
441call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
442{
443 PyObject *args;
444 PyObject *temp;
445
446 args = PyTuple_New(1);
447 if (args == NULL)
448 return -1;
449#ifdef Py_USING_UNICODE
450 temp = (self->returns_unicode
451 ? conv_string_len_to_unicode(buffer, len)
452 : conv_string_len_to_utf8(buffer, len));
453#else
454 temp = conv_string_len_to_utf8(buffer, len);
455#endif
456 if (temp == NULL) {
457 Py_DECREF(args);
458 flag_error(self);
459 return -1;
460 }
461 PyTuple_SET_ITEM(args, 0, temp);
462 /* temp is now a borrowed reference; consider it unused. */
463 self->in_callback = 1;
464 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000465 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000466 /* temp is an owned reference again, or NULL */
467 self->in_callback = 0;
468 Py_DECREF(args);
469 if (temp == NULL) {
470 flag_error(self);
471 return -1;
472 }
473 Py_DECREF(temp);
474 return 0;
475}
476
477static int
478flush_character_buffer(xmlparseobject *self)
479{
480 int rc;
481 if (self->buffer == NULL || self->buffer_used == 0)
482 return 0;
483 rc = call_character_handler(self, self->buffer, self->buffer_used);
484 self->buffer_used = 0;
485 return rc;
486}
487
488static void
489my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
490{
491 xmlparseobject *self = (xmlparseobject *) userData;
492 if (self->buffer == NULL)
493 call_character_handler(self, data, len);
494 else {
495 if ((self->buffer_used + len) > self->buffer_size) {
496 if (flush_character_buffer(self) < 0)
497 return;
498 /* handler might have changed; drop the rest on the floor
499 * if there isn't a handler anymore
500 */
501 if (!have_handler(self, CharacterData))
502 return;
503 }
504 if (len > self->buffer_size) {
505 call_character_handler(self, data, len);
506 self->buffer_used = 0;
507 }
508 else {
509 memcpy(self->buffer + self->buffer_used,
510 data, len * sizeof(XML_Char));
511 self->buffer_used += len;
512 }
513 }
514}
515
Fred Drake85d835f2001-02-08 15:39:08 +0000516static void
517my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000518 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000519{
520 xmlparseobject *self = (xmlparseobject *)userData;
521
Fred Drake71b63ff2002-06-28 22:29:01 +0000522 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000523 PyObject *container, *rv, *args;
524 int i, max;
525
Fred Drake2a3d7db2002-06-28 22:56:48 +0000526 if (flush_character_buffer(self) < 0)
527 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000528 /* Set max to the number of slots filled in atts[]; max/2 is
529 * the number of attributes we need to process.
530 */
531 if (self->specified_attributes) {
532 max = XML_GetSpecifiedAttributeCount(self->itself);
533 }
534 else {
535 max = 0;
536 while (atts[max] != NULL)
537 max += 2;
538 }
539 /* Build the container. */
540 if (self->ordered_attributes)
541 container = PyList_New(max);
542 else
543 container = PyDict_New();
544 if (container == NULL) {
545 flag_error(self);
546 return;
547 }
548 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000549 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000550 PyObject *v;
551 if (n == NULL) {
552 flag_error(self);
553 Py_DECREF(container);
554 return;
555 }
556 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
557 if (v == NULL) {
558 flag_error(self);
559 Py_DECREF(container);
560 Py_DECREF(n);
561 return;
562 }
563 if (self->ordered_attributes) {
564 PyList_SET_ITEM(container, i, n);
565 PyList_SET_ITEM(container, i+1, v);
566 }
567 else if (PyDict_SetItem(container, n, v)) {
568 flag_error(self);
569 Py_DECREF(n);
570 Py_DECREF(v);
571 return;
572 }
573 else {
574 Py_DECREF(n);
575 Py_DECREF(v);
576 }
577 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000578 args = string_intern(self, name);
579 if (args != NULL)
580 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000581 if (args == NULL) {
582 Py_DECREF(container);
583 return;
584 }
585 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000586 self->in_callback = 1;
587 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000588 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000589 self->in_callback = 0;
590 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000591 if (rv == NULL) {
592 flag_error(self);
593 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000594 }
Fred Drake85d835f2001-02-08 15:39:08 +0000595 Py_DECREF(rv);
596 }
597}
598
599#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
600 RETURN, GETUSERDATA) \
601static RC \
602my_##NAME##Handler PARAMS {\
603 xmlparseobject *self = GETUSERDATA ; \
604 PyObject *args = NULL; \
605 PyObject *rv = NULL; \
606 INIT \
607\
Fred Drake71b63ff2002-06-28 22:29:01 +0000608 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000609 if (flush_character_buffer(self) < 0) \
610 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000611 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000612 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000613 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000614 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000615 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000616 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000617 Py_DECREF(args); \
618 if (rv == NULL) { \
619 flag_error(self); \
620 return RETURN; \
621 } \
622 CONVERSION \
623 Py_DECREF(rv); \
624 } \
625 return RETURN; \
626}
627
Fred Drake6f987622000-08-25 18:03:30 +0000628#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
629 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
630 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000631
Fred Drake6f987622000-08-25 18:03:30 +0000632#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
633 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
634 rc = PyInt_AsLong(rv);, rc, \
635 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000636
Fred Drake71b63ff2002-06-28 22:29:01 +0000637VOID_HANDLER(EndElement,
638 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000639 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000640
Fred Drake6f987622000-08-25 18:03:30 +0000641VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000642 (void *userData,
643 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000644 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000645 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000646
Fred Drake6f987622000-08-25 18:03:30 +0000647VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000648 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000649 const XML_Char *entityName,
650 const XML_Char *base,
651 const XML_Char *systemId,
652 const XML_Char *publicId,
653 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000654 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000655 string_intern(self, entityName), string_intern(self, base),
656 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000657 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000658
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000659#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000660VOID_HANDLER(EntityDecl,
661 (void *userData,
662 const XML_Char *entityName,
663 int is_parameter_entity,
664 const XML_Char *value,
665 int value_length,
666 const XML_Char *base,
667 const XML_Char *systemId,
668 const XML_Char *publicId,
669 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000670 ("NiNNNNN",
671 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000672 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000673 string_intern(self, base), string_intern(self, systemId),
674 string_intern(self, publicId),
675 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000676#else
677VOID_HANDLER(EntityDecl,
678 (void *userData,
679 const XML_Char *entityName,
680 int is_parameter_entity,
681 const XML_Char *value,
682 int value_length,
683 const XML_Char *base,
684 const XML_Char *systemId,
685 const XML_Char *publicId,
686 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000687 ("NiNNNNN",
688 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000689 (self->returns_unicode
690 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000691 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000692 string_intern(self, base), string_intern(self, systemId),
693 string_intern(self, publicId),
694 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000695#endif
696
697VOID_HANDLER(XmlDecl,
698 (void *userData,
699 const XML_Char *version,
700 const XML_Char *encoding,
701 int standalone),
702 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000703 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000704 standalone))
705
706static PyObject *
707conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000708 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000709{
710 PyObject *result = NULL;
711 PyObject *children = PyTuple_New(model->numchildren);
712 int i;
713
714 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000715 assert(model->numchildren < INT_MAX);
716 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000717 PyObject *child = conv_content_model(&model->children[i],
718 conv_string);
719 if (child == NULL) {
720 Py_XDECREF(children);
721 return NULL;
722 }
723 PyTuple_SET_ITEM(children, i, child);
724 }
725 result = Py_BuildValue("(iiO&N)",
726 model->type, model->quant,
727 conv_string,model->name, children);
728 }
729 return result;
730}
731
Fred Drake06dd8cf2003-02-02 03:54:17 +0000732static void
733my_ElementDeclHandler(void *userData,
734 const XML_Char *name,
735 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000736{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000737 xmlparseobject *self = (xmlparseobject *)userData;
738 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000739
Fred Drake06dd8cf2003-02-02 03:54:17 +0000740 if (have_handler(self, ElementDecl)) {
741 PyObject *rv = NULL;
742 PyObject *modelobj, *nameobj;
743
744 if (flush_character_buffer(self) < 0)
745 goto finally;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000746#ifdef Py_USING_UNICODE
Fred Drake06dd8cf2003-02-02 03:54:17 +0000747 modelobj = conv_content_model(model,
748 (self->returns_unicode
749 ? conv_string_to_unicode
750 : conv_string_to_utf8));
Fred Drake85d835f2001-02-08 15:39:08 +0000751#else
Fred Drake06dd8cf2003-02-02 03:54:17 +0000752 modelobj = conv_content_model(model, conv_string_to_utf8);
Fred Drake85d835f2001-02-08 15:39:08 +0000753#endif
Fred Drake06dd8cf2003-02-02 03:54:17 +0000754 if (modelobj == NULL) {
755 flag_error(self);
756 goto finally;
757 }
758 nameobj = string_intern(self, name);
759 if (nameobj == NULL) {
760 Py_DECREF(modelobj);
761 flag_error(self);
762 goto finally;
763 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000764 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000765 if (args == NULL) {
766 Py_DECREF(modelobj);
767 flag_error(self);
768 goto finally;
769 }
770 self->in_callback = 1;
771 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000772 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000773 self->in_callback = 0;
774 if (rv == NULL) {
775 flag_error(self);
776 goto finally;
777 }
778 Py_DECREF(rv);
779 }
780 finally:
781 Py_XDECREF(args);
782 XML_FreeContentModel(self->itself, model);
783 return;
784}
Fred Drake85d835f2001-02-08 15:39:08 +0000785
786VOID_HANDLER(AttlistDecl,
787 (void *userData,
788 const XML_Char *elname,
789 const XML_Char *attname,
790 const XML_Char *att_type,
791 const XML_Char *dflt,
792 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000793 ("(NNO&O&i)",
794 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000795 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
796 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000797
Martin v. Löwisc847f402003-01-21 11:09:21 +0000798#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000799VOID_HANDLER(SkippedEntity,
800 (void *userData,
801 const XML_Char *entityName,
802 int is_parameter_entity),
803 ("Ni",
804 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000805#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000806
Fred Drake71b63ff2002-06-28 22:29:01 +0000807VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000808 (void *userData,
809 const XML_Char *notationName,
810 const XML_Char *base,
811 const XML_Char *systemId,
812 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000813 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000814 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000815 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000816
Fred Drake6f987622000-08-25 18:03:30 +0000817VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000818 (void *userData,
819 const XML_Char *prefix,
820 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000821 ("(NN)",
822 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000823
Fred Drake6f987622000-08-25 18:03:30 +0000824VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000825 (void *userData,
826 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000827 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828
Fred Drake6f987622000-08-25 18:03:30 +0000829VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000830 (void *userData, const XML_Char *data),
831 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000832
Fred Drake6f987622000-08-25 18:03:30 +0000833VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000835 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000836
Fred Drake6f987622000-08-25 18:03:30 +0000837VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000839 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000841#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000842VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000843 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000844 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000845
Fred Drake6f987622000-08-25 18:03:30 +0000846VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000847 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000848 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000849#else
Fred Drake6f987622000-08-25 18:03:30 +0000850VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000851 (void *userData, const XML_Char *s, int len),
852 ("(N)", (self->returns_unicode
853 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000854 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000855
Fred Drake6f987622000-08-25 18:03:30 +0000856VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000857 (void *userData, const XML_Char *s, int len),
858 ("(N)", (self->returns_unicode
859 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000860 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000861#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000862
Fred Drake71b63ff2002-06-28 22:29:01 +0000863INT_HANDLER(NotStandalone,
864 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000865 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000866
Fred Drake6f987622000-08-25 18:03:30 +0000867RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000868 (XML_Parser parser,
869 const XML_Char *context,
870 const XML_Char *base,
871 const XML_Char *systemId,
872 const XML_Char *publicId),
873 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000874 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000875 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000876 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000877 rc = PyInt_AsLong(rv);, rc,
878 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000879
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000880/* XXX UnknownEncodingHandler */
881
Fred Drake85d835f2001-02-08 15:39:08 +0000882VOID_HANDLER(StartDoctypeDecl,
883 (void *userData, const XML_Char *doctypeName,
884 const XML_Char *sysid, const XML_Char *pubid,
885 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000886 ("(NNNi)", string_intern(self, doctypeName),
887 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000888 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000889
890VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892/* ---------------------------------------------------------------- */
893
Fred Drake71b63ff2002-06-28 22:29:01 +0000894static PyObject *
895get_parse_result(xmlparseobject *self, int rv)
896{
897 if (PyErr_Occurred()) {
898 return NULL;
899 }
900 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000901 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000902 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000903 if (flush_character_buffer(self) < 0) {
904 return NULL;
905 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000906 return PyInt_FromLong(rv);
907}
908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000910"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000911Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000912
913static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000914xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000915{
Fred Drake0582df92000-07-12 04:49:00 +0000916 char *s;
917 int slen;
918 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000919
Fred Drake0582df92000-07-12 04:49:00 +0000920 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
921 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000922
923 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000924}
925
Fred Drakeca1f4262000-09-21 20:10:23 +0000926/* File reading copied from cPickle */
927
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000928#define BUF_SIZE 2048
929
Fred Drake0582df92000-07-12 04:49:00 +0000930static int
931readinst(char *buf, int buf_size, PyObject *meth)
932{
933 PyObject *arg = NULL;
934 PyObject *bytes = NULL;
935 PyObject *str = NULL;
936 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000937
Fred Drake676940b2000-09-22 15:21:31 +0000938 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000939 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000940
Fred Drake7b6caff2003-07-21 17:05:56 +0000941 if ((arg = PyTuple_New(1)) == NULL) {
942 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000943 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000944 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000945
Tim Peters954eef72000-09-22 06:01:11 +0000946 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000947
Martin v. Löwis9171f022004-10-13 19:50:11 +0000948#if PY_VERSION_HEX < 0x02020000
949 str = PyObject_CallObject(meth, arg);
950#else
951 str = PyObject_Call(meth, arg, NULL);
952#endif
953 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000954 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000955
Fred Drake0582df92000-07-12 04:49:00 +0000956 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000957 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000958 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000959 "read() did not return a string object (type=%.400s)",
960 str->ob_type->tp_name);
961 goto finally;
962 }
963 len = PyString_GET_SIZE(str);
964 if (len > buf_size) {
965 PyErr_Format(PyExc_ValueError,
966 "read() returned too much data: "
967 "%i bytes requested, %i returned",
968 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000969 goto finally;
970 }
971 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000972finally:
Fred Drake0582df92000-07-12 04:49:00 +0000973 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000974 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000975 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000976}
977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000978PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000979"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000981
982static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000983xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000984{
Fred Drake0582df92000-07-12 04:49:00 +0000985 int rv = 1;
986 PyObject *f;
987 FILE *fp;
988 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000989
Fred Drake0582df92000-07-12 04:49:00 +0000990 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
991 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000992
Fred Drake0582df92000-07-12 04:49:00 +0000993 if (PyFile_Check(f)) {
994 fp = PyFile_AsFile(f);
995 }
996 else{
997 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000998 readmethod = PyObject_GetAttrString(f, "read");
999 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001000 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +00001001 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +00001002 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +00001003 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001004 }
1005 }
1006 for (;;) {
1007 int bytes_read;
1008 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +00001009 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +00001010 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001011 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +00001012 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001013
Fred Drake0582df92000-07-12 04:49:00 +00001014 if (fp) {
1015 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
1016 if (bytes_read < 0) {
1017 PyErr_SetFromErrno(PyExc_IOError);
1018 return NULL;
1019 }
1020 }
1021 else {
1022 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +00001023 if (bytes_read < 0) {
1024 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001025 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001026 }
Fred Drake0582df92000-07-12 04:49:00 +00001027 }
1028 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +00001029 if (PyErr_Occurred()) {
1030 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001031 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001032 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001033
Fred Drake0582df92000-07-12 04:49:00 +00001034 if (!rv || bytes_read == 0)
1035 break;
1036 }
Fred Drake7b6caff2003-07-21 17:05:56 +00001037 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +00001038 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001039}
1040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001041PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001042"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001044
1045static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001046xmlparse_SetBase(xmlparseobject *self, PyObject *args)
1047{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001048 char *base;
1049
Fred Drake0582df92000-07-12 04:49:00 +00001050 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001051 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001052 if (!XML_SetBase(self->itself, base)) {
1053 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001054 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001055 Py_INCREF(Py_None);
1056 return Py_None;
1057}
1058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001059PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001060"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001061Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001062
1063static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001064xmlparse_GetBase(xmlparseobject *self, PyObject *args)
1065{
1066 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001067 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001068
Fred Drake0582df92000-07-12 04:49:00 +00001069 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +00001073"GetInputContext() -> string\n\
1074Return the untranslated text of the input that caused the current event.\n\
1075If 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 +00001076for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +00001077
1078static PyObject *
1079xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
1080{
1081 PyObject *result = NULL;
1082
1083 if (PyArg_ParseTuple(args, ":GetInputContext")) {
1084 if (self->in_callback) {
1085 int offset, size;
1086 const char *buffer
1087 = XML_GetInputContext(self->itself, &offset, &size);
1088
1089 if (buffer != NULL)
Martin v. Löwisfd78a6f2005-03-04 14:37:01 +00001090 result = PyString_FromStringAndSize(buffer + offset, size - offset);
Fred Drakebd6101c2001-02-14 18:29:45 +00001091 else {
1092 result = Py_None;
1093 Py_INCREF(result);
1094 }
1095 }
1096 else {
1097 result = Py_None;
1098 Py_INCREF(result);
1099 }
1100 }
1101 return result;
1102}
Fred Drakebd6101c2001-02-14 18:29:45 +00001103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001104PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001105"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001106Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001108
1109static PyObject *
1110xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1111{
1112 char *context;
1113 char *encoding = NULL;
1114 xmlparseobject *new_parser;
1115 int i;
1116
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001117 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001118 &context, &encoding)) {
1119 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001120 }
1121
Martin v. Löwis894258c2001-09-23 10:20:10 +00001122#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001123 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001124 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001125#else
1126 /* Python versions 2.2 and later */
1127 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1128#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001129
1130 if (new_parser == NULL)
1131 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001132 new_parser->buffer_size = self->buffer_size;
1133 new_parser->buffer_used = 0;
1134 if (self->buffer != NULL) {
1135 new_parser->buffer = malloc(new_parser->buffer_size);
1136 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001137#ifndef Py_TPFLAGS_HAVE_GC
1138 /* Code for versions 2.0 and 2.1 */
1139 PyObject_Del(new_parser);
1140#else
1141 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001142 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001143#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001144 return PyErr_NoMemory();
1145 }
1146 }
1147 else
1148 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001149 new_parser->returns_unicode = self->returns_unicode;
1150 new_parser->ordered_attributes = self->ordered_attributes;
1151 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001152 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001153 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001154 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001155 encoding);
1156 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001157 new_parser->intern = self->intern;
1158 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001159#ifdef Py_TPFLAGS_HAVE_GC
1160 PyObject_GC_Track(new_parser);
1161#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001162 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001163#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001164
1165 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001166 Py_DECREF(new_parser);
1167 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001168 }
1169
1170 XML_SetUserData(new_parser->itself, (void *)new_parser);
1171
1172 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001173 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001174 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001175
Fred Drake2a3d7db2002-06-28 22:56:48 +00001176 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001177 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001178 Py_DECREF(new_parser);
1179 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001181 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001182
1183 /* then copy handlers from self */
1184 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001185 PyObject *handler = self->handlers[i];
1186 if (handler != NULL) {
1187 Py_INCREF(handler);
1188 new_parser->handlers[i] = handler;
1189 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001190 handler_info[i].handler);
1191 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001192 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001193 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001194}
1195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001197"SetParamEntityParsing(flag) -> success\n\
1198Controls parsing of parameter entities (including the external DTD\n\
1199subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1200XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1201XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001202was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001203
1204static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001205xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001206{
Fred Drake85d835f2001-02-08 15:39:08 +00001207 int flag;
1208 if (!PyArg_ParseTuple(args, "i", &flag))
1209 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001210 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001211 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001212}
1213
Martin v. Löwisc847f402003-01-21 11:09:21 +00001214
1215#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001216PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1217"UseForeignDTD([flag])\n\
1218Allows the application to provide an artificial external subset if one is\n\
1219not specified as part of the document instance. This readily allows the\n\
1220use of a 'default' document type controlled by the application, while still\n\
1221getting the advantage of providing document type information to the parser.\n\
1222'flag' defaults to True if not provided.");
1223
1224static PyObject *
1225xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1226{
1227 PyObject *flagobj = NULL;
1228 XML_Bool flag = XML_TRUE;
1229 enum XML_Error rc;
1230 if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj))
1231 return NULL;
1232 if (flagobj != NULL)
1233 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1234 rc = XML_UseForeignDTD(self->itself, flag);
1235 if (rc != XML_ERROR_NONE) {
1236 return set_error(self, rc);
1237 }
1238 Py_INCREF(Py_None);
1239 return Py_None;
1240}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001241#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001242
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001243static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001244 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001245 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001246 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001247 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001248 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001249 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001250 {"GetBase", (PyCFunction)xmlparse_GetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001251 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001252 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001253 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001254 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1255 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001256 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1257 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001258#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001259 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1260 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001261#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001262 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001263};
1264
1265/* ---------- */
1266
1267
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001268#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001269
Fred Drake71b63ff2002-06-28 22:29:01 +00001270/* pyexpat international encoding support.
1271 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001272*/
1273
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001274static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001275PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001276
Fred Drake71b63ff2002-06-28 22:29:01 +00001277static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001278init_template_buffer(void)
1279{
1280 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001281 for (i = 0; i < 256; i++) {
1282 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001283 }
Fred Drakebb66a202001-03-01 20:48:17 +00001284 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001285}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001286
Fred Drake71b63ff2002-06-28 22:29:01 +00001287static int
1288PyUnknownEncodingHandler(void *encodingHandlerData,
1289 const XML_Char *name,
1290 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001291{
Fred Drakebb66a202001-03-01 20:48:17 +00001292 PyUnicodeObject *_u_string = NULL;
1293 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001294 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001295
Fred Drakebb66a202001-03-01 20:48:17 +00001296 /* Yes, supports only 8bit encodings */
1297 _u_string = (PyUnicodeObject *)
1298 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001299
Fred Drakebb66a202001-03-01 20:48:17 +00001300 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001301 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001302
Fred Drakebb66a202001-03-01 20:48:17 +00001303 for (i = 0; i < 256; i++) {
1304 /* Stupid to access directly, but fast */
1305 Py_UNICODE c = _u_string->str[i];
1306 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001307 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001308 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001309 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001310 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001311 info->data = NULL;
1312 info->convert = NULL;
1313 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001314 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001315 Py_DECREF(_u_string);
1316 return result;
1317}
1318
1319#endif
1320
1321static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001322newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001323{
1324 int i;
1325 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001326
Martin v. Löwis894258c2001-09-23 10:20:10 +00001327#ifdef Py_TPFLAGS_HAVE_GC
1328 /* Code for versions 2.2 and later */
1329 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1330#else
Fred Drake0582df92000-07-12 04:49:00 +00001331 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001332#endif
Fred Drake0582df92000-07-12 04:49:00 +00001333 if (self == NULL)
1334 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001335
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001336#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001337 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001338#else
1339 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001340#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001341
Fred Drake2a3d7db2002-06-28 22:56:48 +00001342 self->buffer = NULL;
1343 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1344 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001345 self->ordered_attributes = 0;
1346 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001347 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001348 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001349 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001350 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001351 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1352 }
Fred Drake85d835f2001-02-08 15:39:08 +00001353 else {
Fred Drake0582df92000-07-12 04:49:00 +00001354 self->itself = XML_ParserCreate(encoding);
1355 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001356 self->intern = intern;
1357 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001358#ifdef Py_TPFLAGS_HAVE_GC
1359 PyObject_GC_Track(self);
1360#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001361 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001362#endif
Fred Drake0582df92000-07-12 04:49:00 +00001363 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001364 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001365 "XML_ParserCreate failed");
1366 Py_DECREF(self);
1367 return NULL;
1368 }
1369 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001370#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001371 XML_SetUnknownEncodingHandler(self->itself,
1372 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001373#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001374
Fred Drake2a3d7db2002-06-28 22:56:48 +00001375 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001376 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377
Fred Drake7c75bf22002-07-01 14:02:31 +00001378 self->handlers = malloc(sizeof(PyObject *) * i);
1379 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001380 Py_DECREF(self);
1381 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001382 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001383 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001384
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001385 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001386}
1387
1388
1389static void
Fred Drake0582df92000-07-12 04:49:00 +00001390xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001391{
Fred Drake0582df92000-07-12 04:49:00 +00001392 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001393#ifdef Py_TPFLAGS_HAVE_GC
1394 PyObject_GC_UnTrack(self);
1395#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001396 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001397#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001398 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001399 XML_ParserFree(self->itself);
1400 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001401
Fred Drake85d835f2001-02-08 15:39:08 +00001402 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001403 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001404 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001405 temp = self->handlers[i];
1406 self->handlers[i] = NULL;
1407 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001408 }
1409 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001410 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001411 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001412 if (self->buffer != NULL) {
1413 free(self->buffer);
1414 self->buffer = NULL;
1415 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001416 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001417#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001418 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001419 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001420#else
1421 /* Code for versions 2.2 and later. */
1422 PyObject_GC_Del(self);
1423#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001424}
1425
Fred Drake0582df92000-07-12 04:49:00 +00001426static int
1427handlername2int(const char *name)
1428{
1429 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001430 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001431 if (strcmp(name, handler_info[i].name) == 0) {
1432 return i;
1433 }
1434 }
1435 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001436}
1437
1438static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001439get_pybool(int istrue)
1440{
1441 PyObject *result = istrue ? Py_True : Py_False;
1442 Py_INCREF(result);
1443 return result;
1444}
1445
1446static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001447xmlparse_getattr(xmlparseobject *self, char *name)
1448{
Fred Drake71b63ff2002-06-28 22:29:01 +00001449 int handlernum = handlername2int(name);
1450
1451 if (handlernum != -1) {
1452 PyObject *result = self->handlers[handlernum];
1453 if (result == NULL)
1454 result = Py_None;
1455 Py_INCREF(result);
1456 return result;
1457 }
1458 if (name[0] == 'E') {
1459 if (strcmp(name, "ErrorCode") == 0)
1460 return PyInt_FromLong((long)
1461 XML_GetErrorCode(self->itself));
1462 if (strcmp(name, "ErrorLineNumber") == 0)
1463 return PyInt_FromLong((long)
1464 XML_GetErrorLineNumber(self->itself));
1465 if (strcmp(name, "ErrorColumnNumber") == 0)
1466 return PyInt_FromLong((long)
1467 XML_GetErrorColumnNumber(self->itself));
1468 if (strcmp(name, "ErrorByteIndex") == 0)
1469 return PyInt_FromLong((long)
1470 XML_GetErrorByteIndex(self->itself));
1471 }
Dave Cole3203efb2004-08-26 00:37:31 +00001472 if (name[0] == 'C') {
1473 if (strcmp(name, "CurrentLineNumber") == 0)
1474 return PyInt_FromLong((long)
1475 XML_GetCurrentLineNumber(self->itself));
1476 if (strcmp(name, "CurrentColumnNumber") == 0)
1477 return PyInt_FromLong((long)
1478 XML_GetCurrentColumnNumber(self->itself));
1479 if (strcmp(name, "CurrentByteIndex") == 0)
1480 return PyInt_FromLong((long)
1481 XML_GetCurrentByteIndex(self->itself));
1482 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001483 if (name[0] == 'b') {
1484 if (strcmp(name, "buffer_size") == 0)
1485 return PyInt_FromLong((long) self->buffer_size);
1486 if (strcmp(name, "buffer_text") == 0)
1487 return get_pybool(self->buffer != NULL);
1488 if (strcmp(name, "buffer_used") == 0)
1489 return PyInt_FromLong((long) self->buffer_used);
1490 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001491 if (strcmp(name, "namespace_prefixes") == 0)
1492 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001493 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001494 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001495 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001496 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001497 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001498 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001499 if (strcmp(name, "intern") == 0) {
1500 if (self->intern == NULL) {
1501 Py_INCREF(Py_None);
1502 return Py_None;
1503 }
1504 else {
1505 Py_INCREF(self->intern);
1506 return self->intern;
1507 }
1508 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001509
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001510#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001511 do { \
1512 PyObject *o = PyString_FromString(str); \
1513 if (o != NULL) \
1514 PyList_Append(list, o); \
1515 Py_XDECREF(o); \
1516 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001517
Fred Drake0582df92000-07-12 04:49:00 +00001518 if (strcmp(name, "__members__") == 0) {
1519 int i;
1520 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001521 for (i = 0; handler_info[i].name != NULL; i++) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001522 PyObject *o = get_handler_name(&handler_info[i]);
1523 if (o != NULL)
1524 PyList_Append(rc, o);
1525 Py_XDECREF(o);
Fred Drake0582df92000-07-12 04:49:00 +00001526 }
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001527 APPEND(rc, "ErrorCode");
1528 APPEND(rc, "ErrorLineNumber");
1529 APPEND(rc, "ErrorColumnNumber");
1530 APPEND(rc, "ErrorByteIndex");
Dave Cole3203efb2004-08-26 00:37:31 +00001531 APPEND(rc, "CurrentLineNumber");
1532 APPEND(rc, "CurrentColumnNumber");
1533 APPEND(rc, "CurrentByteIndex");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001534 APPEND(rc, "buffer_size");
1535 APPEND(rc, "buffer_text");
1536 APPEND(rc, "buffer_used");
Martin v. Löwis069dde22003-01-21 10:58:18 +00001537 APPEND(rc, "namespace_prefixes");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001538 APPEND(rc, "ordered_attributes");
1539 APPEND(rc, "returns_unicode");
1540 APPEND(rc, "specified_attributes");
1541 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001542
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001543#undef APPEND
Fred Drake0582df92000-07-12 04:49:00 +00001544 return rc;
1545 }
1546 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001547}
1548
Fred Drake6f987622000-08-25 18:03:30 +00001549static int
1550sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001551{
1552 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001553 if (handlernum >= 0) {
1554 xmlhandler c_handler = NULL;
1555 PyObject *temp = self->handlers[handlernum];
1556
1557 if (v == Py_None)
1558 v = NULL;
1559 else if (v != NULL) {
1560 Py_INCREF(v);
1561 c_handler = handler_info[handlernum].handler;
1562 }
Fred Drake0582df92000-07-12 04:49:00 +00001563 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001564 Py_XDECREF(temp);
1565 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001566 return 1;
1567 }
1568 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001569}
1570
1571static int
Fred Drake6f987622000-08-25 18:03:30 +00001572xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001573{
Fred Drake6f987622000-08-25 18:03:30 +00001574 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001575 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001576 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1577 return -1;
1578 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001579 if (strcmp(name, "buffer_text") == 0) {
1580 if (PyObject_IsTrue(v)) {
1581 if (self->buffer == NULL) {
1582 self->buffer = malloc(self->buffer_size);
1583 if (self->buffer == NULL) {
1584 PyErr_NoMemory();
1585 return -1;
1586 }
1587 self->buffer_used = 0;
1588 }
1589 }
1590 else if (self->buffer != NULL) {
1591 if (flush_character_buffer(self) < 0)
1592 return -1;
1593 free(self->buffer);
1594 self->buffer = NULL;
1595 }
1596 return 0;
1597 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001598 if (strcmp(name, "namespace_prefixes") == 0) {
1599 if (PyObject_IsTrue(v))
1600 self->ns_prefixes = 1;
1601 else
1602 self->ns_prefixes = 0;
1603 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1604 return 0;
1605 }
Fred Drake85d835f2001-02-08 15:39:08 +00001606 if (strcmp(name, "ordered_attributes") == 0) {
1607 if (PyObject_IsTrue(v))
1608 self->ordered_attributes = 1;
1609 else
1610 self->ordered_attributes = 0;
1611 return 0;
1612 }
Fred Drake6f987622000-08-25 18:03:30 +00001613 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001614 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001615#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001616 PyErr_SetString(PyExc_ValueError,
1617 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001618 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001619#else
Fred Drake6f987622000-08-25 18:03:30 +00001620 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001621#endif
Fred Drake6f987622000-08-25 18:03:30 +00001622 }
1623 else
1624 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001625 return 0;
1626 }
1627 if (strcmp(name, "specified_attributes") == 0) {
1628 if (PyObject_IsTrue(v))
1629 self->specified_attributes = 1;
1630 else
1631 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001632 return 0;
1633 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001634 if (strcmp(name, "CharacterDataHandler") == 0) {
1635 /* If we're changing the character data handler, flush all
1636 * cached data with the old handler. Not sure there's a
1637 * "right" thing to do, though, but this probably won't
1638 * happen.
1639 */
1640 if (flush_character_buffer(self) < 0)
1641 return -1;
1642 }
Fred Drake6f987622000-08-25 18:03:30 +00001643 if (sethandler(self, name, v)) {
1644 return 0;
1645 }
1646 PyErr_SetString(PyExc_AttributeError, name);
1647 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001648}
1649
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001650#ifdef WITH_CYCLE_GC
1651static int
1652xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1653{
Fred Drakecde79132001-04-25 16:01:30 +00001654 int i, err;
1655 for (i = 0; handler_info[i].name != NULL; i++) {
1656 if (!op->handlers[i])
1657 continue;
1658 err = visit(op->handlers[i], arg);
1659 if (err)
1660 return err;
1661 }
1662 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001663}
1664
1665static int
1666xmlparse_clear(xmlparseobject *op)
1667{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001668 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001669 Py_XDECREF(op->intern);
1670 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001671 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001672}
1673#endif
1674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001676
1677static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001678 PyObject_HEAD_INIT(NULL)
1679 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001680 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001681 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001682 0, /*tp_itemsize*/
1683 /* methods */
1684 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1685 (printfunc)0, /*tp_print*/
1686 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1687 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1688 (cmpfunc)0, /*tp_compare*/
1689 (reprfunc)0, /*tp_repr*/
1690 0, /*tp_as_number*/
1691 0, /*tp_as_sequence*/
1692 0, /*tp_as_mapping*/
1693 (hashfunc)0, /*tp_hash*/
1694 (ternaryfunc)0, /*tp_call*/
1695 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001696 0, /* tp_getattro */
1697 0, /* tp_setattro */
1698 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001699#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001700 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001701#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001702 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001703#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001704 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001705#ifdef WITH_CYCLE_GC
1706 (traverseproc)xmlparse_traverse, /* tp_traverse */
1707 (inquiry)xmlparse_clear /* tp_clear */
1708#else
1709 0, 0
1710#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001711};
1712
1713/* End of code for xmlparser objects */
1714/* -------------------------------------------------------- */
1715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001716PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001717"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001718Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001719
1720static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001721pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1722{
Fred Drakecde79132001-04-25 16:01:30 +00001723 char *encoding = NULL;
1724 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001725 PyObject *intern = NULL;
1726 PyObject *result;
1727 int intern_decref = 0;
Fred Drake71b63ff2002-06-28 22:29:01 +00001728 static char *kwlist[] = {"encoding", "namespace_separator",
Fred Drakeb91a36b2002-06-27 19:40:48 +00001729 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001730
Fred Drakeb91a36b2002-06-27 19:40:48 +00001731 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1732 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001733 return NULL;
1734 if (namespace_separator != NULL
1735 && strlen(namespace_separator) > 1) {
1736 PyErr_SetString(PyExc_ValueError,
1737 "namespace_separator must be at most one"
1738 " character, omitted, or None");
1739 return NULL;
1740 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001741 /* Explicitly passing None means no interning is desired.
1742 Not passing anything means that a new dictionary is used. */
1743 if (intern == Py_None)
1744 intern = NULL;
1745 else if (intern == NULL) {
1746 intern = PyDict_New();
1747 if (!intern)
1748 return NULL;
1749 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001750 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001751 else if (!PyDict_Check(intern)) {
1752 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1753 return NULL;
1754 }
1755
1756 result = newxmlparseobject(encoding, namespace_separator, intern);
1757 if (intern_decref) {
1758 Py_DECREF(intern);
1759 }
1760 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001761}
1762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001764"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001766
1767static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001768pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001769{
Fred Drake0582df92000-07-12 04:49:00 +00001770 long code = 0;
1771
1772 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1773 return NULL;
1774 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001775}
1776
1777/* List of methods defined in the module */
1778
1779static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001780 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1781 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1782 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1783 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001784
Fred Drake0582df92000-07-12 04:49:00 +00001785 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001786};
1787
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001788/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(pyexpat_module_documentation,
1791"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001792
Fred Drake4113b132001-03-24 19:58:26 +00001793/* Return a Python string that represents the version number without the
1794 * extra cruft added by revision control, even if the right options were
1795 * given to the "cvs export" command to make it not include the extra
1796 * cruft.
1797 */
1798static PyObject *
1799get_version_string(void)
1800{
1801 static char *rcsid = "$Revision$";
1802 char *rev = rcsid;
1803 int i = 0;
1804
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001805 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001806 ++rev;
1807 while (rev[i] != ' ' && rev[i] != '\0')
1808 ++i;
1809
1810 return PyString_FromStringAndSize(rev, i);
1811}
1812
Fred Drakecde79132001-04-25 16:01:30 +00001813/* Initialization function for the module */
1814
1815#ifndef MODULE_NAME
1816#define MODULE_NAME "pyexpat"
1817#endif
1818
1819#ifndef MODULE_INITFUNC
1820#define MODULE_INITFUNC initpyexpat
1821#endif
1822
Martin v. Löwis069dde22003-01-21 10:58:18 +00001823#ifndef PyMODINIT_FUNC
1824# ifdef MS_WINDOWS
1825# define PyMODINIT_FUNC __declspec(dllexport) void
1826# else
1827# define PyMODINIT_FUNC void
1828# endif
1829#endif
1830
Mark Hammond8235ea12002-07-19 06:55:41 +00001831PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001832
Martin v. Löwis069dde22003-01-21 10:58:18 +00001833PyMODINIT_FUNC
1834MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001835{
1836 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001837 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001838 PyObject *errors_module;
1839 PyObject *modelmod_name;
1840 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001841 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001842
Fred Drake6f987622000-08-25 18:03:30 +00001843 if (errmod_name == NULL)
1844 return;
Fred Drakecde79132001-04-25 16:01:30 +00001845 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001846 if (modelmod_name == NULL)
1847 return;
Fred Drake6f987622000-08-25 18:03:30 +00001848
Fred Drake0582df92000-07-12 04:49:00 +00001849 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001850
Fred Drake0582df92000-07-12 04:49:00 +00001851 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001852 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001853 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001854
Fred Drake0582df92000-07-12 04:49:00 +00001855 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001856 if (ErrorObject == NULL) {
1857 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001858 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001859 if (ErrorObject == NULL)
1860 return;
1861 }
1862 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001863 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001864 Py_INCREF(ErrorObject);
1865 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001866 Py_INCREF(&Xmlparsetype);
1867 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001868
Fred Drake4113b132001-03-24 19:58:26 +00001869 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001870 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1871 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001872 {
1873 XML_Expat_Version info = XML_ExpatVersionInfo();
1874 PyModule_AddObject(m, "version_info",
1875 Py_BuildValue("(iii)", info.major,
1876 info.minor, info.micro));
1877 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001878#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001879 init_template_buffer();
1880#endif
Fred Drake0582df92000-07-12 04:49:00 +00001881 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001882 compiled, this should check and set native_encoding
1883 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001884 */
Fred Drake93adb692000-09-23 04:55:48 +00001885 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001886
Fred Drake85d835f2001-02-08 15:39:08 +00001887 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001888 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001889 errors_module = PyDict_GetItem(d, errmod_name);
1890 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001891 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001892 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001893 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001894 /* gives away the reference to errors_module */
1895 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001896 }
1897 }
Fred Drake6f987622000-08-25 18:03:30 +00001898 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001899 model_module = PyDict_GetItem(d, modelmod_name);
1900 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001901 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001902 if (model_module != NULL) {
1903 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1904 /* gives away the reference to model_module */
1905 PyModule_AddObject(m, "model", model_module);
1906 }
1907 }
1908 Py_DECREF(modelmod_name);
1909 if (errors_module == NULL || model_module == NULL)
1910 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001911 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001912
Martin v. Löwisc847f402003-01-21 11:09:21 +00001913#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001914 {
1915 const XML_Feature *features = XML_GetFeatureList();
1916 PyObject *list = PyList_New(0);
1917 if (list == NULL)
1918 /* just ignore it */
1919 PyErr_Clear();
1920 else {
1921 int i = 0;
1922 for (; features[i].feature != XML_FEATURE_END; ++i) {
1923 int ok;
1924 PyObject *item = Py_BuildValue("si", features[i].name,
1925 features[i].value);
1926 if (item == NULL) {
1927 Py_DECREF(list);
1928 list = NULL;
1929 break;
1930 }
1931 ok = PyList_Append(list, item);
1932 Py_DECREF(item);
1933 if (ok < 0) {
1934 PyErr_Clear();
1935 break;
1936 }
1937 }
1938 if (list != NULL)
1939 PyModule_AddObject(m, "features", list);
1940 }
1941 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001942#endif
Fred Drake6f987622000-08-25 18:03:30 +00001943
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001944#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001945 PyModule_AddStringConstant(errors_module, #name, \
1946 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001947
Fred Drake0582df92000-07-12 04:49:00 +00001948 MYCONST(XML_ERROR_NO_MEMORY);
1949 MYCONST(XML_ERROR_SYNTAX);
1950 MYCONST(XML_ERROR_NO_ELEMENTS);
1951 MYCONST(XML_ERROR_INVALID_TOKEN);
1952 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1953 MYCONST(XML_ERROR_PARTIAL_CHAR);
1954 MYCONST(XML_ERROR_TAG_MISMATCH);
1955 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1956 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1957 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1958 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1959 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1960 MYCONST(XML_ERROR_ASYNC_ENTITY);
1961 MYCONST(XML_ERROR_BAD_CHAR_REF);
1962 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1963 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1964 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1965 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1966 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001967 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1968 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1969 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001970 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1971 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1972 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1973 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1974 /* Added in Expat 1.95.7. */
1975 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1976 /* Added in Expat 1.95.8. */
1977 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1978 MYCONST(XML_ERROR_INCOMPLETE_PE);
1979 MYCONST(XML_ERROR_XML_DECL);
1980 MYCONST(XML_ERROR_TEXT_DECL);
1981 MYCONST(XML_ERROR_PUBLICID);
1982 MYCONST(XML_ERROR_SUSPENDED);
1983 MYCONST(XML_ERROR_NOT_SUSPENDED);
1984 MYCONST(XML_ERROR_ABORTED);
1985 MYCONST(XML_ERROR_FINISHED);
1986 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001987
Fred Drake85d835f2001-02-08 15:39:08 +00001988 PyModule_AddStringConstant(errors_module, "__doc__",
1989 "Constants used to describe error conditions.");
1990
Fred Drake93adb692000-09-23 04:55:48 +00001991#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001992
Fred Drake85d835f2001-02-08 15:39:08 +00001993#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001994 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1995 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1996 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001997#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001998
Fred Drake85d835f2001-02-08 15:39:08 +00001999#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
2000 PyModule_AddStringConstant(model_module, "__doc__",
2001 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002002
Fred Drake85d835f2001-02-08 15:39:08 +00002003 MYCONST(XML_CTYPE_EMPTY);
2004 MYCONST(XML_CTYPE_ANY);
2005 MYCONST(XML_CTYPE_MIXED);
2006 MYCONST(XML_CTYPE_NAME);
2007 MYCONST(XML_CTYPE_CHOICE);
2008 MYCONST(XML_CTYPE_SEQ);
2009
2010 MYCONST(XML_CQUANT_NONE);
2011 MYCONST(XML_CQUANT_OPT);
2012 MYCONST(XML_CQUANT_REP);
2013 MYCONST(XML_CQUANT_PLUS);
2014#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002015}
2016
Fred Drake6f987622000-08-25 18:03:30 +00002017static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002018clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00002019{
Fred Drakecde79132001-04-25 16:01:30 +00002020 int i = 0;
2021 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002022
Fred Drake71b63ff2002-06-28 22:29:01 +00002023 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002024 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00002025 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002026 else {
Fred Drakecde79132001-04-25 16:01:30 +00002027 temp = self->handlers[i];
2028 self->handlers[i] = NULL;
2029 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002030 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002031 }
Fred Drakecde79132001-04-25 16:01:30 +00002032 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002033}
2034
Tim Peters0c322792002-07-17 16:49:03 +00002035static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002036 {"StartElementHandler",
2037 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002038 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002039 {"EndElementHandler",
2040 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002041 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002042 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002043 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2044 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002045 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002046 (xmlhandlersetter)XML_SetCharacterDataHandler,
2047 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002048 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002049 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002050 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002051 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002052 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002053 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002054 {"StartNamespaceDeclHandler",
2055 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002056 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002057 {"EndNamespaceDeclHandler",
2058 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002059 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002060 {"CommentHandler",
2061 (xmlhandlersetter)XML_SetCommentHandler,
2062 (xmlhandler)my_CommentHandler},
2063 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002064 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002065 (xmlhandler)my_StartCdataSectionHandler},
2066 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002067 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002068 (xmlhandler)my_EndCdataSectionHandler},
2069 {"DefaultHandler",
2070 (xmlhandlersetter)XML_SetDefaultHandler,
2071 (xmlhandler)my_DefaultHandler},
2072 {"DefaultHandlerExpand",
2073 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2074 (xmlhandler)my_DefaultHandlerExpandHandler},
2075 {"NotStandaloneHandler",
2076 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2077 (xmlhandler)my_NotStandaloneHandler},
2078 {"ExternalEntityRefHandler",
2079 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002080 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002081 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002082 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002083 (xmlhandler)my_StartDoctypeDeclHandler},
2084 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002085 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002086 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002087 {"EntityDeclHandler",
2088 (xmlhandlersetter)XML_SetEntityDeclHandler,
2089 (xmlhandler)my_EntityDeclHandler},
2090 {"XmlDeclHandler",
2091 (xmlhandlersetter)XML_SetXmlDeclHandler,
2092 (xmlhandler)my_XmlDeclHandler},
2093 {"ElementDeclHandler",
2094 (xmlhandlersetter)XML_SetElementDeclHandler,
2095 (xmlhandler)my_ElementDeclHandler},
2096 {"AttlistDeclHandler",
2097 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2098 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002099#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002100 {"SkippedEntityHandler",
2101 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2102 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002103#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002104
Fred Drake0582df92000-07-12 04:49:00 +00002105 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002106};