blob: c827581b1d198663e8d598174b748ff14d376a2b [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
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +000025#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
26/* In Python 2.0 and 2.1, disabling Unicode was not possible. */
Martin v. Löwis339d0f72001-08-17 18:39:25 +000027#define Py_USING_UNICODE
Jeremy Hylton9263f572003-06-27 16:13:17 +000028#else
29#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000030#endif
31
Fred Drake0582df92000-07-12 04:49:00 +000032enum HandlerTypes {
33 StartElement,
34 EndElement,
35 ProcessingInstruction,
36 CharacterData,
37 UnparsedEntityDecl,
38 NotationDecl,
39 StartNamespaceDecl,
40 EndNamespaceDecl,
41 Comment,
42 StartCdataSection,
43 EndCdataSection,
44 Default,
45 DefaultHandlerExpand,
46 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000047 ExternalEntityRef,
48 StartDoctypeDecl,
49 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000050 EntityDecl,
51 XmlDecl,
52 ElementDecl,
53 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000054#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000055 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000056#endif
Fred Drake85d835f2001-02-08 15:39:08 +000057 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000058};
59
60static PyObject *ErrorObject;
61
62/* ----------------------------------------------------- */
63
64/* Declarations for objects of type xmlparser */
65
66typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000067 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000068
Fred Drake0582df92000-07-12 04:49:00 +000069 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000070 int returns_unicode; /* True if Unicode strings are returned;
71 if false, UTF-8 strings are returned */
72 int ordered_attributes; /* Return attributes as a list. */
73 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000074 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000075 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000076 XML_Char *buffer; /* Buffer used when accumulating characters */
77 /* NULL if not enabled */
78 int buffer_size; /* Size of buffer, in XML_Char units */
79 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000080 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000081 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000082} xmlparseobject;
83
Fred Drake2a3d7db2002-06-28 22:56:48 +000084#define CHARACTER_DATA_BUFFER_SIZE 8192
85
Jeremy Hylton938ace62002-07-17 16:30:39 +000086static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000087
Fred Drake117ac852002-09-24 16:24:54 +000088typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000089typedef void* xmlhandler;
90
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000091struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000092 const char *name;
93 xmlhandlersetter setter;
94 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000095 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000096 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000097};
98
Jeremy Hylton938ace62002-07-17 16:30:39 +000099static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000100
Fred Drakebd6101c2001-02-14 18:29:45 +0000101/* Set an integer attribute on the error object; return true on success,
102 * false on an exception.
103 */
104static int
105set_error_attr(PyObject *err, char *name, int value)
106{
107 PyObject *v = PyInt_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +0000108
Fred Drakebd6101c2001-02-14 18:29:45 +0000109 if (v != NULL && PyObject_SetAttrString(err, name, v) == -1) {
110 Py_DECREF(v);
111 return 0;
112 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000113 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000114 return 1;
115}
116
117/* Build and set an Expat exception, including positioning
118 * information. Always returns NULL.
119 */
Fred Drake85d835f2001-02-08 15:39:08 +0000120static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000121set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000122{
123 PyObject *err;
124 char buffer[256];
125 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000126 int lineno = XML_GetErrorLineNumber(parser);
127 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000128
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000129 /* There is no risk of overflowing this buffer, since
130 even for 64-bit integers, there is sufficient space. */
131 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000132 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000133 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000134 if ( err != NULL
135 && set_error_attr(err, "code", code)
136 && set_error_attr(err, "offset", column)
137 && set_error_attr(err, "lineno", lineno)) {
138 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000139 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000140 Py_DECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000141 return NULL;
142}
143
Fred Drake71b63ff2002-06-28 22:29:01 +0000144static int
145have_handler(xmlparseobject *self, int type)
146{
147 PyObject *handler = self->handlers[type];
148 return handler != NULL;
149}
150
151static PyObject *
152get_handler_name(struct HandlerInfo *hinfo)
153{
154 PyObject *name = hinfo->nameobj;
155 if (name == NULL) {
156 name = PyString_FromString(hinfo->name);
157 hinfo->nameobj = name;
158 }
159 Py_XINCREF(name);
160 return name;
161}
162
Fred Drake85d835f2001-02-08 15:39:08 +0000163
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000164#ifdef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000165/* Convert a string of XML_Chars into a Unicode string.
166 Returns None if str is a null pointer. */
167
Fred Drake0582df92000-07-12 04:49:00 +0000168static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000169conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000170{
Fred Drake71b63ff2002-06-28 22:29:01 +0000171 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000172 and hence in UTF-8. */
173 /* UTF-8 from Expat, Unicode desired */
174 if (str == NULL) {
175 Py_INCREF(Py_None);
176 return Py_None;
177 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000178 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000179}
180
Fred Drake0582df92000-07-12 04:49:00 +0000181static PyObject *
182conv_string_len_to_unicode(const XML_Char *str, int len)
183{
Fred Drake71b63ff2002-06-28 22:29:01 +0000184 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000185 and hence in UTF-8. */
186 /* UTF-8 from Expat, Unicode desired */
187 if (str == NULL) {
188 Py_INCREF(Py_None);
189 return Py_None;
190 }
Fred Drake6f987622000-08-25 18:03:30 +0000191 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000192}
193#endif
194
195/* Convert a string of XML_Chars into an 8-bit Python string.
196 Returns None if str is a null pointer. */
197
Fred Drake6f987622000-08-25 18:03:30 +0000198static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000199conv_string_to_utf8(const XML_Char *str)
Fred Drake6f987622000-08-25 18:03:30 +0000200{
Fred Drake71b63ff2002-06-28 22:29:01 +0000201 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000202 and hence in UTF-8. */
203 /* UTF-8 from Expat, UTF-8 desired */
204 if (str == NULL) {
205 Py_INCREF(Py_None);
206 return Py_None;
207 }
Fred Drakeb91a36b2002-06-27 19:40:48 +0000208 return PyString_FromString(str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000209}
210
Fred Drake6f987622000-08-25 18:03:30 +0000211static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +0000212conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000213{
Fred Drake71b63ff2002-06-28 22:29:01 +0000214 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake6f987622000-08-25 18:03:30 +0000215 and hence in UTF-8. */
216 /* UTF-8 from Expat, UTF-8 desired */
217 if (str == NULL) {
218 Py_INCREF(Py_None);
219 return Py_None;
220 }
221 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000222}
223
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000224/* Callback routines */
225
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000226static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000227
Martin v. Löwis069dde22003-01-21 10:58:18 +0000228/* This handler is used when an error has been detected, in the hope
229 that actual parsing can be terminated early. This will only help
230 if an external entity reference is encountered. */
231static int
232error_external_entity_ref_handler(XML_Parser parser,
233 const XML_Char *context,
234 const XML_Char *base,
235 const XML_Char *systemId,
236 const XML_Char *publicId)
237{
238 return 0;
239}
240
Fred Drake6f987622000-08-25 18:03:30 +0000241static void
242flag_error(xmlparseobject *self)
243{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000244 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000245 XML_SetExternalEntityRefHandler(self->itself,
246 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000247}
248
249static PyCodeObject*
250getcode(enum HandlerTypes slot, char* func_name, int lineno)
251{
Fred Drakebd6101c2001-02-14 18:29:45 +0000252 PyObject *code = NULL;
253 PyObject *name = NULL;
254 PyObject *nulltuple = NULL;
255 PyObject *filename = NULL;
256
257 if (handler_info[slot].tb_code == NULL) {
258 code = PyString_FromString("");
259 if (code == NULL)
260 goto failed;
261 name = PyString_FromString(func_name);
262 if (name == NULL)
263 goto failed;
264 nulltuple = PyTuple_New(0);
265 if (nulltuple == NULL)
266 goto failed;
267 filename = PyString_FromString(__FILE__);
268 handler_info[slot].tb_code =
269 PyCode_New(0, /* argcount */
270 0, /* nlocals */
271 0, /* stacksize */
272 0, /* flags */
273 code, /* code */
274 nulltuple, /* consts */
275 nulltuple, /* names */
276 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000277#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000278 nulltuple, /* freevars */
279 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000280#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000281 filename, /* filename */
282 name, /* name */
283 lineno, /* firstlineno */
284 code /* lnotab */
285 );
286 if (handler_info[slot].tb_code == NULL)
287 goto failed;
288 Py_DECREF(code);
289 Py_DECREF(nulltuple);
290 Py_DECREF(filename);
291 Py_DECREF(name);
292 }
293 return handler_info[slot].tb_code;
294 failed:
295 Py_XDECREF(code);
296 Py_XDECREF(name);
297 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000298}
299
Jeremy Hylton9263f572003-06-27 16:13:17 +0000300#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000301static int
302trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
303{
304 int result = 0;
305 if (!tstate->use_tracing || tstate->tracing)
306 return 0;
307 if (tstate->c_profilefunc != NULL) {
308 tstate->tracing++;
309 result = tstate->c_profilefunc(tstate->c_profileobj,
310 f, code , val);
311 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
312 || (tstate->c_profilefunc != NULL));
313 tstate->tracing--;
314 if (result)
315 return result;
316 }
317 if (tstate->c_tracefunc != NULL) {
318 tstate->tracing++;
319 result = tstate->c_tracefunc(tstate->c_traceobj,
320 f, code , val);
321 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
322 || (tstate->c_profilefunc != NULL));
323 tstate->tracing--;
324 }
325 return result;
326}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000327
328static int
329trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
330{
331 PyObject *type, *value, *traceback, *arg;
332 int err;
333
334 if (tstate->c_tracefunc == NULL)
335 return 0;
336
337 PyErr_Fetch(&type, &value, &traceback);
338 if (value == NULL) {
339 value = Py_None;
340 Py_INCREF(value);
341 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000342#if PY_VERSION_HEX < 0x02040000
343 arg = Py_BuildValue("(OOO)", type, value, traceback);
344#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000345 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000346#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000347 if (arg == NULL) {
348 PyErr_Restore(type, value, traceback);
349 return 0;
350 }
351 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
352 Py_DECREF(arg);
353 if (err == 0)
354 PyErr_Restore(type, value, traceback);
355 else {
356 Py_XDECREF(type);
357 Py_XDECREF(value);
358 Py_XDECREF(traceback);
359 }
360 return err;
361}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000362#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000363
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000364static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000365call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
366 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000367{
Fred Drakebd6101c2001-02-14 18:29:45 +0000368 PyThreadState *tstate = PyThreadState_GET();
369 PyFrameObject *f;
370 PyObject *res;
371
372 if (c == NULL)
373 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000374
Jeremy Hylton9263f572003-06-27 16:13:17 +0000375 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000376 if (f == NULL)
377 return NULL;
378 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000379#ifdef FIX_TRACE
380 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000381 return NULL;
382 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000383#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000384 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000385 if (res == NULL) {
386 if (tstate->curexc_traceback == NULL)
387 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000388 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000389#ifdef FIX_TRACE
390 if (trace_frame_exc(tstate, f) < 0) {
391 return NULL;
392 }
393 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000394 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000395 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000396 Py_XDECREF(res);
397 res = NULL;
398 }
399 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000400#else
401 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000402#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000403 tstate->frame = f->f_back;
404 Py_DECREF(f);
405 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000406}
407
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000408#ifndef Py_USING_UNICODE
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000409#define STRING_CONV_FUNC conv_string_to_utf8
410#else
Martin v. Löwis069dde22003-01-21 10:58:18 +0000411/* Python 2.0 and later versions, when built with Unicode support */
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000412#define STRING_CONV_FUNC (self->returns_unicode \
413 ? conv_string_to_unicode : conv_string_to_utf8)
414#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000415
Fred Drakeb91a36b2002-06-27 19:40:48 +0000416static PyObject*
417string_intern(xmlparseobject *self, const char* str)
418{
419 PyObject *result = STRING_CONV_FUNC(str);
420 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000421 /* result can be NULL if the unicode conversion failed. */
422 if (!result)
423 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000424 if (!self->intern)
425 return result;
426 value = PyDict_GetItem(self->intern, result);
427 if (!value) {
428 if (PyDict_SetItem(self->intern, result, result) == 0)
429 return result;
430 else
431 return NULL;
432 }
433 Py_INCREF(value);
434 Py_DECREF(result);
435 return value;
436}
437
Fred Drake2a3d7db2002-06-28 22:56:48 +0000438/* Return 0 on success, -1 on exception.
439 * flag_error() will be called before return if needed.
440 */
441static int
442call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
443{
444 PyObject *args;
445 PyObject *temp;
446
447 args = PyTuple_New(1);
448 if (args == NULL)
449 return -1;
450#ifdef Py_USING_UNICODE
451 temp = (self->returns_unicode
452 ? conv_string_len_to_unicode(buffer, len)
453 : conv_string_len_to_utf8(buffer, len));
454#else
455 temp = conv_string_len_to_utf8(buffer, len);
456#endif
457 if (temp == NULL) {
458 Py_DECREF(args);
459 flag_error(self);
460 return -1;
461 }
462 PyTuple_SET_ITEM(args, 0, temp);
463 /* temp is now a borrowed reference; consider it unused. */
464 self->in_callback = 1;
465 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000466 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000467 /* temp is an owned reference again, or NULL */
468 self->in_callback = 0;
469 Py_DECREF(args);
470 if (temp == NULL) {
471 flag_error(self);
472 return -1;
473 }
474 Py_DECREF(temp);
475 return 0;
476}
477
478static int
479flush_character_buffer(xmlparseobject *self)
480{
481 int rc;
482 if (self->buffer == NULL || self->buffer_used == 0)
483 return 0;
484 rc = call_character_handler(self, self->buffer, self->buffer_used);
485 self->buffer_used = 0;
486 return rc;
487}
488
489static void
490my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
491{
492 xmlparseobject *self = (xmlparseobject *) userData;
493 if (self->buffer == NULL)
494 call_character_handler(self, data, len);
495 else {
496 if ((self->buffer_used + len) > self->buffer_size) {
497 if (flush_character_buffer(self) < 0)
498 return;
499 /* handler might have changed; drop the rest on the floor
500 * if there isn't a handler anymore
501 */
502 if (!have_handler(self, CharacterData))
503 return;
504 }
505 if (len > self->buffer_size) {
506 call_character_handler(self, data, len);
507 self->buffer_used = 0;
508 }
509 else {
510 memcpy(self->buffer + self->buffer_used,
511 data, len * sizeof(XML_Char));
512 self->buffer_used += len;
513 }
514 }
515}
516
Fred Drake85d835f2001-02-08 15:39:08 +0000517static void
518my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000519 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000520{
521 xmlparseobject *self = (xmlparseobject *)userData;
522
Fred Drake71b63ff2002-06-28 22:29:01 +0000523 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000524 PyObject *container, *rv, *args;
525 int i, max;
526
Fred Drake2a3d7db2002-06-28 22:56:48 +0000527 if (flush_character_buffer(self) < 0)
528 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000529 /* Set max to the number of slots filled in atts[]; max/2 is
530 * the number of attributes we need to process.
531 */
532 if (self->specified_attributes) {
533 max = XML_GetSpecifiedAttributeCount(self->itself);
534 }
535 else {
536 max = 0;
537 while (atts[max] != NULL)
538 max += 2;
539 }
540 /* Build the container. */
541 if (self->ordered_attributes)
542 container = PyList_New(max);
543 else
544 container = PyDict_New();
545 if (container == NULL) {
546 flag_error(self);
547 return;
548 }
549 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000550 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000551 PyObject *v;
552 if (n == NULL) {
553 flag_error(self);
554 Py_DECREF(container);
555 return;
556 }
557 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
558 if (v == NULL) {
559 flag_error(self);
560 Py_DECREF(container);
561 Py_DECREF(n);
562 return;
563 }
564 if (self->ordered_attributes) {
565 PyList_SET_ITEM(container, i, n);
566 PyList_SET_ITEM(container, i+1, v);
567 }
568 else if (PyDict_SetItem(container, n, v)) {
569 flag_error(self);
570 Py_DECREF(n);
571 Py_DECREF(v);
572 return;
573 }
574 else {
575 Py_DECREF(n);
576 Py_DECREF(v);
577 }
578 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000579 args = string_intern(self, name);
580 if (args != NULL)
581 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000582 if (args == NULL) {
583 Py_DECREF(container);
584 return;
585 }
586 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000587 self->in_callback = 1;
588 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000589 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000590 self->in_callback = 0;
591 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000592 if (rv == NULL) {
593 flag_error(self);
594 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000595 }
Fred Drake85d835f2001-02-08 15:39:08 +0000596 Py_DECREF(rv);
597 }
598}
599
600#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
601 RETURN, GETUSERDATA) \
602static RC \
603my_##NAME##Handler PARAMS {\
604 xmlparseobject *self = GETUSERDATA ; \
605 PyObject *args = NULL; \
606 PyObject *rv = NULL; \
607 INIT \
608\
Fred Drake71b63ff2002-06-28 22:29:01 +0000609 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000610 if (flush_character_buffer(self) < 0) \
611 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000612 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000613 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000614 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000615 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000616 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000617 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000618 Py_DECREF(args); \
619 if (rv == NULL) { \
620 flag_error(self); \
621 return RETURN; \
622 } \
623 CONVERSION \
624 Py_DECREF(rv); \
625 } \
626 return RETURN; \
627}
628
Fred Drake6f987622000-08-25 18:03:30 +0000629#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
630 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
631 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000632
Fred Drake6f987622000-08-25 18:03:30 +0000633#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
634 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
635 rc = PyInt_AsLong(rv);, rc, \
636 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000637
Fred Drake71b63ff2002-06-28 22:29:01 +0000638VOID_HANDLER(EndElement,
639 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000640 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000641
Fred Drake6f987622000-08-25 18:03:30 +0000642VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000643 (void *userData,
644 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000645 const XML_Char *data),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000646 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000647
Fred Drake6f987622000-08-25 18:03:30 +0000648VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000649 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000650 const XML_Char *entityName,
651 const XML_Char *base,
652 const XML_Char *systemId,
653 const XML_Char *publicId,
654 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000655 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000656 string_intern(self, entityName), string_intern(self, base),
657 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000658 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000659
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000660#ifndef Py_USING_UNICODE
Fred Drake85d835f2001-02-08 15:39:08 +0000661VOID_HANDLER(EntityDecl,
662 (void *userData,
663 const XML_Char *entityName,
664 int is_parameter_entity,
665 const XML_Char *value,
666 int value_length,
667 const XML_Char *base,
668 const XML_Char *systemId,
669 const XML_Char *publicId,
670 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000671 ("NiNNNNN",
672 string_intern(self, entityName), is_parameter_entity,
Fred Drake85d835f2001-02-08 15:39:08 +0000673 conv_string_len_to_utf8(value, value_length),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000674 string_intern(self, base), string_intern(self, systemId),
675 string_intern(self, publicId),
676 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000677#else
678VOID_HANDLER(EntityDecl,
679 (void *userData,
680 const XML_Char *entityName,
681 int is_parameter_entity,
682 const XML_Char *value,
683 int value_length,
684 const XML_Char *base,
685 const XML_Char *systemId,
686 const XML_Char *publicId,
687 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000688 ("NiNNNNN",
689 string_intern(self, entityName), is_parameter_entity,
Fred Drake71b63ff2002-06-28 22:29:01 +0000690 (self->returns_unicode
691 ? conv_string_len_to_unicode(value, value_length)
Fred Drake85d835f2001-02-08 15:39:08 +0000692 : conv_string_len_to_utf8(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000693 string_intern(self, base), string_intern(self, systemId),
694 string_intern(self, publicId),
695 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000696#endif
697
698VOID_HANDLER(XmlDecl,
699 (void *userData,
700 const XML_Char *version,
701 const XML_Char *encoding,
702 int standalone),
703 ("(O&O&i)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000704 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000705 standalone))
706
707static PyObject *
708conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000709 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000710{
711 PyObject *result = NULL;
712 PyObject *children = PyTuple_New(model->numchildren);
713 int i;
714
715 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000716 assert(model->numchildren < INT_MAX);
717 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000718 PyObject *child = conv_content_model(&model->children[i],
719 conv_string);
720 if (child == NULL) {
721 Py_XDECREF(children);
722 return NULL;
723 }
724 PyTuple_SET_ITEM(children, i, child);
725 }
726 result = Py_BuildValue("(iiO&N)",
727 model->type, model->quant,
728 conv_string,model->name, children);
729 }
730 return result;
731}
732
Fred Drake06dd8cf2003-02-02 03:54:17 +0000733static void
734my_ElementDeclHandler(void *userData,
735 const XML_Char *name,
736 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000737{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000738 xmlparseobject *self = (xmlparseobject *)userData;
739 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000740
Fred Drake06dd8cf2003-02-02 03:54:17 +0000741 if (have_handler(self, ElementDecl)) {
742 PyObject *rv = NULL;
743 PyObject *modelobj, *nameobj;
744
745 if (flush_character_buffer(self) < 0)
746 goto finally;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000747#ifdef Py_USING_UNICODE
Fred Drake06dd8cf2003-02-02 03:54:17 +0000748 modelobj = conv_content_model(model,
749 (self->returns_unicode
750 ? conv_string_to_unicode
751 : conv_string_to_utf8));
Fred Drake85d835f2001-02-08 15:39:08 +0000752#else
Fred Drake06dd8cf2003-02-02 03:54:17 +0000753 modelobj = conv_content_model(model, conv_string_to_utf8);
Fred Drake85d835f2001-02-08 15:39:08 +0000754#endif
Fred Drake06dd8cf2003-02-02 03:54:17 +0000755 if (modelobj == NULL) {
756 flag_error(self);
757 goto finally;
758 }
759 nameobj = string_intern(self, name);
760 if (nameobj == NULL) {
761 Py_DECREF(modelobj);
762 flag_error(self);
763 goto finally;
764 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000765 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000766 if (args == NULL) {
767 Py_DECREF(modelobj);
768 flag_error(self);
769 goto finally;
770 }
771 self->in_callback = 1;
772 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000773 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000774 self->in_callback = 0;
775 if (rv == NULL) {
776 flag_error(self);
777 goto finally;
778 }
779 Py_DECREF(rv);
780 }
781 finally:
782 Py_XDECREF(args);
783 XML_FreeContentModel(self->itself, model);
784 return;
785}
Fred Drake85d835f2001-02-08 15:39:08 +0000786
787VOID_HANDLER(AttlistDecl,
788 (void *userData,
789 const XML_Char *elname,
790 const XML_Char *attname,
791 const XML_Char *att_type,
792 const XML_Char *dflt,
793 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000794 ("(NNO&O&i)",
795 string_intern(self, elname), string_intern(self, attname),
Fred Drake85d835f2001-02-08 15:39:08 +0000796 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
797 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000798
Martin v. Löwisc847f402003-01-21 11:09:21 +0000799#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000800VOID_HANDLER(SkippedEntity,
801 (void *userData,
802 const XML_Char *entityName,
803 int is_parameter_entity),
804 ("Ni",
805 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000806#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000807
Fred Drake71b63ff2002-06-28 22:29:01 +0000808VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000809 (void *userData,
810 const XML_Char *notationName,
811 const XML_Char *base,
812 const XML_Char *systemId,
813 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000814 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000815 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000816 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000817
Fred Drake6f987622000-08-25 18:03:30 +0000818VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000819 (void *userData,
820 const XML_Char *prefix,
821 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000822 ("(NN)",
823 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000824
Fred Drake6f987622000-08-25 18:03:30 +0000825VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000826 (void *userData,
827 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000828 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829
Fred Drake6f987622000-08-25 18:03:30 +0000830VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000831 (void *userData, const XML_Char *data),
832 ("(O&)", STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833
Fred Drake6f987622000-08-25 18:03:30 +0000834VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000835 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000836 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000837
Fred Drake6f987622000-08-25 18:03:30 +0000838VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000839 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000840 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000841
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000842#ifndef Py_USING_UNICODE
Fred Drake6f987622000-08-25 18:03:30 +0000843VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000844 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000845 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000846
Fred Drake6f987622000-08-25 18:03:30 +0000847VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000848 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000849 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000850#else
Fred Drake6f987622000-08-25 18:03:30 +0000851VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000852 (void *userData, const XML_Char *s, int len),
853 ("(N)", (self->returns_unicode
854 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000855 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000856
Fred Drake6f987622000-08-25 18:03:30 +0000857VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000858 (void *userData, const XML_Char *s, int len),
859 ("(N)", (self->returns_unicode
860 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000861 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000862#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000863
Fred Drake71b63ff2002-06-28 22:29:01 +0000864INT_HANDLER(NotStandalone,
865 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000866 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000867
Fred Drake6f987622000-08-25 18:03:30 +0000868RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000869 (XML_Parser parser,
870 const XML_Char *context,
871 const XML_Char *base,
872 const XML_Char *systemId,
873 const XML_Char *publicId),
874 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000875 ("(O&NNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000876 STRING_CONV_FUNC,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000877 string_intern(self, systemId), string_intern(self, publicId)),
Fred Drake6f987622000-08-25 18:03:30 +0000878 rc = PyInt_AsLong(rv);, rc,
879 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000880
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000881/* XXX UnknownEncodingHandler */
882
Fred Drake85d835f2001-02-08 15:39:08 +0000883VOID_HANDLER(StartDoctypeDecl,
884 (void *userData, const XML_Char *doctypeName,
885 const XML_Char *sysid, const XML_Char *pubid,
886 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000887 ("(NNNi)", string_intern(self, doctypeName),
888 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000889 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000890
891VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000893/* ---------------------------------------------------------------- */
894
Fred Drake71b63ff2002-06-28 22:29:01 +0000895static PyObject *
896get_parse_result(xmlparseobject *self, int rv)
897{
898 if (PyErr_Occurred()) {
899 return NULL;
900 }
901 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000902 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000903 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000904 if (flush_character_buffer(self) < 0) {
905 return NULL;
906 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000907 return PyInt_FromLong(rv);
908}
909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000911"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000913
914static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000915xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000916{
Fred Drake0582df92000-07-12 04:49:00 +0000917 char *s;
918 int slen;
919 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920
Fred Drake0582df92000-07-12 04:49:00 +0000921 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
922 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000923
924 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000925}
926
Fred Drakeca1f4262000-09-21 20:10:23 +0000927/* File reading copied from cPickle */
928
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000929#define BUF_SIZE 2048
930
Fred Drake0582df92000-07-12 04:49:00 +0000931static int
932readinst(char *buf, int buf_size, PyObject *meth)
933{
934 PyObject *arg = NULL;
935 PyObject *bytes = NULL;
936 PyObject *str = NULL;
937 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000938
Fred Drake676940b2000-09-22 15:21:31 +0000939 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000940 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000941
Fred Drake7b6caff2003-07-21 17:05:56 +0000942 if ((arg = PyTuple_New(1)) == NULL) {
943 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000944 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000945 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000946
Tim Peters954eef72000-09-22 06:01:11 +0000947 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000948
Martin v. Löwis9171f022004-10-13 19:50:11 +0000949#if PY_VERSION_HEX < 0x02020000
950 str = PyObject_CallObject(meth, arg);
951#else
952 str = PyObject_Call(meth, arg, NULL);
953#endif
954 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000955 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000956
Fred Drake0582df92000-07-12 04:49:00 +0000957 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000958 if (!PyString_Check(str)) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000959 PyErr_Format(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000960 "read() did not return a string object (type=%.400s)",
961 str->ob_type->tp_name);
962 goto finally;
963 }
964 len = PyString_GET_SIZE(str);
965 if (len > buf_size) {
966 PyErr_Format(PyExc_ValueError,
967 "read() returned too much data: "
968 "%i bytes requested, %i returned",
969 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000970 goto finally;
971 }
972 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000973finally:
Fred Drake0582df92000-07-12 04:49:00 +0000974 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000975 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000976 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000977}
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000980"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000981Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000982
983static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000984xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000985{
Fred Drake0582df92000-07-12 04:49:00 +0000986 int rv = 1;
987 PyObject *f;
988 FILE *fp;
989 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000990
Fred Drake0582df92000-07-12 04:49:00 +0000991 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
992 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000993
Fred Drake0582df92000-07-12 04:49:00 +0000994 if (PyFile_Check(f)) {
995 fp = PyFile_AsFile(f);
996 }
997 else{
998 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000999 readmethod = PyObject_GetAttrString(f, "read");
1000 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001001 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +00001002 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +00001003 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +00001004 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001005 }
1006 }
1007 for (;;) {
1008 int bytes_read;
1009 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +00001010 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +00001011 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001012 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +00001013 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001014
Fred Drake0582df92000-07-12 04:49:00 +00001015 if (fp) {
1016 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
1017 if (bytes_read < 0) {
1018 PyErr_SetFromErrno(PyExc_IOError);
1019 return NULL;
1020 }
1021 }
1022 else {
1023 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +00001024 if (bytes_read < 0) {
1025 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001026 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001027 }
Fred Drake0582df92000-07-12 04:49:00 +00001028 }
1029 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +00001030 if (PyErr_Occurred()) {
1031 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +00001032 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +00001033 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001034
Fred Drake0582df92000-07-12 04:49:00 +00001035 if (!rv || bytes_read == 0)
1036 break;
1037 }
Fred Drake7b6caff2003-07-21 17:05:56 +00001038 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +00001039 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001040}
1041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001043"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001044Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001045
1046static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001047xmlparse_SetBase(xmlparseobject *self, PyObject *args)
1048{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001049 char *base;
1050
Fred Drake0582df92000-07-12 04:49:00 +00001051 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001052 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001053 if (!XML_SetBase(self->itself, base)) {
1054 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001055 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001056 Py_INCREF(Py_None);
1057 return Py_None;
1058}
1059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001060PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +00001061"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001062Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001063
1064static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001065xmlparse_GetBase(xmlparseobject *self, PyObject *args)
1066{
1067 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001068 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001069
Fred Drake0582df92000-07-12 04:49:00 +00001070 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001071}
1072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001073PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +00001074"GetInputContext() -> string\n\
1075Return the untranslated text of the input that caused the current event.\n\
1076If 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 +00001077for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +00001078
1079static PyObject *
1080xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
1081{
1082 PyObject *result = NULL;
1083
1084 if (PyArg_ParseTuple(args, ":GetInputContext")) {
1085 if (self->in_callback) {
1086 int offset, size;
1087 const char *buffer
1088 = XML_GetInputContext(self->itself, &offset, &size);
1089
1090 if (buffer != NULL)
Martin v. Löwisfd78a6f2005-03-04 14:37:01 +00001091 result = PyString_FromStringAndSize(buffer + offset, size - offset);
Fred Drakebd6101c2001-02-14 18:29:45 +00001092 else {
1093 result = Py_None;
1094 Py_INCREF(result);
1095 }
1096 }
1097 else {
1098 result = Py_None;
1099 Py_INCREF(result);
1100 }
1101 }
1102 return result;
1103}
Fred Drakebd6101c2001-02-14 18:29:45 +00001104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001106"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001107Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001108information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001109
1110static PyObject *
1111xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1112{
1113 char *context;
1114 char *encoding = NULL;
1115 xmlparseobject *new_parser;
1116 int i;
1117
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001118 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001119 &context, &encoding)) {
1120 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001121 }
1122
Martin v. Löwis894258c2001-09-23 10:20:10 +00001123#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001124 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001125 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001126#else
1127 /* Python versions 2.2 and later */
1128 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1129#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001130
1131 if (new_parser == NULL)
1132 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001133 new_parser->buffer_size = self->buffer_size;
1134 new_parser->buffer_used = 0;
1135 if (self->buffer != NULL) {
1136 new_parser->buffer = malloc(new_parser->buffer_size);
1137 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001138#ifndef Py_TPFLAGS_HAVE_GC
1139 /* Code for versions 2.0 and 2.1 */
1140 PyObject_Del(new_parser);
1141#else
1142 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001143 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001144#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001145 return PyErr_NoMemory();
1146 }
1147 }
1148 else
1149 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001150 new_parser->returns_unicode = self->returns_unicode;
1151 new_parser->ordered_attributes = self->ordered_attributes;
1152 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001153 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001154 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001155 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001156 encoding);
1157 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001158 new_parser->intern = self->intern;
1159 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001160#ifdef Py_TPFLAGS_HAVE_GC
1161 PyObject_GC_Track(new_parser);
1162#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001163 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001164#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001165
1166 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001167 Py_DECREF(new_parser);
1168 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001169 }
1170
1171 XML_SetUserData(new_parser->itself, (void *)new_parser);
1172
1173 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001174 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001175 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001176
Fred Drake2a3d7db2002-06-28 22:56:48 +00001177 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001178 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001179 Py_DECREF(new_parser);
1180 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001181 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001182 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001183
1184 /* then copy handlers from self */
1185 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001186 PyObject *handler = self->handlers[i];
1187 if (handler != NULL) {
1188 Py_INCREF(handler);
1189 new_parser->handlers[i] = handler;
1190 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001191 handler_info[i].handler);
1192 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001193 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001194 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001195}
1196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001197PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001198"SetParamEntityParsing(flag) -> success\n\
1199Controls parsing of parameter entities (including the external DTD\n\
1200subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1201XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1202XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001203was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001204
1205static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001206xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001207{
Fred Drake85d835f2001-02-08 15:39:08 +00001208 int flag;
1209 if (!PyArg_ParseTuple(args, "i", &flag))
1210 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001211 flag = XML_SetParamEntityParsing(p->itself, flag);
Fred Drake85d835f2001-02-08 15:39:08 +00001212 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001213}
1214
Martin v. Löwisc847f402003-01-21 11:09:21 +00001215
1216#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001217PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1218"UseForeignDTD([flag])\n\
1219Allows the application to provide an artificial external subset if one is\n\
1220not specified as part of the document instance. This readily allows the\n\
1221use of a 'default' document type controlled by the application, while still\n\
1222getting the advantage of providing document type information to the parser.\n\
1223'flag' defaults to True if not provided.");
1224
1225static PyObject *
1226xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1227{
1228 PyObject *flagobj = NULL;
1229 XML_Bool flag = XML_TRUE;
1230 enum XML_Error rc;
1231 if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj))
1232 return NULL;
1233 if (flagobj != NULL)
1234 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1235 rc = XML_UseForeignDTD(self->itself, flag);
1236 if (rc != XML_ERROR_NONE) {
1237 return set_error(self, rc);
1238 }
1239 Py_INCREF(Py_None);
1240 return Py_None;
1241}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001242#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001243
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001244static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001245 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001246 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001247 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Fred Drakebd6101c2001-02-14 18:29:45 +00001248 METH_VARARGS, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001249 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001250 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001251 {"GetBase", (PyCFunction)xmlparse_GetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001252 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001253 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001254 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001255 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1256 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001257 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1258 METH_VARARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001259#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001260 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1261 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001262#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001263 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001264};
1265
1266/* ---------- */
1267
1268
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001269#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001270
Fred Drake71b63ff2002-06-28 22:29:01 +00001271/* pyexpat international encoding support.
1272 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001273*/
1274
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001275static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001276PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001277
Fred Drake71b63ff2002-06-28 22:29:01 +00001278static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001279init_template_buffer(void)
1280{
1281 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001282 for (i = 0; i < 256; i++) {
1283 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001284 }
Fred Drakebb66a202001-03-01 20:48:17 +00001285 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001286}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001287
Fred Drake71b63ff2002-06-28 22:29:01 +00001288static int
1289PyUnknownEncodingHandler(void *encodingHandlerData,
1290 const XML_Char *name,
1291 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001292{
Fred Drakebb66a202001-03-01 20:48:17 +00001293 PyUnicodeObject *_u_string = NULL;
1294 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001295 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001296
Fred Drakebb66a202001-03-01 20:48:17 +00001297 /* Yes, supports only 8bit encodings */
1298 _u_string = (PyUnicodeObject *)
1299 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001300
Fred Drakebb66a202001-03-01 20:48:17 +00001301 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001302 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001303
Fred Drakebb66a202001-03-01 20:48:17 +00001304 for (i = 0; i < 256; i++) {
1305 /* Stupid to access directly, but fast */
1306 Py_UNICODE c = _u_string->str[i];
1307 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001308 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001309 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001310 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001311 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001312 info->data = NULL;
1313 info->convert = NULL;
1314 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001315 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001316 Py_DECREF(_u_string);
1317 return result;
1318}
1319
1320#endif
1321
1322static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001323newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001324{
1325 int i;
1326 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001327
Martin v. Löwis894258c2001-09-23 10:20:10 +00001328#ifdef Py_TPFLAGS_HAVE_GC
1329 /* Code for versions 2.2 and later */
1330 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1331#else
Fred Drake0582df92000-07-12 04:49:00 +00001332 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001333#endif
Fred Drake0582df92000-07-12 04:49:00 +00001334 if (self == NULL)
1335 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001336
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001337#ifdef Py_USING_UNICODE
Fred Drake0582df92000-07-12 04:49:00 +00001338 self->returns_unicode = 1;
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001339#else
1340 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001341#endif
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001342
Fred Drake2a3d7db2002-06-28 22:56:48 +00001343 self->buffer = NULL;
1344 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1345 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001346 self->ordered_attributes = 0;
1347 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001348 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001349 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001350 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001351 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001352 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1353 }
Fred Drake85d835f2001-02-08 15:39:08 +00001354 else {
Fred Drake0582df92000-07-12 04:49:00 +00001355 self->itself = XML_ParserCreate(encoding);
1356 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001357 self->intern = intern;
1358 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001359#ifdef Py_TPFLAGS_HAVE_GC
1360 PyObject_GC_Track(self);
1361#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001362 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001363#endif
Fred Drake0582df92000-07-12 04:49:00 +00001364 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001365 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001366 "XML_ParserCreate failed");
1367 Py_DECREF(self);
1368 return NULL;
1369 }
1370 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001371#ifdef Py_USING_UNICODE
Fred Drake7c75bf22002-07-01 14:02:31 +00001372 XML_SetUnknownEncodingHandler(self->itself,
1373 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001374#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001375
Fred Drake2a3d7db2002-06-28 22:56:48 +00001376 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001377 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001378
Fred Drake7c75bf22002-07-01 14:02:31 +00001379 self->handlers = malloc(sizeof(PyObject *) * i);
1380 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001381 Py_DECREF(self);
1382 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001383 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001384 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001386 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001387}
1388
1389
1390static void
Fred Drake0582df92000-07-12 04:49:00 +00001391xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001392{
Fred Drake0582df92000-07-12 04:49:00 +00001393 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001394#ifdef Py_TPFLAGS_HAVE_GC
1395 PyObject_GC_UnTrack(self);
1396#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001397 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001398#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001399 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001400 XML_ParserFree(self->itself);
1401 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001402
Fred Drake85d835f2001-02-08 15:39:08 +00001403 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001404 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001405 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001406 temp = self->handlers[i];
1407 self->handlers[i] = NULL;
1408 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001409 }
1410 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001411 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001412 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001413 if (self->buffer != NULL) {
1414 free(self->buffer);
1415 self->buffer = NULL;
1416 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001417 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001418#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001419 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001420 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001421#else
1422 /* Code for versions 2.2 and later. */
1423 PyObject_GC_Del(self);
1424#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001425}
1426
Fred Drake0582df92000-07-12 04:49:00 +00001427static int
1428handlername2int(const char *name)
1429{
1430 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001431 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001432 if (strcmp(name, handler_info[i].name) == 0) {
1433 return i;
1434 }
1435 }
1436 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001437}
1438
1439static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001440get_pybool(int istrue)
1441{
1442 PyObject *result = istrue ? Py_True : Py_False;
1443 Py_INCREF(result);
1444 return result;
1445}
1446
1447static PyObject *
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001448xmlparse_getattr(xmlparseobject *self, char *name)
1449{
Fred Drake71b63ff2002-06-28 22:29:01 +00001450 int handlernum = handlername2int(name);
1451
1452 if (handlernum != -1) {
1453 PyObject *result = self->handlers[handlernum];
1454 if (result == NULL)
1455 result = Py_None;
1456 Py_INCREF(result);
1457 return result;
1458 }
1459 if (name[0] == 'E') {
1460 if (strcmp(name, "ErrorCode") == 0)
1461 return PyInt_FromLong((long)
1462 XML_GetErrorCode(self->itself));
1463 if (strcmp(name, "ErrorLineNumber") == 0)
1464 return PyInt_FromLong((long)
1465 XML_GetErrorLineNumber(self->itself));
1466 if (strcmp(name, "ErrorColumnNumber") == 0)
1467 return PyInt_FromLong((long)
1468 XML_GetErrorColumnNumber(self->itself));
1469 if (strcmp(name, "ErrorByteIndex") == 0)
1470 return PyInt_FromLong((long)
1471 XML_GetErrorByteIndex(self->itself));
1472 }
Dave Cole3203efb2004-08-26 00:37:31 +00001473 if (name[0] == 'C') {
1474 if (strcmp(name, "CurrentLineNumber") == 0)
1475 return PyInt_FromLong((long)
1476 XML_GetCurrentLineNumber(self->itself));
1477 if (strcmp(name, "CurrentColumnNumber") == 0)
1478 return PyInt_FromLong((long)
1479 XML_GetCurrentColumnNumber(self->itself));
1480 if (strcmp(name, "CurrentByteIndex") == 0)
1481 return PyInt_FromLong((long)
1482 XML_GetCurrentByteIndex(self->itself));
1483 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001484 if (name[0] == 'b') {
1485 if (strcmp(name, "buffer_size") == 0)
1486 return PyInt_FromLong((long) self->buffer_size);
1487 if (strcmp(name, "buffer_text") == 0)
1488 return get_pybool(self->buffer != NULL);
1489 if (strcmp(name, "buffer_used") == 0)
1490 return PyInt_FromLong((long) self->buffer_used);
1491 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001492 if (strcmp(name, "namespace_prefixes") == 0)
1493 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001494 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001495 return get_pybool(self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001496 if (strcmp(name, "returns_unicode") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001497 return get_pybool((long) self->returns_unicode);
Fred Drake85d835f2001-02-08 15:39:08 +00001498 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001499 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001500 if (strcmp(name, "intern") == 0) {
1501 if (self->intern == NULL) {
1502 Py_INCREF(Py_None);
1503 return Py_None;
1504 }
1505 else {
1506 Py_INCREF(self->intern);
1507 return self->intern;
1508 }
1509 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001510
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001511#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001512 do { \
1513 PyObject *o = PyString_FromString(str); \
1514 if (o != NULL) \
1515 PyList_Append(list, o); \
1516 Py_XDECREF(o); \
1517 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001518
Fred Drake0582df92000-07-12 04:49:00 +00001519 if (strcmp(name, "__members__") == 0) {
1520 int i;
1521 PyObject *rc = PyList_New(0);
Fred Drake71b63ff2002-06-28 22:29:01 +00001522 for (i = 0; handler_info[i].name != NULL; i++) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001523 PyObject *o = get_handler_name(&handler_info[i]);
1524 if (o != NULL)
1525 PyList_Append(rc, o);
1526 Py_XDECREF(o);
Fred Drake0582df92000-07-12 04:49:00 +00001527 }
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001528 APPEND(rc, "ErrorCode");
1529 APPEND(rc, "ErrorLineNumber");
1530 APPEND(rc, "ErrorColumnNumber");
1531 APPEND(rc, "ErrorByteIndex");
Dave Cole3203efb2004-08-26 00:37:31 +00001532 APPEND(rc, "CurrentLineNumber");
1533 APPEND(rc, "CurrentColumnNumber");
1534 APPEND(rc, "CurrentByteIndex");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001535 APPEND(rc, "buffer_size");
1536 APPEND(rc, "buffer_text");
1537 APPEND(rc, "buffer_used");
Martin v. Löwis069dde22003-01-21 10:58:18 +00001538 APPEND(rc, "namespace_prefixes");
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001539 APPEND(rc, "ordered_attributes");
1540 APPEND(rc, "returns_unicode");
1541 APPEND(rc, "specified_attributes");
1542 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001543
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001544#undef APPEND
Fred Drake0582df92000-07-12 04:49:00 +00001545 return rc;
1546 }
1547 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001548}
1549
Fred Drake6f987622000-08-25 18:03:30 +00001550static int
1551sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001552{
1553 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001554 if (handlernum >= 0) {
1555 xmlhandler c_handler = NULL;
1556 PyObject *temp = self->handlers[handlernum];
1557
1558 if (v == Py_None)
1559 v = NULL;
1560 else if (v != NULL) {
1561 Py_INCREF(v);
1562 c_handler = handler_info[handlernum].handler;
1563 }
Fred Drake0582df92000-07-12 04:49:00 +00001564 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001565 Py_XDECREF(temp);
1566 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001567 return 1;
1568 }
1569 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570}
1571
1572static int
Fred Drake6f987622000-08-25 18:03:30 +00001573xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001574{
Fred Drake6f987622000-08-25 18:03:30 +00001575 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001576 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001577 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1578 return -1;
1579 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001580 if (strcmp(name, "buffer_text") == 0) {
1581 if (PyObject_IsTrue(v)) {
1582 if (self->buffer == NULL) {
1583 self->buffer = malloc(self->buffer_size);
1584 if (self->buffer == NULL) {
1585 PyErr_NoMemory();
1586 return -1;
1587 }
1588 self->buffer_used = 0;
1589 }
1590 }
1591 else if (self->buffer != NULL) {
1592 if (flush_character_buffer(self) < 0)
1593 return -1;
1594 free(self->buffer);
1595 self->buffer = NULL;
1596 }
1597 return 0;
1598 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001599 if (strcmp(name, "namespace_prefixes") == 0) {
1600 if (PyObject_IsTrue(v))
1601 self->ns_prefixes = 1;
1602 else
1603 self->ns_prefixes = 0;
1604 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1605 return 0;
1606 }
Fred Drake85d835f2001-02-08 15:39:08 +00001607 if (strcmp(name, "ordered_attributes") == 0) {
1608 if (PyObject_IsTrue(v))
1609 self->ordered_attributes = 1;
1610 else
1611 self->ordered_attributes = 0;
1612 return 0;
1613 }
Fred Drake6f987622000-08-25 18:03:30 +00001614 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001615 if (PyObject_IsTrue(v)) {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001616#ifndef Py_USING_UNICODE
Fred Drake71b63ff2002-06-28 22:29:01 +00001617 PyErr_SetString(PyExc_ValueError,
1618 "Unicode support not available");
Fred Drake6f987622000-08-25 18:03:30 +00001619 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001620#else
Fred Drake6f987622000-08-25 18:03:30 +00001621 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001622#endif
Fred Drake6f987622000-08-25 18:03:30 +00001623 }
1624 else
1625 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001626 return 0;
1627 }
1628 if (strcmp(name, "specified_attributes") == 0) {
1629 if (PyObject_IsTrue(v))
1630 self->specified_attributes = 1;
1631 else
1632 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001633 return 0;
1634 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001635 if (strcmp(name, "CharacterDataHandler") == 0) {
1636 /* If we're changing the character data handler, flush all
1637 * cached data with the old handler. Not sure there's a
1638 * "right" thing to do, though, but this probably won't
1639 * happen.
1640 */
1641 if (flush_character_buffer(self) < 0)
1642 return -1;
1643 }
Fred Drake6f987622000-08-25 18:03:30 +00001644 if (sethandler(self, name, v)) {
1645 return 0;
1646 }
1647 PyErr_SetString(PyExc_AttributeError, name);
1648 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001649}
1650
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001651#ifdef WITH_CYCLE_GC
1652static int
1653xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1654{
Fred Drakecde79132001-04-25 16:01:30 +00001655 int i, err;
1656 for (i = 0; handler_info[i].name != NULL; i++) {
1657 if (!op->handlers[i])
1658 continue;
1659 err = visit(op->handlers[i], arg);
1660 if (err)
1661 return err;
1662 }
1663 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001664}
1665
1666static int
1667xmlparse_clear(xmlparseobject *op)
1668{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001669 clear_handlers(op, 0);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001670 Py_XDECREF(op->intern);
1671 op->intern = 0;
Fred Drakecde79132001-04-25 16:01:30 +00001672 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001673}
1674#endif
1675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001676PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001677
1678static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001679 PyObject_HEAD_INIT(NULL)
1680 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00001681 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001682 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001683 0, /*tp_itemsize*/
1684 /* methods */
1685 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1686 (printfunc)0, /*tp_print*/
1687 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1688 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1689 (cmpfunc)0, /*tp_compare*/
1690 (reprfunc)0, /*tp_repr*/
1691 0, /*tp_as_number*/
1692 0, /*tp_as_sequence*/
1693 0, /*tp_as_mapping*/
1694 (hashfunc)0, /*tp_hash*/
1695 (ternaryfunc)0, /*tp_call*/
1696 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001697 0, /* tp_getattro */
1698 0, /* tp_setattro */
1699 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001700#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001702#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001703 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001704#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001705 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001706#ifdef WITH_CYCLE_GC
1707 (traverseproc)xmlparse_traverse, /* tp_traverse */
1708 (inquiry)xmlparse_clear /* tp_clear */
1709#else
1710 0, 0
1711#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001712};
1713
1714/* End of code for xmlparser objects */
1715/* -------------------------------------------------------- */
1716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001718"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001719Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001720
1721static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001722pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1723{
Fred Drakecde79132001-04-25 16:01:30 +00001724 char *encoding = NULL;
1725 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001726 PyObject *intern = NULL;
1727 PyObject *result;
1728 int intern_decref = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001729 static const char *kwlist[] = {"encoding", "namespace_separator",
1730 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001731
Fred Drakeb91a36b2002-06-27 19:40:48 +00001732 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1733 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001734 return NULL;
1735 if (namespace_separator != NULL
1736 && strlen(namespace_separator) > 1) {
1737 PyErr_SetString(PyExc_ValueError,
1738 "namespace_separator must be at most one"
1739 " character, omitted, or None");
1740 return NULL;
1741 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001742 /* Explicitly passing None means no interning is desired.
1743 Not passing anything means that a new dictionary is used. */
1744 if (intern == Py_None)
1745 intern = NULL;
1746 else if (intern == NULL) {
1747 intern = PyDict_New();
1748 if (!intern)
1749 return NULL;
1750 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001751 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001752 else if (!PyDict_Check(intern)) {
1753 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1754 return NULL;
1755 }
1756
1757 result = newxmlparseobject(encoding, namespace_separator, intern);
1758 if (intern_decref) {
1759 Py_DECREF(intern);
1760 }
1761 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001762}
1763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001764PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001765"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001766Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001767
1768static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001769pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001770{
Fred Drake0582df92000-07-12 04:49:00 +00001771 long code = 0;
1772
1773 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1774 return NULL;
1775 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001776}
1777
1778/* List of methods defined in the module */
1779
1780static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001781 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1782 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1783 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1784 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001785
Fred Drake0582df92000-07-12 04:49:00 +00001786 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001787};
1788
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001789/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791PyDoc_STRVAR(pyexpat_module_documentation,
1792"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001793
Fred Drake4113b132001-03-24 19:58:26 +00001794/* Return a Python string that represents the version number without the
1795 * extra cruft added by revision control, even if the right options were
1796 * given to the "cvs export" command to make it not include the extra
1797 * cruft.
1798 */
1799static PyObject *
1800get_version_string(void)
1801{
1802 static char *rcsid = "$Revision$";
1803 char *rev = rcsid;
1804 int i = 0;
1805
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001806 while (!isdigit(Py_CHARMASK(*rev)))
Fred Drake4113b132001-03-24 19:58:26 +00001807 ++rev;
1808 while (rev[i] != ' ' && rev[i] != '\0')
1809 ++i;
1810
1811 return PyString_FromStringAndSize(rev, i);
1812}
1813
Fred Drakecde79132001-04-25 16:01:30 +00001814/* Initialization function for the module */
1815
1816#ifndef MODULE_NAME
1817#define MODULE_NAME "pyexpat"
1818#endif
1819
1820#ifndef MODULE_INITFUNC
1821#define MODULE_INITFUNC initpyexpat
1822#endif
1823
Martin v. Löwis069dde22003-01-21 10:58:18 +00001824#ifndef PyMODINIT_FUNC
1825# ifdef MS_WINDOWS
1826# define PyMODINIT_FUNC __declspec(dllexport) void
1827# else
1828# define PyMODINIT_FUNC void
1829# endif
1830#endif
1831
Mark Hammond8235ea12002-07-19 06:55:41 +00001832PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001833
Martin v. Löwis069dde22003-01-21 10:58:18 +00001834PyMODINIT_FUNC
1835MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001836{
1837 PyObject *m, *d;
Fred Drakecde79132001-04-25 16:01:30 +00001838 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001839 PyObject *errors_module;
1840 PyObject *modelmod_name;
1841 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001842 PyObject *sys_modules;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001843 static struct PyExpat_CAPI capi;
1844 PyObject* capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001845
Fred Drake6f987622000-08-25 18:03:30 +00001846 if (errmod_name == NULL)
1847 return;
Fred Drakecde79132001-04-25 16:01:30 +00001848 modelmod_name = PyString_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001849 if (modelmod_name == NULL)
1850 return;
Fred Drake6f987622000-08-25 18:03:30 +00001851
Fred Drake0582df92000-07-12 04:49:00 +00001852 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001853
Fred Drake0582df92000-07-12 04:49:00 +00001854 /* Create the module and add the functions */
Fred Drakecde79132001-04-25 16:01:30 +00001855 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
Fred Drake85d835f2001-02-08 15:39:08 +00001856 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001857
Fred Drake0582df92000-07-12 04:49:00 +00001858 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001859 if (ErrorObject == NULL) {
1860 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001861 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001862 if (ErrorObject == NULL)
1863 return;
1864 }
1865 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001866 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001867 Py_INCREF(ErrorObject);
1868 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001869 Py_INCREF(&Xmlparsetype);
1870 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001871
Fred Drake4113b132001-03-24 19:58:26 +00001872 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001873 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1874 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001875 {
1876 XML_Expat_Version info = XML_ExpatVersionInfo();
1877 PyModule_AddObject(m, "version_info",
1878 Py_BuildValue("(iii)", info.major,
1879 info.minor, info.micro));
1880 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001881#ifdef Py_USING_UNICODE
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001882 init_template_buffer();
1883#endif
Fred Drake0582df92000-07-12 04:49:00 +00001884 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001885 compiled, this should check and set native_encoding
1886 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001887 */
Fred Drake93adb692000-09-23 04:55:48 +00001888 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001889
Fred Drake85d835f2001-02-08 15:39:08 +00001890 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001891 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001892 errors_module = PyDict_GetItem(d, errmod_name);
1893 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001894 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001895 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001896 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001897 /* gives away the reference to errors_module */
1898 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001899 }
1900 }
Fred Drake6f987622000-08-25 18:03:30 +00001901 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001902 model_module = PyDict_GetItem(d, modelmod_name);
1903 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001904 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001905 if (model_module != NULL) {
1906 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1907 /* gives away the reference to model_module */
1908 PyModule_AddObject(m, "model", model_module);
1909 }
1910 }
1911 Py_DECREF(modelmod_name);
1912 if (errors_module == NULL || model_module == NULL)
1913 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001914 return;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001915
Martin v. Löwisc847f402003-01-21 11:09:21 +00001916#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001917 {
1918 const XML_Feature *features = XML_GetFeatureList();
1919 PyObject *list = PyList_New(0);
1920 if (list == NULL)
1921 /* just ignore it */
1922 PyErr_Clear();
1923 else {
1924 int i = 0;
1925 for (; features[i].feature != XML_FEATURE_END; ++i) {
1926 int ok;
1927 PyObject *item = Py_BuildValue("si", features[i].name,
1928 features[i].value);
1929 if (item == NULL) {
1930 Py_DECREF(list);
1931 list = NULL;
1932 break;
1933 }
1934 ok = PyList_Append(list, item);
1935 Py_DECREF(item);
1936 if (ok < 0) {
1937 PyErr_Clear();
1938 break;
1939 }
1940 }
1941 if (list != NULL)
1942 PyModule_AddObject(m, "features", list);
1943 }
1944 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001945#endif
Fred Drake6f987622000-08-25 18:03:30 +00001946
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001947#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001948 PyModule_AddStringConstant(errors_module, #name, \
1949 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001950
Fred Drake0582df92000-07-12 04:49:00 +00001951 MYCONST(XML_ERROR_NO_MEMORY);
1952 MYCONST(XML_ERROR_SYNTAX);
1953 MYCONST(XML_ERROR_NO_ELEMENTS);
1954 MYCONST(XML_ERROR_INVALID_TOKEN);
1955 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1956 MYCONST(XML_ERROR_PARTIAL_CHAR);
1957 MYCONST(XML_ERROR_TAG_MISMATCH);
1958 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1959 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1960 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1961 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1962 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1963 MYCONST(XML_ERROR_ASYNC_ENTITY);
1964 MYCONST(XML_ERROR_BAD_CHAR_REF);
1965 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1966 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1967 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1968 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1969 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001970 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1971 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1972 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001973 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1974 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1975 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1976 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1977 /* Added in Expat 1.95.7. */
1978 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1979 /* Added in Expat 1.95.8. */
1980 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1981 MYCONST(XML_ERROR_INCOMPLETE_PE);
1982 MYCONST(XML_ERROR_XML_DECL);
1983 MYCONST(XML_ERROR_TEXT_DECL);
1984 MYCONST(XML_ERROR_PUBLICID);
1985 MYCONST(XML_ERROR_SUSPENDED);
1986 MYCONST(XML_ERROR_NOT_SUSPENDED);
1987 MYCONST(XML_ERROR_ABORTED);
1988 MYCONST(XML_ERROR_FINISHED);
1989 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001990
Fred Drake85d835f2001-02-08 15:39:08 +00001991 PyModule_AddStringConstant(errors_module, "__doc__",
1992 "Constants used to describe error conditions.");
1993
Fred Drake93adb692000-09-23 04:55:48 +00001994#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001995
Fred Drake85d835f2001-02-08 15:39:08 +00001996#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001997 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1998 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1999 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00002000#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002001
Fred Drake85d835f2001-02-08 15:39:08 +00002002#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
2003 PyModule_AddStringConstant(model_module, "__doc__",
2004 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002005
Fred Drake85d835f2001-02-08 15:39:08 +00002006 MYCONST(XML_CTYPE_EMPTY);
2007 MYCONST(XML_CTYPE_ANY);
2008 MYCONST(XML_CTYPE_MIXED);
2009 MYCONST(XML_CTYPE_NAME);
2010 MYCONST(XML_CTYPE_CHOICE);
2011 MYCONST(XML_CTYPE_SEQ);
2012
2013 MYCONST(XML_CQUANT_NONE);
2014 MYCONST(XML_CQUANT_OPT);
2015 MYCONST(XML_CQUANT_REP);
2016 MYCONST(XML_CQUANT_PLUS);
2017#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00002018
2019 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002020 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00002021 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002022 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
2023 capi.MINOR_VERSION = XML_MINOR_VERSION;
2024 capi.MICRO_VERSION = XML_MICRO_VERSION;
2025 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00002026 capi.GetErrorCode = XML_GetErrorCode;
2027 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
2028 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002029 capi.Parse = XML_Parse;
2030 capi.ParserCreate_MM = XML_ParserCreate_MM;
2031 capi.ParserFree = XML_ParserFree;
2032 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
2033 capi.SetCommentHandler = XML_SetCommentHandler;
2034 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
2035 capi.SetElementHandler = XML_SetElementHandler;
2036 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
2037 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
2038 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
2039 capi.SetUserData = XML_SetUserData;
Fredrik Lundhc3345042005-12-13 19:49:55 +00002040
2041 /* export as cobject */
Fredrik Lundhcc117db2005-12-13 21:55:36 +00002042 capi_object = PyCObject_FromVoidPtr(&capi, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002043 if (capi_object)
2044 PyModule_AddObject(m, "expat_CAPI", capi_object);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002045}
2046
Fred Drake6f987622000-08-25 18:03:30 +00002047static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002048clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00002049{
Fred Drakecde79132001-04-25 16:01:30 +00002050 int i = 0;
2051 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002052
Fred Drake71b63ff2002-06-28 22:29:01 +00002053 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002054 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00002055 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002056 else {
Fred Drakecde79132001-04-25 16:01:30 +00002057 temp = self->handlers[i];
2058 self->handlers[i] = NULL;
2059 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002060 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002061 }
Fred Drakecde79132001-04-25 16:01:30 +00002062 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002063}
2064
Tim Peters0c322792002-07-17 16:49:03 +00002065static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002066 {"StartElementHandler",
2067 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002068 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002069 {"EndElementHandler",
2070 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002071 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002072 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002073 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2074 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002075 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002076 (xmlhandlersetter)XML_SetCharacterDataHandler,
2077 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002078 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002079 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002080 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002081 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002082 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002083 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002084 {"StartNamespaceDeclHandler",
2085 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002086 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002087 {"EndNamespaceDeclHandler",
2088 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002089 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002090 {"CommentHandler",
2091 (xmlhandlersetter)XML_SetCommentHandler,
2092 (xmlhandler)my_CommentHandler},
2093 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002094 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002095 (xmlhandler)my_StartCdataSectionHandler},
2096 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002097 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002098 (xmlhandler)my_EndCdataSectionHandler},
2099 {"DefaultHandler",
2100 (xmlhandlersetter)XML_SetDefaultHandler,
2101 (xmlhandler)my_DefaultHandler},
2102 {"DefaultHandlerExpand",
2103 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2104 (xmlhandler)my_DefaultHandlerExpandHandler},
2105 {"NotStandaloneHandler",
2106 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2107 (xmlhandler)my_NotStandaloneHandler},
2108 {"ExternalEntityRefHandler",
2109 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002110 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002111 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002112 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002113 (xmlhandler)my_StartDoctypeDeclHandler},
2114 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002115 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002116 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002117 {"EntityDeclHandler",
2118 (xmlhandlersetter)XML_SetEntityDeclHandler,
2119 (xmlhandler)my_EntityDeclHandler},
2120 {"XmlDeclHandler",
2121 (xmlhandlersetter)XML_SetXmlDeclHandler,
2122 (xmlhandler)my_XmlDeclHandler},
2123 {"ElementDeclHandler",
2124 (xmlhandlersetter)XML_SetElementDeclHandler,
2125 (xmlhandler)my_ElementDeclHandler},
2126 {"AttlistDeclHandler",
2127 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2128 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002129#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002130 {"SkippedEntityHandler",
2131 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2132 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002133#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002134
Fred Drake0582df92000-07-12 04:49:00 +00002135 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002136};