blob: ce6535449746caf49a183810e70728d909436d45 [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
Fred Drake2a3d7db2002-06-28 22:56:48 +000071#define CHARACTER_DATA_BUFFER_SIZE 8192
72
Jeremy Hylton938ace62002-07-17 16:30:39 +000073static PyTypeObject Xmlparsetype;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000074
Fred Drake117ac852002-09-24 16:24:54 +000075typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000076typedef void* xmlhandler;
77
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000078struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000079 const char *name;
80 xmlhandlersetter setter;
81 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000082 PyCodeObject *tb_code;
Fred Drake71b63ff2002-06-28 22:29:01 +000083 PyObject *nameobj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000084};
85
Jeremy Hylton938ace62002-07-17 16:30:39 +000086static struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000087
Fred Drakebd6101c2001-02-14 18:29:45 +000088/* Set an integer attribute on the error object; return true on success,
89 * false on an exception.
90 */
91static int
92set_error_attr(PyObject *err, char *name, int value)
93{
Christian Heimes217cfd12007-12-02 14:31:20 +000094 PyObject *v = PyLong_FromLong(value);
Fred Drake85d835f2001-02-08 15:39:08 +000095
Neal Norwitz2f5e9902006-03-08 06:36:45 +000096 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
97 Py_XDECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +000098 return 0;
99 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000100 Py_DECREF(v);
Fred Drakebd6101c2001-02-14 18:29:45 +0000101 return 1;
102}
103
104/* Build and set an Expat exception, including positioning
105 * information. Always returns NULL.
106 */
Fred Drake85d835f2001-02-08 15:39:08 +0000107static PyObject *
Martin v. Löwis069dde22003-01-21 10:58:18 +0000108set_error(xmlparseobject *self, enum XML_Error code)
Fred Drake85d835f2001-02-08 15:39:08 +0000109{
110 PyObject *err;
Victor Stinner499dfcf2011-03-21 13:26:24 +0100111 PyObject *buffer;
Fred Drake85d835f2001-02-08 15:39:08 +0000112 XML_Parser parser = self->itself;
Fred Drakebd6101c2001-02-14 18:29:45 +0000113 int lineno = XML_GetErrorLineNumber(parser);
114 int column = XML_GetErrorColumnNumber(parser);
Fred Drake85d835f2001-02-08 15:39:08 +0000115
Victor Stinner499dfcf2011-03-21 13:26:24 +0100116 buffer = PyUnicode_FromFormat("%s: line %i, column %i",
117 XML_ErrorString(code), lineno, column);
118 if (buffer == NULL)
119 return NULL;
120 err = PyObject_CallFunction(ErrorObject, "O", buffer);
121 Py_DECREF(buffer);
Fred Drakebd6101c2001-02-14 18:29:45 +0000122 if ( err != NULL
123 && set_error_attr(err, "code", code)
124 && set_error_attr(err, "offset", column)
125 && set_error_attr(err, "lineno", lineno)) {
126 PyErr_SetObject(ErrorObject, err);
Fred Drake85d835f2001-02-08 15:39:08 +0000127 }
Neal Norwitz2f5e9902006-03-08 06:36:45 +0000128 Py_XDECREF(err);
Fred Drake85d835f2001-02-08 15:39:08 +0000129 return NULL;
130}
131
Fred Drake71b63ff2002-06-28 22:29:01 +0000132static int
133have_handler(xmlparseobject *self, int type)
134{
135 PyObject *handler = self->handlers[type];
136 return handler != NULL;
137}
138
139static PyObject *
140get_handler_name(struct HandlerInfo *hinfo)
141{
142 PyObject *name = hinfo->nameobj;
143 if (name == NULL) {
Neal Norwitz392c5be2007-08-25 17:20:32 +0000144 name = PyUnicode_FromString(hinfo->name);
Fred Drake71b63ff2002-06-28 22:29:01 +0000145 hinfo->nameobj = name;
146 }
147 Py_XINCREF(name);
148 return name;
149}
150
Fred Drake85d835f2001-02-08 15:39:08 +0000151
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000152/* Convert a string of XML_Chars into a Unicode string.
153 Returns None if str is a null pointer. */
154
Fred Drake0582df92000-07-12 04:49:00 +0000155static PyObject *
Fred Drakeb91a36b2002-06-27 19:40:48 +0000156conv_string_to_unicode(const XML_Char *str)
Fred Drake0582df92000-07-12 04:49:00 +0000157{
Fred Drake71b63ff2002-06-28 22:29:01 +0000158 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000159 and hence in UTF-8. */
160 /* UTF-8 from Expat, Unicode desired */
161 if (str == NULL) {
162 Py_INCREF(Py_None);
163 return Py_None;
164 }
Fred Drake71b63ff2002-06-28 22:29:01 +0000165 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000166}
167
Fred Drake0582df92000-07-12 04:49:00 +0000168static PyObject *
169conv_string_len_to_unicode(const XML_Char *str, int len)
170{
Fred Drake71b63ff2002-06-28 22:29:01 +0000171 /* XXX currently this code assumes that XML_Char is 8-bit,
Fred Drake0582df92000-07-12 04:49:00 +0000172 and hence in UTF-8. */
173 /* UTF-8 from Expat, Unicode desired */
174 if (str == NULL) {
175 Py_INCREF(Py_None);
176 return Py_None;
177 }
Fred Drake6f987622000-08-25 18:03:30 +0000178 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000179}
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000180
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000181/* Callback routines */
182
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000183static void clear_handlers(xmlparseobject *self, int initial);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000184
Martin v. Löwis069dde22003-01-21 10:58:18 +0000185/* This handler is used when an error has been detected, in the hope
186 that actual parsing can be terminated early. This will only help
187 if an external entity reference is encountered. */
188static int
189error_external_entity_ref_handler(XML_Parser parser,
190 const XML_Char *context,
191 const XML_Char *base,
192 const XML_Char *systemId,
193 const XML_Char *publicId)
194{
195 return 0;
196}
197
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000198/* Dummy character data handler used when an error (exception) has
199 been detected, and the actual parsing can be terminated early.
200 This is needed since character data handler can't be safely removed
201 from within the character data handler, but can be replaced. It is
202 used only from the character data handler trampoline, and must be
203 used right after `flag_error()` is called. */
204static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205noop_character_data_handler(void *userData, const XML_Char *data, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000206{
207 /* Do nothing. */
208}
209
Fred Drake6f987622000-08-25 18:03:30 +0000210static void
211flag_error(xmlparseobject *self)
212{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +0000213 clear_handlers(self, 0);
Martin v. Löwis069dde22003-01-21 10:58:18 +0000214 XML_SetExternalEntityRefHandler(self->itself,
215 error_external_entity_ref_handler);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000216}
217
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000218static PyObject*
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200219call_with_frame(char *funcname, int lineno, PyObject* func, PyObject* args,
Fred Drake39689c52004-08-13 03:12:57 +0000220 xmlparseobject *self)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000221{
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200222 PyObject *res;
Fred Drakebd6101c2001-02-14 18:29:45 +0000223
Fred Drakebd6101c2001-02-14 18:29:45 +0000224 res = PyEval_CallObject(func, args);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000225 if (res == NULL) {
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200226 _PyTraceback_Add(funcname, __FILE__, lineno);
Fred Drake39689c52004-08-13 03:12:57 +0000227 XML_StopParser(self->itself, XML_FALSE);
Jeremy Hylton9263f572003-06-27 16:13:17 +0000228 }
Fred Drakebd6101c2001-02-14 18:29:45 +0000229 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000230}
231
Fred Drakeb91a36b2002-06-27 19:40:48 +0000232static PyObject*
233string_intern(xmlparseobject *self, const char* str)
234{
Guido van Rossum4ca94712007-07-23 17:42:32 +0000235 PyObject *result = conv_string_to_unicode(str);
Fred Drakeb91a36b2002-06-27 19:40:48 +0000236 PyObject *value;
Neal Norwitz484d9a42005-09-30 04:46:49 +0000237 /* result can be NULL if the unicode conversion failed. */
238 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000240 if (!self->intern)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return result;
Fred Drakeb91a36b2002-06-27 19:40:48 +0000242 value = PyDict_GetItem(self->intern, result);
243 if (!value) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (PyDict_SetItem(self->intern, result, result) == 0)
Fred Drakeb91a36b2002-06-27 19:40:48 +0000245 return result;
246 else
247 return NULL;
248 }
249 Py_INCREF(value);
250 Py_DECREF(result);
251 return value;
252}
253
Fred Drake2a3d7db2002-06-28 22:56:48 +0000254/* Return 0 on success, -1 on exception.
255 * flag_error() will be called before return if needed.
256 */
257static int
258call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
259{
260 PyObject *args;
261 PyObject *temp;
262
Georg Brandlc01537f2010-10-15 16:26:08 +0000263 if (!have_handler(self, CharacterData))
264 return -1;
265
Fred Drake2a3d7db2002-06-28 22:56:48 +0000266 args = PyTuple_New(1);
267 if (args == NULL)
268 return -1;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000269 temp = (conv_string_len_to_unicode(buffer, len));
Fred Drake2a3d7db2002-06-28 22:56:48 +0000270 if (temp == NULL) {
271 Py_DECREF(args);
272 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 XML_SetCharacterDataHandler(self->itself,
274 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000275 return -1;
276 }
277 PyTuple_SET_ITEM(args, 0, temp);
278 /* temp is now a borrowed reference; consider it unused. */
279 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200280 temp = call_with_frame("CharacterData", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000281 self->handlers[CharacterData], args, self);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000282 /* temp is an owned reference again, or NULL */
283 self->in_callback = 0;
284 Py_DECREF(args);
285 if (temp == NULL) {
286 flag_error(self);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 XML_SetCharacterDataHandler(self->itself,
288 noop_character_data_handler);
Fred Drake2a3d7db2002-06-28 22:56:48 +0000289 return -1;
290 }
291 Py_DECREF(temp);
292 return 0;
293}
294
295static int
296flush_character_buffer(xmlparseobject *self)
297{
298 int rc;
299 if (self->buffer == NULL || self->buffer_used == 0)
300 return 0;
301 rc = call_character_handler(self, self->buffer, self->buffer_used);
302 self->buffer_used = 0;
303 return rc;
304}
305
306static void
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
Fred Drake2a3d7db2002-06-28 22:56:48 +0000308{
309 xmlparseobject *self = (xmlparseobject *) userData;
Victor Stinner9e09c262013-07-18 23:17:01 +0200310
311 if (PyErr_Occurred())
312 return;
313
Fred Drake2a3d7db2002-06-28 22:56:48 +0000314 if (self->buffer == NULL)
315 call_character_handler(self, data, len);
316 else {
317 if ((self->buffer_used + len) > self->buffer_size) {
318 if (flush_character_buffer(self) < 0)
319 return;
320 /* handler might have changed; drop the rest on the floor
321 * if there isn't a handler anymore
322 */
323 if (!have_handler(self, CharacterData))
324 return;
325 }
326 if (len > self->buffer_size) {
327 call_character_handler(self, data, len);
328 self->buffer_used = 0;
329 }
330 else {
331 memcpy(self->buffer + self->buffer_used,
332 data, len * sizeof(XML_Char));
333 self->buffer_used += len;
334 }
335 }
336}
337
Fred Drake85d835f2001-02-08 15:39:08 +0000338static void
339my_StartElementHandler(void *userData,
Fred Drake71b63ff2002-06-28 22:29:01 +0000340 const XML_Char *name, const XML_Char *atts[])
Fred Drake85d835f2001-02-08 15:39:08 +0000341{
342 xmlparseobject *self = (xmlparseobject *)userData;
343
Fred Drake71b63ff2002-06-28 22:29:01 +0000344 if (have_handler(self, StartElement)) {
Fred Drake85d835f2001-02-08 15:39:08 +0000345 PyObject *container, *rv, *args;
346 int i, max;
347
Victor Stinner9e09c262013-07-18 23:17:01 +0200348 if (PyErr_Occurred())
349 return;
350
Fred Drake2a3d7db2002-06-28 22:56:48 +0000351 if (flush_character_buffer(self) < 0)
352 return;
Fred Drake85d835f2001-02-08 15:39:08 +0000353 /* Set max to the number of slots filled in atts[]; max/2 is
354 * the number of attributes we need to process.
355 */
356 if (self->specified_attributes) {
357 max = XML_GetSpecifiedAttributeCount(self->itself);
358 }
359 else {
360 max = 0;
361 while (atts[max] != NULL)
362 max += 2;
363 }
364 /* Build the container. */
365 if (self->ordered_attributes)
366 container = PyList_New(max);
367 else
368 container = PyDict_New();
369 if (container == NULL) {
370 flag_error(self);
371 return;
372 }
373 for (i = 0; i < max; i += 2) {
Fred Drakeb91a36b2002-06-27 19:40:48 +0000374 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
Fred Drake85d835f2001-02-08 15:39:08 +0000375 PyObject *v;
376 if (n == NULL) {
377 flag_error(self);
378 Py_DECREF(container);
379 return;
380 }
Guido van Rossum4ca94712007-07-23 17:42:32 +0000381 v = conv_string_to_unicode((XML_Char *) atts[i+1]);
Fred Drake85d835f2001-02-08 15:39:08 +0000382 if (v == NULL) {
383 flag_error(self);
384 Py_DECREF(container);
385 Py_DECREF(n);
386 return;
387 }
388 if (self->ordered_attributes) {
389 PyList_SET_ITEM(container, i, n);
390 PyList_SET_ITEM(container, i+1, v);
391 }
392 else if (PyDict_SetItem(container, n, v)) {
393 flag_error(self);
394 Py_DECREF(n);
395 Py_DECREF(v);
396 return;
397 }
398 else {
399 Py_DECREF(n);
400 Py_DECREF(v);
401 }
402 }
Neal Norwitz484d9a42005-09-30 04:46:49 +0000403 args = string_intern(self, name);
404 if (args != NULL)
405 args = Py_BuildValue("(NN)", args, container);
Fred Drake85d835f2001-02-08 15:39:08 +0000406 if (args == NULL) {
407 Py_DECREF(container);
408 return;
409 }
410 /* Container is now a borrowed reference; ignore it. */
Fred Drakebd6101c2001-02-14 18:29:45 +0000411 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200412 rv = call_with_frame("StartElement", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000413 self->handlers[StartElement], args, self);
Fred Drakebd6101c2001-02-14 18:29:45 +0000414 self->in_callback = 0;
415 Py_DECREF(args);
Fred Drake85d835f2001-02-08 15:39:08 +0000416 if (rv == NULL) {
417 flag_error(self);
418 return;
Fred Drakebd6101c2001-02-14 18:29:45 +0000419 }
Fred Drake85d835f2001-02-08 15:39:08 +0000420 Py_DECREF(rv);
421 }
422}
423
424#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
425 RETURN, GETUSERDATA) \
426static RC \
427my_##NAME##Handler PARAMS {\
428 xmlparseobject *self = GETUSERDATA ; \
429 PyObject *args = NULL; \
430 PyObject *rv = NULL; \
431 INIT \
432\
Fred Drake71b63ff2002-06-28 22:29:01 +0000433 if (have_handler(self, NAME)) { \
Victor Stinner9e09c262013-07-18 23:17:01 +0200434 if (PyErr_Occurred()) \
435 return RETURN; \
Fred Drake2a3d7db2002-06-28 22:56:48 +0000436 if (flush_character_buffer(self) < 0) \
437 return RETURN; \
Fred Drake85d835f2001-02-08 15:39:08 +0000438 args = Py_BuildValue PARAM_FORMAT ;\
Martin v. Löwis1d7c55f2001-11-10 13:57:55 +0000439 if (!args) { flag_error(self); return RETURN;} \
Fred Drakebd6101c2001-02-14 18:29:45 +0000440 self->in_callback = 1; \
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200441 rv = call_with_frame(#NAME,__LINE__, \
Fred Drake39689c52004-08-13 03:12:57 +0000442 self->handlers[NAME], args, self); \
Fred Drakebd6101c2001-02-14 18:29:45 +0000443 self->in_callback = 0; \
Fred Drake85d835f2001-02-08 15:39:08 +0000444 Py_DECREF(args); \
445 if (rv == NULL) { \
446 flag_error(self); \
447 return RETURN; \
448 } \
449 CONVERSION \
450 Py_DECREF(rv); \
451 } \
452 return RETURN; \
453}
454
Fred Drake6f987622000-08-25 18:03:30 +0000455#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
457 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000458
Fred Drake6f987622000-08-25 18:03:30 +0000459#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
461 rc = PyLong_AsLong(rv);, rc, \
462 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000463
Fred Drake71b63ff2002-06-28 22:29:01 +0000464VOID_HANDLER(EndElement,
465 (void *userData, const XML_Char *name),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000466 ("(N)", string_intern(self, name)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000467
Fred Drake6f987622000-08-25 18:03:30 +0000468VOID_HANDLER(ProcessingInstruction,
Fred Drake71b63ff2002-06-28 22:29:01 +0000469 (void *userData,
470 const XML_Char *target,
Fred Drake85d835f2001-02-08 15:39:08 +0000471 const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000472 ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000473
Fred Drake6f987622000-08-25 18:03:30 +0000474VOID_HANDLER(UnparsedEntityDecl,
Fred Drake71b63ff2002-06-28 22:29:01 +0000475 (void *userData,
Fred Drake85d835f2001-02-08 15:39:08 +0000476 const XML_Char *entityName,
477 const XML_Char *base,
478 const XML_Char *systemId,
479 const XML_Char *publicId,
480 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000481 ("(NNNNN)",
Fred Drake71b63ff2002-06-28 22:29:01 +0000482 string_intern(self, entityName), string_intern(self, base),
483 string_intern(self, systemId), string_intern(self, publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000484 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000485
Fred Drake85d835f2001-02-08 15:39:08 +0000486VOID_HANDLER(EntityDecl,
487 (void *userData,
488 const XML_Char *entityName,
489 int is_parameter_entity,
490 const XML_Char *value,
491 int value_length,
492 const XML_Char *base,
493 const XML_Char *systemId,
494 const XML_Char *publicId,
495 const XML_Char *notationName),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000496 ("NiNNNNN",
497 string_intern(self, entityName), is_parameter_entity,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000498 (conv_string_len_to_unicode(value, value_length)),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000499 string_intern(self, base), string_intern(self, systemId),
500 string_intern(self, publicId),
501 string_intern(self, notationName)))
Fred Drake85d835f2001-02-08 15:39:08 +0000502
503VOID_HANDLER(XmlDecl,
504 (void *userData,
505 const XML_Char *version,
506 const XML_Char *encoding,
507 int standalone),
508 ("(O&O&i)",
Guido van Rossum4ca94712007-07-23 17:42:32 +0000509 conv_string_to_unicode ,version, conv_string_to_unicode ,encoding,
Fred Drake85d835f2001-02-08 15:39:08 +0000510 standalone))
511
512static PyObject *
513conv_content_model(XML_Content * const model,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000514 PyObject *(*conv_string)(const XML_Char *))
Fred Drake85d835f2001-02-08 15:39:08 +0000515{
516 PyObject *result = NULL;
517 PyObject *children = PyTuple_New(model->numchildren);
518 int i;
519
520 if (children != NULL) {
Tim Peters9544fc52001-07-28 09:36:36 +0000521 assert(model->numchildren < INT_MAX);
522 for (i = 0; i < (int)model->numchildren; ++i) {
Fred Drake85d835f2001-02-08 15:39:08 +0000523 PyObject *child = conv_content_model(&model->children[i],
524 conv_string);
525 if (child == NULL) {
526 Py_XDECREF(children);
527 return NULL;
528 }
529 PyTuple_SET_ITEM(children, i, child);
530 }
531 result = Py_BuildValue("(iiO&N)",
532 model->type, model->quant,
533 conv_string,model->name, children);
534 }
535 return result;
536}
537
Fred Drake06dd8cf2003-02-02 03:54:17 +0000538static void
539my_ElementDeclHandler(void *userData,
540 const XML_Char *name,
541 XML_Content *model)
Fred Drake85d835f2001-02-08 15:39:08 +0000542{
Fred Drake06dd8cf2003-02-02 03:54:17 +0000543 xmlparseobject *self = (xmlparseobject *)userData;
544 PyObject *args = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +0000545
Fred Drake06dd8cf2003-02-02 03:54:17 +0000546 if (have_handler(self, ElementDecl)) {
547 PyObject *rv = NULL;
548 PyObject *modelobj, *nameobj;
549
Victor Stinner9e09c262013-07-18 23:17:01 +0200550 if (PyErr_Occurred())
551 return;
552
Fred Drake06dd8cf2003-02-02 03:54:17 +0000553 if (flush_character_buffer(self) < 0)
554 goto finally;
Guido van Rossum4ca94712007-07-23 17:42:32 +0000555 modelobj = conv_content_model(model, (conv_string_to_unicode));
Fred Drake06dd8cf2003-02-02 03:54:17 +0000556 if (modelobj == NULL) {
557 flag_error(self);
558 goto finally;
559 }
560 nameobj = string_intern(self, name);
561 if (nameobj == NULL) {
562 Py_DECREF(modelobj);
563 flag_error(self);
564 goto finally;
565 }
Michael W. Hudson0bb84542004-08-03 11:31:31 +0000566 args = Py_BuildValue("NN", nameobj, modelobj);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000567 if (args == NULL) {
568 Py_DECREF(modelobj);
569 flag_error(self);
570 goto finally;
571 }
572 self->in_callback = 1;
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200573 rv = call_with_frame("ElementDecl", __LINE__,
Fred Drake39689c52004-08-13 03:12:57 +0000574 self->handlers[ElementDecl], args, self);
Fred Drake06dd8cf2003-02-02 03:54:17 +0000575 self->in_callback = 0;
576 if (rv == NULL) {
577 flag_error(self);
578 goto finally;
579 }
580 Py_DECREF(rv);
581 }
582 finally:
583 Py_XDECREF(args);
584 XML_FreeContentModel(self->itself, model);
585 return;
586}
Fred Drake85d835f2001-02-08 15:39:08 +0000587
588VOID_HANDLER(AttlistDecl,
589 (void *userData,
590 const XML_Char *elname,
591 const XML_Char *attname,
592 const XML_Char *att_type,
593 const XML_Char *dflt,
594 int isrequired),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000595 ("(NNO&O&i)",
596 string_intern(self, elname), string_intern(self, attname),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000597 conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt,
Fred Drake85d835f2001-02-08 15:39:08 +0000598 isrequired))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000599
Martin v. Löwisc847f402003-01-21 11:09:21 +0000600#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +0000601VOID_HANDLER(SkippedEntity,
602 (void *userData,
603 const XML_Char *entityName,
604 int is_parameter_entity),
605 ("Ni",
606 string_intern(self, entityName), is_parameter_entity))
Martin v. Löwisc847f402003-01-21 11:09:21 +0000607#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +0000608
Fred Drake71b63ff2002-06-28 22:29:01 +0000609VOID_HANDLER(NotationDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 (void *userData,
611 const XML_Char *notationName,
612 const XML_Char *base,
613 const XML_Char *systemId,
614 const XML_Char *publicId),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000615 ("(NNNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 string_intern(self, notationName), string_intern(self, base),
617 string_intern(self, systemId), string_intern(self, publicId)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000618
Fred Drake6f987622000-08-25 18:03:30 +0000619VOID_HANDLER(StartNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 (void *userData,
621 const XML_Char *prefix,
622 const XML_Char *uri),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000623 ("(NN)",
624 string_intern(self, prefix), string_intern(self, uri)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000625
Fred Drake6f987622000-08-25 18:03:30 +0000626VOID_HANDLER(EndNamespaceDecl,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 (void *userData,
628 const XML_Char *prefix),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000629 ("(N)", string_intern(self, prefix)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000630
Fred Drake6f987622000-08-25 18:03:30 +0000631VOID_HANDLER(Comment,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000632 (void *userData, const XML_Char *data),
Guido van Rossum4ca94712007-07-23 17:42:32 +0000633 ("(O&)", conv_string_to_unicode ,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000634
Fred Drake6f987622000-08-25 18:03:30 +0000635VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000636 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 ("()"))
Fred Drake71b63ff2002-06-28 22:29:01 +0000638
Fred Drake6f987622000-08-25 18:03:30 +0000639VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000640 (void *userData),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000642
Fred Drake6f987622000-08-25 18:03:30 +0000643VOID_HANDLER(Default,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 (void *userData, const XML_Char *s, int len),
645 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000646
Fred Drake6f987622000-08-25 18:03:30 +0000647VOID_HANDLER(DefaultHandlerExpand,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 (void *userData, const XML_Char *s, int len),
649 ("(N)", (conv_string_len_to_unicode(s,len))))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000650
Fred Drake71b63ff2002-06-28 22:29:01 +0000651INT_HANDLER(NotStandalone,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 (void *userData),
653 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000654
Fred Drake6f987622000-08-25 18:03:30 +0000655RC_HANDLER(int, ExternalEntityRef,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 (XML_Parser parser,
657 const XML_Char *context,
658 const XML_Char *base,
659 const XML_Char *systemId,
660 const XML_Char *publicId),
661 int rc=0;,
Fred Drakeb91a36b2002-06-27 19:40:48 +0000662 ("(O&NNN)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 conv_string_to_unicode ,context, string_intern(self, base),
664 string_intern(self, systemId), string_intern(self, publicId)),
665 rc = PyLong_AsLong(rv);, rc,
666 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000667
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000668/* XXX UnknownEncodingHandler */
669
Fred Drake85d835f2001-02-08 15:39:08 +0000670VOID_HANDLER(StartDoctypeDecl,
671 (void *userData, const XML_Char *doctypeName,
672 const XML_Char *sysid, const XML_Char *pubid,
673 int has_internal_subset),
Fred Drakeb91a36b2002-06-27 19:40:48 +0000674 ("(NNNi)", string_intern(self, doctypeName),
675 string_intern(self, sysid), string_intern(self, pubid),
Fred Drake85d835f2001-02-08 15:39:08 +0000676 has_internal_subset))
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000677
678VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000679
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000680/* ---------------------------------------------------------------- */
Brett Cannond0aeda82014-08-22 14:23:20 -0400681/*[clinic input]
682class pyexpat.xmlparser "xmlparseobject *" "&Xmlparsetype"
683[clinic start generated code]*/
684/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2393162385232e1c]*/
685
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000686
Fred Drake71b63ff2002-06-28 22:29:01 +0000687static PyObject *
688get_parse_result(xmlparseobject *self, int rv)
689{
690 if (PyErr_Occurred()) {
691 return NULL;
692 }
693 if (rv == 0) {
Martin v. Löwis069dde22003-01-21 10:58:18 +0000694 return set_error(self, XML_GetErrorCode(self->itself));
Fred Drake71b63ff2002-06-28 22:29:01 +0000695 }
Fred Drake2a3d7db2002-06-28 22:56:48 +0000696 if (flush_character_buffer(self) < 0) {
697 return NULL;
698 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000699 return PyLong_FromLong(rv);
Fred Drake71b63ff2002-06-28 22:29:01 +0000700}
701
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200702#define MAX_CHUNK_SIZE (1 << 20)
703
Brett Cannond0aeda82014-08-22 14:23:20 -0400704/*[clinic input]
705pyexpat.xmlparser.Parse
706
707 data: object
708 isFinal: int = 0
709 /
710
711Parse XML data.
712
713`isfinal' should be true at end of input.
714[clinic start generated code]*/
715
716PyDoc_STRVAR(pyexpat_xmlparser_Parse__doc__,
717"Parse($self, data, isFinal=0, /)\n"
718"--\n"
719"\n"
720"Parse XML data.\n"
721"\n"
722"`isfinal\' should be true at end of input.");
723
724#define PYEXPAT_XMLPARSER_PARSE_METHODDEF \
725 {"Parse", (PyCFunction)pyexpat_xmlparser_Parse, METH_VARARGS, pyexpat_xmlparser_Parse__doc__},
726
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000727static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400728pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, int isFinal);
729
730static PyObject *
731pyexpat_xmlparser_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000732{
Brett Cannond0aeda82014-08-22 14:23:20 -0400733 PyObject *return_value = NULL;
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200734 PyObject *data;
Fred Drake0582df92000-07-12 04:49:00 +0000735 int isFinal = 0;
Brett Cannond0aeda82014-08-22 14:23:20 -0400736
737 if (!PyArg_ParseTuple(args,
738 "O|i:Parse",
739 &data, &isFinal))
740 goto exit;
741 return_value = pyexpat_xmlparser_Parse_impl(self, data, isFinal);
742
743exit:
744 return return_value;
745}
746
747static PyObject *
748pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, int isFinal)
749/*[clinic end generated code: output=65b1652b01f20856 input=e37b81b8948ca7e0]*/
750{
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200751 const char *s;
752 Py_ssize_t slen;
753 Py_buffer view;
754 int rc;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000755
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200756 if (PyUnicode_Check(data)) {
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200757 view.buf = NULL;
Serhiy Storchaka36b365c2013-02-04 18:28:01 +0200758 s = PyUnicode_AsUTF8AndSize(data, &slen);
759 if (s == NULL)
760 return NULL;
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200761 /* Explicitly set UTF-8 encoding. Return code ignored. */
762 (void)XML_SetEncoding(self->itself, "utf-8");
763 }
764 else {
765 if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0)
766 return NULL;
767 s = view.buf;
768 slen = view.len;
769 }
770
771 while (slen > MAX_CHUNK_SIZE) {
772 rc = XML_Parse(self->itself, s, MAX_CHUNK_SIZE, 0);
773 if (!rc)
774 goto done;
775 s += MAX_CHUNK_SIZE;
776 slen -= MAX_CHUNK_SIZE;
777 }
Christian Heimesba723202013-11-22 00:46:18 +0100778 assert(MAX_CHUNK_SIZE < INT_MAX && slen < INT_MAX);
779 rc = XML_Parse(self->itself, s, (int)slen, isFinal);
Serhiy Storchaka43536e92013-02-04 18:26:15 +0200780
781done:
782 if (view.buf != NULL)
783 PyBuffer_Release(&view);
784 return get_parse_result(self, rc);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000785}
786
Fred Drakeca1f4262000-09-21 20:10:23 +0000787/* File reading copied from cPickle */
788
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000789#define BUF_SIZE 2048
790
Fred Drake0582df92000-07-12 04:49:00 +0000791static int
792readinst(char *buf, int buf_size, PyObject *meth)
793{
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000794 PyObject *str;
795 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000796 char *ptr;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000797
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000798 str = PyObject_CallFunction(meth, "n", buf_size);
Martin v. Löwis9171f022004-10-13 19:50:11 +0000799 if (str == NULL)
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000800 goto error;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000801
Christian Heimes72b710a2008-05-26 13:28:38 +0000802 if (PyBytes_Check(str))
803 ptr = PyBytes_AS_STRING(str);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000804 else if (PyByteArray_Check(str))
805 ptr = PyByteArray_AS_STRING(str);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000806 else {
Fred Drake71b63ff2002-06-28 22:29:01 +0000807 PyErr_Format(PyExc_TypeError,
Guido van Rossum4ca94712007-07-23 17:42:32 +0000808 "read() did not return a bytes object (type=%.400s)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000809 Py_TYPE(str)->tp_name);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000810 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000811 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000812 len = Py_SIZE(str);
Fred Drake0582df92000-07-12 04:49:00 +0000813 if (len > buf_size) {
814 PyErr_Format(PyExc_ValueError,
815 "read() returned too much data: "
Victor Stinner9d6f9362011-01-04 22:00:04 +0000816 "%i bytes requested, %zd returned",
Fred Drake0582df92000-07-12 04:49:00 +0000817 buf_size, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000818 goto error;
Fred Drake0582df92000-07-12 04:49:00 +0000819 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000820 memcpy(buf, ptr, len);
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000821 Py_DECREF(str);
822 /* len <= buf_size <= INT_MAX */
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000823 return (int)len;
Victor Stinner95f1dfc2011-01-10 23:00:36 +0000824
825error:
826 Py_XDECREF(str);
827 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000828}
829
Brett Cannond0aeda82014-08-22 14:23:20 -0400830/*[clinic input]
831pyexpat.xmlparser.ParseFile
832
833 file: object
834 /
835
836Parse XML data from file-like object.
837[clinic start generated code]*/
838
839PyDoc_STRVAR(pyexpat_xmlparser_ParseFile__doc__,
840"ParseFile($self, file, /)\n"
841"--\n"
842"\n"
843"Parse XML data from file-like object.");
844
845#define PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF \
846 {"ParseFile", (PyCFunction)pyexpat_xmlparser_ParseFile, METH_O, pyexpat_xmlparser_ParseFile__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000847
848static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400849pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file)
850/*[clinic end generated code: output=2e13803c3d8c22b2 input=fbb5a12b6038d735]*/
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000851{
Fred Drake0582df92000-07-12 04:49:00 +0000852 int rv = 1;
Fred Drake0582df92000-07-12 04:49:00 +0000853 PyObject *readmethod = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200854 _Py_IDENTIFIER(read);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000855
Brett Cannond0aeda82014-08-22 14:23:20 -0400856 readmethod = _PyObject_GetAttrId(file, &PyId_read);
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000857 if (readmethod == NULL) {
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000858 PyErr_SetString(PyExc_TypeError,
859 "argument must have 'read' attribute");
860 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000861 }
862 for (;;) {
863 int bytes_read;
864 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
Fred Drake7b6caff2003-07-21 17:05:56 +0000865 if (buf == NULL) {
Fred Drakef239c6d2003-07-21 17:22:43 +0000866 Py_XDECREF(readmethod);
Ned Deilye7d532f2014-03-27 16:39:58 -0700867 return get_parse_result(self, 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000868 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000869
Benjamin Peterson4e7f2852010-08-08 16:54:58 +0000870 bytes_read = readinst(buf, BUF_SIZE, readmethod);
871 if (bytes_read < 0) {
872 Py_DECREF(readmethod);
873 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000874 }
875 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
Fred Drake7b6caff2003-07-21 17:05:56 +0000876 if (PyErr_Occurred()) {
877 Py_XDECREF(readmethod);
Fred Drake0582df92000-07-12 04:49:00 +0000878 return NULL;
Fred Drake7b6caff2003-07-21 17:05:56 +0000879 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000880
Fred Drake0582df92000-07-12 04:49:00 +0000881 if (!rv || bytes_read == 0)
882 break;
883 }
Fred Drake7b6caff2003-07-21 17:05:56 +0000884 Py_XDECREF(readmethod);
Fred Drake71b63ff2002-06-28 22:29:01 +0000885 return get_parse_result(self, rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000886}
887
Brett Cannond0aeda82014-08-22 14:23:20 -0400888/*[clinic input]
889pyexpat.xmlparser.SetBase
890
891 base: str
892 /
893
894Set the base URL for the parser.
895[clinic start generated code]*/
896
897PyDoc_STRVAR(pyexpat_xmlparser_SetBase__doc__,
898"SetBase($self, base, /)\n"
899"--\n"
900"\n"
901"Set the base URL for the parser.");
902
903#define PYEXPAT_XMLPARSER_SETBASE_METHODDEF \
904 {"SetBase", (PyCFunction)pyexpat_xmlparser_SetBase, METH_VARARGS, pyexpat_xmlparser_SetBase__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000905
906static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400907pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000908
Brett Cannond0aeda82014-08-22 14:23:20 -0400909static PyObject *
910pyexpat_xmlparser_SetBase(xmlparseobject *self, PyObject *args)
911{
912 PyObject *return_value = NULL;
913 const char *base;
914
915 if (!PyArg_ParseTuple(args,
916 "s:SetBase",
917 &base))
918 goto exit;
919 return_value = pyexpat_xmlparser_SetBase_impl(self, base);
920
921exit:
922 return return_value;
923}
924
925static PyObject *
926pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base)
927/*[clinic end generated code: output=5bdb49f6689a5f93 input=c684e5de895ee1a8]*/
928{
Fred Drake0582df92000-07-12 04:49:00 +0000929 if (!XML_SetBase(self->itself, base)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000931 }
Brett Cannond0aeda82014-08-22 14:23:20 -0400932 Py_RETURN_NONE;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000933}
934
Brett Cannond0aeda82014-08-22 14:23:20 -0400935/*[clinic input]
936pyexpat.xmlparser.GetBase
937
938Return base URL string for the parser.
939[clinic start generated code]*/
940
941PyDoc_STRVAR(pyexpat_xmlparser_GetBase__doc__,
942"GetBase($self, /)\n"
943"--\n"
944"\n"
945"Return base URL string for the parser.");
946
947#define PYEXPAT_XMLPARSER_GETBASE_METHODDEF \
948 {"GetBase", (PyCFunction)pyexpat_xmlparser_GetBase, METH_NOARGS, pyexpat_xmlparser_GetBase__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000949
950static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400951pyexpat_xmlparser_GetBase_impl(xmlparseobject *self);
952
953static PyObject *
954pyexpat_xmlparser_GetBase(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
955{
956 return pyexpat_xmlparser_GetBase_impl(self);
957}
958
959static PyObject *
960pyexpat_xmlparser_GetBase_impl(xmlparseobject *self)
961/*[clinic end generated code: output=ef6046ee28f2b8ee input=918d71c38009620e]*/
Fred Drake0582df92000-07-12 04:49:00 +0000962{
Fred Drake0582df92000-07-12 04:49:00 +0000963 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000964}
965
Brett Cannond0aeda82014-08-22 14:23:20 -0400966/*[clinic input]
967pyexpat.xmlparser.GetInputContext
968
969Return the untranslated text of the input that caused the current event.
970
971If the event was generated by a large amount of text (such as a start tag
972for an element with many attributes), not all of the text may be available.
973[clinic start generated code]*/
974
975PyDoc_STRVAR(pyexpat_xmlparser_GetInputContext__doc__,
976"GetInputContext($self, /)\n"
977"--\n"
978"\n"
979"Return the untranslated text of the input that caused the current event.\n"
980"\n"
981"If the event was generated by a large amount of text (such as a start tag\n"
982"for an element with many attributes), not all of the text may be available.");
983
984#define PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF \
985 {"GetInputContext", (PyCFunction)pyexpat_xmlparser_GetInputContext, METH_NOARGS, pyexpat_xmlparser_GetInputContext__doc__},
Fred Drakebd6101c2001-02-14 18:29:45 +0000986
987static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -0400988pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self);
989
990static PyObject *
991pyexpat_xmlparser_GetInputContext(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
992{
993 return pyexpat_xmlparser_GetInputContext_impl(self);
994}
995
996static PyObject *
997pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self)
998/*[clinic end generated code: output=62ff03390f074cd2 input=034df8712db68379]*/
Fred Drakebd6101c2001-02-14 18:29:45 +0000999{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001000 if (self->in_callback) {
1001 int offset, size;
1002 const char *buffer
1003 = XML_GetInputContext(self->itself, &offset, &size);
Fred Drakebd6101c2001-02-14 18:29:45 +00001004
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001005 if (buffer != NULL)
Christian Heimes72b710a2008-05-26 13:28:38 +00001006 return PyBytes_FromStringAndSize(buffer + offset,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001007 size - offset);
1008 else
1009 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +00001010 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001011 else
1012 Py_RETURN_NONE;
Fred Drakebd6101c2001-02-14 18:29:45 +00001013}
Fred Drakebd6101c2001-02-14 18:29:45 +00001014
Brett Cannond0aeda82014-08-22 14:23:20 -04001015/*[clinic input]
1016pyexpat.xmlparser.ExternalEntityParserCreate
1017
1018 context: str(nullable=True)
1019 encoding: str = NULL
1020 /
1021
1022Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler.
1023[clinic start generated code]*/
1024
1025PyDoc_STRVAR(pyexpat_xmlparser_ExternalEntityParserCreate__doc__,
1026"ExternalEntityParserCreate($self, context, encoding=None, /)\n"
1027"--\n"
1028"\n"
1029"Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler.");
1030
1031#define PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF \
1032 {"ExternalEntityParserCreate", (PyCFunction)pyexpat_xmlparser_ExternalEntityParserCreate, METH_VARARGS, pyexpat_xmlparser_ExternalEntityParserCreate__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001033
1034static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -04001035pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, const char *context, const char *encoding);
1036
1037static PyObject *
1038pyexpat_xmlparser_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001039{
Brett Cannond0aeda82014-08-22 14:23:20 -04001040 PyObject *return_value = NULL;
1041 const char *context;
1042 const char *encoding = NULL;
1043
1044 if (!PyArg_ParseTuple(args,
1045 "z|s:ExternalEntityParserCreate",
1046 &context, &encoding))
1047 goto exit;
1048 return_value = pyexpat_xmlparser_ExternalEntityParserCreate_impl(self, context, encoding);
1049
1050exit:
1051 return return_value;
1052}
1053
1054static PyObject *
1055pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, const char *context, const char *encoding)
1056/*[clinic end generated code: output=4948c35f3dd01133 input=283206575d960272]*/
1057{
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001058 xmlparseobject *new_parser;
1059 int i;
1060
Martin v. Löwis894258c2001-09-23 10:20:10 +00001061 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake85d835f2001-02-08 15:39:08 +00001062 if (new_parser == NULL)
1063 return NULL;
Fred Drake2a3d7db2002-06-28 22:56:48 +00001064 new_parser->buffer_size = self->buffer_size;
1065 new_parser->buffer_used = 0;
Victor Stinnerb4ba9862010-09-10 22:25:19 +00001066 new_parser->buffer = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001067 new_parser->ordered_attributes = self->ordered_attributes;
1068 new_parser->specified_attributes = self->specified_attributes;
Fred Drakebd6101c2001-02-14 18:29:45 +00001069 new_parser->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001070 new_parser->ns_prefixes = self->ns_prefixes;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001071 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 encoding);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001073 new_parser->handlers = 0;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001074 new_parser->intern = self->intern;
1075 Py_XINCREF(new_parser->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001076 PyObject_GC_Track(new_parser);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001077
Victor Stinnerb4ba9862010-09-10 22:25:19 +00001078 if (self->buffer != NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001079 new_parser->buffer = PyMem_Malloc(new_parser->buffer_size);
Victor Stinnerb4ba9862010-09-10 22:25:19 +00001080 if (new_parser->buffer == NULL) {
1081 Py_DECREF(new_parser);
1082 return PyErr_NoMemory();
1083 }
1084 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001085 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +00001086 Py_DECREF(new_parser);
1087 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001088 }
1089
1090 XML_SetUserData(new_parser->itself, (void *)new_parser);
1091
1092 /* allocate and clear handlers first */
Fred Drake2a3d7db2002-06-28 22:56:48 +00001093 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +00001094 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001095
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001096 new_parser->handlers = PyMem_New(PyObject *, i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001097 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +00001098 Py_DECREF(new_parser);
1099 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001100 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001101 clear_handlers(new_parser, 1);
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001102
1103 /* then copy handlers from self */
1104 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001105 PyObject *handler = self->handlers[i];
1106 if (handler != NULL) {
1107 Py_INCREF(handler);
1108 new_parser->handlers[i] = handler;
1109 handler_info[i].setter(new_parser->itself,
Fred Drake85d835f2001-02-08 15:39:08 +00001110 handler_info[i].handler);
1111 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001112 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001113 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001114}
1115
Brett Cannond0aeda82014-08-22 14:23:20 -04001116/*[clinic input]
1117pyexpat.xmlparser.SetParamEntityParsing
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001118
Brett Cannond0aeda82014-08-22 14:23:20 -04001119 flag: int
1120 /
1121
1122Controls parsing of parameter entities (including the external DTD subset).
1123
1124Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,
1125XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and
1126XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag
1127was successful.
1128[clinic start generated code]*/
1129
1130PyDoc_STRVAR(pyexpat_xmlparser_SetParamEntityParsing__doc__,
1131"SetParamEntityParsing($self, flag, /)\n"
1132"--\n"
1133"\n"
1134"Controls parsing of parameter entities (including the external DTD subset).\n"
1135"\n"
1136"Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n"
1137"XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n"
1138"XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n"
1139"was successful.");
1140
1141#define PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF \
1142 {"SetParamEntityParsing", (PyCFunction)pyexpat_xmlparser_SetParamEntityParsing, METH_VARARGS, pyexpat_xmlparser_SetParamEntityParsing__doc__},
1143
1144static PyObject *
1145pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag);
1146
1147static PyObject *
1148pyexpat_xmlparser_SetParamEntityParsing(xmlparseobject *self, PyObject *args)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001149{
Brett Cannond0aeda82014-08-22 14:23:20 -04001150 PyObject *return_value = NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00001151 int flag;
Brett Cannond0aeda82014-08-22 14:23:20 -04001152
1153 if (!PyArg_ParseTuple(args,
1154 "i:SetParamEntityParsing",
1155 &flag))
1156 goto exit;
1157 return_value = pyexpat_xmlparser_SetParamEntityParsing_impl(self, flag);
1158
1159exit:
1160 return return_value;
1161}
1162
1163static PyObject *
1164pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag)
1165/*[clinic end generated code: output=0f820882bc7768cc input=8aea19b4b15e9af1]*/
1166{
1167 flag = XML_SetParamEntityParsing(self->itself, flag);
Christian Heimes217cfd12007-12-02 14:31:20 +00001168 return PyLong_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001169}
1170
Martin v. Löwisc847f402003-01-21 11:09:21 +00001171
1172#if XML_COMBINED_VERSION >= 19505
Brett Cannond0aeda82014-08-22 14:23:20 -04001173/*[clinic input]
1174pyexpat.xmlparser.UseForeignDTD
1175
1176 flag: bool = True
1177 /
1178
1179Allows the application to provide an artificial external subset if one is not specified as part of the document instance.
1180
1181This readily allows the use of a 'default' document type controlled by the
1182application, while still getting the advantage of providing document type
1183information to the parser. 'flag' defaults to True if not provided.
1184[clinic start generated code]*/
1185
1186PyDoc_STRVAR(pyexpat_xmlparser_UseForeignDTD__doc__,
1187"UseForeignDTD($self, flag=True, /)\n"
1188"--\n"
1189"\n"
1190"Allows the application to provide an artificial external subset if one is not specified as part of the document instance.\n"
1191"\n"
1192"This readily allows the use of a \'default\' document type controlled by the\n"
1193"application, while still getting the advantage of providing document type\n"
1194"information to the parser. \'flag\' defaults to True if not provided.");
1195
1196#define PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF \
1197 {"UseForeignDTD", (PyCFunction)pyexpat_xmlparser_UseForeignDTD, METH_VARARGS, pyexpat_xmlparser_UseForeignDTD__doc__},
Martin v. Löwis069dde22003-01-21 10:58:18 +00001198
1199static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -04001200pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag);
1201
1202static PyObject *
1203pyexpat_xmlparser_UseForeignDTD(xmlparseobject *self, PyObject *args)
Martin v. Löwis069dde22003-01-21 10:58:18 +00001204{
Brett Cannond0aeda82014-08-22 14:23:20 -04001205 PyObject *return_value = NULL;
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001206 int flag = 1;
Brett Cannond0aeda82014-08-22 14:23:20 -04001207
1208 if (!PyArg_ParseTuple(args,
1209 "|p:UseForeignDTD",
1210 &flag))
1211 goto exit;
1212 return_value = pyexpat_xmlparser_UseForeignDTD_impl(self, flag);
1213
1214exit:
1215 return return_value;
1216}
1217
1218static PyObject *
1219pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag)
1220/*[clinic end generated code: output=22e924ae6cad67d6 input=78144c519d116a6e]*/
1221{
Martin v. Löwis069dde22003-01-21 10:58:18 +00001222 enum XML_Error rc;
Brett Cannond0aeda82014-08-22 14:23:20 -04001223
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001224 rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
Martin v. Löwis069dde22003-01-21 10:58:18 +00001225 if (rc != XML_ERROR_NONE) {
1226 return set_error(self, rc);
1227 }
1228 Py_INCREF(Py_None);
1229 return Py_None;
1230}
Martin v. Löwisc847f402003-01-21 11:09:21 +00001231#endif
Martin v. Löwis069dde22003-01-21 10:58:18 +00001232
Brett Cannond0aeda82014-08-22 14:23:20 -04001233/*[clinic input]
1234pyexpat.xmlparser.__dir__
1235[clinic start generated code]*/
1236
1237PyDoc_STRVAR(pyexpat_xmlparser___dir____doc__,
1238"__dir__($self, /)\n"
1239"--");
1240
1241#define PYEXPAT_XMLPARSER___DIR___METHODDEF \
1242 {"__dir__", (PyCFunction)pyexpat_xmlparser___dir__, METH_NOARGS, pyexpat_xmlparser___dir____doc__},
1243
1244static PyObject *
1245pyexpat_xmlparser___dir___impl(xmlparseobject *self);
1246
1247static PyObject *
1248pyexpat_xmlparser___dir__(xmlparseobject *self, PyObject *Py_UNUSED(ignored))
1249{
1250 return pyexpat_xmlparser___dir___impl(self);
1251}
1252
1253static PyObject *
1254pyexpat_xmlparser___dir___impl(xmlparseobject *self)
1255/*[clinic end generated code: output=1ed6efe83bc304cc input=76aa455f2a661384]*/
1256{
1257#define APPEND(list, str) \
1258 do { \
1259 PyObject *o = PyUnicode_FromString(str); \
1260 if (o != NULL) \
1261 PyList_Append(list, o); \
1262 Py_XDECREF(o); \
1263 } while (0)
1264
1265 int i;
1266 PyObject *rc = PyList_New(0);
1267 if (!rc)
1268 return NULL;
1269 for (i = 0; handler_info[i].name != NULL; i++) {
1270 PyObject *o = get_handler_name(&handler_info[i]);
1271 if (o != NULL)
1272 PyList_Append(rc, o);
1273 Py_XDECREF(o);
1274 }
1275 APPEND(rc, "ErrorCode");
1276 APPEND(rc, "ErrorLineNumber");
1277 APPEND(rc, "ErrorColumnNumber");
1278 APPEND(rc, "ErrorByteIndex");
1279 APPEND(rc, "CurrentLineNumber");
1280 APPEND(rc, "CurrentColumnNumber");
1281 APPEND(rc, "CurrentByteIndex");
1282 APPEND(rc, "buffer_size");
1283 APPEND(rc, "buffer_text");
1284 APPEND(rc, "buffer_used");
1285 APPEND(rc, "namespace_prefixes");
1286 APPEND(rc, "ordered_attributes");
1287 APPEND(rc, "specified_attributes");
1288 APPEND(rc, "intern");
1289
1290#undef APPEND
1291
1292 if (PyErr_Occurred()) {
1293 Py_DECREF(rc);
1294 rc = NULL;
1295 }
1296
1297 return rc;
1298}
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001299
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001300static struct PyMethodDef xmlparse_methods[] = {
Brett Cannond0aeda82014-08-22 14:23:20 -04001301 PYEXPAT_XMLPARSER_PARSE_METHODDEF
1302 PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF
1303 PYEXPAT_XMLPARSER_SETBASE_METHODDEF
1304 PYEXPAT_XMLPARSER_GETBASE_METHODDEF
1305 PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF
1306 PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF
1307 PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF
Martin v. Löwisc847f402003-01-21 11:09:21 +00001308#if XML_COMBINED_VERSION >= 19505
Brett Cannond0aeda82014-08-22 14:23:20 -04001309 PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
Martin v. Löwisc847f402003-01-21 11:09:21 +00001310#endif
Brett Cannond0aeda82014-08-22 14:23:20 -04001311 PYEXPAT_XMLPARSER___DIR___METHODDEF
1312 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001313};
1314
1315/* ---------- */
1316
1317
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001318
Fred Drake71b63ff2002-06-28 22:29:01 +00001319/* pyexpat international encoding support.
1320 Make it as simple as possible.
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001321*/
1322
Fred Drake71b63ff2002-06-28 22:29:01 +00001323static int
1324PyUnknownEncodingHandler(void *encodingHandlerData,
1325 const XML_Char *name,
1326 XML_Encoding *info)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001327{
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001328 static unsigned char template_buffer[256] = {0};
1329 PyObject* u;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001330 int i;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001331 void *data;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001332 unsigned int kind;
Fred Drake71b63ff2002-06-28 22:29:01 +00001333
Victor Stinner9e09c262013-07-18 23:17:01 +02001334 if (PyErr_Occurred())
1335 return XML_STATUS_ERROR;
1336
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001337 if (template_buffer[1] == 0) {
1338 for (i = 0; i < 256; i++)
1339 template_buffer[i] = i;
Tim Peters63cb99e2001-02-17 18:12:50 +00001340 }
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001341
1342 u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace");
Christian Heimesb5821552013-06-29 20:43:13 +02001343 if (u == NULL || PyUnicode_READY(u)) {
Christian Heimes72172422013-06-29 21:49:27 +02001344 Py_XDECREF(u);
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001345 return XML_STATUS_ERROR;
Christian Heimesb5821552013-06-29 20:43:13 +02001346 }
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001347
1348 if (PyUnicode_GET_LENGTH(u) != 256) {
1349 Py_DECREF(u);
1350 PyErr_SetString(PyExc_ValueError,
1351 "multi-byte encodings are not supported");
1352 return XML_STATUS_ERROR;
1353 }
1354
1355 kind = PyUnicode_KIND(u);
1356 data = PyUnicode_DATA(u);
1357 for (i = 0; i < 256; i++) {
1358 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1359 if (ch != Py_UNICODE_REPLACEMENT_CHARACTER)
1360 info->map[i] = ch;
1361 else
1362 info->map[i] = -1;
1363 }
1364
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001365 info->data = NULL;
1366 info->convert = NULL;
1367 info->release = NULL;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07001368 Py_DECREF(u);
1369
1370 return XML_STATUS_OK;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001371}
1372
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001373
1374static PyObject *
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001375newxmlparseobject(const char *encoding, const char *namespace_separator, PyObject *intern)
Fred Drake0582df92000-07-12 04:49:00 +00001376{
1377 int i;
1378 xmlparseobject *self;
Fred Drake71b63ff2002-06-28 22:29:01 +00001379
Martin v. Löwis894258c2001-09-23 10:20:10 +00001380 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
Fred Drake0582df92000-07-12 04:49:00 +00001381 if (self == NULL)
1382 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001383
Fred Drake2a3d7db2002-06-28 22:56:48 +00001384 self->buffer = NULL;
1385 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1386 self->buffer_used = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001387 self->ordered_attributes = 0;
1388 self->specified_attributes = 0;
Fred Drakebd6101c2001-02-14 18:29:45 +00001389 self->in_callback = 0;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001390 self->ns_prefixes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001391 self->handlers = NULL;
Victor Stinner54b2d2e2013-07-15 17:15:57 +02001392 self->intern = intern;
1393 Py_XINCREF(self->intern);
1394 PyObject_GC_Track(self);
1395
Christian Heimesfa535f52013-07-07 17:35:11 +02001396 /* namespace_separator is either NULL or contains one char + \0 */
1397 self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler,
1398 namespace_separator);
Victor Stinner54b2d2e2013-07-15 17:15:57 +02001399 if (self->itself == NULL) {
1400 PyErr_SetString(PyExc_RuntimeError,
1401 "XML_ParserCreate failed");
1402 Py_DECREF(self);
1403 return NULL;
1404 }
Gregory P. Smith25227712012-03-14 18:10:37 -07001405#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT)
1406 /* This feature was added upstream in libexpat 2.1.0. Our expat copy
1407 * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT
1408 * to indicate that we can still use it. */
Gregory P. Smith8e91cf62012-03-14 14:26:55 -07001409 XML_SetHashSalt(self->itself,
Christian Heimes985ecdc2013-11-20 11:46:18 +01001410 (unsigned long)_Py_HashSecret.expat.hashsalt);
Gregory P. Smith25227712012-03-14 18:10:37 -07001411#endif
Fred Drake0582df92000-07-12 04:49:00 +00001412 XML_SetUserData(self->itself, (void *)self);
Fred Drake7c75bf22002-07-01 14:02:31 +00001413 XML_SetUnknownEncodingHandler(self->itself,
1414 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001415
Fred Drake2a3d7db2002-06-28 22:56:48 +00001416 for (i = 0; handler_info[i].name != NULL; i++)
Fred Drake0582df92000-07-12 04:49:00 +00001417 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001418
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001419 self->handlers = PyMem_New(PyObject *, i);
Fred Drake7c75bf22002-07-01 14:02:31 +00001420 if (!self->handlers) {
Fred Drake71b63ff2002-06-28 22:29:01 +00001421 Py_DECREF(self);
1422 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001423 }
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001424 clear_handlers(self, 1);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001425
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001426 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001427}
1428
1429
1430static void
Fred Drake0582df92000-07-12 04:49:00 +00001431xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001432{
Fred Drake0582df92000-07-12 04:49:00 +00001433 int i;
Martin v. Löwis894258c2001-09-23 10:20:10 +00001434 PyObject_GC_UnTrack(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001435 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001436 XML_ParserFree(self->itself);
1437 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001438
Fred Drake85d835f2001-02-08 15:39:08 +00001439 if (self->handlers != NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001440 PyObject *temp;
Fred Drake85d835f2001-02-08 15:39:08 +00001441 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drakecde79132001-04-25 16:01:30 +00001442 temp = self->handlers[i];
1443 self->handlers[i] = NULL;
1444 Py_XDECREF(temp);
Fred Drake85d835f2001-02-08 15:39:08 +00001445 }
Victor Stinnerb6404912013-07-07 16:21:41 +02001446 PyMem_Free(self->handlers);
Fred Drake71b63ff2002-06-28 22:29:01 +00001447 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001448 }
Fred Drake2a3d7db2002-06-28 22:56:48 +00001449 if (self->buffer != NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001450 PyMem_Free(self->buffer);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001451 self->buffer = NULL;
1452 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001453 Py_XDECREF(self->intern);
Martin v. Löwis894258c2001-09-23 10:20:10 +00001454 PyObject_GC_Del(self);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001455}
1456
Fred Drake0582df92000-07-12 04:49:00 +00001457static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001458handlername2int(PyObject *name)
Fred Drake0582df92000-07-12 04:49:00 +00001459{
1460 int i;
Fred Drake71b63ff2002-06-28 22:29:01 +00001461 for (i = 0; handler_info[i].name != NULL; i++) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001462 if (PyUnicode_CompareWithASCIIString(
1463 name, handler_info[i].name) == 0) {
Fred Drake0582df92000-07-12 04:49:00 +00001464 return i;
1465 }
1466 }
1467 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001468}
1469
1470static PyObject *
Fred Drake71b63ff2002-06-28 22:29:01 +00001471get_pybool(int istrue)
1472{
1473 PyObject *result = istrue ? Py_True : Py_False;
1474 Py_INCREF(result);
1475 return result;
1476}
1477
1478static PyObject *
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001479xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001480{
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001481 Py_UCS4 first_char;
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001482 int handlernum = -1;
1483
Alexander Belopolskye239d232010-12-08 23:31:48 +00001484 if (!PyUnicode_Check(nameobj))
1485 goto generic;
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001486 if (PyUnicode_READY(nameobj))
1487 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488
Alexander Belopolskye239d232010-12-08 23:31:48 +00001489 handlernum = handlername2int(nameobj);
Fred Drake71b63ff2002-06-28 22:29:01 +00001490
1491 if (handlernum != -1) {
1492 PyObject *result = self->handlers[handlernum];
1493 if (result == NULL)
1494 result = Py_None;
1495 Py_INCREF(result);
1496 return result;
1497 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001498
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001499 first_char = PyUnicode_READ_CHAR(nameobj, 0);
1500 if (first_char == 'E') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001501 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorCode") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001502 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001503 XML_GetErrorCode(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001504 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001505 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001506 XML_GetErrorLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001507 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001508 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001509 XML_GetErrorColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001510 if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001511 return PyLong_FromLong((long)
Fred Drake71b63ff2002-06-28 22:29:01 +00001512 XML_GetErrorByteIndex(self->itself));
1513 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001514 if (first_char == 'C') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001515 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentLineNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001516 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001517 XML_GetCurrentLineNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001518 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentColumnNumber") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001519 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001520 XML_GetCurrentColumnNumber(self->itself));
Alexander Belopolskye239d232010-12-08 23:31:48 +00001521 if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentByteIndex") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001522 return PyLong_FromLong((long)
Dave Cole3203efb2004-08-26 00:37:31 +00001523 XML_GetCurrentByteIndex(self->itself));
1524 }
Victor Stinner9e5bd6c2011-10-01 01:05:40 +02001525 if (first_char == 'b') {
Alexander Belopolskye239d232010-12-08 23:31:48 +00001526 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_size") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001527 return PyLong_FromLong((long) self->buffer_size);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001528 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_text") == 0)
Fred Drake2a3d7db2002-06-28 22:56:48 +00001529 return get_pybool(self->buffer != NULL);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001530 if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_used") == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +00001531 return PyLong_FromLong((long) self->buffer_used);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001532 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001533 if (PyUnicode_CompareWithASCIIString(nameobj, "namespace_prefixes") == 0)
Martin v. Löwis069dde22003-01-21 10:58:18 +00001534 return get_pybool(self->ns_prefixes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001535 if (PyUnicode_CompareWithASCIIString(nameobj, "ordered_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001536 return get_pybool(self->ordered_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001537 if (PyUnicode_CompareWithASCIIString(nameobj, "specified_attributes") == 0)
Fred Drake71b63ff2002-06-28 22:29:01 +00001538 return get_pybool((long) self->specified_attributes);
Alexander Belopolskye239d232010-12-08 23:31:48 +00001539 if (PyUnicode_CompareWithASCIIString(nameobj, "intern") == 0) {
Fred Drakeb91a36b2002-06-27 19:40:48 +00001540 if (self->intern == NULL) {
1541 Py_INCREF(Py_None);
1542 return Py_None;
1543 }
1544 else {
1545 Py_INCREF(self->intern);
1546 return self->intern;
1547 }
1548 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001549 generic:
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001550 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +00001551}
1552
Fred Drake6f987622000-08-25 18:03:30 +00001553static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001554sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001555{
1556 int handlernum = handlername2int(name);
Fred Drake71b63ff2002-06-28 22:29:01 +00001557 if (handlernum >= 0) {
1558 xmlhandler c_handler = NULL;
1559 PyObject *temp = self->handlers[handlernum];
1560
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001561 if (v == Py_None) {
1562 /* If this is the character data handler, and a character
1563 data handler is already active, we need to be more
1564 careful. What we can safely do is replace the existing
1565 character data handler callback function with a no-op
1566 function that will refuse to call Python. The downside
1567 is that this doesn't completely remove the character
1568 data handler from the C layer if there's any callback
1569 active, so Expat does a little more work than it
1570 otherwise would, but that's really an odd case. A more
1571 elaborate system of handlers and state could remove the
1572 C handler more effectively. */
1573 if (handlernum == CharacterData && self->in_callback)
1574 c_handler = noop_character_data_handler;
Fred Drake71b63ff2002-06-28 22:29:01 +00001575 v = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001576 }
Fred Drake71b63ff2002-06-28 22:29:01 +00001577 else if (v != NULL) {
1578 Py_INCREF(v);
1579 c_handler = handler_info[handlernum].handler;
1580 }
Fred Drake0582df92000-07-12 04:49:00 +00001581 self->handlers[handlernum] = v;
Fred Drake71b63ff2002-06-28 22:29:01 +00001582 Py_XDECREF(temp);
1583 handler_info[handlernum].setter(self->itself, c_handler);
Fred Drake0582df92000-07-12 04:49:00 +00001584 return 1;
1585 }
1586 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001587}
1588
1589static int
Alexander Belopolskye239d232010-12-08 23:31:48 +00001590xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001591{
Fred Drake6f987622000-08-25 18:03:30 +00001592 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001593 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001594 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1595 return -1;
1596 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001597 assert(PyUnicode_Check(name));
1598 if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001599 int b = PyObject_IsTrue(v);
1600 if (b < 0)
1601 return -1;
1602 if (b) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001603 if (self->buffer == NULL) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001604 self->buffer = PyMem_Malloc(self->buffer_size);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001605 if (self->buffer == NULL) {
1606 PyErr_NoMemory();
1607 return -1;
1608 }
1609 self->buffer_used = 0;
1610 }
1611 }
1612 else if (self->buffer != NULL) {
1613 if (flush_character_buffer(self) < 0)
1614 return -1;
Victor Stinnerb6404912013-07-07 16:21:41 +02001615 PyMem_Free(self->buffer);
Fred Drake2a3d7db2002-06-28 22:56:48 +00001616 self->buffer = NULL;
1617 }
1618 return 0;
1619 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001620 if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001621 int b = PyObject_IsTrue(v);
1622 if (b < 0)
1623 return -1;
1624 self->ns_prefixes = b;
Martin v. Löwis069dde22003-01-21 10:58:18 +00001625 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1626 return 0;
1627 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001628 if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001629 int b = PyObject_IsTrue(v);
1630 if (b < 0)
1631 return -1;
1632 self->ordered_attributes = b;
Fred Drake85d835f2001-02-08 15:39:08 +00001633 return 0;
1634 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001635 if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
Antoine Pitrou6f430e42012-08-15 23:18:25 +02001636 int b = PyObject_IsTrue(v);
1637 if (b < 0)
1638 return -1;
1639 self->specified_attributes = b;
Fred Drake6f987622000-08-25 18:03:30 +00001640 return 0;
1641 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001642
Alexander Belopolskye239d232010-12-08 23:31:48 +00001643 if (PyUnicode_CompareWithASCIIString(name, "buffer_size") == 0) {
Christian Heimes2380ac72008-01-09 00:17:24 +00001644 long new_buffer_size;
1645 if (!PyLong_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1647 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001648 }
1649
1650 new_buffer_size=PyLong_AS_LONG(v);
1651 /* trivial case -- no change */
1652 if (new_buffer_size == self->buffer_size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 return 0;
Christian Heimes2380ac72008-01-09 00:17:24 +00001654 }
1655
1656 if (new_buffer_size <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1658 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001659 }
1660
1661 /* check maximum */
1662 if (new_buffer_size > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 char errmsg[100];
1664 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1665 PyErr_SetString(PyExc_ValueError, errmsg);
1666 return -1;
Christian Heimes2380ac72008-01-09 00:17:24 +00001667 }
1668
1669 if (self->buffer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 /* there is already a buffer */
1671 if (self->buffer_used != 0) {
Christian Heimes09994a92013-07-20 22:41:58 +02001672 if (flush_character_buffer(self) < 0) {
1673 return -1;
1674 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 }
1676 /* free existing buffer */
Victor Stinnerb6404912013-07-07 16:21:41 +02001677 PyMem_Free(self->buffer);
Christian Heimes2380ac72008-01-09 00:17:24 +00001678 }
Victor Stinnerb6404912013-07-07 16:21:41 +02001679 self->buffer = PyMem_Malloc(new_buffer_size);
Christian Heimes2380ac72008-01-09 00:17:24 +00001680 if (self->buffer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 PyErr_NoMemory();
1682 return -1;
1683 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001684 self->buffer_size = new_buffer_size;
1685 return 0;
1686 }
1687
Alexander Belopolskye239d232010-12-08 23:31:48 +00001688 if (PyUnicode_CompareWithASCIIString(name, "CharacterDataHandler") == 0) {
Fred Drake2a3d7db2002-06-28 22:56:48 +00001689 /* If we're changing the character data handler, flush all
1690 * cached data with the old handler. Not sure there's a
1691 * "right" thing to do, though, but this probably won't
1692 * happen.
1693 */
1694 if (flush_character_buffer(self) < 0)
1695 return -1;
1696 }
Fred Drake6f987622000-08-25 18:03:30 +00001697 if (sethandler(self, name, v)) {
1698 return 0;
1699 }
Alexander Belopolskye239d232010-12-08 23:31:48 +00001700 PyErr_SetObject(PyExc_AttributeError, name);
Fred Drake6f987622000-08-25 18:03:30 +00001701 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001702}
1703
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001704static int
1705xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1706{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001707 int i;
1708 for (i = 0; handler_info[i].name != NULL; i++)
1709 Py_VISIT(op->handlers[i]);
Fred Drakecde79132001-04-25 16:01:30 +00001710 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001711}
1712
1713static int
1714xmlparse_clear(xmlparseobject *op)
1715{
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00001716 clear_handlers(op, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001717 Py_CLEAR(op->intern);
Fred Drakecde79132001-04-25 16:01:30 +00001718 return 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001719}
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001721PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001722
1723static PyTypeObject Xmlparsetype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 PyVarObject_HEAD_INIT(NULL, 0)
1725 "pyexpat.xmlparser", /*tp_name*/
Antoine Pitrou23683ef2011-01-04 00:00:31 +00001726 sizeof(xmlparseobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 0, /*tp_itemsize*/
1728 /* methods */
1729 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1730 (printfunc)0, /*tp_print*/
1731 0, /*tp_getattr*/
Alexander Belopolskye239d232010-12-08 23:31:48 +00001732 0, /*tp_setattr*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 0, /*tp_reserved*/
1734 (reprfunc)0, /*tp_repr*/
1735 0, /*tp_as_number*/
1736 0, /*tp_as_sequence*/
1737 0, /*tp_as_mapping*/
1738 (hashfunc)0, /*tp_hash*/
1739 (ternaryfunc)0, /*tp_call*/
1740 (reprfunc)0, /*tp_str*/
1741 (getattrofunc)xmlparse_getattro, /* tp_getattro */
Alexander Belopolskye239d232010-12-08 23:31:48 +00001742 (setattrofunc)xmlparse_setattro, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 0, /* tp_as_buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 Xmlparsetype__doc__, /* tp_doc - Documentation string */
1746 (traverseproc)xmlparse_traverse, /* tp_traverse */
1747 (inquiry)xmlparse_clear, /* tp_clear */
1748 0, /* tp_richcompare */
1749 0, /* tp_weaklistoffset */
1750 0, /* tp_iter */
1751 0, /* tp_iternext */
1752 xmlparse_methods, /* tp_methods */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001753};
1754
1755/* End of code for xmlparser objects */
1756/* -------------------------------------------------------- */
1757
Brett Cannond0aeda82014-08-22 14:23:20 -04001758/*[clinic input]
1759pyexpat.ParserCreate
1760
1761 encoding: str(nullable=True) = NULL
1762 namespace_separator: str(nullable=True) = NULL
1763 intern: object = NULL
1764
1765Return a new XML parser object.
1766[clinic start generated code]*/
1767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
Brett Cannond0aeda82014-08-22 14:23:20 -04001769"ParserCreate($module, /, encoding=None, namespace_separator=None,\n"
1770" intern=None)\n"
1771"--\n"
1772"\n"
1773"Return a new XML parser object.");
1774
1775#define PYEXPAT_PARSERCREATE_METHODDEF \
1776 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate, METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001777
1778static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -04001779pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, const char *namespace_separator, PyObject *intern);
1780
1781static PyObject *
1782pyexpat_ParserCreate(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Fred Drake0582df92000-07-12 04:49:00 +00001783{
Brett Cannond0aeda82014-08-22 14:23:20 -04001784 PyObject *return_value = NULL;
1785 static char *_keywords[] = {"encoding", "namespace_separator", "intern", NULL};
1786 const char *encoding = NULL;
1787 const char *namespace_separator = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001788 PyObject *intern = NULL;
Brett Cannond0aeda82014-08-22 14:23:20 -04001789
1790 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
1791 "|zzO:ParserCreate", _keywords,
1792 &encoding, &namespace_separator, &intern))
1793 goto exit;
1794 return_value = pyexpat_ParserCreate_impl(module, encoding, namespace_separator, intern);
1795
1796exit:
1797 return return_value;
1798}
1799
1800static PyObject *
1801pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, const char *namespace_separator, PyObject *intern)
1802/*[clinic end generated code: output=4fc027dd33b7a2ac input=71b9f471aa6f8f86]*/
1803{
Fred Drakeb91a36b2002-06-27 19:40:48 +00001804 PyObject *result;
1805 int intern_decref = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001806
Fred Drakecde79132001-04-25 16:01:30 +00001807 if (namespace_separator != NULL
1808 && strlen(namespace_separator) > 1) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "namespace_separator must be at most one"
1811 " character, omitted, or None");
1812 return NULL;
1813 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001814 /* Explicitly passing None means no interning is desired.
1815 Not passing anything means that a new dictionary is used. */
1816 if (intern == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 intern = NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001818 else if (intern == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 intern = PyDict_New();
1820 if (!intern)
1821 return NULL;
1822 intern_decref = 1;
Fred Drake71b63ff2002-06-28 22:29:01 +00001823 }
Fred Drakeb91a36b2002-06-27 19:40:48 +00001824 else if (!PyDict_Check(intern)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1826 return NULL;
Fred Drakeb91a36b2002-06-27 19:40:48 +00001827 }
1828
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001829 result = newxmlparseobject(encoding, namespace_separator, intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001830 if (intern_decref) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 Py_DECREF(intern);
Fred Drakeb91a36b2002-06-27 19:40:48 +00001832 }
1833 return result;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001834}
1835
Brett Cannond0aeda82014-08-22 14:23:20 -04001836/*[clinic input]
1837pyexpat.ErrorString
1838
1839 code: long
1840 /
1841
1842Returns string error for given number.
1843[clinic start generated code]*/
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(pyexpat_ErrorString__doc__,
Brett Cannond0aeda82014-08-22 14:23:20 -04001846"ErrorString($module, code, /)\n"
1847"--\n"
1848"\n"
1849"Returns string error for given number.");
1850
1851#define PYEXPAT_ERRORSTRING_METHODDEF \
1852 {"ErrorString", (PyCFunction)pyexpat_ErrorString, METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001853
1854static PyObject *
Brett Cannond0aeda82014-08-22 14:23:20 -04001855pyexpat_ErrorString_impl(PyModuleDef *module, long code);
Fred Drake0582df92000-07-12 04:49:00 +00001856
Brett Cannond0aeda82014-08-22 14:23:20 -04001857static PyObject *
1858pyexpat_ErrorString(PyModuleDef *module, PyObject *args)
1859{
1860 PyObject *return_value = NULL;
1861 long code;
1862
1863 if (!PyArg_ParseTuple(args,
1864 "l:ErrorString",
1865 &code))
1866 goto exit;
1867 return_value = pyexpat_ErrorString_impl(module, code);
1868
1869exit:
1870 return return_value;
1871}
1872
1873static PyObject *
1874pyexpat_ErrorString_impl(PyModuleDef *module, long code)
1875/*[clinic end generated code: output=c70f3cd82bfaf067 input=cc67de010d9e62b3]*/
1876{
Fred Drake0582df92000-07-12 04:49:00 +00001877 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001878}
1879
1880/* List of methods defined in the module */
1881
1882static struct PyMethodDef pyexpat_methods[] = {
Brett Cannond0aeda82014-08-22 14:23:20 -04001883 PYEXPAT_PARSERCREATE_METHODDEF
1884 PYEXPAT_ERRORSTRING_METHODDEF
1885 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001886};
1887
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001888/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890PyDoc_STRVAR(pyexpat_module_documentation,
1891"Python wrapper for Expat parser.");
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001892
Fred Drakecde79132001-04-25 16:01:30 +00001893/* Initialization function for the module */
1894
1895#ifndef MODULE_NAME
1896#define MODULE_NAME "pyexpat"
1897#endif
1898
1899#ifndef MODULE_INITFUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001900#define MODULE_INITFUNC PyInit_pyexpat
Fred Drakecde79132001-04-25 16:01:30 +00001901#endif
1902
Martin v. Löwis1a214512008-06-11 05:26:20 +00001903static struct PyModuleDef pyexpatmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 PyModuleDef_HEAD_INIT,
1905 MODULE_NAME,
1906 pyexpat_module_documentation,
1907 -1,
1908 pyexpat_methods,
1909 NULL,
1910 NULL,
1911 NULL,
1912 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001913};
1914
Martin v. Löwis069dde22003-01-21 10:58:18 +00001915PyMODINIT_FUNC
1916MODULE_INITFUNC(void)
Fred Drake0582df92000-07-12 04:49:00 +00001917{
1918 PyObject *m, *d;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001919 PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001920 PyObject *errors_module;
1921 PyObject *modelmod_name;
1922 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001923 PyObject *sys_modules;
Georg Brandlb4dac712010-10-15 14:46:48 +00001924 PyObject *tmpnum, *tmpstr;
1925 PyObject *codes_dict;
1926 PyObject *rev_codes_dict;
1927 int res;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00001928 static struct PyExpat_CAPI capi;
Georg Brandlb4dac712010-10-15 14:46:48 +00001929 PyObject *capi_object;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001930
Fred Drake6f987622000-08-25 18:03:30 +00001931 if (errmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001932 return NULL;
Neal Norwitz392c5be2007-08-25 17:20:32 +00001933 modelmod_name = PyUnicode_FromString(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001934 if (modelmod_name == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001935 return NULL;
Fred Drake6f987622000-08-25 18:03:30 +00001936
Amaury Forgeot d'Arcba4105c2008-07-02 21:41:01 +00001937 if (PyType_Ready(&Xmlparsetype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001939
Fred Drake0582df92000-07-12 04:49:00 +00001940 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001941 m = PyModule_Create(&pyexpatmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001942 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001944
Fred Drake0582df92000-07-12 04:49:00 +00001945 /* Add some symbolic constants to the module */
Fred Drakebd6101c2001-02-14 18:29:45 +00001946 if (ErrorObject == NULL) {
1947 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
Fred Drake93adb692000-09-23 04:55:48 +00001948 NULL, NULL);
Fred Drakebd6101c2001-02-14 18:29:45 +00001949 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001950 return NULL;
Fred Drakebd6101c2001-02-14 18:29:45 +00001951 }
1952 Py_INCREF(ErrorObject);
Fred Drake93adb692000-09-23 04:55:48 +00001953 PyModule_AddObject(m, "error", ErrorObject);
Fred Drakebd6101c2001-02-14 18:29:45 +00001954 Py_INCREF(ErrorObject);
1955 PyModule_AddObject(m, "ExpatError", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001956 Py_INCREF(&Xmlparsetype);
1957 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001958
Fred Drake738293d2000-12-21 17:25:07 +00001959 PyModule_AddStringConstant(m, "EXPAT_VERSION",
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03001960 XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001961 {
1962 XML_Expat_Version info = XML_ExpatVersionInfo();
1963 PyModule_AddObject(m, "version_info",
1964 Py_BuildValue("(iii)", info.major,
1965 info.minor, info.micro));
1966 }
Fred Drake0582df92000-07-12 04:49:00 +00001967 /* XXX When Expat supports some way of figuring out how it was
Fred Drake71b63ff2002-06-28 22:29:01 +00001968 compiled, this should check and set native_encoding
1969 appropriately.
Fred Drake0582df92000-07-12 04:49:00 +00001970 */
Fred Drake93adb692000-09-23 04:55:48 +00001971 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001972
Fred Drake85d835f2001-02-08 15:39:08 +00001973 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001974 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001975 errors_module = PyDict_GetItem(d, errmod_name);
1976 if (errors_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001977 errors_module = PyModule_New(MODULE_NAME ".errors");
Fred Drake6f987622000-08-25 18:03:30 +00001978 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001979 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001980 /* gives away the reference to errors_module */
1981 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001982 }
1983 }
Fred Drake6f987622000-08-25 18:03:30 +00001984 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001985 model_module = PyDict_GetItem(d, modelmod_name);
1986 if (model_module == NULL) {
Fred Drakecde79132001-04-25 16:01:30 +00001987 model_module = PyModule_New(MODULE_NAME ".model");
Fred Drake85d835f2001-02-08 15:39:08 +00001988 if (model_module != NULL) {
1989 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1990 /* gives away the reference to model_module */
1991 PyModule_AddObject(m, "model", model_module);
1992 }
1993 }
1994 Py_DECREF(modelmod_name);
1995 if (errors_module == NULL || model_module == NULL)
1996 /* Don't core dump later! */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001997 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Martin v. Löwisc847f402003-01-21 11:09:21 +00001999#if XML_COMBINED_VERSION > 19505
Martin v. Löwis069dde22003-01-21 10:58:18 +00002000 {
2001 const XML_Feature *features = XML_GetFeatureList();
2002 PyObject *list = PyList_New(0);
2003 if (list == NULL)
2004 /* just ignore it */
2005 PyErr_Clear();
2006 else {
2007 int i = 0;
2008 for (; features[i].feature != XML_FEATURE_END; ++i) {
2009 int ok;
2010 PyObject *item = Py_BuildValue("si", features[i].name,
2011 features[i].value);
2012 if (item == NULL) {
2013 Py_DECREF(list);
2014 list = NULL;
2015 break;
2016 }
2017 ok = PyList_Append(list, item);
2018 Py_DECREF(item);
2019 if (ok < 0) {
2020 PyErr_Clear();
2021 break;
2022 }
2023 }
2024 if (list != NULL)
2025 PyModule_AddObject(m, "features", list);
2026 }
2027 }
Martin v. Löwisc847f402003-01-21 11:09:21 +00002028#endif
Fred Drake6f987622000-08-25 18:03:30 +00002029
Georg Brandlb4dac712010-10-15 14:46:48 +00002030 codes_dict = PyDict_New();
2031 rev_codes_dict = PyDict_New();
2032 if (codes_dict == NULL || rev_codes_dict == NULL) {
2033 Py_XDECREF(codes_dict);
2034 Py_XDECREF(rev_codes_dict);
2035 return NULL;
2036 }
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002037
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002038#define MYCONST(name) \
Georg Brandlb4dac712010-10-15 14:46:48 +00002039 if (PyModule_AddStringConstant(errors_module, #name, \
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03002040 XML_ErrorString(name)) < 0) \
Georg Brandlb4dac712010-10-15 14:46:48 +00002041 return NULL; \
2042 tmpnum = PyLong_FromLong(name); \
2043 if (tmpnum == NULL) return NULL; \
2044 res = PyDict_SetItemString(codes_dict, \
2045 XML_ErrorString(name), tmpnum); \
2046 if (res < 0) return NULL; \
2047 tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \
2048 if (tmpstr == NULL) return NULL; \
2049 res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \
2050 Py_DECREF(tmpstr); \
2051 Py_DECREF(tmpnum); \
2052 if (res < 0) return NULL; \
Fred Drake7bd9f412000-07-04 23:51:31 +00002053
Fred Drake0582df92000-07-12 04:49:00 +00002054 MYCONST(XML_ERROR_NO_MEMORY);
2055 MYCONST(XML_ERROR_SYNTAX);
2056 MYCONST(XML_ERROR_NO_ELEMENTS);
2057 MYCONST(XML_ERROR_INVALID_TOKEN);
2058 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
2059 MYCONST(XML_ERROR_PARTIAL_CHAR);
2060 MYCONST(XML_ERROR_TAG_MISMATCH);
2061 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
2062 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
2063 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
2064 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
2065 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
2066 MYCONST(XML_ERROR_ASYNC_ENTITY);
2067 MYCONST(XML_ERROR_BAD_CHAR_REF);
2068 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
2069 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
2070 MYCONST(XML_ERROR_MISPLACED_XML_PI);
2071 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
2072 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002073 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
2074 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
2075 MYCONST(XML_ERROR_NOT_STANDALONE);
Fred Drake283b6702004-08-04 22:28:16 +00002076 MYCONST(XML_ERROR_UNEXPECTED_STATE);
2077 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
2078 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
2079 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
2080 /* Added in Expat 1.95.7. */
2081 MYCONST(XML_ERROR_UNBOUND_PREFIX);
2082 /* Added in Expat 1.95.8. */
2083 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
2084 MYCONST(XML_ERROR_INCOMPLETE_PE);
2085 MYCONST(XML_ERROR_XML_DECL);
2086 MYCONST(XML_ERROR_TEXT_DECL);
2087 MYCONST(XML_ERROR_PUBLICID);
2088 MYCONST(XML_ERROR_SUSPENDED);
2089 MYCONST(XML_ERROR_NOT_SUSPENDED);
2090 MYCONST(XML_ERROR_ABORTED);
2091 MYCONST(XML_ERROR_FINISHED);
2092 MYCONST(XML_ERROR_SUSPEND_PE);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002093
Georg Brandlb4dac712010-10-15 14:46:48 +00002094 if (PyModule_AddStringConstant(errors_module, "__doc__",
2095 "Constants used to describe "
2096 "error conditions.") < 0)
2097 return NULL;
Fred Drake85d835f2001-02-08 15:39:08 +00002098
Georg Brandlb4dac712010-10-15 14:46:48 +00002099 if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0)
2100 return NULL;
2101 if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0)
2102 return NULL;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00002103
Fred Drake93adb692000-09-23 04:55:48 +00002104#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002105
Fred Drake85d835f2001-02-08 15:39:08 +00002106#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002107 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
2108 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
2109 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00002110#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002111
Fred Drake85d835f2001-02-08 15:39:08 +00002112#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
2113 PyModule_AddStringConstant(model_module, "__doc__",
2114 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002115
Fred Drake85d835f2001-02-08 15:39:08 +00002116 MYCONST(XML_CTYPE_EMPTY);
2117 MYCONST(XML_CTYPE_ANY);
2118 MYCONST(XML_CTYPE_MIXED);
2119 MYCONST(XML_CTYPE_NAME);
2120 MYCONST(XML_CTYPE_CHOICE);
2121 MYCONST(XML_CTYPE_SEQ);
2122
2123 MYCONST(XML_CQUANT_NONE);
2124 MYCONST(XML_CQUANT_OPT);
2125 MYCONST(XML_CQUANT_REP);
2126 MYCONST(XML_CQUANT_PLUS);
2127#undef MYCONST
Fredrik Lundhc3345042005-12-13 19:49:55 +00002128
2129 /* initialize pyexpat dispatch table */
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002130 capi.size = sizeof(capi);
Fredrik Lundhcc117db2005-12-13 21:55:36 +00002131 capi.magic = PyExpat_CAPI_MAGIC;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002132 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
2133 capi.MINOR_VERSION = XML_MINOR_VERSION;
2134 capi.MICRO_VERSION = XML_MICRO_VERSION;
2135 capi.ErrorString = XML_ErrorString;
Fredrik Lundhcc117db2005-12-13 21:55:36 +00002136 capi.GetErrorCode = XML_GetErrorCode;
2137 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
2138 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002139 capi.Parse = XML_Parse;
2140 capi.ParserCreate_MM = XML_ParserCreate_MM;
2141 capi.ParserFree = XML_ParserFree;
2142 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
2143 capi.SetCommentHandler = XML_SetCommentHandler;
2144 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
2145 capi.SetElementHandler = XML_SetElementHandler;
2146 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
2147 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
2148 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
2149 capi.SetUserData = XML_SetUserData;
Eli Bendersky2b6b73e2012-06-01 11:32:34 +03002150 capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
Serhiy Storchaka66d53fa2013-05-22 17:07:51 +03002151 capi.SetEncoding = XML_SetEncoding;
Eli Bendersky6dc32b32013-05-25 05:25:48 -07002152 capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153
Benjamin Petersonb173f782009-05-05 22:31:58 +00002154 /* export using capsule */
2155 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
Fredrik Lundhd7a42882005-12-13 20:43:04 +00002156 if (capi_object)
2157 PyModule_AddObject(m, "expat_CAPI", capi_object);
Martin v. Löwis1a214512008-06-11 05:26:20 +00002158 return m;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002159}
2160
Fred Drake6f987622000-08-25 18:03:30 +00002161static void
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002162clear_handlers(xmlparseobject *self, int initial)
Fred Drake0582df92000-07-12 04:49:00 +00002163{
Fred Drakecde79132001-04-25 16:01:30 +00002164 int i = 0;
2165 PyObject *temp;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002166
Fred Drake71b63ff2002-06-28 22:29:01 +00002167 for (; handler_info[i].name != NULL; i++) {
Martin v. Löwis5b68ce32001-10-21 08:53:52 +00002168 if (initial)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 self->handlers[i] = NULL;
2170 else {
Fred Drakecde79132001-04-25 16:01:30 +00002171 temp = self->handlers[i];
2172 self->handlers[i] = NULL;
2173 Py_XDECREF(temp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 handler_info[i].setter(self->itself, NULL);
Fred Drakecde79132001-04-25 16:01:30 +00002175 }
Fred Drakecde79132001-04-25 16:01:30 +00002176 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002177}
2178
Tim Peters0c322792002-07-17 16:49:03 +00002179static struct HandlerInfo handler_info[] = {
Fred Drake71b63ff2002-06-28 22:29:01 +00002180 {"StartElementHandler",
2181 (xmlhandlersetter)XML_SetStartElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002182 (xmlhandler)my_StartElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002183 {"EndElementHandler",
2184 (xmlhandlersetter)XML_SetEndElementHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002185 (xmlhandler)my_EndElementHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002186 {"ProcessingInstructionHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002187 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2188 (xmlhandler)my_ProcessingInstructionHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002189 {"CharacterDataHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002190 (xmlhandlersetter)XML_SetCharacterDataHandler,
2191 (xmlhandler)my_CharacterDataHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002192 {"UnparsedEntityDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002193 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002194 (xmlhandler)my_UnparsedEntityDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002195 {"NotationDeclHandler",
Fred Drake0582df92000-07-12 04:49:00 +00002196 (xmlhandlersetter)XML_SetNotationDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002197 (xmlhandler)my_NotationDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002198 {"StartNamespaceDeclHandler",
2199 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002200 (xmlhandler)my_StartNamespaceDeclHandler},
Fred Drake71b63ff2002-06-28 22:29:01 +00002201 {"EndNamespaceDeclHandler",
2202 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002203 (xmlhandler)my_EndNamespaceDeclHandler},
Fred Drake0582df92000-07-12 04:49:00 +00002204 {"CommentHandler",
2205 (xmlhandlersetter)XML_SetCommentHandler,
2206 (xmlhandler)my_CommentHandler},
2207 {"StartCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002208 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002209 (xmlhandler)my_StartCdataSectionHandler},
2210 {"EndCdataSectionHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002211 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
Fred Drake0582df92000-07-12 04:49:00 +00002212 (xmlhandler)my_EndCdataSectionHandler},
2213 {"DefaultHandler",
2214 (xmlhandlersetter)XML_SetDefaultHandler,
2215 (xmlhandler)my_DefaultHandler},
2216 {"DefaultHandlerExpand",
2217 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2218 (xmlhandler)my_DefaultHandlerExpandHandler},
2219 {"NotStandaloneHandler",
2220 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2221 (xmlhandler)my_NotStandaloneHandler},
2222 {"ExternalEntityRefHandler",
2223 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
Fred Drake2a3d7db2002-06-28 22:56:48 +00002224 (xmlhandler)my_ExternalEntityRefHandler},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002225 {"StartDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002226 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002227 (xmlhandler)my_StartDoctypeDeclHandler},
2228 {"EndDoctypeDeclHandler",
Fred Drake71b63ff2002-06-28 22:29:01 +00002229 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002230 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00002231 {"EntityDeclHandler",
2232 (xmlhandlersetter)XML_SetEntityDeclHandler,
2233 (xmlhandler)my_EntityDeclHandler},
2234 {"XmlDeclHandler",
2235 (xmlhandlersetter)XML_SetXmlDeclHandler,
2236 (xmlhandler)my_XmlDeclHandler},
2237 {"ElementDeclHandler",
2238 (xmlhandlersetter)XML_SetElementDeclHandler,
2239 (xmlhandler)my_ElementDeclHandler},
2240 {"AttlistDeclHandler",
2241 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2242 (xmlhandler)my_AttlistDeclHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002243#if XML_COMBINED_VERSION >= 19504
Martin v. Löwis069dde22003-01-21 10:58:18 +00002244 {"SkippedEntityHandler",
2245 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2246 (xmlhandler)my_SkippedEntityHandler},
Martin v. Löwisc847f402003-01-21 11:09:21 +00002247#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002248
Fred Drake0582df92000-07-12 04:49:00 +00002249 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00002250};
Brett Cannond0aeda82014-08-22 14:23:20 -04002251
2252/*[clinic input]
2253dump buffer
2254[clinic start generated code]*/
2255
2256#ifndef PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
2257 #define PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
2258#endif /* !defined(PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF) */
2259/*[clinic end generated code: output=a7880cb78bbd58ce input=524ce2e021e4eba6]*/