blob: 02c5d400090361cea7d1fd0296065db5b8991d19 [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
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000011#ifndef PyDoc_STRVAR
Martin v. Löwis069dde22003-01-21 10:58:18 +000012
13/*
14 * fdrake says:
15 * Don't change the PyDoc_STR macro definition to (str), because
16 * '''the parentheses cause compile failures
17 * ("non-constant static initializer" or something like that)
18 * on some platforms (Irix?)'''
19 */
Fred Drakef57b22a2002-09-02 15:54:06 +000020#define PyDoc_STR(str) str
Fred Drake7c75bf22002-07-01 14:02:31 +000021#define PyDoc_VAR(name) static char name[]
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000022#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000023#endif
24
Jeremy Hylton9263f572003-06-27 16:13:17 +000025#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000026
Fred Drake0582df92000-07-12 04:49:00 +000027enum HandlerTypes {
28 StartElement,
29 EndElement,
30 ProcessingInstruction,
31 CharacterData,
32 UnparsedEntityDecl,
33 NotationDecl,
34 StartNamespaceDecl,
35 EndNamespaceDecl,
36 Comment,
37 StartCdataSection,
38 EndCdataSection,
39 Default,
40 DefaultHandlerExpand,
41 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000042 ExternalEntityRef,
43 StartDoctypeDecl,
44 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000045 EntityDecl,
46 XmlDecl,
47 ElementDecl,
48 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000049#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000050 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000051#endif
Fred Drake85d835f2001-02-08 15:39:08 +000052 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000053};
54
55static PyObject *ErrorObject;
56
57/* ----------------------------------------------------- */
58
59/* Declarations for objects of type xmlparser */
60
61typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000062 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000063
Fred Drake0582df92000-07-12 04:49:00 +000064 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000065 int ordered_attributes; /* Return attributes as a list. */
66 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000067 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000068 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000069 XML_Char *buffer; /* Buffer used when accumulating characters */
70 /* NULL if not enabled */
71 int buffer_size; /* Size of buffer, in XML_Char units */
72 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000073 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000074 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000075} xmlparseobject;
76
Fred Drake2a3d7db2002-06-28 22:56:48 +000077#define CHARACTER_DATA_BUFFER_SIZE 8192
78
Jeremy Hylton938ace62002-07-17 16:30:39 +000079static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000080
Fred Drake117ac852002-09-24 16:24:54 +000081typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000082typedef void* xmlhandler;
83
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000084struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000085 const char *name;
86 xmlhandlersetter setter;
87 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000088 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000089 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090};
91
Jeremy Hylton938ace62002-07-17 16:30:39 +000092static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000093
Fred Drakebd6101c2001-02-14 18:29:45 +000094/* Set an integer attribute on the error object; return true on success,
95 * false on an exception.
96 */
97static int
98set_error_attr(PyObject *err, char *name, int value)
99{
100 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000101
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000102 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
103 Py_XDECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000104 return 0;
105 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000106 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000107 return 1;
108}
109
110/* Build and set an Expat exception, including positioning
111 * information. Always returns NULL.
112 */
Fred Drake85d835f2001-02-08 15:39:08 +0000113static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000114set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000115{
116 PyObject *err;
117 char buffer[256];
118 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000119 int lineno = XML_GetErrorLineNumber(parser);
120 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000121
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000122 /* There is no risk of overflowing this buffer, since
123 even for 64-bit integers, there is sufficient space. */
124 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000125 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000126 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000127 if ( err != NULL
128 && set_error_attr(err, "code", code)
129 && set_error_attr(err, "offset", column)
130 && set_error_attr(err, "lineno", lineno)) {
131 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000132 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000133 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000134 return NULL;
135}
136
Fred Drake71b63ff2002-06-28 22:29:01 +0000137static int
138have_handler(xmlparseobject *self, int type)
139{
140 PyObject *handler = self->handlers[type];
141 return handler != NULL;
142}
143
144static PyObject *
145get_handler_name(struct HandlerInfo *hinfo)
146{
147 PyObject *name = hinfo->nameobj;
148 if (name == NULL) {
149 name = PyString_FromString(hinfo->name);
150 hinfo->nameobj = name;
151 }
152 Py_XINCREF(name);
153 return name;
154}
155
Fred Drake85d835f2001-02-08 15:39:08 +0000156
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000157/* Convert a string of XML_Chars into a Unicode string.
158 Returns None if str is a null pointer. */
159
Fred Drake0582df92000-07-12 04:49:00 +0000160static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000161conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000162{
Fred Drake71b63ff2002-06-28 22:29:01 +0000163 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000164 and hence in UTF-8. */
165 /* UTF-8 from Expat, Unicode desired */
166 if (str == NULL) {
167 Py_INCREF(Py_None);
168 return Py_None;
169 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000170 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171}
172
Fred Drake0582df92000-07-12 04:49:00 +0000173static PyObject *
174conv_string_len_to_unicode(const XML_Char *str, int len)
175{
Fred Drake71b63ff2002-06-28 22:29:01 +0000176 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000177 and hence in UTF-8. */
178 /* UTF-8 from Expat, Unicode desired */
179 if (str == NULL) {
180 Py_INCREF(Py_None);
181 return Py_None;
182 }
Fred Drake6f987622000-08-25 18:03:30 +0000183 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000184}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000185
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000186/* Callback routines */
187
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000188static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000189
Martin v. Löwis069dde22003-01-21 10:58:18 +0000190/* This handler is used when an error has been detected, in the hope
191 that actual parsing can be terminated early. This will only help
192 if an external entity reference is encountered. */
193static int
194error_external_entity_ref_handler(XML_Parser parser,
195 const XML_Char *context,
196 const XML_Char *base,
197 const XML_Char *systemId,
198 const XML_Char *publicId)
199{
200 return 0;
201}
202
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203/* Dummy character data handler used when an error (exception) has
204 been detected, and the actual parsing can be terminated early.
205 This is needed since character data handler can't be safely removed
206 from within the character data handler, but can be replaced. It is
207 used only from the character data handler trampoline, and must be
208 used right after `flag_error()` is called. */
209static void
210noop_character_data_handler(void *userData, const XML_Char *data, int len)
211{
212 /* Do nothing. */
213}
214
Fred Drake6f987622000-08-25 18:03:30 +0000215static void
216flag_error(xmlparseobject *self)
217{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000218 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000219 XML_SetExternalEntityRefHandler(self->itself,
220 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000221}
222
223static PyCodeObject*
224getcode(enum HandlerTypes slot, char* func_name, int lineno)
225{
Fred Drakebd6101c2001-02-14 18:29:45 +0000226 PyObject *code = NULL;
227 PyObject *name = NULL;
228 PyObject *nulltuple = NULL;
229 PyObject *filename = NULL;
230
231 if (handler_info[slot].tb_code == NULL) {
232 code = PyString_FromString("");
233 if (code == NULL)
234 goto failed;
235 name = PyString_FromString(func_name);
236 if (name == NULL)
237 goto failed;
238 nulltuple = PyTuple_New(0);
239 if (nulltuple == NULL)
240 goto failed;
241 filename = PyString_FromString(__FILE__);
242 handler_info[slot].tb_code =
243 PyCode_New(0, /* argcount */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000244 0, /* kwonlyargcount */
Fred Drakebd6101c2001-02-14 18:29:45 +0000245 0, /* nlocals */
246 0, /* stacksize */
247 0, /* flags */
248 code, /* code */
249 nulltuple, /* consts */
250 nulltuple, /* names */
251 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000252#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000253 nulltuple, /* freevars */
254 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000255#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000256 filename, /* filename */
257 name, /* name */
258 lineno, /* firstlineno */
259 code /* lnotab */
260 );
261 if (handler_info[slot].tb_code == NULL)
262 goto failed;
263 Py_DECREF(code);
264 Py_DECREF(nulltuple);
265 Py_DECREF(filename);
266 Py_DECREF(name);
267 }
268 return handler_info[slot].tb_code;
269 failed:
270 Py_XDECREF(code);
271 Py_XDECREF(name);
272 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000273}
274
Jeremy Hylton9263f572003-06-27 16:13:17 +0000275#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000276static int
277trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
278{
279 int result = 0;
280 if (!tstate->use_tracing || tstate->tracing)
281 return 0;
282 if (tstate->c_profilefunc != NULL) {
283 tstate->tracing++;
284 result = tstate->c_profilefunc(tstate->c_profileobj,
285 f, code , val);
286 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
287 || (tstate->c_profilefunc != NULL));
288 tstate->tracing--;
289 if (result)
290 return result;
291 }
292 if (tstate->c_tracefunc != NULL) {
293 tstate->tracing++;
294 result = tstate->c_tracefunc(tstate->c_traceobj,
295 f, code , val);
296 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
297 || (tstate->c_profilefunc != NULL));
298 tstate->tracing--;
299 }
300 return result;
301}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000302
303static int
304trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
305{
306 PyObject *type, *value, *traceback, *arg;
307 int err;
308
309 if (tstate->c_tracefunc == NULL)
310 return 0;
311
312 PyErr_Fetch(&type, &value, &traceback);
313 if (value == NULL) {
314 value = Py_None;
315 Py_INCREF(value);
316 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000317#if PY_VERSION_HEX < 0x02040000
318 arg = Py_BuildValue("(OOO)", type, value, traceback);
319#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000320 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000321#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000322 if (arg == NULL) {
323 PyErr_Restore(type, value, traceback);
324 return 0;
325 }
326 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
327 Py_DECREF(arg);
328 if (err == 0)
329 PyErr_Restore(type, value, traceback);
330 else {
331 Py_XDECREF(type);
332 Py_XDECREF(value);
333 Py_XDECREF(traceback);
334 }
335 return err;
336}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000337#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000338
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000339static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000340call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
341 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000342{
Fred Drakebd6101c2001-02-14 18:29:45 +0000343 PyThreadState *tstate = PyThreadState_GET();
344 PyFrameObject *f;
345 PyObject *res;
346
347 if (c == NULL)
348 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000349
Jeremy Hylton9263f572003-06-27 16:13:17 +0000350 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000351 if (f == NULL)
352 return NULL;
353 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000354#ifdef FIX_TRACE
355 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000356 return NULL;
357 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000358#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000359 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000360 if (res == NULL) {
361 if (tstate->curexc_traceback == NULL)
362 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000363 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000364#ifdef FIX_TRACE
365 if (trace_frame_exc(tstate, f) < 0) {
366 return NULL;
367 }
368 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000369 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000370 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000371 Py_XDECREF(res);
372 res = NULL;
373 }
374 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000375#else
376 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000377#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000378 tstate->frame = f->f_back;
379 Py_DECREF(f);
380 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000381}
382
Fred Drakeb91a36b2002-06-27 19:40:48 +0000383static PyObject*
384string_intern(xmlparseobject *self, const char* str)
385{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000386 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000387 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000388 /* result can be NULL if the unicode conversion failed. */
389 if (!result)
390 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000391 if (!self->intern)
392 return result;
393 value = PyDict_GetItem(self->intern, result);
394 if (!value) {
395 if (PyDict_SetItem(self->intern, result, result) == 0)
396 return result;
397 else
398 return NULL;
399 }
400 Py_INCREF(value);
401 Py_DECREF(result);
402 return value;
403}
404
Fred Drake2a3d7db2002-06-28 22:56:48 +0000405/* Return 0 on success, -1 on exception.
406 * flag_error() will be called before return if needed.
407 */
408static int
409call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
410{
411 PyObject *args;
412 PyObject *temp;
413
414 args = PyTuple_New(1);
415 if (args == NULL)
416 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000417 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000418 if (temp == NULL) {
419 Py_DECREF(args);
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 PyTuple_SET_ITEM(args, 0, temp);
426 /* temp is now a borrowed reference; consider it unused. */
427 self->in_callback = 1;
428 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000429 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000430 /* temp is an owned reference again, or NULL */
431 self->in_callback = 0;
432 Py_DECREF(args);
433 if (temp == NULL) {
434 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000435 XML_SetCharacterDataHandler(self->itself,
436 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000437 return -1;
438 }
439 Py_DECREF(temp);
440 return 0;
441}
442
443static int
444flush_character_buffer(xmlparseobject *self)
445{
446 int rc;
447 if (self->buffer == NULL || self->buffer_used == 0)
448 return 0;
449 rc = call_character_handler(self, self->buffer, self->buffer_used);
450 self->buffer_used = 0;
451 return rc;
452}
453
454static void
455my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
456{
457 xmlparseobject *self = (xmlparseobject *) userData;
458 if (self->buffer == NULL)
459 call_character_handler(self, data, len);
460 else {
461 if ((self->buffer_used + len) > self->buffer_size) {
462 if (flush_character_buffer(self) < 0)
463 return;
464 /* handler might have changed; drop the rest on the floor
465 * if there isn't a handler anymore
466 */
467 if (!have_handler(self, CharacterData))
468 return;
469 }
470 if (len > self->buffer_size) {
471 call_character_handler(self, data, len);
472 self->buffer_used = 0;
473 }
474 else {
475 memcpy(self->buffer + self->buffer_used,
476 data, len * sizeof(XML_Char));
477 self->buffer_used += len;
478 }
479 }
480}
481
Fred Drake85d835f2001-02-08 15:39:08 +0000482static void
483my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000484 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000485{
486 xmlparseobject *self = (xmlparseobject *)userData;
487
Fred Drake71b63ff2002-06-28 22:29:01 +0000488 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000489 PyObject *container, *rv, *args;
490 int i, max;
491
Fred Drake2a3d7db2002-06-28 22:56:48 +0000492 if (flush_character_buffer(self) < 0)
493 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000494 /* Set max to the number of slots filled in atts[]; max/2 is
495 * the number of attributes we need to process.
496 */
497 if (self->specified_attributes) {
498 max = XML_GetSpecifiedAttributeCount(self->itself);
499 }
500 else {
501 max = 0;
502 while (atts[max] != NULL)
503 max += 2;
504 }
505 /* Build the container. */
506 if (self->ordered_attributes)
507 container = PyList_New(max);
508 else
509 container = PyDict_New();
510 if (container == NULL) {
511 flag_error(self);
512 return;
513 }
514 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000515 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000516 PyObject *v;
517 if (n == NULL) {
518 flag_error(self);
519 Py_DECREF(container);
520 return;
521 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000522 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000523 if (v == NULL) {
524 flag_error(self);
525 Py_DECREF(container);
526 Py_DECREF(n);
527 return;
528 }
529 if (self->ordered_attributes) {
530 PyList_SET_ITEM(container, i, n);
531 PyList_SET_ITEM(container, i+1, v);
532 }
533 else if (PyDict_SetItem(container, n, v)) {
534 flag_error(self);
535 Py_DECREF(n);
536 Py_DECREF(v);
537 return;
538 }
539 else {
540 Py_DECREF(n);
541 Py_DECREF(v);
542 }
543 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000544 args = string_intern(self, name);
545 if (args != NULL)
546 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000547 if (args == NULL) {
548 Py_DECREF(container);
549 return;
550 }
551 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000552 self->in_callback = 1;
553 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000554 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000555 self->in_callback = 0;
556 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000557 if (rv == NULL) {
558 flag_error(self);
559 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000560 }
Fred Drake85d835f2001-02-08 15:39:08 +0000561 Py_DECREF(rv);
562 }
563}
564
565#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
566 RETURN, GETUSERDATA) \
567static RC \
568my_##NAME##Handler PARAMS {\
569 xmlparseobject *self = GETUSERDATA ; \
570 PyObject *args = NULL; \
571 PyObject *rv = NULL; \
572 INIT \
573\
Fred Drake71b63ff2002-06-28 22:29:01 +0000574 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000575 if (flush_character_buffer(self) < 0) \
576 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000577 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000578 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000579 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000580 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000581 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000582 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000583 Py_DECREF(args); \
584 if (rv == NULL) { \
585 flag_error(self); \
586 return RETURN; \
587 } \
588 CONVERSION \
589 Py_DECREF(rv); \
590 } \
591 return RETURN; \
592}
593
Fred Drake6f987622000-08-25 18:03:30 +0000594#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
595 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
596 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000597
Fred Drake6f987622000-08-25 18:03:30 +0000598#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
599 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
600 rc = PyInt_AsLong(rv);, rc, \
601 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000602
Fred Drake71b63ff2002-06-28 22:29:01 +0000603VOID_HANDLER(EndElement,
604 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000605 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000606
Fred Drake6f987622000-08-25 18:03:30 +0000607VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000608 (void *userData,
609 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000610 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000611 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000612
Fred Drake6f987622000-08-25 18:03:30 +0000613VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000614 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000615 const XML_Char *entityName,
616 const XML_Char *base,
617 const XML_Char *systemId,
618 const XML_Char *publicId,
619 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000620 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000621 string_intern(self, entityName), string_intern(self, base),
622 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000623 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000624
Fred Drake85d835f2001-02-08 15:39:08 +0000625VOID_HANDLER(EntityDecl,
626 (void *userData,
627 const XML_Char *entityName,
628 int is_parameter_entity,
629 const XML_Char *value,
630 int value_length,
631 const XML_Char *base,
632 const XML_Char *systemId,
633 const XML_Char *publicId,
634 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000635 ("NiNNNNN",
636 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000637 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000638 string_intern(self, base), string_intern(self, systemId),
639 string_intern(self, publicId),
640 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000641
642VOID_HANDLER(XmlDecl,
643 (void *userData,
644 const XML_Char *version,
645 const XML_Char *encoding,
646 int standalone),
647 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000648 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000649 standalone))
650
651static PyObject *
652conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000653 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000654{
655 PyObject *result = NULL;
656 PyObject *children = PyTuple_New(model->numchildren);
657 int i;
658
659 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000660 assert(model->numchildren < INT_MAX);
661 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000662 PyObject *child = conv_content_model(&model->children[i],
663 conv_string);
664 if (child == NULL) {
665 Py_XDECREF(children);
666 return NULL;
667 }
668 PyTuple_SET_ITEM(children, i, child);
669 }
670 result = Py_BuildValue("(iiO&N)",
671 model->type, model->quant,
672 conv_string,model->name, children);
673 }
674 return result;
675}
676
Fred Drake06dd8cf2003-02-02 03:54:17 +0000677static void
678my_ElementDeclHandler(void *userData,
679 const XML_Char *name,
680 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000681{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000682 xmlparseobject *self = (xmlparseobject *)userData;
683 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000684
Fred Drake06dd8cf2003-02-02 03:54:17 +0000685 if (have_handler(self, ElementDecl)) {
686 PyObject *rv = NULL;
687 PyObject *modelobj, *nameobj;
688
689 if (flush_character_buffer(self) < 0)
690 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000691 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000692 if (modelobj == NULL) {
693 flag_error(self);
694 goto finally;
695 }
696 nameobj = string_intern(self, name);
697 if (nameobj == NULL) {
698 Py_DECREF(modelobj);
699 flag_error(self);
700 goto finally;
701 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000702 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000703 if (args == NULL) {
704 Py_DECREF(modelobj);
705 flag_error(self);
706 goto finally;
707 }
708 self->in_callback = 1;
709 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000710 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000711 self->in_callback = 0;
712 if (rv == NULL) {
713 flag_error(self);
714 goto finally;
715 }
716 Py_DECREF(rv);
717 }
718 finally:
719 Py_XDECREF(args);
720 XML_FreeContentModel(self->itself, model);
721 return;
722}
Fred Drake85d835f2001-02-08 15:39:08 +0000723
724VOID_HANDLER(AttlistDecl,
725 (void *userData,
726 const XML_Char *elname,
727 const XML_Char *attname,
728 const XML_Char *att_type,
729 const XML_Char *dflt,
730 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000731 ("(NNO&O&i)",
732 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000733 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000734 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000735
Martin v. Löwisc847f402003-01-21 11:09:21 +0000736#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000737VOID_HANDLER(SkippedEntity,
738 (void *userData,
739 const XML_Char *entityName,
740 int is_parameter_entity),
741 ("Ni",
742 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000743#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000744
Fred Drake71b63ff2002-06-28 22:29:01 +0000745VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000746 (void *userData,
747 const XML_Char *notationName,
748 const XML_Char *base,
749 const XML_Char *systemId,
750 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000751 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000752 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000753 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000754
Fred Drake6f987622000-08-25 18:03:30 +0000755VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000756 (void *userData,
757 const XML_Char *prefix,
758 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000759 ("(NN)",
760 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000761
Fred Drake6f987622000-08-25 18:03:30 +0000762VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000763 (void *userData,
764 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000765 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000766
Fred Drake6f987622000-08-25 18:03:30 +0000767VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000768 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000769 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770
Fred Drake6f987622000-08-25 18:03:30 +0000771VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000772 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000773 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000774
Fred Drake6f987622000-08-25 18:03:30 +0000775VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000777 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000778
Fred Drake6f987622000-08-25 18:03:30 +0000779VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000780 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000781 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000782
Fred Drake6f987622000-08-25 18:03:30 +0000783VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000784 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000785 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000786
Fred Drake71b63ff2002-06-28 22:29:01 +0000787INT_HANDLER(NotStandalone,
788 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000789 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000790
Fred Drake6f987622000-08-25 18:03:30 +0000791RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000792 (XML_Parser parser,
793 const XML_Char *context,
794 const XML_Char *base,
795 const XML_Char *systemId,
796 const XML_Char *publicId),
797 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000798 ("(O&NNN)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000799 conv_string_to_unicode ,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000800 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000801 rc = PyInt_AsLong(rv);, rc,
802 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000803
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000804/* XXX UnknownEncodingHandler */
805
Fred Drake85d835f2001-02-08 15:39:08 +0000806VOID_HANDLER(StartDoctypeDecl,
807 (void *userData, const XML_Char *doctypeName,
808 const XML_Char *sysid, const XML_Char *pubid,
809 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000810 ("(NNNi)", string_intern(self, doctypeName),
811 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000812 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000813
814VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000815
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000816/* ---------------------------------------------------------------- */
817
Fred Drake71b63ff2002-06-28 22:29:01 +0000818static PyObject *
819get_parse_result(xmlparseobject *self, int rv)
820{
821 if (PyErr_Occurred()) {
822 return NULL;
823 }
824 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000825 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000826 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000827 if (flush_character_buffer(self) < 0) {
828 return NULL;
829 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000830 return PyInt_FromLong(rv);
831}
832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000834"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000835Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000836
837static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000838xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000839{
Fred Drake0582df92000-07-12 04:49:00 +0000840 char *s;
841 int slen;
842 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000843
Fred Drake0582df92000-07-12 04:49:00 +0000844 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
845 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000846
847 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848}
849
Fred Drakeca1f4262000-09-21 20:10:23 +0000850/* File reading copied from cPickle */
851
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000852#define BUF_SIZE 2048
853
Fred Drake0582df92000-07-12 04:49:00 +0000854static int
855readinst(char *buf, int buf_size, PyObject *meth)
856{
857 PyObject *arg = NULL;
858 PyObject *bytes = NULL;
859 PyObject *str = NULL;
860 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000861
Fred Drake676940b2000-09-22 15:21:31 +0000862 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000863 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000864
Fred Drake7b6caff2003-07-21 17:05:56 +0000865 if ((arg = PyTuple_New(1)) == NULL) {
866 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000867 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000868 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000869
Tim Peters954eef72000-09-22 06:01:11 +0000870 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000871
Martin v. Löwis9171f022004-10-13 19:50:11 +0000872#if PY_VERSION_HEX < 0x02020000
873 str = PyObject_CallObject(meth, arg);
874#else
875 str = PyObject_Call(meth, arg, NULL);
876#endif
877 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000878 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000879
Fred Drake0582df92000-07-12 04:49:00 +0000880 /* XXX what to do if it returns a Unicode string? */
Guido van Rossum4ca94712007-07-23 17:42:32 +0000881 if (!PyBytes_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000882 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000883 "read() did not return a bytes object (type=%.400s)",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000884 Py_Type(str)->tp_name);
Fred Drake0582df92000-07-12 04:49:00 +0000885 goto finally;
886 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000887 len = PyBytes_GET_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000888 if (len > buf_size) {
889 PyErr_Format(PyExc_ValueError,
890 "read() returned too much data: "
891 "%i bytes requested, %i returned",
892 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000893 goto finally;
894 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000895 memcpy(buf, PyBytes_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896finally:
Fred Drake0582df92000-07-12 04:49:00 +0000897 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000898 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000899 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000900}
901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000903"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000904Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000905
906static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000907xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000908{
Fred Drake0582df92000-07-12 04:49:00 +0000909 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000910 FILE *fp;
911 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000912
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000913 {
Fred Drake0582df92000-07-12 04:49:00 +0000914 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000915 readmethod = PyObject_GetAttrString(f, "read");
916 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000917 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000918 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000919 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +0000920 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000921 }
922 }
923 for (;;) {
924 int bytes_read;
925 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000926 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000927 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000928 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000929 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000930
Fred Drake0582df92000-07-12 04:49:00 +0000931 if (fp) {
932 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
933 if (bytes_read < 0) {
934 PyErr_SetFromErrno(PyExc_IOError);
935 return NULL;
936 }
937 }
938 else {
939 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +0000940 if (bytes_read < 0) {
941 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000942 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000943 }
Fred Drake0582df92000-07-12 04:49:00 +0000944 }
945 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000946 if (PyErr_Occurred()) {
947 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000948 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000949 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000950
Fred Drake0582df92000-07-12 04:49:00 +0000951 if (!rv || bytes_read == 0)
952 break;
953 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000954 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000955 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000956}
957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000958PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000959"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000960Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000961
962static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000963xmlparse_SetBase(xmlparseobject *self, PyObject *args)
964{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000965 char *base;
966
Fred Drake0582df92000-07-12 04:49:00 +0000967 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000968 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000969 if (!XML_SetBase(self->itself, base)) {
970 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000971 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000972 Py_INCREF(Py_None);
973 return Py_None;
974}
975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000976PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000977"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000978Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000979
980static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000981xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
Fred Drake0582df92000-07-12 04:49:00 +0000982{
Fred Drake0582df92000-07-12 04:49:00 +0000983 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000984}
985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000986PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000987"GetInputContext() -> string\n\
988Return the untranslated text of the input that caused the current event.\n\
989If 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 +0000990for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000991
992static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000993xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
Fred Drakebd6101c2001-02-14 18:29:45 +0000994{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000995 if (self->in_callback) {
996 int offset, size;
997 const char *buffer
998 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000999
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001000 if (buffer != NULL)
Guido van Rossum4ca94712007-07-23 17:42:32 +00001001 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001002 size - offset);
1003 else
1004 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +00001005 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001006 else
1007 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +00001008}
Fred Drakebd6101c2001-02-14 18:29:45 +00001009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001011"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001012Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001014
1015static PyObject *
1016xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1017{
1018 char *context;
1019 char *encoding = NULL;
1020 xmlparseobject *new_parser;
1021 int i;
1022
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001023 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001024 &context, &encoding)) {
1025 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001026 }
1027
Martin v. Löwis894258c2001-09-23 10:20:10 +00001028#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001029 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001030 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001031#else
1032 /* Python versions 2.2 and later */
1033 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1034#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001035
1036 if (new_parser == NULL)
1037 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001038 new_parser->buffer_size = self->buffer_size;
1039 new_parser->buffer_used = 0;
1040 if (self->buffer != NULL) {
1041 new_parser->buffer = malloc(new_parser->buffer_size);
1042 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001043#ifndef Py_TPFLAGS_HAVE_GC
1044 /* Code for versions 2.0 and 2.1 */
1045 PyObject_Del(new_parser);
1046#else
1047 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001048 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001049#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001050 return PyErr_NoMemory();
1051 }
1052 }
1053 else
1054 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001055 new_parser->ordered_attributes = self->ordered_attributes;
1056 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001057 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001058 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001059 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001060 encoding);
1061 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001062 new_parser->intern = self->intern;
1063 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001064#ifdef Py_TPFLAGS_HAVE_GC
1065 PyObject_GC_Track(new_parser);
1066#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001067 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001068#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001069
1070 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001071 Py_DECREF(new_parser);
1072 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001073 }
1074
1075 XML_SetUserData(new_parser->itself, (void *)new_parser);
1076
1077 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001078 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001079 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001080
Fred Drake2a3d7db2002-06-28 22:56:48 +00001081 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001082 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001083 Py_DECREF(new_parser);
1084 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001085 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001086 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001087
1088 /* then copy handlers from self */
1089 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001090 PyObject *handler = self->handlers[i];
1091 if (handler != NULL) {
1092 Py_INCREF(handler);
1093 new_parser->handlers[i] = handler;
1094 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001095 handler_info[i].handler);
1096 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001097 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001098 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001099}
1100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001102"SetParamEntityParsing(flag) -> success\n\
1103Controls parsing of parameter entities (including the external DTD\n\
1104subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1105XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1106XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001108
1109static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001110xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001111{
Fred Drake85d835f2001-02-08 15:39:08 +00001112 int flag;
1113 if (!PyArg_ParseTuple(args, "i", &flag))
1114 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001115 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001116 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001117}
1118
Martin v. Löwisc847f402003-01-21 11:09:21 +00001119
1120#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001121PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1122"UseForeignDTD([flag])\n\
1123Allows the application to provide an artificial external subset if one is\n\
1124not specified as part of the document instance. This readily allows the\n\
1125use of a 'default' document type controlled by the application, while still\n\
1126getting the advantage of providing document type information to the parser.\n\
1127'flag' defaults to True if not provided.");
1128
1129static PyObject *
1130xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1131{
1132 PyObject *flagobj = NULL;
1133 XML_Bool flag = XML_TRUE;
1134 enum XML_Error rc;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001135 if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
Martin v. Löwis069dde22003-01-21 10:58:18 +00001136 return NULL;
1137 if (flagobj != NULL)
1138 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1139 rc = XML_UseForeignDTD(self->itself, flag);
1140 if (rc != XML_ERROR_NONE) {
1141 return set_error(self, rc);
1142 }
1143 Py_INCREF(Py_None);
1144 return Py_None;
1145}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001146#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001147
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001148static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001149 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001150 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001151 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001152 METH_O, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001153 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001154 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001155 {"GetBase", (PyCFunction)xmlparse_GetBase,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001156 METH_NOARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001157 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001158 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001159 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1160 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001161 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001162 METH_NOARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001163#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001164 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1165 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001166#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001167 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001168};
1169
1170/* ---------- */
1171
1172
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001173
Fred Drake71b63ff2002-06-28 22:29:01 +00001174/* pyexpat international encoding support.
1175 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001176*/
1177
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001178static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001179PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180
Fred Drake71b63ff2002-06-28 22:29:01 +00001181static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001182init_template_buffer(void)
1183{
1184 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001185 for (i = 0; i < 256; i++) {
1186 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001187 }
Fred Drakebb66a202001-03-01 20:48:17 +00001188 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001189}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001190
Fred Drake71b63ff2002-06-28 22:29:01 +00001191static int
1192PyUnknownEncodingHandler(void *encodingHandlerData,
1193 const XML_Char *name,
1194 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001195{
Fred Drakebb66a202001-03-01 20:48:17 +00001196 PyUnicodeObject *_u_string = NULL;
1197 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001198 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001199
Fred Drakebb66a202001-03-01 20:48:17 +00001200 /* Yes, supports only 8bit encodings */
1201 _u_string = (PyUnicodeObject *)
1202 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001203
Fred Drakebb66a202001-03-01 20:48:17 +00001204 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001205 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001206
Fred Drakebb66a202001-03-01 20:48:17 +00001207 for (i = 0; i < 256; i++) {
1208 /* Stupid to access directly, but fast */
1209 Py_UNICODE c = _u_string->str[i];
1210 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001211 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001212 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001213 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001214 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001215 info->data = NULL;
1216 info->convert = NULL;
1217 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001218 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001219 Py_DECREF(_u_string);
1220 return result;
1221}
1222
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001223
1224static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001225newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001226{
1227 int i;
1228 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001229
Martin v. Löwis894258c2001-09-23 10:20:10 +00001230#ifdef Py_TPFLAGS_HAVE_GC
1231 /* Code for versions 2.2 and later */
1232 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1233#else
Fred Drake0582df92000-07-12 04:49:00 +00001234 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001235#endif
Fred Drake0582df92000-07-12 04:49:00 +00001236 if (self == NULL)
1237 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001238
Fred Drake2a3d7db2002-06-28 22:56:48 +00001239 self->buffer = NULL;
1240 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1241 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001242 self->ordered_attributes = 0;
1243 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001244 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001245 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001246 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001247 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001248 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1249 }
Fred Drake85d835f2001-02-08 15:39:08 +00001250 else {
Fred Drake0582df92000-07-12 04:49:00 +00001251 self->itself = XML_ParserCreate(encoding);
1252 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001253 self->intern = intern;
1254 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001255#ifdef Py_TPFLAGS_HAVE_GC
1256 PyObject_GC_Track(self);
1257#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001258 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001259#endif
Fred Drake0582df92000-07-12 04:49:00 +00001260 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001261 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001262 "XML_ParserCreate failed");
1263 Py_DECREF(self);
1264 return NULL;
1265 }
1266 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001267 XML_SetUnknownEncodingHandler(self->itself,
1268 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269
Fred Drake2a3d7db2002-06-28 22:56:48 +00001270 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001271 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272
Fred Drake7c75bf22002-07-01 14:02:31 +00001273 self->handlers = malloc(sizeof(PyObject *) * i);
1274 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001275 Py_DECREF(self);
1276 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001277 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001278 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001279
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001280 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001281}
1282
1283
1284static void
Fred Drake0582df92000-07-12 04:49:00 +00001285xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001286{
Fred Drake0582df92000-07-12 04:49:00 +00001287 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001288#ifdef Py_TPFLAGS_HAVE_GC
1289 PyObject_GC_UnTrack(self);
1290#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001291 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001292#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001293 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001294 XML_ParserFree(self->itself);
1295 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001296
Fred Drake85d835f2001-02-08 15:39:08 +00001297 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001298 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001299 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001300 temp = self->handlers[i];
1301 self->handlers[i] = NULL;
1302 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001303 }
1304 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001305 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001306 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001307 if (self->buffer != NULL) {
1308 free(self->buffer);
1309 self->buffer = NULL;
1310 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001311 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001312#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001313 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001314 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001315#else
1316 /* Code for versions 2.2 and later. */
1317 PyObject_GC_Del(self);
1318#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001319}
1320
Fred Drake0582df92000-07-12 04:49:00 +00001321static int
1322handlername2int(const char *name)
1323{
1324 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001325 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001326 if (strcmp(name, handler_info[i].name) == 0) {
1327 return i;
1328 }
1329 }
1330 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001331}
1332
1333static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001334get_pybool(int istrue)
1335{
1336 PyObject *result = istrue ? Py_True : Py_False;
1337 Py_INCREF(result);
1338 return result;
1339}
1340
1341static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001342xmlparse_getattr(xmlparseobject *self, char *name)
1343{
Fred Drake71b63ff2002-06-28 22:29:01 +00001344 int handlernum = handlername2int(name);
1345
1346 if (handlernum != -1) {
1347 PyObject *result = self->handlers[handlernum];
1348 if (result == NULL)
1349 result = Py_None;
1350 Py_INCREF(result);
1351 return result;
1352 }
1353 if (name[0] == 'E') {
1354 if (strcmp(name, "ErrorCode") == 0)
1355 return PyInt_FromLong((long)
1356 XML_GetErrorCode(self->itself));
1357 if (strcmp(name, "ErrorLineNumber") == 0)
1358 return PyInt_FromLong((long)
1359 XML_GetErrorLineNumber(self->itself));
1360 if (strcmp(name, "ErrorColumnNumber") == 0)
1361 return PyInt_FromLong((long)
1362 XML_GetErrorColumnNumber(self->itself));
1363 if (strcmp(name, "ErrorByteIndex") == 0)
1364 return PyInt_FromLong((long)
1365 XML_GetErrorByteIndex(self->itself));
1366 }
Dave Cole3203efb2004-08-26 00:37:31 +00001367 if (name[0] == 'C') {
1368 if (strcmp(name, "CurrentLineNumber") == 0)
1369 return PyInt_FromLong((long)
1370 XML_GetCurrentLineNumber(self->itself));
1371 if (strcmp(name, "CurrentColumnNumber") == 0)
1372 return PyInt_FromLong((long)
1373 XML_GetCurrentColumnNumber(self->itself));
1374 if (strcmp(name, "CurrentByteIndex") == 0)
1375 return PyInt_FromLong((long)
1376 XML_GetCurrentByteIndex(self->itself));
1377 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001378 if (name[0] == 'b') {
1379 if (strcmp(name, "buffer_size") == 0)
1380 return PyInt_FromLong((long) self->buffer_size);
1381 if (strcmp(name, "buffer_text") == 0)
1382 return get_pybool(self->buffer != NULL);
1383 if (strcmp(name, "buffer_used") == 0)
1384 return PyInt_FromLong((long) self->buffer_used);
1385 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001386 if (strcmp(name, "namespace_prefixes") == 0)
1387 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001388 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001389 return get_pybool(self->ordered_attributes);
Fred Drake85d835f2001-02-08 15:39:08 +00001390 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001391 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001392 if (strcmp(name, "intern") == 0) {
1393 if (self->intern == NULL) {
1394 Py_INCREF(Py_None);
1395 return Py_None;
1396 }
1397 else {
1398 Py_INCREF(self->intern);
1399 return self->intern;
1400 }
1401 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001402
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001403 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
1404}
1405
1406static PyObject *
1407xmlparse_dir(PyObject *self, PyObject* noargs)
1408{
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001409#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001410 do { \
1411 PyObject *o = PyString_FromString(str); \
1412 if (o != NULL) \
1413 PyList_Append(list, o); \
1414 Py_XDECREF(o); \
1415 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001416
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001417 int i;
1418 PyObject *rc = PyList_New(0);
1419 if (!rc)
1420 return NULL;
1421 for (i = 0; handler_info[i].name != NULL; i++) {
1422 PyObject *o = get_handler_name(&handler_info[i]);
1423 if (o != NULL)
1424 PyList_Append(rc, o);
1425 Py_XDECREF(o);
1426 }
1427 APPEND(rc, "ErrorCode");
1428 APPEND(rc, "ErrorLineNumber");
1429 APPEND(rc, "ErrorColumnNumber");
1430 APPEND(rc, "ErrorByteIndex");
1431 APPEND(rc, "CurrentLineNumber");
1432 APPEND(rc, "CurrentColumnNumber");
1433 APPEND(rc, "CurrentByteIndex");
1434 APPEND(rc, "buffer_size");
1435 APPEND(rc, "buffer_text");
1436 APPEND(rc, "buffer_used");
1437 APPEND(rc, "namespace_prefixes");
1438 APPEND(rc, "ordered_attributes");
1439 APPEND(rc, "specified_attributes");
1440 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001441
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001442#undef APPEND
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001443
1444 if (PyErr_Occurred()) {
1445 Py_DECREF(rc);
1446 rc = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001447 }
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001448
1449 return rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001450}
1451
Fred Drake6f987622000-08-25 18:03:30 +00001452static int
1453sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001454{
1455 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001456 if (handlernum >= 0) {
1457 xmlhandler c_handler = NULL;
1458 PyObject *temp = self->handlers[handlernum];
1459
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001460 if (v == Py_None) {
1461 /* If this is the character data handler, and a character
1462 data handler is already active, we need to be more
1463 careful. What we can safely do is replace the existing
1464 character data handler callback function with a no-op
1465 function that will refuse to call Python. The downside
1466 is that this doesn't completely remove the character
1467 data handler from the C layer if there's any callback
1468 active, so Expat does a little more work than it
1469 otherwise would, but that's really an odd case. A more
1470 elaborate system of handlers and state could remove the
1471 C handler more effectively. */
1472 if (handlernum == CharacterData && self->in_callback)
1473 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001474 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001475 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001476 else if (v != NULL) {
1477 Py_INCREF(v);
1478 c_handler = handler_info[handlernum].handler;
1479 }
Fred Drake0582df92000-07-12 04:49:00 +00001480 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001481 Py_XDECREF(temp);
1482 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001483 return 1;
1484 }
1485 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001486}
1487
1488static int
Fred Drake6f987622000-08-25 18:03:30 +00001489xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001490{
Fred Drake6f987622000-08-25 18:03:30 +00001491 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001492 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001493 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1494 return -1;
1495 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001496 if (strcmp(name, "buffer_text") == 0) {
1497 if (PyObject_IsTrue(v)) {
1498 if (self->buffer == NULL) {
1499 self->buffer = malloc(self->buffer_size);
1500 if (self->buffer == NULL) {
1501 PyErr_NoMemory();
1502 return -1;
1503 }
1504 self->buffer_used = 0;
1505 }
1506 }
1507 else if (self->buffer != NULL) {
1508 if (flush_character_buffer(self) < 0)
1509 return -1;
1510 free(self->buffer);
1511 self->buffer = NULL;
1512 }
1513 return 0;
1514 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001515 if (strcmp(name, "namespace_prefixes") == 0) {
1516 if (PyObject_IsTrue(v))
1517 self->ns_prefixes = 1;
1518 else
1519 self->ns_prefixes = 0;
1520 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1521 return 0;
1522 }
Fred Drake85d835f2001-02-08 15:39:08 +00001523 if (strcmp(name, "ordered_attributes") == 0) {
1524 if (PyObject_IsTrue(v))
1525 self->ordered_attributes = 1;
1526 else
1527 self->ordered_attributes = 0;
1528 return 0;
1529 }
Fred Drake85d835f2001-02-08 15:39:08 +00001530 if (strcmp(name, "specified_attributes") == 0) {
1531 if (PyObject_IsTrue(v))
1532 self->specified_attributes = 1;
1533 else
1534 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001535 return 0;
1536 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001537 if (strcmp(name, "CharacterDataHandler") == 0) {
1538 /* If we're changing the character data handler, flush all
1539 * cached data with the old handler. Not sure there's a
1540 * "right" thing to do, though, but this probably won't
1541 * happen.
1542 */
1543 if (flush_character_buffer(self) < 0)
1544 return -1;
1545 }
Fred Drake6f987622000-08-25 18:03:30 +00001546 if (sethandler(self, name, v)) {
1547 return 0;
1548 }
1549 PyErr_SetString(PyExc_AttributeError, name);
1550 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001551}
1552
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001553static int
1554xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1555{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556 int i;
1557 for (i = 0; handler_info[i].name != NULL; i++)
1558 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001559 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001560}
1561
1562static int
1563xmlparse_clear(xmlparseobject *op)
1564{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001565 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001566 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001567 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001568}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001571
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001572static PyMethodDef xmlparse_tp_methods[] = {
1573 {"__dir__", xmlparse_dir, METH_NOARGS}
1574};
1575
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001576static PyTypeObject Xmlparsetype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001577 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001578 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001579 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001580 0, /*tp_itemsize*/
1581 /* methods */
1582 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1583 (printfunc)0, /*tp_print*/
1584 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1585 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1586 (cmpfunc)0, /*tp_compare*/
1587 (reprfunc)0, /*tp_repr*/
1588 0, /*tp_as_number*/
1589 0, /*tp_as_sequence*/
1590 0, /*tp_as_mapping*/
1591 (hashfunc)0, /*tp_hash*/
1592 (ternaryfunc)0, /*tp_call*/
1593 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001594 0, /* tp_getattro */
1595 0, /* tp_setattro */
1596 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001597#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001599#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001600 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001601#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001602 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001603 (traverseproc)xmlparse_traverse, /* tp_traverse */
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001604 (inquiry)xmlparse_clear, /* tp_clear */
1605 0, /* tp_richcompare */
1606 0, /* tp_weaklistoffset */
1607 0, /* tp_iter */
1608 0, /* tp_iternext */
1609 xmlparse_tp_methods /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001610};
1611
1612/* End of code for xmlparser objects */
1613/* -------------------------------------------------------- */
1614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001615PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001616"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001617Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001618
1619static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001620pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1621{
Fred Drakecde79132001-04-25 16:01:30 +00001622 char *encoding = NULL;
1623 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001624 PyObject *intern = NULL;
1625 PyObject *result;
1626 int intern_decref = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001627 static char *kwlist[] = {"encoding", "namespace_separator",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001628 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001629
Fred Drakeb91a36b2002-06-27 19:40:48 +00001630 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1631 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001632 return NULL;
1633 if (namespace_separator != NULL
1634 && strlen(namespace_separator) > 1) {
1635 PyErr_SetString(PyExc_ValueError,
1636 "namespace_separator must be at most one"
1637 " character, omitted, or None");
1638 return NULL;
1639 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001640 /* Explicitly passing None means no interning is desired.
1641 Not passing anything means that a new dictionary is used. */
1642 if (intern == Py_None)
1643 intern = NULL;
1644 else if (intern == NULL) {
1645 intern = PyDict_New();
1646 if (!intern)
1647 return NULL;
1648 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001649 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001650 else if (!PyDict_Check(intern)) {
1651 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1652 return NULL;
1653 }
1654
1655 result = newxmlparseobject(encoding, namespace_separator, intern);
1656 if (intern_decref) {
1657 Py_DECREF(intern);
1658 }
1659 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001660}
1661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001663"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001664Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001665
1666static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001667pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001668{
Fred Drake0582df92000-07-12 04:49:00 +00001669 long code = 0;
1670
1671 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1672 return NULL;
1673 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001674}
1675
1676/* List of methods defined in the module */
1677
1678static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001679 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1680 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1681 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1682 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001683
Fred Drake0582df92000-07-12 04:49:00 +00001684 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001685};
1686
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001687/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001689PyDoc_STRVAR(pyexpat_module_documentation,
1690"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001691
Fred Drake4113b132001-03-24 19:58:26 +00001692/* Return a Python string that represents the version number without the
1693 * extra cruft added by revision control, even if the right options were
1694 * given to the "cvs export" command to make it not include the extra
1695 * cruft.
1696 */
1697static PyObject *
1698get_version_string(void)
1699{
1700 static char *rcsid = "$Revision$";
1701 char *rev = rcsid;
1702 int i = 0;
1703
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001704 while (!isdigit(Py_CHARMASK(*rev)))
Fred Drake4113b132001-03-24 19:58:26 +00001705 ++rev;
1706 while (rev[i] != ' ' && rev[i] != '\0')
1707 ++i;
1708
1709 return PyString_FromStringAndSize(rev, i);
1710}
1711
Fred Drakecde79132001-04-25 16:01:30 +00001712/* Initialization function for the module */
1713
1714#ifndef MODULE_NAME
1715#define MODULE_NAME "pyexpat"
1716#endif
1717
1718#ifndef MODULE_INITFUNC
1719#define MODULE_INITFUNC initpyexpat
1720#endif
1721
Martin v. Löwis069dde22003-01-21 10:58:18 +00001722#ifndef PyMODINIT_FUNC
1723# ifdef MS_WINDOWS
1724# define PyMODINIT_FUNC __declspec(dllexport) void
1725# else
1726# define PyMODINIT_FUNC void
1727# endif
1728#endif
1729
Mark Hammond8235ea12002-07-19 06:55:41 +00001730PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001731
Martin v. Löwis069dde22003-01-21 10:58:18 +00001732PyMODINIT_FUNC
1733MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001734{
1735 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001736 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001737 PyObject *errors_module;
1738 PyObject *modelmod_name;
1739 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001740 PyObject *sys_modules;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001741 static struct PyExpat_CAPI capi;
1742 PyObject* capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001743
Fred Drake6f987622000-08-25 18:03:30 +00001744 if (errmod_name == NULL)
1745 return;
Fred Drakecde79132001-04-25 16:01:30 +00001746 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001747 if (modelmod_name == NULL)
1748 return;
Fred Drake6f987622000-08-25 18:03:30 +00001749
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001750 Py_Type(&Xmlparsetype) = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001751
Fred Drake0582df92000-07-12 04:49:00 +00001752 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001753 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001754 pyexpat_module_documentation);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001755 if (m == NULL)
1756 return;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001757
Fred Drake0582df92000-07-12 04:49:00 +00001758 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001759 if (ErrorObject == NULL) {
1760 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001761 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001762 if (ErrorObject == NULL)
1763 return;
1764 }
1765 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001766 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001767 Py_INCREF(ErrorObject);
1768 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001769 Py_INCREF(&Xmlparsetype);
1770 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001771
Fred Drake4113b132001-03-24 19:58:26 +00001772 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001773 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1774 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001775 {
1776 XML_Expat_Version info = XML_ExpatVersionInfo();
1777 PyModule_AddObject(m, "version_info",
1778 Py_BuildValue("(iii)", info.major,
1779 info.minor, info.micro));
1780 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001781 init_template_buffer();
Fred Drake0582df92000-07-12 04:49:00 +00001782 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001783 compiled, this should check and set native_encoding
1784 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001785 */
Fred Drake93adb692000-09-23 04:55:48 +00001786 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001787
Fred Drake85d835f2001-02-08 15:39:08 +00001788 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001789 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001790 errors_module = PyDict_GetItem(d, errmod_name);
1791 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001792 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001793 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001794 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001795 /* gives away the reference to errors_module */
1796 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001797 }
1798 }
Fred Drake6f987622000-08-25 18:03:30 +00001799 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001800 model_module = PyDict_GetItem(d, modelmod_name);
1801 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001802 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001803 if (model_module != NULL) {
1804 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1805 /* gives away the reference to model_module */
1806 PyModule_AddObject(m, "model", model_module);
1807 }
1808 }
1809 Py_DECREF(modelmod_name);
1810 if (errors_module == NULL || model_module == NULL)
1811 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001812 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001813
Martin v. Löwisc847f402003-01-21 11:09:21 +00001814#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001815 {
1816 const XML_Feature *features = XML_GetFeatureList();
1817 PyObject *list = PyList_New(0);
1818 if (list == NULL)
1819 /* just ignore it */
1820 PyErr_Clear();
1821 else {
1822 int i = 0;
1823 for (; features[i].feature != XML_FEATURE_END; ++i) {
1824 int ok;
1825 PyObject *item = Py_BuildValue("si", features[i].name,
1826 features[i].value);
1827 if (item == NULL) {
1828 Py_DECREF(list);
1829 list = NULL;
1830 break;
1831 }
1832 ok = PyList_Append(list, item);
1833 Py_DECREF(item);
1834 if (ok < 0) {
1835 PyErr_Clear();
1836 break;
1837 }
1838 }
1839 if (list != NULL)
1840 PyModule_AddObject(m, "features", list);
1841 }
1842 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001843#endif
Fred Drake6f987622000-08-25 18:03:30 +00001844
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001845#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001846 PyModule_AddStringConstant(errors_module, #name, \
1847 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001848
Fred Drake0582df92000-07-12 04:49:00 +00001849 MYCONST(XML_ERROR_NO_MEMORY);
1850 MYCONST(XML_ERROR_SYNTAX);
1851 MYCONST(XML_ERROR_NO_ELEMENTS);
1852 MYCONST(XML_ERROR_INVALID_TOKEN);
1853 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1854 MYCONST(XML_ERROR_PARTIAL_CHAR);
1855 MYCONST(XML_ERROR_TAG_MISMATCH);
1856 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1857 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1858 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1859 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1860 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1861 MYCONST(XML_ERROR_ASYNC_ENTITY);
1862 MYCONST(XML_ERROR_BAD_CHAR_REF);
1863 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1864 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1865 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1866 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1867 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001868 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1869 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1870 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001871 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1872 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1873 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1874 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1875 /* Added in Expat 1.95.7. */
1876 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1877 /* Added in Expat 1.95.8. */
1878 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1879 MYCONST(XML_ERROR_INCOMPLETE_PE);
1880 MYCONST(XML_ERROR_XML_DECL);
1881 MYCONST(XML_ERROR_TEXT_DECL);
1882 MYCONST(XML_ERROR_PUBLICID);
1883 MYCONST(XML_ERROR_SUSPENDED);
1884 MYCONST(XML_ERROR_NOT_SUSPENDED);
1885 MYCONST(XML_ERROR_ABORTED);
1886 MYCONST(XML_ERROR_FINISHED);
1887 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001888
Fred Drake85d835f2001-02-08 15:39:08 +00001889 PyModule_AddStringConstant(errors_module, "__doc__",
1890 "Constants used to describe error conditions.");
1891
Fred Drake93adb692000-09-23 04:55:48 +00001892#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001893
Fred Drake85d835f2001-02-08 15:39:08 +00001894#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001895 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1896 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1897 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001898#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001899
Fred Drake85d835f2001-02-08 15:39:08 +00001900#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1901 PyModule_AddStringConstant(model_module, "__doc__",
1902 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001903
Fred Drake85d835f2001-02-08 15:39:08 +00001904 MYCONST(XML_CTYPE_EMPTY);
1905 MYCONST(XML_CTYPE_ANY);
1906 MYCONST(XML_CTYPE_MIXED);
1907 MYCONST(XML_CTYPE_NAME);
1908 MYCONST(XML_CTYPE_CHOICE);
1909 MYCONST(XML_CTYPE_SEQ);
1910
1911 MYCONST(XML_CQUANT_NONE);
1912 MYCONST(XML_CQUANT_OPT);
1913 MYCONST(XML_CQUANT_REP);
1914 MYCONST(XML_CQUANT_PLUS);
1915#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001916
1917 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001918 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001919 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001920 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1921 capi.MINOR_VERSION = XML_MINOR_VERSION;
1922 capi.MICRO_VERSION = XML_MICRO_VERSION;
1923 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001924 capi.GetErrorCode = XML_GetErrorCode;
1925 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1926 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001927 capi.Parse = XML_Parse;
1928 capi.ParserCreate_MM = XML_ParserCreate_MM;
1929 capi.ParserFree = XML_ParserFree;
1930 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1931 capi.SetCommentHandler = XML_SetCommentHandler;
1932 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1933 capi.SetElementHandler = XML_SetElementHandler;
1934 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1935 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1936 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1937 capi.SetUserData = XML_SetUserData;
Fredrik Lundhc3345042005-12-13 19:49:55 +00001938
1939 /* export as cobject */
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001940 capi_object = PyCObject_FromVoidPtr(&capi, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001941 if (capi_object)
1942 PyModule_AddObject(m, "expat_CAPI", capi_object);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001943}
1944
Fred Drake6f987622000-08-25 18:03:30 +00001945static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001946clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001947{
Fred Drakecde79132001-04-25 16:01:30 +00001948 int i = 0;
1949 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001950
Fred Drake71b63ff2002-06-28 22:29:01 +00001951 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001952 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00001953 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001954 else {
Fred Drakecde79132001-04-25 16:01:30 +00001955 temp = self->handlers[i];
1956 self->handlers[i] = NULL;
1957 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001958 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001959 }
Fred Drakecde79132001-04-25 16:01:30 +00001960 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001961}
1962
Tim Peters0c322792002-07-17 16:49:03 +00001963static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001964 {"StartElementHandler",
1965 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001966 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001967 {"EndElementHandler",
1968 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001969 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001970 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001971 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1972 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001973 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001974 (xmlhandlersetter)XML_SetCharacterDataHandler,
1975 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001976 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001977 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001978 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001979 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001980 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001981 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001982 {"StartNamespaceDeclHandler",
1983 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001984 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001985 {"EndNamespaceDeclHandler",
1986 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001987 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001988 {"CommentHandler",
1989 (xmlhandlersetter)XML_SetCommentHandler,
1990 (xmlhandler)my_CommentHandler},
1991 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001992 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001993 (xmlhandler)my_StartCdataSectionHandler},
1994 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001995 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001996 (xmlhandler)my_EndCdataSectionHandler},
1997 {"DefaultHandler",
1998 (xmlhandlersetter)XML_SetDefaultHandler,
1999 (xmlhandler)my_DefaultHandler},
2000 {"DefaultHandlerExpand",
2001 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2002 (xmlhandler)my_DefaultHandlerExpandHandler},
2003 {"NotStandaloneHandler",
2004 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2005 (xmlhandler)my_NotStandaloneHandler},
2006 {"ExternalEntityRefHandler",
2007 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002008 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002009 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002010 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002011 (xmlhandler)my_StartDoctypeDeclHandler},
2012 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002013 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002014 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002015 {"EntityDeclHandler",
2016 (xmlhandlersetter)XML_SetEntityDeclHandler,
2017 (xmlhandler)my_EntityDeclHandler},
2018 {"XmlDeclHandler",
2019 (xmlhandlersetter)XML_SetXmlDeclHandler,
2020 (xmlhandler)my_XmlDeclHandler},
2021 {"ElementDeclHandler",
2022 (xmlhandlersetter)XML_SetElementDeclHandler,
2023 (xmlhandler)my_ElementDeclHandler},
2024 {"AttlistDeclHandler",
2025 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2026 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002027#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002028 {"SkippedEntityHandler",
2029 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2030 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002031#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002032
Fred Drake0582df92000-07-12 04:49:00 +00002033 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002034};