blob: 852d09388922d9f4c62cb6160cf117861e91de82 [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
196noop_character_data_handler(void *userData, const XML_Char *data, int len)
197{
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 PyObject *code = NULL;
213 PyObject *name = NULL;
214 PyObject *nulltuple = NULL;
215 PyObject *filename = NULL;
216
217 if (handler_info[slot].tb_code == NULL) {
218 code = PyString_FromString("");
219 if (code == NULL)
220 goto failed;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000221 name = PyUnicode_FromString(func_name);
Fred Drakebd6101c2001-02-14 18:29:45 +0000222 if (name == NULL)
223 goto failed;
224 nulltuple = PyTuple_New(0);
225 if (nulltuple == NULL)
226 goto failed;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000227 filename = PyUnicode_DecodeFSDefault(__FILE__);
Fred Drakebd6101c2001-02-14 18:29:45 +0000228 handler_info[slot].tb_code =
229 PyCode_New(0, /* argcount */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000230 0, /* kwonlyargcount */
Fred Drakebd6101c2001-02-14 18:29:45 +0000231 0, /* nlocals */
232 0, /* stacksize */
233 0, /* flags */
234 code, /* code */
235 nulltuple, /* consts */
236 nulltuple, /* names */
237 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000238#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000239 nulltuple, /* freevars */
240 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000241#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000242 filename, /* filename */
243 name, /* name */
244 lineno, /* firstlineno */
245 code /* lnotab */
246 );
247 if (handler_info[slot].tb_code == NULL)
248 goto failed;
249 Py_DECREF(code);
250 Py_DECREF(nulltuple);
251 Py_DECREF(filename);
252 Py_DECREF(name);
253 }
254 return handler_info[slot].tb_code;
255 failed:
256 Py_XDECREF(code);
257 Py_XDECREF(name);
258 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000259}
260
Jeremy Hylton9263f572003-06-27 16:13:17 +0000261#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000262static int
263trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
264{
265 int result = 0;
266 if (!tstate->use_tracing || tstate->tracing)
267 return 0;
268 if (tstate->c_profilefunc != NULL) {
269 tstate->tracing++;
270 result = tstate->c_profilefunc(tstate->c_profileobj,
271 f, code , val);
272 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
273 || (tstate->c_profilefunc != NULL));
274 tstate->tracing--;
275 if (result)
276 return result;
277 }
278 if (tstate->c_tracefunc != NULL) {
279 tstate->tracing++;
280 result = tstate->c_tracefunc(tstate->c_traceobj,
281 f, code , val);
282 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
283 || (tstate->c_profilefunc != NULL));
284 tstate->tracing--;
285 }
286 return result;
287}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000288
289static int
290trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
291{
292 PyObject *type, *value, *traceback, *arg;
293 int err;
294
295 if (tstate->c_tracefunc == NULL)
296 return 0;
297
298 PyErr_Fetch(&type, &value, &traceback);
299 if (value == NULL) {
300 value = Py_None;
301 Py_INCREF(value);
302 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000303#if PY_VERSION_HEX < 0x02040000
304 arg = Py_BuildValue("(OOO)", type, value, traceback);
305#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000306 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000307#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000308 if (arg == NULL) {
309 PyErr_Restore(type, value, traceback);
310 return 0;
311 }
312 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
313 Py_DECREF(arg);
314 if (err == 0)
315 PyErr_Restore(type, value, traceback);
316 else {
317 Py_XDECREF(type);
318 Py_XDECREF(value);
319 Py_XDECREF(traceback);
320 }
321 return err;
322}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000323#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000324
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000325static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000326call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
327 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000328{
Fred Drakebd6101c2001-02-14 18:29:45 +0000329 PyThreadState *tstate = PyThreadState_GET();
330 PyFrameObject *f;
331 PyObject *res;
332
333 if (c == NULL)
334 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000335
Jeremy Hylton9263f572003-06-27 16:13:17 +0000336 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000337 if (f == NULL)
338 return NULL;
339 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000340#ifdef FIX_TRACE
341 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000342 return NULL;
343 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000344#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000345 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000346 if (res == NULL) {
347 if (tstate->curexc_traceback == NULL)
348 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000349 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000350#ifdef FIX_TRACE
351 if (trace_frame_exc(tstate, f) < 0) {
352 return NULL;
353 }
354 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000355 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000356 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000357 Py_XDECREF(res);
358 res = NULL;
359 }
360 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000361#else
362 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000363#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000364 tstate->frame = f->f_back;
365 Py_DECREF(f);
366 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000367}
368
Fred Drakeb91a36b2002-06-27 19:40:48 +0000369static PyObject*
370string_intern(xmlparseobject *self, const char* str)
371{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000372 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000373 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000374 /* result can be NULL if the unicode conversion failed. */
375 if (!result)
376 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000377 if (!self->intern)
378 return result;
379 value = PyDict_GetItem(self->intern, result);
380 if (!value) {
381 if (PyDict_SetItem(self->intern, result, result) == 0)
382 return result;
383 else
384 return NULL;
385 }
386 Py_INCREF(value);
387 Py_DECREF(result);
388 return value;
389}
390
Fred Drake2a3d7db2002-06-28 22:56:48 +0000391/* Return 0 on success, -1 on exception.
392 * flag_error() will be called before return if needed.
393 */
394static int
395call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
396{
397 PyObject *args;
398 PyObject *temp;
399
400 args = PyTuple_New(1);
401 if (args == NULL)
402 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000403 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000404 if (temp == NULL) {
405 Py_DECREF(args);
406 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407 XML_SetCharacterDataHandler(self->itself,
408 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000409 return -1;
410 }
411 PyTuple_SET_ITEM(args, 0, temp);
412 /* temp is now a borrowed reference; consider it unused. */
413 self->in_callback = 1;
414 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000415 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000416 /* temp is an owned reference again, or NULL */
417 self->in_callback = 0;
418 Py_DECREF(args);
419 if (temp == NULL) {
420 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421 XML_SetCharacterDataHandler(self->itself,
422 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000423 return -1;
424 }
425 Py_DECREF(temp);
426 return 0;
427}
428
429static int
430flush_character_buffer(xmlparseobject *self)
431{
432 int rc;
433 if (self->buffer == NULL || self->buffer_used == 0)
434 return 0;
435 rc = call_character_handler(self, self->buffer, self->buffer_used);
436 self->buffer_used = 0;
437 return rc;
438}
439
440static void
441my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
442{
443 xmlparseobject *self = (xmlparseobject *) userData;
444 if (self->buffer == NULL)
445 call_character_handler(self, data, len);
446 else {
447 if ((self->buffer_used + len) > self->buffer_size) {
448 if (flush_character_buffer(self) < 0)
449 return;
450 /* handler might have changed; drop the rest on the floor
451 * if there isn't a handler anymore
452 */
453 if (!have_handler(self, CharacterData))
454 return;
455 }
456 if (len > self->buffer_size) {
457 call_character_handler(self, data, len);
458 self->buffer_used = 0;
459 }
460 else {
461 memcpy(self->buffer + self->buffer_used,
462 data, len * sizeof(XML_Char));
463 self->buffer_used += len;
464 }
465 }
466}
467
Fred Drake85d835f2001-02-08 15:39:08 +0000468static void
469my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000470 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000471{
472 xmlparseobject *self = (xmlparseobject *)userData;
473
Fred Drake71b63ff2002-06-28 22:29:01 +0000474 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000475 PyObject *container, *rv, *args;
476 int i, max;
477
Fred Drake2a3d7db2002-06-28 22:56:48 +0000478 if (flush_character_buffer(self) < 0)
479 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000480 /* Set max to the number of slots filled in atts[]; max/2 is
481 * the number of attributes we need to process.
482 */
483 if (self->specified_attributes) {
484 max = XML_GetSpecifiedAttributeCount(self->itself);
485 }
486 else {
487 max = 0;
488 while (atts[max] != NULL)
489 max += 2;
490 }
491 /* Build the container. */
492 if (self->ordered_attributes)
493 container = PyList_New(max);
494 else
495 container = PyDict_New();
496 if (container == NULL) {
497 flag_error(self);
498 return;
499 }
500 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000501 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000502 PyObject *v;
503 if (n == NULL) {
504 flag_error(self);
505 Py_DECREF(container);
506 return;
507 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000508 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000509 if (v == NULL) {
510 flag_error(self);
511 Py_DECREF(container);
512 Py_DECREF(n);
513 return;
514 }
515 if (self->ordered_attributes) {
516 PyList_SET_ITEM(container, i, n);
517 PyList_SET_ITEM(container, i+1, v);
518 }
519 else if (PyDict_SetItem(container, n, v)) {
520 flag_error(self);
521 Py_DECREF(n);
522 Py_DECREF(v);
523 return;
524 }
525 else {
526 Py_DECREF(n);
527 Py_DECREF(v);
528 }
529 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000530 args = string_intern(self, name);
531 if (args != NULL)
532 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000533 if (args == NULL) {
534 Py_DECREF(container);
535 return;
536 }
537 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000538 self->in_callback = 1;
539 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000540 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000541 self->in_callback = 0;
542 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000543 if (rv == NULL) {
544 flag_error(self);
545 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000546 }
Fred Drake85d835f2001-02-08 15:39:08 +0000547 Py_DECREF(rv);
548 }
549}
550
551#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
552 RETURN, GETUSERDATA) \
553static RC \
554my_##NAME##Handler PARAMS {\
555 xmlparseobject *self = GETUSERDATA ; \
556 PyObject *args = NULL; \
557 PyObject *rv = NULL; \
558 INIT \
559\
Fred Drake71b63ff2002-06-28 22:29:01 +0000560 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000561 if (flush_character_buffer(self) < 0) \
562 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000563 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000564 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000565 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000566 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000567 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000568 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000569 Py_DECREF(args); \
570 if (rv == NULL) { \
571 flag_error(self); \
572 return RETURN; \
573 } \
574 CONVERSION \
575 Py_DECREF(rv); \
576 } \
577 return RETURN; \
578}
579
Fred Drake6f987622000-08-25 18:03:30 +0000580#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
581 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
582 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000583
Fred Drake6f987622000-08-25 18:03:30 +0000584#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
585 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
Christian Heimes217cfd12007-12-02 14:31:20 +0000586 rc = PyLong_AsLong(rv);, rc, \
Fred Drake6f987622000-08-25 18:03:30 +0000587 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000588
Fred Drake71b63ff2002-06-28 22:29:01 +0000589VOID_HANDLER(EndElement,
590 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000591 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000592
Fred Drake6f987622000-08-25 18:03:30 +0000593VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000594 (void *userData,
595 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000596 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000597 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000598
Fred Drake6f987622000-08-25 18:03:30 +0000599VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000600 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000601 const XML_Char *entityName,
602 const XML_Char *base,
603 const XML_Char *systemId,
604 const XML_Char *publicId,
605 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000606 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000607 string_intern(self, entityName), string_intern(self, base),
608 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000609 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000610
Fred Drake85d835f2001-02-08 15:39:08 +0000611VOID_HANDLER(EntityDecl,
612 (void *userData,
613 const XML_Char *entityName,
614 int is_parameter_entity,
615 const XML_Char *value,
616 int value_length,
617 const XML_Char *base,
618 const XML_Char *systemId,
619 const XML_Char *publicId,
620 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000621 ("NiNNNNN",
622 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000623 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000624 string_intern(self, base), string_intern(self, systemId),
625 string_intern(self, publicId),
626 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000627
628VOID_HANDLER(XmlDecl,
629 (void *userData,
630 const XML_Char *version,
631 const XML_Char *encoding,
632 int standalone),
633 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000634 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000635 standalone))
636
637static PyObject *
638conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000639 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000640{
641 PyObject *result = NULL;
642 PyObject *children = PyTuple_New(model->numchildren);
643 int i;
644
645 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000646 assert(model->numchildren < INT_MAX);
647 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000648 PyObject *child = conv_content_model(&model->children[i],
649 conv_string);
650 if (child == NULL) {
651 Py_XDECREF(children);
652 return NULL;
653 }
654 PyTuple_SET_ITEM(children, i, child);
655 }
656 result = Py_BuildValue("(iiO&N)",
657 model->type, model->quant,
658 conv_string,model->name, children);
659 }
660 return result;
661}
662
Fred Drake06dd8cf2003-02-02 03:54:17 +0000663static void
664my_ElementDeclHandler(void *userData,
665 const XML_Char *name,
666 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000667{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000668 xmlparseobject *self = (xmlparseobject *)userData;
669 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000670
Fred Drake06dd8cf2003-02-02 03:54:17 +0000671 if (have_handler(self, ElementDecl)) {
672 PyObject *rv = NULL;
673 PyObject *modelobj, *nameobj;
674
675 if (flush_character_buffer(self) < 0)
676 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000677 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000678 if (modelobj == NULL) {
679 flag_error(self);
680 goto finally;
681 }
682 nameobj = string_intern(self, name);
683 if (nameobj == NULL) {
684 Py_DECREF(modelobj);
685 flag_error(self);
686 goto finally;
687 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000688 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000689 if (args == NULL) {
690 Py_DECREF(modelobj);
691 flag_error(self);
692 goto finally;
693 }
694 self->in_callback = 1;
695 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000696 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000697 self->in_callback = 0;
698 if (rv == NULL) {
699 flag_error(self);
700 goto finally;
701 }
702 Py_DECREF(rv);
703 }
704 finally:
705 Py_XDECREF(args);
706 XML_FreeContentModel(self->itself, model);
707 return;
708}
Fred Drake85d835f2001-02-08 15:39:08 +0000709
710VOID_HANDLER(AttlistDecl,
711 (void *userData,
712 const XML_Char *elname,
713 const XML_Char *attname,
714 const XML_Char *att_type,
715 const XML_Char *dflt,
716 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000717 ("(NNO&O&i)",
718 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000719 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000720 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000721
Martin v. Löwisc847f402003-01-21 11:09:21 +0000722#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000723VOID_HANDLER(SkippedEntity,
724 (void *userData,
725 const XML_Char *entityName,
726 int is_parameter_entity),
727 ("Ni",
728 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000729#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000730
Fred Drake71b63ff2002-06-28 22:29:01 +0000731VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000732 (void *userData,
733 const XML_Char *notationName,
734 const XML_Char *base,
735 const XML_Char *systemId,
736 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000737 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000738 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000739 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000740
Fred Drake6f987622000-08-25 18:03:30 +0000741VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000742 (void *userData,
743 const XML_Char *prefix,
744 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000745 ("(NN)",
746 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000747
Fred Drake6f987622000-08-25 18:03:30 +0000748VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000749 (void *userData,
750 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000751 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000752
Fred Drake6f987622000-08-25 18:03:30 +0000753VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000754 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000755 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756
Fred Drake6f987622000-08-25 18:03:30 +0000757VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000759 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000760
Fred Drake6f987622000-08-25 18:03:30 +0000761VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000763 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764
Fred Drake6f987622000-08-25 18:03:30 +0000765VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000766 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000767 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000768
Fred Drake6f987622000-08-25 18:03:30 +0000769VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000770 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000771 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000772
Fred Drake71b63ff2002-06-28 22:29:01 +0000773INT_HANDLER(NotStandalone,
774 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000775 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776
Fred Drake6f987622000-08-25 18:03:30 +0000777RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000778 (XML_Parser parser,
779 const XML_Char *context,
780 const XML_Char *base,
781 const XML_Char *systemId,
782 const XML_Char *publicId),
783 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000784 ("(O&NNN)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000785 conv_string_to_unicode ,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000786 string_intern(self, systemId), string_intern(self, publicId)),
Christian Heimes217cfd12007-12-02 14:31:20 +0000787 rc = PyLong_AsLong(rv);, rc,
Fred Drake6f987622000-08-25 18:03:30 +0000788 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000790/* XXX UnknownEncodingHandler */
791
Fred Drake85d835f2001-02-08 15:39:08 +0000792VOID_HANDLER(StartDoctypeDecl,
793 (void *userData, const XML_Char *doctypeName,
794 const XML_Char *sysid, const XML_Char *pubid,
795 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000796 ("(NNNi)", string_intern(self, doctypeName),
797 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000798 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000799
800VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000802/* ---------------------------------------------------------------- */
803
Fred Drake71b63ff2002-06-28 22:29:01 +0000804static PyObject *
805get_parse_result(xmlparseobject *self, int rv)
806{
807 if (PyErr_Occurred()) {
808 return NULL;
809 }
810 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000811 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000812 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000813 if (flush_character_buffer(self) < 0) {
814 return NULL;
815 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000816 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000817}
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000820"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000821Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000822
823static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000824xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825{
Fred Drake0582df92000-07-12 04:49:00 +0000826 char *s;
827 int slen;
828 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829
Fred Drake0582df92000-07-12 04:49:00 +0000830 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
831 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000832
833 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834}
835
Fred Drakeca1f4262000-09-21 20:10:23 +0000836/* File reading copied from cPickle */
837
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838#define BUF_SIZE 2048
839
Fred Drake0582df92000-07-12 04:49:00 +0000840static int
841readinst(char *buf, int buf_size, PyObject *meth)
842{
843 PyObject *arg = NULL;
844 PyObject *bytes = NULL;
845 PyObject *str = NULL;
846 int len = -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000847 char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848
Christian Heimes217cfd12007-12-02 14:31:20 +0000849 if ((bytes = PyLong_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000850 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000851
Fred Drake7b6caff2003-07-21 17:05:56 +0000852 if ((arg = PyTuple_New(1)) == NULL) {
853 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000854 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000855 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000856
Tim Peters954eef72000-09-22 06:01:11 +0000857 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000858
Martin v. Löwis9171f022004-10-13 19:50:11 +0000859#if PY_VERSION_HEX < 0x02020000
860 str = PyObject_CallObject(meth, arg);
861#else
862 str = PyObject_Call(meth, arg, NULL);
863#endif
864 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000865 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000866
Guido van Rossum98297ee2007-11-06 21:34:58 +0000867 if (PyString_Check(str))
868 ptr = PyString_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000869 else if (PyByteArray_Check(str))
870 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000871 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000872 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000873 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000874 Py_TYPE(str)->tp_name);
Fred Drake0582df92000-07-12 04:49:00 +0000875 goto finally;
876 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000877 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000878 if (len > buf_size) {
879 PyErr_Format(PyExc_ValueError,
880 "read() returned too much data: "
881 "%i bytes requested, %i returned",
882 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000883 goto finally;
884 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000885 memcpy(buf, ptr, len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886finally:
Fred Drake0582df92000-07-12 04:49:00 +0000887 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000888 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000889 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890}
891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000893"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000895
896static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000897xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000898{
Fred Drake0582df92000-07-12 04:49:00 +0000899 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000900 FILE *fp;
901 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000903 {
Fred Drake0582df92000-07-12 04:49:00 +0000904 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000905 readmethod = PyObject_GetAttrString(f, "read");
906 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000907 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000908 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000909 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +0000910 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000911 }
912 }
913 for (;;) {
914 int bytes_read;
915 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000916 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000917 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000918 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000919 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920
Fred Drake0582df92000-07-12 04:49:00 +0000921 if (fp) {
922 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
923 if (bytes_read < 0) {
924 PyErr_SetFromErrno(PyExc_IOError);
925 return NULL;
926 }
927 }
928 else {
929 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +0000930 if (bytes_read < 0) {
931 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000932 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000933 }
Fred Drake0582df92000-07-12 04:49:00 +0000934 }
935 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000936 if (PyErr_Occurred()) {
937 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000938 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000939 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000940
Fred Drake0582df92000-07-12 04:49:00 +0000941 if (!rv || bytes_read == 0)
942 break;
943 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000944 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000945 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000946}
947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000949"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000950Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000951
952static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000953xmlparse_SetBase(xmlparseobject *self, PyObject *args)
954{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000955 char *base;
956
Fred Drake0582df92000-07-12 04:49:00 +0000957 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000958 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000959 if (!XML_SetBase(self->itself, base)) {
960 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000961 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000962 Py_INCREF(Py_None);
963 return Py_None;
964}
965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000966PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000967"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000968Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000969
970static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000971xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
Fred Drake0582df92000-07-12 04:49:00 +0000972{
Fred Drake0582df92000-07-12 04:49:00 +0000973 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000974}
975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000976PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000977"GetInputContext() -> string\n\
978Return the untranslated text of the input that caused the current event.\n\
979If 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 +0000980for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000981
982static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000983xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
Fred Drakebd6101c2001-02-14 18:29:45 +0000984{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000985 if (self->in_callback) {
986 int offset, size;
987 const char *buffer
988 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000989
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000990 if (buffer != NULL)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000991 return PyString_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000992 size - offset);
993 else
994 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000995 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000996 else
997 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000998}
Fred Drakebd6101c2001-02-14 18:29:45 +0000999
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001000PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001001"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001002Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001004
1005static PyObject *
1006xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1007{
1008 char *context;
1009 char *encoding = NULL;
1010 xmlparseobject *new_parser;
1011 int i;
1012
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001013 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001014 &context, &encoding)) {
1015 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016 }
1017
Martin v. Löwis894258c2001-09-23 10:20:10 +00001018#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001019 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001020 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001021#else
1022 /* Python versions 2.2 and later */
1023 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1024#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001025
1026 if (new_parser == NULL)
1027 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001028 new_parser->buffer_size = self->buffer_size;
1029 new_parser->buffer_used = 0;
1030 if (self->buffer != NULL) {
1031 new_parser->buffer = malloc(new_parser->buffer_size);
1032 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001033#ifndef Py_TPFLAGS_HAVE_GC
1034 /* Code for versions 2.0 and 2.1 */
1035 PyObject_Del(new_parser);
1036#else
1037 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001038 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001039#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001040 return PyErr_NoMemory();
1041 }
1042 }
1043 else
1044 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001045 new_parser->ordered_attributes = self->ordered_attributes;
1046 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001047 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001048 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001049 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001050 encoding);
1051 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001052 new_parser->intern = self->intern;
1053 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001054#ifdef Py_TPFLAGS_HAVE_GC
1055 PyObject_GC_Track(new_parser);
1056#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001057 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001058#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001059
1060 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001061 Py_DECREF(new_parser);
1062 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001063 }
1064
1065 XML_SetUserData(new_parser->itself, (void *)new_parser);
1066
1067 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001068 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001069 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001070
Fred Drake2a3d7db2002-06-28 22:56:48 +00001071 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001072 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001073 Py_DECREF(new_parser);
1074 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001075 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001076 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001077
1078 /* then copy handlers from self */
1079 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001080 PyObject *handler = self->handlers[i];
1081 if (handler != NULL) {
1082 Py_INCREF(handler);
1083 new_parser->handlers[i] = handler;
1084 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001085 handler_info[i].handler);
1086 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001087 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001088 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001089}
1090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092"SetParamEntityParsing(flag) -> success\n\
1093Controls parsing of parameter entities (including the external DTD\n\
1094subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1095XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1096XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001098
1099static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001100xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101{
Fred Drake85d835f2001-02-08 15:39:08 +00001102 int flag;
1103 if (!PyArg_ParseTuple(args, "i", &flag))
1104 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001105 flag = XML_SetParamEntityParsing(p->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001106 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107}
1108
Martin v. Löwisc847f402003-01-21 11:09:21 +00001109
1110#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001111PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1112"UseForeignDTD([flag])\n\
1113Allows the application to provide an artificial external subset if one is\n\
1114not specified as part of the document instance. This readily allows the\n\
1115use of a 'default' document type controlled by the application, while still\n\
1116getting the advantage of providing document type information to the parser.\n\
1117'flag' defaults to True if not provided.");
1118
1119static PyObject *
1120xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1121{
1122 PyObject *flagobj = NULL;
1123 XML_Bool flag = XML_TRUE;
1124 enum XML_Error rc;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001125 if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
Martin v. Löwis069dde22003-01-21 10:58:18 +00001126 return NULL;
1127 if (flagobj != NULL)
1128 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1129 rc = XML_UseForeignDTD(self->itself, flag);
1130 if (rc != XML_ERROR_NONE) {
1131 return set_error(self, rc);
1132 }
1133 Py_INCREF(Py_None);
1134 return Py_None;
1135}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001136#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001137
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001138static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001139 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001140 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001141 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001142 METH_O, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001143 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001144 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001145 {"GetBase", (PyCFunction)xmlparse_GetBase,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001146 METH_NOARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001147 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001148 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001149 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1150 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001151 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001152 METH_NOARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001153#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001154 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1155 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001156#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001157 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001158};
1159
1160/* ---------- */
1161
1162
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001163
Fred Drake71b63ff2002-06-28 22:29:01 +00001164/* pyexpat international encoding support.
1165 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001166*/
1167
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001168static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001169PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001170
Fred Drake71b63ff2002-06-28 22:29:01 +00001171static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001172init_template_buffer(void)
1173{
1174 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001175 for (i = 0; i < 256; i++) {
1176 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001177 }
Fred Drakebb66a202001-03-01 20:48:17 +00001178 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001179}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180
Fred Drake71b63ff2002-06-28 22:29:01 +00001181static int
1182PyUnknownEncodingHandler(void *encodingHandlerData,
1183 const XML_Char *name,
1184 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001185{
Fred Drakebb66a202001-03-01 20:48:17 +00001186 PyUnicodeObject *_u_string = NULL;
1187 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001188 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001189
Fred Drakebb66a202001-03-01 20:48:17 +00001190 /* Yes, supports only 8bit encodings */
1191 _u_string = (PyUnicodeObject *)
1192 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001193
Fred Drakebb66a202001-03-01 20:48:17 +00001194 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001195 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001196
Fred Drakebb66a202001-03-01 20:48:17 +00001197 for (i = 0; i < 256; i++) {
1198 /* Stupid to access directly, but fast */
1199 Py_UNICODE c = _u_string->str[i];
1200 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001201 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001202 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001203 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001204 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001205 info->data = NULL;
1206 info->convert = NULL;
1207 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001208 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001209 Py_DECREF(_u_string);
1210 return result;
1211}
1212
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001213
1214static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001215newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001216{
1217 int i;
1218 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001219
Martin v. Löwis894258c2001-09-23 10:20:10 +00001220#ifdef Py_TPFLAGS_HAVE_GC
1221 /* Code for versions 2.2 and later */
1222 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1223#else
Fred Drake0582df92000-07-12 04:49:00 +00001224 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001225#endif
Fred Drake0582df92000-07-12 04:49:00 +00001226 if (self == NULL)
1227 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001228
Fred Drake2a3d7db2002-06-28 22:56:48 +00001229 self->buffer = NULL;
1230 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1231 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001232 self->ordered_attributes = 0;
1233 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001234 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001235 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001236 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001237 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001238 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1239 }
Fred Drake85d835f2001-02-08 15:39:08 +00001240 else {
Fred Drake0582df92000-07-12 04:49:00 +00001241 self->itself = XML_ParserCreate(encoding);
1242 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001243 self->intern = intern;
1244 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001245#ifdef Py_TPFLAGS_HAVE_GC
1246 PyObject_GC_Track(self);
1247#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001248 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001249#endif
Fred Drake0582df92000-07-12 04:49:00 +00001250 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001251 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001252 "XML_ParserCreate failed");
1253 Py_DECREF(self);
1254 return NULL;
1255 }
1256 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001257 XML_SetUnknownEncodingHandler(self->itself,
1258 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001259
Fred Drake2a3d7db2002-06-28 22:56:48 +00001260 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001261 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001262
Fred Drake7c75bf22002-07-01 14:02:31 +00001263 self->handlers = malloc(sizeof(PyObject *) * i);
1264 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001265 Py_DECREF(self);
1266 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001267 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001268 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001270 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001271}
1272
1273
1274static void
Fred Drake0582df92000-07-12 04:49:00 +00001275xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001276{
Fred Drake0582df92000-07-12 04:49:00 +00001277 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001278#ifdef Py_TPFLAGS_HAVE_GC
1279 PyObject_GC_UnTrack(self);
1280#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001281 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001282#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001283 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001284 XML_ParserFree(self->itself);
1285 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001286
Fred Drake85d835f2001-02-08 15:39:08 +00001287 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001288 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001289 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001290 temp = self->handlers[i];
1291 self->handlers[i] = NULL;
1292 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001293 }
1294 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001295 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001296 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001297 if (self->buffer != NULL) {
1298 free(self->buffer);
1299 self->buffer = NULL;
1300 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001301 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001302#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001303 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001304 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001305#else
1306 /* Code for versions 2.2 and later. */
1307 PyObject_GC_Del(self);
1308#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001309}
1310
Fred Drake0582df92000-07-12 04:49:00 +00001311static int
1312handlername2int(const char *name)
1313{
1314 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001315 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001316 if (strcmp(name, handler_info[i].name) == 0) {
1317 return i;
1318 }
1319 }
1320 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001321}
1322
1323static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001324get_pybool(int istrue)
1325{
1326 PyObject *result = istrue ? Py_True : Py_False;
1327 Py_INCREF(result);
1328 return result;
1329}
1330
1331static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001332xmlparse_getattr(xmlparseobject *self, char *name)
1333{
Fred Drake71b63ff2002-06-28 22:29:01 +00001334 int handlernum = handlername2int(name);
1335
1336 if (handlernum != -1) {
1337 PyObject *result = self->handlers[handlernum];
1338 if (result == NULL)
1339 result = Py_None;
1340 Py_INCREF(result);
1341 return result;
1342 }
1343 if (name[0] == 'E') {
1344 if (strcmp(name, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001345 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001346 XML_GetErrorCode(self->itself));
1347 if (strcmp(name, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001348 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001349 XML_GetErrorLineNumber(self->itself));
1350 if (strcmp(name, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001351 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001352 XML_GetErrorColumnNumber(self->itself));
1353 if (strcmp(name, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001354 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001355 XML_GetErrorByteIndex(self->itself));
1356 }
Dave Cole3203efb2004-08-26 00:37:31 +00001357 if (name[0] == 'C') {
1358 if (strcmp(name, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001359 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001360 XML_GetCurrentLineNumber(self->itself));
1361 if (strcmp(name, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001362 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001363 XML_GetCurrentColumnNumber(self->itself));
1364 if (strcmp(name, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001365 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001366 XML_GetCurrentByteIndex(self->itself));
1367 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001368 if (name[0] == 'b') {
1369 if (strcmp(name, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001370 return PyLong_FromLong((long) self->buffer_size);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001371 if (strcmp(name, "buffer_text") == 0)
1372 return get_pybool(self->buffer != NULL);
1373 if (strcmp(name, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001374 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001375 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001376 if (strcmp(name, "namespace_prefixes") == 0)
1377 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001378 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001379 return get_pybool(self->ordered_attributes);
Fred Drake85d835f2001-02-08 15:39:08 +00001380 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001381 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001382 if (strcmp(name, "intern") == 0) {
1383 if (self->intern == NULL) {
1384 Py_INCREF(Py_None);
1385 return Py_None;
1386 }
1387 else {
1388 Py_INCREF(self->intern);
1389 return self->intern;
1390 }
1391 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001392
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001393 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
1394}
1395
1396static PyObject *
1397xmlparse_dir(PyObject *self, PyObject* noargs)
1398{
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001399#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001400 do { \
Neal Norwitz392c5be2007-08-25 17:20:32 +00001401 PyObject *o = PyUnicode_FromString(str); \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001402 if (o != NULL) \
1403 PyList_Append(list, o); \
1404 Py_XDECREF(o); \
1405 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001406
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001407 int i;
1408 PyObject *rc = PyList_New(0);
1409 if (!rc)
1410 return NULL;
1411 for (i = 0; handler_info[i].name != NULL; i++) {
1412 PyObject *o = get_handler_name(&handler_info[i]);
1413 if (o != NULL)
1414 PyList_Append(rc, o);
1415 Py_XDECREF(o);
1416 }
1417 APPEND(rc, "ErrorCode");
1418 APPEND(rc, "ErrorLineNumber");
1419 APPEND(rc, "ErrorColumnNumber");
1420 APPEND(rc, "ErrorByteIndex");
1421 APPEND(rc, "CurrentLineNumber");
1422 APPEND(rc, "CurrentColumnNumber");
1423 APPEND(rc, "CurrentByteIndex");
1424 APPEND(rc, "buffer_size");
1425 APPEND(rc, "buffer_text");
1426 APPEND(rc, "buffer_used");
1427 APPEND(rc, "namespace_prefixes");
1428 APPEND(rc, "ordered_attributes");
1429 APPEND(rc, "specified_attributes");
1430 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001431
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001432#undef APPEND
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001433
1434 if (PyErr_Occurred()) {
1435 Py_DECREF(rc);
1436 rc = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001437 }
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001438
1439 return rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001440}
1441
Fred Drake6f987622000-08-25 18:03:30 +00001442static int
1443sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001444{
1445 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001446 if (handlernum >= 0) {
1447 xmlhandler c_handler = NULL;
1448 PyObject *temp = self->handlers[handlernum];
1449
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001450 if (v == Py_None) {
1451 /* If this is the character data handler, and a character
1452 data handler is already active, we need to be more
1453 careful. What we can safely do is replace the existing
1454 character data handler callback function with a no-op
1455 function that will refuse to call Python. The downside
1456 is that this doesn't completely remove the character
1457 data handler from the C layer if there's any callback
1458 active, so Expat does a little more work than it
1459 otherwise would, but that's really an odd case. A more
1460 elaborate system of handlers and state could remove the
1461 C handler more effectively. */
1462 if (handlernum == CharacterData && self->in_callback)
1463 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001464 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001465 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001466 else if (v != NULL) {
1467 Py_INCREF(v);
1468 c_handler = handler_info[handlernum].handler;
1469 }
Fred Drake0582df92000-07-12 04:49:00 +00001470 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001471 Py_XDECREF(temp);
1472 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001473 return 1;
1474 }
1475 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001476}
1477
1478static int
Fred Drake6f987622000-08-25 18:03:30 +00001479xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001480{
Fred Drake6f987622000-08-25 18:03:30 +00001481 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001482 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001483 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1484 return -1;
1485 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001486 if (strcmp(name, "buffer_text") == 0) {
1487 if (PyObject_IsTrue(v)) {
1488 if (self->buffer == NULL) {
1489 self->buffer = malloc(self->buffer_size);
1490 if (self->buffer == NULL) {
1491 PyErr_NoMemory();
1492 return -1;
1493 }
1494 self->buffer_used = 0;
1495 }
1496 }
1497 else if (self->buffer != NULL) {
1498 if (flush_character_buffer(self) < 0)
1499 return -1;
1500 free(self->buffer);
1501 self->buffer = NULL;
1502 }
1503 return 0;
1504 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001505 if (strcmp(name, "namespace_prefixes") == 0) {
1506 if (PyObject_IsTrue(v))
1507 self->ns_prefixes = 1;
1508 else
1509 self->ns_prefixes = 0;
1510 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1511 return 0;
1512 }
Fred Drake85d835f2001-02-08 15:39:08 +00001513 if (strcmp(name, "ordered_attributes") == 0) {
1514 if (PyObject_IsTrue(v))
1515 self->ordered_attributes = 1;
1516 else
1517 self->ordered_attributes = 0;
1518 return 0;
1519 }
Fred Drake85d835f2001-02-08 15:39:08 +00001520 if (strcmp(name, "specified_attributes") == 0) {
1521 if (PyObject_IsTrue(v))
1522 self->specified_attributes = 1;
1523 else
1524 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001525 return 0;
1526 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001527
1528 if (strcmp(name, "buffer_size") == 0) {
1529 long new_buffer_size;
1530 if (!PyLong_Check(v)) {
1531 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1532 return -1;
1533 }
1534
1535 new_buffer_size=PyLong_AS_LONG(v);
1536 /* trivial case -- no change */
1537 if (new_buffer_size == self->buffer_size) {
1538 return 0;
1539 }
1540
1541 if (new_buffer_size <= 0) {
1542 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1543 return -1;
1544 }
1545
1546 /* check maximum */
1547 if (new_buffer_size > INT_MAX) {
1548 char errmsg[100];
1549 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1550 PyErr_SetString(PyExc_ValueError, errmsg);
1551 return -1;
1552 }
1553
1554 if (self->buffer != NULL) {
1555 /* there is already a buffer */
1556 if (self->buffer_used != 0) {
1557 flush_character_buffer(self);
1558 }
1559 /* free existing buffer */
1560 free(self->buffer);
1561 }
1562 self->buffer = malloc(new_buffer_size);
1563 if (self->buffer == NULL) {
1564 PyErr_NoMemory();
1565 return -1;
1566 }
1567 self->buffer_size = new_buffer_size;
1568 return 0;
1569 }
1570
Fred Drake2a3d7db2002-06-28 22:56:48 +00001571 if (strcmp(name, "CharacterDataHandler") == 0) {
1572 /* If we're changing the character data handler, flush all
1573 * cached data with the old handler. Not sure there's a
1574 * "right" thing to do, though, but this probably won't
1575 * happen.
1576 */
1577 if (flush_character_buffer(self) < 0)
1578 return -1;
1579 }
Fred Drake6f987622000-08-25 18:03:30 +00001580 if (sethandler(self, name, v)) {
1581 return 0;
1582 }
1583 PyErr_SetString(PyExc_AttributeError, name);
1584 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001585}
1586
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001587static int
1588xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1589{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001590 int i;
1591 for (i = 0; handler_info[i].name != NULL; i++)
1592 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001593 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001594}
1595
1596static int
1597xmlparse_clear(xmlparseobject *op)
1598{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001599 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001600 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001601 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001602}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001605
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001606static PyMethodDef xmlparse_tp_methods[] = {
Christian Heimes651b61f2007-11-21 00:54:38 +00001607 {"__dir__", xmlparse_dir, METH_NOARGS},
1608 {NULL, NULL} /* sentinel */
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001609};
1610
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001611static PyTypeObject Xmlparsetype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001612 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001613 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001614 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001615 0, /*tp_itemsize*/
1616 /* methods */
1617 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1618 (printfunc)0, /*tp_print*/
1619 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1620 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1621 (cmpfunc)0, /*tp_compare*/
1622 (reprfunc)0, /*tp_repr*/
1623 0, /*tp_as_number*/
1624 0, /*tp_as_sequence*/
1625 0, /*tp_as_mapping*/
1626 (hashfunc)0, /*tp_hash*/
1627 (ternaryfunc)0, /*tp_call*/
1628 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001629 0, /* tp_getattro */
1630 0, /* tp_setattro */
1631 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001632#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001634#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001636#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001637 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001638 (traverseproc)xmlparse_traverse, /* tp_traverse */
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001639 (inquiry)xmlparse_clear, /* tp_clear */
1640 0, /* tp_richcompare */
1641 0, /* tp_weaklistoffset */
1642 0, /* tp_iter */
1643 0, /* tp_iternext */
1644 xmlparse_tp_methods /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001645};
1646
1647/* End of code for xmlparser objects */
1648/* -------------------------------------------------------- */
1649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001651"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001653
1654static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001655pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1656{
Fred Drakecde79132001-04-25 16:01:30 +00001657 char *encoding = NULL;
1658 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001659 PyObject *intern = NULL;
1660 PyObject *result;
1661 int intern_decref = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001662 static char *kwlist[] = {"encoding", "namespace_separator",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001663 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001664
Fred Drakeb91a36b2002-06-27 19:40:48 +00001665 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1666 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001667 return NULL;
1668 if (namespace_separator != NULL
1669 && strlen(namespace_separator) > 1) {
1670 PyErr_SetString(PyExc_ValueError,
1671 "namespace_separator must be at most one"
1672 " character, omitted, or None");
1673 return NULL;
1674 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001675 /* Explicitly passing None means no interning is desired.
1676 Not passing anything means that a new dictionary is used. */
1677 if (intern == Py_None)
1678 intern = NULL;
1679 else if (intern == NULL) {
1680 intern = PyDict_New();
1681 if (!intern)
1682 return NULL;
1683 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001684 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001685 else if (!PyDict_Check(intern)) {
1686 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1687 return NULL;
1688 }
1689
1690 result = newxmlparseobject(encoding, namespace_separator, intern);
1691 if (intern_decref) {
1692 Py_DECREF(intern);
1693 }
1694 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001695}
1696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001697PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001698"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001699Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001700
1701static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001702pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001703{
Fred Drake0582df92000-07-12 04:49:00 +00001704 long code = 0;
1705
1706 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1707 return NULL;
1708 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001709}
1710
1711/* List of methods defined in the module */
1712
1713static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001714 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1715 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1716 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1717 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001718
Fred Drake0582df92000-07-12 04:49:00 +00001719 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001720};
1721
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001722/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724PyDoc_STRVAR(pyexpat_module_documentation,
1725"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001726
Fred Drake4113b132001-03-24 19:58:26 +00001727/* Return a Python string that represents the version number without the
1728 * extra cruft added by revision control, even if the right options were
1729 * given to the "cvs export" command to make it not include the extra
1730 * cruft.
1731 */
1732static PyObject *
1733get_version_string(void)
1734{
1735 static char *rcsid = "$Revision$";
1736 char *rev = rcsid;
1737 int i = 0;
1738
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001739 while (!isdigit(Py_CHARMASK(*rev)))
Fred Drake4113b132001-03-24 19:58:26 +00001740 ++rev;
1741 while (rev[i] != ' ' && rev[i] != '\0')
1742 ++i;
1743
Neal Norwitz392c5be2007-08-25 17:20:32 +00001744 return PyUnicode_FromStringAndSize(rev, i);
Fred Drake4113b132001-03-24 19:58:26 +00001745}
1746
Fred Drakecde79132001-04-25 16:01:30 +00001747/* Initialization function for the module */
1748
1749#ifndef MODULE_NAME
1750#define MODULE_NAME "pyexpat"
1751#endif
1752
1753#ifndef MODULE_INITFUNC
1754#define MODULE_INITFUNC initpyexpat
1755#endif
1756
Martin v. Löwis069dde22003-01-21 10:58:18 +00001757#ifndef PyMODINIT_FUNC
1758# ifdef MS_WINDOWS
1759# define PyMODINIT_FUNC __declspec(dllexport) void
1760# else
1761# define PyMODINIT_FUNC void
1762# endif
1763#endif
1764
Mark Hammond8235ea12002-07-19 06:55:41 +00001765PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001766
Martin v. Löwis069dde22003-01-21 10:58:18 +00001767PyMODINIT_FUNC
1768MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001769{
1770 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001771 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001772 PyObject *errors_module;
1773 PyObject *modelmod_name;
1774 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001775 PyObject *sys_modules;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001776 static struct PyExpat_CAPI capi;
1777 PyObject* capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001778
Fred Drake6f987622000-08-25 18:03:30 +00001779 if (errmod_name == NULL)
1780 return;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001781 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001782 if (modelmod_name == NULL)
1783 return;
Fred Drake6f987622000-08-25 18:03:30 +00001784
Christian Heimes90aa7642007-12-19 02:45:37 +00001785 Py_TYPE(&Xmlparsetype) = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001786
Fred Drake0582df92000-07-12 04:49:00 +00001787 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001788 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001789 pyexpat_module_documentation);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001790 if (m == NULL)
1791 return;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001792
Fred Drake0582df92000-07-12 04:49:00 +00001793 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001794 if (ErrorObject == NULL) {
1795 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001796 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001797 if (ErrorObject == NULL)
1798 return;
1799 }
1800 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001801 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001802 Py_INCREF(ErrorObject);
1803 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001804 Py_INCREF(&Xmlparsetype);
1805 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001806
Fred Drake4113b132001-03-24 19:58:26 +00001807 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001808 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1809 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001810 {
1811 XML_Expat_Version info = XML_ExpatVersionInfo();
1812 PyModule_AddObject(m, "version_info",
1813 Py_BuildValue("(iii)", info.major,
1814 info.minor, info.micro));
1815 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001816 init_template_buffer();
Fred Drake0582df92000-07-12 04:49:00 +00001817 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001818 compiled, this should check and set native_encoding
1819 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001820 */
Fred Drake93adb692000-09-23 04:55:48 +00001821 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001822
Fred Drake85d835f2001-02-08 15:39:08 +00001823 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001824 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001825 errors_module = PyDict_GetItem(d, errmod_name);
1826 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001827 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001828 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001829 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001830 /* gives away the reference to errors_module */
1831 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001832 }
1833 }
Fred Drake6f987622000-08-25 18:03:30 +00001834 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001835 model_module = PyDict_GetItem(d, modelmod_name);
1836 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001837 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001838 if (model_module != NULL) {
1839 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1840 /* gives away the reference to model_module */
1841 PyModule_AddObject(m, "model", model_module);
1842 }
1843 }
1844 Py_DECREF(modelmod_name);
1845 if (errors_module == NULL || model_module == NULL)
1846 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001847 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001848
Martin v. Löwisc847f402003-01-21 11:09:21 +00001849#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001850 {
1851 const XML_Feature *features = XML_GetFeatureList();
1852 PyObject *list = PyList_New(0);
1853 if (list == NULL)
1854 /* just ignore it */
1855 PyErr_Clear();
1856 else {
1857 int i = 0;
1858 for (; features[i].feature != XML_FEATURE_END; ++i) {
1859 int ok;
1860 PyObject *item = Py_BuildValue("si", features[i].name,
1861 features[i].value);
1862 if (item == NULL) {
1863 Py_DECREF(list);
1864 list = NULL;
1865 break;
1866 }
1867 ok = PyList_Append(list, item);
1868 Py_DECREF(item);
1869 if (ok < 0) {
1870 PyErr_Clear();
1871 break;
1872 }
1873 }
1874 if (list != NULL)
1875 PyModule_AddObject(m, "features", list);
1876 }
1877 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001878#endif
Fred Drake6f987622000-08-25 18:03:30 +00001879
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001880#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001881 PyModule_AddStringConstant(errors_module, #name, \
1882 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001883
Fred Drake0582df92000-07-12 04:49:00 +00001884 MYCONST(XML_ERROR_NO_MEMORY);
1885 MYCONST(XML_ERROR_SYNTAX);
1886 MYCONST(XML_ERROR_NO_ELEMENTS);
1887 MYCONST(XML_ERROR_INVALID_TOKEN);
1888 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1889 MYCONST(XML_ERROR_PARTIAL_CHAR);
1890 MYCONST(XML_ERROR_TAG_MISMATCH);
1891 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1892 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1893 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1894 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1895 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1896 MYCONST(XML_ERROR_ASYNC_ENTITY);
1897 MYCONST(XML_ERROR_BAD_CHAR_REF);
1898 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1899 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1900 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1901 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1902 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001903 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1904 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1905 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001906 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1907 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1908 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1909 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1910 /* Added in Expat 1.95.7. */
1911 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1912 /* Added in Expat 1.95.8. */
1913 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1914 MYCONST(XML_ERROR_INCOMPLETE_PE);
1915 MYCONST(XML_ERROR_XML_DECL);
1916 MYCONST(XML_ERROR_TEXT_DECL);
1917 MYCONST(XML_ERROR_PUBLICID);
1918 MYCONST(XML_ERROR_SUSPENDED);
1919 MYCONST(XML_ERROR_NOT_SUSPENDED);
1920 MYCONST(XML_ERROR_ABORTED);
1921 MYCONST(XML_ERROR_FINISHED);
1922 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001923
Fred Drake85d835f2001-02-08 15:39:08 +00001924 PyModule_AddStringConstant(errors_module, "__doc__",
1925 "Constants used to describe error conditions.");
1926
Fred Drake93adb692000-09-23 04:55:48 +00001927#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001928
Fred Drake85d835f2001-02-08 15:39:08 +00001929#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001930 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1931 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1932 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001933#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001934
Fred Drake85d835f2001-02-08 15:39:08 +00001935#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1936 PyModule_AddStringConstant(model_module, "__doc__",
1937 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001938
Fred Drake85d835f2001-02-08 15:39:08 +00001939 MYCONST(XML_CTYPE_EMPTY);
1940 MYCONST(XML_CTYPE_ANY);
1941 MYCONST(XML_CTYPE_MIXED);
1942 MYCONST(XML_CTYPE_NAME);
1943 MYCONST(XML_CTYPE_CHOICE);
1944 MYCONST(XML_CTYPE_SEQ);
1945
1946 MYCONST(XML_CQUANT_NONE);
1947 MYCONST(XML_CQUANT_OPT);
1948 MYCONST(XML_CQUANT_REP);
1949 MYCONST(XML_CQUANT_PLUS);
1950#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001951
1952 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001953 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001954 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001955 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1956 capi.MINOR_VERSION = XML_MINOR_VERSION;
1957 capi.MICRO_VERSION = XML_MICRO_VERSION;
1958 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001959 capi.GetErrorCode = XML_GetErrorCode;
1960 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1961 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001962 capi.Parse = XML_Parse;
1963 capi.ParserCreate_MM = XML_ParserCreate_MM;
1964 capi.ParserFree = XML_ParserFree;
1965 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1966 capi.SetCommentHandler = XML_SetCommentHandler;
1967 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1968 capi.SetElementHandler = XML_SetElementHandler;
1969 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1970 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1971 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1972 capi.SetUserData = XML_SetUserData;
Fredrik Lundhc3345042005-12-13 19:49:55 +00001973
1974 /* export as cobject */
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001975 capi_object = PyCObject_FromVoidPtr(&capi, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001976 if (capi_object)
1977 PyModule_AddObject(m, "expat_CAPI", capi_object);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001978}
1979
Fred Drake6f987622000-08-25 18:03:30 +00001980static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001981clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001982{
Fred Drakecde79132001-04-25 16:01:30 +00001983 int i = 0;
1984 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001985
Fred Drake71b63ff2002-06-28 22:29:01 +00001986 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001987 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001988 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001989 else {
Fred Drakecde79132001-04-25 16:01:30 +00001990 temp = self->handlers[i];
1991 self->handlers[i] = NULL;
1992 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001993 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001994 }
Fred Drakecde79132001-04-25 16:01:30 +00001995 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001996}
1997
Tim Peters0c322792002-07-17 16:49:03 +00001998static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001999 {"StartElementHandler",
2000 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002001 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002002 {"EndElementHandler",
2003 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002004 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002005 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002006 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2007 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002008 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002009 (xmlhandlersetter)XML_SetCharacterDataHandler,
2010 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002011 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002012 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002013 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002014 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002015 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002016 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002017 {"StartNamespaceDeclHandler",
2018 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002019 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002020 {"EndNamespaceDeclHandler",
2021 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002022 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002023 {"CommentHandler",
2024 (xmlhandlersetter)XML_SetCommentHandler,
2025 (xmlhandler)my_CommentHandler},
2026 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002027 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002028 (xmlhandler)my_StartCdataSectionHandler},
2029 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002030 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002031 (xmlhandler)my_EndCdataSectionHandler},
2032 {"DefaultHandler",
2033 (xmlhandlersetter)XML_SetDefaultHandler,
2034 (xmlhandler)my_DefaultHandler},
2035 {"DefaultHandlerExpand",
2036 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2037 (xmlhandler)my_DefaultHandlerExpandHandler},
2038 {"NotStandaloneHandler",
2039 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2040 (xmlhandler)my_NotStandaloneHandler},
2041 {"ExternalEntityRefHandler",
2042 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002043 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002044 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002045 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002046 (xmlhandler)my_StartDoctypeDeclHandler},
2047 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002048 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002049 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002050 {"EntityDeclHandler",
2051 (xmlhandlersetter)XML_SetEntityDeclHandler,
2052 (xmlhandler)my_EntityDeclHandler},
2053 {"XmlDeclHandler",
2054 (xmlhandlersetter)XML_SetXmlDeclHandler,
2055 (xmlhandler)my_XmlDeclHandler},
2056 {"ElementDeclHandler",
2057 (xmlhandlersetter)XML_SetElementDeclHandler,
2058 (xmlhandler)my_ElementDeclHandler},
2059 {"AttlistDeclHandler",
2060 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2061 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002062#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002063 {"SkippedEntityHandler",
2064 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2065 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002066#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002067
Fred Drake0582df92000-07-12 04:49:00 +00002068 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002069};