blob: a500a1e25d32e7fcc04152290429b16cbb696a36 [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;
Victor Stinner499dfcf2011-03-21 13:26:24 +0100103 PyObject *buffer;
Fred Drake85d835f2001-02-08 15:39:08 +0000104 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
Victor Stinner499dfcf2011-03-21 13:26:24 +0100108 buffer = PyUnicode_FromFormat("%s: line %i, column %i",
109 XML_ErrorString(code), lineno, column);
110 if (buffer == NULL)
111 return NULL;
112 err = PyObject_CallFunction(ErrorObject, "O", buffer);
113 Py_DECREF(buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000114 if ( err != NULL
115 && set_error_attr(err, "code", code)
116 && set_error_attr(err, "offset", column)
117 && set_error_attr(err, "lineno", lineno)) {
118 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000119 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000120 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000121 return NULL;
122}
123
Fred Drake71b63ff2002-06-28 22:29:01 +0000124static int
125have_handler(xmlparseobject *self, int type)
126{
127 PyObject *handler = self->handlers[type];
128 return handler != NULL;
129}
130
131static PyObject *
132get_handler_name(struct HandlerInfo *hinfo)
133{
134 PyObject *name = hinfo->nameobj;
135 if (name == NULL) {
Neal Norwitz392c5be2007-08-25 17:20:32 +0000136 name = PyUnicode_FromString(hinfo->name);
Fred Drake71b63ff2002-06-28 22:29:01 +0000137 hinfo->nameobj = name;
138 }
139 Py_XINCREF(name);
140 return name;
141}
142
Fred Drake85d835f2001-02-08 15:39:08 +0000143
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000144/* Convert a string of XML_Chars into a Unicode string.
145 Returns None if str is a null pointer. */
146
Fred Drake0582df92000-07-12 04:49:00 +0000147static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000148conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000149{
Fred Drake71b63ff2002-06-28 22:29:01 +0000150 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000151 and hence in UTF-8. */
152 /* UTF-8 from Expat, Unicode desired */
153 if (str == NULL) {
154 Py_INCREF(Py_None);
155 return Py_None;
156 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000157 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000158}
159
Fred Drake0582df92000-07-12 04:49:00 +0000160static PyObject *
161conv_string_len_to_unicode(const XML_Char *str, int len)
162{
Fred Drake71b63ff2002-06-28 22:29:01 +0000163 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000164 and hence in UTF-8. */
165 /* UTF-8 from Expat, Unicode desired */
166 if (str == NULL) {
167 Py_INCREF(Py_None);
168 return Py_None;
169 }
Fred Drake6f987622000-08-25 18:03:30 +0000170 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000171}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000172
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000173/* Callback routines */
174
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000175static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000176
Martin v. Löwis069dde22003-01-21 10:58:18 +0000177/* This handler is used when an error has been detected, in the hope
178 that actual parsing can be terminated early. This will only help
179 if an external entity reference is encountered. */
180static int
181error_external_entity_ref_handler(XML_Parser parser,
182 const XML_Char *context,
183 const XML_Char *base,
184 const XML_Char *systemId,
185 const XML_Char *publicId)
186{
187 return 0;
188}
189
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000190/* Dummy character data handler used when an error (exception) has
191 been detected, and the actual parsing can be terminated early.
192 This is needed since character data handler can't be safely removed
193 from within the character data handler, but can be replaced. It is
194 used only from the character data handler trampoline, and must be
195 used right after `flag_error()` is called. */
196static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197noop_character_data_handler(void *userData, const XML_Char *data, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000198{
199 /* Do nothing. */
200}
201
Fred Drake6f987622000-08-25 18:03:30 +0000202static void
203flag_error(xmlparseobject *self)
204{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000205 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000206 XML_SetExternalEntityRefHandler(self->itself,
207 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000208}
209
210static PyCodeObject*
211getcode(enum HandlerTypes slot, char* func_name, int lineno)
212{
Fred Drakebd6101c2001-02-14 18:29:45 +0000213 if (handler_info[slot].tb_code == NULL) {
Fred Drakebd6101c2001-02-14 18:29:45 +0000214 handler_info[slot].tb_code =
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000215 PyCode_NewEmpty(__FILE__, func_name, lineno);
Fred Drakebd6101c2001-02-14 18:29:45 +0000216 }
217 return handler_info[slot].tb_code;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000218}
219
Jeremy Hylton9263f572003-06-27 16:13:17 +0000220#ifdef FIX_TRACE
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000221static int
222trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
223{
224 int result = 0;
225 if (!tstate->use_tracing || tstate->tracing)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return 0;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000227 if (tstate->c_profilefunc != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 tstate->tracing++;
229 result = tstate->c_profilefunc(tstate->c_profileobj,
230 f, code , val);
231 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
232 || (tstate->c_profilefunc != NULL));
233 tstate->tracing--;
234 if (result)
235 return result;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000236 }
237 if (tstate->c_tracefunc != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 tstate->tracing++;
239 result = tstate->c_tracefunc(tstate->c_traceobj,
240 f, code , val);
241 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
242 || (tstate->c_profilefunc != NULL));
243 tstate->tracing--;
244 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000245 return result;
246}
Jeremy Hylton9263f572003-06-27 16:13:17 +0000247
248static int
249trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
250{
251 PyObject *type, *value, *traceback, *arg;
252 int err;
253
254 if (tstate->c_tracefunc == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return 0;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000256
257 PyErr_Fetch(&type, &value, &traceback);
258 if (value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 value = Py_None;
260 Py_INCREF(value);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000261 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000262 arg = PyTuple_Pack(3, type, value, traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000263 if (arg == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyErr_Restore(type, value, traceback);
265 return 0;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000266 }
267 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
268 Py_DECREF(arg);
269 if (err == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 PyErr_Restore(type, value, traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000271 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_XDECREF(type);
273 Py_XDECREF(value);
274 Py_XDECREF(traceback);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000275 }
276 return err;
277}
Martin v. Löwis069dde22003-01-21 10:58:18 +0000278#endif
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000279
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000280static PyObject*
Fred Drake39689c52004-08-13 03:12:57 +0000281call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
282 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000283{
Fred Drakebd6101c2001-02-14 18:29:45 +0000284 PyThreadState *tstate = PyThreadState_GET();
285 PyFrameObject *f;
286 PyObject *res;
287
288 if (c == NULL)
289 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290
Jeremy Hylton9263f572003-06-27 16:13:17 +0000291 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +0000292 if (f == NULL)
293 return NULL;
294 tstate->frame = f;
Jeremy Hylton9263f572003-06-27 16:13:17 +0000295#ifdef FIX_TRACE
296 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 return NULL;
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000298 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000299#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000300 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000301 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (tstate->curexc_traceback == NULL)
303 PyTraceBack_Here(f);
Fred Drake39689c52004-08-13 03:12:57 +0000304 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000305#ifdef FIX_TRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (trace_frame_exc(tstate, f) < 0) {
307 return NULL;
308 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000309 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000310 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
312 Py_XDECREF(res);
313 res = NULL;
314 }
Martin v. Löwis7d6e19d2002-08-04 08:24:49 +0000315 }
Jeremy Hylton9263f572003-06-27 16:13:17 +0000316#else
317 }
Martin v. Löwis069dde22003-01-21 10:58:18 +0000318#endif
Fred Drakebd6101c2001-02-14 18:29:45 +0000319 tstate->frame = f->f_back;
320 Py_DECREF(f);
321 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000322}
323
Fred Drakeb91a36b2002-06-27 19:40:48 +0000324static PyObject*
325string_intern(xmlparseobject *self, const char* str)
326{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000327 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000328 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000329 /* result can be NULL if the unicode conversion failed. */
330 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000332 if (!self->intern)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000334 value = PyDict_GetItem(self->intern, result);
335 if (!value) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (PyDict_SetItem(self->intern, result, result) == 0)
Fred Drakeb91a36b2002-06-27 19:40:48 +0000337 return result;
338 else
339 return NULL;
340 }
341 Py_INCREF(value);
342 Py_DECREF(result);
343 return value;
344}
345
Fred Drake2a3d7db2002-06-28 22:56:48 +0000346/* Return 0 on success, -1 on exception.
347 * flag_error() will be called before return if needed.
348 */
349static int
350call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
351{
352 PyObject *args;
353 PyObject *temp;
354
Georg Brandlc01537f2010-10-15 16:26:08 +0000355 if (!have_handler(self, CharacterData))
356 return -1;
357
Fred Drake2a3d7db2002-06-28 22:56:48 +0000358 args = PyTuple_New(1);
359 if (args == NULL)
360 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000361 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000362 if (temp == NULL) {
363 Py_DECREF(args);
364 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365 XML_SetCharacterDataHandler(self->itself,
366 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000367 return -1;
368 }
369 PyTuple_SET_ITEM(args, 0, temp);
370 /* temp is now a borrowed reference; consider it unused. */
371 self->in_callback = 1;
372 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000373 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000374 /* temp is an owned reference again, or NULL */
375 self->in_callback = 0;
376 Py_DECREF(args);
377 if (temp == NULL) {
378 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000379 XML_SetCharacterDataHandler(self->itself,
380 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000381 return -1;
382 }
383 Py_DECREF(temp);
384 return 0;
385}
386
387static int
388flush_character_buffer(xmlparseobject *self)
389{
390 int rc;
391 if (self->buffer == NULL || self->buffer_used == 0)
392 return 0;
393 rc = call_character_handler(self, self->buffer, self->buffer_used);
394 self->buffer_used = 0;
395 return rc;
396}
397
398static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
Fred Drake2a3d7db2002-06-28 22:56:48 +0000400{
401 xmlparseobject *self = (xmlparseobject *) userData;
402 if (self->buffer == NULL)
403 call_character_handler(self, data, len);
404 else {
405 if ((self->buffer_used + len) > self->buffer_size) {
406 if (flush_character_buffer(self) < 0)
407 return;
408 /* handler might have changed; drop the rest on the floor
409 * if there isn't a handler anymore
410 */
411 if (!have_handler(self, CharacterData))
412 return;
413 }
414 if (len > self->buffer_size) {
415 call_character_handler(self, data, len);
416 self->buffer_used = 0;
417 }
418 else {
419 memcpy(self->buffer + self->buffer_used,
420 data, len * sizeof(XML_Char));
421 self->buffer_used += len;
422 }
423 }
424}
425
Fred Drake85d835f2001-02-08 15:39:08 +0000426static void
427my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000428 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000429{
430 xmlparseobject *self = (xmlparseobject *)userData;
431
Fred Drake71b63ff2002-06-28 22:29:01 +0000432 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000433 PyObject *container, *rv, *args;
434 int i, max;
435
Fred Drake2a3d7db2002-06-28 22:56:48 +0000436 if (flush_character_buffer(self) < 0)
437 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000438 /* Set max to the number of slots filled in atts[]; max/2 is
439 * the number of attributes we need to process.
440 */
441 if (self->specified_attributes) {
442 max = XML_GetSpecifiedAttributeCount(self->itself);
443 }
444 else {
445 max = 0;
446 while (atts[max] != NULL)
447 max += 2;
448 }
449 /* Build the container. */
450 if (self->ordered_attributes)
451 container = PyList_New(max);
452 else
453 container = PyDict_New();
454 if (container == NULL) {
455 flag_error(self);
456 return;
457 }
458 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000459 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000460 PyObject *v;
461 if (n == NULL) {
462 flag_error(self);
463 Py_DECREF(container);
464 return;
465 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000466 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000467 if (v == NULL) {
468 flag_error(self);
469 Py_DECREF(container);
470 Py_DECREF(n);
471 return;
472 }
473 if (self->ordered_attributes) {
474 PyList_SET_ITEM(container, i, n);
475 PyList_SET_ITEM(container, i+1, v);
476 }
477 else if (PyDict_SetItem(container, n, v)) {
478 flag_error(self);
479 Py_DECREF(n);
480 Py_DECREF(v);
481 return;
482 }
483 else {
484 Py_DECREF(n);
485 Py_DECREF(v);
486 }
487 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000488 args = string_intern(self, name);
489 if (args != NULL)
490 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000491 if (args == NULL) {
492 Py_DECREF(container);
493 return;
494 }
495 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000496 self->in_callback = 1;
497 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000498 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000499 self->in_callback = 0;
500 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000501 if (rv == NULL) {
502 flag_error(self);
503 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000504 }
Fred Drake85d835f2001-02-08 15:39:08 +0000505 Py_DECREF(rv);
506 }
507}
508
509#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
510 RETURN, GETUSERDATA) \
511static RC \
512my_##NAME##Handler PARAMS {\
513 xmlparseobject *self = GETUSERDATA ; \
514 PyObject *args = NULL; \
515 PyObject *rv = NULL; \
516 INIT \
517\
Fred Drake71b63ff2002-06-28 22:29:01 +0000518 if (have_handler(self, NAME)) { \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000519 if (flush_character_buffer(self) < 0) \
520 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000521 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000522 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000523 self->in_callback = 1; \
Fred Drake85d835f2001-02-08 15:39:08 +0000524 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
Fred Drake39689c52004-08-13 03:12:57 +0000525 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000526 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000527 Py_DECREF(args); \
528 if (rv == NULL) { \
529 flag_error(self); \
530 return RETURN; \
531 } \
532 CONVERSION \
533 Py_DECREF(rv); \
534 } \
535 return RETURN; \
536}
537
Fred Drake6f987622000-08-25 18:03:30 +0000538#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
540 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000541
Fred Drake6f987622000-08-25 18:03:30 +0000542#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
544 rc = PyLong_AsLong(rv);, rc, \
545 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000546
Fred Drake71b63ff2002-06-28 22:29:01 +0000547VOID_HANDLER(EndElement,
548 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000549 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000550
Fred Drake6f987622000-08-25 18:03:30 +0000551VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000552 (void *userData,
553 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000554 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000555 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000556
Fred Drake6f987622000-08-25 18:03:30 +0000557VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000558 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000559 const XML_Char *entityName,
560 const XML_Char *base,
561 const XML_Char *systemId,
562 const XML_Char *publicId,
563 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000564 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000565 string_intern(self, entityName), string_intern(self, base),
566 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000567 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000568
Fred Drake85d835f2001-02-08 15:39:08 +0000569VOID_HANDLER(EntityDecl,
570 (void *userData,
571 const XML_Char *entityName,
572 int is_parameter_entity,
573 const XML_Char *value,
574 int value_length,
575 const XML_Char *base,
576 const XML_Char *systemId,
577 const XML_Char *publicId,
578 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000579 ("NiNNNNN",
580 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000581 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000582 string_intern(self, base), string_intern(self, systemId),
583 string_intern(self, publicId),
584 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000585
586VOID_HANDLER(XmlDecl,
587 (void *userData,
588 const XML_Char *version,
589 const XML_Char *encoding,
590 int standalone),
591 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000592 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000593 standalone))
594
595static PyObject *
596conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000597 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000598{
599 PyObject *result = NULL;
600 PyObject *children = PyTuple_New(model->numchildren);
601 int i;
602
603 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000604 assert(model->numchildren < INT_MAX);
605 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000606 PyObject *child = conv_content_model(&model->children[i],
607 conv_string);
608 if (child == NULL) {
609 Py_XDECREF(children);
610 return NULL;
611 }
612 PyTuple_SET_ITEM(children, i, child);
613 }
614 result = Py_BuildValue("(iiO&N)",
615 model->type, model->quant,
616 conv_string,model->name, children);
617 }
618 return result;
619}
620
Fred Drake06dd8cf2003-02-02 03:54:17 +0000621static void
622my_ElementDeclHandler(void *userData,
623 const XML_Char *name,
624 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000625{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000626 xmlparseobject *self = (xmlparseobject *)userData;
627 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000628
Fred Drake06dd8cf2003-02-02 03:54:17 +0000629 if (have_handler(self, ElementDecl)) {
630 PyObject *rv = NULL;
631 PyObject *modelobj, *nameobj;
632
633 if (flush_character_buffer(self) < 0)
634 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000635 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000636 if (modelobj == NULL) {
637 flag_error(self);
638 goto finally;
639 }
640 nameobj = string_intern(self, name);
641 if (nameobj == NULL) {
642 Py_DECREF(modelobj);
643 flag_error(self);
644 goto finally;
645 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000646 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000647 if (args == NULL) {
648 Py_DECREF(modelobj);
649 flag_error(self);
650 goto finally;
651 }
652 self->in_callback = 1;
653 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
Fred Drake39689c52004-08-13 03:12:57 +0000654 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000655 self->in_callback = 0;
656 if (rv == NULL) {
657 flag_error(self);
658 goto finally;
659 }
660 Py_DECREF(rv);
661 }
662 finally:
663 Py_XDECREF(args);
664 XML_FreeContentModel(self->itself, model);
665 return;
666}
Fred Drake85d835f2001-02-08 15:39:08 +0000667
668VOID_HANDLER(AttlistDecl,
669 (void *userData,
670 const XML_Char *elname,
671 const XML_Char *attname,
672 const XML_Char *att_type,
673 const XML_Char *dflt,
674 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000675 ("(NNO&O&i)",
676 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000677 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000678 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000679
Martin v. Löwisc847f402003-01-21 11:09:21 +0000680#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000681VOID_HANDLER(SkippedEntity,
682 (void *userData,
683 const XML_Char *entityName,
684 int is_parameter_entity),
685 ("Ni",
686 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000687#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000688
Fred Drake71b63ff2002-06-28 22:29:01 +0000689VOID_HANDLER(NotationDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 (void *userData,
691 const XML_Char *notationName,
692 const XML_Char *base,
693 const XML_Char *systemId,
694 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000695 ("(NNNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 string_intern(self, notationName), string_intern(self, base),
697 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000698
Fred Drake6f987622000-08-25 18:03:30 +0000699VOID_HANDLER(StartNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 (void *userData,
701 const XML_Char *prefix,
702 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000703 ("(NN)",
704 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000705
Fred Drake6f987622000-08-25 18:03:30 +0000706VOID_HANDLER(EndNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 (void *userData,
708 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000709 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000710
Fred Drake6f987622000-08-25 18:03:30 +0000711VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000712 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000713 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000714
Fred Drake6f987622000-08-25 18:03:30 +0000715VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000716 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000718
Fred Drake6f987622000-08-25 18:03:30 +0000719VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000720 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000722
Fred Drake6f987622000-08-25 18:03:30 +0000723VOID_HANDLER(Default,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 (void *userData, const XML_Char *s, int len),
725 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000726
Fred Drake6f987622000-08-25 18:03:30 +0000727VOID_HANDLER(DefaultHandlerExpand,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 (void *userData, const XML_Char *s, int len),
729 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000730
Fred Drake71b63ff2002-06-28 22:29:01 +0000731INT_HANDLER(NotStandalone,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 (void *userData),
733 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000734
Fred Drake6f987622000-08-25 18:03:30 +0000735RC_HANDLER(int, ExternalEntityRef,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 (XML_Parser parser,
737 const XML_Char *context,
738 const XML_Char *base,
739 const XML_Char *systemId,
740 const XML_Char *publicId),
741 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000742 ("(O&NNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 conv_string_to_unicode ,context, string_intern(self, base),
744 string_intern(self, systemId), string_intern(self, publicId)),
745 rc = PyLong_AsLong(rv);, rc,
746 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000747
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000748/* XXX UnknownEncodingHandler */
749
Fred Drake85d835f2001-02-08 15:39:08 +0000750VOID_HANDLER(StartDoctypeDecl,
751 (void *userData, const XML_Char *doctypeName,
752 const XML_Char *sysid, const XML_Char *pubid,
753 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000754 ("(NNNi)", string_intern(self, doctypeName),
755 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000756 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000757
758VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000759
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000760/* ---------------------------------------------------------------- */
761
Fred Drake71b63ff2002-06-28 22:29:01 +0000762static PyObject *
763get_parse_result(xmlparseobject *self, int rv)
764{
765 if (PyErr_Occurred()) {
766 return NULL;
767 }
768 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000769 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000770 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000771 if (flush_character_buffer(self) < 0) {
772 return NULL;
773 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000774 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000775}
776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000777PyDoc_STRVAR(xmlparse_Parse__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000778"Parse(data[, isfinal])\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000779Parse XML data. `isfinal' should be true at end of input.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000780
781static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000782xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000783{
Fred Drake0582df92000-07-12 04:49:00 +0000784 char *s;
785 int slen;
786 int isFinal = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000787
Fred Drake0582df92000-07-12 04:49:00 +0000788 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
789 return NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +0000790
791 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000792}
793
Fred Drakeca1f4262000-09-21 20:10:23 +0000794/* File reading copied from cPickle */
795
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000796#define BUF_SIZE 2048
797
Fred Drake0582df92000-07-12 04:49:00 +0000798static int
799readinst(char *buf, int buf_size, PyObject *meth)
800{
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000801 PyObject *str;
802 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000803 char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000804
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000805 str = PyObject_CallFunction(meth, "n", buf_size);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000806 if (str == NULL)
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000807 goto error;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000808
Christian Heimes72b710a2008-05-26 13:28:38 +0000809 if (PyBytes_Check(str))
810 ptr = PyBytes_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000811 else if (PyByteArray_Check(str))
812 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000813 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000814 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000815 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000816 Py_TYPE(str)->tp_name);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000817 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000818 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000819 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000820 if (len > buf_size) {
821 PyErr_Format(PyExc_ValueError,
822 "read() returned too much data: "
Victor Stinner9d6f9362011-01-04 22:00:04 +0000823 "%i bytes requested, %zd returned",
Fred Drake0582df92000-07-12 04:49:00 +0000824 buf_size, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000825 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000826 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000827 memcpy(buf, ptr, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000828 Py_DECREF(str);
829 /* len <= buf_size <= INT_MAX */
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000830 return (int)len;
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000831
832error:
833 Py_XDECREF(str);
834 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000835}
836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000837PyDoc_STRVAR(xmlparse_ParseFile__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000838"ParseFile(file)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000839Parse XML data from file-like object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000840
841static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000842xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000843{
Fred Drake0582df92000-07-12 04:49:00 +0000844 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000845 PyObject *readmethod = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200846 _Py_IDENTIFIER(read);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000847
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200848 readmethod = _PyObject_GetAttrId(f, &PyId_read);
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000849 if (readmethod == NULL) {
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000850 PyErr_SetString(PyExc_TypeError,
851 "argument must have 'read' attribute");
852 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000853 }
854 for (;;) {
855 int bytes_read;
856 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000857 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000858 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000859 return PyErr_NoMemory();
Fred Drake7b6caff2003-07-21 17:05:56 +0000860 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000861
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000862 bytes_read = readinst(buf, BUF_SIZE, readmethod);
863 if (bytes_read < 0) {
864 Py_DECREF(readmethod);
865 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000866 }
867 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000868 if (PyErr_Occurred()) {
869 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000870 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000871 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000872
Fred Drake0582df92000-07-12 04:49:00 +0000873 if (!rv || bytes_read == 0)
874 break;
875 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000876 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000877 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000878}
879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880PyDoc_STRVAR(xmlparse_SetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000881"SetBase(base_url)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882Set the base URL for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000883
884static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000885xmlparse_SetBase(xmlparseobject *self, PyObject *args)
886{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000887 char *base;
888
Fred Drake0582df92000-07-12 04:49:00 +0000889 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000890 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000891 if (!XML_SetBase(self->itself, base)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000893 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000894 Py_INCREF(Py_None);
895 return Py_None;
896}
897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000898PyDoc_STRVAR(xmlparse_GetBase__doc__,
Thomas Wouters35317302000-07-22 16:34:15 +0000899"GetBase() -> url\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900Return base URL string for the parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000901
902static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000903xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
Fred Drake0582df92000-07-12 04:49:00 +0000904{
Fred Drake0582df92000-07-12 04:49:00 +0000905 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000906}
907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000908PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
Fred Drakebd6101c2001-02-14 18:29:45 +0000909"GetInputContext() -> string\n\
910Return the untranslated text of the input that caused the current event.\n\
911If 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 +0000912for an element with many attributes), not all of the text may be available.");
Fred Drakebd6101c2001-02-14 18:29:45 +0000913
914static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000915xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
Fred Drakebd6101c2001-02-14 18:29:45 +0000916{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000917 if (self->in_callback) {
918 int offset, size;
919 const char *buffer
920 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000921
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000922 if (buffer != NULL)
Christian Heimes72b710a2008-05-26 13:28:38 +0000923 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000924 size - offset);
925 else
926 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000927 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000928 else
929 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000930}
Fred Drakebd6101c2001-02-14 18:29:45 +0000931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
Fred Drake2d4ac202001-01-03 15:36:25 +0000933"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000934Create a parser for parsing an external entity based on the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935information passed to the ExternalEntityRefHandler.");
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000936
937static PyObject *
938xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
939{
940 char *context;
941 char *encoding = NULL;
942 xmlparseobject *new_parser;
943 int i;
944
Martin v. Löwisc57428d2001-09-19 09:55:09 +0000945 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
Fred Drakecde79132001-04-25 16:01:30 +0000946 &context, &encoding)) {
947 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000948 }
949
Martin v. Löwis894258c2001-09-23 10:20:10 +0000950 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake85d835f2001-02-08 15:39:08 +0000951 if (new_parser == NULL)
952 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000953 new_parser->buffer_size = self->buffer_size;
954 new_parser->buffer_used = 0;
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000955 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000956 new_parser->ordered_attributes = self->ordered_attributes;
957 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000958 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +0000959 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000960 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 encoding);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000962 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000963 new_parser->intern = self->intern;
964 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000965 PyObject_GC_Track(new_parser);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000966
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000967 if (self->buffer != NULL) {
968 new_parser->buffer = malloc(new_parser->buffer_size);
969 if (new_parser->buffer == NULL) {
970 Py_DECREF(new_parser);
971 return PyErr_NoMemory();
972 }
973 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000974 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000975 Py_DECREF(new_parser);
976 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000977 }
978
979 XML_SetUserData(new_parser->itself, (void *)new_parser);
980
981 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +0000982 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000983 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000984
Fred Drake2a3d7db2002-06-28 22:56:48 +0000985 new_parser->handlers = malloc(sizeof(PyObject *) * i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000986 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000987 Py_DECREF(new_parser);
988 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000989 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000990 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000991
992 /* then copy handlers from self */
993 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000994 PyObject *handler = self->handlers[i];
995 if (handler != NULL) {
996 Py_INCREF(handler);
997 new_parser->handlers[i] = handler;
998 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +0000999 handler_info[i].handler);
1000 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001001 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001002 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001003}
1004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001005PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001006"SetParamEntityParsing(flag) -> success\n\
1007Controls parsing of parameter entities (including the external DTD\n\
1008subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1009XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1010XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011was successful.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001012
1013static PyObject*
Fred Drakebd6101c2001-02-14 18:29:45 +00001014xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001015{
Fred Drake85d835f2001-02-08 15:39:08 +00001016 int flag;
1017 if (!PyArg_ParseTuple(args, "i", &flag))
1018 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001019 flag = XML_SetParamEntityParsing(p->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001020 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001021}
1022
Martin v. Löwisc847f402003-01-21 11:09:21 +00001023
1024#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001025PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1026"UseForeignDTD([flag])\n\
1027Allows the application to provide an artificial external subset if one is\n\
1028not specified as part of the document instance. This readily allows the\n\
1029use of a 'default' document type controlled by the application, while still\n\
1030getting the advantage of providing document type information to the parser.\n\
1031'flag' defaults to True if not provided.");
1032
1033static PyObject *
1034xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1035{
1036 PyObject *flagobj = NULL;
1037 XML_Bool flag = XML_TRUE;
1038 enum XML_Error rc;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001039 if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
Martin v. Löwis069dde22003-01-21 10:58:18 +00001040 return NULL;
1041 if (flagobj != NULL)
1042 flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1043 rc = XML_UseForeignDTD(self->itself, flag);
1044 if (rc != XML_ERROR_NONE) {
1045 return set_error(self, rc);
1046 }
1047 Py_INCREF(Py_None);
1048 return Py_None;
1049}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001050#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001051
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001052static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs);
1053
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001054static struct PyMethodDef xmlparse_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 {"Parse", (PyCFunction)xmlparse_Parse,
1056 METH_VARARGS, xmlparse_Parse__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001057 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 METH_O, xmlparse_ParseFile__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001059 {"SetBase", (PyCFunction)xmlparse_SetBase,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 METH_VARARGS, xmlparse_SetBase__doc__},
Fred Drake0582df92000-07-12 04:49:00 +00001061 {"GetBase", (PyCFunction)xmlparse_GetBase,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 METH_NOARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001063 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001065 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +00001067 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 METH_NOARGS, xmlparse_GetInputContext__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001069#if XML_COMBINED_VERSION >= 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001070 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001072#endif
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001073 {"__dir__", xmlparse_dir, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001075};
1076
1077/* ---------- */
1078
1079
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001080
Fred Drake71b63ff2002-06-28 22:29:01 +00001081/* pyexpat international encoding support.
1082 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001083*/
1084
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001085static char template_buffer[257];
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001086
Fred Drake71b63ff2002-06-28 22:29:01 +00001087static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001088init_template_buffer(void)
1089{
1090 int i;
Fred Drakebb66a202001-03-01 20:48:17 +00001091 for (i = 0; i < 256; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001093 }
Fred Drakebb66a202001-03-01 20:48:17 +00001094 template_buffer[256] = 0;
Tim Peters63cb99e2001-02-17 18:12:50 +00001095}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001096
Fred Drake71b63ff2002-06-28 22:29:01 +00001097static int
1098PyUnknownEncodingHandler(void *encodingHandlerData,
1099 const XML_Char *name,
1100 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001101{
Fred Drakebb66a202001-03-01 20:48:17 +00001102 PyUnicodeObject *_u_string = NULL;
1103 int result = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001104 int i;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001105 int kind;
1106 void *data;
Fred Drake71b63ff2002-06-28 22:29:01 +00001107
Fred Drakebb66a202001-03-01 20:48:17 +00001108 /* Yes, supports only 8bit encodings */
1109 _u_string = (PyUnicodeObject *)
1110 PyUnicode_Decode(template_buffer, 256, name, "replace");
Fred Drake71b63ff2002-06-28 22:29:01 +00001111
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001112 if (_u_string == NULL || PyUnicode_READY(_u_string) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 return result;
Fred Drake71b63ff2002-06-28 22:29:01 +00001114
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001115 kind = PyUnicode_KIND(_u_string);
1116 data = PyUnicode_DATA(_u_string);
1117
Fred Drakebb66a202001-03-01 20:48:17 +00001118 for (i = 0; i < 256; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* Stupid to access directly, but fast */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001120 Py_UCS4 c = PyUnicode_READ(kind, data, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
1122 info->map[i] = -1;
1123 else
1124 info->map[i] = c;
Tim Peters63cb99e2001-02-17 18:12:50 +00001125 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001126 info->data = NULL;
1127 info->convert = NULL;
1128 info->release = NULL;
Fred Drake71b63ff2002-06-28 22:29:01 +00001129 result = 1;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001130 Py_DECREF(_u_string);
1131 return result;
1132}
1133
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001134
1135static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +00001136newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001137{
1138 int i;
1139 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001140
Martin v. Löwis894258c2001-09-23 10:20:10 +00001141 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake0582df92000-07-12 04:49:00 +00001142 if (self == NULL)
1143 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001144
Fred Drake2a3d7db2002-06-28 22:56:48 +00001145 self->buffer = NULL;
1146 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1147 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001148 self->ordered_attributes = 0;
1149 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001150 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001151 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001152 self->handlers = NULL;
Fred Drakecde79132001-04-25 16:01:30 +00001153 if (namespace_separator != NULL) {
Fred Drake0582df92000-07-12 04:49:00 +00001154 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1155 }
Fred Drake85d835f2001-02-08 15:39:08 +00001156 else {
Fred Drake0582df92000-07-12 04:49:00 +00001157 self->itself = XML_ParserCreate(encoding);
1158 }
Gregory P. Smith25227712012-03-14 18:10:37 -07001159#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT)
1160 /* This feature was added upstream in libexpat 2.1.0. Our expat copy
1161 * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT
1162 * to indicate that we can still use it. */
Gregory P. Smith8e91cf62012-03-14 14:26:55 -07001163 XML_SetHashSalt(self->itself,
1164 (unsigned long)_Py_HashSecret.prefix);
Gregory P. Smith25227712012-03-14 18:10:37 -07001165#endif
Fred Drakeb91a36b2002-06-27 19:40:48 +00001166 self->intern = intern;
1167 Py_XINCREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001168 PyObject_GC_Track(self);
Fred Drake0582df92000-07-12 04:49:00 +00001169 if (self->itself == NULL) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001170 PyErr_SetString(PyExc_RuntimeError,
Fred Drake0582df92000-07-12 04:49:00 +00001171 "XML_ParserCreate failed");
1172 Py_DECREF(self);
1173 return NULL;
1174 }
1175 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001176 XML_SetUnknownEncodingHandler(self->itself,
1177 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001178
Fred Drake2a3d7db2002-06-28 22:56:48 +00001179 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001180 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001181
Fred Drake7c75bf22002-07-01 14:02:31 +00001182 self->handlers = malloc(sizeof(PyObject *) * i);
1183 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001184 Py_DECREF(self);
1185 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001186 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001187 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001188
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001189 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001190}
1191
1192
1193static void
Fred Drake0582df92000-07-12 04:49:00 +00001194xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001195{
Fred Drake0582df92000-07-12 04:49:00 +00001196 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001197 PyObject_GC_UnTrack(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001198 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001199 XML_ParserFree(self->itself);
1200 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001201
Fred Drake85d835f2001-02-08 15:39:08 +00001202 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001203 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001204 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001205 temp = self->handlers[i];
1206 self->handlers[i] = NULL;
1207 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001208 }
1209 free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001210 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001211 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001212 if (self->buffer != NULL) {
1213 free(self->buffer);
1214 self->buffer = NULL;
1215 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001216 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001217 PyObject_GC_Del(self);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001218}
1219
Fred Drake0582df92000-07-12 04:49:00 +00001220static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001221handlername2int(PyObject *name)
Fred Drake0582df92000-07-12 04:49:00 +00001222{
1223 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001224 for (i = 0; handler_info[i].name != NULL; i++) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001225 if (PyUnicode_CompareWithASCIIString(
1226 name, handler_info[i].name) == 0) {
Fred Drake0582df92000-07-12 04:49:00 +00001227 return i;
1228 }
1229 }
1230 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001231}
1232
1233static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001234get_pybool(int istrue)
1235{
1236 PyObject *result = istrue ? Py_True : Py_False;
1237 Py_INCREF(result);
1238 return result;
1239}
1240
1241static PyObject *
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001242xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001243{
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001244 Py_UCS4 first_char;
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001245 int handlernum = -1;
1246
Alexander Belopolskye239d232010-12-08 23:31:48 +00001247 if (!PyUnicode_Check(nameobj))
1248 goto generic;
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001249 if (PyUnicode_READY(nameobj))
1250 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251
Alexander Belopolskye239d232010-12-08 23:31:48 +00001252 handlernum = handlername2int(nameobj);
Fred Drake71b63ff2002-06-28 22:29:01 +00001253
1254 if (handlernum != -1) {
1255 PyObject *result = self->handlers[handlernum];
1256 if (result == NULL)
1257 result = Py_None;
1258 Py_INCREF(result);
1259 return result;
1260 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001261
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001262 first_char = PyUnicode_READ_CHAR(nameobj, 0);
1263 if (first_char == 'E') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001264 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001265 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001266 XML_GetErrorCode(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001267 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001268 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001269 XML_GetErrorLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001270 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001271 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001272 XML_GetErrorColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001273 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001274 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001275 XML_GetErrorByteIndex(self->itself));
1276 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001277 if (first_char == 'C') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001278 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001279 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001280 XML_GetCurrentLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001281 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001282 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001283 XML_GetCurrentColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001284 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001285 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001286 XML_GetCurrentByteIndex(self->itself));
1287 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001288 if (first_char == 'b') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001289 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001290 return PyLong_FromLong((long) self->buffer_size);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001291 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_text") == 0)
Fred Drake2a3d7db2002-06-28 22:56:48 +00001292 return get_pybool(self->buffer != NULL);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001293 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001294 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001295 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001296 if (PyUnicode_CompareWithASCIIString(nameobj, "namespace_prefixes") == 0)
Martin v. Löwis069dde22003-01-21 10:58:18 +00001297 return get_pybool(self->ns_prefixes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001298 if (PyUnicode_CompareWithASCIIString(nameobj, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001299 return get_pybool(self->ordered_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001300 if (PyUnicode_CompareWithASCIIString(nameobj, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001301 return get_pybool((long) self->specified_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001302 if (PyUnicode_CompareWithASCIIString(nameobj, "intern") == 0) {
Fred Drakeb91a36b2002-06-27 19:40:48 +00001303 if (self->intern == NULL) {
1304 Py_INCREF(Py_None);
1305 return Py_None;
1306 }
1307 else {
1308 Py_INCREF(self->intern);
1309 return self->intern;
1310 }
1311 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001312 generic:
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001313 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001314}
1315
1316static PyObject *
1317xmlparse_dir(PyObject *self, PyObject* noargs)
1318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319#define APPEND(list, str) \
1320 do { \
1321 PyObject *o = PyUnicode_FromString(str); \
1322 if (o != NULL) \
1323 PyList_Append(list, o); \
1324 Py_XDECREF(o); \
Martin v. Löwis069dde22003-01-21 10:58:18 +00001325 } while (0)
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001326
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001327 int i;
1328 PyObject *rc = PyList_New(0);
1329 if (!rc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 return NULL;
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001331 for (i = 0; handler_info[i].name != NULL; i++) {
1332 PyObject *o = get_handler_name(&handler_info[i]);
1333 if (o != NULL)
1334 PyList_Append(rc, o);
1335 Py_XDECREF(o);
1336 }
1337 APPEND(rc, "ErrorCode");
1338 APPEND(rc, "ErrorLineNumber");
1339 APPEND(rc, "ErrorColumnNumber");
1340 APPEND(rc, "ErrorByteIndex");
1341 APPEND(rc, "CurrentLineNumber");
1342 APPEND(rc, "CurrentColumnNumber");
1343 APPEND(rc, "CurrentByteIndex");
1344 APPEND(rc, "buffer_size");
1345 APPEND(rc, "buffer_text");
1346 APPEND(rc, "buffer_used");
1347 APPEND(rc, "namespace_prefixes");
1348 APPEND(rc, "ordered_attributes");
1349 APPEND(rc, "specified_attributes");
1350 APPEND(rc, "intern");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001351
Neal Norwitzfa56e2d2003-01-19 15:40:09 +00001352#undef APPEND
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001353
1354 if (PyErr_Occurred()) {
1355 Py_DECREF(rc);
1356 rc = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001357 }
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001358
1359 return rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001360}
1361
Fred Drake6f987622000-08-25 18:03:30 +00001362static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001363sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001364{
1365 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001366 if (handlernum >= 0) {
1367 xmlhandler c_handler = NULL;
1368 PyObject *temp = self->handlers[handlernum];
1369
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001370 if (v == Py_None) {
1371 /* If this is the character data handler, and a character
1372 data handler is already active, we need to be more
1373 careful. What we can safely do is replace the existing
1374 character data handler callback function with a no-op
1375 function that will refuse to call Python. The downside
1376 is that this doesn't completely remove the character
1377 data handler from the C layer if there's any callback
1378 active, so Expat does a little more work than it
1379 otherwise would, but that's really an odd case. A more
1380 elaborate system of handlers and state could remove the
1381 C handler more effectively. */
1382 if (handlernum == CharacterData && self->in_callback)
1383 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001384 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001385 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001386 else if (v != NULL) {
1387 Py_INCREF(v);
1388 c_handler = handler_info[handlernum].handler;
1389 }
Fred Drake0582df92000-07-12 04:49:00 +00001390 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001391 Py_XDECREF(temp);
1392 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001393 return 1;
1394 }
1395 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001396}
1397
1398static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001399xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001400{
Fred Drake6f987622000-08-25 18:03:30 +00001401 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001402 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001403 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1404 return -1;
1405 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001406 assert(PyUnicode_Check(name));
1407 if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001408 if (PyObject_IsTrue(v)) {
1409 if (self->buffer == NULL) {
1410 self->buffer = malloc(self->buffer_size);
1411 if (self->buffer == NULL) {
1412 PyErr_NoMemory();
1413 return -1;
1414 }
1415 self->buffer_used = 0;
1416 }
1417 }
1418 else if (self->buffer != NULL) {
1419 if (flush_character_buffer(self) < 0)
1420 return -1;
1421 free(self->buffer);
1422 self->buffer = NULL;
1423 }
1424 return 0;
1425 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001426 if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +00001427 if (PyObject_IsTrue(v))
1428 self->ns_prefixes = 1;
1429 else
1430 self->ns_prefixes = 0;
1431 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1432 return 0;
1433 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001434 if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001435 if (PyObject_IsTrue(v))
1436 self->ordered_attributes = 1;
1437 else
1438 self->ordered_attributes = 0;
1439 return 0;
1440 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001441 if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001442 if (PyObject_IsTrue(v))
1443 self->specified_attributes = 1;
1444 else
1445 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001446 return 0;
1447 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001448
Alexander Belopolskye239d232010-12-08 23:31:48 +00001449 if (PyUnicode_CompareWithASCIIString(name, "buffer_size") == 0) {
Christian Heimes2380ac72008-01-09 00:17:24 +00001450 long new_buffer_size;
1451 if (!PyLong_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1453 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001454 }
1455
1456 new_buffer_size=PyLong_AS_LONG(v);
1457 /* trivial case -- no change */
1458 if (new_buffer_size == self->buffer_size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 return 0;
Christian Heimes2380ac72008-01-09 00:17:24 +00001460 }
1461
1462 if (new_buffer_size <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1464 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001465 }
1466
1467 /* check maximum */
1468 if (new_buffer_size > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 char errmsg[100];
1470 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1471 PyErr_SetString(PyExc_ValueError, errmsg);
1472 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001473 }
1474
1475 if (self->buffer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 /* there is already a buffer */
1477 if (self->buffer_used != 0) {
1478 flush_character_buffer(self);
1479 }
1480 /* free existing buffer */
1481 free(self->buffer);
Christian Heimes2380ac72008-01-09 00:17:24 +00001482 }
1483 self->buffer = malloc(new_buffer_size);
1484 if (self->buffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 PyErr_NoMemory();
1486 return -1;
1487 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001488 self->buffer_size = new_buffer_size;
1489 return 0;
1490 }
1491
Alexander Belopolskye239d232010-12-08 23:31:48 +00001492 if (PyUnicode_CompareWithASCIIString(name, "CharacterDataHandler") == 0) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001493 /* If we're changing the character data handler, flush all
1494 * cached data with the old handler. Not sure there's a
1495 * "right" thing to do, though, but this probably won't
1496 * happen.
1497 */
1498 if (flush_character_buffer(self) < 0)
1499 return -1;
1500 }
Fred Drake6f987622000-08-25 18:03:30 +00001501 if (sethandler(self, name, v)) {
1502 return 0;
1503 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001504 PyErr_SetObject(PyExc_AttributeError, name);
Fred Drake6f987622000-08-25 18:03:30 +00001505 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001506}
1507
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001508static int
1509xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1510{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511 int i;
1512 for (i = 0; handler_info[i].name != NULL; i++)
1513 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001514 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001515}
1516
1517static int
1518xmlparse_clear(xmlparseobject *op)
1519{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001520 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001521 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001522 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001523}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001526
1527static PyTypeObject Xmlparsetype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 PyVarObject_HEAD_INIT(NULL, 0)
1529 "pyexpat.xmlparser", /*tp_name*/
Antoine Pitrou23683ef2011-01-04 00:00:31 +00001530 sizeof(xmlparseobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 0, /*tp_itemsize*/
1532 /* methods */
1533 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1534 (printfunc)0, /*tp_print*/
1535 0, /*tp_getattr*/
Alexander Belopolskye239d232010-12-08 23:31:48 +00001536 0, /*tp_setattr*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 0, /*tp_reserved*/
1538 (reprfunc)0, /*tp_repr*/
1539 0, /*tp_as_number*/
1540 0, /*tp_as_sequence*/
1541 0, /*tp_as_mapping*/
1542 (hashfunc)0, /*tp_hash*/
1543 (ternaryfunc)0, /*tp_call*/
1544 (reprfunc)0, /*tp_str*/
1545 (getattrofunc)xmlparse_getattro, /* tp_getattro */
Alexander Belopolskye239d232010-12-08 23:31:48 +00001546 (setattrofunc)xmlparse_setattro, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 0, /* tp_as_buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Xmlparsetype__doc__, /* tp_doc - Documentation string */
1550 (traverseproc)xmlparse_traverse, /* tp_traverse */
1551 (inquiry)xmlparse_clear, /* tp_clear */
1552 0, /* tp_richcompare */
1553 0, /* tp_weaklistoffset */
1554 0, /* tp_iter */
1555 0, /* tp_iternext */
1556 xmlparse_methods, /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001557};
1558
1559/* End of code for xmlparser objects */
1560/* -------------------------------------------------------- */
1561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001562PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001563"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564Return a new XML parser object.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001565
1566static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001567pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1568{
Fred Drakecde79132001-04-25 16:01:30 +00001569 char *encoding = NULL;
1570 char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001571 PyObject *intern = NULL;
1572 PyObject *result;
1573 int intern_decref = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001574 static char *kwlist[] = {"encoding", "namespace_separator",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001575 "intern", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001576
Fred Drakeb91a36b2002-06-27 19:40:48 +00001577 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1578 &encoding, &namespace_separator, &intern))
Fred Drakecde79132001-04-25 16:01:30 +00001579 return NULL;
1580 if (namespace_separator != NULL
1581 && strlen(namespace_separator) > 1) {
1582 PyErr_SetString(PyExc_ValueError,
1583 "namespace_separator must be at most one"
1584 " character, omitted, or None");
1585 return NULL;
1586 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001587 /* Explicitly passing None means no interning is desired.
1588 Not passing anything means that a new dictionary is used. */
1589 if (intern == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 intern = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001591 else if (intern == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 intern = PyDict_New();
1593 if (!intern)
1594 return NULL;
1595 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001596 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001597 else if (!PyDict_Check(intern)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1599 return NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001600 }
1601
1602 result = newxmlparseobject(encoding, namespace_separator, intern);
1603 if (intern_decref) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 Py_DECREF(intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001605 }
1606 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001607}
1608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Fred Drake0582df92000-07-12 04:49:00 +00001610"ErrorString(errno) -> string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001611Returns string error for given number.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001612
1613static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001614pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001615{
Fred Drake0582df92000-07-12 04:49:00 +00001616 long code = 0;
1617
1618 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1619 return NULL;
1620 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001621}
1622
1623/* List of methods defined in the module */
1624
1625static struct PyMethodDef pyexpat_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
Fred Drake0582df92000-07-12 04:49:00 +00001627 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1629 METH_VARARGS, pyexpat_ErrorString__doc__},
Fred Drake71b63ff2002-06-28 22:29:01 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001632};
1633
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001634/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636PyDoc_STRVAR(pyexpat_module_documentation,
1637"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001638
Fred Drakecde79132001-04-25 16:01:30 +00001639/* Initialization function for the module */
1640
1641#ifndef MODULE_NAME
1642#define MODULE_NAME "pyexpat"
1643#endif
1644
1645#ifndef MODULE_INITFUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001646#define MODULE_INITFUNC PyInit_pyexpat
Fred Drakecde79132001-04-25 16:01:30 +00001647#endif
1648
Martin v. Löwis069dde22003-01-21 10:58:18 +00001649#ifndef PyMODINIT_FUNC
1650# ifdef MS_WINDOWS
1651# define PyMODINIT_FUNC __declspec(dllexport) void
1652# else
1653# define PyMODINIT_FUNC void
1654# endif
1655#endif
1656
Mark Hammond8235ea12002-07-19 06:55:41 +00001657PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
Fred Drakecde79132001-04-25 16:01:30 +00001658
Martin v. Löwis1a214512008-06-11 05:26:20 +00001659static struct PyModuleDef pyexpatmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyModuleDef_HEAD_INIT,
1661 MODULE_NAME,
1662 pyexpat_module_documentation,
1663 -1,
1664 pyexpat_methods,
1665 NULL,
1666 NULL,
1667 NULL,
1668 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001669};
1670
Martin v. Löwis069dde22003-01-21 10:58:18 +00001671PyMODINIT_FUNC
1672MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001673{
1674 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001675 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001676 PyObject *errors_module;
1677 PyObject *modelmod_name;
1678 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001679 PyObject *sys_modules;
Georg Brandlb4dac712010-10-15 14:46:48 +00001680 PyObject *tmpnum, *tmpstr;
1681 PyObject *codes_dict;
1682 PyObject *rev_codes_dict;
1683 int res;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001684 static struct PyExpat_CAPI capi;
Georg Brandlb4dac712010-10-15 14:46:48 +00001685 PyObject *capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001686
Fred Drake6f987622000-08-25 18:03:30 +00001687 if (errmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001688 return NULL;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001689 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001690 if (modelmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001691 return NULL;
Fred Drake6f987622000-08-25 18:03:30 +00001692
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001693 if (PyType_Ready(&Xmlparsetype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001695
Fred Drake0582df92000-07-12 04:49:00 +00001696 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001697 m = PyModule_Create(&pyexpatmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001698 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001700
Fred Drake0582df92000-07-12 04:49:00 +00001701 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001702 if (ErrorObject == NULL) {
1703 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001704 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001705 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001706 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001707 }
1708 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001709 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001710 Py_INCREF(ErrorObject);
1711 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001712 Py_INCREF(&Xmlparsetype);
1713 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001714
Fred Drake738293d2000-12-21 17:25:07 +00001715 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1716 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001717 {
1718 XML_Expat_Version info = XML_ExpatVersionInfo();
1719 PyModule_AddObject(m, "version_info",
1720 Py_BuildValue("(iii)", info.major,
1721 info.minor, info.micro));
1722 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001723 init_template_buffer();
Fred Drake0582df92000-07-12 04:49:00 +00001724 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001725 compiled, this should check and set native_encoding
1726 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001727 */
Fred Drake93adb692000-09-23 04:55:48 +00001728 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001729
Fred Drake85d835f2001-02-08 15:39:08 +00001730 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001731 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001732 errors_module = PyDict_GetItem(d, errmod_name);
1733 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001734 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001735 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001736 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001737 /* gives away the reference to errors_module */
1738 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001739 }
1740 }
Fred Drake6f987622000-08-25 18:03:30 +00001741 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001742 model_module = PyDict_GetItem(d, modelmod_name);
1743 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001744 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001745 if (model_module != NULL) {
1746 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1747 /* gives away the reference to model_module */
1748 PyModule_AddObject(m, "model", model_module);
1749 }
1750 }
1751 Py_DECREF(modelmod_name);
1752 if (errors_module == NULL || model_module == NULL)
1753 /* Don't core dump later! */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001754 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755
Martin v. Löwisc847f402003-01-21 11:09:21 +00001756#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001757 {
1758 const XML_Feature *features = XML_GetFeatureList();
1759 PyObject *list = PyList_New(0);
1760 if (list == NULL)
1761 /* just ignore it */
1762 PyErr_Clear();
1763 else {
1764 int i = 0;
1765 for (; features[i].feature != XML_FEATURE_END; ++i) {
1766 int ok;
1767 PyObject *item = Py_BuildValue("si", features[i].name,
1768 features[i].value);
1769 if (item == NULL) {
1770 Py_DECREF(list);
1771 list = NULL;
1772 break;
1773 }
1774 ok = PyList_Append(list, item);
1775 Py_DECREF(item);
1776 if (ok < 0) {
1777 PyErr_Clear();
1778 break;
1779 }
1780 }
1781 if (list != NULL)
1782 PyModule_AddObject(m, "features", list);
1783 }
1784 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001785#endif
Fred Drake6f987622000-08-25 18:03:30 +00001786
Georg Brandlb4dac712010-10-15 14:46:48 +00001787 codes_dict = PyDict_New();
1788 rev_codes_dict = PyDict_New();
1789 if (codes_dict == NULL || rev_codes_dict == NULL) {
1790 Py_XDECREF(codes_dict);
1791 Py_XDECREF(rev_codes_dict);
1792 return NULL;
1793 }
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001794
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001795#define MYCONST(name) \
Georg Brandlb4dac712010-10-15 14:46:48 +00001796 if (PyModule_AddStringConstant(errors_module, #name, \
1797 (char *)XML_ErrorString(name)) < 0) \
1798 return NULL; \
1799 tmpnum = PyLong_FromLong(name); \
1800 if (tmpnum == NULL) return NULL; \
1801 res = PyDict_SetItemString(codes_dict, \
1802 XML_ErrorString(name), tmpnum); \
1803 if (res < 0) return NULL; \
1804 tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \
1805 if (tmpstr == NULL) return NULL; \
1806 res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \
1807 Py_DECREF(tmpstr); \
1808 Py_DECREF(tmpnum); \
1809 if (res < 0) return NULL; \
Fred Drake7bd9f412000-07-04 23:51:31 +00001810
Fred Drake0582df92000-07-12 04:49:00 +00001811 MYCONST(XML_ERROR_NO_MEMORY);
1812 MYCONST(XML_ERROR_SYNTAX);
1813 MYCONST(XML_ERROR_NO_ELEMENTS);
1814 MYCONST(XML_ERROR_INVALID_TOKEN);
1815 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1816 MYCONST(XML_ERROR_PARTIAL_CHAR);
1817 MYCONST(XML_ERROR_TAG_MISMATCH);
1818 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1819 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1820 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1821 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1822 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1823 MYCONST(XML_ERROR_ASYNC_ENTITY);
1824 MYCONST(XML_ERROR_BAD_CHAR_REF);
1825 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1826 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1827 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1828 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1829 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001830 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1831 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1832 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001833 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1834 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1835 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1836 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1837 /* Added in Expat 1.95.7. */
1838 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1839 /* Added in Expat 1.95.8. */
1840 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1841 MYCONST(XML_ERROR_INCOMPLETE_PE);
1842 MYCONST(XML_ERROR_XML_DECL);
1843 MYCONST(XML_ERROR_TEXT_DECL);
1844 MYCONST(XML_ERROR_PUBLICID);
1845 MYCONST(XML_ERROR_SUSPENDED);
1846 MYCONST(XML_ERROR_NOT_SUSPENDED);
1847 MYCONST(XML_ERROR_ABORTED);
1848 MYCONST(XML_ERROR_FINISHED);
1849 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001850
Georg Brandlb4dac712010-10-15 14:46:48 +00001851 if (PyModule_AddStringConstant(errors_module, "__doc__",
1852 "Constants used to describe "
1853 "error conditions.") < 0)
1854 return NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001855
Georg Brandlb4dac712010-10-15 14:46:48 +00001856 if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0)
1857 return NULL;
1858 if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0)
1859 return NULL;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001860
Fred Drake93adb692000-09-23 04:55:48 +00001861#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001862
Fred Drake85d835f2001-02-08 15:39:08 +00001863#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001864 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1865 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1866 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001867#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001868
Fred Drake85d835f2001-02-08 15:39:08 +00001869#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1870 PyModule_AddStringConstant(model_module, "__doc__",
1871 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001872
Fred Drake85d835f2001-02-08 15:39:08 +00001873 MYCONST(XML_CTYPE_EMPTY);
1874 MYCONST(XML_CTYPE_ANY);
1875 MYCONST(XML_CTYPE_MIXED);
1876 MYCONST(XML_CTYPE_NAME);
1877 MYCONST(XML_CTYPE_CHOICE);
1878 MYCONST(XML_CTYPE_SEQ);
1879
1880 MYCONST(XML_CQUANT_NONE);
1881 MYCONST(XML_CQUANT_OPT);
1882 MYCONST(XML_CQUANT_REP);
1883 MYCONST(XML_CQUANT_PLUS);
1884#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001885
1886 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001887 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001888 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001889 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1890 capi.MINOR_VERSION = XML_MINOR_VERSION;
1891 capi.MICRO_VERSION = XML_MICRO_VERSION;
1892 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001893 capi.GetErrorCode = XML_GetErrorCode;
1894 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1895 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001896 capi.Parse = XML_Parse;
1897 capi.ParserCreate_MM = XML_ParserCreate_MM;
1898 capi.ParserFree = XML_ParserFree;
1899 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1900 capi.SetCommentHandler = XML_SetCommentHandler;
1901 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1902 capi.SetElementHandler = XML_SetElementHandler;
1903 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1904 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1905 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1906 capi.SetUserData = XML_SetUserData;
Eli Bendersky2b6b73e2012-06-01 11:32:34 +03001907 capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908
Benjamin Petersonb173f782009-05-05 22:31:58 +00001909 /* export using capsule */
1910 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001911 if (capi_object)
1912 PyModule_AddObject(m, "expat_CAPI", capi_object);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001913 return m;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001914}
1915
Fred Drake6f987622000-08-25 18:03:30 +00001916static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001917clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001918{
Fred Drakecde79132001-04-25 16:01:30 +00001919 int i = 0;
1920 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001921
Fred Drake71b63ff2002-06-28 22:29:01 +00001922 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001923 if (initial)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 self->handlers[i] = NULL;
1925 else {
Fred Drakecde79132001-04-25 16:01:30 +00001926 temp = self->handlers[i];
1927 self->handlers[i] = NULL;
1928 Py_XDECREF(temp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001930 }
Fred Drakecde79132001-04-25 16:01:30 +00001931 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001932}
1933
Tim Peters0c322792002-07-17 16:49:03 +00001934static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001935 {"StartElementHandler",
1936 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001937 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001938 {"EndElementHandler",
1939 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001940 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001941 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001942 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1943 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001944 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001945 (xmlhandlersetter)XML_SetCharacterDataHandler,
1946 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001947 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001948 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001949 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001950 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001951 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001952 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001953 {"StartNamespaceDeclHandler",
1954 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001955 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001956 {"EndNamespaceDeclHandler",
1957 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001958 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001959 {"CommentHandler",
1960 (xmlhandlersetter)XML_SetCommentHandler,
1961 (xmlhandler)my_CommentHandler},
1962 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001963 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001964 (xmlhandler)my_StartCdataSectionHandler},
1965 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001966 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001967 (xmlhandler)my_EndCdataSectionHandler},
1968 {"DefaultHandler",
1969 (xmlhandlersetter)XML_SetDefaultHandler,
1970 (xmlhandler)my_DefaultHandler},
1971 {"DefaultHandlerExpand",
1972 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1973 (xmlhandler)my_DefaultHandlerExpandHandler},
1974 {"NotStandaloneHandler",
1975 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1976 (xmlhandler)my_NotStandaloneHandler},
1977 {"ExternalEntityRefHandler",
1978 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001979 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001980 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001981 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001982 (xmlhandler)my_StartDoctypeDeclHandler},
1983 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001984 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001985 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001986 {"EntityDeclHandler",
1987 (xmlhandlersetter)XML_SetEntityDeclHandler,
1988 (xmlhandler)my_EntityDeclHandler},
1989 {"XmlDeclHandler",
1990 (xmlhandlersetter)XML_SetXmlDeclHandler,
1991 (xmlhandler)my_XmlDeclHandler},
1992 {"ElementDeclHandler",
1993 (xmlhandlersetter)XML_SetElementDeclHandler,
1994 (xmlhandler)my_ElementDeclHandler},
1995 {"AttlistDeclHandler",
1996 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1997 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001998#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00001999 {"SkippedEntityHandler",
2000 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2001 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002002#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002003
Fred Drake0582df92000-07-12 04:49:00 +00002004 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002005};