blob: 7a862c0f6d216a6090b15f540ff43e456bcee966 [file] [log] [blame]
Martin v. Löwis7090ed12001-09-19 10:37:50 +00001#include "Python.h"
Fred Drake4113b132001-03-24 19:58:26 +00002#include <ctype.h>
3
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00004#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00005#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00006
Fredrik Lundhc3345042005-12-13 19:49:55 +00007#include "pyexpat.h"
8
Martin v. Löwisc847f402003-01-21 11:09:21 +00009#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
10
Jeremy Hylton9263f572003-06-27 16:13:17 +000011#define FIX_TRACE
Martin v. Löwis339d0f72001-08-17 18:39:25 +000012
Fred Drake0582df92000-07-12 04:49:00 +000013enum HandlerTypes {
14 StartElement,
15 EndElement,
16 ProcessingInstruction,
17 CharacterData,
18 UnparsedEntityDecl,
19 NotationDecl,
20 StartNamespaceDecl,
21 EndNamespaceDecl,
22 Comment,
23 StartCdataSection,
24 EndCdataSection,
25 Default,
26 DefaultHandlerExpand,
27 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000028 ExternalEntityRef,
29 StartDoctypeDecl,
30 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000031 EntityDecl,
32 XmlDecl,
33 ElementDecl,
34 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000035#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000036 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000037#endif
Fred Drake85d835f2001-02-08 15:39:08 +000038 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000039};
40
41static PyObject *ErrorObject;
42
43/* ----------------------------------------------------- */
44
45/* Declarations for objects of type xmlparser */
46
47typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000048 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000049
Fred Drake0582df92000-07-12 04:49:00 +000050 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000051 int ordered_attributes; /* Return attributes as a list. */
52 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000053 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000054 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000055 XML_Char *buffer; /* Buffer used when accumulating characters */
56 /* NULL if not enabled */
57 int buffer_size; /* Size of buffer, in XML_Char units */
58 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000059 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000060 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000061} xmlparseobject;
62
Fred Drake2a3d7db2002-06-28 22:56:48 +000063#define CHARACTER_DATA_BUFFER_SIZE 8192
64
Jeremy Hylton938ace62002-07-17 16:30:39 +000065static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000066
Fred Drake117ac852002-09-24 16:24:54 +000067typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000068typedef void* xmlhandler;
69
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000070struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000071 const char *name;
72 xmlhandlersetter setter;
73 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000074 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000075 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076};
77
Jeremy Hylton938ace62002-07-17 16:30:39 +000078static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000079
Fred Drakebd6101c2001-02-14 18:29:45 +000080/* Set an integer attribute on the error object; return true on success,
81 * false on an exception.
82 */
83static int
84set_error_attr(PyObject *err, char *name, int value)
85{
Christian Heimes217cfd12007-12-02 14:31:20 +000086 PyObject *v = PyLong_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000087
Neal Norwitz2f5e9902006-03-08 06:36:45 +000088 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
89 Py_XDECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +000090 return 0;
91 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +000092 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +000093 return 1;
94}
95
96/* Build and set an Expat exception, including positioning
97 * information. Always returns NULL.
98 */
Fred Drake85d835f2001-02-08 15:39:08 +000099static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000100set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000101{
102 PyObject *err;
103 char buffer[256];
104 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000105 int lineno = XML_GetErrorLineNumber(parser);
106 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000107
Martin v. Löwis6b2cf0e2002-06-30 06:03:35 +0000108 /* There is no risk of overflowing this buffer, since
109 even for 64-bit integers, there is sufficient space. */
110 sprintf(buffer, "%.200s: line %i, column %i",
Fred Drakebd6101c2001-02-14 18:29:45 +0000111 XML_ErrorString(code), lineno, column);
Fred Drake85d835f2001-02-08 15:39:08 +0000112 err = PyObject_CallFunction(ErrorObject, "s", buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 if ( err != NULL
114 && set_error_attr(err, "code", code)
115 && set_error_attr(err, "offset", column)
116 && set_error_attr(err, "lineno", lineno)) {
117 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000118 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000119 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000120 return NULL;
121}
122
Fred Drake71b63ff2002-06-28 22:29:01 +0000123static int
124have_handler(xmlparseobject *self, int type)
125{
126 PyObject *handler = self->handlers[type];
127 return handler != NULL;
128}
129
130static PyObject *
131get_handler_name(struct HandlerInfo *hinfo)
132{
133 PyObject *name = hinfo->nameobj;
134 if (name == NULL) {
Neal Norwitz392c5be2007-08-25 17:20:32 +0000135 name = PyUnicode_FromString(hinfo->name);
Fred Drake71b63ff2002-06-28 22:29:01 +0000136 hinfo->nameobj = name;
137 }
138 Py_XINCREF(name);
139 return name;
140}
141
Fred Drake85d835f2001-02-08 15:39:08 +0000142
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000143/* Convert a string of XML_Chars into a Unicode string.
144 Returns None if str is a null pointer. */
145
Fred Drake0582df92000-07-12 04:49:00 +0000146static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000147conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000148{
Fred Drake71b63ff2002-06-28 22:29:01 +0000149 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000150 and hence in UTF-8. */
151 /* UTF-8 from Expat, Unicode desired */
152 if (str == NULL) {
153 Py_INCREF(Py_None);
154 return Py_None;
155 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000156 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000157}
158
Fred Drake0582df92000-07-12 04:49:00 +0000159static PyObject *
160conv_string_len_to_unicode(const XML_Char *str, int len)
161{
Fred Drake71b63ff2002-06-28 22:29:01 +0000162 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000163 and hence in UTF-8. */
164 /* UTF-8 from Expat, Unicode desired */
165 if (str == NULL) {
166 Py_INCREF(Py_None);
167 return Py_None;
168 }
Fred Drake6f987622000-08-25 18:03:30 +0000169 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000170}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000172/* Callback routines */
173
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000174static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000175
Martin v. Löwis069dde22003-01-21 10:58:18 +0000176/* This handler is used when an error has been detected, in the hope
177 that actual parsing can be terminated early. This will only help
178 if an external entity reference is encountered. */
179static int
180error_external_entity_ref_handler(XML_Parser parser,
181 const XML_Char *context,
182 const XML_Char *base,
183 const XML_Char *systemId,
184 const XML_Char *publicId)
185{
186 return 0;
187}
188
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189/* Dummy character data handler used when an error (exception) has
190 been detected, and the actual parsing can be terminated early.
191 This is needed since character data handler can't be safely removed
192 from within the character data handler, but can be replaced. It is
193 used only from the character data handler trampoline, and must be
194 used right after `flag_error()` is called. */
195static void
196noop_character_data_handler(void *userData, const XML_Char *data, int len)
197{
198 /* Do nothing. */
199}
200
Fred Drake6f987622000-08-25 18:03:30 +0000201static void
202flag_error(xmlparseobject *self)
203{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000204 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000205 XML_SetExternalEntityRefHandler(self->itself,
206 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000207}
208
209static PyCodeObject*
210getcode(enum HandlerTypes slot, char* func_name, int lineno)
211{
Fred Drakebd6101c2001-02-14 18:29:45 +0000212 PyObject *code = NULL;
213 PyObject *name = NULL;
214 PyObject *nulltuple = NULL;
215 PyObject *filename = NULL;
216
217 if (handler_info[slot].tb_code == NULL) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000218 code = PyBytes_FromString("");
Fred Drakebd6101c2001-02-14 18:29:45 +0000219 if (code == NULL)
220 goto failed;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000221 name = PyUnicode_FromString(func_name);
Fred Drakebd6101c2001-02-14 18:29:45 +0000222 if (name == NULL)
223 goto failed;
224 nulltuple = PyTuple_New(0);
225 if (nulltuple == NULL)
226 goto failed;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000227 filename = PyUnicode_DecodeFSDefault(__FILE__);
Fred Drakebd6101c2001-02-14 18:29:45 +0000228 handler_info[slot].tb_code =
229 PyCode_New(0, /* argcount */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000230 0, /* kwonlyargcount */
Fred Drakebd6101c2001-02-14 18:29:45 +0000231 0, /* nlocals */
232 0, /* stacksize */
233 0, /* flags */
234 code, /* code */
235 nulltuple, /* consts */
236 nulltuple, /* names */
237 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000238#if PYTHON_API_VERSION >= 1010
Fred Drakebd6101c2001-02-14 18:29:45 +0000239 nulltuple, /* freevars */
240 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000241#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000242 filename, /* filename */
243 name, /* name */
244 lineno, /* firstlineno */
245 code /* lnotab */
246 );
247 if (handler_info[slot].tb_code == NULL)
248 goto failed;
249 Py_DECREF(code);
250 Py_DECREF(nulltuple);
251 Py_DECREF(filename);
252 Py_DECREF(name);
253 }
254 return handler_info[slot].tb_code;
255 failed:
256 Py_XDECREF(code);
257 Py_XDECREF(name);
258 return NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000259}
260
Jeremy Hylton9263f572003-06-27 16:13:17 +0000261#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000262static int
263trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
264{
265 int result = 0;
266 if (!tstate->use_tracing || tstate->tracing)
267 return 0;
268 if (tstate->c_profilefunc != NULL) {
269 tstate->tracing++;
270 result = tstate->c_profilefunc(tstate->c_profileobj,
271 f, code , val);
272 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
273 || (tstate->c_profilefunc != NULL));
274 tstate->tracing--;
275 if (result)
276 return result;
277 }
278 if (tstate->c_tracefunc != NULL) {
279 tstate->tracing++;
280 result = tstate->c_tracefunc(tstate->c_traceobj,
281 f, code , val);
282 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
283 || (tstate->c_profilefunc != NULL));
284 tstate->tracing--;
285 }
286 return result;
287}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000288
289static int
290trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
291{
292 PyObject *type, *value, *traceback, *arg;
293 int err;
294
295 if (tstate->c_tracefunc == NULL)
296 return 0;
297
298 PyErr_Fetch(&type, &value, &traceback);
299 if (value == NULL) {
300 value = Py_None;
301 Py_INCREF(value);
302 }
Martin v. Löwis9171f022004-10-13 19:50:11 +0000303#if PY_VERSION_HEX < 0x02040000
304 arg = Py_BuildValue("(OOO)", type, value, traceback);
305#else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000306 arg = PyTuple_Pack(3, type, value, traceback);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000307#endif
Jeremy Hylton9263f572003-06-27 16:13:17 +0000308 if (arg == NULL) {
309 PyErr_Restore(type, value, traceback);
310 return 0;
311 }
312 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
313 Py_DECREF(arg);
314 if (err == 0)
315 PyErr_Restore(type, value, traceback);
316 else {
317 Py_XDECREF(type);
318 Py_XDECREF(value);
319 Py_XDECREF(traceback);
320 }
321 return err;
322}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000323#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000324
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000325static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000326call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
327 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000328{
Fred Drakebd6101c2001-02-14 18:29:45 +0000329 PyThreadState *tstate = PyThreadState_GET();
330 PyFrameObject *f;
331 PyObject *res;
332
333 if (c == NULL)
334 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000335
Jeremy Hylton9263f572003-06-27 16:13:17 +0000336 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000337 if (f == NULL)
338 return NULL;
339 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000340#ifdef FIX_TRACE
341 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000342 return NULL;
343 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000344#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000345 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000346 if (res == NULL) {
347 if (tstate->curexc_traceback == NULL)
348 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000349 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000350#ifdef FIX_TRACE
351 if (trace_frame_exc(tstate, f) < 0) {
352 return NULL;
353 }
354 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000355 else {
Jeremy Hylton9263f572003-06-27 16:13:17 +0000356 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000357 Py_XDECREF(res);
358 res = NULL;
359 }
360 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000361#else
362 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000363#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000364 tstate->frame = f->f_back;
365 Py_DECREF(f);
366 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000367}
368
Fred Drakeb91a36b2002-06-27 19:40:48 +0000369static PyObject*
370string_intern(xmlparseobject *self, const char* str)
371{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000372 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000373 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000374 /* result can be NULL if the unicode conversion failed. */
375 if (!result)
376 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000377 if (!self->intern)
378 return result;
379 value = PyDict_GetItem(self->intern, result);
380 if (!value) {
381 if (PyDict_SetItem(self->intern, result, result) == 0)
382 return result;
383 else
384 return NULL;
385 }
386 Py_INCREF(value);
387 Py_DECREF(result);
388 return value;
389}
390
Fred Drake2a3d7db2002-06-28 22:56:48 +0000391/* Return 0 on success, -1 on exception.
392 * flag_error() will be called before return if needed.
393 */
394static int
395call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
396{
397 PyObject *args;
398 PyObject *temp;
399
400 args = PyTuple_New(1);
401 if (args == NULL)
402 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000403 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000404 if (temp == NULL) {
405 Py_DECREF(args);
406 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407 XML_SetCharacterDataHandler(self->itself,
408 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000409 return -1;
410 }
411 PyTuple_SET_ITEM(args, 0, temp);
412 /* temp is now a borrowed reference; consider it unused. */
413 self->in_callback = 1;
414 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000415 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000416 /* temp is an owned reference again, or NULL */
417 self->in_callback = 0;
418 Py_DECREF(args);
419 if (temp == NULL) {
420 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421 XML_SetCharacterDataHandler(self->itself,
422 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000423 return -1;
424 }
425 Py_DECREF(temp);
426 return 0;
427}
428
429static int
430flush_character_buffer(xmlparseobject *self)
431{
432 int rc;
433 if (self->buffer == NULL || self->buffer_used == 0)
434 return 0;
435 rc = call_character_handler(self, self->buffer, self->buffer_used);
436 self->buffer_used = 0;
437 return rc;
438}
439
440static void
441my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
442{
443 xmlparseobject *self = (xmlparseobject *) userData;
444 if (self->buffer == NULL)
445 call_character_handler(self, data, len);
446 else {
447 if ((self->buffer_used + len) > self->buffer_size) {
448 if (flush_character_buffer(self) < 0)
449 return;
450 /* handler might have changed; drop the rest on the floor
451 * if there isn't a handler anymore
452 */
453 if (!have_handler(self, CharacterData))
454 return;
455 }
456 if (len > self->buffer_size) {
457 call_character_handler(self, data, len);
458 self->buffer_used = 0;
459 }
460 else {
461 memcpy(self->buffer + self->buffer_used,
462 data, len * sizeof(XML_Char));
463 self->buffer_used += len;
464 }
465 }
466}
467
Fred Drake85d835f2001-02-08 15:39:08 +0000468static void
469my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000470 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000471{
472 xmlparseobject *self = (xmlparseobject *)userData;
473
Fred Drake71b63ff2002-06-28 22:29:01 +0000474 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000475 PyObject *container, *rv, *args;
476 int i, max;
477
Fred Drake2a3d7db2002-06-28 22:56:48 +0000478 if (flush_character_buffer(self) < 0)
479 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000480 /* Set max to the number of slots filled in atts[]; max/2 is
481 * the number of attributes we need to process.
482 */
483 if (self->specified_attributes) {
484 max = XML_GetSpecifiedAttributeCount(self->itself);
485 }
486 else {
487 max = 0;
488 while (atts[max] != NULL)
489 max += 2;
490 }
491 /* Build the container. */
492 if (self->ordered_attributes)
493 container = PyList_New(max);
494 else
495 container = PyDict_New();
496 if (container == NULL) {
497 flag_error(self);
498 return;
499 }
500 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000501 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000502 PyObject *v;
503 if (n == NULL) {
504 flag_error(self);
505 Py_DECREF(container);
506 return;
507 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000508 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000509 if (v == NULL) {
510 flag_error(self);
511 Py_DECREF(container);
512 Py_DECREF(n);
513 return;
514 }
515 if (self->ordered_attributes) {
516 PyList_SET_ITEM(container, i, n);
517 PyList_SET_ITEM(container, i+1, v);
518 }
519 else if (PyDict_SetItem(container, n, v)) {
520 flag_error(self);
521 Py_DECREF(n);
522 Py_DECREF(v);
523 return;
524 }
525 else {
526 Py_DECREF(n);
527 Py_DECREF(v);
528 }
529 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000530 args = string_intern(self, name);
531 if (args != NULL)
532 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000533 if (args == NULL) {
534 Py_DECREF(container);
535 return;
536 }
537 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000538 self->in_callback = 1;
539 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000540 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000541 self->in_callback = 0;
542 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000543 if (rv == NULL) {
544 flag_error(self);
545 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000546 }
Fred Drake85d835f2001-02-08 15:39:08 +0000547 Py_DECREF(rv);
548 }
549}
550
551#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
552 RETURN, GETUSERDATA) \
553static RC \
554my_##NAME##Handler PARAMS {\
555 xmlparseobject *self = GETUSERDATA ; \
556 PyObject *args = NULL; \
557 PyObject *rv = NULL; \
558 INIT \
559\
Fred Drake71b63ff2002-06-28 22:29:01 +0000560 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000561 if (flush_character_buffer(self) < 0) \
562 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000563 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000564 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000565 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000566 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000567 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000568 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000569 Py_DECREF(args); \
570 if (rv == NULL) { \
571 flag_error(self); \
572 return RETURN; \
573 } \
574 CONVERSION \
575 Py_DECREF(rv); \
576 } \
577 return RETURN; \
578}
579
Fred Drake6f987622000-08-25 18:03:30 +0000580#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
581 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
582 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000583
Fred Drake6f987622000-08-25 18:03:30 +0000584#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
585 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
Christian Heimes217cfd12007-12-02 14:31:20 +0000586 rc = PyLong_AsLong(rv);, rc, \
Fred Drake6f987622000-08-25 18:03:30 +0000587 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000588
Fred Drake71b63ff2002-06-28 22:29:01 +0000589VOID_HANDLER(EndElement,
590 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000591 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000592
Fred Drake6f987622000-08-25 18:03:30 +0000593VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000594 (void *userData,
595 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000596 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000597 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000598
Fred Drake6f987622000-08-25 18:03:30 +0000599VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000600 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000601 const XML_Char *entityName,
602 const XML_Char *base,
603 const XML_Char *systemId,
604 const XML_Char *publicId,
605 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000606 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000607 string_intern(self, entityName), string_intern(self, base),
608 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000609 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000610
Fred Drake85d835f2001-02-08 15:39:08 +0000611VOID_HANDLER(EntityDecl,
612 (void *userData,
613 const XML_Char *entityName,
614 int is_parameter_entity,
615 const XML_Char *value,
616 int value_length,
617 const XML_Char *base,
618 const XML_Char *systemId,
619 const XML_Char *publicId,
620 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000621 ("NiNNNNN",
622 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000623 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000624 string_intern(self, base), string_intern(self, systemId),
625 string_intern(self, publicId),
626 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000627
628VOID_HANDLER(XmlDecl,
629 (void *userData,
630 const XML_Char *version,
631 const XML_Char *encoding,
632 int standalone),
633 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000634 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000635 standalone))
636
637static PyObject *
638conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000639 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000640{
641 PyObject *result = NULL;
642 PyObject *children = PyTuple_New(model->numchildren);
643 int i;
644
645 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000646 assert(model->numchildren < INT_MAX);
647 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000648 PyObject *child = conv_content_model(&model->children[i],
649 conv_string);
650 if (child == NULL) {
651 Py_XDECREF(children);
652 return NULL;
653 }
654 PyTuple_SET_ITEM(children, i, child);
655 }
656 result = Py_BuildValue("(iiO&N)",
657 model->type, model->quant,
658 conv_string,model->name, children);
659 }
660 return result;
661}
662
Fred Drake06dd8cf2003-02-02 03:54:17 +0000663static void
664my_ElementDeclHandler(void *userData,
665 const XML_Char *name,
666 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000667{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000668 xmlparseobject *self = (xmlparseobject *)userData;
669 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000670
Fred Drake06dd8cf2003-02-02 03:54:17 +0000671 if (have_handler(self, ElementDecl)) {
672 PyObject *rv = NULL;
673 PyObject *modelobj, *nameobj;
674
675 if (flush_character_buffer(self) < 0)
676 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000677 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000678 if (modelobj == NULL) {
679 flag_error(self);
680 goto finally;
681 }
682 nameobj = string_intern(self, name);
683 if (nameobj == NULL) {
684 Py_DECREF(modelobj);
685 flag_error(self);
686 goto finally;
687 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000688 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000689 if (args == NULL) {
690 Py_DECREF(modelobj);
691 flag_error(self);
692 goto finally;
693 }
694 self->in_callback = 1;
695 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000696 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000697 self->in_callback = 0;
698 if (rv == NULL) {
699 flag_error(self);
700 goto finally;
701 }
702 Py_DECREF(rv);
703 }
704 finally:
705 Py_XDECREF(args);
706 XML_FreeContentModel(self->itself, model);
707 return;
708}
Fred Drake85d835f2001-02-08 15:39:08 +0000709
710VOID_HANDLER(AttlistDecl,
711 (void *userData,
712 const XML_Char *elname,
713 const XML_Char *attname,
714 const XML_Char *att_type,
715 const XML_Char *dflt,
716 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000717 ("(NNO&O&i)",
718 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000719 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000720 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000721
Martin v. Löwisc847f402003-01-21 11:09:21 +0000722#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000723VOID_HANDLER(SkippedEntity,
724 (void *userData,
725 const XML_Char *entityName,
726 int is_parameter_entity),
727 ("Ni",
728 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000729#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000730
Fred Drake71b63ff2002-06-28 22:29:01 +0000731VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000732 (void *userData,
733 const XML_Char *notationName,
734 const XML_Char *base,
735 const XML_Char *systemId,
736 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000737 ("(NNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000738 string_intern(self, notationName), string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000739 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000740
Fred Drake6f987622000-08-25 18:03:30 +0000741VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000742 (void *userData,
743 const XML_Char *prefix,
744 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000745 ("(NN)",
746 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000747
Fred Drake6f987622000-08-25 18:03:30 +0000748VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000749 (void *userData,
750 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000751 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000752
Fred Drake6f987622000-08-25 18:03:30 +0000753VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000754 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000755 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756
Fred Drake6f987622000-08-25 18:03:30 +0000757VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000759 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000760
Fred Drake6f987622000-08-25 18:03:30 +0000761VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000763 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764
Fred Drake6f987622000-08-25 18:03:30 +0000765VOID_HANDLER(Default,
Fred Drake71b63ff2002-06-28 22:29:01 +0000766 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000767 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000768
Fred Drake6f987622000-08-25 18:03:30 +0000769VOID_HANDLER(DefaultHandlerExpand,
Fred Drake71b63ff2002-06-28 22:29:01 +0000770 (void *userData, const XML_Char *s, int len),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000771 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000772
Fred Drake71b63ff2002-06-28 22:29:01 +0000773INT_HANDLER(NotStandalone,
774 (void *userData),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000775 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000776
Fred Drake6f987622000-08-25 18:03:30 +0000777RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000778 (XML_Parser parser,
779 const XML_Char *context,
780 const XML_Char *base,
781 const XML_Char *systemId,
782 const XML_Char *publicId),
783 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000784 ("(O&NNN)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000785 conv_string_to_unicode ,context, string_intern(self, base),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000786 string_intern(self, systemId), string_intern(self, publicId)),
Christian Heimes217cfd12007-12-02 14:31:20 +0000787 rc = PyLong_AsLong(rv);, rc,
Fred Drake6f987622000-08-25 18:03:30 +0000788 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000790/* XXX UnknownEncodingHandler */
791
Fred Drake85d835f2001-02-08 15:39:08 +0000792VOID_HANDLER(StartDoctypeDecl,
793 (void *userData, const XML_Char *doctypeName,
794 const XML_Char *sysid, const XML_Char *pubid,
795 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000796 ("(NNNi)", string_intern(self, doctypeName),
797 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000798 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000799
800VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000802/* ---------------------------------------------------------------- */
803
Fred Drake71b63ff2002-06-28 22:29:01 +0000804static PyObject *
805get_parse_result(xmlparseobject *self, int rv)
806{
807 if (PyErr_Occurred()) {
808 return NULL;
809 }
810 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000811 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000812 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000813 if (flush_character_buffer(self) < 0) {
814 return NULL;
815 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000816 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000817}
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000820"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000821Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000822
823static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000824xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000825{
Fred Drake0582df92000-07-12 04:49:00 +0000826 char *s;
827 int slen;
828 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829
Fred Drake0582df92000-07-12 04:49:00 +0000830 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
831 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000832
833 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834}
835
Fred Drakeca1f4262000-09-21 20:10:23 +0000836/* File reading copied from cPickle */
837
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000838#define BUF_SIZE 2048
839
Fred Drake0582df92000-07-12 04:49:00 +0000840static int
841readinst(char *buf, int buf_size, PyObject *meth)
842{
843 PyObject *arg = NULL;
844 PyObject *bytes = NULL;
845 PyObject *str = NULL;
846 int len = -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000847 char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000848
Christian Heimes217cfd12007-12-02 14:31:20 +0000849 if ((bytes = PyLong_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000850 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000851
Fred Drake7b6caff2003-07-21 17:05:56 +0000852 if ((arg = PyTuple_New(1)) == NULL) {
853 Py_DECREF(bytes);
Fred Drake0582df92000-07-12 04:49:00 +0000854 goto finally;
Fred Drake7b6caff2003-07-21 17:05:56 +0000855 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000856
Tim Peters954eef72000-09-22 06:01:11 +0000857 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000858
Martin v. Löwis9171f022004-10-13 19:50:11 +0000859#if PY_VERSION_HEX < 0x02020000
860 str = PyObject_CallObject(meth, arg);
861#else
862 str = PyObject_Call(meth, arg, NULL);
863#endif
864 if (str == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000865 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000866
Christian Heimes72b710a2008-05-26 13:28:38 +0000867 if (PyBytes_Check(str))
868 ptr = PyBytes_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000869 else if (PyByteArray_Check(str))
870 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000871 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000872 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000873 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000874 Py_TYPE(str)->tp_name);
Fred Drake0582df92000-07-12 04:49:00 +0000875 goto finally;
876 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000877 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000878 if (len > buf_size) {
879 PyErr_Format(PyExc_ValueError,
880 "read() returned too much data: "
881 "%i bytes requested, %i returned",
882 buf_size, len);
Fred Drake0582df92000-07-12 04:49:00 +0000883 goto finally;
884 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000885 memcpy(buf, ptr, len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886finally:
Fred Drake0582df92000-07-12 04:49:00 +0000887 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000888 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000889 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890}
891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000893"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000895
896static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000897xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000898{
Fred Drake0582df92000-07-12 04:49:00 +0000899 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000900 FILE *fp;
901 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000902
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000903 {
Fred Drake0582df92000-07-12 04:49:00 +0000904 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000905 readmethod = PyObject_GetAttrString(f, "read");
906 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000907 PyErr_Clear();
Fred Drake71b63ff2002-06-28 22:29:01 +0000908 PyErr_SetString(PyExc_TypeError,
Fred Drake0582df92000-07-12 04:49:00 +0000909 "argument must have 'read' attribute");
Fred Drake814f9fe2002-07-19 22:03:03 +0000910 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000911 }
912 }
913 for (;;) {
914 int bytes_read;
915 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000916 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000917 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000918 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000919 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000920
Fred Drake0582df92000-07-12 04:49:00 +0000921 if (fp) {
922 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
923 if (bytes_read < 0) {
924 PyErr_SetFromErrno(PyExc_IOError);
925 return NULL;
926 }
927 }
928 else {
929 bytes_read = readinst(buf, BUF_SIZE, readmethod);
Fred Drake7b6caff2003-07-21 17:05:56 +0000930 if (bytes_read < 0) {
931 Py_DECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000932 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000933 }
Fred Drake0582df92000-07-12 04:49:00 +0000934 }
935 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000936 if (PyErr_Occurred()) {
937 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000938 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000939 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000940
Fred Drake0582df92000-07-12 04:49:00 +0000941 if (!rv || bytes_read == 0)
942 break;
943 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000944 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000945 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000946}
947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000949"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000950Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000951
952static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000953xmlparse_SetBase(xmlparseobject *self, PyObject *args)
954{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000955 char *base;
956
Fred Drake0582df92000-07-12 04:49:00 +0000957 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000958 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000959 if (!XML_SetBase(self->itself, base)) {
960 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000961 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000962 Py_INCREF(Py_None);
963 return Py_None;
964}
965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000966PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000967"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000968Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000969
970static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000971xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
Fred Drake0582df92000-07-12 04:49:00 +0000972{
Fred Drake0582df92000-07-12 04:49:00 +0000973 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000974}
975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000976PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000977"GetInputContext() -> string\n\
978Return the untranslated text of the input that caused the current event.\n\
979If the event was generated by a large amount of text (such as a start tag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000981
982static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000983xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
Fred Drakebd6101c2001-02-14 18:29:45 +0000984{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000985 if (self->in_callback) {
986 int offset, size;
987 const char *buffer
988 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000989
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000990 if (buffer != NULL)
Christian Heimes72b710a2008-05-26 13:28:38 +0000991 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000992 size - offset);
993 else
994 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000995 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000996 else
997 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000998}
Fred Drakebd6101c2001-02-14 18:29:45 +0000999
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001000PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +00001001"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +00001002Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001004
1005static PyObject *
1006xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1007{
1008 char *context;
1009 char *encoding = NULL;
1010 xmlparseobject *new_parser;
1011 int i;
1012
Martin v. Löwisc57428d2001-09-19 09:55:09 +00001013 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +00001014 &context, &encoding)) {
1015 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001016 }
1017
Martin v. Löwis894258c2001-09-23 10:20:10 +00001018#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001019 /* Python versions 2.0 and 2.1 */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001020 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001021#else
1022 /* Python versions 2.2 and later */
1023 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1024#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001025
1026 if (new_parser == NULL)
1027 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001028 new_parser->buffer_size = self->buffer_size;
1029 new_parser->buffer_used = 0;
1030 if (self->buffer != NULL) {
1031 new_parser->buffer = malloc(new_parser->buffer_size);
1032 if (new_parser->buffer == NULL) {
Fred Drakeb28467b2002-07-02 15:44:36 +00001033#ifndef Py_TPFLAGS_HAVE_GC
1034 /* Code for versions 2.0 and 2.1 */
1035 PyObject_Del(new_parser);
1036#else
1037 /* Code for versions 2.2 and later. */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001038 PyObject_GC_Del(new_parser);
Fred Drakeb28467b2002-07-02 15:44:36 +00001039#endif
Fred Drake2a3d7db2002-06-28 22:56:48 +00001040 return PyErr_NoMemory();
1041 }
1042 }
1043 else
1044 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001045 new_parser->ordered_attributes = self->ordered_attributes;
1046 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001047 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001048 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001049 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001050 encoding);
1051 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001052 new_parser->intern = self->intern;
1053 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001054#ifdef Py_TPFLAGS_HAVE_GC
1055 PyObject_GC_Track(new_parser);
1056#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001057 PyObject_GC_Init(new_parser);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001058#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001059
1060 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001061 Py_DECREF(new_parser);
1062 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001063 }
1064
1065 XML_SetUserData(new_parser->itself, (void *)new_parser);
1066
1067 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001068 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001069 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001070
Fred Drake2a3d7db2002-06-28 22:56:48 +00001071 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001072 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001073 Py_DECREF(new_parser);
1074 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001075 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001076 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001077
1078 /* then copy handlers from self */
1079 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001080 PyObject *handler = self->handlers[i];
1081 if (handler != NULL) {
1082 Py_INCREF(handler);
1083 new_parser->handlers[i] = handler;
1084 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001085 handler_info[i].handler);
1086 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001087 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001088 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001089}
1090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001092"SetParamEntityParsing(flag) -> success\n\
1093Controls parsing of parameter entities (including the external DTD\n\
1094subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1095XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1096XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001098
1099static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001100xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101{
Fred Drake85d835f2001-02-08 15:39:08 +00001102 int flag;
1103 if (!PyArg_ParseTuple(args, "i", &flag))
1104 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001105 flag = XML_SetParamEntityParsing(p->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001106 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107}
1108
Martin v. Löwisc847f402003-01-21 11:09:21 +00001109
1110#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001111PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1112"UseForeignDTD([flag])\n\
1113Allows the application to provide an artificial external subset if one is\n\
1114not specified as part of the document instance. This readily allows the\n\
1115use of a 'default' document type controlled by the application, while still\n\
1116getting the advantage of providing document type information to the parser.\n\
1117'flag' defaults to True if not provided.");
1118
1119static PyObject *
1120xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1121{
1122 PyObject *flagobj = NULL;
1123 XML_Bool flag = XML_TRUE;
1124 enum XML_Error rc;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001125 if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
Martin v. Löwis069dde22003-01-21 10:58:18 +00001126 return NULL;
1127 if (flagobj != NULL)
1128 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1129 rc = XML_UseForeignDTD(self->itself, flag);
1130 if (rc != XML_ERROR_NONE) {
1131 return set_error(self, rc);
1132 }
1133 Py_INCREF(Py_None);
1134 return Py_None;
1135}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001136#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001137
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001138static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs);
1139
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001140static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001141 {"Parse", (PyCFunction)xmlparse_Parse,
Fred Drakebd6101c2001-02-14 18:29:45 +00001142 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001143 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001144 METH_O, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001145 {"SetBase", (PyCFunction)xmlparse_SetBase,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001146 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001147 {"GetBase", (PyCFunction)xmlparse_GetBase,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001148 METH_NOARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001149 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Martin v. Löwis069dde22003-01-21 10:58:18 +00001150 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001151 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1152 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001153 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001154 METH_NOARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001155#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001156 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1157 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001158#endif
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001159 {"__dir__", xmlparse_dir, METH_NOARGS},
Martin v. Löwis069dde22003-01-21 10:58:18 +00001160 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001161};
1162
1163/* ---------- */
1164
1165
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001166
Fred Drake71b63ff2002-06-28 22:29:01 +00001167/* pyexpat international encoding support.
1168 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001169*/
1170
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001171static char template_buffer[257];
Fred Drakebb66a202001-03-01 20:48:17 +00001172PyObject *template_string = NULL;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001173
Fred Drake71b63ff2002-06-28 22:29:01 +00001174static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001175init_template_buffer(void)
1176{
1177 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001178 for (i = 0; i < 256; i++) {
1179 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001180 }
Fred Drakebb66a202001-03-01 20:48:17 +00001181 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001182}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001183
Fred Drake71b63ff2002-06-28 22:29:01 +00001184static int
1185PyUnknownEncodingHandler(void *encodingHandlerData,
1186 const XML_Char *name,
1187 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001188{
Fred Drakebb66a202001-03-01 20:48:17 +00001189 PyUnicodeObject *_u_string = NULL;
1190 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001191 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001192
Fred Drakebb66a202001-03-01 20:48:17 +00001193 /* Yes, supports only 8bit encodings */
1194 _u_string = (PyUnicodeObject *)
1195 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001196
Fred Drakebb66a202001-03-01 20:48:17 +00001197 if (_u_string == NULL)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001198 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001199
Fred Drakebb66a202001-03-01 20:48:17 +00001200 for (i = 0; i < 256; i++) {
1201 /* Stupid to access directly, but fast */
1202 Py_UNICODE c = _u_string->str[i];
1203 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001204 info->map[i] = -1;
Fred Drakebb66a202001-03-01 20:48:17 +00001205 else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001206 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001207 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001208 info->data = NULL;
1209 info->convert = NULL;
1210 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001211 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001212 Py_DECREF(_u_string);
1213 return result;
1214}
1215
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001216
1217static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001218newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001219{
1220 int i;
1221 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001222
Martin v. Löwis894258c2001-09-23 10:20:10 +00001223#ifdef Py_TPFLAGS_HAVE_GC
1224 /* Code for versions 2.2 and later */
1225 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1226#else
Fred Drake0582df92000-07-12 04:49:00 +00001227 self = PyObject_New(xmlparseobject, &Xmlparsetype);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001228#endif
Fred Drake0582df92000-07-12 04:49:00 +00001229 if (self == NULL)
1230 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001231
Fred Drake2a3d7db2002-06-28 22:56:48 +00001232 self->buffer = NULL;
1233 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1234 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001235 self->ordered_attributes = 0;
1236 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001237 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001238 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001239 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001240 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001241 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1242 }
Fred Drake85d835f2001-02-08 15:39:08 +00001243 else {
Fred Drake0582df92000-07-12 04:49:00 +00001244 self->itself = XML_ParserCreate(encoding);
1245 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001246 self->intern = intern;
1247 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001248#ifdef Py_TPFLAGS_HAVE_GC
1249 PyObject_GC_Track(self);
1250#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001251 PyObject_GC_Init(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001252#endif
Fred Drake0582df92000-07-12 04:49:00 +00001253 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001254 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001255 "XML_ParserCreate failed");
1256 Py_DECREF(self);
1257 return NULL;
1258 }
1259 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001260 XML_SetUnknownEncodingHandler(self->itself,
1261 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001262
Fred Drake2a3d7db2002-06-28 22:56:48 +00001263 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001264 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001265
Fred Drake7c75bf22002-07-01 14:02:31 +00001266 self->handlers = malloc(sizeof(PyObject *) * i);
1267 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001268 Py_DECREF(self);
1269 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001270 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001271 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001272
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001273 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001274}
1275
1276
1277static void
Fred Drake0582df92000-07-12 04:49:00 +00001278xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001279{
Fred Drake0582df92000-07-12 04:49:00 +00001280 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001281#ifdef Py_TPFLAGS_HAVE_GC
1282 PyObject_GC_UnTrack(self);
1283#else
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001284 PyObject_GC_Fini(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001285#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001286 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001287 XML_ParserFree(self->itself);
1288 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001289
Fred Drake85d835f2001-02-08 15:39:08 +00001290 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001291 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001292 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001293 temp = self->handlers[i];
1294 self->handlers[i] = NULL;
1295 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001296 }
1297 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001298 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001299 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001300 if (self->buffer != NULL) {
1301 free(self->buffer);
1302 self->buffer = NULL;
1303 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001304 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001305#ifndef Py_TPFLAGS_HAVE_GC
Martin v. Löwisb4fcf4d2002-06-30 06:40:55 +00001306 /* Code for versions 2.0 and 2.1 */
Fred Drake0582df92000-07-12 04:49:00 +00001307 PyObject_Del(self);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001308#else
1309 /* Code for versions 2.2 and later. */
1310 PyObject_GC_Del(self);
1311#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001312}
1313
Fred Drake0582df92000-07-12 04:49:00 +00001314static int
1315handlername2int(const char *name)
1316{
1317 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001318 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake0582df92000-07-12 04:49:00 +00001319 if (strcmp(name, handler_info[i].name) == 0) {
1320 return i;
1321 }
1322 }
1323 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001324}
1325
1326static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001327get_pybool(int istrue)
1328{
1329 PyObject *result = istrue ? Py_True : Py_False;
1330 Py_INCREF(result);
1331 return result;
1332}
1333
1334static PyObject *
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001335xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001336{
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001337 char *name = "";
1338 int handlernum = -1;
1339
1340 if (PyUnicode_Check(nameobj))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001341 name = _PyUnicode_AsString(nameobj);
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001342
1343 handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001344
1345 if (handlernum != -1) {
1346 PyObject *result = self->handlers[handlernum];
1347 if (result == NULL)
1348 result = Py_None;
1349 Py_INCREF(result);
1350 return result;
1351 }
1352 if (name[0] == 'E') {
1353 if (strcmp(name, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001354 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001355 XML_GetErrorCode(self->itself));
1356 if (strcmp(name, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001357 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001358 XML_GetErrorLineNumber(self->itself));
1359 if (strcmp(name, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001360 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001361 XML_GetErrorColumnNumber(self->itself));
1362 if (strcmp(name, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001363 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001364 XML_GetErrorByteIndex(self->itself));
1365 }
Dave Cole3203efb2004-08-26 00:37:31 +00001366 if (name[0] == 'C') {
1367 if (strcmp(name, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001368 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001369 XML_GetCurrentLineNumber(self->itself));
1370 if (strcmp(name, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001371 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001372 XML_GetCurrentColumnNumber(self->itself));
1373 if (strcmp(name, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001374 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001375 XML_GetCurrentByteIndex(self->itself));
1376 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001377 if (name[0] == 'b') {
1378 if (strcmp(name, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001379 return PyLong_FromLong((long) self->buffer_size);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001380 if (strcmp(name, "buffer_text") == 0)
1381 return get_pybool(self->buffer != NULL);
1382 if (strcmp(name, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001383 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001384 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001385 if (strcmp(name, "namespace_prefixes") == 0)
1386 return get_pybool(self->ns_prefixes);
Fred Drake85d835f2001-02-08 15:39:08 +00001387 if (strcmp(name, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001388 return get_pybool(self->ordered_attributes);
Fred Drake85d835f2001-02-08 15:39:08 +00001389 if (strcmp(name, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001390 return get_pybool((long) self->specified_attributes);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001391 if (strcmp(name, "intern") == 0) {
1392 if (self->intern == NULL) {
1393 Py_INCREF(Py_None);
1394 return Py_None;
1395 }
1396 else {
1397 Py_INCREF(self->intern);
1398 return self->intern;
1399 }
1400 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001401
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001402 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001403}
1404
1405static PyObject *
1406xmlparse_dir(PyObject *self, PyObject* noargs)
1407{
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001408#define APPEND(list, str) \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001409 do { \
Neal Norwitz392c5be2007-08-25 17:20:32 +00001410 PyObject *o = PyUnicode_FromString(str); \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001411 if (o != NULL) \
1412 PyList_Append(list, o); \
1413 Py_XDECREF(o); \
1414 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001415
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001416 int i;
1417 PyObject *rc = PyList_New(0);
1418 if (!rc)
1419 return NULL;
1420 for (i = 0; handler_info[i].name != NULL; i++) {
1421 PyObject *o = get_handler_name(&handler_info[i]);
1422 if (o != NULL)
1423 PyList_Append(rc, o);
1424 Py_XDECREF(o);
1425 }
1426 APPEND(rc, "ErrorCode");
1427 APPEND(rc, "ErrorLineNumber");
1428 APPEND(rc, "ErrorColumnNumber");
1429 APPEND(rc, "ErrorByteIndex");
1430 APPEND(rc, "CurrentLineNumber");
1431 APPEND(rc, "CurrentColumnNumber");
1432 APPEND(rc, "CurrentByteIndex");
1433 APPEND(rc, "buffer_size");
1434 APPEND(rc, "buffer_text");
1435 APPEND(rc, "buffer_used");
1436 APPEND(rc, "namespace_prefixes");
1437 APPEND(rc, "ordered_attributes");
1438 APPEND(rc, "specified_attributes");
1439 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001440
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001441#undef APPEND
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001442
1443 if (PyErr_Occurred()) {
1444 Py_DECREF(rc);
1445 rc = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001446 }
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001447
1448 return rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001449}
1450
Fred Drake6f987622000-08-25 18:03:30 +00001451static int
1452sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001453{
1454 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001455 if (handlernum >= 0) {
1456 xmlhandler c_handler = NULL;
1457 PyObject *temp = self->handlers[handlernum];
1458
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001459 if (v == Py_None) {
1460 /* If this is the character data handler, and a character
1461 data handler is already active, we need to be more
1462 careful. What we can safely do is replace the existing
1463 character data handler callback function with a no-op
1464 function that will refuse to call Python. The downside
1465 is that this doesn't completely remove the character
1466 data handler from the C layer if there's any callback
1467 active, so Expat does a little more work than it
1468 otherwise would, but that's really an odd case. A more
1469 elaborate system of handlers and state could remove the
1470 C handler more effectively. */
1471 if (handlernum == CharacterData && self->in_callback)
1472 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001473 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001474 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001475 else if (v != NULL) {
1476 Py_INCREF(v);
1477 c_handler = handler_info[handlernum].handler;
1478 }
Fred Drake0582df92000-07-12 04:49:00 +00001479 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001480 Py_XDECREF(temp);
1481 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001482 return 1;
1483 }
1484 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001485}
1486
1487static int
Fred Drake6f987622000-08-25 18:03:30 +00001488xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001489{
Fred Drake6f987622000-08-25 18:03:30 +00001490 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001491 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001492 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1493 return -1;
1494 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001495 if (strcmp(name, "buffer_text") == 0) {
1496 if (PyObject_IsTrue(v)) {
1497 if (self->buffer == NULL) {
1498 self->buffer = malloc(self->buffer_size);
1499 if (self->buffer == NULL) {
1500 PyErr_NoMemory();
1501 return -1;
1502 }
1503 self->buffer_used = 0;
1504 }
1505 }
1506 else if (self->buffer != NULL) {
1507 if (flush_character_buffer(self) < 0)
1508 return -1;
1509 free(self->buffer);
1510 self->buffer = NULL;
1511 }
1512 return 0;
1513 }
Martin v. Löwis069dde22003-01-21 10:58:18 +00001514 if (strcmp(name, "namespace_prefixes") == 0) {
1515 if (PyObject_IsTrue(v))
1516 self->ns_prefixes = 1;
1517 else
1518 self->ns_prefixes = 0;
1519 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1520 return 0;
1521 }
Fred Drake85d835f2001-02-08 15:39:08 +00001522 if (strcmp(name, "ordered_attributes") == 0) {
1523 if (PyObject_IsTrue(v))
1524 self->ordered_attributes = 1;
1525 else
1526 self->ordered_attributes = 0;
1527 return 0;
1528 }
Fred Drake85d835f2001-02-08 15:39:08 +00001529 if (strcmp(name, "specified_attributes") == 0) {
1530 if (PyObject_IsTrue(v))
1531 self->specified_attributes = 1;
1532 else
1533 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001534 return 0;
1535 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001536
1537 if (strcmp(name, "buffer_size") == 0) {
1538 long new_buffer_size;
1539 if (!PyLong_Check(v)) {
1540 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1541 return -1;
1542 }
1543
1544 new_buffer_size=PyLong_AS_LONG(v);
1545 /* trivial case -- no change */
1546 if (new_buffer_size == self->buffer_size) {
1547 return 0;
1548 }
1549
1550 if (new_buffer_size <= 0) {
1551 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1552 return -1;
1553 }
1554
1555 /* check maximum */
1556 if (new_buffer_size > INT_MAX) {
1557 char errmsg[100];
1558 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1559 PyErr_SetString(PyExc_ValueError, errmsg);
1560 return -1;
1561 }
1562
1563 if (self->buffer != NULL) {
1564 /* there is already a buffer */
1565 if (self->buffer_used != 0) {
1566 flush_character_buffer(self);
1567 }
1568 /* free existing buffer */
1569 free(self->buffer);
1570 }
1571 self->buffer = malloc(new_buffer_size);
1572 if (self->buffer == NULL) {
1573 PyErr_NoMemory();
1574 return -1;
1575 }
1576 self->buffer_size = new_buffer_size;
1577 return 0;
1578 }
1579
Fred Drake2a3d7db2002-06-28 22:56:48 +00001580 if (strcmp(name, "CharacterDataHandler") == 0) {
1581 /* If we're changing the character data handler, flush all
1582 * cached data with the old handler. Not sure there's a
1583 * "right" thing to do, though, but this probably won't
1584 * happen.
1585 */
1586 if (flush_character_buffer(self) < 0)
1587 return -1;
1588 }
Fred Drake6f987622000-08-25 18:03:30 +00001589 if (sethandler(self, name, v)) {
1590 return 0;
1591 }
1592 PyErr_SetString(PyExc_AttributeError, name);
1593 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001594}
1595
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001596static int
1597xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1598{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001599 int i;
1600 for (i = 0; handler_info[i].name != NULL; i++)
1601 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001602 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001603}
1604
1605static int
1606xmlparse_clear(xmlparseobject *op)
1607{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001608 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001610 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001611}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001614
1615static PyTypeObject Xmlparsetype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001616 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001617 "pyexpat.xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001618 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001619 0, /*tp_itemsize*/
1620 /* methods */
1621 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1622 (printfunc)0, /*tp_print*/
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001623 0, /*tp_getattr*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001624 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001625 0, /*tp_reserved*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001626 (reprfunc)0, /*tp_repr*/
1627 0, /*tp_as_number*/
1628 0, /*tp_as_sequence*/
1629 0, /*tp_as_mapping*/
1630 (hashfunc)0, /*tp_hash*/
1631 (ternaryfunc)0, /*tp_call*/
1632 (reprfunc)0, /*tp_str*/
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001633 (getattrofunc)xmlparse_getattro, /* tp_getattro */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001634 0, /* tp_setattro */
1635 0, /* tp_as_buffer */
Martin v. Löwis894258c2001-09-23 10:20:10 +00001636#ifdef Py_TPFLAGS_HAVE_GC
Fred Drake71b63ff2002-06-28 22:29:01 +00001637 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001638#else
Fred Drake71b63ff2002-06-28 22:29:01 +00001639 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Martin v. Löwis894258c2001-09-23 10:20:10 +00001640#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001641 Xmlparsetype__doc__, /* tp_doc - Documentation string */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001642 (traverseproc)xmlparse_traverse, /* tp_traverse */
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001643 (inquiry)xmlparse_clear, /* tp_clear */
1644 0, /* tp_richcompare */
1645 0, /* tp_weaklistoffset */
1646 0, /* tp_iter */
1647 0, /* tp_iternext */
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001648 xmlparse_methods, /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001649};
1650
1651/* End of code for xmlparser objects */
1652/* -------------------------------------------------------- */
1653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001655"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001657
1658static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001659pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1660{
Fred Drakecde79132001-04-25 16:01:30 +00001661 char *encoding = NULL;
1662 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001663 PyObject *intern = NULL;
1664 PyObject *result;
1665 int intern_decref = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001666 static char *kwlist[] = {"encoding", "namespace_separator",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001667 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001668
Fred Drakeb91a36b2002-06-27 19:40:48 +00001669 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1670 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001671 return NULL;
1672 if (namespace_separator != NULL
1673 && strlen(namespace_separator) > 1) {
1674 PyErr_SetString(PyExc_ValueError,
1675 "namespace_separator must be at most one"
1676 " character, omitted, or None");
1677 return NULL;
1678 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001679 /* Explicitly passing None means no interning is desired.
1680 Not passing anything means that a new dictionary is used. */
1681 if (intern == Py_None)
1682 intern = NULL;
1683 else if (intern == NULL) {
1684 intern = PyDict_New();
1685 if (!intern)
1686 return NULL;
1687 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001688 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001689 else if (!PyDict_Check(intern)) {
1690 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1691 return NULL;
1692 }
1693
1694 result = newxmlparseobject(encoding, namespace_separator, intern);
1695 if (intern_decref) {
1696 Py_DECREF(intern);
1697 }
1698 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001699}
1700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001702"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001704
1705static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001706pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001707{
Fred Drake0582df92000-07-12 04:49:00 +00001708 long code = 0;
1709
1710 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1711 return NULL;
1712 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001713}
1714
1715/* List of methods defined in the module */
1716
1717static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001718 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1719 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1720 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1721 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001722
Fred Drake0582df92000-07-12 04:49:00 +00001723 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001724};
1725
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001726/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001728PyDoc_STRVAR(pyexpat_module_documentation,
1729"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001730
Fred Drake4113b132001-03-24 19:58:26 +00001731/* Return a Python string that represents the version number without the
1732 * extra cruft added by revision control, even if the right options were
1733 * given to the "cvs export" command to make it not include the extra
1734 * cruft.
1735 */
1736static PyObject *
1737get_version_string(void)
1738{
1739 static char *rcsid = "$Revision$";
1740 char *rev = rcsid;
1741 int i = 0;
1742
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001743 while (!isdigit(Py_CHARMASK(*rev)))
Fred Drake4113b132001-03-24 19:58:26 +00001744 ++rev;
1745 while (rev[i] != ' ' && rev[i] != '\0')
1746 ++i;
1747
Neal Norwitz392c5be2007-08-25 17:20:32 +00001748 return PyUnicode_FromStringAndSize(rev, i);
Fred Drake4113b132001-03-24 19:58:26 +00001749}
1750
Fred Drakecde79132001-04-25 16:01:30 +00001751/* Initialization function for the module */
1752
1753#ifndef MODULE_NAME
1754#define MODULE_NAME "pyexpat"
1755#endif
1756
1757#ifndef MODULE_INITFUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001758#define MODULE_INITFUNC PyInit_pyexpat
Fred Drakecde79132001-04-25 16:01:30 +00001759#endif
1760
Martin v. Löwis069dde22003-01-21 10:58:18 +00001761#ifndef PyMODINIT_FUNC
1762# ifdef MS_WINDOWS
1763# define PyMODINIT_FUNC __declspec(dllexport) void
1764# else
1765# define PyMODINIT_FUNC void
1766# endif
1767#endif
1768
Mark Hammond8235ea12002-07-19 06:55:41 +00001769PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001770
Martin v. Löwis1a214512008-06-11 05:26:20 +00001771static struct PyModuleDef pyexpatmodule = {
1772 PyModuleDef_HEAD_INIT,
1773 MODULE_NAME,
1774 pyexpat_module_documentation,
1775 -1,
1776 pyexpat_methods,
1777 NULL,
1778 NULL,
1779 NULL,
1780 NULL
1781};
1782
Martin v. Löwis069dde22003-01-21 10:58:18 +00001783PyMODINIT_FUNC
1784MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001785{
1786 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001787 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001788 PyObject *errors_module;
1789 PyObject *modelmod_name;
1790 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001791 PyObject *sys_modules;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001792 static struct PyExpat_CAPI capi;
1793 PyObject* capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001794
Fred Drake6f987622000-08-25 18:03:30 +00001795 if (errmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001796 return NULL;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001797 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001798 if (modelmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001799 return NULL;
Fred Drake6f987622000-08-25 18:03:30 +00001800
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001801 if (PyType_Ready(&Xmlparsetype) < 0)
1802 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001803
Fred Drake0582df92000-07-12 04:49:00 +00001804 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001805 m = PyModule_Create(&pyexpatmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001806 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001807 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001808
Fred Drake0582df92000-07-12 04:49:00 +00001809 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001810 if (ErrorObject == NULL) {
1811 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001812 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001813 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001814 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001815 }
1816 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001817 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001818 Py_INCREF(ErrorObject);
1819 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001820 Py_INCREF(&Xmlparsetype);
1821 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001822
Fred Drake4113b132001-03-24 19:58:26 +00001823 PyModule_AddObject(m, "__version__", get_version_string());
Fred Drake738293d2000-12-21 17:25:07 +00001824 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1825 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001826 {
1827 XML_Expat_Version info = XML_ExpatVersionInfo();
1828 PyModule_AddObject(m, "version_info",
1829 Py_BuildValue("(iii)", info.major,
1830 info.minor, info.micro));
1831 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001832 init_template_buffer();
Fred Drake0582df92000-07-12 04:49:00 +00001833 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001834 compiled, this should check and set native_encoding
1835 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001836 */
Fred Drake93adb692000-09-23 04:55:48 +00001837 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001838
Fred Drake85d835f2001-02-08 15:39:08 +00001839 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001840 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001841 errors_module = PyDict_GetItem(d, errmod_name);
1842 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001843 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001844 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001845 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001846 /* gives away the reference to errors_module */
1847 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001848 }
1849 }
Fred Drake6f987622000-08-25 18:03:30 +00001850 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001851 model_module = PyDict_GetItem(d, modelmod_name);
1852 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001853 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001854 if (model_module != NULL) {
1855 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1856 /* gives away the reference to model_module */
1857 PyModule_AddObject(m, "model", model_module);
1858 }
1859 }
1860 Py_DECREF(modelmod_name);
1861 if (errors_module == NULL || model_module == NULL)
1862 /* Don't core dump later! */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001863 return NULL;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001864
Martin v. Löwisc847f402003-01-21 11:09:21 +00001865#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001866 {
1867 const XML_Feature *features = XML_GetFeatureList();
1868 PyObject *list = PyList_New(0);
1869 if (list == NULL)
1870 /* just ignore it */
1871 PyErr_Clear();
1872 else {
1873 int i = 0;
1874 for (; features[i].feature != XML_FEATURE_END; ++i) {
1875 int ok;
1876 PyObject *item = Py_BuildValue("si", features[i].name,
1877 features[i].value);
1878 if (item == NULL) {
1879 Py_DECREF(list);
1880 list = NULL;
1881 break;
1882 }
1883 ok = PyList_Append(list, item);
1884 Py_DECREF(item);
1885 if (ok < 0) {
1886 PyErr_Clear();
1887 break;
1888 }
1889 }
1890 if (list != NULL)
1891 PyModule_AddObject(m, "features", list);
1892 }
1893 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001894#endif
Fred Drake6f987622000-08-25 18:03:30 +00001895
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001896#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001897 PyModule_AddStringConstant(errors_module, #name, \
1898 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001899
Fred Drake0582df92000-07-12 04:49:00 +00001900 MYCONST(XML_ERROR_NO_MEMORY);
1901 MYCONST(XML_ERROR_SYNTAX);
1902 MYCONST(XML_ERROR_NO_ELEMENTS);
1903 MYCONST(XML_ERROR_INVALID_TOKEN);
1904 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1905 MYCONST(XML_ERROR_PARTIAL_CHAR);
1906 MYCONST(XML_ERROR_TAG_MISMATCH);
1907 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1908 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1909 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1910 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1911 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1912 MYCONST(XML_ERROR_ASYNC_ENTITY);
1913 MYCONST(XML_ERROR_BAD_CHAR_REF);
1914 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1915 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1916 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1917 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1918 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001919 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1920 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1921 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001922 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1923 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1924 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1925 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1926 /* Added in Expat 1.95.7. */
1927 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1928 /* Added in Expat 1.95.8. */
1929 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1930 MYCONST(XML_ERROR_INCOMPLETE_PE);
1931 MYCONST(XML_ERROR_XML_DECL);
1932 MYCONST(XML_ERROR_TEXT_DECL);
1933 MYCONST(XML_ERROR_PUBLICID);
1934 MYCONST(XML_ERROR_SUSPENDED);
1935 MYCONST(XML_ERROR_NOT_SUSPENDED);
1936 MYCONST(XML_ERROR_ABORTED);
1937 MYCONST(XML_ERROR_FINISHED);
1938 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001939
Fred Drake85d835f2001-02-08 15:39:08 +00001940 PyModule_AddStringConstant(errors_module, "__doc__",
1941 "Constants used to describe error conditions.");
1942
Fred Drake93adb692000-09-23 04:55:48 +00001943#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001944
Fred Drake85d835f2001-02-08 15:39:08 +00001945#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001946 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1947 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1948 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001949#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001950
Fred Drake85d835f2001-02-08 15:39:08 +00001951#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1952 PyModule_AddStringConstant(model_module, "__doc__",
1953 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001954
Fred Drake85d835f2001-02-08 15:39:08 +00001955 MYCONST(XML_CTYPE_EMPTY);
1956 MYCONST(XML_CTYPE_ANY);
1957 MYCONST(XML_CTYPE_MIXED);
1958 MYCONST(XML_CTYPE_NAME);
1959 MYCONST(XML_CTYPE_CHOICE);
1960 MYCONST(XML_CTYPE_SEQ);
1961
1962 MYCONST(XML_CQUANT_NONE);
1963 MYCONST(XML_CQUANT_OPT);
1964 MYCONST(XML_CQUANT_REP);
1965 MYCONST(XML_CQUANT_PLUS);
1966#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001967
1968 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001969 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001970 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001971 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1972 capi.MINOR_VERSION = XML_MINOR_VERSION;
1973 capi.MICRO_VERSION = XML_MICRO_VERSION;
1974 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001975 capi.GetErrorCode = XML_GetErrorCode;
1976 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1977 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001978 capi.Parse = XML_Parse;
1979 capi.ParserCreate_MM = XML_ParserCreate_MM;
1980 capi.ParserFree = XML_ParserFree;
1981 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1982 capi.SetCommentHandler = XML_SetCommentHandler;
1983 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1984 capi.SetElementHandler = XML_SetElementHandler;
1985 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1986 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1987 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1988 capi.SetUserData = XML_SetUserData;
Fredrik Lundhc3345042005-12-13 19:49:55 +00001989
Benjamin Petersonb173f782009-05-05 22:31:58 +00001990 /* export using capsule */
1991 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001992 if (capi_object)
1993 PyModule_AddObject(m, "expat_CAPI", capi_object);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001994 return m;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001995}
1996
Fred Drake6f987622000-08-25 18:03:30 +00001997static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001998clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001999{
Fred Drakecde79132001-04-25 16:01:30 +00002000 int i = 0;
2001 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002002
Fred Drake71b63ff2002-06-28 22:29:01 +00002003 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002004 if (initial)
Fred Drake71b63ff2002-06-28 22:29:01 +00002005 self->handlers[i] = NULL;
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002006 else {
Fred Drakecde79132001-04-25 16:01:30 +00002007 temp = self->handlers[i];
2008 self->handlers[i] = NULL;
2009 Py_XDECREF(temp);
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002010 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002011 }
Fred Drakecde79132001-04-25 16:01:30 +00002012 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002013}
2014
Tim Peters0c322792002-07-17 16:49:03 +00002015static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002016 {"StartElementHandler",
2017 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002018 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002019 {"EndElementHandler",
2020 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002021 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002022 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002023 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2024 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002025 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002026 (xmlhandlersetter)XML_SetCharacterDataHandler,
2027 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002028 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002029 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002030 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002031 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002032 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002033 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002034 {"StartNamespaceDeclHandler",
2035 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002036 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002037 {"EndNamespaceDeclHandler",
2038 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002039 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002040 {"CommentHandler",
2041 (xmlhandlersetter)XML_SetCommentHandler,
2042 (xmlhandler)my_CommentHandler},
2043 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002044 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002045 (xmlhandler)my_StartCdataSectionHandler},
2046 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002047 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002048 (xmlhandler)my_EndCdataSectionHandler},
2049 {"DefaultHandler",
2050 (xmlhandlersetter)XML_SetDefaultHandler,
2051 (xmlhandler)my_DefaultHandler},
2052 {"DefaultHandlerExpand",
2053 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2054 (xmlhandler)my_DefaultHandlerExpandHandler},
2055 {"NotStandaloneHandler",
2056 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2057 (xmlhandler)my_NotStandaloneHandler},
2058 {"ExternalEntityRefHandler",
2059 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002060 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002061 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002062 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002063 (xmlhandler)my_StartDoctypeDeclHandler},
2064 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002065 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002066 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002067 {"EntityDeclHandler",
2068 (xmlhandlersetter)XML_SetEntityDeclHandler,
2069 (xmlhandler)my_EntityDeclHandler},
2070 {"XmlDeclHandler",
2071 (xmlhandlersetter)XML_SetXmlDeclHandler,
2072 (xmlhandler)my_XmlDeclHandler},
2073 {"ElementDeclHandler",
2074 (xmlhandlersetter)XML_SetElementDeclHandler,
2075 (xmlhandler)my_ElementDeclHandler},
2076 {"AttlistDeclHandler",
2077 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2078 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002079#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002080 {"SkippedEntityHandler",
2081 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2082 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002083#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002084
Fred Drake0582df92000-07-12 04:49:00 +00002085 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002086};