blob: 9267c69e1952091216be0e3552543e0d3ed37b8d [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
Brett Cannond0aeda82014-08-22 14:23:20 -04009/* Do not emit Clinic output to a file as that wreaks havoc with conditionally
10 included methods. */
11/*[clinic input]
12module pyexpat
13[clinic start generated code]*/
14/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b168d503a4490c15]*/
15
Martin v. Löwisc847f402003-01-21 11:09:21 +000016#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
17
Christian Heimesfa535f52013-07-07 17:35:11 +020018static XML_Memory_Handling_Suite ExpatMemoryHandler = {
19 PyObject_Malloc, PyObject_Realloc, PyObject_Free};
20
Fred Drake0582df92000-07-12 04:49:00 +000021enum HandlerTypes {
22 StartElement,
23 EndElement,
24 ProcessingInstruction,
25 CharacterData,
26 UnparsedEntityDecl,
27 NotationDecl,
28 StartNamespaceDecl,
29 EndNamespaceDecl,
30 Comment,
31 StartCdataSection,
32 EndCdataSection,
33 Default,
34 DefaultHandlerExpand,
35 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000036 ExternalEntityRef,
37 StartDoctypeDecl,
38 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000039 EntityDecl,
40 XmlDecl,
41 ElementDecl,
42 AttlistDecl,
Martin v. Löwisc847f402003-01-21 11:09:21 +000043#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +000044 SkippedEntity,
Martin v. Löwisc847f402003-01-21 11:09:21 +000045#endif
Fred Drake85d835f2001-02-08 15:39:08 +000046 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000047};
48
49static PyObject *ErrorObject;
50
51/* ----------------------------------------------------- */
52
53/* Declarations for objects of type xmlparser */
54
55typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000056 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000057
Fred Drake0582df92000-07-12 04:49:00 +000058 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000059 int ordered_attributes; /* Return attributes as a list. */
60 int specified_attributes; /* Report only specified attributes. */
Fred Drakebd6101c2001-02-14 18:29:45 +000061 int in_callback; /* Is a callback active? */
Martin v. Löwis069dde22003-01-21 10:58:18 +000062 int ns_prefixes; /* Namespace-triplets mode? */
Fred Drake2a3d7db2002-06-28 22:56:48 +000063 XML_Char *buffer; /* Buffer used when accumulating characters */
64 /* NULL if not enabled */
65 int buffer_size; /* Size of buffer, in XML_Char units */
66 int buffer_used; /* Buffer units in use */
Fred Drakeb91a36b2002-06-27 19:40:48 +000067 PyObject *intern; /* Dictionary to intern strings */
Fred Drake0582df92000-07-12 04:49:00 +000068 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000069} xmlparseobject;
70
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030071#include "clinic/pyexpat.c.h"
72
Fred Drake2a3d7db2002-06-28 22:56:48 +000073#define CHARACTER_DATA_BUFFER_SIZE 8192
74
Jeremy Hylton938ace62002-07-17 16:30:39 +000075static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076
Fred Drake117ac852002-09-24 16:24:54 +000077typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078typedef void* xmlhandler;
79
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000080struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000081 const char *name;
82 xmlhandlersetter setter;
83 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000084 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000085 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000086};
87
Jeremy Hylton938ace62002-07-17 16:30:39 +000088static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000089
Fred Drakebd6101c2001-02-14 18:29:45 +000090/* Set an integer attribute on the error object; return true on success,
91 * false on an exception.
92 */
93static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020094set_error_attr(PyObject *err, const char *name, int value)
Fred Drakebd6101c2001-02-14 18:29:45 +000095{
Christian Heimes217cfd12007-12-02 14:31:20 +000096 PyObject *v = PyLong_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000097
Neal Norwitz2f5e9902006-03-08 06:36:45 +000098 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
99 Py_XDECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000100 return 0;
101 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000102 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000103 return 1;
104}
105
106/* Build and set an Expat exception, including positioning
107 * information. Always returns NULL.
108 */
Fred Drake85d835f2001-02-08 15:39:08 +0000109static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000110set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000111{
112 PyObject *err;
Victor Stinner499dfcf2011-03-21 13:26:24 +0100113 PyObject *buffer;
Fred Drake85d835f2001-02-08 15:39:08 +0000114 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000115 int lineno = XML_GetErrorLineNumber(parser);
116 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000117
Victor Stinner499dfcf2011-03-21 13:26:24 +0100118 buffer = PyUnicode_FromFormat("%s: line %i, column %i",
119 XML_ErrorString(code), lineno, column);
120 if (buffer == NULL)
121 return NULL;
122 err = PyObject_CallFunction(ErrorObject, "O", buffer);
123 Py_DECREF(buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000124 if ( err != NULL
125 && set_error_attr(err, "code", code)
126 && set_error_attr(err, "offset", column)
127 && set_error_attr(err, "lineno", lineno)) {
128 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000129 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000130 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000131 return NULL;
132}
133
Fred Drake71b63ff2002-06-28 22:29:01 +0000134static int
135have_handler(xmlparseobject *self, int type)
136{
137 PyObject *handler = self->handlers[type];
138 return handler != NULL;
139}
140
141static PyObject *
142get_handler_name(struct HandlerInfo *hinfo)
143{
144 PyObject *name = hinfo->nameobj;
145 if (name == NULL) {
Neal Norwitz392c5be2007-08-25 17:20:32 +0000146 name = PyUnicode_FromString(hinfo->name);
Fred Drake71b63ff2002-06-28 22:29:01 +0000147 hinfo->nameobj = name;
148 }
149 Py_XINCREF(name);
150 return name;
151}
152
Fred Drake85d835f2001-02-08 15:39:08 +0000153
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000154/* Convert a string of XML_Chars into a Unicode string.
155 Returns None if str is a null pointer. */
156
Fred Drake0582df92000-07-12 04:49:00 +0000157static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000158conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000159{
Fred Drake71b63ff2002-06-28 22:29:01 +0000160 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000161 and hence in UTF-8. */
162 /* UTF-8 from Expat, Unicode desired */
163 if (str == NULL) {
164 Py_INCREF(Py_None);
165 return Py_None;
166 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000167 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000168}
169
Fred Drake0582df92000-07-12 04:49:00 +0000170static PyObject *
171conv_string_len_to_unicode(const XML_Char *str, int len)
172{
Fred Drake71b63ff2002-06-28 22:29:01 +0000173 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000174 and hence in UTF-8. */
175 /* UTF-8 from Expat, Unicode desired */
176 if (str == NULL) {
177 Py_INCREF(Py_None);
178 return Py_None;
179 }
Fred Drake6f987622000-08-25 18:03:30 +0000180 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000181}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000182
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000183/* Callback routines */
184
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000185static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000186
Martin v. Löwis069dde22003-01-21 10:58:18 +0000187/* This handler is used when an error has been detected, in the hope
188 that actual parsing can be terminated early. This will only help
189 if an external entity reference is encountered. */
190static int
191error_external_entity_ref_handler(XML_Parser parser,
192 const XML_Char *context,
193 const XML_Char *base,
194 const XML_Char *systemId,
195 const XML_Char *publicId)
196{
197 return 0;
198}
199
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200/* Dummy character data handler used when an error (exception) has
201 been detected, and the actual parsing can be terminated early.
202 This is needed since character data handler can't be safely removed
203 from within the character data handler, but can be replaced. It is
204 used only from the character data handler trampoline, and must be
205 used right after `flag_error()` is called. */
206static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207noop_character_data_handler(void *userData, const XML_Char *data, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208{
209 /* Do nothing. */
210}
211
Fred Drake6f987622000-08-25 18:03:30 +0000212static void
213flag_error(xmlparseobject *self)
214{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000215 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000216 XML_SetExternalEntityRefHandler(self->itself,
217 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000218}
219
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000220static PyObject*
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200221call_with_frame(const char *funcname, int lineno, PyObject* func, PyObject* args,
Fred Drake39689c52004-08-13 03:12:57 +0000222 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000223{
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200224 PyObject *res;
Fred Drakebd6101c2001-02-14 18:29:45 +0000225
Fred Drakebd6101c2001-02-14 18:29:45 +0000226 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000227 if (res == NULL) {
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200228 _PyTraceback_Add(funcname, __FILE__, lineno);
Fred Drake39689c52004-08-13 03:12:57 +0000229 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000230 }
Fred Drakebd6101c2001-02-14 18:29:45 +0000231 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000232}
233
Fred Drakeb91a36b2002-06-27 19:40:48 +0000234static PyObject*
235string_intern(xmlparseobject *self, const char* str)
236{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000237 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000238 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000239 /* result can be NULL if the unicode conversion failed. */
240 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000242 if (!self->intern)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000244 value = PyDict_GetItem(self->intern, result);
245 if (!value) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (PyDict_SetItem(self->intern, result, result) == 0)
Fred Drakeb91a36b2002-06-27 19:40:48 +0000247 return result;
248 else
249 return NULL;
250 }
251 Py_INCREF(value);
252 Py_DECREF(result);
253 return value;
254}
255
Fred Drake2a3d7db2002-06-28 22:56:48 +0000256/* Return 0 on success, -1 on exception.
257 * flag_error() will be called before return if needed.
258 */
259static int
260call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
261{
262 PyObject *args;
263 PyObject *temp;
264
Georg Brandlc01537f2010-10-15 16:26:08 +0000265 if (!have_handler(self, CharacterData))
266 return -1;
267
Fred Drake2a3d7db2002-06-28 22:56:48 +0000268 args = PyTuple_New(1);
269 if (args == NULL)
270 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000271 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000272 if (temp == NULL) {
273 Py_DECREF(args);
274 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275 XML_SetCharacterDataHandler(self->itself,
276 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000277 return -1;
278 }
279 PyTuple_SET_ITEM(args, 0, temp);
280 /* temp is now a borrowed reference; consider it unused. */
281 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200282 temp = call_with_frame("CharacterData", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000283 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000284 /* temp is an owned reference again, or NULL */
285 self->in_callback = 0;
286 Py_DECREF(args);
287 if (temp == NULL) {
288 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289 XML_SetCharacterDataHandler(self->itself,
290 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000291 return -1;
292 }
293 Py_DECREF(temp);
294 return 0;
295}
296
297static int
298flush_character_buffer(xmlparseobject *self)
299{
300 int rc;
301 if (self->buffer == NULL || self->buffer_used == 0)
302 return 0;
303 rc = call_character_handler(self, self->buffer, self->buffer_used);
304 self->buffer_used = 0;
305 return rc;
306}
307
308static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
Fred Drake2a3d7db2002-06-28 22:56:48 +0000310{
311 xmlparseobject *self = (xmlparseobject *) userData;
Victor Stinner9e09c262013-07-18 23:17:01 +0200312
313 if (PyErr_Occurred())
314 return;
315
Fred Drake2a3d7db2002-06-28 22:56:48 +0000316 if (self->buffer == NULL)
317 call_character_handler(self, data, len);
318 else {
319 if ((self->buffer_used + len) > self->buffer_size) {
320 if (flush_character_buffer(self) < 0)
321 return;
322 /* handler might have changed; drop the rest on the floor
323 * if there isn't a handler anymore
324 */
325 if (!have_handler(self, CharacterData))
326 return;
327 }
328 if (len > self->buffer_size) {
329 call_character_handler(self, data, len);
330 self->buffer_used = 0;
331 }
332 else {
333 memcpy(self->buffer + self->buffer_used,
334 data, len * sizeof(XML_Char));
335 self->buffer_used += len;
336 }
337 }
338}
339
Fred Drake85d835f2001-02-08 15:39:08 +0000340static void
341my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000342 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000343{
344 xmlparseobject *self = (xmlparseobject *)userData;
345
Fred Drake71b63ff2002-06-28 22:29:01 +0000346 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000347 PyObject *container, *rv, *args;
348 int i, max;
349
Victor Stinner9e09c262013-07-18 23:17:01 +0200350 if (PyErr_Occurred())
351 return;
352
Fred Drake2a3d7db2002-06-28 22:56:48 +0000353 if (flush_character_buffer(self) < 0)
354 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000355 /* Set max to the number of slots filled in atts[]; max/2 is
356 * the number of attributes we need to process.
357 */
358 if (self->specified_attributes) {
359 max = XML_GetSpecifiedAttributeCount(self->itself);
360 }
361 else {
362 max = 0;
363 while (atts[max] != NULL)
364 max += 2;
365 }
366 /* Build the container. */
367 if (self->ordered_attributes)
368 container = PyList_New(max);
369 else
370 container = PyDict_New();
371 if (container == NULL) {
372 flag_error(self);
373 return;
374 }
375 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000376 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000377 PyObject *v;
378 if (n == NULL) {
379 flag_error(self);
380 Py_DECREF(container);
381 return;
382 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000383 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000384 if (v == NULL) {
385 flag_error(self);
386 Py_DECREF(container);
387 Py_DECREF(n);
388 return;
389 }
390 if (self->ordered_attributes) {
391 PyList_SET_ITEM(container, i, n);
392 PyList_SET_ITEM(container, i+1, v);
393 }
394 else if (PyDict_SetItem(container, n, v)) {
395 flag_error(self);
396 Py_DECREF(n);
397 Py_DECREF(v);
398 return;
399 }
400 else {
401 Py_DECREF(n);
402 Py_DECREF(v);
403 }
404 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000405 args = string_intern(self, name);
406 if (args != NULL)
407 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000408 if (args == NULL) {
409 Py_DECREF(container);
410 return;
411 }
412 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000413 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200414 rv = call_with_frame("StartElement", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000415 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000416 self->in_callback = 0;
417 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000418 if (rv == NULL) {
419 flag_error(self);
420 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000421 }
Fred Drake85d835f2001-02-08 15:39:08 +0000422 Py_DECREF(rv);
423 }
424}
425
426#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
427 RETURN, GETUSERDATA) \
428static RC \
429my_##NAME##Handler PARAMS {\
430 xmlparseobject *self = GETUSERDATA ; \
431 PyObject *args = NULL; \
432 PyObject *rv = NULL; \
433 INIT \
434\
Fred Drake71b63ff2002-06-28 22:29:01 +0000435 if (have_handler(self, NAME)) { \
Victor Stinner9e09c262013-07-18 23:17:01 +0200436 if (PyErr_Occurred()) \
437 return RETURN; \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000438 if (flush_character_buffer(self) < 0) \
439 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000440 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000441 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000442 self->in_callback = 1; \
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200443 rv = call_with_frame(#NAME,__LINE__, \
Fred Drake39689c52004-08-13 03:12:57 +0000444 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000445 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000446 Py_DECREF(args); \
447 if (rv == NULL) { \
448 flag_error(self); \
449 return RETURN; \
450 } \
451 CONVERSION \
452 Py_DECREF(rv); \
453 } \
454 return RETURN; \
455}
456
Fred Drake6f987622000-08-25 18:03:30 +0000457#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
459 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000460
Fred Drake6f987622000-08-25 18:03:30 +0000461#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
463 rc = PyLong_AsLong(rv);, rc, \
464 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000465
Fred Drake71b63ff2002-06-28 22:29:01 +0000466VOID_HANDLER(EndElement,
467 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000468 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000469
Fred Drake6f987622000-08-25 18:03:30 +0000470VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000471 (void *userData,
472 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000473 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000474 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000475
Fred Drake6f987622000-08-25 18:03:30 +0000476VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000477 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000478 const XML_Char *entityName,
479 const XML_Char *base,
480 const XML_Char *systemId,
481 const XML_Char *publicId,
482 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000483 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000484 string_intern(self, entityName), string_intern(self, base),
485 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000486 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000487
Fred Drake85d835f2001-02-08 15:39:08 +0000488VOID_HANDLER(EntityDecl,
489 (void *userData,
490 const XML_Char *entityName,
491 int is_parameter_entity,
492 const XML_Char *value,
493 int value_length,
494 const XML_Char *base,
495 const XML_Char *systemId,
496 const XML_Char *publicId,
497 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000498 ("NiNNNNN",
499 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000500 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000501 string_intern(self, base), string_intern(self, systemId),
502 string_intern(self, publicId),
503 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000504
505VOID_HANDLER(XmlDecl,
506 (void *userData,
507 const XML_Char *version,
508 const XML_Char *encoding,
509 int standalone),
510 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000511 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000512 standalone))
513
514static PyObject *
515conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000516 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000517{
518 PyObject *result = NULL;
519 PyObject *children = PyTuple_New(model->numchildren);
520 int i;
521
522 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000523 assert(model->numchildren < INT_MAX);
524 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000525 PyObject *child = conv_content_model(&model->children[i],
526 conv_string);
527 if (child == NULL) {
528 Py_XDECREF(children);
529 return NULL;
530 }
531 PyTuple_SET_ITEM(children, i, child);
532 }
533 result = Py_BuildValue("(iiO&N)",
534 model->type, model->quant,
535 conv_string,model->name, children);
536 }
537 return result;
538}
539
Fred Drake06dd8cf2003-02-02 03:54:17 +0000540static void
541my_ElementDeclHandler(void *userData,
542 const XML_Char *name,
543 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000544{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000545 xmlparseobject *self = (xmlparseobject *)userData;
546 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000547
Fred Drake06dd8cf2003-02-02 03:54:17 +0000548 if (have_handler(self, ElementDecl)) {
549 PyObject *rv = NULL;
550 PyObject *modelobj, *nameobj;
551
Victor Stinner9e09c262013-07-18 23:17:01 +0200552 if (PyErr_Occurred())
553 return;
554
Fred Drake06dd8cf2003-02-02 03:54:17 +0000555 if (flush_character_buffer(self) < 0)
556 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000557 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000558 if (modelobj == NULL) {
559 flag_error(self);
560 goto finally;
561 }
562 nameobj = string_intern(self, name);
563 if (nameobj == NULL) {
564 Py_DECREF(modelobj);
565 flag_error(self);
566 goto finally;
567 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000568 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000569 if (args == NULL) {
570 Py_DECREF(modelobj);
571 flag_error(self);
572 goto finally;
573 }
574 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200575 rv = call_with_frame("ElementDecl", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000576 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000577 self->in_callback = 0;
578 if (rv == NULL) {
579 flag_error(self);
580 goto finally;
581 }
582 Py_DECREF(rv);
583 }
584 finally:
585 Py_XDECREF(args);
586 XML_FreeContentModel(self->itself, model);
587 return;
588}
Fred Drake85d835f2001-02-08 15:39:08 +0000589
590VOID_HANDLER(AttlistDecl,
591 (void *userData,
592 const XML_Char *elname,
593 const XML_Char *attname,
594 const XML_Char *att_type,
595 const XML_Char *dflt,
596 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000597 ("(NNO&O&i)",
598 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000599 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000600 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000601
Martin v. Löwisc847f402003-01-21 11:09:21 +0000602#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000603VOID_HANDLER(SkippedEntity,
604 (void *userData,
605 const XML_Char *entityName,
606 int is_parameter_entity),
607 ("Ni",
608 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000609#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000610
Fred Drake71b63ff2002-06-28 22:29:01 +0000611VOID_HANDLER(NotationDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 (void *userData,
613 const XML_Char *notationName,
614 const XML_Char *base,
615 const XML_Char *systemId,
616 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000617 ("(NNNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 string_intern(self, notationName), string_intern(self, base),
619 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000620
Fred Drake6f987622000-08-25 18:03:30 +0000621VOID_HANDLER(StartNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 (void *userData,
623 const XML_Char *prefix,
624 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000625 ("(NN)",
626 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000627
Fred Drake6f987622000-08-25 18:03:30 +0000628VOID_HANDLER(EndNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 (void *userData,
630 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000631 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000632
Fred Drake6f987622000-08-25 18:03:30 +0000633VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000634 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000635 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000636
Fred Drake6f987622000-08-25 18:03:30 +0000637VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000638 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000640
Fred Drake6f987622000-08-25 18:03:30 +0000641VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000642 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000644
Fred Drake6f987622000-08-25 18:03:30 +0000645VOID_HANDLER(Default,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 (void *userData, const XML_Char *s, int len),
647 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000648
Fred Drake6f987622000-08-25 18:03:30 +0000649VOID_HANDLER(DefaultHandlerExpand,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 (void *userData, const XML_Char *s, int len),
651 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000652
Fred Drake71b63ff2002-06-28 22:29:01 +0000653INT_HANDLER(NotStandalone,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 (void *userData),
655 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656
Fred Drake6f987622000-08-25 18:03:30 +0000657RC_HANDLER(int, ExternalEntityRef,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 (XML_Parser parser,
659 const XML_Char *context,
660 const XML_Char *base,
661 const XML_Char *systemId,
662 const XML_Char *publicId),
663 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000664 ("(O&NNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 conv_string_to_unicode ,context, string_intern(self, base),
666 string_intern(self, systemId), string_intern(self, publicId)),
667 rc = PyLong_AsLong(rv);, rc,
668 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000669
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000670/* XXX UnknownEncodingHandler */
671
Fred Drake85d835f2001-02-08 15:39:08 +0000672VOID_HANDLER(StartDoctypeDecl,
673 (void *userData, const XML_Char *doctypeName,
674 const XML_Char *sysid, const XML_Char *pubid,
675 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000676 ("(NNNi)", string_intern(self, doctypeName),
677 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000678 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000679
680VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000681
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000682/* ---------------------------------------------------------------- */
Brett Cannond0aeda82014-08-22 14:23:20 -0400683/*[clinic input]
684class pyexpat.xmlparser "xmlparseobject *" "&Xmlparsetype"
685[clinic start generated code]*/
686/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2393162385232e1c]*/
687
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000688
Fred Drake71b63ff2002-06-28 22:29:01 +0000689static PyObject *
690get_parse_result(xmlparseobject *self, int rv)
691{
692 if (PyErr_Occurred()) {
693 return NULL;
694 }
695 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000696 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000697 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000698 if (flush_character_buffer(self) < 0) {
699 return NULL;
700 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000701 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000702}
703
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200704#define MAX_CHUNK_SIZE (1 << 20)
705
Brett Cannond0aeda82014-08-22 14:23:20 -0400706/*[clinic input]
707pyexpat.xmlparser.Parse
708
709 data: object
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300710 isfinal: int(c_default="0") = False
Brett Cannond0aeda82014-08-22 14:23:20 -0400711 /
712
713Parse XML data.
714
715`isfinal' should be true at end of input.
716[clinic start generated code]*/
717
Brett Cannond0aeda82014-08-22 14:23:20 -0400718static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400719pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data,
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300720 int isfinal)
721/*[clinic end generated code: output=f4db843dd1f4ed4b input=199d9e8e92ebbb4b]*/
Brett Cannond0aeda82014-08-22 14:23:20 -0400722{
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200723 const char *s;
724 Py_ssize_t slen;
725 Py_buffer view;
726 int rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000727
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200728 if (PyUnicode_Check(data)) {
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200729 view.buf = NULL;
Serhiy Storchaka36b365c2013-02-04 18:28:01 +0200730 s = PyUnicode_AsUTF8AndSize(data, &slen);
731 if (s == NULL)
732 return NULL;
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200733 /* Explicitly set UTF-8 encoding. Return code ignored. */
734 (void)XML_SetEncoding(self->itself, "utf-8");
735 }
736 else {
737 if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0)
738 return NULL;
739 s = view.buf;
740 slen = view.len;
741 }
742
743 while (slen > MAX_CHUNK_SIZE) {
744 rc = XML_Parse(self->itself, s, MAX_CHUNK_SIZE, 0);
745 if (!rc)
746 goto done;
747 s += MAX_CHUNK_SIZE;
748 slen -= MAX_CHUNK_SIZE;
749 }
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200750 Py_BUILD_ASSERT(MAX_CHUNK_SIZE <= INT_MAX);
751 assert(slen <= INT_MAX);
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300752 rc = XML_Parse(self->itself, s, (int)slen, isfinal);
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200753
754done:
755 if (view.buf != NULL)
756 PyBuffer_Release(&view);
757 return get_parse_result(self, rc);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000758}
759
Fred Drakeca1f4262000-09-21 20:10:23 +0000760/* File reading copied from cPickle */
761
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000762#define BUF_SIZE 2048
763
Fred Drake0582df92000-07-12 04:49:00 +0000764static int
765readinst(char *buf, int buf_size, PyObject *meth)
766{
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000767 PyObject *str;
768 Py_ssize_t len;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200769 const char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000770
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000771 str = PyObject_CallFunction(meth, "n", buf_size);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000772 if (str == NULL)
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000773 goto error;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000774
Christian Heimes72b710a2008-05-26 13:28:38 +0000775 if (PyBytes_Check(str))
776 ptr = PyBytes_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000777 else if (PyByteArray_Check(str))
778 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000779 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000780 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000781 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000782 Py_TYPE(str)->tp_name);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000783 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000784 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000785 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000786 if (len > buf_size) {
787 PyErr_Format(PyExc_ValueError,
788 "read() returned too much data: "
Victor Stinner9d6f9362011-01-04 22:00:04 +0000789 "%i bytes requested, %zd returned",
Fred Drake0582df92000-07-12 04:49:00 +0000790 buf_size, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000791 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000792 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000793 memcpy(buf, ptr, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000794 Py_DECREF(str);
795 /* len <= buf_size <= INT_MAX */
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000796 return (int)len;
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000797
798error:
799 Py_XDECREF(str);
800 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801}
802
Brett Cannond0aeda82014-08-22 14:23:20 -0400803/*[clinic input]
804pyexpat.xmlparser.ParseFile
805
806 file: object
807 /
808
809Parse XML data from file-like object.
810[clinic start generated code]*/
811
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000812static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400813pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300814/*[clinic end generated code: output=2adc6a13100cc42b input=fbb5a12b6038d735]*/
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000815{
Fred Drake0582df92000-07-12 04:49:00 +0000816 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000817 PyObject *readmethod = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200818 _Py_IDENTIFIER(read);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000819
Brett Cannond0aeda82014-08-22 14:23:20 -0400820 readmethod = _PyObject_GetAttrId(file, &PyId_read);
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000821 if (readmethod == NULL) {
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000822 PyErr_SetString(PyExc_TypeError,
823 "argument must have 'read' attribute");
824 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000825 }
826 for (;;) {
827 int bytes_read;
828 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000829 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000830 Py_XDECREF(readmethod);
Ned Deilye7d532f2014-03-27 16:39:58 -0700831 return get_parse_result(self, 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000832 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000833
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000834 bytes_read = readinst(buf, BUF_SIZE, readmethod);
835 if (bytes_read < 0) {
836 Py_DECREF(readmethod);
837 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000838 }
839 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000840 if (PyErr_Occurred()) {
841 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000842 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000843 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000844
Fred Drake0582df92000-07-12 04:49:00 +0000845 if (!rv || bytes_read == 0)
846 break;
847 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000848 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000849 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000850}
851
Brett Cannond0aeda82014-08-22 14:23:20 -0400852/*[clinic input]
853pyexpat.xmlparser.SetBase
854
855 base: str
856 /
857
858Set the base URL for the parser.
859[clinic start generated code]*/
860
Brett Cannond0aeda82014-08-22 14:23:20 -0400861static PyObject *
862pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300863/*[clinic end generated code: output=c212ddceb607b539 input=c684e5de895ee1a8]*/
Brett Cannond0aeda82014-08-22 14:23:20 -0400864{
Fred Drake0582df92000-07-12 04:49:00 +0000865 if (!XML_SetBase(self->itself, base)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000867 }
Brett Cannond0aeda82014-08-22 14:23:20 -0400868 Py_RETURN_NONE;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000869}
870
Brett Cannond0aeda82014-08-22 14:23:20 -0400871/*[clinic input]
872pyexpat.xmlparser.GetBase
873
874Return base URL string for the parser.
875[clinic start generated code]*/
876
Brett Cannond0aeda82014-08-22 14:23:20 -0400877static PyObject *
878pyexpat_xmlparser_GetBase_impl(xmlparseobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300879/*[clinic end generated code: output=2886cb21f9a8739a input=918d71c38009620e]*/
Fred Drake0582df92000-07-12 04:49:00 +0000880{
Fred Drake0582df92000-07-12 04:49:00 +0000881 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000882}
883
Brett Cannond0aeda82014-08-22 14:23:20 -0400884/*[clinic input]
885pyexpat.xmlparser.GetInputContext
886
887Return the untranslated text of the input that caused the current event.
888
889If the event was generated by a large amount of text (such as a start tag
890for an element with many attributes), not all of the text may be available.
891[clinic start generated code]*/
892
Brett Cannond0aeda82014-08-22 14:23:20 -0400893static PyObject *
894pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300895/*[clinic end generated code: output=a88026d683fc22cc input=034df8712db68379]*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000896{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000897 if (self->in_callback) {
898 int offset, size;
899 const char *buffer
900 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +0000901
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000902 if (buffer != NULL)
Christian Heimes72b710a2008-05-26 13:28:38 +0000903 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000904 size - offset);
905 else
906 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000907 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000908 else
909 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +0000910}
Fred Drakebd6101c2001-02-14 18:29:45 +0000911
Brett Cannond0aeda82014-08-22 14:23:20 -0400912/*[clinic input]
913pyexpat.xmlparser.ExternalEntityParserCreate
914
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700915 context: str(accept={str, NoneType})
Brett Cannond0aeda82014-08-22 14:23:20 -0400916 encoding: str = NULL
917 /
918
919Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler.
920[clinic start generated code]*/
921
Brett Cannond0aeda82014-08-22 14:23:20 -0400922static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400923pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
924 const char *context,
925 const char *encoding)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700926/*[clinic end generated code: output=535cda9d7a0fbcd6 input=b906714cc122c322]*/
Brett Cannond0aeda82014-08-22 14:23:20 -0400927{
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000928 xmlparseobject *new_parser;
929 int i;
930
Martin v. Löwis894258c2001-09-23 10:20:10 +0000931 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake85d835f2001-02-08 15:39:08 +0000932 if (new_parser == NULL)
933 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +0000934 new_parser->buffer_size = self->buffer_size;
935 new_parser->buffer_used = 0;
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000936 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000937 new_parser->ordered_attributes = self->ordered_attributes;
938 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +0000939 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +0000940 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000941 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 encoding);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000943 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000944 new_parser->intern = self->intern;
945 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +0000946 PyObject_GC_Track(new_parser);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000947
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000948 if (self->buffer != NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +0200949 new_parser->buffer = PyMem_Malloc(new_parser->buffer_size);
Victor Stinnerb4ba9862010-09-10 22:25:19 +0000950 if (new_parser->buffer == NULL) {
951 Py_DECREF(new_parser);
952 return PyErr_NoMemory();
953 }
954 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000955 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000956 Py_DECREF(new_parser);
957 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000958 }
959
960 XML_SetUserData(new_parser->itself, (void *)new_parser);
961
962 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +0000963 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000964 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000965
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200966 new_parser->handlers = PyMem_New(PyObject *, i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000967 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000968 Py_DECREF(new_parser);
969 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000970 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000971 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000972
973 /* then copy handlers from self */
974 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +0000975 PyObject *handler = self->handlers[i];
976 if (handler != NULL) {
977 Py_INCREF(handler);
978 new_parser->handlers[i] = handler;
979 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +0000980 handler_info[i].handler);
981 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000982 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000983 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000984}
985
Brett Cannond0aeda82014-08-22 14:23:20 -0400986/*[clinic input]
987pyexpat.xmlparser.SetParamEntityParsing
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000988
Brett Cannond0aeda82014-08-22 14:23:20 -0400989 flag: int
990 /
991
992Controls parsing of parameter entities (including the external DTD subset).
993
994Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,
995XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and
996XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag
997was successful.
998[clinic start generated code]*/
999
Brett Cannond0aeda82014-08-22 14:23:20 -04001000static PyObject *
1001pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001002/*[clinic end generated code: output=18668ee8e760d64c input=8aea19b4b15e9af1]*/
Brett Cannond0aeda82014-08-22 14:23:20 -04001003{
1004 flag = XML_SetParamEntityParsing(self->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001005 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001006}
1007
Martin v. Löwisc847f402003-01-21 11:09:21 +00001008
1009#if XML_COMBINED_VERSION >= 19505
Brett Cannond0aeda82014-08-22 14:23:20 -04001010/*[clinic input]
1011pyexpat.xmlparser.UseForeignDTD
1012
1013 flag: bool = True
1014 /
1015
1016Allows the application to provide an artificial external subset if one is not specified as part of the document instance.
1017
1018This readily allows the use of a 'default' document type controlled by the
1019application, while still getting the advantage of providing document type
1020information to the parser. 'flag' defaults to True if not provided.
1021[clinic start generated code]*/
1022
Brett Cannond0aeda82014-08-22 14:23:20 -04001023static PyObject *
1024pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001025/*[clinic end generated code: output=cfaa9aa50bb0f65c input=78144c519d116a6e]*/
Brett Cannond0aeda82014-08-22 14:23:20 -04001026{
Martin v. Löwis069dde22003-01-21 10:58:18 +00001027 enum XML_Error rc;
Brett Cannond0aeda82014-08-22 14:23:20 -04001028
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001029 rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
Martin v. Löwis069dde22003-01-21 10:58:18 +00001030 if (rc != XML_ERROR_NONE) {
1031 return set_error(self, rc);
1032 }
1033 Py_INCREF(Py_None);
1034 return Py_None;
1035}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001036#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001037
Brett Cannond0aeda82014-08-22 14:23:20 -04001038/*[clinic input]
1039pyexpat.xmlparser.__dir__
1040[clinic start generated code]*/
1041
Brett Cannond0aeda82014-08-22 14:23:20 -04001042static PyObject *
1043pyexpat_xmlparser___dir___impl(xmlparseobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001044/*[clinic end generated code: output=bc22451efb9e4d17 input=76aa455f2a661384]*/
Brett Cannond0aeda82014-08-22 14:23:20 -04001045{
1046#define APPEND(list, str) \
1047 do { \
1048 PyObject *o = PyUnicode_FromString(str); \
1049 if (o != NULL) \
1050 PyList_Append(list, o); \
1051 Py_XDECREF(o); \
1052 } while (0)
1053
1054 int i;
1055 PyObject *rc = PyList_New(0);
1056 if (!rc)
1057 return NULL;
1058 for (i = 0; handler_info[i].name != NULL; i++) {
1059 PyObject *o = get_handler_name(&handler_info[i]);
1060 if (o != NULL)
1061 PyList_Append(rc, o);
1062 Py_XDECREF(o);
1063 }
1064 APPEND(rc, "ErrorCode");
1065 APPEND(rc, "ErrorLineNumber");
1066 APPEND(rc, "ErrorColumnNumber");
1067 APPEND(rc, "ErrorByteIndex");
1068 APPEND(rc, "CurrentLineNumber");
1069 APPEND(rc, "CurrentColumnNumber");
1070 APPEND(rc, "CurrentByteIndex");
1071 APPEND(rc, "buffer_size");
1072 APPEND(rc, "buffer_text");
1073 APPEND(rc, "buffer_used");
1074 APPEND(rc, "namespace_prefixes");
1075 APPEND(rc, "ordered_attributes");
1076 APPEND(rc, "specified_attributes");
1077 APPEND(rc, "intern");
1078
1079#undef APPEND
1080
1081 if (PyErr_Occurred()) {
1082 Py_DECREF(rc);
1083 rc = NULL;
1084 }
1085
1086 return rc;
1087}
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001088
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001089static struct PyMethodDef xmlparse_methods[] = {
Brett Cannond0aeda82014-08-22 14:23:20 -04001090 PYEXPAT_XMLPARSER_PARSE_METHODDEF
1091 PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF
1092 PYEXPAT_XMLPARSER_SETBASE_METHODDEF
1093 PYEXPAT_XMLPARSER_GETBASE_METHODDEF
1094 PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF
1095 PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF
1096 PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF
Martin v. Löwisc847f402003-01-21 11:09:21 +00001097#if XML_COMBINED_VERSION >= 19505
Brett Cannond0aeda82014-08-22 14:23:20 -04001098 PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
Martin v. Löwisc847f402003-01-21 11:09:21 +00001099#endif
Brett Cannond0aeda82014-08-22 14:23:20 -04001100 PYEXPAT_XMLPARSER___DIR___METHODDEF
1101 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001102};
1103
1104/* ---------- */
1105
1106
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001107
Fred Drake71b63ff2002-06-28 22:29:01 +00001108/* pyexpat international encoding support.
1109 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001110*/
1111
Fred Drake71b63ff2002-06-28 22:29:01 +00001112static int
1113PyUnknownEncodingHandler(void *encodingHandlerData,
1114 const XML_Char *name,
1115 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001116{
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001117 static unsigned char template_buffer[256] = {0};
1118 PyObject* u;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001119 int i;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001120 void *data;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001121 unsigned int kind;
Fred Drake71b63ff2002-06-28 22:29:01 +00001122
Victor Stinner9e09c262013-07-18 23:17:01 +02001123 if (PyErr_Occurred())
1124 return XML_STATUS_ERROR;
1125
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001126 if (template_buffer[1] == 0) {
1127 for (i = 0; i < 256; i++)
1128 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001129 }
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001130
1131 u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace");
Christian Heimesb5821552013-06-29 20:43:13 +02001132 if (u == NULL || PyUnicode_READY(u)) {
Christian Heimes72172422013-06-29 21:49:27 +02001133 Py_XDECREF(u);
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001134 return XML_STATUS_ERROR;
Christian Heimesb5821552013-06-29 20:43:13 +02001135 }
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001136
1137 if (PyUnicode_GET_LENGTH(u) != 256) {
1138 Py_DECREF(u);
1139 PyErr_SetString(PyExc_ValueError,
1140 "multi-byte encodings are not supported");
1141 return XML_STATUS_ERROR;
1142 }
1143
1144 kind = PyUnicode_KIND(u);
1145 data = PyUnicode_DATA(u);
1146 for (i = 0; i < 256; i++) {
1147 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1148 if (ch != Py_UNICODE_REPLACEMENT_CHARACTER)
1149 info->map[i] = ch;
1150 else
1151 info->map[i] = -1;
1152 }
1153
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001154 info->data = NULL;
1155 info->convert = NULL;
1156 info->release = NULL;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001157 Py_DECREF(u);
1158
1159 return XML_STATUS_OK;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001160}
1161
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001162
1163static PyObject *
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001164newxmlparseobject(const char *encoding, const char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001165{
1166 int i;
1167 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001168
Martin v. Löwis894258c2001-09-23 10:20:10 +00001169 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake0582df92000-07-12 04:49:00 +00001170 if (self == NULL)
1171 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001172
Fred Drake2a3d7db2002-06-28 22:56:48 +00001173 self->buffer = NULL;
1174 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1175 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001176 self->ordered_attributes = 0;
1177 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001178 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001179 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001180 self->handlers = NULL;
Victor Stinner54b2d2e2013-07-15 17:15:57 +02001181 self->intern = intern;
1182 Py_XINCREF(self->intern);
1183 PyObject_GC_Track(self);
1184
Christian Heimesfa535f52013-07-07 17:35:11 +02001185 /* namespace_separator is either NULL or contains one char + \0 */
1186 self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler,
1187 namespace_separator);
Victor Stinner54b2d2e2013-07-15 17:15:57 +02001188 if (self->itself == NULL) {
1189 PyErr_SetString(PyExc_RuntimeError,
1190 "XML_ParserCreate failed");
1191 Py_DECREF(self);
1192 return NULL;
1193 }
Gregory P. Smith25227712012-03-14 18:10:37 -07001194#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT)
1195 /* This feature was added upstream in libexpat 2.1.0. Our expat copy
1196 * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT
1197 * to indicate that we can still use it. */
Gregory P. Smith8e91cf62012-03-14 14:26:55 -07001198 XML_SetHashSalt(self->itself,
Christian Heimes985ecdc2013-11-20 11:46:18 +01001199 (unsigned long)_Py_HashSecret.expat.hashsalt);
Gregory P. Smith25227712012-03-14 18:10:37 -07001200#endif
Fred Drake0582df92000-07-12 04:49:00 +00001201 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001202 XML_SetUnknownEncodingHandler(self->itself,
1203 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001204
Fred Drake2a3d7db2002-06-28 22:56:48 +00001205 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001206 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001207
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001208 self->handlers = PyMem_New(PyObject *, i);
Fred Drake7c75bf22002-07-01 14:02:31 +00001209 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001210 Py_DECREF(self);
1211 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001212 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001213 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001214
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001215 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001216}
1217
1218
1219static void
Fred Drake0582df92000-07-12 04:49:00 +00001220xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001221{
Fred Drake0582df92000-07-12 04:49:00 +00001222 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001223 PyObject_GC_UnTrack(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001224 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001225 XML_ParserFree(self->itself);
1226 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001227
Fred Drake85d835f2001-02-08 15:39:08 +00001228 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001229 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001230 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001231 temp = self->handlers[i];
1232 self->handlers[i] = NULL;
1233 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001234 }
Victor Stinnerb6404912013-07-07 16:21:41 +02001235 PyMem_Free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001236 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001237 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001238 if (self->buffer != NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001239 PyMem_Free(self->buffer);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001240 self->buffer = NULL;
1241 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001242 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001243 PyObject_GC_Del(self);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001244}
1245
Fred Drake0582df92000-07-12 04:49:00 +00001246static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001247handlername2int(PyObject *name)
Fred Drake0582df92000-07-12 04:49:00 +00001248{
1249 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001250 for (i = 0; handler_info[i].name != NULL; i++) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001251 if (PyUnicode_CompareWithASCIIString(
1252 name, handler_info[i].name) == 0) {
Fred Drake0582df92000-07-12 04:49:00 +00001253 return i;
1254 }
1255 }
1256 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001257}
1258
1259static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001260get_pybool(int istrue)
1261{
1262 PyObject *result = istrue ? Py_True : Py_False;
1263 Py_INCREF(result);
1264 return result;
1265}
1266
1267static PyObject *
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001268xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269{
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001270 Py_UCS4 first_char;
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001271 int handlernum = -1;
1272
Alexander Belopolskye239d232010-12-08 23:31:48 +00001273 if (!PyUnicode_Check(nameobj))
1274 goto generic;
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001275 if (PyUnicode_READY(nameobj))
1276 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277
Alexander Belopolskye239d232010-12-08 23:31:48 +00001278 handlernum = handlername2int(nameobj);
Fred Drake71b63ff2002-06-28 22:29:01 +00001279
1280 if (handlernum != -1) {
1281 PyObject *result = self->handlers[handlernum];
1282 if (result == NULL)
1283 result = Py_None;
1284 Py_INCREF(result);
1285 return result;
1286 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001287
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001288 first_char = PyUnicode_READ_CHAR(nameobj, 0);
1289 if (first_char == 'E') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001290 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001291 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001292 XML_GetErrorCode(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001293 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001294 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001295 XML_GetErrorLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001296 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001297 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001298 XML_GetErrorColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001299 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001300 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001301 XML_GetErrorByteIndex(self->itself));
1302 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001303 if (first_char == 'C') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001304 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001305 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001306 XML_GetCurrentLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001307 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001308 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001309 XML_GetCurrentColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001310 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001311 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001312 XML_GetCurrentByteIndex(self->itself));
1313 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001314 if (first_char == 'b') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001315 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001316 return PyLong_FromLong((long) self->buffer_size);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001317 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_text") == 0)
Fred Drake2a3d7db2002-06-28 22:56:48 +00001318 return get_pybool(self->buffer != NULL);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001319 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001320 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001321 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001322 if (PyUnicode_CompareWithASCIIString(nameobj, "namespace_prefixes") == 0)
Martin v. Löwis069dde22003-01-21 10:58:18 +00001323 return get_pybool(self->ns_prefixes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001324 if (PyUnicode_CompareWithASCIIString(nameobj, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001325 return get_pybool(self->ordered_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001326 if (PyUnicode_CompareWithASCIIString(nameobj, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001327 return get_pybool((long) self->specified_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001328 if (PyUnicode_CompareWithASCIIString(nameobj, "intern") == 0) {
Fred Drakeb91a36b2002-06-27 19:40:48 +00001329 if (self->intern == NULL) {
1330 Py_INCREF(Py_None);
1331 return Py_None;
1332 }
1333 else {
1334 Py_INCREF(self->intern);
1335 return self->intern;
1336 }
1337 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001338 generic:
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001339 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001340}
1341
Fred Drake6f987622000-08-25 18:03:30 +00001342static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001343sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001344{
1345 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001346 if (handlernum >= 0) {
1347 xmlhandler c_handler = NULL;
1348 PyObject *temp = self->handlers[handlernum];
1349
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001350 if (v == Py_None) {
1351 /* If this is the character data handler, and a character
1352 data handler is already active, we need to be more
1353 careful. What we can safely do is replace the existing
1354 character data handler callback function with a no-op
1355 function that will refuse to call Python. The downside
1356 is that this doesn't completely remove the character
1357 data handler from the C layer if there's any callback
1358 active, so Expat does a little more work than it
1359 otherwise would, but that's really an odd case. A more
1360 elaborate system of handlers and state could remove the
1361 C handler more effectively. */
1362 if (handlernum == CharacterData && self->in_callback)
1363 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001364 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001365 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001366 else if (v != NULL) {
1367 Py_INCREF(v);
1368 c_handler = handler_info[handlernum].handler;
1369 }
Fred Drake0582df92000-07-12 04:49:00 +00001370 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001371 Py_XDECREF(temp);
1372 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001373 return 1;
1374 }
1375 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001376}
1377
1378static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001379xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001380{
Fred Drake6f987622000-08-25 18:03:30 +00001381 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Serhiy Storchaka931331a2015-09-07 22:37:02 +03001382 if (!PyUnicode_Check(name)) {
1383 PyErr_Format(PyExc_TypeError,
1384 "attribute name must be string, not '%.200s'",
1385 name->ob_type->tp_name);
1386 return -1;
1387 }
Fred Drake85d835f2001-02-08 15:39:08 +00001388 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001389 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1390 return -1;
1391 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001392 if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001393 int b = PyObject_IsTrue(v);
1394 if (b < 0)
1395 return -1;
1396 if (b) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001397 if (self->buffer == NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001398 self->buffer = PyMem_Malloc(self->buffer_size);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001399 if (self->buffer == NULL) {
1400 PyErr_NoMemory();
1401 return -1;
1402 }
1403 self->buffer_used = 0;
1404 }
1405 }
1406 else if (self->buffer != NULL) {
1407 if (flush_character_buffer(self) < 0)
1408 return -1;
Victor Stinnerb6404912013-07-07 16:21:41 +02001409 PyMem_Free(self->buffer);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001410 self->buffer = NULL;
1411 }
1412 return 0;
1413 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001414 if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001415 int b = PyObject_IsTrue(v);
1416 if (b < 0)
1417 return -1;
1418 self->ns_prefixes = b;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001419 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1420 return 0;
1421 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001422 if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001423 int b = PyObject_IsTrue(v);
1424 if (b < 0)
1425 return -1;
1426 self->ordered_attributes = b;
Fred Drake85d835f2001-02-08 15:39:08 +00001427 return 0;
1428 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001429 if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001430 int b = PyObject_IsTrue(v);
1431 if (b < 0)
1432 return -1;
1433 self->specified_attributes = b;
Fred Drake6f987622000-08-25 18:03:30 +00001434 return 0;
1435 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001436
Alexander Belopolskye239d232010-12-08 23:31:48 +00001437 if (PyUnicode_CompareWithASCIIString(name, "buffer_size") == 0) {
Christian Heimes2380ac72008-01-09 00:17:24 +00001438 long new_buffer_size;
1439 if (!PyLong_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1441 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001442 }
1443
Serhiy Storchakade5f9f42015-09-07 22:51:56 +03001444 new_buffer_size = PyLong_AsLong(v);
1445 if (new_buffer_size <= 0) {
1446 if (!PyErr_Occurred())
1447 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1448 return -1;
1449 }
1450
Christian Heimes2380ac72008-01-09 00:17:24 +00001451 /* trivial case -- no change */
1452 if (new_buffer_size == self->buffer_size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 return 0;
Christian Heimes2380ac72008-01-09 00:17:24 +00001454 }
1455
Christian Heimes2380ac72008-01-09 00:17:24 +00001456 /* check maximum */
1457 if (new_buffer_size > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 char errmsg[100];
1459 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1460 PyErr_SetString(PyExc_ValueError, errmsg);
1461 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001462 }
1463
1464 if (self->buffer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /* there is already a buffer */
1466 if (self->buffer_used != 0) {
Christian Heimes09994a92013-07-20 22:41:58 +02001467 if (flush_character_buffer(self) < 0) {
1468 return -1;
1469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 }
1471 /* free existing buffer */
Victor Stinnerb6404912013-07-07 16:21:41 +02001472 PyMem_Free(self->buffer);
Christian Heimes2380ac72008-01-09 00:17:24 +00001473 }
Victor Stinnerb6404912013-07-07 16:21:41 +02001474 self->buffer = PyMem_Malloc(new_buffer_size);
Christian Heimes2380ac72008-01-09 00:17:24 +00001475 if (self->buffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 PyErr_NoMemory();
1477 return -1;
1478 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001479 self->buffer_size = new_buffer_size;
1480 return 0;
1481 }
1482
Alexander Belopolskye239d232010-12-08 23:31:48 +00001483 if (PyUnicode_CompareWithASCIIString(name, "CharacterDataHandler") == 0) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001484 /* If we're changing the character data handler, flush all
1485 * cached data with the old handler. Not sure there's a
1486 * "right" thing to do, though, but this probably won't
1487 * happen.
1488 */
1489 if (flush_character_buffer(self) < 0)
1490 return -1;
1491 }
Fred Drake6f987622000-08-25 18:03:30 +00001492 if (sethandler(self, name, v)) {
1493 return 0;
1494 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001495 PyErr_SetObject(PyExc_AttributeError, name);
Fred Drake6f987622000-08-25 18:03:30 +00001496 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001497}
1498
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001499static int
1500xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1501{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502 int i;
1503 for (i = 0; handler_info[i].name != NULL; i++)
1504 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001505 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001506}
1507
1508static int
1509xmlparse_clear(xmlparseobject *op)
1510{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001511 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001512 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001513 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001514}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001516PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001517
1518static PyTypeObject Xmlparsetype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 PyVarObject_HEAD_INIT(NULL, 0)
1520 "pyexpat.xmlparser", /*tp_name*/
Antoine Pitrou23683ef2011-01-04 00:00:31 +00001521 sizeof(xmlparseobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 0, /*tp_itemsize*/
1523 /* methods */
1524 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1525 (printfunc)0, /*tp_print*/
1526 0, /*tp_getattr*/
Alexander Belopolskye239d232010-12-08 23:31:48 +00001527 0, /*tp_setattr*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 0, /*tp_reserved*/
1529 (reprfunc)0, /*tp_repr*/
1530 0, /*tp_as_number*/
1531 0, /*tp_as_sequence*/
1532 0, /*tp_as_mapping*/
1533 (hashfunc)0, /*tp_hash*/
1534 (ternaryfunc)0, /*tp_call*/
1535 (reprfunc)0, /*tp_str*/
1536 (getattrofunc)xmlparse_getattro, /* tp_getattro */
Alexander Belopolskye239d232010-12-08 23:31:48 +00001537 (setattrofunc)xmlparse_setattro, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 0, /* tp_as_buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 Xmlparsetype__doc__, /* tp_doc - Documentation string */
1541 (traverseproc)xmlparse_traverse, /* tp_traverse */
1542 (inquiry)xmlparse_clear, /* tp_clear */
1543 0, /* tp_richcompare */
1544 0, /* tp_weaklistoffset */
1545 0, /* tp_iter */
1546 0, /* tp_iternext */
1547 xmlparse_methods, /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001548};
1549
1550/* End of code for xmlparser objects */
1551/* -------------------------------------------------------- */
1552
Brett Cannond0aeda82014-08-22 14:23:20 -04001553/*[clinic input]
1554pyexpat.ParserCreate
1555
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001556 encoding: str(accept={str, NoneType}) = NULL
1557 namespace_separator: str(accept={str, NoneType}) = NULL
Brett Cannond0aeda82014-08-22 14:23:20 -04001558 intern: object = NULL
1559
1560Return a new XML parser object.
1561[clinic start generated code]*/
1562
Brett Cannond0aeda82014-08-22 14:23:20 -04001563static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001564pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding,
1565 const char *namespace_separator, PyObject *intern)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001566/*[clinic end generated code: output=81fccd233e1743a8 input=23d29704acad385d]*/
Brett Cannond0aeda82014-08-22 14:23:20 -04001567{
Fred Drakeb91a36b2002-06-27 19:40:48 +00001568 PyObject *result;
1569 int intern_decref = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001570
Fred Drakecde79132001-04-25 16:01:30 +00001571 if (namespace_separator != NULL
1572 && strlen(namespace_separator) > 1) {
1573 PyErr_SetString(PyExc_ValueError,
1574 "namespace_separator must be at most one"
1575 " character, omitted, or None");
1576 return NULL;
1577 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001578 /* Explicitly passing None means no interning is desired.
1579 Not passing anything means that a new dictionary is used. */
1580 if (intern == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 intern = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001582 else if (intern == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 intern = PyDict_New();
1584 if (!intern)
1585 return NULL;
1586 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001587 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001588 else if (!PyDict_Check(intern)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1590 return NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001591 }
1592
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001593 result = newxmlparseobject(encoding, namespace_separator, intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001594 if (intern_decref) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 Py_DECREF(intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001596 }
1597 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001598}
1599
Brett Cannond0aeda82014-08-22 14:23:20 -04001600/*[clinic input]
1601pyexpat.ErrorString
1602
1603 code: long
1604 /
1605
1606Returns string error for given number.
1607[clinic start generated code]*/
1608
Brett Cannond0aeda82014-08-22 14:23:20 -04001609static PyObject *
1610pyexpat_ErrorString_impl(PyModuleDef *module, long code)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001611/*[clinic end generated code: output=d87668108b6868e5 input=cc67de010d9e62b3]*/
Brett Cannond0aeda82014-08-22 14:23:20 -04001612{
Fred Drake0582df92000-07-12 04:49:00 +00001613 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001614}
1615
1616/* List of methods defined in the module */
1617
1618static struct PyMethodDef pyexpat_methods[] = {
Brett Cannond0aeda82014-08-22 14:23:20 -04001619 PYEXPAT_PARSERCREATE_METHODDEF
1620 PYEXPAT_ERRORSTRING_METHODDEF
1621 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001622};
1623
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001624/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626PyDoc_STRVAR(pyexpat_module_documentation,
1627"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001628
Fred Drakecde79132001-04-25 16:01:30 +00001629/* Initialization function for the module */
1630
1631#ifndef MODULE_NAME
1632#define MODULE_NAME "pyexpat"
1633#endif
1634
1635#ifndef MODULE_INITFUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001636#define MODULE_INITFUNC PyInit_pyexpat
Fred Drakecde79132001-04-25 16:01:30 +00001637#endif
1638
Martin v. Löwis1a214512008-06-11 05:26:20 +00001639static struct PyModuleDef pyexpatmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PyModuleDef_HEAD_INIT,
1641 MODULE_NAME,
1642 pyexpat_module_documentation,
1643 -1,
1644 pyexpat_methods,
1645 NULL,
1646 NULL,
1647 NULL,
1648 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001649};
1650
Martin v. Löwis069dde22003-01-21 10:58:18 +00001651PyMODINIT_FUNC
1652MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001653{
1654 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001655 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001656 PyObject *errors_module;
1657 PyObject *modelmod_name;
1658 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001659 PyObject *sys_modules;
Georg Brandlb4dac712010-10-15 14:46:48 +00001660 PyObject *tmpnum, *tmpstr;
1661 PyObject *codes_dict;
1662 PyObject *rev_codes_dict;
1663 int res;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001664 static struct PyExpat_CAPI capi;
Georg Brandlb4dac712010-10-15 14:46:48 +00001665 PyObject *capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001666
Fred Drake6f987622000-08-25 18:03:30 +00001667 if (errmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001668 return NULL;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001669 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001670 if (modelmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001671 return NULL;
Fred Drake6f987622000-08-25 18:03:30 +00001672
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001673 if (PyType_Ready(&Xmlparsetype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001675
Fred Drake0582df92000-07-12 04:49:00 +00001676 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001677 m = PyModule_Create(&pyexpatmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001678 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001680
Fred Drake0582df92000-07-12 04:49:00 +00001681 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001682 if (ErrorObject == NULL) {
1683 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001684 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001685 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001686 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001687 }
1688 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001689 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001690 Py_INCREF(ErrorObject);
1691 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001692 Py_INCREF(&Xmlparsetype);
1693 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001694
Fred Drake738293d2000-12-21 17:25:07 +00001695 PyModule_AddStringConstant(m, "EXPAT_VERSION",
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001696 XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001697 {
1698 XML_Expat_Version info = XML_ExpatVersionInfo();
1699 PyModule_AddObject(m, "version_info",
1700 Py_BuildValue("(iii)", info.major,
1701 info.minor, info.micro));
1702 }
Fred Drake0582df92000-07-12 04:49:00 +00001703 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001704 compiled, this should check and set native_encoding
1705 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001706 */
Fred Drake93adb692000-09-23 04:55:48 +00001707 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001708
Fred Drake85d835f2001-02-08 15:39:08 +00001709 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001710 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001711 errors_module = PyDict_GetItem(d, errmod_name);
1712 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001713 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001714 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001715 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001716 /* gives away the reference to errors_module */
1717 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001718 }
1719 }
Fred Drake6f987622000-08-25 18:03:30 +00001720 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001721 model_module = PyDict_GetItem(d, modelmod_name);
1722 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001723 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001724 if (model_module != NULL) {
1725 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1726 /* gives away the reference to model_module */
1727 PyModule_AddObject(m, "model", model_module);
1728 }
1729 }
1730 Py_DECREF(modelmod_name);
1731 if (errors_module == NULL || model_module == NULL)
1732 /* Don't core dump later! */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001733 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734
Martin v. Löwisc847f402003-01-21 11:09:21 +00001735#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00001736 {
1737 const XML_Feature *features = XML_GetFeatureList();
1738 PyObject *list = PyList_New(0);
1739 if (list == NULL)
1740 /* just ignore it */
1741 PyErr_Clear();
1742 else {
1743 int i = 0;
1744 for (; features[i].feature != XML_FEATURE_END; ++i) {
1745 int ok;
1746 PyObject *item = Py_BuildValue("si", features[i].name,
1747 features[i].value);
1748 if (item == NULL) {
1749 Py_DECREF(list);
1750 list = NULL;
1751 break;
1752 }
1753 ok = PyList_Append(list, item);
1754 Py_DECREF(item);
1755 if (ok < 0) {
1756 PyErr_Clear();
1757 break;
1758 }
1759 }
1760 if (list != NULL)
1761 PyModule_AddObject(m, "features", list);
1762 }
1763 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00001764#endif
Fred Drake6f987622000-08-25 18:03:30 +00001765
Georg Brandlb4dac712010-10-15 14:46:48 +00001766 codes_dict = PyDict_New();
1767 rev_codes_dict = PyDict_New();
1768 if (codes_dict == NULL || rev_codes_dict == NULL) {
1769 Py_XDECREF(codes_dict);
1770 Py_XDECREF(rev_codes_dict);
1771 return NULL;
1772 }
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001773
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001774#define MYCONST(name) \
Georg Brandlb4dac712010-10-15 14:46:48 +00001775 if (PyModule_AddStringConstant(errors_module, #name, \
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001776 XML_ErrorString(name)) < 0) \
Georg Brandlb4dac712010-10-15 14:46:48 +00001777 return NULL; \
1778 tmpnum = PyLong_FromLong(name); \
1779 if (tmpnum == NULL) return NULL; \
1780 res = PyDict_SetItemString(codes_dict, \
1781 XML_ErrorString(name), tmpnum); \
1782 if (res < 0) return NULL; \
1783 tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \
1784 if (tmpstr == NULL) return NULL; \
1785 res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \
1786 Py_DECREF(tmpstr); \
1787 Py_DECREF(tmpnum); \
1788 if (res < 0) return NULL; \
Fred Drake7bd9f412000-07-04 23:51:31 +00001789
Fred Drake0582df92000-07-12 04:49:00 +00001790 MYCONST(XML_ERROR_NO_MEMORY);
1791 MYCONST(XML_ERROR_SYNTAX);
1792 MYCONST(XML_ERROR_NO_ELEMENTS);
1793 MYCONST(XML_ERROR_INVALID_TOKEN);
1794 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1795 MYCONST(XML_ERROR_PARTIAL_CHAR);
1796 MYCONST(XML_ERROR_TAG_MISMATCH);
1797 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1798 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1799 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1800 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1801 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1802 MYCONST(XML_ERROR_ASYNC_ENTITY);
1803 MYCONST(XML_ERROR_BAD_CHAR_REF);
1804 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1805 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1806 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1807 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1808 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001809 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1810 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1811 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00001812 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1813 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1814 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1815 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1816 /* Added in Expat 1.95.7. */
1817 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1818 /* Added in Expat 1.95.8. */
1819 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1820 MYCONST(XML_ERROR_INCOMPLETE_PE);
1821 MYCONST(XML_ERROR_XML_DECL);
1822 MYCONST(XML_ERROR_TEXT_DECL);
1823 MYCONST(XML_ERROR_PUBLICID);
1824 MYCONST(XML_ERROR_SUSPENDED);
1825 MYCONST(XML_ERROR_NOT_SUSPENDED);
1826 MYCONST(XML_ERROR_ABORTED);
1827 MYCONST(XML_ERROR_FINISHED);
1828 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001829
Georg Brandlb4dac712010-10-15 14:46:48 +00001830 if (PyModule_AddStringConstant(errors_module, "__doc__",
1831 "Constants used to describe "
1832 "error conditions.") < 0)
1833 return NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001834
Georg Brandlb4dac712010-10-15 14:46:48 +00001835 if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0)
1836 return NULL;
1837 if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0)
1838 return NULL;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001839
Fred Drake93adb692000-09-23 04:55:48 +00001840#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001841
Fred Drake85d835f2001-02-08 15:39:08 +00001842#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001843 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1844 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1845 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001846#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001847
Fred Drake85d835f2001-02-08 15:39:08 +00001848#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1849 PyModule_AddStringConstant(model_module, "__doc__",
1850 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001851
Fred Drake85d835f2001-02-08 15:39:08 +00001852 MYCONST(XML_CTYPE_EMPTY);
1853 MYCONST(XML_CTYPE_ANY);
1854 MYCONST(XML_CTYPE_MIXED);
1855 MYCONST(XML_CTYPE_NAME);
1856 MYCONST(XML_CTYPE_CHOICE);
1857 MYCONST(XML_CTYPE_SEQ);
1858
1859 MYCONST(XML_CQUANT_NONE);
1860 MYCONST(XML_CQUANT_OPT);
1861 MYCONST(XML_CQUANT_REP);
1862 MYCONST(XML_CQUANT_PLUS);
1863#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00001864
1865 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001866 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001867 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001868 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1869 capi.MINOR_VERSION = XML_MINOR_VERSION;
1870 capi.MICRO_VERSION = XML_MICRO_VERSION;
1871 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00001872 capi.GetErrorCode = XML_GetErrorCode;
1873 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1874 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001875 capi.Parse = XML_Parse;
1876 capi.ParserCreate_MM = XML_ParserCreate_MM;
1877 capi.ParserFree = XML_ParserFree;
1878 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1879 capi.SetCommentHandler = XML_SetCommentHandler;
1880 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1881 capi.SetElementHandler = XML_SetElementHandler;
1882 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1883 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1884 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1885 capi.SetUserData = XML_SetUserData;
Eli Bendersky2b6b73e2012-06-01 11:32:34 +03001886 capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03001887 capi.SetEncoding = XML_SetEncoding;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001888 capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889
Benjamin Petersonb173f782009-05-05 22:31:58 +00001890 /* export using capsule */
1891 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001892 if (capi_object)
1893 PyModule_AddObject(m, "expat_CAPI", capi_object);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001894 return m;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001895}
1896
Fred Drake6f987622000-08-25 18:03:30 +00001897static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001898clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00001899{
Fred Drakecde79132001-04-25 16:01:30 +00001900 int i = 0;
1901 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001902
Fred Drake71b63ff2002-06-28 22:29:01 +00001903 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001904 if (initial)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 self->handlers[i] = NULL;
1906 else {
Fred Drakecde79132001-04-25 16:01:30 +00001907 temp = self->handlers[i];
1908 self->handlers[i] = NULL;
1909 Py_XDECREF(temp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00001911 }
Fred Drakecde79132001-04-25 16:01:30 +00001912 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001913}
1914
Tim Peters0c322792002-07-17 16:49:03 +00001915static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00001916 {"StartElementHandler",
1917 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001918 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001919 {"EndElementHandler",
1920 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001921 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001922 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001923 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1924 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001925 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001926 (xmlhandlersetter)XML_SetCharacterDataHandler,
1927 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001928 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001929 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001930 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001931 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00001932 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001933 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001934 {"StartNamespaceDeclHandler",
1935 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001936 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00001937 {"EndNamespaceDeclHandler",
1938 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001939 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00001940 {"CommentHandler",
1941 (xmlhandlersetter)XML_SetCommentHandler,
1942 (xmlhandler)my_CommentHandler},
1943 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001944 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001945 (xmlhandler)my_StartCdataSectionHandler},
1946 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001947 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00001948 (xmlhandler)my_EndCdataSectionHandler},
1949 {"DefaultHandler",
1950 (xmlhandlersetter)XML_SetDefaultHandler,
1951 (xmlhandler)my_DefaultHandler},
1952 {"DefaultHandlerExpand",
1953 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1954 (xmlhandler)my_DefaultHandlerExpandHandler},
1955 {"NotStandaloneHandler",
1956 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1957 (xmlhandler)my_NotStandaloneHandler},
1958 {"ExternalEntityRefHandler",
1959 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00001960 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001961 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001962 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001963 (xmlhandler)my_StartDoctypeDeclHandler},
1964 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00001965 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001966 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001967 {"EntityDeclHandler",
1968 (xmlhandlersetter)XML_SetEntityDeclHandler,
1969 (xmlhandler)my_EntityDeclHandler},
1970 {"XmlDeclHandler",
1971 (xmlhandlersetter)XML_SetXmlDeclHandler,
1972 (xmlhandler)my_XmlDeclHandler},
1973 {"ElementDeclHandler",
1974 (xmlhandlersetter)XML_SetElementDeclHandler,
1975 (xmlhandler)my_ElementDeclHandler},
1976 {"AttlistDeclHandler",
1977 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1978 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001979#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00001980 {"SkippedEntityHandler",
1981 (xmlhandlersetter)XML_SetSkippedEntityHandler,
1982 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00001983#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001984
Fred Drake0582df92000-07-12 04:49:00 +00001985 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001986};
Brett Cannond0aeda82014-08-22 14:23:20 -04001987
1988/*[clinic input]
1989dump buffer
1990[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001991/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/