blob: e603984a108c58d28e51cabdd2e9bbc5e62a67f6 [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
Fredrik Lundhc3345042005-12-13 19:49:55 +00007#include "pyexpat.h"
8
Martin v. Löwisc847f402003-01-21 11:09:21 +00009#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
10
Jeremy Hylton9263f572003-06-27 16:13:17 +000011#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000012
Fred Drake0582df92000-07-12 04:49:00 +000013enum HandlerTypes {
14 StartElement,
15 EndElement,
16 ProcessingInstruction,
17 CharacterData,
18 UnparsedEntityDecl,
19 NotationDecl,
20 StartNamespaceDecl,
21 EndNamespaceDecl,
22 Comment,
23 StartCdataSection,
24 EndCdataSection,
25 Default,
26 DefaultHandlerExpand,
27 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000028 ExternalEntityRef,
29 StartDoctypeDecl,
30 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000031 EntityDecl,
32 XmlDecl,
33 ElementDecl,
34 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000035#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000036 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000037#endif
Fred Drake85d835f2001-02-08 15:39:08 +000038 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000039};
40
41static PyObject *ErrorObject;
42
43/* ----------------------------------------------------- */
44
45/* Declarations for objects of type xmlparser */
46
47typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000048 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000049
Fred Drake0582df92000-07-12 04:49:00 +000050 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000051 int ordered_attributes; /* Return attributes as a list. */
52 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000053 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000054 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000055 XML_Char *buffer; /* Buffer used when accumulating characters */
56 /* NULL if not enabled */
57 int buffer_size; /* Size of buffer, in XML_Char units */
58 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000059 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000060 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000061} xmlparseobject;
62
Fred Drake2a3d7db2002-06-28 22:56:48 +000063#define CHARACTER_DATA_BUFFER_SIZE 8192
64
Jeremy Hylton938ace62002-07-17 16:30:39 +000065static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000066
Fred Drake117ac852002-09-24 16:24:54 +000067typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000068typedef void* xmlhandler;
69
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000070struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000071 const char *name;
72 xmlhandlersetter setter;
73 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000074 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000075 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076};
77
Jeremy Hylton938ace62002-07-17 16:30:39 +000078static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000079
Fred Drakebd6101c2001-02-14 18:29:45 +000080/* Set an integer attribute on the error object; return true on success,
81 * false on an exception.
82 */
83static int
84set_error_attr(PyObject *err, char *name, int value)
85{
Christian Heimes217cfd12007-12-02 14:31:20 +000086 PyObject *v = PyLong_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000087
Neal Norwitz2f5e9902006-03-08 06:36:45 +000088 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
89 Py_XDECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +000090 return 0;
91 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +000092 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +000093 return 1;
94}
95
96/* Build and set an Expat exception, including positioning
97 * information. Always returns NULL.
98 */
Fred Drake85d835f2001-02-08 15:39:08 +000099static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000100set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000101{
102 PyObject *err;
103 char buffer[256];
104 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000105 int lineno = XML_GetErrorLineNumber(parser);
106 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000107
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000108 /* There is no risk of overflowing this buffer, since
109 even for 64-bit integers, there is sufficient space. */
110 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000111 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000112 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 if ( err != NULL
114 && set_error_attr(err, "code", code)
115 && set_error_attr(err, "offset", column)
116 && set_error_attr(err, "lineno", lineno)) {
117 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000118 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000119 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000120 return NULL;
121}
122
Fred Drake71b63ff2002-06-28 22:29:01 +0000123static int
124have_handler(xmlparseobject *self, int type)
125{
126 PyObject *handler = self->handlers[type];
127 return handler != NULL;
128}
129
130static PyObject *
131get_handler_name(struct HandlerInfo *hinfo)
132{
133 PyObject *name = hinfo->nameobj;
134 if (name == NULL) {
Neal Norwitz392c5be2007-08-25 17:20:32 +0000135 name = PyUnicode_FromString(hinfo->name);
Fred Drake71b63ff2002-06-28 22:29:01 +0000136 hinfo->nameobj = name;
137 }
138 Py_XINCREF(name);
139 return name;
140}
141
Fred Drake85d835f2001-02-08 15:39:08 +0000142
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000143/* Convert a string of XML_Chars into a Unicode string.
144 Returns None if str is a null pointer. */
145
Fred Drake0582df92000-07-12 04:49:00 +0000146static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000147conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000148{
Fred Drake71b63ff2002-06-28 22:29:01 +0000149 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000150 and hence in UTF-8. */
151 /* UTF-8 from Expat, Unicode desired */
152 if (str == NULL) {
153 Py_INCREF(Py_None);
154 return Py_None;
155 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000156 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000157}
158
Fred Drake0582df92000-07-12 04:49:00 +0000159static PyObject *
160conv_string_len_to_unicode(const XML_Char *str, int len)
161{
Fred Drake71b63ff2002-06-28 22:29:01 +0000162 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000163 and hence in UTF-8. */
164 /* UTF-8 from Expat, Unicode desired */
165 if (str == NULL) {
166 Py_INCREF(Py_None);
167 return Py_None;
168 }
Fred Drake6f987622000-08-25 18:03:30 +0000169 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000170}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000172/* Callback routines */
173
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000174static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000175
Martin v. Löwis069dde22003-01-21 10:58:18 +0000176/* This handler is used when an error has been detected, in the hope
177 that actual parsing can be terminated early. This will only help
178 if an external entity reference is encountered. */
179static int
180error_external_entity_ref_handler(XML_Parser parser,
181 const XML_Char *context,
182 const XML_Char *base,
183 const XML_Char *systemId,
184 const XML_Char *publicId)
185{
186 return 0;
187}
188
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189/* Dummy character data handler used when an error (exception) has
190 been detected, and the actual parsing can be terminated early.
191 This is needed since character data handler can't be safely removed
192 from within the character data handler, but can be replaced. It is
193 used only from the character data handler trampoline, and must be
194 used right after `flag_error()` is called. */
195static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196noop_character_data_handler(void *userData, const XML_Char *data, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000197{
198 /* Do nothing. */
199}
200
Fred Drake6f987622000-08-25 18:03:30 +0000201static void
202flag_error(xmlparseobject *self)
203{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000204 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000205 XML_SetExternalEntityRefHandler(self->itself,
206 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000207}
208
209static PyCodeObject*
210getcode(enum HandlerTypes slot, char* func_name, int lineno)
211{
Fred Drakebd6101c2001-02-14 18:29:45 +0000212 if (handler_info[slot].tb_code == NULL) {
Fred Drakebd6101c2001-02-14 18:29:45 +0000213 handler_info[slot].tb_code =
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000214 PyCode_NewEmpty(__FILE__, func_name, lineno);
Fred Drakebd6101c2001-02-14 18:29:45 +0000215 }
216 return handler_info[slot].tb_code;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000217}
218
Jeremy Hylton9263f572003-06-27 16:13:17 +0000219#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000220static int
221trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
222{
223 int result = 0;
224 if (!tstate->use_tracing || tstate->tracing)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return 0;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000226 if (tstate->c_profilefunc != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 tstate->tracing++;
228 result = tstate->c_profilefunc(tstate->c_profileobj,
229 f, code , val);
230 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
231 || (tstate->c_profilefunc != NULL));
232 tstate->tracing--;
233 if (result)
234 return result;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000235 }
236 if (tstate->c_tracefunc != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 tstate->tracing++;
238 result = tstate->c_tracefunc(tstate->c_traceobj,
239 f, code , val);
240 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
241 || (tstate->c_profilefunc != NULL));
242 tstate->tracing--;
243 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000244 return result;
245}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000246
247static int
248trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
249{
250 PyObject *type, *value, *traceback, *arg;
251 int err;
252
253 if (tstate->c_tracefunc == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 return 0;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000255
256 PyErr_Fetch(&type, &value, &traceback);
257 if (value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 value = Py_None;
259 Py_INCREF(value);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000260 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000261 arg = PyTuple_Pack(3, type, value, traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000262 if (arg == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyErr_Restore(type, value, traceback);
264 return 0;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000265 }
266 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
267 Py_DECREF(arg);
268 if (err == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyErr_Restore(type, value, traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000270 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 Py_XDECREF(type);
272 Py_XDECREF(value);
273 Py_XDECREF(traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000274 }
275 return err;
276}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000277#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000278
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000279static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000280call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
281 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000282{
Fred Drakebd6101c2001-02-14 18:29:45 +0000283 PyThreadState *tstate = PyThreadState_GET();
284 PyFrameObject *f;
285 PyObject *res;
286
287 if (c == NULL)
288 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289
Jeremy Hylton9263f572003-06-27 16:13:17 +0000290 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000291 if (f == NULL)
292 return NULL;
293 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000294#ifdef FIX_TRACE
295 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000297 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000298#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000299 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000300 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (tstate->curexc_traceback == NULL)
302 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000303 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000304#ifdef FIX_TRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (trace_frame_exc(tstate, f) < 0) {
306 return NULL;
307 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000308 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000309 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
311 Py_XDECREF(res);
312 res = NULL;
313 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000314 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000315#else
316 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000317#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000318 tstate->frame = f->f_back;
319 Py_DECREF(f);
320 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000321}
322
Fred Drakeb91a36b2002-06-27 19:40:48 +0000323static PyObject*
324string_intern(xmlparseobject *self, const char* str)
325{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000326 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000327 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000328 /* result can be NULL if the unicode conversion failed. */
329 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000331 if (!self->intern)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000333 value = PyDict_GetItem(self->intern, result);
334 if (!value) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (PyDict_SetItem(self->intern, result, result) == 0)
Fred Drakeb91a36b2002-06-27 19:40:48 +0000336 return result;
337 else
338 return NULL;
339 }
340 Py_INCREF(value);
341 Py_DECREF(result);
342 return value;
343}
344
Fred Drake2a3d7db2002-06-28 22:56:48 +0000345/* Return 0 on success, -1 on exception.
346 * flag_error() will be called before return if needed.
347 */
348static int
349call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
350{
351 PyObject *args;
352 PyObject *temp;
353
354 args = PyTuple_New(1);
355 if (args == NULL)
356 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000357 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000358 if (temp == NULL) {
359 Py_DECREF(args);
360 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361 XML_SetCharacterDataHandler(self->itself,
362 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000363 return -1;
364 }
365 PyTuple_SET_ITEM(args, 0, temp);
366 /* temp is now a borrowed reference; consider it unused. */
367 self->in_callback = 1;
368 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000369 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000370 /* temp is an owned reference again, or NULL */
371 self->in_callback = 0;
372 Py_DECREF(args);
373 if (temp == NULL) {
374 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000375 XML_SetCharacterDataHandler(self->itself,
376 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000377 return -1;
378 }
379 Py_DECREF(temp);
380 return 0;
381}
382
383static int
384flush_character_buffer(xmlparseobject *self)
385{
386 int rc;
387 if (self->buffer == NULL || self->buffer_used == 0)
388 return 0;
389 rc = call_character_handler(self, self->buffer, self->buffer_used);
390 self->buffer_used = 0;
391 return rc;
392}
393
394static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
Fred Drake2a3d7db2002-06-28 22:56:48 +0000396{
397 xmlparseobject *self = (xmlparseobject *) userData;
398 if (self->buffer == NULL)
399 call_character_handler(self, data, len);
400 else {
401 if ((self->buffer_used + len) > self->buffer_size) {
402 if (flush_character_buffer(self) < 0)
403 return;
404 /* handler might have changed; drop the rest on the floor
405 * if there isn't a handler anymore
406 */
407 if (!have_handler(self, CharacterData))
408 return;
409 }
410 if (len > self->buffer_size) {
411 call_character_handler(self, data, len);
412 self->buffer_used = 0;
413 }
414 else {
415 memcpy(self->buffer + self->buffer_used,
416 data, len * sizeof(XML_Char));
417 self->buffer_used += len;
418 }
419 }
420}
421
Fred Drake85d835f2001-02-08 15:39:08 +0000422static void
423my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000424 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000425{
426 xmlparseobject *self = (xmlparseobject *)userData;
427
Fred Drake71b63ff2002-06-28 22:29:01 +0000428 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000429 PyObject *container, *rv, *args;
430 int i, max;
431
Fred Drake2a3d7db2002-06-28 22:56:48 +0000432 if (flush_character_buffer(self) < 0)
433 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000434 /* Set max to the number of slots filled in atts[]; max/2 is
435 * the number of attributes we need to process.
436 */
437 if (self->specified_attributes) {
438 max = XML_GetSpecifiedAttributeCount(self->itself);
439 }
440 else {
441 max = 0;
442 while (atts[max] != NULL)
443 max += 2;
444 }
445 /* Build the container. */
446 if (self->ordered_attributes)
447 container = PyList_New(max);
448 else
449 container = PyDict_New();
450 if (container == NULL) {
451 flag_error(self);
452 return;
453 }
454 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000455 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000456 PyObject *v;
457 if (n == NULL) {
458 flag_error(self);
459 Py_DECREF(container);
460 return;
461 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000462 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000463 if (v == NULL) {
464 flag_error(self);
465 Py_DECREF(container);
466 Py_DECREF(n);
467 return;
468 }
469 if (self->ordered_attributes) {
470 PyList_SET_ITEM(container, i, n);
471 PyList_SET_ITEM(container, i+1, v);
472 }
473 else if (PyDict_SetItem(container, n, v)) {
474 flag_error(self);
475 Py_DECREF(n);
476 Py_DECREF(v);
477 return;
478 }
479 else {
480 Py_DECREF(n);
481 Py_DECREF(v);
482 }
483 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000484 args = string_intern(self, name);
485 if (args != NULL)
486 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000487 if (args == NULL) {
488 Py_DECREF(container);
489 return;
490 }
491 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000492 self->in_callback = 1;
493 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000494 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000495 self->in_callback = 0;
496 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000497 if (rv == NULL) {
498 flag_error(self);
499 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000500 }
Fred Drake85d835f2001-02-08 15:39:08 +0000501 Py_DECREF(rv);
502 }
503}
504
505#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
506 RETURN, GETUSERDATA) \
507static RC \
508my_##NAME##Handler PARAMS {\
509 xmlparseobject *self = GETUSERDATA ; \
510 PyObject *args = NULL; \
511 PyObject *rv = NULL; \
512 INIT \
513\
Fred Drake71b63ff2002-06-28 22:29:01 +0000514 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000515 if (flush_character_buffer(self) < 0) \
516 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000517 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000518 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000519 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000520 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000521 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000522 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000523 Py_DECREF(args); \
524 if (rv == NULL) { \
525 flag_error(self); \
526 return RETURN; \
527 } \
528 CONVERSION \
529 Py_DECREF(rv); \
530 } \
531 return RETURN; \
532}
533
Fred Drake6f987622000-08-25 18:03:30 +0000534#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
536 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000537
Fred Drake6f987622000-08-25 18:03:30 +0000538#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
540 rc = PyLong_AsLong(rv);, rc, \
541 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000542
Fred Drake71b63ff2002-06-28 22:29:01 +0000543VOID_HANDLER(EndElement,
544 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000545 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000546
Fred Drake6f987622000-08-25 18:03:30 +0000547VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000548 (void *userData,
549 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000550 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000551 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000552
Fred Drake6f987622000-08-25 18:03:30 +0000553VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000554 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000555 const XML_Char *entityName,
556 const XML_Char *base,
557 const XML_Char *systemId,
558 const XML_Char *publicId,
559 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000560 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000561 string_intern(self, entityName), string_intern(self, base),
562 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000563 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000564
Fred Drake85d835f2001-02-08 15:39:08 +0000565VOID_HANDLER(EntityDecl,
566 (void *userData,
567 const XML_Char *entityName,
568 int is_parameter_entity,
569 const XML_Char *value,
570 int value_length,
571 const XML_Char *base,
572 const XML_Char *systemId,
573 const XML_Char *publicId,
574 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000575 ("NiNNNNN",
576 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000577 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000578 string_intern(self, base), string_intern(self, systemId),
579 string_intern(self, publicId),
580 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000581
582VOID_HANDLER(XmlDecl,
583 (void *userData,
584 const XML_Char *version,
585 const XML_Char *encoding,
586 int standalone),
587 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000588 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000589 standalone))
590
591static PyObject *
592conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000593 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000594{
595 PyObject *result = NULL;
596 PyObject *children = PyTuple_New(model->numchildren);
597 int i;
598
599 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000600 assert(model->numchildren < INT_MAX);
601 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000602 PyObject *child = conv_content_model(&model->children[i],
603 conv_string);
604 if (child == NULL) {
605 Py_XDECREF(children);
606 return NULL;
607 }
608 PyTuple_SET_ITEM(children, i, child);
609 }
610 result = Py_BuildValue("(iiO&N)",
611 model->type, model->quant,
612 conv_string,model->name, children);
613 }
614 return result;
615}
616
Fred Drake06dd8cf2003-02-02 03:54:17 +0000617static void
618my_ElementDeclHandler(void *userData,
619 const XML_Char *name,
620 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000621{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000622 xmlparseobject *self = (xmlparseobject *)userData;
623 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000624
Fred Drake06dd8cf2003-02-02 03:54:17 +0000625 if (have_handler(self, ElementDecl)) {
626 PyObject *rv = NULL;
627 PyObject *modelobj, *nameobj;
628
629 if (flush_character_buffer(self) < 0)
630 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000631 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000632 if (modelobj == NULL) {
633 flag_error(self);
634 goto finally;
635 }
636 nameobj = string_intern(self, name);
637 if (nameobj == NULL) {
638 Py_DECREF(modelobj);
639 flag_error(self);
640 goto finally;
641 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000642 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000643 if (args == NULL) {
644 Py_DECREF(modelobj);
645 flag_error(self);
646 goto finally;
647 }
648 self->in_callback = 1;
649 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000650 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000651 self->in_callback = 0;
652 if (rv == NULL) {
653 flag_error(self);
654 goto finally;
655 }
656 Py_DECREF(rv);
657 }
658 finally:
659 Py_XDECREF(args);
660 XML_FreeContentModel(self->itself, model);
661 return;
662}
Fred Drake85d835f2001-02-08 15:39:08 +0000663
664VOID_HANDLER(AttlistDecl,
665 (void *userData,
666 const XML_Char *elname,
667 const XML_Char *attname,
668 const XML_Char *att_type,
669 const XML_Char *dflt,
670 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000671 ("(NNO&O&i)",
672 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000673 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000674 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000675
Martin v. Löwisc847f402003-01-21 11:09:21 +0000676#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000677VOID_HANDLER(SkippedEntity,
678 (void *userData,
679 const XML_Char *entityName,
680 int is_parameter_entity),
681 ("Ni",
682 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000683#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000684
Fred Drake71b63ff2002-06-28 22:29:01 +0000685VOID_HANDLER(NotationDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 (void *userData,
687 const XML_Char *notationName,
688 const XML_Char *base,
689 const XML_Char *systemId,
690 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000691 ("(NNNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 string_intern(self, notationName), string_intern(self, base),
693 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000694
Fred Drake6f987622000-08-25 18:03:30 +0000695VOID_HANDLER(StartNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 (void *userData,
697 const XML_Char *prefix,
698 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000699 ("(NN)",
700 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000701
Fred Drake6f987622000-08-25 18:03:30 +0000702VOID_HANDLER(EndNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 (void *userData,
704 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000705 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000706
Fred Drake6f987622000-08-25 18:03:30 +0000707VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000708 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000709 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000710
Fred Drake6f987622000-08-25 18:03:30 +0000711VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000712 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000714
Fred Drake6f987622000-08-25 18:03:30 +0000715VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000716 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000718
Fred Drake6f987622000-08-25 18:03:30 +0000719VOID_HANDLER(Default,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 (void *userData, const XML_Char *s, int len),
721 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000722
Fred Drake6f987622000-08-25 18:03:30 +0000723VOID_HANDLER(DefaultHandlerExpand,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 (void *userData, const XML_Char *s, int len),
725 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000726
Fred Drake71b63ff2002-06-28 22:29:01 +0000727INT_HANDLER(NotStandalone,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 (void *userData),
729 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000730
Fred Drake6f987622000-08-25 18:03:30 +0000731RC_HANDLER(int, ExternalEntityRef,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 (XML_Parser parser,
733 const XML_Char *context,
734 const XML_Char *base,
735 const XML_Char *systemId,
736 const XML_Char *publicId),
737 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000738 ("(O&NNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 conv_string_to_unicode ,context, string_intern(self, base),
740 string_intern(self, systemId), string_intern(self, publicId)),
741 rc = PyLong_AsLong(rv);, rc,
742 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000743
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000744/* XXX UnknownEncodingHandler */
745
Fred Drake85d835f2001-02-08 15:39:08 +0000746VOID_HANDLER(StartDoctypeDecl,
747 (void *userData, const XML_Char *doctypeName,
748 const XML_Char *sysid, const XML_Char *pubid,
749 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000750 ("(NNNi)", string_intern(self, doctypeName),
751 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000752 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000753
754VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000755
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756/* ---------------------------------------------------------------- */
757
Fred Drake71b63ff2002-06-28 22:29:01 +0000758static PyObject *
759get_parse_result(xmlparseobject *self, int rv)
760{
761 if (PyErr_Occurred()) {
762 return NULL;
763 }
764 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000765 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000766 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000767 if (flush_character_buffer(self) < 0) {
768 return NULL;
769 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000770 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000771}
772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000773PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000774"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776
777static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000778xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000779{
Fred Drake0582df92000-07-12 04:49:00 +0000780 char *s;
781 int slen;
782 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783
Fred Drake0582df92000-07-12 04:49:00 +0000784 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
785 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000786
787 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000788}
789
Fred Drakeca1f4262000-09-21 20:10:23 +0000790/* File reading copied from cPickle */
791
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000792#define BUF_SIZE 2048
793
Fred Drake0582df92000-07-12 04:49:00 +0000794static int
795readinst(char *buf, int buf_size, PyObject *meth)
796{
797 PyObject *arg = NULL;
798 PyObject *bytes = NULL;
799 PyObject *str = NULL;
800 int len = -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000801 char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000802
Christian Heimes217cfd12007-12-02 14:31:20 +0000803 if ((bytes = PyLong_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000804 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000805
Fred Drake7b6caff2003-07-21 17:05:56 +0000806 if ((arg = PyTuple_New(1)) == NULL) {
807 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000808 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000809 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000810
Tim Peters954eef72000-09-22 06:01:11 +0000811 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000812
Martin v. Löwis9171f022004-10-13 19:50:11 +0000813 str = PyObject_Call(meth, arg, NULL);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000814 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000815 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000816
Christian Heimes72b710a2008-05-26 13:28:38 +0000817 if (PyBytes_Check(str))
818 ptr = PyBytes_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000819 else if (PyByteArray_Check(str))
820 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000821 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000822 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000823 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000824 Py_TYPE(str)->tp_name);
Fred Drake0582df92000-07-12 04:49:00 +0000825 goto finally;
826 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000827 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000828 if (len > buf_size) {
829 PyErr_Format(PyExc_ValueError,
830 "read() returned too much data: "
831 "%i bytes requested, %i returned",
832 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000833 goto finally;
834 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000835 memcpy(buf, ptr, len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836finally:
Fred Drake0582df92000-07-12 04:49:00 +0000837 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000838 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000839 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840}
841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000842PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000843"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000844Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000845
846static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000847xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848{
Fred Drake0582df92000-07-12 04:49:00 +0000849 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000850 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000851
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000852
853 readmethod = PyObject_GetAttrString(f, "read");
854 if (readmethod == NULL) {
855 PyErr_Clear();
856 PyErr_SetString(PyExc_TypeError,
857 "argument must have 'read' attribute");
858 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000859 }
860 for (;;) {
861 int bytes_read;
862 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000863 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000864 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000865 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000866 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000867
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000868 bytes_read = readinst(buf, BUF_SIZE, readmethod);
869 if (bytes_read < 0) {
870 Py_DECREF(readmethod);
871 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000872 }
873 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000874 if (PyErr_Occurred()) {
875 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000876 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000877 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878
Fred Drake0582df92000-07-12 04:49:00 +0000879 if (!rv || bytes_read == 0)
880 break;
881 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000882 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000883 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000884}
885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000886PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000887"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000889
890static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000891xmlparse_SetBase(xmlparseobject *self, PyObject *args)
892{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000893 char *base;
894
Fred Drake0582df92000-07-12 04:49:00 +0000895 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000897 if (!XML_SetBase(self->itself, base)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000899 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000900 Py_INCREF(Py_None);
901 return Py_None;
902}
903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000904PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000905"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000907
908static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000909xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
Fred Drake0582df92000-07-12 04:49:00 +0000910{
Fred Drake0582df92000-07-12 04:49:00 +0000911 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000912}
913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000915"GetInputContext() -> string\n\
916Return the untranslated text of the input that caused the current event.\n\
917If 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 +0000918for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000919
920static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000921xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
Fred Drakebd6101c2001-02-14 18:29:45 +0000922{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000923 if (self->in_callback) {
924 int offset, size;
925 const char *buffer
926 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000927
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000928 if (buffer != NULL)
Christian Heimes72b710a2008-05-26 13:28:38 +0000929 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000930 size - offset);
931 else
932 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000933 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000934 else
935 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000936}
Fred Drakebd6101c2001-02-14 18:29:45 +0000937
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000938PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000939"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000940Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000941information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000942
943static PyObject *
944xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
945{
946 char *context;
947 char *encoding = NULL;
948 xmlparseobject *new_parser;
949 int i;
950
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000951 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000952 &context, &encoding)) {
953 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000954 }
955
Martin v. Löwis894258c2001-09-23 10:20:10 +0000956 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake85d835f2001-02-08 15:39:08 +0000957 if (new_parser == NULL)
958 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000959 new_parser->buffer_size = self->buffer_size;
960 new_parser->buffer_used = 0;
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000961 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000962 new_parser->ordered_attributes = self->ordered_attributes;
963 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000964 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +0000965 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000966 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 encoding);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000968 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000969 new_parser->intern = self->intern;
970 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000971 PyObject_GC_Track(new_parser);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000972
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000973 if (self->buffer != NULL) {
974 new_parser->buffer = malloc(new_parser->buffer_size);
975 if (new_parser->buffer == NULL) {
976 Py_DECREF(new_parser);
977 return PyErr_NoMemory();
978 }
979 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000980 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000981 Py_DECREF(new_parser);
982 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000983 }
984
985 XML_SetUserData(new_parser->itself, (void *)new_parser);
986
987 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +0000988 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000989 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000990
Fred Drake2a3d7db2002-06-28 22:56:48 +0000991 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000992 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000993 Py_DECREF(new_parser);
994 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000995 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000996 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000997
998 /* then copy handlers from self */
999 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001000 PyObject *handler = self->handlers[i];
1001 if (handler != NULL) {
1002 Py_INCREF(handler);
1003 new_parser->handlers[i] = handler;
1004 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001005 handler_info[i].handler);
1006 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001007 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001008 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001009}
1010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001012"SetParamEntityParsing(flag) -> success\n\
1013Controls parsing of parameter entities (including the external DTD\n\
1014subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1015XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1016XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001018
1019static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001020xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001021{
Fred Drake85d835f2001-02-08 15:39:08 +00001022 int flag;
1023 if (!PyArg_ParseTuple(args, "i", &flag))
1024 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001025 flag = XML_SetParamEntityParsing(p->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001026 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001027}
1028
Martin v. Löwisc847f402003-01-21 11:09:21 +00001029
1030#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001031PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1032"UseForeignDTD([flag])\n\
1033Allows the application to provide an artificial external subset if one is\n\
1034not specified as part of the document instance. This readily allows the\n\
1035use of a 'default' document type controlled by the application, while still\n\
1036getting the advantage of providing document type information to the parser.\n\
1037'flag' defaults to True if not provided.");
1038
1039static PyObject *
1040xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1041{
1042 PyObject *flagobj = NULL;
1043 XML_Bool flag = XML_TRUE;
1044 enum XML_Error rc;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001045 if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
Martin v. Löwis069dde22003-01-21 10:58:18 +00001046 return NULL;
1047 if (flagobj != NULL)
1048 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1049 rc = XML_UseForeignDTD(self->itself, flag);
1050 if (rc != XML_ERROR_NONE) {
1051 return set_error(self, rc);
1052 }
1053 Py_INCREF(Py_None);
1054 return Py_None;
1055}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001056#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001057
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001058static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs);
1059
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001060static struct PyMethodDef xmlparse_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 {"Parse", (PyCFunction)xmlparse_Parse,
1062 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001063 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 METH_O, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001065 {"SetBase", (PyCFunction)xmlparse_SetBase,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001067 {"GetBase", (PyCFunction)xmlparse_GetBase,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 METH_NOARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001069 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001071 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001073 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 METH_NOARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001075#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001076 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001078#endif
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001079 {"__dir__", xmlparse_dir, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001081};
1082
1083/* ---------- */
1084
1085
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001086
Fred Drake71b63ff2002-06-28 22:29:01 +00001087/* pyexpat international encoding support.
1088 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001089*/
1090
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001091static char template_buffer[257];
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092
Fred Drake71b63ff2002-06-28 22:29:01 +00001093static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001094init_template_buffer(void)
1095{
1096 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001097 for (i = 0; i < 256; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001099 }
Fred Drakebb66a202001-03-01 20:48:17 +00001100 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001101}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102
Fred Drake71b63ff2002-06-28 22:29:01 +00001103static int
1104PyUnknownEncodingHandler(void *encodingHandlerData,
1105 const XML_Char *name,
1106 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107{
Fred Drakebb66a202001-03-01 20:48:17 +00001108 PyUnicodeObject *_u_string = NULL;
1109 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001111
Fred Drakebb66a202001-03-01 20:48:17 +00001112 /* Yes, supports only 8bit encodings */
1113 _u_string = (PyUnicodeObject *)
1114 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001115
Fred Drakebb66a202001-03-01 20:48:17 +00001116 if (_u_string == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001118
Fred Drakebb66a202001-03-01 20:48:17 +00001119 for (i = 0; i < 256; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Stupid to access directly, but fast */
1121 Py_UNICODE c = _u_string->str[i];
1122 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
1123 info->map[i] = -1;
1124 else
1125 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001126 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001127 info->data = NULL;
1128 info->convert = NULL;
1129 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001130 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001131 Py_DECREF(_u_string);
1132 return result;
1133}
1134
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001135
1136static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001137newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001138{
1139 int i;
1140 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001141
Martin v. Löwis894258c2001-09-23 10:20:10 +00001142 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake0582df92000-07-12 04:49:00 +00001143 if (self == NULL)
1144 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001145
Fred Drake2a3d7db2002-06-28 22:56:48 +00001146 self->buffer = NULL;
1147 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1148 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001149 self->ordered_attributes = 0;
1150 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001151 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001152 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001153 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001154 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001155 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1156 }
Fred Drake85d835f2001-02-08 15:39:08 +00001157 else {
Fred Drake0582df92000-07-12 04:49:00 +00001158 self->itself = XML_ParserCreate(encoding);
1159 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001160 self->intern = intern;
1161 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001162 PyObject_GC_Track(self);
Fred Drake0582df92000-07-12 04:49:00 +00001163 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001164 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001165 "XML_ParserCreate failed");
1166 Py_DECREF(self);
1167 return NULL;
1168 }
1169 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001170 XML_SetUnknownEncodingHandler(self->itself,
1171 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001172
Fred Drake2a3d7db2002-06-28 22:56:48 +00001173 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001174 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001175
Fred Drake7c75bf22002-07-01 14:02:31 +00001176 self->handlers = malloc(sizeof(PyObject *) * i);
1177 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001178 Py_DECREF(self);
1179 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001181 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001182
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001184}
1185
1186
1187static void
Fred Drake0582df92000-07-12 04:49:00 +00001188xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189{
Fred Drake0582df92000-07-12 04:49:00 +00001190 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001191 PyObject_GC_UnTrack(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001192 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001193 XML_ParserFree(self->itself);
1194 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001195
Fred Drake85d835f2001-02-08 15:39:08 +00001196 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001197 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001198 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001199 temp = self->handlers[i];
1200 self->handlers[i] = NULL;
1201 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001202 }
1203 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001204 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001205 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001206 if (self->buffer != NULL) {
1207 free(self->buffer);
1208 self->buffer = NULL;
1209 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001210 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001211 PyObject_GC_Del(self);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001212}
1213
Fred Drake0582df92000-07-12 04:49:00 +00001214static int
1215handlername2int(const char *name)
1216{
1217 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001218 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001219 if (strcmp(name, handler_info[i].name) == 0) {
1220 return i;
1221 }
1222 }
1223 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001224}
1225
1226static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001227get_pybool(int istrue)
1228{
1229 PyObject *result = istrue ? Py_True : Py_False;
1230 Py_INCREF(result);
1231 return result;
1232}
1233
1234static PyObject *
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001235xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001236{
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001237 char *name = "";
1238 int handlernum = -1;
1239
1240 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 name = _PyUnicode_AsString(nameobj);
1242
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001243 handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001244
1245 if (handlernum != -1) {
1246 PyObject *result = self->handlers[handlernum];
1247 if (result == NULL)
1248 result = Py_None;
1249 Py_INCREF(result);
1250 return result;
1251 }
1252 if (name[0] == 'E') {
1253 if (strcmp(name, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001254 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001255 XML_GetErrorCode(self->itself));
1256 if (strcmp(name, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001257 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001258 XML_GetErrorLineNumber(self->itself));
1259 if (strcmp(name, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001260 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001261 XML_GetErrorColumnNumber(self->itself));
1262 if (strcmp(name, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001263 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001264 XML_GetErrorByteIndex(self->itself));
1265 }
Dave Cole3203efb2004-08-26 00:37:31 +00001266 if (name[0] == 'C') {
1267 if (strcmp(name, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001268 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001269 XML_GetCurrentLineNumber(self->itself));
1270 if (strcmp(name, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001271 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001272 XML_GetCurrentColumnNumber(self->itself));
1273 if (strcmp(name, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001274 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001275 XML_GetCurrentByteIndex(self->itself));
1276 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001277 if (name[0] == 'b') {
1278 if (strcmp(name, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001279 return PyLong_FromLong((long) self->buffer_size);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001280 if (strcmp(name, "buffer_text") == 0)
1281 return get_pybool(self->buffer != NULL);
1282 if (strcmp(name, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001283 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001284 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001285 if (strcmp(name, "namespace_prefixes") == 0)
1286 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001287 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001288 return get_pybool(self->ordered_attributes);
Fred Drake85d835f2001-02-08 15:39:08 +00001289 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001290 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001291 if (strcmp(name, "intern") == 0) {
1292 if (self->intern == NULL) {
1293 Py_INCREF(Py_None);
1294 return Py_None;
1295 }
1296 else {
1297 Py_INCREF(self->intern);
1298 return self->intern;
1299 }
1300 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001301
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001302 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001303}
1304
1305static PyObject *
1306xmlparse_dir(PyObject *self, PyObject* noargs)
1307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308#define APPEND(list, str) \
1309 do { \
1310 PyObject *o = PyUnicode_FromString(str); \
1311 if (o != NULL) \
1312 PyList_Append(list, o); \
1313 Py_XDECREF(o); \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001314 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001315
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001316 int i;
1317 PyObject *rc = PyList_New(0);
1318 if (!rc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 return NULL;
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001320 for (i = 0; handler_info[i].name != NULL; i++) {
1321 PyObject *o = get_handler_name(&handler_info[i]);
1322 if (o != NULL)
1323 PyList_Append(rc, o);
1324 Py_XDECREF(o);
1325 }
1326 APPEND(rc, "ErrorCode");
1327 APPEND(rc, "ErrorLineNumber");
1328 APPEND(rc, "ErrorColumnNumber");
1329 APPEND(rc, "ErrorByteIndex");
1330 APPEND(rc, "CurrentLineNumber");
1331 APPEND(rc, "CurrentColumnNumber");
1332 APPEND(rc, "CurrentByteIndex");
1333 APPEND(rc, "buffer_size");
1334 APPEND(rc, "buffer_text");
1335 APPEND(rc, "buffer_used");
1336 APPEND(rc, "namespace_prefixes");
1337 APPEND(rc, "ordered_attributes");
1338 APPEND(rc, "specified_attributes");
1339 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001340
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001341#undef APPEND
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001342
1343 if (PyErr_Occurred()) {
1344 Py_DECREF(rc);
1345 rc = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001346 }
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001347
1348 return rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001349}
1350
Fred Drake6f987622000-08-25 18:03:30 +00001351static int
1352sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001353{
1354 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001355 if (handlernum >= 0) {
1356 xmlhandler c_handler = NULL;
1357 PyObject *temp = self->handlers[handlernum];
1358
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001359 if (v == Py_None) {
1360 /* If this is the character data handler, and a character
1361 data handler is already active, we need to be more
1362 careful. What we can safely do is replace the existing
1363 character data handler callback function with a no-op
1364 function that will refuse to call Python. The downside
1365 is that this doesn't completely remove the character
1366 data handler from the C layer if there's any callback
1367 active, so Expat does a little more work than it
1368 otherwise would, but that's really an odd case. A more
1369 elaborate system of handlers and state could remove the
1370 C handler more effectively. */
1371 if (handlernum == CharacterData && self->in_callback)
1372 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001373 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001374 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001375 else if (v != NULL) {
1376 Py_INCREF(v);
1377 c_handler = handler_info[handlernum].handler;
1378 }
Fred Drake0582df92000-07-12 04:49:00 +00001379 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001380 Py_XDECREF(temp);
1381 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001382 return 1;
1383 }
1384 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385}
1386
1387static int
Fred Drake6f987622000-08-25 18:03:30 +00001388xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001389{
Fred Drake6f987622000-08-25 18:03:30 +00001390 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001391 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001392 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1393 return -1;
1394 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001395 if (strcmp(name, "buffer_text") == 0) {
1396 if (PyObject_IsTrue(v)) {
1397 if (self->buffer == NULL) {
1398 self->buffer = malloc(self->buffer_size);
1399 if (self->buffer == NULL) {
1400 PyErr_NoMemory();
1401 return -1;
1402 }
1403 self->buffer_used = 0;
1404 }
1405 }
1406 else if (self->buffer != NULL) {
1407 if (flush_character_buffer(self) < 0)
1408 return -1;
1409 free(self->buffer);
1410 self->buffer = NULL;
1411 }
1412 return 0;
1413 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001414 if (strcmp(name, "namespace_prefixes") == 0) {
1415 if (PyObject_IsTrue(v))
1416 self->ns_prefixes = 1;
1417 else
1418 self->ns_prefixes = 0;
1419 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1420 return 0;
1421 }
Fred Drake85d835f2001-02-08 15:39:08 +00001422 if (strcmp(name, "ordered_attributes") == 0) {
1423 if (PyObject_IsTrue(v))
1424 self->ordered_attributes = 1;
1425 else
1426 self->ordered_attributes = 0;
1427 return 0;
1428 }
Fred Drake85d835f2001-02-08 15:39:08 +00001429 if (strcmp(name, "specified_attributes") == 0) {
1430 if (PyObject_IsTrue(v))
1431 self->specified_attributes = 1;
1432 else
1433 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001434 return 0;
1435 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001436
1437 if (strcmp(name, "buffer_size") == 0) {
1438 long new_buffer_size;
1439 if (!PyLong_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1441 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001442 }
1443
1444 new_buffer_size=PyLong_AS_LONG(v);
1445 /* trivial case -- no change */
1446 if (new_buffer_size == self->buffer_size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 return 0;
Christian Heimes2380ac72008-01-09 00:17:24 +00001448 }
1449
1450 if (new_buffer_size <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1452 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001453 }
1454
1455 /* check maximum */
1456 if (new_buffer_size > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 char errmsg[100];
1458 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1459 PyErr_SetString(PyExc_ValueError, errmsg);
1460 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001461 }
1462
1463 if (self->buffer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* there is already a buffer */
1465 if (self->buffer_used != 0) {
1466 flush_character_buffer(self);
1467 }
1468 /* free existing buffer */
1469 free(self->buffer);
Christian Heimes2380ac72008-01-09 00:17:24 +00001470 }
1471 self->buffer = malloc(new_buffer_size);
1472 if (self->buffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 PyErr_NoMemory();
1474 return -1;
1475 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001476 self->buffer_size = new_buffer_size;
1477 return 0;
1478 }
1479
Fred Drake2a3d7db2002-06-28 22:56:48 +00001480 if (strcmp(name, "CharacterDataHandler") == 0) {
1481 /* If we're changing the character data handler, flush all
1482 * cached data with the old handler. Not sure there's a
1483 * "right" thing to do, though, but this probably won't
1484 * happen.
1485 */
1486 if (flush_character_buffer(self) < 0)
1487 return -1;
1488 }
Fred Drake6f987622000-08-25 18:03:30 +00001489 if (sethandler(self, name, v)) {
1490 return 0;
1491 }
1492 PyErr_SetString(PyExc_AttributeError, name);
1493 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001494}
1495
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001496static int
1497xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1498{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001499 int i;
1500 for (i = 0; handler_info[i].name != NULL; i++)
1501 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001502 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001503}
1504
1505static int
1506xmlparse_clear(xmlparseobject *op)
1507{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001508 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001509 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001510 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001511}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001513PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001514
1515static PyTypeObject Xmlparsetype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 PyVarObject_HEAD_INIT(NULL, 0)
1517 "pyexpat.xmlparser", /*tp_name*/
1518 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
1519 0, /*tp_itemsize*/
1520 /* methods */
1521 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1522 (printfunc)0, /*tp_print*/
1523 0, /*tp_getattr*/
1524 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1525 0, /*tp_reserved*/
1526 (reprfunc)0, /*tp_repr*/
1527 0, /*tp_as_number*/
1528 0, /*tp_as_sequence*/
1529 0, /*tp_as_mapping*/
1530 (hashfunc)0, /*tp_hash*/
1531 (ternaryfunc)0, /*tp_call*/
1532 (reprfunc)0, /*tp_str*/
1533 (getattrofunc)xmlparse_getattro, /* tp_getattro */
1534 0, /* tp_setattro */
1535 0, /* tp_as_buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 Xmlparsetype__doc__, /* tp_doc - Documentation string */
1538 (traverseproc)xmlparse_traverse, /* tp_traverse */
1539 (inquiry)xmlparse_clear, /* tp_clear */
1540 0, /* tp_richcompare */
1541 0, /* tp_weaklistoffset */
1542 0, /* tp_iter */
1543 0, /* tp_iternext */
1544 xmlparse_methods, /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001545};
1546
1547/* End of code for xmlparser objects */
1548/* -------------------------------------------------------- */
1549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001551"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001553
1554static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001555pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1556{
Fred Drakecde79132001-04-25 16:01:30 +00001557 char *encoding = NULL;
1558 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001559 PyObject *intern = NULL;
1560 PyObject *result;
1561 int intern_decref = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001562 static char *kwlist[] = {"encoding", "namespace_separator",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001563 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001564
Fred Drakeb91a36b2002-06-27 19:40:48 +00001565 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1566 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001567 return NULL;
1568 if (namespace_separator != NULL
1569 && strlen(namespace_separator) > 1) {
1570 PyErr_SetString(PyExc_ValueError,
1571 "namespace_separator must be at most one"
1572 " character, omitted, or None");
1573 return NULL;
1574 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001575 /* Explicitly passing None means no interning is desired.
1576 Not passing anything means that a new dictionary is used. */
1577 if (intern == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 intern = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001579 else if (intern == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 intern = PyDict_New();
1581 if (!intern)
1582 return NULL;
1583 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001584 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001585 else if (!PyDict_Check(intern)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1587 return NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001588 }
1589
1590 result = newxmlparseobject(encoding, namespace_separator, intern);
1591 if (intern_decref) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 Py_DECREF(intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001593 }
1594 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001595}
1596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001598"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001600
1601static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001602pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001603{
Fred Drake0582df92000-07-12 04:49:00 +00001604 long code = 0;
1605
1606 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1607 return NULL;
1608 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001609}
1610
1611/* List of methods defined in the module */
1612
1613static struct PyMethodDef pyexpat_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
Fred Drake0582df92000-07-12 04:49:00 +00001615 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1617 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001620};
1621
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001622/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624PyDoc_STRVAR(pyexpat_module_documentation,
1625"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001626
Fred Drake4113b132001-03-24 19:58:26 +00001627/* Return a Python string that represents the version number without the
1628 * extra cruft added by revision control, even if the right options were
1629 * given to the "cvs export" command to make it not include the extra
1630 * cruft.
1631 */
1632static PyObject *
1633get_version_string(void)
1634{
1635 static char *rcsid = "$Revision$";
1636 char *rev = rcsid;
1637 int i = 0;
1638
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001639 while (!isdigit(Py_CHARMASK(*rev)))
Fred Drake4113b132001-03-24 19:58:26 +00001640 ++rev;
1641 while (rev[i] != ' ' && rev[i] != '\0')
1642 ++i;
1643
Neal Norwitz392c5be2007-08-25 17:20:32 +00001644 return PyUnicode_FromStringAndSize(rev, i);
Fred Drake4113b132001-03-24 19:58:26 +00001645}
1646
Fred Drakecde79132001-04-25 16:01:30 +00001647/* Initialization function for the module */
1648
1649#ifndef MODULE_NAME
1650#define MODULE_NAME "pyexpat"
1651#endif
1652
1653#ifndef MODULE_INITFUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001654#define MODULE_INITFUNC PyInit_pyexpat
Fred Drakecde79132001-04-25 16:01:30 +00001655#endif
1656
Martin v. Löwis069dde22003-01-21 10:58:18 +00001657#ifndef PyMODINIT_FUNC
1658# ifdef MS_WINDOWS
1659# define PyMODINIT_FUNC __declspec(dllexport) void
1660# else
1661# define PyMODINIT_FUNC void
1662# endif
1663#endif
1664
Mark Hammond8235ea12002-07-19 06:55:41 +00001665PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001666
Martin v. Löwis1a214512008-06-11 05:26:20 +00001667static struct PyModuleDef pyexpatmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 PyModuleDef_HEAD_INIT,
1669 MODULE_NAME,
1670 pyexpat_module_documentation,
1671 -1,
1672 pyexpat_methods,
1673 NULL,
1674 NULL,
1675 NULL,
1676 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001677};
1678
Martin v. Löwis069dde22003-01-21 10:58:18 +00001679PyMODINIT_FUNC
1680MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001681{
1682 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001683 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001684 PyObject *errors_module;
1685 PyObject *modelmod_name;
1686 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001687 PyObject *sys_modules;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001688 static struct PyExpat_CAPI capi;
1689 PyObject* capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001690
Fred Drake6f987622000-08-25 18:03:30 +00001691 if (errmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001692 return NULL;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001693 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001694 if (modelmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001695 return NULL;
Fred Drake6f987622000-08-25 18:03:30 +00001696
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001697 if (PyType_Ready(&Xmlparsetype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001699
Fred Drake0582df92000-07-12 04:49:00 +00001700 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001701 m = PyModule_Create(&pyexpatmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001702 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001704
Fred Drake0582df92000-07-12 04:49:00 +00001705 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001706 if (ErrorObject == NULL) {
1707 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001708 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001709 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001710 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001711 }
1712 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001713 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001714 Py_INCREF(ErrorObject);
1715 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001716 Py_INCREF(&Xmlparsetype);
1717 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001718
Fred Drake4113b132001-03-24 19:58:26 +00001719 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001720 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1721 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001722 {
1723 XML_Expat_Version info = XML_ExpatVersionInfo();
1724 PyModule_AddObject(m, "version_info",
1725 Py_BuildValue("(iii)", info.major,
1726 info.minor, info.micro));
1727 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001728 init_template_buffer();
Fred Drake0582df92000-07-12 04:49:00 +00001729 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001730 compiled, this should check and set native_encoding
1731 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001732 */
Fred Drake93adb692000-09-23 04:55:48 +00001733 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001734
Fred Drake85d835f2001-02-08 15:39:08 +00001735 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001736 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001737 errors_module = PyDict_GetItem(d, errmod_name);
1738 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001739 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001740 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001741 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001742 /* gives away the reference to errors_module */
1743 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001744 }
1745 }
Fred Drake6f987622000-08-25 18:03:30 +00001746 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001747 model_module = PyDict_GetItem(d, modelmod_name);
1748 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001749 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001750 if (model_module != NULL) {
1751 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1752 /* gives away the reference to model_module */
1753 PyModule_AddObject(m, "model", model_module);
1754 }
1755 }
1756 Py_DECREF(modelmod_name);
1757 if (errors_module == NULL || model_module == NULL)
1758 /* Don't core dump later! */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001759 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760
Martin v. Löwisc847f402003-01-21 11:09:21 +00001761#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001762 {
1763 const XML_Feature *features = XML_GetFeatureList();
1764 PyObject *list = PyList_New(0);
1765 if (list == NULL)
1766 /* just ignore it */
1767 PyErr_Clear();
1768 else {
1769 int i = 0;
1770 for (; features[i].feature != XML_FEATURE_END; ++i) {
1771 int ok;
1772 PyObject *item = Py_BuildValue("si", features[i].name,
1773 features[i].value);
1774 if (item == NULL) {
1775 Py_DECREF(list);
1776 list = NULL;
1777 break;
1778 }
1779 ok = PyList_Append(list, item);
1780 Py_DECREF(item);
1781 if (ok < 0) {
1782 PyErr_Clear();
1783 break;
1784 }
1785 }
1786 if (list != NULL)
1787 PyModule_AddObject(m, "features", list);
1788 }
1789 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001790#endif
Fred Drake6f987622000-08-25 18:03:30 +00001791
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001792#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001793 PyModule_AddStringConstant(errors_module, #name, \
1794 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001795
Fred Drake0582df92000-07-12 04:49:00 +00001796 MYCONST(XML_ERROR_NO_MEMORY);
1797 MYCONST(XML_ERROR_SYNTAX);
1798 MYCONST(XML_ERROR_NO_ELEMENTS);
1799 MYCONST(XML_ERROR_INVALID_TOKEN);
1800 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1801 MYCONST(XML_ERROR_PARTIAL_CHAR);
1802 MYCONST(XML_ERROR_TAG_MISMATCH);
1803 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1804 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1805 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1806 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1807 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1808 MYCONST(XML_ERROR_ASYNC_ENTITY);
1809 MYCONST(XML_ERROR_BAD_CHAR_REF);
1810 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1811 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1812 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1813 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1814 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001815 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1816 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1817 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001818 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1819 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1820 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1821 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1822 /* Added in Expat 1.95.7. */
1823 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1824 /* Added in Expat 1.95.8. */
1825 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1826 MYCONST(XML_ERROR_INCOMPLETE_PE);
1827 MYCONST(XML_ERROR_XML_DECL);
1828 MYCONST(XML_ERROR_TEXT_DECL);
1829 MYCONST(XML_ERROR_PUBLICID);
1830 MYCONST(XML_ERROR_SUSPENDED);
1831 MYCONST(XML_ERROR_NOT_SUSPENDED);
1832 MYCONST(XML_ERROR_ABORTED);
1833 MYCONST(XML_ERROR_FINISHED);
1834 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001835
Fred Drake85d835f2001-02-08 15:39:08 +00001836 PyModule_AddStringConstant(errors_module, "__doc__",
1837 "Constants used to describe error conditions.");
1838
Fred Drake93adb692000-09-23 04:55:48 +00001839#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001840
Fred Drake85d835f2001-02-08 15:39:08 +00001841#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001842 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1843 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1844 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001845#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001846
Fred Drake85d835f2001-02-08 15:39:08 +00001847#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1848 PyModule_AddStringConstant(model_module, "__doc__",
1849 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001850
Fred Drake85d835f2001-02-08 15:39:08 +00001851 MYCONST(XML_CTYPE_EMPTY);
1852 MYCONST(XML_CTYPE_ANY);
1853 MYCONST(XML_CTYPE_MIXED);
1854 MYCONST(XML_CTYPE_NAME);
1855 MYCONST(XML_CTYPE_CHOICE);
1856 MYCONST(XML_CTYPE_SEQ);
1857
1858 MYCONST(XML_CQUANT_NONE);
1859 MYCONST(XML_CQUANT_OPT);
1860 MYCONST(XML_CQUANT_REP);
1861 MYCONST(XML_CQUANT_PLUS);
1862#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001863
1864 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001865 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001866 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001867 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1868 capi.MINOR_VERSION = XML_MINOR_VERSION;
1869 capi.MICRO_VERSION = XML_MICRO_VERSION;
1870 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001871 capi.GetErrorCode = XML_GetErrorCode;
1872 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1873 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001874 capi.Parse = XML_Parse;
1875 capi.ParserCreate_MM = XML_ParserCreate_MM;
1876 capi.ParserFree = XML_ParserFree;
1877 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1878 capi.SetCommentHandler = XML_SetCommentHandler;
1879 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1880 capi.SetElementHandler = XML_SetElementHandler;
1881 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1882 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1883 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1884 capi.SetUserData = XML_SetUserData;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885
Benjamin Petersonb173f782009-05-05 22:31:58 +00001886 /* export using capsule */
1887 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001888 if (capi_object)
1889 PyModule_AddObject(m, "expat_CAPI", capi_object);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001890 return m;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001891}
1892
Fred Drake6f987622000-08-25 18:03:30 +00001893static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001894clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001895{
Fred Drakecde79132001-04-25 16:01:30 +00001896 int i = 0;
1897 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001898
Fred Drake71b63ff2002-06-28 22:29:01 +00001899 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001900 if (initial)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 self->handlers[i] = NULL;
1902 else {
Fred Drakecde79132001-04-25 16:01:30 +00001903 temp = self->handlers[i];
1904 self->handlers[i] = NULL;
1905 Py_XDECREF(temp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001907 }
Fred Drakecde79132001-04-25 16:01:30 +00001908 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001909}
1910
Tim Peters0c322792002-07-17 16:49:03 +00001911static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001912 {"StartElementHandler",
1913 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001914 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001915 {"EndElementHandler",
1916 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001917 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001918 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001919 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1920 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001921 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001922 (xmlhandlersetter)XML_SetCharacterDataHandler,
1923 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001924 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001925 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001926 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001927 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001928 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001929 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001930 {"StartNamespaceDeclHandler",
1931 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001932 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001933 {"EndNamespaceDeclHandler",
1934 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001935 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001936 {"CommentHandler",
1937 (xmlhandlersetter)XML_SetCommentHandler,
1938 (xmlhandler)my_CommentHandler},
1939 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001940 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001941 (xmlhandler)my_StartCdataSectionHandler},
1942 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001943 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001944 (xmlhandler)my_EndCdataSectionHandler},
1945 {"DefaultHandler",
1946 (xmlhandlersetter)XML_SetDefaultHandler,
1947 (xmlhandler)my_DefaultHandler},
1948 {"DefaultHandlerExpand",
1949 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1950 (xmlhandler)my_DefaultHandlerExpandHandler},
1951 {"NotStandaloneHandler",
1952 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1953 (xmlhandler)my_NotStandaloneHandler},
1954 {"ExternalEntityRefHandler",
1955 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001956 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001957 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001958 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001959 (xmlhandler)my_StartDoctypeDeclHandler},
1960 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001961 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001962 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001963 {"EntityDeclHandler",
1964 (xmlhandlersetter)XML_SetEntityDeclHandler,
1965 (xmlhandler)my_EntityDeclHandler},
1966 {"XmlDeclHandler",
1967 (xmlhandlersetter)XML_SetXmlDeclHandler,
1968 (xmlhandler)my_XmlDeclHandler},
1969 {"ElementDeclHandler",
1970 (xmlhandlersetter)XML_SetElementDeclHandler,
1971 (xmlhandler)my_ElementDeclHandler},
1972 {"AttlistDeclHandler",
1973 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1974 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001975#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00001976 {"SkippedEntityHandler",
1977 (xmlhandlersetter)XML_SetSkippedEntityHandler,
1978 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001979#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001980
Fred Drake0582df92000-07-12 04:49:00 +00001981 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001982};