blob: d1def2408e662a6240cccf8d153c3dd9d89306a9 [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 "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00005#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00006
Martin v. Löwisc847f402003-01-21 11:09:21 +00007#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
8
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00009#ifndef PyDoc_STRVAR
Martin v. Löwis069dde22003-01-21 10:58:18 +000010
11/*
12 * fdrake says:
13 * Don't change the PyDoc_STR macro definition to (str), because
14 * '''the parentheses cause compile failures
15 * ("non-constant static initializer" or something like that)
16 * on some platforms (Irix?)'''
17 */
Fred Drakef57b22a2002-09-02 15:54:06 +000018#define PyDoc_STR(str) str
Fred Drake7c75bf22002-07-01 14:02:31 +000019#define PyDoc_VAR(name) static char name[]
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000020#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000021#endif
22
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000023#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
24/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000025#define Py_USING_UNICODE
Jeremy Hylton9263f572003-06-27 16:13:17 +000026#else
27#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000028#endif
29
Fred Drake0582df92000-07-12 04:49:00 +000030enum HandlerTypes {
31 StartElement,
32 EndElement,
33 ProcessingInstruction,
34 CharacterData,
35 UnparsedEntityDecl,
36 NotationDecl,
37 StartNamespaceDecl,
38 EndNamespaceDecl,
39 Comment,
40 StartCdataSection,
41 EndCdataSection,
42 Default,
43 DefaultHandlerExpand,
44 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000045 ExternalEntityRef,
46 StartDoctypeDecl,
47 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000048 EntityDecl,
49 XmlDecl,
50 ElementDecl,
51 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000052#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000053 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000054#endif
Fred Drake85d835f2001-02-08 15:39:08 +000055 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000056};
57
58static PyObject *ErrorObject;
59
60/* ----------------------------------------------------- */
61
62/* Declarations for objects of type xmlparser */
63
64typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000065 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000066
Fred Drake0582df92000-07-12 04:49:00 +000067 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000068 int returns_unicode; /* True if Unicode strings are returned;
69 if false, UTF-8 strings are returned */
70 int ordered_attributes; /* Return attributes as a list. */
71 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000072 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000073 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000074 XML_Char *buffer; /* Buffer used when accumulating characters */
75 /* NULL if not enabled */
76 int buffer_size; /* Size of buffer, in XML_Char units */
77 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000078 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000079 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080} xmlparseobject;
81
Fred Drake2a3d7db2002-06-28 22:56:48 +000082#define CHARACTER_DATA_BUFFER_SIZE 8192
83
Jeremy Hylton938ace62002-07-17 16:30:39 +000084static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000085
Fred Drake117ac852002-09-24 16:24:54 +000086typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000087typedef void* xmlhandler;
88
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000089struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000090 const char *name;
91 xmlhandlersetter setter;
92 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000093 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000094 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000095};
96
Jeremy Hylton938ace62002-07-17 16:30:39 +000097static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000098
Fred Drakebd6101c2001-02-14 18:29:45 +000099/* Set an integer attribute on the error object; return true on success,
100 * false on an exception.
101 */
102static int
103set_error_attr(PyObject *err, char *name, int value)
104{
105 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000106
Fred Drakebd6101c2001-02-14 18:29:45 +0000107 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
108 Py_DECREF(v);
109 return 0;
110 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000111 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000112 return 1;
113}
114
115/* Build and set an Expat exception, including positioning
116 * information. Always returns NULL.
117 */
Fred Drake85d835f2001-02-08 15:39:08 +0000118static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000119set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000120{
121 PyObject *err;
122 char buffer[256];
123 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000124 int lineno = XML_GetErrorLineNumber(parser);
125 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000126
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000127 /* There is no risk of overflowing this buffer, since
128 even for 64-bit integers, there is sufficient space. */
129 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000130 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000131 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000132 if ( err != NULL
133 && set_error_attr(err, "code", code)
134 && set_error_attr(err, "offset", column)
135 && set_error_attr(err, "lineno", lineno)) {
136 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000137 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000138 Py_DECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000139 return NULL;
140}
141
Fred Drake71b63ff2002-06-28 22:29:01 +0000142static int
143have_handler(xmlparseobject *self, int type)
144{
145 PyObject *handler = self->handlers[type];
146 return handler != NULL;
147}
148
149static PyObject *
150get_handler_name(struct HandlerInfo *hinfo)
151{
152 PyObject *name = hinfo->nameobj;
153 if (name == NULL) {
154 name = PyString_FromString(hinfo->name);
155 hinfo->nameobj = name;
156 }
157 Py_XINCREF(name);
158 return name;
159}
160
Fred Drake85d835f2001-02-08 15:39:08 +0000161
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000162#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000163/* Convert a string of XML_Chars into a Unicode string.
164 Returns None if str is a null pointer. */
165
Fred Drake0582df92000-07-12 04:49:00 +0000166static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000167conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000168{
Fred Drake71b63ff2002-06-28 22:29:01 +0000169 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000170 and hence in UTF-8. */
171 /* UTF-8 from Expat, Unicode desired */
172 if (str == NULL) {
173 Py_INCREF(Py_None);
174 return Py_None;
175 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000176 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000177}
178
Fred Drake0582df92000-07-12 04:49:00 +0000179static PyObject *
180conv_string_len_to_unicode(const XML_Char *str, int len)
181{
Fred Drake71b63ff2002-06-28 22:29:01 +0000182 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000183 and hence in UTF-8. */
184 /* UTF-8 from Expat, Unicode desired */
185 if (str == NULL) {
186 Py_INCREF(Py_None);
187 return Py_None;
188 }
Fred Drake6f987622000-08-25 18:03:30 +0000189 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000190}
191#endif
192
193/* Convert a string of XML_Chars into an 8-bit Python string.
194 Returns None if str is a null pointer. */
195
Fred Drake6f987622000-08-25 18:03:30 +0000196static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000197conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000198{
Fred Drake71b63ff2002-06-28 22:29:01 +0000199 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000200 and hence in UTF-8. */
201 /* UTF-8 from Expat, UTF-8 desired */
202 if (str == NULL) {
203 Py_INCREF(Py_None);
204 return Py_None;
205 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000206 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000207}
208
Fred Drake6f987622000-08-25 18:03:30 +0000209static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000210conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000211{
Fred Drake71b63ff2002-06-28 22:29:01 +0000212 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000213 and hence in UTF-8. */
214 /* UTF-8 from Expat, UTF-8 desired */
215 if (str == NULL) {
216 Py_INCREF(Py_None);
217 return Py_None;
218 }
219 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000220}
221
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000222/* Callback routines */
223
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000224static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000225
Martin v. Löwis069dde22003-01-21 10:58:18 +0000226/* This handler is used when an error has been detected, in the hope
227 that actual parsing can be terminated early. This will only help
228 if an external entity reference is encountered. */
229static int
230error_external_entity_ref_handler(XML_Parser parser,
231 const XML_Char *context,
232 const XML_Char *base,
233 const XML_Char *systemId,
234 const XML_Char *publicId)
235{
236 return 0;
237}
238
Fred Drake6f987622000-08-25 18:03:30 +0000239static void
240flag_error(xmlparseobject *self)
241{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000242 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000243 XML_SetExternalEntityRefHandler(self->itself,
244 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000245}
246
247static PyCodeObject*
248getcode(enum HandlerTypes slot, char* func_name, int lineno)
249{
Fred Drakebd6101c2001-02-14 18:29:45 +0000250 PyObject *code = NULL;
251 PyObject *name = NULL;
252 PyObject *nulltuple = NULL;
253 PyObject *filename = NULL;
254
255 if (handler_info[slot].tb_code == NULL) {
256 code = PyString_FromString("");
257 if (code == NULL)
258 goto failed;
259 name = PyString_FromString(func_name);
260 if (name == NULL)
261 goto failed;
262 nulltuple = PyTuple_New(0);
263 if (nulltuple == NULL)
264 goto failed;
265 filename = PyString_FromString(__FILE__);
266 handler_info[slot].tb_code =
267 PyCode_New(0, /* argcount */
268 0, /* nlocals */
269 0, /* stacksize */
270 0, /* flags */
271 code, /* code */
272 nulltuple, /* consts */
273 nulltuple, /* names */
274 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000275#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000276 nulltuple, /* freevars */
277 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000278#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000279 filename, /* filename */
280 name, /* name */
281 lineno, /* firstlineno */
282 code /* lnotab */
283 );
284 if (handler_info[slot].tb_code == NULL)
285 goto failed;
286 Py_DECREF(code);
287 Py_DECREF(nulltuple);
288 Py_DECREF(filename);
289 Py_DECREF(name);
290 }
291 return handler_info[slot].tb_code;
292 failed:
293 Py_XDECREF(code);
294 Py_XDECREF(name);
295 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000296}
297
Jeremy Hylton9263f572003-06-27 16:13:17 +0000298#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000299static int
300trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
301{
302 int result = 0;
303 if (!tstate->use_tracing || tstate->tracing)
304 return 0;
305 if (tstate->c_profilefunc != NULL) {
306 tstate->tracing++;
307 result = tstate->c_profilefunc(tstate->c_profileobj,
308 f, code , val);
309 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
310 || (tstate->c_profilefunc != NULL));
311 tstate->tracing--;
312 if (result)
313 return result;
314 }
315 if (tstate->c_tracefunc != NULL) {
316 tstate->tracing++;
317 result = tstate->c_tracefunc(tstate->c_traceobj,
318 f, code , val);
319 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
320 || (tstate->c_profilefunc != NULL));
321 tstate->tracing--;
322 }
323 return result;
324}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000325
326static int
327trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
328{
329 PyObject *type, *value, *traceback, *arg;
330 int err;
331
332 if (tstate->c_tracefunc == NULL)
333 return 0;
334
335 PyErr_Fetch(&type, &value, &traceback);
336 if (value == NULL) {
337 value = Py_None;
338 Py_INCREF(value);
339 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000340#if PY_VERSION_HEX < 0x02040000
341 arg = Py_BuildValue("(OOO)", type, value, traceback);
342#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000343 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000344#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000345 if (arg == NULL) {
346 PyErr_Restore(type, value, traceback);
347 return 0;
348 }
349 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
350 Py_DECREF(arg);
351 if (err == 0)
352 PyErr_Restore(type, value, traceback);
353 else {
354 Py_XDECREF(type);
355 Py_XDECREF(value);
356 Py_XDECREF(traceback);
357 }
358 return err;
359}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000360#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000361
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000362static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000363call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
364 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000365{
Fred Drakebd6101c2001-02-14 18:29:45 +0000366 PyThreadState *tstate = PyThreadState_GET();
367 PyFrameObject *f;
368 PyObject *res;
369
370 if (c == NULL)
371 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000372
Jeremy Hylton9263f572003-06-27 16:13:17 +0000373 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000374 if (f == NULL)
375 return NULL;
376 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000377#ifdef FIX_TRACE
378 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000379 return NULL;
380 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000381#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000382 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000383 if (res == NULL) {
384 if (tstate->curexc_traceback == NULL)
385 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000386 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000387#ifdef FIX_TRACE
388 if (trace_frame_exc(tstate, f) < 0) {
389 return NULL;
390 }
391 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000392 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000393 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000394 Py_XDECREF(res);
395 res = NULL;
396 }
397 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000398#else
399 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000400#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000401 tstate->frame = f->f_back;
402 Py_DECREF(f);
403 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000404}
405
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000406#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000407#define STRING_CONV_FUNC conv_string_to_utf8
408#else
Martin v. Löwis069dde22003-01-21 10:58:18 +0000409/* Python 2.0 and later versions, when built with Unicode support */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000410#define STRING_CONV_FUNC (self->returns_unicode \
411 ? conv_string_to_unicode : conv_string_to_utf8)
412#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000413
Fred Drakeb91a36b2002-06-27 19:40:48 +0000414static PyObject*
415string_intern(xmlparseobject *self, const char* str)
416{
417 PyObject *result = STRING_CONV_FUNC(str);
418 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000419 /* result can be NULL if the unicode conversion failed. */
420 if (!result)
421 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000422 if (!self->intern)
423 return result;
424 value = PyDict_GetItem(self->intern, result);
425 if (!value) {
426 if (PyDict_SetItem(self->intern, result, result) == 0)
427 return result;
428 else
429 return NULL;
430 }
431 Py_INCREF(value);
432 Py_DECREF(result);
433 return value;
434}
435
Fred Drake2a3d7db2002-06-28 22:56:48 +0000436/* Return 0 on success, -1 on exception.
437 * flag_error() will be called before return if needed.
438 */
439static int
440call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
441{
442 PyObject *args;
443 PyObject *temp;
444
445 args = PyTuple_New(1);
446 if (args == NULL)
447 return -1;
448#ifdef Py_USING_UNICODE
449 temp = (self->returns_unicode
450 ? conv_string_len_to_unicode(buffer, len)
451 : conv_string_len_to_utf8(buffer, len));
452#else
453 temp = conv_string_len_to_utf8(buffer, len);
454#endif
455 if (temp == NULL) {
456 Py_DECREF(args);
457 flag_error(self);
458 return -1;
459 }
460 PyTuple_SET_ITEM(args, 0, temp);
461 /* temp is now a borrowed reference; consider it unused. */
462 self->in_callback = 1;
463 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000464 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000465 /* temp is an owned reference again, or NULL */
466 self->in_callback = 0;
467 Py_DECREF(args);
468 if (temp == NULL) {
469 flag_error(self);
470 return -1;
471 }
472 Py_DECREF(temp);
473 return 0;
474}
475
476static int
477flush_character_buffer(xmlparseobject *self)
478{
479 int rc;
480 if (self->buffer == NULL || self->buffer_used == 0)
481 return 0;
482 rc = call_character_handler(self, self->buffer, self->buffer_used);
483 self->buffer_used = 0;
484 return rc;
485}
486
487static void
488my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
489{
490 xmlparseobject *self = (xmlparseobject *) userData;
491 if (self->buffer == NULL)
492 call_character_handler(self, data, len);
493 else {
494 if ((self->buffer_used + len) > self->buffer_size) {
495 if (flush_character_buffer(self) < 0)
496 return;
497 /* handler might have changed; drop the rest on the floor
498 * if there isn't a handler anymore
499 */
500 if (!have_handler(self, CharacterData))
501 return;
502 }
503 if (len > self->buffer_size) {
504 call_character_handler(self, data, len);
505 self->buffer_used = 0;
506 }
507 else {
508 memcpy(self->buffer + self->buffer_used,
509 data, len * sizeof(XML_Char));
510 self->buffer_used += len;
511 }
512 }
513}
514
Fred Drake85d835f2001-02-08 15:39:08 +0000515static void
516my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000517 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000518{
519 xmlparseobject *self = (xmlparseobject *)userData;
520
Fred Drake71b63ff2002-06-28 22:29:01 +0000521 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000522 PyObject *container, *rv, *args;
523 int i, max;
524
Fred Drake2a3d7db2002-06-28 22:56:48 +0000525 if (flush_character_buffer(self) < 0)
526 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000527 /* Set max to the number of slots filled in atts[]; max/2 is
528 * the number of attributes we need to process.
529 */
530 if (self->specified_attributes) {
531 max = XML_GetSpecifiedAttributeCount(self->itself);
532 }
533 else {
534 max = 0;
535 while (atts[max] != NULL)
536 max += 2;
537 }
538 /* Build the container. */
539 if (self->ordered_attributes)
540 container = PyList_New(max);
541 else
542 container = PyDict_New();
543 if (container == NULL) {
544 flag_error(self);
545 return;
546 }
547 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000548 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000549 PyObject *v;
550 if (n == NULL) {
551 flag_error(self);
552 Py_DECREF(container);
553 return;
554 }
555 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
556 if (v == NULL) {
557 flag_error(self);
558 Py_DECREF(container);
559 Py_DECREF(n);
560 return;
561 }
562 if (self->ordered_attributes) {
563 PyList_SET_ITEM(container, i, n);
564 PyList_SET_ITEM(container, i+1, v);
565 }
566 else if (PyDict_SetItem(container, n, v)) {
567 flag_error(self);
568 Py_DECREF(n);
569 Py_DECREF(v);
570 return;
571 }
572 else {
573 Py_DECREF(n);
574 Py_DECREF(v);
575 }
576 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000577 args = string_intern(self, name);
578 if (args != NULL)
579 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000580 if (args == NULL) {
581 Py_DECREF(container);
582 return;
583 }
584 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000585 self->in_callback = 1;
586 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000587 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000588 self->in_callback = 0;
589 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000590 if (rv == NULL) {
591 flag_error(self);
592 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000593 }
Fred Drake85d835f2001-02-08 15:39:08 +0000594 Py_DECREF(rv);
595 }
596}
597
598#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
599 RETURN, GETUSERDATA) \
600static RC \
601my_##NAME##Handler PARAMS {\
602 xmlparseobject *self = GETUSERDATA ; \
603 PyObject *args = NULL; \
604 PyObject *rv = NULL; \
605 INIT \
606\
Fred Drake71b63ff2002-06-28 22:29:01 +0000607 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000608 if (flush_character_buffer(self) < 0) \
609 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000610 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000611 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000612 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000613 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000614 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000615 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000616 Py_DECREF(args); \
617 if (rv == NULL) { \
618 flag_error(self); \
619 return RETURN; \
620 } \
621 CONVERSION \
622 Py_DECREF(rv); \
623 } \
624 return RETURN; \
625}
626
Fred Drake6f987622000-08-25 18:03:30 +0000627#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
628 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
629 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000630
Fred Drake6f987622000-08-25 18:03:30 +0000631#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
632 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
633 rc = PyInt_AsLong(rv);, rc, \
634 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000635
Fred Drake71b63ff2002-06-28 22:29:01 +0000636VOID_HANDLER(EndElement,
637 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000638 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000639
Fred Drake6f987622000-08-25 18:03:30 +0000640VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000641 (void *userData,
642 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000643 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000644 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000645
Fred Drake6f987622000-08-25 18:03:30 +0000646VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000647 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000648 const XML_Char *entityName,
649 const XML_Char *base,
650 const XML_Char *systemId,
651 const XML_Char *publicId,
652 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000653 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000654 string_intern(self, entityName), string_intern(self, base),
655 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000656 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000657
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000658#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000659VOID_HANDLER(EntityDecl,
660 (void *userData,
661 const XML_Char *entityName,
662 int is_parameter_entity,
663 const XML_Char *value,
664 int value_length,
665 const XML_Char *base,
666 const XML_Char *systemId,
667 const XML_Char *publicId,
668 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000669 ("NiNNNNN",
670 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000671 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000672 string_intern(self, base), string_intern(self, systemId),
673 string_intern(self, publicId),
674 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000675#else
676VOID_HANDLER(EntityDecl,
677 (void *userData,
678 const XML_Char *entityName,
679 int is_parameter_entity,
680 const XML_Char *value,
681 int value_length,
682 const XML_Char *base,
683 const XML_Char *systemId,
684 const XML_Char *publicId,
685 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000686 ("NiNNNNN",
687 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000688 (self->returns_unicode
689 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000690 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000691 string_intern(self, base), string_intern(self, systemId),
692 string_intern(self, publicId),
693 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000694#endif
695
696VOID_HANDLER(XmlDecl,
697 (void *userData,
698 const XML_Char *version,
699 const XML_Char *encoding,
700 int standalone),
701 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000702 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000703 standalone))
704
705static PyObject *
706conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000707 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000708{
709 PyObject *result = NULL;
710 PyObject *children = PyTuple_New(model->numchildren);
711 int i;
712
713 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000714 assert(model->numchildren < INT_MAX);
715 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000716 PyObject *child = conv_content_model(&model->children[i],
717 conv_string);
718 if (child == NULL) {
719 Py_XDECREF(children);
720 return NULL;
721 }
722 PyTuple_SET_ITEM(children, i, child);
723 }
724 result = Py_BuildValue("(iiO&N)",
725 model->type, model->quant,
726 conv_string,model->name, children);
727 }
728 return result;
729}
730
Fred Drake06dd8cf2003-02-02 03:54:17 +0000731static void
732my_ElementDeclHandler(void *userData,
733 const XML_Char *name,
734 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000735{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000736 xmlparseobject *self = (xmlparseobject *)userData;
737 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000738
Fred Drake06dd8cf2003-02-02 03:54:17 +0000739 if (have_handler(self, ElementDecl)) {
740 PyObject *rv = NULL;
741 PyObject *modelobj, *nameobj;
742
743 if (flush_character_buffer(self) < 0)
744 goto finally;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000745#ifdef Py_USING_UNICODE
Fred Drake06dd8cf2003-02-02 03:54:17 +0000746 modelobj = conv_content_model(model,
747 (self->returns_unicode
748 ? conv_string_to_unicode
749 : conv_string_to_utf8));
Fred Drake85d835f2001-02-08 15:39:08 +0000750#else
Fred Drake06dd8cf2003-02-02 03:54:17 +0000751 modelobj = conv_content_model(model, conv_string_to_utf8);
Fred Drake85d835f2001-02-08 15:39:08 +0000752#endif
Fred Drake06dd8cf2003-02-02 03:54:17 +0000753 if (modelobj == NULL) {
754 flag_error(self);
755 goto finally;
756 }
757 nameobj = string_intern(self, name);
758 if (nameobj == NULL) {
759 Py_DECREF(modelobj);
760 flag_error(self);
761 goto finally;
762 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000763 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000764 if (args == NULL) {
765 Py_DECREF(modelobj);
766 flag_error(self);
767 goto finally;
768 }
769 self->in_callback = 1;
770 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000771 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000772 self->in_callback = 0;
773 if (rv == NULL) {
774 flag_error(self);
775 goto finally;
776 }
777 Py_DECREF(rv);
778 }
779 finally:
780 Py_XDECREF(args);
781 XML_FreeContentModel(self->itself, model);
782 return;
783}
Fred Drake85d835f2001-02-08 15:39:08 +0000784
785VOID_HANDLER(AttlistDecl,
786 (void *userData,
787 const XML_Char *elname,
788 const XML_Char *attname,
789 const XML_Char *att_type,
790 const XML_Char *dflt,
791 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000792 ("(NNO&O&i)",
793 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000794 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
795 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000796
Martin v. Löwisc847f402003-01-21 11:09:21 +0000797#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000798VOID_HANDLER(SkippedEntity,
799 (void *userData,
800 const XML_Char *entityName,
801 int is_parameter_entity),
802 ("Ni",
803 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000804#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000805
Fred Drake71b63ff2002-06-28 22:29:01 +0000806VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000807 (void *userData,
808 const XML_Char *notationName,
809 const XML_Char *base,
810 const XML_Char *systemId,
811 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000812 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000813 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000814 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000815
Fred Drake6f987622000-08-25 18:03:30 +0000816VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000817 (void *userData,
818 const XML_Char *prefix,
819 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000820 ("(NN)",
821 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000822
Fred Drake6f987622000-08-25 18:03:30 +0000823VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000824 (void *userData,
825 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000826 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000827
Fred Drake6f987622000-08-25 18:03:30 +0000828VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000829 (void *userData, const XML_Char *data),
830 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000831
Fred Drake6f987622000-08-25 18:03:30 +0000832VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000834 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000835
Fred Drake6f987622000-08-25 18:03:30 +0000836VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000837 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000838 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000839
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000840#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000841VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000842 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000843 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000844
Fred Drake6f987622000-08-25 18:03:30 +0000845VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000846 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000847 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000848#else
Fred Drake6f987622000-08-25 18:03:30 +0000849VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000850 (void *userData, const XML_Char *s, int len),
851 ("(N)", (self->returns_unicode
852 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000853 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000854
Fred Drake6f987622000-08-25 18:03:30 +0000855VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000856 (void *userData, const XML_Char *s, int len),
857 ("(N)", (self->returns_unicode
858 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000859 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000860#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000861
Fred Drake71b63ff2002-06-28 22:29:01 +0000862INT_HANDLER(NotStandalone,
863 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000864 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000865
Fred Drake6f987622000-08-25 18:03:30 +0000866RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000867 (XML_Parser parser,
868 const XML_Char *context,
869 const XML_Char *base,
870 const XML_Char *systemId,
871 const XML_Char *publicId),
872 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000873 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000874 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000875 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000876 rc = PyInt_AsLong(rv);, rc,
877 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000879/* XXX UnknownEncodingHandler */
880
Fred Drake85d835f2001-02-08 15:39:08 +0000881VOID_HANDLER(StartDoctypeDecl,
882 (void *userData, const XML_Char *doctypeName,
883 const XML_Char *sysid, const XML_Char *pubid,
884 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000885 ("(NNNi)", string_intern(self, doctypeName),
886 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000887 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000888
889VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000891/* ---------------------------------------------------------------- */
892
Fred Drake71b63ff2002-06-28 22:29:01 +0000893static PyObject *
894get_parse_result(xmlparseobject *self, int rv)
895{
896 if (PyErr_Occurred()) {
897 return NULL;
898 }
899 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000900 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000901 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000902 if (flush_character_buffer(self) < 0) {
903 return NULL;
904 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000905 return PyInt_FromLong(rv);
906}
907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000908PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000909"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000911
912static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000913xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000914{
Fred Drake0582df92000-07-12 04:49:00 +0000915 char *s;
916 int slen;
917 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000918
Fred Drake0582df92000-07-12 04:49:00 +0000919 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
920 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000921
922 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000923}
924
Fred Drakeca1f4262000-09-21 20:10:23 +0000925/* File reading copied from cPickle */
926
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000927#define BUF_SIZE 2048
928
Fred Drake0582df92000-07-12 04:49:00 +0000929static int
930readinst(char *buf, int buf_size, PyObject *meth)
931{
932 PyObject *arg = NULL;
933 PyObject *bytes = NULL;
934 PyObject *str = NULL;
935 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000936
Fred Drake676940b2000-09-22 15:21:31 +0000937 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000938 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000939
Fred Drake7b6caff2003-07-21 17:05:56 +0000940 if ((arg = PyTuple_New(1)) == NULL) {
941 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000942 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000943 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000944
Tim Peters954eef72000-09-22 06:01:11 +0000945 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000946
Martin v. Löwis9171f022004-10-13 19:50:11 +0000947#if PY_VERSION_HEX < 0x02020000
948 str = PyObject_CallObject(meth, arg);
949#else
950 str = PyObject_Call(meth, arg, NULL);
951#endif
952 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000953 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000954
Fred Drake0582df92000-07-12 04:49:00 +0000955 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000956 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000957 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000958 "read() did not return a string object (type=%.400s)",
959 str->ob_type->tp_name);
960 goto finally;
961 }
962 len = PyString_GET_SIZE(str);
963 if (len > buf_size) {
964 PyErr_Format(PyExc_ValueError,
965 "read() returned too much data: "
966 "%i bytes requested, %i returned",
967 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000968 goto finally;
969 }
970 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000971finally:
Fred Drake0582df92000-07-12 04:49:00 +0000972 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000973 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000974 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000975}
976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000978"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000980
981static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000982xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000983{
Fred Drake0582df92000-07-12 04:49:00 +0000984 int rv = 1;
985 PyObject *f;
986 FILE *fp;
987 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000988
Fred Drake0582df92000-07-12 04:49:00 +0000989 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
990 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000991
Fred Drake0582df92000-07-12 04:49:00 +0000992 if (PyFile_Check(f)) {
993 fp = PyFile_AsFile(f);
994 }
995 else{
996 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000997 readmethod = PyObject_GetAttrString(f, "read");
998 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000999 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +00001000 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +00001001 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +00001002 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001003 }
1004 }
1005 for (;;) {
1006 int bytes_read;
1007 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +00001008 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +00001009 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001010 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +00001011 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001012
Fred Drake0582df92000-07-12 04:49:00 +00001013 if (fp) {
1014 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
1015 if (bytes_read < 0) {
1016 PyErr_SetFromErrno(PyExc_IOError);
1017 return NULL;
1018 }
1019 }
1020 else {
1021 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +00001022 if (bytes_read < 0) {
1023 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001024 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001025 }
Fred Drake0582df92000-07-12 04:49:00 +00001026 }
1027 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +00001028 if (PyErr_Occurred()) {
1029 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001030 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001031 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001032
Fred Drake0582df92000-07-12 04:49:00 +00001033 if (!rv || bytes_read == 0)
1034 break;
1035 }
Fred Drake7b6caff2003-07-21 17:05:56 +00001036 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +00001037 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001038}
1039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001041"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001043
1044static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001045xmlparse_SetBase(xmlparseobject *self, PyObject *args)
1046{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001047 char *base;
1048
Fred Drake0582df92000-07-12 04:49:00 +00001049 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001050 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001051 if (!XML_SetBase(self->itself, base)) {
1052 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001053 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001054 Py_INCREF(Py_None);
1055 return Py_None;
1056}
1057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001059"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001060Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001061
1062static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001063xmlparse_GetBase(xmlparseobject *self, PyObject *args)
1064{
1065 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001066 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001067
Fred Drake0582df92000-07-12 04:49:00 +00001068 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001069}
1070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +00001072"GetInputContext() -> string\n\
1073Return the untranslated text of the input that caused the current event.\n\
1074If 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 +00001075for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +00001076
1077static PyObject *
1078xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
1079{
1080 PyObject *result = NULL;
1081
1082 if (PyArg_ParseTuple(args, ":GetInputContext")) {
1083 if (self->in_callback) {
1084 int offset, size;
1085 const char *buffer
1086 = XML_GetInputContext(self->itself, &offset, &size);
1087
1088 if (buffer != NULL)
Martin v. Löwisfd78a6f2005-03-04 14:37:01 +00001089 result = PyString_FromStringAndSize(buffer + offset, size - offset);
Fred Drakebd6101c2001-02-14 18:29:45 +00001090 else {
1091 result = Py_None;
1092 Py_INCREF(result);
1093 }
1094 }
1095 else {
1096 result = Py_None;
1097 Py_INCREF(result);
1098 }
1099 }
1100 return result;
1101}
Fred Drakebd6101c2001-02-14 18:29:45 +00001102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001104"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001105Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001106information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001107
1108static PyObject *
1109xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1110{
1111 char *context;
1112 char *encoding = NULL;
1113 xmlparseobject *new_parser;
1114 int i;
1115
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001116 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001117 &context, &encoding)) {
1118 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001119 }
1120
Martin v. Löwis894258c2001-09-23 10:20:10 +00001121#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001122 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001123 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001124#else
1125 /* Python versions 2.2 and later */
1126 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1127#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001128
1129 if (new_parser == NULL)
1130 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001131 new_parser->buffer_size = self->buffer_size;
1132 new_parser->buffer_used = 0;
1133 if (self->buffer != NULL) {
1134 new_parser->buffer = malloc(new_parser->buffer_size);
1135 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001136#ifndef Py_TPFLAGS_HAVE_GC
1137 /* Code for versions 2.0 and 2.1 */
1138 PyObject_Del(new_parser);
1139#else
1140 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001141 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001142#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001143 return PyErr_NoMemory();
1144 }
1145 }
1146 else
1147 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001148 new_parser->returns_unicode = self->returns_unicode;
1149 new_parser->ordered_attributes = self->ordered_attributes;
1150 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001151 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001152 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001153 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001154 encoding);
1155 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001156 new_parser->intern = self->intern;
1157 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001158#ifdef Py_TPFLAGS_HAVE_GC
1159 PyObject_GC_Track(new_parser);
1160#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001161 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001162#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001163
1164 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001165 Py_DECREF(new_parser);
1166 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001167 }
1168
1169 XML_SetUserData(new_parser->itself, (void *)new_parser);
1170
1171 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001172 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001173 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001174
Fred Drake2a3d7db2002-06-28 22:56:48 +00001175 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001176 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001177 Py_DECREF(new_parser);
1178 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001179 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001180 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001181
1182 /* then copy handlers from self */
1183 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001184 PyObject *handler = self->handlers[i];
1185 if (handler != NULL) {
1186 Py_INCREF(handler);
1187 new_parser->handlers[i] = handler;
1188 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001189 handler_info[i].handler);
1190 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001191 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001192 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001193}
1194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001196"SetParamEntityParsing(flag) -> success\n\
1197Controls parsing of parameter entities (including the external DTD\n\
1198subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1199XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1200XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001202
1203static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001204xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001205{
Fred Drake85d835f2001-02-08 15:39:08 +00001206 int flag;
1207 if (!PyArg_ParseTuple(args, "i", &flag))
1208 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001209 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001210 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001211}
1212
Martin v. Löwisc847f402003-01-21 11:09:21 +00001213
1214#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001215PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1216"UseForeignDTD([flag])\n\
1217Allows the application to provide an artificial external subset if one is\n\
1218not specified as part of the document instance. This readily allows the\n\
1219use of a 'default' document type controlled by the application, while still\n\
1220getting the advantage of providing document type information to the parser.\n\
1221'flag' defaults to True if not provided.");
1222
1223static PyObject *
1224xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1225{
1226 PyObject *flagobj = NULL;
1227 XML_Bool flag = XML_TRUE;
1228 enum XML_Error rc;
1229 if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj))
1230 return NULL;
1231 if (flagobj != NULL)
1232 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1233 rc = XML_UseForeignDTD(self->itself, flag);
1234 if (rc != XML_ERROR_NONE) {
1235 return set_error(self, rc);
1236 }
1237 Py_INCREF(Py_None);
1238 return Py_None;
1239}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001240#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001241
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001242static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001243 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001244 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001245 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001246 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001247 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001248 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001249 {"GetBase", (PyCFunction)xmlparse_GetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001250 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001251 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001252 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001253 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1254 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001255 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1256 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001257#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001258 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1259 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001260#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001261 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001262};
1263
1264/* ---------- */
1265
1266
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001267#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001268
Fred Drake71b63ff2002-06-28 22:29:01 +00001269/* pyexpat international encoding support.
1270 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001271*/
1272
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001273static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001274PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001275
Fred Drake71b63ff2002-06-28 22:29:01 +00001276static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001277init_template_buffer(void)
1278{
1279 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001280 for (i = 0; i < 256; i++) {
1281 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001282 }
Fred Drakebb66a202001-03-01 20:48:17 +00001283 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001284}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001285
Fred Drake71b63ff2002-06-28 22:29:01 +00001286static int
1287PyUnknownEncodingHandler(void *encodingHandlerData,
1288 const XML_Char *name,
1289 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001290{
Fred Drakebb66a202001-03-01 20:48:17 +00001291 PyUnicodeObject *_u_string = NULL;
1292 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001293 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001294
Fred Drakebb66a202001-03-01 20:48:17 +00001295 /* Yes, supports only 8bit encodings */
1296 _u_string = (PyUnicodeObject *)
1297 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001298
Fred Drakebb66a202001-03-01 20:48:17 +00001299 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001300 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001301
Fred Drakebb66a202001-03-01 20:48:17 +00001302 for (i = 0; i < 256; i++) {
1303 /* Stupid to access directly, but fast */
1304 Py_UNICODE c = _u_string->str[i];
1305 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001306 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001307 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001308 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001309 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001310 info->data = NULL;
1311 info->convert = NULL;
1312 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001313 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001314 Py_DECREF(_u_string);
1315 return result;
1316}
1317
1318#endif
1319
1320static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001321newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001322{
1323 int i;
1324 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001325
Martin v. Löwis894258c2001-09-23 10:20:10 +00001326#ifdef Py_TPFLAGS_HAVE_GC
1327 /* Code for versions 2.2 and later */
1328 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1329#else
Fred Drake0582df92000-07-12 04:49:00 +00001330 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001331#endif
Fred Drake0582df92000-07-12 04:49:00 +00001332 if (self == NULL)
1333 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001334
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001335#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001336 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001337#else
1338 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001339#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001340
Fred Drake2a3d7db2002-06-28 22:56:48 +00001341 self->buffer = NULL;
1342 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1343 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001344 self->ordered_attributes = 0;
1345 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001346 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001347 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001348 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001349 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001350 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1351 }
Fred Drake85d835f2001-02-08 15:39:08 +00001352 else {
Fred Drake0582df92000-07-12 04:49:00 +00001353 self->itself = XML_ParserCreate(encoding);
1354 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001355 self->intern = intern;
1356 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001357#ifdef Py_TPFLAGS_HAVE_GC
1358 PyObject_GC_Track(self);
1359#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001360 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001361#endif
Fred Drake0582df92000-07-12 04:49:00 +00001362 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001363 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001364 "XML_ParserCreate failed");
1365 Py_DECREF(self);
1366 return NULL;
1367 }
1368 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001369#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001370 XML_SetUnknownEncodingHandler(self->itself,
1371 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001372#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001373
Fred Drake2a3d7db2002-06-28 22:56:48 +00001374 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001375 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001376
Fred Drake7c75bf22002-07-01 14:02:31 +00001377 self->handlers = malloc(sizeof(PyObject *) * i);
1378 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001379 Py_DECREF(self);
1380 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001381 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001382 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001383
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001384 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385}
1386
1387
1388static void
Fred Drake0582df92000-07-12 04:49:00 +00001389xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001390{
Fred Drake0582df92000-07-12 04:49:00 +00001391 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001392#ifdef Py_TPFLAGS_HAVE_GC
1393 PyObject_GC_UnTrack(self);
1394#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001395 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001396#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001397 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001398 XML_ParserFree(self->itself);
1399 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001400
Fred Drake85d835f2001-02-08 15:39:08 +00001401 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001402 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001403 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001404 temp = self->handlers[i];
1405 self->handlers[i] = NULL;
1406 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001407 }
1408 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001409 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001410 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001411 if (self->buffer != NULL) {
1412 free(self->buffer);
1413 self->buffer = NULL;
1414 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001415 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001416#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001417 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001418 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001419#else
1420 /* Code for versions 2.2 and later. */
1421 PyObject_GC_Del(self);
1422#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001423}
1424
Fred Drake0582df92000-07-12 04:49:00 +00001425static int
1426handlername2int(const char *name)
1427{
1428 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001429 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001430 if (strcmp(name, handler_info[i].name) == 0) {
1431 return i;
1432 }
1433 }
1434 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001435}
1436
1437static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001438get_pybool(int istrue)
1439{
1440 PyObject *result = istrue ? Py_True : Py_False;
1441 Py_INCREF(result);
1442 return result;
1443}
1444
1445static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001446xmlparse_getattr(xmlparseobject *self, char *name)
1447{
Fred Drake71b63ff2002-06-28 22:29:01 +00001448 int handlernum = handlername2int(name);
1449
1450 if (handlernum != -1) {
1451 PyObject *result = self->handlers[handlernum];
1452 if (result == NULL)
1453 result = Py_None;
1454 Py_INCREF(result);
1455 return result;
1456 }
1457 if (name[0] == 'E') {
1458 if (strcmp(name, "ErrorCode") == 0)
1459 return PyInt_FromLong((long)
1460 XML_GetErrorCode(self->itself));
1461 if (strcmp(name, "ErrorLineNumber") == 0)
1462 return PyInt_FromLong((long)
1463 XML_GetErrorLineNumber(self->itself));
1464 if (strcmp(name, "ErrorColumnNumber") == 0)
1465 return PyInt_FromLong((long)
1466 XML_GetErrorColumnNumber(self->itself));
1467 if (strcmp(name, "ErrorByteIndex") == 0)
1468 return PyInt_FromLong((long)
1469 XML_GetErrorByteIndex(self->itself));
1470 }
Dave Cole3203efb2004-08-26 00:37:31 +00001471 if (name[0] == 'C') {
1472 if (strcmp(name, "CurrentLineNumber") == 0)
1473 return PyInt_FromLong((long)
1474 XML_GetCurrentLineNumber(self->itself));
1475 if (strcmp(name, "CurrentColumnNumber") == 0)
1476 return PyInt_FromLong((long)
1477 XML_GetCurrentColumnNumber(self->itself));
1478 if (strcmp(name, "CurrentByteIndex") == 0)
1479 return PyInt_FromLong((long)
1480 XML_GetCurrentByteIndex(self->itself));
1481 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001482 if (name[0] == 'b') {
1483 if (strcmp(name, "buffer_size") == 0)
1484 return PyInt_FromLong((long) self->buffer_size);
1485 if (strcmp(name, "buffer_text") == 0)
1486 return get_pybool(self->buffer != NULL);
1487 if (strcmp(name, "buffer_used") == 0)
1488 return PyInt_FromLong((long) self->buffer_used);
1489 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001490 if (strcmp(name, "namespace_prefixes") == 0)
1491 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001492 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001493 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001494 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001495 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001496 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001497 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001498 if (strcmp(name, "intern") == 0) {
1499 if (self->intern == NULL) {
1500 Py_INCREF(Py_None);
1501 return Py_None;
1502 }
1503 else {
1504 Py_INCREF(self->intern);
1505 return self->intern;
1506 }
1507 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001508
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001509#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001510 do { \
1511 PyObject *o = PyString_FromString(str); \
1512 if (o != NULL) \
1513 PyList_Append(list, o); \
1514 Py_XDECREF(o); \
1515 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001516
Fred Drake0582df92000-07-12 04:49:00 +00001517 if (strcmp(name, "__members__") == 0) {
1518 int i;
1519 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001520 for (i = 0; handler_info[i].name != NULL; i++) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001521 PyObject *o = get_handler_name(&handler_info[i]);
1522 if (o != NULL)
1523 PyList_Append(rc, o);
1524 Py_XDECREF(o);
Fred Drake0582df92000-07-12 04:49:00 +00001525 }
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001526 APPEND(rc, "ErrorCode");
1527 APPEND(rc, "ErrorLineNumber");
1528 APPEND(rc, "ErrorColumnNumber");
1529 APPEND(rc, "ErrorByteIndex");
Dave Cole3203efb2004-08-26 00:37:31 +00001530 APPEND(rc, "CurrentLineNumber");
1531 APPEND(rc, "CurrentColumnNumber");
1532 APPEND(rc, "CurrentByteIndex");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001533 APPEND(rc, "buffer_size");
1534 APPEND(rc, "buffer_text");
1535 APPEND(rc, "buffer_used");
Martin v. Löwis069dde22003-01-21 10:58:18 +00001536 APPEND(rc, "namespace_prefixes");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001537 APPEND(rc, "ordered_attributes");
1538 APPEND(rc, "returns_unicode");
1539 APPEND(rc, "specified_attributes");
1540 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001541
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001542#undef APPEND
Fred Drake0582df92000-07-12 04:49:00 +00001543 return rc;
1544 }
1545 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001546}
1547
Fred Drake6f987622000-08-25 18:03:30 +00001548static int
1549sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001550{
1551 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001552 if (handlernum >= 0) {
1553 xmlhandler c_handler = NULL;
1554 PyObject *temp = self->handlers[handlernum];
1555
1556 if (v == Py_None)
1557 v = NULL;
1558 else if (v != NULL) {
1559 Py_INCREF(v);
1560 c_handler = handler_info[handlernum].handler;
1561 }
Fred Drake0582df92000-07-12 04:49:00 +00001562 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001563 Py_XDECREF(temp);
1564 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001565 return 1;
1566 }
1567 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001568}
1569
1570static int
Fred Drake6f987622000-08-25 18:03:30 +00001571xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001572{
Fred Drake6f987622000-08-25 18:03:30 +00001573 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001574 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001575 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1576 return -1;
1577 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001578 if (strcmp(name, "buffer_text") == 0) {
1579 if (PyObject_IsTrue(v)) {
1580 if (self->buffer == NULL) {
1581 self->buffer = malloc(self->buffer_size);
1582 if (self->buffer == NULL) {
1583 PyErr_NoMemory();
1584 return -1;
1585 }
1586 self->buffer_used = 0;
1587 }
1588 }
1589 else if (self->buffer != NULL) {
1590 if (flush_character_buffer(self) < 0)
1591 return -1;
1592 free(self->buffer);
1593 self->buffer = NULL;
1594 }
1595 return 0;
1596 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001597 if (strcmp(name, "namespace_prefixes") == 0) {
1598 if (PyObject_IsTrue(v))
1599 self->ns_prefixes = 1;
1600 else
1601 self->ns_prefixes = 0;
1602 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1603 return 0;
1604 }
Fred Drake85d835f2001-02-08 15:39:08 +00001605 if (strcmp(name, "ordered_attributes") == 0) {
1606 if (PyObject_IsTrue(v))
1607 self->ordered_attributes = 1;
1608 else
1609 self->ordered_attributes = 0;
1610 return 0;
1611 }
Fred Drake6f987622000-08-25 18:03:30 +00001612 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001613 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001614#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001615 PyErr_SetString(PyExc_ValueError,
1616 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001617 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001618#else
Fred Drake6f987622000-08-25 18:03:30 +00001619 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001620#endif
Fred Drake6f987622000-08-25 18:03:30 +00001621 }
1622 else
1623 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001624 return 0;
1625 }
1626 if (strcmp(name, "specified_attributes") == 0) {
1627 if (PyObject_IsTrue(v))
1628 self->specified_attributes = 1;
1629 else
1630 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001631 return 0;
1632 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001633 if (strcmp(name, "CharacterDataHandler") == 0) {
1634 /* If we're changing the character data handler, flush all
1635 * cached data with the old handler. Not sure there's a
1636 * "right" thing to do, though, but this probably won't
1637 * happen.
1638 */
1639 if (flush_character_buffer(self) < 0)
1640 return -1;
1641 }
Fred Drake6f987622000-08-25 18:03:30 +00001642 if (sethandler(self, name, v)) {
1643 return 0;
1644 }
1645 PyErr_SetString(PyExc_AttributeError, name);
1646 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001647}
1648
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001649#ifdef WITH_CYCLE_GC
1650static int
1651xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1652{
Fred Drakecde79132001-04-25 16:01:30 +00001653 int i, err;
1654 for (i = 0; handler_info[i].name != NULL; i++) {
1655 if (!op->handlers[i])
1656 continue;
1657 err = visit(op->handlers[i], arg);
1658 if (err)
1659 return err;
1660 }
1661 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001662}
1663
1664static int
1665xmlparse_clear(xmlparseobject *op)
1666{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001667 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001668 Py_XDECREF(op->intern);
1669 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001670 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001671}
1672#endif
1673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001675
1676static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001677 PyObject_HEAD_INIT(NULL)
1678 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001679 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001680 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001681 0, /*tp_itemsize*/
1682 /* methods */
1683 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1684 (printfunc)0, /*tp_print*/
1685 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1686 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1687 (cmpfunc)0, /*tp_compare*/
1688 (reprfunc)0, /*tp_repr*/
1689 0, /*tp_as_number*/
1690 0, /*tp_as_sequence*/
1691 0, /*tp_as_mapping*/
1692 (hashfunc)0, /*tp_hash*/
1693 (ternaryfunc)0, /*tp_call*/
1694 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001695 0, /* tp_getattro */
1696 0, /* tp_setattro */
1697 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001698#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001700#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001702#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001703 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001704#ifdef WITH_CYCLE_GC
1705 (traverseproc)xmlparse_traverse, /* tp_traverse */
1706 (inquiry)xmlparse_clear /* tp_clear */
1707#else
1708 0, 0
1709#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001710};
1711
1712/* End of code for xmlparser objects */
1713/* -------------------------------------------------------- */
1714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001715PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001716"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001718
1719static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001720pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1721{
Fred Drakecde79132001-04-25 16:01:30 +00001722 char *encoding = NULL;
1723 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001724 PyObject *intern = NULL;
1725 PyObject *result;
1726 int intern_decref = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001727 static const char *kwlist[] = {"encoding", "namespace_separator",
1728 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001729
Fred Drakeb91a36b2002-06-27 19:40:48 +00001730 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1731 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001732 return NULL;
1733 if (namespace_separator != NULL
1734 && strlen(namespace_separator) > 1) {
1735 PyErr_SetString(PyExc_ValueError,
1736 "namespace_separator must be at most one"
1737 " character, omitted, or None");
1738 return NULL;
1739 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001740 /* Explicitly passing None means no interning is desired.
1741 Not passing anything means that a new dictionary is used. */
1742 if (intern == Py_None)
1743 intern = NULL;
1744 else if (intern == NULL) {
1745 intern = PyDict_New();
1746 if (!intern)
1747 return NULL;
1748 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001749 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001750 else if (!PyDict_Check(intern)) {
1751 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1752 return NULL;
1753 }
1754
1755 result = newxmlparseobject(encoding, namespace_separator, intern);
1756 if (intern_decref) {
1757 Py_DECREF(intern);
1758 }
1759 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001760}
1761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001762PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001763"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001764Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001765
1766static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001767pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001768{
Fred Drake0582df92000-07-12 04:49:00 +00001769 long code = 0;
1770
1771 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1772 return NULL;
1773 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001774}
1775
1776/* List of methods defined in the module */
1777
1778static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001779 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1780 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1781 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1782 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001783
Fred Drake0582df92000-07-12 04:49:00 +00001784 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001785};
1786
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001787/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(pyexpat_module_documentation,
1790"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001791
Fred Drake4113b132001-03-24 19:58:26 +00001792/* Return a Python string that represents the version number without the
1793 * extra cruft added by revision control, even if the right options were
1794 * given to the "cvs export" command to make it not include the extra
1795 * cruft.
1796 */
1797static PyObject *
1798get_version_string(void)
1799{
1800 static char *rcsid = "$Revision$";
1801 char *rev = rcsid;
1802 int i = 0;
1803
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001804 while (!isdigit((int)*rev))
Fred Drake4113b132001-03-24 19:58:26 +00001805 ++rev;
1806 while (rev[i] != ' ' && rev[i] != '\0')
1807 ++i;
1808
1809 return PyString_FromStringAndSize(rev, i);
1810}
1811
Fred Drakecde79132001-04-25 16:01:30 +00001812/* Initialization function for the module */
1813
1814#ifndef MODULE_NAME
1815#define MODULE_NAME "pyexpat"
1816#endif
1817
1818#ifndef MODULE_INITFUNC
1819#define MODULE_INITFUNC initpyexpat
1820#endif
1821
Martin v. Löwis069dde22003-01-21 10:58:18 +00001822#ifndef PyMODINIT_FUNC
1823# ifdef MS_WINDOWS
1824# define PyMODINIT_FUNC __declspec(dllexport) void
1825# else
1826# define PyMODINIT_FUNC void
1827# endif
1828#endif
1829
Mark Hammond8235ea12002-07-19 06:55:41 +00001830PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001831
Martin v. Löwis069dde22003-01-21 10:58:18 +00001832PyMODINIT_FUNC
1833MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001834{
1835 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001836 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001837 PyObject *errors_module;
1838 PyObject *modelmod_name;
1839 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001840 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001841
Fred Drake6f987622000-08-25 18:03:30 +00001842 if (errmod_name == NULL)
1843 return;
Fred Drakecde79132001-04-25 16:01:30 +00001844 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001845 if (modelmod_name == NULL)
1846 return;
Fred Drake6f987622000-08-25 18:03:30 +00001847
Fred Drake0582df92000-07-12 04:49:00 +00001848 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001849
Fred Drake0582df92000-07-12 04:49:00 +00001850 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001851 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001852 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001853
Fred Drake0582df92000-07-12 04:49:00 +00001854 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001855 if (ErrorObject == NULL) {
1856 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001857 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001858 if (ErrorObject == NULL)
1859 return;
1860 }
1861 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001862 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001863 Py_INCREF(ErrorObject);
1864 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001865 Py_INCREF(&Xmlparsetype);
1866 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001867
Fred Drake4113b132001-03-24 19:58:26 +00001868 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001869 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1870 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001871 {
1872 XML_Expat_Version info = XML_ExpatVersionInfo();
1873 PyModule_AddObject(m, "version_info",
1874 Py_BuildValue("(iii)", info.major,
1875 info.minor, info.micro));
1876 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001877#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001878 init_template_buffer();
1879#endif
Fred Drake0582df92000-07-12 04:49:00 +00001880 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001881 compiled, this should check and set native_encoding
1882 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001883 */
Fred Drake93adb692000-09-23 04:55:48 +00001884 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001885
Fred Drake85d835f2001-02-08 15:39:08 +00001886 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001887 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001888 errors_module = PyDict_GetItem(d, errmod_name);
1889 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001890 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001891 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001892 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001893 /* gives away the reference to errors_module */
1894 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001895 }
1896 }
Fred Drake6f987622000-08-25 18:03:30 +00001897 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001898 model_module = PyDict_GetItem(d, modelmod_name);
1899 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001900 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001901 if (model_module != NULL) {
1902 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1903 /* gives away the reference to model_module */
1904 PyModule_AddObject(m, "model", model_module);
1905 }
1906 }
1907 Py_DECREF(modelmod_name);
1908 if (errors_module == NULL || model_module == NULL)
1909 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001910 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001911
Martin v. Löwisc847f402003-01-21 11:09:21 +00001912#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001913 {
1914 const XML_Feature *features = XML_GetFeatureList();
1915 PyObject *list = PyList_New(0);
1916 if (list == NULL)
1917 /* just ignore it */
1918 PyErr_Clear();
1919 else {
1920 int i = 0;
1921 for (; features[i].feature != XML_FEATURE_END; ++i) {
1922 int ok;
1923 PyObject *item = Py_BuildValue("si", features[i].name,
1924 features[i].value);
1925 if (item == NULL) {
1926 Py_DECREF(list);
1927 list = NULL;
1928 break;
1929 }
1930 ok = PyList_Append(list, item);
1931 Py_DECREF(item);
1932 if (ok < 0) {
1933 PyErr_Clear();
1934 break;
1935 }
1936 }
1937 if (list != NULL)
1938 PyModule_AddObject(m, "features", list);
1939 }
1940 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001941#endif
Fred Drake6f987622000-08-25 18:03:30 +00001942
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001943#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001944 PyModule_AddStringConstant(errors_module, #name, \
1945 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001946
Fred Drake0582df92000-07-12 04:49:00 +00001947 MYCONST(XML_ERROR_NO_MEMORY);
1948 MYCONST(XML_ERROR_SYNTAX);
1949 MYCONST(XML_ERROR_NO_ELEMENTS);
1950 MYCONST(XML_ERROR_INVALID_TOKEN);
1951 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1952 MYCONST(XML_ERROR_PARTIAL_CHAR);
1953 MYCONST(XML_ERROR_TAG_MISMATCH);
1954 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1955 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1956 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1957 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1958 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1959 MYCONST(XML_ERROR_ASYNC_ENTITY);
1960 MYCONST(XML_ERROR_BAD_CHAR_REF);
1961 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1962 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1963 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1964 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1965 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001966 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1967 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1968 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001969 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1970 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1971 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1972 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1973 /* Added in Expat 1.95.7. */
1974 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1975 /* Added in Expat 1.95.8. */
1976 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1977 MYCONST(XML_ERROR_INCOMPLETE_PE);
1978 MYCONST(XML_ERROR_XML_DECL);
1979 MYCONST(XML_ERROR_TEXT_DECL);
1980 MYCONST(XML_ERROR_PUBLICID);
1981 MYCONST(XML_ERROR_SUSPENDED);
1982 MYCONST(XML_ERROR_NOT_SUSPENDED);
1983 MYCONST(XML_ERROR_ABORTED);
1984 MYCONST(XML_ERROR_FINISHED);
1985 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001986
Fred Drake85d835f2001-02-08 15:39:08 +00001987 PyModule_AddStringConstant(errors_module, "__doc__",
1988 "Constants used to describe error conditions.");
1989
Fred Drake93adb692000-09-23 04:55:48 +00001990#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001991
Fred Drake85d835f2001-02-08 15:39:08 +00001992#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001993 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1994 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1995 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001996#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001997
Fred Drake85d835f2001-02-08 15:39:08 +00001998#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1999 PyModule_AddStringConstant(model_module, "__doc__",
2000 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002001
Fred Drake85d835f2001-02-08 15:39:08 +00002002 MYCONST(XML_CTYPE_EMPTY);
2003 MYCONST(XML_CTYPE_ANY);
2004 MYCONST(XML_CTYPE_MIXED);
2005 MYCONST(XML_CTYPE_NAME);
2006 MYCONST(XML_CTYPE_CHOICE);
2007 MYCONST(XML_CTYPE_SEQ);
2008
2009 MYCONST(XML_CQUANT_NONE);
2010 MYCONST(XML_CQUANT_OPT);
2011 MYCONST(XML_CQUANT_REP);
2012 MYCONST(XML_CQUANT_PLUS);
2013#undef MYCONST
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002014}
2015
Fred Drake6f987622000-08-25 18:03:30 +00002016static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002017clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00002018{
Fred Drakecde79132001-04-25 16:01:30 +00002019 int i = 0;
2020 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002021
Fred Drake71b63ff2002-06-28 22:29:01 +00002022 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002023 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00002024 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002025 else {
Fred Drakecde79132001-04-25 16:01:30 +00002026 temp = self->handlers[i];
2027 self->handlers[i] = NULL;
2028 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002029 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002030 }
Fred Drakecde79132001-04-25 16:01:30 +00002031 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002032}
2033
Tim Peters0c322792002-07-17 16:49:03 +00002034static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002035 {"StartElementHandler",
2036 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002037 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002038 {"EndElementHandler",
2039 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002040 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002041 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002042 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2043 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002044 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002045 (xmlhandlersetter)XML_SetCharacterDataHandler,
2046 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002047 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002048 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002049 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002050 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002051 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002052 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002053 {"StartNamespaceDeclHandler",
2054 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002055 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002056 {"EndNamespaceDeclHandler",
2057 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002058 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002059 {"CommentHandler",
2060 (xmlhandlersetter)XML_SetCommentHandler,
2061 (xmlhandler)my_CommentHandler},
2062 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002063 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002064 (xmlhandler)my_StartCdataSectionHandler},
2065 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002066 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002067 (xmlhandler)my_EndCdataSectionHandler},
2068 {"DefaultHandler",
2069 (xmlhandlersetter)XML_SetDefaultHandler,
2070 (xmlhandler)my_DefaultHandler},
2071 {"DefaultHandlerExpand",
2072 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2073 (xmlhandler)my_DefaultHandlerExpandHandler},
2074 {"NotStandaloneHandler",
2075 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2076 (xmlhandler)my_NotStandaloneHandler},
2077 {"ExternalEntityRefHandler",
2078 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002079 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002080 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002081 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002082 (xmlhandler)my_StartDoctypeDeclHandler},
2083 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002084 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002085 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002086 {"EntityDeclHandler",
2087 (xmlhandlersetter)XML_SetEntityDeclHandler,
2088 (xmlhandler)my_EntityDeclHandler},
2089 {"XmlDeclHandler",
2090 (xmlhandlersetter)XML_SetXmlDeclHandler,
2091 (xmlhandler)my_XmlDeclHandler},
2092 {"ElementDeclHandler",
2093 (xmlhandlersetter)XML_SetElementDeclHandler,
2094 (xmlhandler)my_ElementDeclHandler},
2095 {"AttlistDeclHandler",
2096 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2097 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002098#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002099 {"SkippedEntityHandler",
2100 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2101 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002102#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002103
Fred Drake0582df92000-07-12 04:49:00 +00002104 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002105};