Martin v. Löwis | 7090ed1 | 2001-09-19 10:37:50 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Fred Drake | 4113b13 | 2001-03-24 19:58:26 +0000 | [diff] [blame] | 2 | #include <ctype.h> |
| 3 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Fred Drake | a77254a | 2000-09-29 19:23:29 +0000 | [diff] [blame] | 5 | #include "expat.h" |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 6 | |
Fredrik Lundh | c334504 | 2005-12-13 19:49:55 +0000 | [diff] [blame] | 7 | #include "pyexpat.h" |
| 8 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 9 | /* Do not emit Clinic output to a file as that wreaks havoc with conditionally |
| 10 | included methods. */ |
| 11 | /*[clinic input] |
| 12 | module pyexpat |
| 13 | [clinic start generated code]*/ |
| 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b168d503a4490c15]*/ |
| 15 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 16 | #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION) |
| 17 | |
Christian Heimes | fa535f5 | 2013-07-07 17:35:11 +0200 | [diff] [blame] | 18 | static XML_Memory_Handling_Suite ExpatMemoryHandler = { |
| 19 | PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
| 20 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 21 | enum 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öwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 36 | ExternalEntityRef, |
| 37 | StartDoctypeDecl, |
| 38 | EndDoctypeDecl, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 39 | EntityDecl, |
| 40 | XmlDecl, |
| 41 | ElementDecl, |
| 42 | AttlistDecl, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 43 | #if XML_COMBINED_VERSION >= 19504 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 44 | SkippedEntity, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 45 | #endif |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 46 | _DummyDecl |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static PyObject *ErrorObject; |
| 50 | |
| 51 | /* ----------------------------------------------------- */ |
| 52 | |
| 53 | /* Declarations for objects of type xmlparser */ |
| 54 | |
| 55 | typedef struct { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 56 | PyObject_HEAD |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 57 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 58 | XML_Parser itself; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 59 | int ordered_attributes; /* Return attributes as a list. */ |
| 60 | int specified_attributes; /* Report only specified attributes. */ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 61 | int in_callback; /* Is a callback active? */ |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 62 | int ns_prefixes; /* Namespace-triplets mode? */ |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 63 | 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 Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 67 | PyObject *intern; /* Dictionary to intern strings */ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 68 | PyObject **handlers; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 69 | } xmlparseobject; |
| 70 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 71 | #include "clinic/pyexpat.c.h" |
| 72 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 73 | #define CHARACTER_DATA_BUFFER_SIZE 8192 |
| 74 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 75 | static PyTypeObject Xmlparsetype; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 76 | |
Fred Drake | 117ac85 | 2002-09-24 16:24:54 +0000 | [diff] [blame] | 77 | typedef void (*xmlhandlersetter)(XML_Parser self, void *meth); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 78 | typedef void* xmlhandler; |
| 79 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 80 | struct HandlerInfo { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 81 | const char *name; |
| 82 | xmlhandlersetter setter; |
| 83 | xmlhandler handler; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 84 | PyCodeObject *tb_code; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 85 | PyObject *nameobj; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 88 | static struct HandlerInfo handler_info[64]; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 89 | |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 90 | /* Set an integer attribute on the error object; return true on success, |
| 91 | * false on an exception. |
| 92 | */ |
| 93 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 94 | set_error_attr(PyObject *err, const char *name, int value) |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 95 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 96 | PyObject *v = PyLong_FromLong(value); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 97 | |
Neal Norwitz | 2f5e990 | 2006-03-08 06:36:45 +0000 | [diff] [blame] | 98 | if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) { |
| 99 | Py_XDECREF(v); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Michael W. Hudson | 0bb8454 | 2004-08-03 11:31:31 +0000 | [diff] [blame] | 102 | Py_DECREF(v); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | /* Build and set an Expat exception, including positioning |
| 107 | * information. Always returns NULL. |
| 108 | */ |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 109 | static PyObject * |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 110 | set_error(xmlparseobject *self, enum XML_Error code) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 111 | { |
| 112 | PyObject *err; |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 113 | PyObject *buffer; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 114 | XML_Parser parser = self->itself; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 115 | int lineno = XML_GetErrorLineNumber(parser); |
| 116 | int column = XML_GetErrorColumnNumber(parser); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 117 | |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 118 | buffer = PyUnicode_FromFormat("%s: line %i, column %i", |
| 119 | XML_ErrorString(code), lineno, column); |
| 120 | if (buffer == NULL) |
| 121 | return NULL; |
| 122 | err = PyObject_CallFunction(ErrorObject, "O", buffer); |
| 123 | Py_DECREF(buffer); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 124 | if ( err != NULL |
| 125 | && set_error_attr(err, "code", code) |
| 126 | && set_error_attr(err, "offset", column) |
| 127 | && set_error_attr(err, "lineno", lineno)) { |
| 128 | PyErr_SetObject(ErrorObject, err); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 129 | } |
Neal Norwitz | 2f5e990 | 2006-03-08 06:36:45 +0000 | [diff] [blame] | 130 | Py_XDECREF(err); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 134 | static int |
| 135 | have_handler(xmlparseobject *self, int type) |
| 136 | { |
| 137 | PyObject *handler = self->handlers[type]; |
| 138 | return handler != NULL; |
| 139 | } |
| 140 | |
| 141 | static PyObject * |
| 142 | get_handler_name(struct HandlerInfo *hinfo) |
| 143 | { |
| 144 | PyObject *name = hinfo->nameobj; |
| 145 | if (name == NULL) { |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 146 | name = PyUnicode_FromString(hinfo->name); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 147 | hinfo->nameobj = name; |
| 148 | } |
| 149 | Py_XINCREF(name); |
| 150 | return name; |
| 151 | } |
| 152 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 153 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 154 | /* Convert a string of XML_Chars into a Unicode string. |
| 155 | Returns None if str is a null pointer. */ |
| 156 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 157 | static PyObject * |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 158 | conv_string_to_unicode(const XML_Char *str) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 159 | { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 160 | /* XXX currently this code assumes that XML_Char is 8-bit, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 161 | and hence in UTF-8. */ |
| 162 | /* UTF-8 from Expat, Unicode desired */ |
| 163 | if (str == NULL) { |
| 164 | Py_INCREF(Py_None); |
| 165 | return Py_None; |
| 166 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 167 | return PyUnicode_DecodeUTF8(str, strlen(str), "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 170 | static PyObject * |
| 171 | conv_string_len_to_unicode(const XML_Char *str, int len) |
| 172 | { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 173 | /* XXX currently this code assumes that XML_Char is 8-bit, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 174 | and hence in UTF-8. */ |
| 175 | /* UTF-8 from Expat, Unicode desired */ |
| 176 | if (str == NULL) { |
| 177 | Py_INCREF(Py_None); |
| 178 | return Py_None; |
| 179 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 180 | return PyUnicode_DecodeUTF8((const char *)str, len, "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 181 | } |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 182 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 183 | /* Callback routines */ |
| 184 | |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 185 | static void clear_handlers(xmlparseobject *self, int initial); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 187 | /* This handler is used when an error has been detected, in the hope |
| 188 | that actual parsing can be terminated early. This will only help |
| 189 | if an external entity reference is encountered. */ |
| 190 | static int |
| 191 | error_external_entity_ref_handler(XML_Parser parser, |
| 192 | const XML_Char *context, |
| 193 | const XML_Char *base, |
| 194 | const XML_Char *systemId, |
| 195 | const XML_Char *publicId) |
| 196 | { |
| 197 | return 0; |
| 198 | } |
| 199 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 200 | /* Dummy character data handler used when an error (exception) has |
| 201 | been detected, and the actual parsing can be terminated early. |
| 202 | This is needed since character data handler can't be safely removed |
| 203 | from within the character data handler, but can be replaced. It is |
| 204 | used only from the character data handler trampoline, and must be |
| 205 | used right after `flag_error()` is called. */ |
| 206 | static void |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | noop_character_data_handler(void *userData, const XML_Char *data, int len) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 208 | { |
| 209 | /* Do nothing. */ |
| 210 | } |
| 211 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 212 | static void |
| 213 | flag_error(xmlparseobject *self) |
| 214 | { |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 215 | clear_handlers(self, 0); |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 216 | XML_SetExternalEntityRefHandler(self->itself, |
| 217 | error_external_entity_ref_handler); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 220 | static PyObject* |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 221 | call_with_frame(const char *funcname, int lineno, PyObject* func, PyObject* args, |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 222 | xmlparseobject *self) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 223 | { |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 224 | PyObject *res; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 225 | |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 226 | res = PyEval_CallObject(func, args); |
Jeremy Hylton | 9263f57 | 2003-06-27 16:13:17 +0000 | [diff] [blame] | 227 | if (res == NULL) { |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 228 | _PyTraceback_Add(funcname, __FILE__, lineno); |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 229 | XML_StopParser(self->itself, XML_FALSE); |
Jeremy Hylton | 9263f57 | 2003-06-27 16:13:17 +0000 | [diff] [blame] | 230 | } |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 231 | return res; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 234 | static PyObject* |
| 235 | string_intern(xmlparseobject *self, const char* str) |
| 236 | { |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 237 | PyObject *result = conv_string_to_unicode(str); |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 238 | PyObject *value; |
Neal Norwitz | 484d9a4 | 2005-09-30 04:46:49 +0000 | [diff] [blame] | 239 | /* result can be NULL if the unicode conversion failed. */ |
| 240 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | return result; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 242 | if (!self->intern) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | return result; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 244 | value = PyDict_GetItem(self->intern, result); |
| 245 | if (!value) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (PyDict_SetItem(self->intern, result, result) == 0) |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 247 | return result; |
| 248 | else |
| 249 | return NULL; |
| 250 | } |
| 251 | Py_INCREF(value); |
| 252 | Py_DECREF(result); |
| 253 | return value; |
| 254 | } |
| 255 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 256 | /* Return 0 on success, -1 on exception. |
| 257 | * flag_error() will be called before return if needed. |
| 258 | */ |
| 259 | static int |
| 260 | call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len) |
| 261 | { |
| 262 | PyObject *args; |
| 263 | PyObject *temp; |
| 264 | |
Georg Brandl | c01537f | 2010-10-15 16:26:08 +0000 | [diff] [blame] | 265 | if (!have_handler(self, CharacterData)) |
| 266 | return -1; |
| 267 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 268 | args = PyTuple_New(1); |
| 269 | if (args == NULL) |
| 270 | return -1; |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 271 | temp = (conv_string_len_to_unicode(buffer, len)); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 272 | if (temp == NULL) { |
| 273 | Py_DECREF(args); |
| 274 | flag_error(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 275 | XML_SetCharacterDataHandler(self->itself, |
| 276 | noop_character_data_handler); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | PyTuple_SET_ITEM(args, 0, temp); |
| 280 | /* temp is now a borrowed reference; consider it unused. */ |
| 281 | self->in_callback = 1; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 282 | temp = call_with_frame("CharacterData", __LINE__, |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 283 | self->handlers[CharacterData], args, self); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 284 | /* temp is an owned reference again, or NULL */ |
| 285 | self->in_callback = 0; |
| 286 | Py_DECREF(args); |
| 287 | if (temp == NULL) { |
| 288 | flag_error(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 289 | XML_SetCharacterDataHandler(self->itself, |
| 290 | noop_character_data_handler); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | Py_DECREF(temp); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | flush_character_buffer(xmlparseobject *self) |
| 299 | { |
| 300 | int rc; |
| 301 | if (self->buffer == NULL || self->buffer_used == 0) |
| 302 | return 0; |
| 303 | rc = call_character_handler(self, self->buffer, self->buffer_used); |
| 304 | self->buffer_used = 0; |
| 305 | return rc; |
| 306 | } |
| 307 | |
| 308 | static void |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | my_CharacterDataHandler(void *userData, const XML_Char *data, int len) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 310 | { |
| 311 | xmlparseobject *self = (xmlparseobject *) userData; |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 312 | |
| 313 | if (PyErr_Occurred()) |
| 314 | return; |
| 315 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 316 | if (self->buffer == NULL) |
| 317 | call_character_handler(self, data, len); |
| 318 | else { |
| 319 | if ((self->buffer_used + len) > self->buffer_size) { |
| 320 | if (flush_character_buffer(self) < 0) |
| 321 | return; |
| 322 | /* handler might have changed; drop the rest on the floor |
| 323 | * if there isn't a handler anymore |
| 324 | */ |
| 325 | if (!have_handler(self, CharacterData)) |
| 326 | return; |
| 327 | } |
| 328 | if (len > self->buffer_size) { |
| 329 | call_character_handler(self, data, len); |
| 330 | self->buffer_used = 0; |
| 331 | } |
| 332 | else { |
| 333 | memcpy(self->buffer + self->buffer_used, |
| 334 | data, len * sizeof(XML_Char)); |
| 335 | self->buffer_used += len; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 340 | static void |
| 341 | my_StartElementHandler(void *userData, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 342 | const XML_Char *name, const XML_Char *atts[]) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 343 | { |
| 344 | xmlparseobject *self = (xmlparseobject *)userData; |
| 345 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 346 | if (have_handler(self, StartElement)) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 347 | PyObject *container, *rv, *args; |
| 348 | int i, max; |
| 349 | |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 350 | if (PyErr_Occurred()) |
| 351 | return; |
| 352 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 353 | if (flush_character_buffer(self) < 0) |
| 354 | return; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 355 | /* Set max to the number of slots filled in atts[]; max/2 is |
| 356 | * the number of attributes we need to process. |
| 357 | */ |
| 358 | if (self->specified_attributes) { |
| 359 | max = XML_GetSpecifiedAttributeCount(self->itself); |
| 360 | } |
| 361 | else { |
| 362 | max = 0; |
| 363 | while (atts[max] != NULL) |
| 364 | max += 2; |
| 365 | } |
| 366 | /* Build the container. */ |
| 367 | if (self->ordered_attributes) |
| 368 | container = PyList_New(max); |
| 369 | else |
| 370 | container = PyDict_New(); |
| 371 | if (container == NULL) { |
| 372 | flag_error(self); |
| 373 | return; |
| 374 | } |
| 375 | for (i = 0; i < max; i += 2) { |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 376 | PyObject *n = string_intern(self, (XML_Char *) atts[i]); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 377 | PyObject *v; |
| 378 | if (n == NULL) { |
| 379 | flag_error(self); |
| 380 | Py_DECREF(container); |
| 381 | return; |
| 382 | } |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 383 | v = conv_string_to_unicode((XML_Char *) atts[i+1]); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 384 | if (v == NULL) { |
| 385 | flag_error(self); |
| 386 | Py_DECREF(container); |
| 387 | Py_DECREF(n); |
| 388 | return; |
| 389 | } |
| 390 | if (self->ordered_attributes) { |
| 391 | PyList_SET_ITEM(container, i, n); |
| 392 | PyList_SET_ITEM(container, i+1, v); |
| 393 | } |
| 394 | else if (PyDict_SetItem(container, n, v)) { |
| 395 | flag_error(self); |
| 396 | Py_DECREF(n); |
| 397 | Py_DECREF(v); |
| 398 | return; |
| 399 | } |
| 400 | else { |
| 401 | Py_DECREF(n); |
| 402 | Py_DECREF(v); |
| 403 | } |
| 404 | } |
Neal Norwitz | 484d9a4 | 2005-09-30 04:46:49 +0000 | [diff] [blame] | 405 | args = string_intern(self, name); |
| 406 | if (args != NULL) |
| 407 | args = Py_BuildValue("(NN)", args, container); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 408 | if (args == NULL) { |
| 409 | Py_DECREF(container); |
| 410 | return; |
| 411 | } |
| 412 | /* Container is now a borrowed reference; ignore it. */ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 413 | self->in_callback = 1; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 414 | rv = call_with_frame("StartElement", __LINE__, |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 415 | self->handlers[StartElement], args, self); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 416 | self->in_callback = 0; |
| 417 | Py_DECREF(args); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 418 | if (rv == NULL) { |
| 419 | flag_error(self); |
| 420 | return; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 421 | } |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 422 | Py_DECREF(rv); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \ |
| 427 | RETURN, GETUSERDATA) \ |
| 428 | static RC \ |
| 429 | my_##NAME##Handler PARAMS {\ |
| 430 | xmlparseobject *self = GETUSERDATA ; \ |
| 431 | PyObject *args = NULL; \ |
| 432 | PyObject *rv = NULL; \ |
| 433 | INIT \ |
| 434 | \ |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 435 | if (have_handler(self, NAME)) { \ |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 436 | if (PyErr_Occurred()) \ |
| 437 | return RETURN; \ |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 438 | if (flush_character_buffer(self) < 0) \ |
| 439 | return RETURN; \ |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 440 | args = Py_BuildValue PARAM_FORMAT ;\ |
Martin v. Löwis | 1d7c55f | 2001-11-10 13:57:55 +0000 | [diff] [blame] | 441 | if (!args) { flag_error(self); return RETURN;} \ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 442 | self->in_callback = 1; \ |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 443 | rv = call_with_frame(#NAME,__LINE__, \ |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 444 | self->handlers[NAME], args, self); \ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 445 | self->in_callback = 0; \ |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 446 | Py_DECREF(args); \ |
| 447 | if (rv == NULL) { \ |
| 448 | flag_error(self); \ |
| 449 | return RETURN; \ |
| 450 | } \ |
| 451 | CONVERSION \ |
| 452 | Py_DECREF(rv); \ |
| 453 | } \ |
| 454 | return RETURN; \ |
| 455 | } |
| 456 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 457 | #define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\ |
| 459 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 460 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 461 | #define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \ |
| 463 | rc = PyLong_AsLong(rv);, rc, \ |
| 464 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 465 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 466 | VOID_HANDLER(EndElement, |
| 467 | (void *userData, const XML_Char *name), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 468 | ("(N)", string_intern(self, name))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 469 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 470 | VOID_HANDLER(ProcessingInstruction, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 471 | (void *userData, |
| 472 | const XML_Char *target, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 473 | const XML_Char *data), |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 474 | ("(NO&)", string_intern(self, target), conv_string_to_unicode ,data)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 475 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 476 | VOID_HANDLER(UnparsedEntityDecl, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 477 | (void *userData, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 478 | const XML_Char *entityName, |
| 479 | const XML_Char *base, |
| 480 | const XML_Char *systemId, |
| 481 | const XML_Char *publicId, |
| 482 | const XML_Char *notationName), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 483 | ("(NNNNN)", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 484 | string_intern(self, entityName), string_intern(self, base), |
| 485 | string_intern(self, systemId), string_intern(self, publicId), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 486 | string_intern(self, notationName))) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 487 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 488 | VOID_HANDLER(EntityDecl, |
| 489 | (void *userData, |
| 490 | const XML_Char *entityName, |
| 491 | int is_parameter_entity, |
| 492 | const XML_Char *value, |
| 493 | int value_length, |
| 494 | const XML_Char *base, |
| 495 | const XML_Char *systemId, |
| 496 | const XML_Char *publicId, |
| 497 | const XML_Char *notationName), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 498 | ("NiNNNNN", |
| 499 | string_intern(self, entityName), is_parameter_entity, |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 500 | (conv_string_len_to_unicode(value, value_length)), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 501 | string_intern(self, base), string_intern(self, systemId), |
| 502 | string_intern(self, publicId), |
| 503 | string_intern(self, notationName))) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 504 | |
| 505 | VOID_HANDLER(XmlDecl, |
| 506 | (void *userData, |
| 507 | const XML_Char *version, |
| 508 | const XML_Char *encoding, |
| 509 | int standalone), |
| 510 | ("(O&O&i)", |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 511 | conv_string_to_unicode ,version, conv_string_to_unicode ,encoding, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 512 | standalone)) |
| 513 | |
| 514 | static PyObject * |
| 515 | conv_content_model(XML_Content * const model, |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 516 | PyObject *(*conv_string)(const XML_Char *)) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 517 | { |
| 518 | PyObject *result = NULL; |
| 519 | PyObject *children = PyTuple_New(model->numchildren); |
| 520 | int i; |
| 521 | |
| 522 | if (children != NULL) { |
Tim Peters | 9544fc5 | 2001-07-28 09:36:36 +0000 | [diff] [blame] | 523 | assert(model->numchildren < INT_MAX); |
| 524 | for (i = 0; i < (int)model->numchildren; ++i) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 525 | PyObject *child = conv_content_model(&model->children[i], |
| 526 | conv_string); |
| 527 | if (child == NULL) { |
| 528 | Py_XDECREF(children); |
| 529 | return NULL; |
| 530 | } |
| 531 | PyTuple_SET_ITEM(children, i, child); |
| 532 | } |
| 533 | result = Py_BuildValue("(iiO&N)", |
| 534 | model->type, model->quant, |
| 535 | conv_string,model->name, children); |
| 536 | } |
| 537 | return result; |
| 538 | } |
| 539 | |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 540 | static void |
| 541 | my_ElementDeclHandler(void *userData, |
| 542 | const XML_Char *name, |
| 543 | XML_Content *model) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 544 | { |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 545 | xmlparseobject *self = (xmlparseobject *)userData; |
| 546 | PyObject *args = NULL; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 547 | |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 548 | if (have_handler(self, ElementDecl)) { |
| 549 | PyObject *rv = NULL; |
| 550 | PyObject *modelobj, *nameobj; |
| 551 | |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 552 | if (PyErr_Occurred()) |
| 553 | return; |
| 554 | |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 555 | if (flush_character_buffer(self) < 0) |
| 556 | goto finally; |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 557 | modelobj = conv_content_model(model, (conv_string_to_unicode)); |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 558 | if (modelobj == NULL) { |
| 559 | flag_error(self); |
| 560 | goto finally; |
| 561 | } |
| 562 | nameobj = string_intern(self, name); |
| 563 | if (nameobj == NULL) { |
| 564 | Py_DECREF(modelobj); |
| 565 | flag_error(self); |
| 566 | goto finally; |
| 567 | } |
Michael W. Hudson | 0bb8454 | 2004-08-03 11:31:31 +0000 | [diff] [blame] | 568 | args = Py_BuildValue("NN", nameobj, modelobj); |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 569 | if (args == NULL) { |
| 570 | Py_DECREF(modelobj); |
| 571 | flag_error(self); |
| 572 | goto finally; |
| 573 | } |
| 574 | self->in_callback = 1; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 575 | rv = call_with_frame("ElementDecl", __LINE__, |
Fred Drake | 39689c5 | 2004-08-13 03:12:57 +0000 | [diff] [blame] | 576 | self->handlers[ElementDecl], args, self); |
Fred Drake | 06dd8cf | 2003-02-02 03:54:17 +0000 | [diff] [blame] | 577 | self->in_callback = 0; |
| 578 | if (rv == NULL) { |
| 579 | flag_error(self); |
| 580 | goto finally; |
| 581 | } |
| 582 | Py_DECREF(rv); |
| 583 | } |
| 584 | finally: |
| 585 | Py_XDECREF(args); |
| 586 | XML_FreeContentModel(self->itself, model); |
| 587 | return; |
| 588 | } |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 589 | |
| 590 | VOID_HANDLER(AttlistDecl, |
| 591 | (void *userData, |
| 592 | const XML_Char *elname, |
| 593 | const XML_Char *attname, |
| 594 | const XML_Char *att_type, |
| 595 | const XML_Char *dflt, |
| 596 | int isrequired), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 597 | ("(NNO&O&i)", |
| 598 | string_intern(self, elname), string_intern(self, attname), |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 599 | conv_string_to_unicode ,att_type, conv_string_to_unicode ,dflt, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 600 | isrequired)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 601 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 602 | #if XML_COMBINED_VERSION >= 19504 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 603 | VOID_HANDLER(SkippedEntity, |
| 604 | (void *userData, |
| 605 | const XML_Char *entityName, |
| 606 | int is_parameter_entity), |
| 607 | ("Ni", |
| 608 | string_intern(self, entityName), is_parameter_entity)) |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 609 | #endif |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 610 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 611 | VOID_HANDLER(NotationDecl, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | (void *userData, |
| 613 | const XML_Char *notationName, |
| 614 | const XML_Char *base, |
| 615 | const XML_Char *systemId, |
| 616 | const XML_Char *publicId), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 617 | ("(NNNN)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | string_intern(self, notationName), string_intern(self, base), |
| 619 | string_intern(self, systemId), string_intern(self, publicId))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 620 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 621 | VOID_HANDLER(StartNamespaceDecl, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | (void *userData, |
| 623 | const XML_Char *prefix, |
| 624 | const XML_Char *uri), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 625 | ("(NN)", |
| 626 | string_intern(self, prefix), string_intern(self, uri))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 627 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 628 | VOID_HANDLER(EndNamespaceDecl, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | (void *userData, |
| 630 | const XML_Char *prefix), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 631 | ("(N)", string_intern(self, prefix))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 632 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 633 | VOID_HANDLER(Comment, |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 634 | (void *userData, const XML_Char *data), |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 635 | ("(O&)", conv_string_to_unicode ,data)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 636 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 637 | VOID_HANDLER(StartCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 638 | (void *userData), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | ("()")) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 640 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 641 | VOID_HANDLER(EndCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 642 | (void *userData), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 644 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 645 | VOID_HANDLER(Default, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | (void *userData, const XML_Char *s, int len), |
| 647 | ("(N)", (conv_string_len_to_unicode(s,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 648 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 649 | VOID_HANDLER(DefaultHandlerExpand, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | (void *userData, const XML_Char *s, int len), |
| 651 | ("(N)", (conv_string_len_to_unicode(s,len)))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 652 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 653 | INT_HANDLER(NotStandalone, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | (void *userData), |
| 655 | ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 656 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 657 | RC_HANDLER(int, ExternalEntityRef, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | (XML_Parser parser, |
| 659 | const XML_Char *context, |
| 660 | const XML_Char *base, |
| 661 | const XML_Char *systemId, |
| 662 | const XML_Char *publicId), |
| 663 | int rc=0;, |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 664 | ("(O&NNN)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | conv_string_to_unicode ,context, string_intern(self, base), |
| 666 | string_intern(self, systemId), string_intern(self, publicId)), |
| 667 | rc = PyLong_AsLong(rv);, rc, |
| 668 | XML_GetUserData(parser)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 669 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 670 | /* XXX UnknownEncodingHandler */ |
| 671 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 672 | VOID_HANDLER(StartDoctypeDecl, |
| 673 | (void *userData, const XML_Char *doctypeName, |
| 674 | const XML_Char *sysid, const XML_Char *pubid, |
| 675 | int has_internal_subset), |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 676 | ("(NNNi)", string_intern(self, doctypeName), |
| 677 | string_intern(self, sysid), string_intern(self, pubid), |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 678 | has_internal_subset)) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 679 | |
| 680 | VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 681 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 682 | /* ---------------------------------------------------------------- */ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 683 | /*[clinic input] |
| 684 | class pyexpat.xmlparser "xmlparseobject *" "&Xmlparsetype" |
| 685 | [clinic start generated code]*/ |
| 686 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2393162385232e1c]*/ |
| 687 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 688 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 689 | static PyObject * |
| 690 | get_parse_result(xmlparseobject *self, int rv) |
| 691 | { |
| 692 | if (PyErr_Occurred()) { |
| 693 | return NULL; |
| 694 | } |
| 695 | if (rv == 0) { |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 696 | return set_error(self, XML_GetErrorCode(self->itself)); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 697 | } |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 698 | if (flush_character_buffer(self) < 0) { |
| 699 | return NULL; |
| 700 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 701 | return PyLong_FromLong(rv); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 704 | #define MAX_CHUNK_SIZE (1 << 20) |
| 705 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 706 | /*[clinic input] |
| 707 | pyexpat.xmlparser.Parse |
| 708 | |
| 709 | data: object |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 710 | isfinal: int(c_default="0") = False |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 711 | / |
| 712 | |
| 713 | Parse XML data. |
| 714 | |
| 715 | `isfinal' should be true at end of input. |
| 716 | [clinic start generated code]*/ |
| 717 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 718 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 719 | pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 720 | int isfinal) |
| 721 | /*[clinic end generated code: output=f4db843dd1f4ed4b input=199d9e8e92ebbb4b]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 722 | { |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 723 | const char *s; |
| 724 | Py_ssize_t slen; |
| 725 | Py_buffer view; |
| 726 | int rc; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 727 | |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 728 | if (PyUnicode_Check(data)) { |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 729 | view.buf = NULL; |
Serhiy Storchaka | 36b365c | 2013-02-04 18:28:01 +0200 | [diff] [blame] | 730 | s = PyUnicode_AsUTF8AndSize(data, &slen); |
| 731 | if (s == NULL) |
| 732 | return NULL; |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 733 | /* Explicitly set UTF-8 encoding. Return code ignored. */ |
| 734 | (void)XML_SetEncoding(self->itself, "utf-8"); |
| 735 | } |
| 736 | else { |
| 737 | if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0) |
| 738 | return NULL; |
| 739 | s = view.buf; |
| 740 | slen = view.len; |
| 741 | } |
| 742 | |
| 743 | while (slen > MAX_CHUNK_SIZE) { |
| 744 | rc = XML_Parse(self->itself, s, MAX_CHUNK_SIZE, 0); |
| 745 | if (!rc) |
| 746 | goto done; |
| 747 | s += MAX_CHUNK_SIZE; |
| 748 | slen -= MAX_CHUNK_SIZE; |
| 749 | } |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 750 | Py_BUILD_ASSERT(MAX_CHUNK_SIZE <= INT_MAX); |
| 751 | assert(slen <= INT_MAX); |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 752 | rc = XML_Parse(self->itself, s, (int)slen, isfinal); |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 753 | |
| 754 | done: |
| 755 | if (view.buf != NULL) |
| 756 | PyBuffer_Release(&view); |
| 757 | return get_parse_result(self, rc); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 760 | /* File reading copied from cPickle */ |
| 761 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 762 | #define BUF_SIZE 2048 |
| 763 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 764 | static int |
| 765 | readinst(char *buf, int buf_size, PyObject *meth) |
| 766 | { |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 767 | PyObject *str; |
| 768 | Py_ssize_t len; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 769 | const char *ptr; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 770 | |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 771 | str = PyObject_CallFunction(meth, "n", buf_size); |
Martin v. Löwis | 9171f02 | 2004-10-13 19:50:11 +0000 | [diff] [blame] | 772 | if (str == NULL) |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 773 | goto error; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 774 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 775 | if (PyBytes_Check(str)) |
| 776 | ptr = PyBytes_AS_STRING(str); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 777 | else if (PyByteArray_Check(str)) |
| 778 | ptr = PyByteArray_AS_STRING(str); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 779 | else { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 780 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 781 | "read() did not return a bytes object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 782 | Py_TYPE(str)->tp_name); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 783 | goto error; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 784 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 785 | len = Py_SIZE(str); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 786 | if (len > buf_size) { |
| 787 | PyErr_Format(PyExc_ValueError, |
| 788 | "read() returned too much data: " |
Victor Stinner | 9d6f936 | 2011-01-04 22:00:04 +0000 | [diff] [blame] | 789 | "%i bytes requested, %zd returned", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 790 | buf_size, len); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 791 | goto error; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 792 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 793 | memcpy(buf, ptr, len); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 794 | Py_DECREF(str); |
| 795 | /* len <= buf_size <= INT_MAX */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 796 | return (int)len; |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 797 | |
| 798 | error: |
| 799 | Py_XDECREF(str); |
| 800 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 803 | /*[clinic input] |
| 804 | pyexpat.xmlparser.ParseFile |
| 805 | |
| 806 | file: object |
| 807 | / |
| 808 | |
| 809 | Parse XML data from file-like object. |
| 810 | [clinic start generated code]*/ |
| 811 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 812 | static PyObject * |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 813 | pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 814 | /*[clinic end generated code: output=2adc6a13100cc42b input=fbb5a12b6038d735]*/ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 815 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 816 | int rv = 1; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 817 | PyObject *readmethod = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 818 | _Py_IDENTIFIER(read); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 819 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 820 | readmethod = _PyObject_GetAttrId(file, &PyId_read); |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 821 | if (readmethod == NULL) { |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 822 | PyErr_SetString(PyExc_TypeError, |
| 823 | "argument must have 'read' attribute"); |
| 824 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 825 | } |
| 826 | for (;;) { |
| 827 | int bytes_read; |
| 828 | void *buf = XML_GetBuffer(self->itself, BUF_SIZE); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 829 | if (buf == NULL) { |
Fred Drake | f239c6d | 2003-07-21 17:22:43 +0000 | [diff] [blame] | 830 | Py_XDECREF(readmethod); |
Ned Deily | e7d532f | 2014-03-27 16:39:58 -0700 | [diff] [blame] | 831 | return get_parse_result(self, 0); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 832 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 833 | |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 834 | bytes_read = readinst(buf, BUF_SIZE, readmethod); |
| 835 | if (bytes_read < 0) { |
| 836 | Py_DECREF(readmethod); |
| 837 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 838 | } |
| 839 | rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 840 | if (PyErr_Occurred()) { |
| 841 | Py_XDECREF(readmethod); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 842 | return NULL; |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 843 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 844 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 845 | if (!rv || bytes_read == 0) |
| 846 | break; |
| 847 | } |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 848 | Py_XDECREF(readmethod); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 849 | return get_parse_result(self, rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 852 | /*[clinic input] |
| 853 | pyexpat.xmlparser.SetBase |
| 854 | |
| 855 | base: str |
| 856 | / |
| 857 | |
| 858 | Set the base URL for the parser. |
| 859 | [clinic start generated code]*/ |
| 860 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 861 | static PyObject * |
| 862 | pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 863 | /*[clinic end generated code: output=c212ddceb607b539 input=c684e5de895ee1a8]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 864 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 865 | if (!XML_SetBase(self->itself, base)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 867 | } |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 868 | Py_RETURN_NONE; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 871 | /*[clinic input] |
| 872 | pyexpat.xmlparser.GetBase |
| 873 | |
| 874 | Return base URL string for the parser. |
| 875 | [clinic start generated code]*/ |
| 876 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 877 | static PyObject * |
| 878 | pyexpat_xmlparser_GetBase_impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 879 | /*[clinic end generated code: output=2886cb21f9a8739a input=918d71c38009620e]*/ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 880 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 881 | return Py_BuildValue("z", XML_GetBase(self->itself)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 884 | /*[clinic input] |
| 885 | pyexpat.xmlparser.GetInputContext |
| 886 | |
| 887 | Return the untranslated text of the input that caused the current event. |
| 888 | |
| 889 | If the event was generated by a large amount of text (such as a start tag |
| 890 | for an element with many attributes), not all of the text may be available. |
| 891 | [clinic start generated code]*/ |
| 892 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 893 | static PyObject * |
| 894 | pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 895 | /*[clinic end generated code: output=a88026d683fc22cc input=034df8712db68379]*/ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 896 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 897 | if (self->in_callback) { |
| 898 | int offset, size; |
| 899 | const char *buffer |
| 900 | = XML_GetInputContext(self->itself, &offset, &size); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 901 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 902 | if (buffer != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 903 | return PyBytes_FromStringAndSize(buffer + offset, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 904 | size - offset); |
| 905 | else |
| 906 | Py_RETURN_NONE; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 907 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 908 | else |
| 909 | Py_RETURN_NONE; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 910 | } |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 911 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 912 | /*[clinic input] |
| 913 | pyexpat.xmlparser.ExternalEntityParserCreate |
| 914 | |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 915 | context: str(accept={str, NoneType}) |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 916 | encoding: str = NULL |
| 917 | / |
| 918 | |
| 919 | Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler. |
| 920 | [clinic start generated code]*/ |
| 921 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 922 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 923 | pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, |
| 924 | const char *context, |
| 925 | const char *encoding) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 926 | /*[clinic end generated code: output=535cda9d7a0fbcd6 input=b906714cc122c322]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 927 | { |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 928 | xmlparseobject *new_parser; |
| 929 | int i; |
| 930 | |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 931 | new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 932 | if (new_parser == NULL) |
| 933 | return NULL; |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 934 | new_parser->buffer_size = self->buffer_size; |
| 935 | new_parser->buffer_used = 0; |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 936 | new_parser->buffer = NULL; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 937 | new_parser->ordered_attributes = self->ordered_attributes; |
| 938 | new_parser->specified_attributes = self->specified_attributes; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 939 | new_parser->in_callback = 0; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 940 | new_parser->ns_prefixes = self->ns_prefixes; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 941 | new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | encoding); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 943 | new_parser->handlers = 0; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 944 | new_parser->intern = self->intern; |
| 945 | Py_XINCREF(new_parser->intern); |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 946 | PyObject_GC_Track(new_parser); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 947 | |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 948 | if (self->buffer != NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 949 | new_parser->buffer = PyMem_Malloc(new_parser->buffer_size); |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 950 | if (new_parser->buffer == NULL) { |
| 951 | Py_DECREF(new_parser); |
| 952 | return PyErr_NoMemory(); |
| 953 | } |
| 954 | } |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 955 | if (!new_parser->itself) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 956 | Py_DECREF(new_parser); |
| 957 | return PyErr_NoMemory(); |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | XML_SetUserData(new_parser->itself, (void *)new_parser); |
| 961 | |
| 962 | /* allocate and clear handlers first */ |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 963 | for (i = 0; handler_info[i].name != NULL; i++) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 964 | /* do nothing */; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 965 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 966 | new_parser->handlers = PyMem_New(PyObject *, i); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 967 | if (!new_parser->handlers) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 968 | Py_DECREF(new_parser); |
| 969 | return PyErr_NoMemory(); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 970 | } |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 971 | clear_handlers(new_parser, 1); |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 972 | |
| 973 | /* then copy handlers from self */ |
| 974 | for (i = 0; handler_info[i].name != NULL; i++) { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 975 | PyObject *handler = self->handlers[i]; |
| 976 | if (handler != NULL) { |
| 977 | Py_INCREF(handler); |
| 978 | new_parser->handlers[i] = handler; |
| 979 | handler_info[i].setter(new_parser->itself, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 980 | handler_info[i].handler); |
| 981 | } |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 982 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 983 | return (PyObject *)new_parser; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 986 | /*[clinic input] |
| 987 | pyexpat.xmlparser.SetParamEntityParsing |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 988 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 989 | flag: int |
| 990 | / |
| 991 | |
| 992 | Controls parsing of parameter entities (including the external DTD subset). |
| 993 | |
| 994 | Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER, |
| 995 | XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and |
| 996 | XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag |
| 997 | was successful. |
| 998 | [clinic start generated code]*/ |
| 999 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1000 | static PyObject * |
| 1001 | pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1002 | /*[clinic end generated code: output=18668ee8e760d64c input=8aea19b4b15e9af1]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1003 | { |
| 1004 | flag = XML_SetParamEntityParsing(self->itself, flag); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1005 | return PyLong_FromLong(flag); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1008 | |
| 1009 | #if XML_COMBINED_VERSION >= 19505 |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1010 | /*[clinic input] |
| 1011 | pyexpat.xmlparser.UseForeignDTD |
| 1012 | |
| 1013 | flag: bool = True |
| 1014 | / |
| 1015 | |
| 1016 | Allows the application to provide an artificial external subset if one is not specified as part of the document instance. |
| 1017 | |
| 1018 | This readily allows the use of a 'default' document type controlled by the |
| 1019 | application, while still getting the advantage of providing document type |
| 1020 | information to the parser. 'flag' defaults to True if not provided. |
| 1021 | [clinic start generated code]*/ |
| 1022 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1023 | static PyObject * |
| 1024 | pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1025 | /*[clinic end generated code: output=cfaa9aa50bb0f65c input=78144c519d116a6e]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1026 | { |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1027 | enum XML_Error rc; |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1028 | |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1029 | rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE); |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1030 | if (rc != XML_ERROR_NONE) { |
| 1031 | return set_error(self, rc); |
| 1032 | } |
| 1033 | Py_INCREF(Py_None); |
| 1034 | return Py_None; |
| 1035 | } |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1036 | #endif |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1037 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1038 | /*[clinic input] |
| 1039 | pyexpat.xmlparser.__dir__ |
| 1040 | [clinic start generated code]*/ |
| 1041 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1042 | static PyObject * |
| 1043 | pyexpat_xmlparser___dir___impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1044 | /*[clinic end generated code: output=bc22451efb9e4d17 input=76aa455f2a661384]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1045 | { |
| 1046 | #define APPEND(list, str) \ |
| 1047 | do { \ |
| 1048 | PyObject *o = PyUnicode_FromString(str); \ |
| 1049 | if (o != NULL) \ |
| 1050 | PyList_Append(list, o); \ |
| 1051 | Py_XDECREF(o); \ |
| 1052 | } while (0) |
| 1053 | |
| 1054 | int i; |
| 1055 | PyObject *rc = PyList_New(0); |
| 1056 | if (!rc) |
| 1057 | return NULL; |
| 1058 | for (i = 0; handler_info[i].name != NULL; i++) { |
| 1059 | PyObject *o = get_handler_name(&handler_info[i]); |
| 1060 | if (o != NULL) |
| 1061 | PyList_Append(rc, o); |
| 1062 | Py_XDECREF(o); |
| 1063 | } |
| 1064 | APPEND(rc, "ErrorCode"); |
| 1065 | APPEND(rc, "ErrorLineNumber"); |
| 1066 | APPEND(rc, "ErrorColumnNumber"); |
| 1067 | APPEND(rc, "ErrorByteIndex"); |
| 1068 | APPEND(rc, "CurrentLineNumber"); |
| 1069 | APPEND(rc, "CurrentColumnNumber"); |
| 1070 | APPEND(rc, "CurrentByteIndex"); |
| 1071 | APPEND(rc, "buffer_size"); |
| 1072 | APPEND(rc, "buffer_text"); |
| 1073 | APPEND(rc, "buffer_used"); |
| 1074 | APPEND(rc, "namespace_prefixes"); |
| 1075 | APPEND(rc, "ordered_attributes"); |
| 1076 | APPEND(rc, "specified_attributes"); |
| 1077 | APPEND(rc, "intern"); |
| 1078 | |
| 1079 | #undef APPEND |
| 1080 | |
| 1081 | if (PyErr_Occurred()) { |
| 1082 | Py_DECREF(rc); |
| 1083 | rc = NULL; |
| 1084 | } |
| 1085 | |
| 1086 | return rc; |
| 1087 | } |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1088 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1089 | static struct PyMethodDef xmlparse_methods[] = { |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1090 | PYEXPAT_XMLPARSER_PARSE_METHODDEF |
| 1091 | PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF |
| 1092 | PYEXPAT_XMLPARSER_SETBASE_METHODDEF |
| 1093 | PYEXPAT_XMLPARSER_GETBASE_METHODDEF |
| 1094 | PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF |
| 1095 | PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF |
| 1096 | PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1097 | #if XML_COMBINED_VERSION >= 19505 |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1098 | PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1099 | #endif |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1100 | PYEXPAT_XMLPARSER___DIR___METHODDEF |
| 1101 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1102 | }; |
| 1103 | |
| 1104 | /* ---------- */ |
| 1105 | |
| 1106 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1107 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1108 | /* pyexpat international encoding support. |
| 1109 | Make it as simple as possible. |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1110 | */ |
| 1111 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1112 | static int |
| 1113 | PyUnknownEncodingHandler(void *encodingHandlerData, |
| 1114 | const XML_Char *name, |
| 1115 | XML_Encoding *info) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1116 | { |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1117 | static unsigned char template_buffer[256] = {0}; |
| 1118 | PyObject* u; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1119 | int i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | void *data; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1121 | unsigned int kind; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1122 | |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 1123 | if (PyErr_Occurred()) |
| 1124 | return XML_STATUS_ERROR; |
| 1125 | |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1126 | if (template_buffer[1] == 0) { |
| 1127 | for (i = 0; i < 256; i++) |
| 1128 | template_buffer[i] = i; |
Tim Peters | 63cb99e | 2001-02-17 18:12:50 +0000 | [diff] [blame] | 1129 | } |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1130 | |
| 1131 | u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace"); |
Christian Heimes | b582155 | 2013-06-29 20:43:13 +0200 | [diff] [blame] | 1132 | if (u == NULL || PyUnicode_READY(u)) { |
Christian Heimes | 7217242 | 2013-06-29 21:49:27 +0200 | [diff] [blame] | 1133 | Py_XDECREF(u); |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1134 | return XML_STATUS_ERROR; |
Christian Heimes | b582155 | 2013-06-29 20:43:13 +0200 | [diff] [blame] | 1135 | } |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1136 | |
| 1137 | if (PyUnicode_GET_LENGTH(u) != 256) { |
| 1138 | Py_DECREF(u); |
| 1139 | PyErr_SetString(PyExc_ValueError, |
| 1140 | "multi-byte encodings are not supported"); |
| 1141 | return XML_STATUS_ERROR; |
| 1142 | } |
| 1143 | |
| 1144 | kind = PyUnicode_KIND(u); |
| 1145 | data = PyUnicode_DATA(u); |
| 1146 | for (i = 0; i < 256; i++) { |
| 1147 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 1148 | if (ch != Py_UNICODE_REPLACEMENT_CHARACTER) |
| 1149 | info->map[i] = ch; |
| 1150 | else |
| 1151 | info->map[i] = -1; |
| 1152 | } |
| 1153 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1154 | info->data = NULL; |
| 1155 | info->convert = NULL; |
| 1156 | info->release = NULL; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1157 | Py_DECREF(u); |
| 1158 | |
| 1159 | return XML_STATUS_OK; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1162 | |
| 1163 | static PyObject * |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1164 | newxmlparseobject(const char *encoding, const char *namespace_separator, PyObject *intern) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1165 | { |
| 1166 | int i; |
| 1167 | xmlparseobject *self; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1168 | |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1169 | self = PyObject_GC_New(xmlparseobject, &Xmlparsetype); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1170 | if (self == NULL) |
| 1171 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1172 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1173 | self->buffer = NULL; |
| 1174 | self->buffer_size = CHARACTER_DATA_BUFFER_SIZE; |
| 1175 | self->buffer_used = 0; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1176 | self->ordered_attributes = 0; |
| 1177 | self->specified_attributes = 0; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1178 | self->in_callback = 0; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1179 | self->ns_prefixes = 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1180 | self->handlers = NULL; |
Victor Stinner | 54b2d2e | 2013-07-15 17:15:57 +0200 | [diff] [blame] | 1181 | self->intern = intern; |
| 1182 | Py_XINCREF(self->intern); |
| 1183 | PyObject_GC_Track(self); |
| 1184 | |
Christian Heimes | fa535f5 | 2013-07-07 17:35:11 +0200 | [diff] [blame] | 1185 | /* namespace_separator is either NULL or contains one char + \0 */ |
| 1186 | self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler, |
| 1187 | namespace_separator); |
Victor Stinner | 54b2d2e | 2013-07-15 17:15:57 +0200 | [diff] [blame] | 1188 | if (self->itself == NULL) { |
| 1189 | PyErr_SetString(PyExc_RuntimeError, |
| 1190 | "XML_ParserCreate failed"); |
| 1191 | Py_DECREF(self); |
| 1192 | return NULL; |
| 1193 | } |
Gregory P. Smith | 2522771 | 2012-03-14 18:10:37 -0700 | [diff] [blame] | 1194 | #if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT) |
| 1195 | /* This feature was added upstream in libexpat 2.1.0. Our expat copy |
| 1196 | * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT |
| 1197 | * to indicate that we can still use it. */ |
Gregory P. Smith | 8e91cf6 | 2012-03-14 14:26:55 -0700 | [diff] [blame] | 1198 | XML_SetHashSalt(self->itself, |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 1199 | (unsigned long)_Py_HashSecret.expat.hashsalt); |
Gregory P. Smith | 2522771 | 2012-03-14 18:10:37 -0700 | [diff] [blame] | 1200 | #endif |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1201 | XML_SetUserData(self->itself, (void *)self); |
Fred Drake | 7c75bf2 | 2002-07-01 14:02:31 +0000 | [diff] [blame] | 1202 | XML_SetUnknownEncodingHandler(self->itself, |
| 1203 | (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1204 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1205 | for (i = 0; handler_info[i].name != NULL; i++) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1206 | /* do nothing */; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1207 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1208 | self->handlers = PyMem_New(PyObject *, i); |
Fred Drake | 7c75bf2 | 2002-07-01 14:02:31 +0000 | [diff] [blame] | 1209 | if (!self->handlers) { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1210 | Py_DECREF(self); |
| 1211 | return PyErr_NoMemory(); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1212 | } |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1213 | clear_handlers(self, 1); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1214 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1215 | return (PyObject*)self; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | static void |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1220 | xmlparse_dealloc(xmlparseobject *self) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1221 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1222 | int i; |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1223 | PyObject_GC_UnTrack(self); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1224 | if (self->itself != NULL) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1225 | XML_ParserFree(self->itself); |
| 1226 | self->itself = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1227 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1228 | if (self->handlers != NULL) { |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 1229 | for (i = 0; handler_info[i].name != NULL; i++) |
| 1230 | Py_CLEAR(self->handlers[i]); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1231 | PyMem_Free(self->handlers); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1232 | self->handlers = NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1233 | } |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1234 | if (self->buffer != NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1235 | PyMem_Free(self->buffer); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1236 | self->buffer = NULL; |
| 1237 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1238 | Py_XDECREF(self->intern); |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1239 | PyObject_GC_Del(self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1242 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1243 | handlername2int(PyObject *name) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1244 | { |
| 1245 | int i; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1246 | for (i = 0; handler_info[i].name != NULL; i++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1247 | if (_PyUnicode_EqualToASCIIString(name, handler_info[i].name)) { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1248 | return i; |
| 1249 | } |
| 1250 | } |
| 1251 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static PyObject * |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1255 | get_pybool(int istrue) |
| 1256 | { |
| 1257 | PyObject *result = istrue ? Py_True : Py_False; |
| 1258 | Py_INCREF(result); |
| 1259 | return result; |
| 1260 | } |
| 1261 | |
| 1262 | static PyObject * |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1263 | xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1264 | { |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1265 | Py_UCS4 first_char; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1266 | int handlernum = -1; |
| 1267 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1268 | if (!PyUnicode_Check(nameobj)) |
| 1269 | goto generic; |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1270 | if (PyUnicode_READY(nameobj)) |
| 1271 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1273 | handlernum = handlername2int(nameobj); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1274 | |
| 1275 | if (handlernum != -1) { |
| 1276 | PyObject *result = self->handlers[handlernum]; |
| 1277 | if (result == NULL) |
| 1278 | result = Py_None; |
| 1279 | Py_INCREF(result); |
| 1280 | return result; |
| 1281 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1282 | |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1283 | first_char = PyUnicode_READ_CHAR(nameobj, 0); |
| 1284 | if (first_char == 'E') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1285 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorCode")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1286 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1287 | XML_GetErrorCode(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1288 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorLineNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1289 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1290 | XML_GetErrorLineNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1291 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorColumnNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1292 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1293 | XML_GetErrorColumnNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1294 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorByteIndex")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1295 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1296 | XML_GetErrorByteIndex(self->itself)); |
| 1297 | } |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1298 | if (first_char == 'C') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1299 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentLineNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1300 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1301 | XML_GetCurrentLineNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1302 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentColumnNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1303 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1304 | XML_GetCurrentColumnNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1305 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentByteIndex")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1306 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1307 | XML_GetCurrentByteIndex(self->itself)); |
| 1308 | } |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1309 | if (first_char == 'b') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1310 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_size")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1311 | return PyLong_FromLong((long) self->buffer_size); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1312 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_text")) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1313 | return get_pybool(self->buffer != NULL); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1314 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_used")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1315 | return PyLong_FromLong((long) self->buffer_used); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1316 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1317 | if (_PyUnicode_EqualToASCIIString(nameobj, "namespace_prefixes")) |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1318 | return get_pybool(self->ns_prefixes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1319 | if (_PyUnicode_EqualToASCIIString(nameobj, "ordered_attributes")) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1320 | return get_pybool(self->ordered_attributes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1321 | if (_PyUnicode_EqualToASCIIString(nameobj, "specified_attributes")) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1322 | return get_pybool((long) self->specified_attributes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1323 | if (_PyUnicode_EqualToASCIIString(nameobj, "intern")) { |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1324 | if (self->intern == NULL) { |
| 1325 | Py_INCREF(Py_None); |
| 1326 | return Py_None; |
| 1327 | } |
| 1328 | else { |
| 1329 | Py_INCREF(self->intern); |
| 1330 | return self->intern; |
| 1331 | } |
| 1332 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1333 | generic: |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1334 | return PyObject_GenericGetAttr((PyObject*)self, nameobj); |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1337 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1338 | sethandler(xmlparseobject *self, PyObject *name, PyObject* v) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1339 | { |
| 1340 | int handlernum = handlername2int(name); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1341 | if (handlernum >= 0) { |
| 1342 | xmlhandler c_handler = NULL; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1343 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1344 | if (v == Py_None) { |
| 1345 | /* If this is the character data handler, and a character |
| 1346 | data handler is already active, we need to be more |
| 1347 | careful. What we can safely do is replace the existing |
| 1348 | character data handler callback function with a no-op |
| 1349 | function that will refuse to call Python. The downside |
| 1350 | is that this doesn't completely remove the character |
| 1351 | data handler from the C layer if there's any callback |
| 1352 | active, so Expat does a little more work than it |
| 1353 | otherwise would, but that's really an odd case. A more |
| 1354 | elaborate system of handlers and state could remove the |
| 1355 | C handler more effectively. */ |
| 1356 | if (handlernum == CharacterData && self->in_callback) |
| 1357 | c_handler = noop_character_data_handler; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1358 | v = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1359 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1360 | else if (v != NULL) { |
| 1361 | Py_INCREF(v); |
| 1362 | c_handler = handler_info[handlernum].handler; |
| 1363 | } |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 1364 | Py_XSETREF(self->handlers[handlernum], v); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1365 | handler_info[handlernum].setter(self->itself, c_handler); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1366 | return 1; |
| 1367 | } |
| 1368 | return 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1372 | xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1373 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1374 | /* Set attribute 'name' to value 'v'. v==NULL means delete */ |
Serhiy Storchaka | 931331a | 2015-09-07 22:37:02 +0300 | [diff] [blame] | 1375 | if (!PyUnicode_Check(name)) { |
| 1376 | PyErr_Format(PyExc_TypeError, |
| 1377 | "attribute name must be string, not '%.200s'", |
| 1378 | name->ob_type->tp_name); |
| 1379 | return -1; |
| 1380 | } |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1381 | if (v == NULL) { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1382 | PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute"); |
| 1383 | return -1; |
| 1384 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1385 | if (_PyUnicode_EqualToASCIIString(name, "buffer_text")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1386 | int b = PyObject_IsTrue(v); |
| 1387 | if (b < 0) |
| 1388 | return -1; |
| 1389 | if (b) { |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1390 | if (self->buffer == NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1391 | self->buffer = PyMem_Malloc(self->buffer_size); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1392 | if (self->buffer == NULL) { |
| 1393 | PyErr_NoMemory(); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | self->buffer_used = 0; |
| 1397 | } |
| 1398 | } |
| 1399 | else if (self->buffer != NULL) { |
| 1400 | if (flush_character_buffer(self) < 0) |
| 1401 | return -1; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1402 | PyMem_Free(self->buffer); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1403 | self->buffer = NULL; |
| 1404 | } |
| 1405 | return 0; |
| 1406 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1407 | if (_PyUnicode_EqualToASCIIString(name, "namespace_prefixes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1408 | int b = PyObject_IsTrue(v); |
| 1409 | if (b < 0) |
| 1410 | return -1; |
| 1411 | self->ns_prefixes = b; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1412 | XML_SetReturnNSTriplet(self->itself, self->ns_prefixes); |
| 1413 | return 0; |
| 1414 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1415 | if (_PyUnicode_EqualToASCIIString(name, "ordered_attributes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1416 | int b = PyObject_IsTrue(v); |
| 1417 | if (b < 0) |
| 1418 | return -1; |
| 1419 | self->ordered_attributes = b; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1422 | if (_PyUnicode_EqualToASCIIString(name, "specified_attributes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1423 | int b = PyObject_IsTrue(v); |
| 1424 | if (b < 0) |
| 1425 | return -1; |
| 1426 | self->specified_attributes = b; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1429 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1430 | if (_PyUnicode_EqualToASCIIString(name, "buffer_size")) { |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1431 | long new_buffer_size; |
| 1432 | if (!PyLong_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer"); |
| 1434 | return -1; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Serhiy Storchaka | de5f9f4 | 2015-09-07 22:51:56 +0300 | [diff] [blame] | 1437 | new_buffer_size = PyLong_AsLong(v); |
| 1438 | if (new_buffer_size <= 0) { |
| 1439 | if (!PyErr_Occurred()) |
| 1440 | PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero"); |
| 1441 | return -1; |
| 1442 | } |
| 1443 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1444 | /* trivial case -- no change */ |
| 1445 | if (new_buffer_size == self->buffer_size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | return 0; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1449 | /* check maximum */ |
| 1450 | if (new_buffer_size > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | char errmsg[100]; |
| 1452 | sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX); |
| 1453 | PyErr_SetString(PyExc_ValueError, errmsg); |
| 1454 | return -1; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | if (self->buffer != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | /* there is already a buffer */ |
| 1459 | if (self->buffer_used != 0) { |
Christian Heimes | 09994a9 | 2013-07-20 22:41:58 +0200 | [diff] [blame] | 1460 | if (flush_character_buffer(self) < 0) { |
| 1461 | return -1; |
| 1462 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | } |
| 1464 | /* free existing buffer */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1465 | PyMem_Free(self->buffer); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1466 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1467 | self->buffer = PyMem_Malloc(new_buffer_size); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1468 | if (self->buffer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | PyErr_NoMemory(); |
| 1470 | return -1; |
| 1471 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1472 | self->buffer_size = new_buffer_size; |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1476 | if (_PyUnicode_EqualToASCIIString(name, "CharacterDataHandler")) { |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1477 | /* If we're changing the character data handler, flush all |
| 1478 | * cached data with the old handler. Not sure there's a |
| 1479 | * "right" thing to do, though, but this probably won't |
| 1480 | * happen. |
| 1481 | */ |
| 1482 | if (flush_character_buffer(self) < 0) |
| 1483 | return -1; |
| 1484 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1485 | if (sethandler(self, name, v)) { |
| 1486 | return 0; |
| 1487 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1488 | PyErr_SetObject(PyExc_AttributeError, name); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1489 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1492 | static int |
| 1493 | xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg) |
| 1494 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1495 | int i; |
| 1496 | for (i = 0; handler_info[i].name != NULL; i++) |
| 1497 | Py_VISIT(op->handlers[i]); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1498 | return 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static int |
| 1502 | xmlparse_clear(xmlparseobject *op) |
| 1503 | { |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1504 | clear_handlers(op, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1505 | Py_CLEAR(op->intern); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1506 | return 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1507 | } |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1509 | PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser"); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1510 | |
| 1511 | static PyTypeObject Xmlparsetype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1513 | "pyexpat.xmlparser", /*tp_name*/ |
Antoine Pitrou | 23683ef | 2011-01-04 00:00:31 +0000 | [diff] [blame] | 1514 | sizeof(xmlparseobject), /*tp_basicsize*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | 0, /*tp_itemsize*/ |
| 1516 | /* methods */ |
| 1517 | (destructor)xmlparse_dealloc, /*tp_dealloc*/ |
| 1518 | (printfunc)0, /*tp_print*/ |
| 1519 | 0, /*tp_getattr*/ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1520 | 0, /*tp_setattr*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | 0, /*tp_reserved*/ |
| 1522 | (reprfunc)0, /*tp_repr*/ |
| 1523 | 0, /*tp_as_number*/ |
| 1524 | 0, /*tp_as_sequence*/ |
| 1525 | 0, /*tp_as_mapping*/ |
| 1526 | (hashfunc)0, /*tp_hash*/ |
| 1527 | (ternaryfunc)0, /*tp_call*/ |
| 1528 | (reprfunc)0, /*tp_str*/ |
| 1529 | (getattrofunc)xmlparse_getattro, /* tp_getattro */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1530 | (setattrofunc)xmlparse_setattro, /* tp_setattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | 0, /* tp_as_buffer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | Xmlparsetype__doc__, /* tp_doc - Documentation string */ |
| 1534 | (traverseproc)xmlparse_traverse, /* tp_traverse */ |
| 1535 | (inquiry)xmlparse_clear, /* tp_clear */ |
| 1536 | 0, /* tp_richcompare */ |
| 1537 | 0, /* tp_weaklistoffset */ |
| 1538 | 0, /* tp_iter */ |
| 1539 | 0, /* tp_iternext */ |
| 1540 | xmlparse_methods, /* tp_methods */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1541 | }; |
| 1542 | |
| 1543 | /* End of code for xmlparser objects */ |
| 1544 | /* -------------------------------------------------------- */ |
| 1545 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1546 | /*[clinic input] |
| 1547 | pyexpat.ParserCreate |
| 1548 | |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 1549 | encoding: str(accept={str, NoneType}) = NULL |
| 1550 | namespace_separator: str(accept={str, NoneType}) = NULL |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1551 | intern: object = NULL |
| 1552 | |
| 1553 | Return a new XML parser object. |
| 1554 | [clinic start generated code]*/ |
| 1555 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1556 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1557 | pyexpat_ParserCreate_impl(PyObject *module, const char *encoding, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1558 | const char *namespace_separator, PyObject *intern) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1559 | /*[clinic end generated code: output=295c0cf01ab1146c input=23d29704acad385d]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1560 | { |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1561 | PyObject *result; |
| 1562 | int intern_decref = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1563 | |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1564 | if (namespace_separator != NULL |
| 1565 | && strlen(namespace_separator) > 1) { |
| 1566 | PyErr_SetString(PyExc_ValueError, |
| 1567 | "namespace_separator must be at most one" |
| 1568 | " character, omitted, or None"); |
| 1569 | return NULL; |
| 1570 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1571 | /* Explicitly passing None means no interning is desired. |
| 1572 | Not passing anything means that a new dictionary is used. */ |
| 1573 | if (intern == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | intern = NULL; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1575 | else if (intern == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | intern = PyDict_New(); |
| 1577 | if (!intern) |
| 1578 | return NULL; |
| 1579 | intern_decref = 1; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1580 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1581 | else if (!PyDict_Check(intern)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | PyErr_SetString(PyExc_TypeError, "intern must be a dictionary"); |
| 1583 | return NULL; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1586 | result = newxmlparseobject(encoding, namespace_separator, intern); |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1587 | if (intern_decref) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | Py_DECREF(intern); |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1589 | } |
| 1590 | return result; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1593 | /*[clinic input] |
| 1594 | pyexpat.ErrorString |
| 1595 | |
| 1596 | code: long |
| 1597 | / |
| 1598 | |
| 1599 | Returns string error for given number. |
| 1600 | [clinic start generated code]*/ |
| 1601 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1602 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1603 | pyexpat_ErrorString_impl(PyObject *module, long code) |
| 1604 | /*[clinic end generated code: output=2feae50d166f2174 input=cc67de010d9e62b3]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1605 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1606 | return Py_BuildValue("z", XML_ErrorString((int)code)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /* List of methods defined in the module */ |
| 1610 | |
| 1611 | static struct PyMethodDef pyexpat_methods[] = { |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1612 | PYEXPAT_PARSERCREATE_METHODDEF |
| 1613 | PYEXPAT_ERRORSTRING_METHODDEF |
| 1614 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1615 | }; |
| 1616 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 1617 | /* Module docstring */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1619 | PyDoc_STRVAR(pyexpat_module_documentation, |
| 1620 | "Python wrapper for Expat parser."); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1621 | |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1622 | /* Initialization function for the module */ |
| 1623 | |
| 1624 | #ifndef MODULE_NAME |
| 1625 | #define MODULE_NAME "pyexpat" |
| 1626 | #endif |
| 1627 | |
| 1628 | #ifndef MODULE_INITFUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1629 | #define MODULE_INITFUNC PyInit_pyexpat |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1630 | #endif |
| 1631 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1632 | static struct PyModuleDef pyexpatmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | PyModuleDef_HEAD_INIT, |
| 1634 | MODULE_NAME, |
| 1635 | pyexpat_module_documentation, |
| 1636 | -1, |
| 1637 | pyexpat_methods, |
| 1638 | NULL, |
| 1639 | NULL, |
| 1640 | NULL, |
| 1641 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1642 | }; |
| 1643 | |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1644 | PyMODINIT_FUNC |
| 1645 | MODULE_INITFUNC(void) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1646 | { |
| 1647 | PyObject *m, *d; |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 1648 | PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1649 | PyObject *errors_module; |
| 1650 | PyObject *modelmod_name; |
| 1651 | PyObject *model_module; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1652 | PyObject *sys_modules; |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1653 | PyObject *tmpnum, *tmpstr; |
| 1654 | PyObject *codes_dict; |
| 1655 | PyObject *rev_codes_dict; |
| 1656 | int res; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1657 | static struct PyExpat_CAPI capi; |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1658 | PyObject *capi_object; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1659 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1660 | if (errmod_name == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1661 | return NULL; |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 1662 | modelmod_name = PyUnicode_FromString(MODULE_NAME ".model"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1663 | if (modelmod_name == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1664 | return NULL; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1665 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1666 | if (PyType_Ready(&Xmlparsetype) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1668 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1669 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1670 | m = PyModule_Create(&pyexpatmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1671 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1673 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1674 | /* Add some symbolic constants to the module */ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1675 | if (ErrorObject == NULL) { |
| 1676 | ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError", |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1677 | NULL, NULL); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1678 | if (ErrorObject == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1679 | return NULL; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1680 | } |
| 1681 | Py_INCREF(ErrorObject); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1682 | PyModule_AddObject(m, "error", ErrorObject); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1683 | Py_INCREF(ErrorObject); |
| 1684 | PyModule_AddObject(m, "ExpatError", ErrorObject); |
Fred Drake | 4ba298c | 2000-10-29 04:57:53 +0000 | [diff] [blame] | 1685 | Py_INCREF(&Xmlparsetype); |
| 1686 | PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1687 | |
Fred Drake | 738293d | 2000-12-21 17:25:07 +0000 | [diff] [blame] | 1688 | PyModule_AddStringConstant(m, "EXPAT_VERSION", |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1689 | XML_ExpatVersion()); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1690 | { |
| 1691 | XML_Expat_Version info = XML_ExpatVersionInfo(); |
| 1692 | PyModule_AddObject(m, "version_info", |
| 1693 | Py_BuildValue("(iii)", info.major, |
| 1694 | info.minor, info.micro)); |
| 1695 | } |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1696 | /* XXX When Expat supports some way of figuring out how it was |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1697 | compiled, this should check and set native_encoding |
| 1698 | appropriately. |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1699 | */ |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1700 | PyModule_AddStringConstant(m, "native_encoding", "UTF-8"); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 1701 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1702 | sys_modules = PySys_GetObject("modules"); |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1703 | if (sys_modules == NULL) { |
| 1704 | Py_DECREF(m); |
| 1705 | return NULL; |
| 1706 | } |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1707 | d = PyModule_GetDict(m); |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1708 | if (d == NULL) { |
| 1709 | Py_DECREF(m); |
| 1710 | return NULL; |
| 1711 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1712 | errors_module = PyDict_GetItem(d, errmod_name); |
| 1713 | if (errors_module == NULL) { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1714 | errors_module = PyModule_New(MODULE_NAME ".errors"); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1715 | if (errors_module != NULL) { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1716 | PyDict_SetItem(sys_modules, errmod_name, errors_module); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1717 | /* gives away the reference to errors_module */ |
| 1718 | PyModule_AddObject(m, "errors", errors_module); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1721 | Py_DECREF(errmod_name); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1722 | model_module = PyDict_GetItem(d, modelmod_name); |
| 1723 | if (model_module == NULL) { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1724 | model_module = PyModule_New(MODULE_NAME ".model"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1725 | if (model_module != NULL) { |
| 1726 | PyDict_SetItem(sys_modules, modelmod_name, model_module); |
| 1727 | /* gives away the reference to model_module */ |
| 1728 | PyModule_AddObject(m, "model", model_module); |
| 1729 | } |
| 1730 | } |
| 1731 | Py_DECREF(modelmod_name); |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1732 | if (errors_module == NULL || model_module == NULL) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1733 | /* Don't core dump later! */ |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1734 | Py_DECREF(m); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1735 | return NULL; |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1736 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1738 | #if XML_COMBINED_VERSION > 19505 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1739 | { |
| 1740 | const XML_Feature *features = XML_GetFeatureList(); |
| 1741 | PyObject *list = PyList_New(0); |
| 1742 | if (list == NULL) |
| 1743 | /* just ignore it */ |
| 1744 | PyErr_Clear(); |
| 1745 | else { |
| 1746 | int i = 0; |
| 1747 | for (; features[i].feature != XML_FEATURE_END; ++i) { |
| 1748 | int ok; |
| 1749 | PyObject *item = Py_BuildValue("si", features[i].name, |
| 1750 | features[i].value); |
| 1751 | if (item == NULL) { |
| 1752 | Py_DECREF(list); |
| 1753 | list = NULL; |
| 1754 | break; |
| 1755 | } |
| 1756 | ok = PyList_Append(list, item); |
| 1757 | Py_DECREF(item); |
| 1758 | if (ok < 0) { |
| 1759 | PyErr_Clear(); |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | if (list != NULL) |
| 1764 | PyModule_AddObject(m, "features", list); |
| 1765 | } |
| 1766 | } |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1767 | #endif |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1768 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1769 | codes_dict = PyDict_New(); |
| 1770 | rev_codes_dict = PyDict_New(); |
| 1771 | if (codes_dict == NULL || rev_codes_dict == NULL) { |
| 1772 | Py_XDECREF(codes_dict); |
| 1773 | Py_XDECREF(rev_codes_dict); |
| 1774 | return NULL; |
| 1775 | } |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1776 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1777 | #define MYCONST(name) \ |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1778 | if (PyModule_AddStringConstant(errors_module, #name, \ |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1779 | XML_ErrorString(name)) < 0) \ |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1780 | return NULL; \ |
| 1781 | tmpnum = PyLong_FromLong(name); \ |
| 1782 | if (tmpnum == NULL) return NULL; \ |
| 1783 | res = PyDict_SetItemString(codes_dict, \ |
| 1784 | XML_ErrorString(name), tmpnum); \ |
| 1785 | if (res < 0) return NULL; \ |
| 1786 | tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \ |
| 1787 | if (tmpstr == NULL) return NULL; \ |
| 1788 | res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \ |
| 1789 | Py_DECREF(tmpstr); \ |
| 1790 | Py_DECREF(tmpnum); \ |
| 1791 | if (res < 0) return NULL; \ |
Fred Drake | 7bd9f41 | 2000-07-04 23:51:31 +0000 | [diff] [blame] | 1792 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1793 | MYCONST(XML_ERROR_NO_MEMORY); |
| 1794 | MYCONST(XML_ERROR_SYNTAX); |
| 1795 | MYCONST(XML_ERROR_NO_ELEMENTS); |
| 1796 | MYCONST(XML_ERROR_INVALID_TOKEN); |
| 1797 | MYCONST(XML_ERROR_UNCLOSED_TOKEN); |
| 1798 | MYCONST(XML_ERROR_PARTIAL_CHAR); |
| 1799 | MYCONST(XML_ERROR_TAG_MISMATCH); |
| 1800 | MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE); |
| 1801 | MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT); |
| 1802 | MYCONST(XML_ERROR_PARAM_ENTITY_REF); |
| 1803 | MYCONST(XML_ERROR_UNDEFINED_ENTITY); |
| 1804 | MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF); |
| 1805 | MYCONST(XML_ERROR_ASYNC_ENTITY); |
| 1806 | MYCONST(XML_ERROR_BAD_CHAR_REF); |
| 1807 | MYCONST(XML_ERROR_BINARY_ENTITY_REF); |
| 1808 | MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF); |
| 1809 | MYCONST(XML_ERROR_MISPLACED_XML_PI); |
| 1810 | MYCONST(XML_ERROR_UNKNOWN_ENCODING); |
| 1811 | MYCONST(XML_ERROR_INCORRECT_ENCODING); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1812 | MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION); |
| 1813 | MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING); |
| 1814 | MYCONST(XML_ERROR_NOT_STANDALONE); |
Fred Drake | 283b670 | 2004-08-04 22:28:16 +0000 | [diff] [blame] | 1815 | MYCONST(XML_ERROR_UNEXPECTED_STATE); |
| 1816 | MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE); |
| 1817 | MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD); |
| 1818 | MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING); |
| 1819 | /* Added in Expat 1.95.7. */ |
| 1820 | MYCONST(XML_ERROR_UNBOUND_PREFIX); |
| 1821 | /* Added in Expat 1.95.8. */ |
| 1822 | MYCONST(XML_ERROR_UNDECLARING_PREFIX); |
| 1823 | MYCONST(XML_ERROR_INCOMPLETE_PE); |
| 1824 | MYCONST(XML_ERROR_XML_DECL); |
| 1825 | MYCONST(XML_ERROR_TEXT_DECL); |
| 1826 | MYCONST(XML_ERROR_PUBLICID); |
| 1827 | MYCONST(XML_ERROR_SUSPENDED); |
| 1828 | MYCONST(XML_ERROR_NOT_SUSPENDED); |
| 1829 | MYCONST(XML_ERROR_ABORTED); |
| 1830 | MYCONST(XML_ERROR_FINISHED); |
| 1831 | MYCONST(XML_ERROR_SUSPEND_PE); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1832 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1833 | if (PyModule_AddStringConstant(errors_module, "__doc__", |
| 1834 | "Constants used to describe " |
| 1835 | "error conditions.") < 0) |
| 1836 | return NULL; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1837 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1838 | if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0) |
| 1839 | return NULL; |
| 1840 | if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0) |
| 1841 | return NULL; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1842 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1843 | #undef MYCONST |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1844 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1845 | #define MYCONST(c) PyModule_AddIntConstant(m, #c, c) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1846 | MYCONST(XML_PARAM_ENTITY_PARSING_NEVER); |
| 1847 | MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); |
| 1848 | MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1849 | #undef MYCONST |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1850 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1851 | #define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c) |
| 1852 | PyModule_AddStringConstant(model_module, "__doc__", |
| 1853 | "Constants used to interpret content model information."); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1854 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1855 | MYCONST(XML_CTYPE_EMPTY); |
| 1856 | MYCONST(XML_CTYPE_ANY); |
| 1857 | MYCONST(XML_CTYPE_MIXED); |
| 1858 | MYCONST(XML_CTYPE_NAME); |
| 1859 | MYCONST(XML_CTYPE_CHOICE); |
| 1860 | MYCONST(XML_CTYPE_SEQ); |
| 1861 | |
| 1862 | MYCONST(XML_CQUANT_NONE); |
| 1863 | MYCONST(XML_CQUANT_OPT); |
| 1864 | MYCONST(XML_CQUANT_REP); |
| 1865 | MYCONST(XML_CQUANT_PLUS); |
| 1866 | #undef MYCONST |
Fredrik Lundh | c334504 | 2005-12-13 19:49:55 +0000 | [diff] [blame] | 1867 | |
| 1868 | /* initialize pyexpat dispatch table */ |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1869 | capi.size = sizeof(capi); |
Fredrik Lundh | cc117db | 2005-12-13 21:55:36 +0000 | [diff] [blame] | 1870 | capi.magic = PyExpat_CAPI_MAGIC; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1871 | capi.MAJOR_VERSION = XML_MAJOR_VERSION; |
| 1872 | capi.MINOR_VERSION = XML_MINOR_VERSION; |
| 1873 | capi.MICRO_VERSION = XML_MICRO_VERSION; |
| 1874 | capi.ErrorString = XML_ErrorString; |
Fredrik Lundh | cc117db | 2005-12-13 21:55:36 +0000 | [diff] [blame] | 1875 | capi.GetErrorCode = XML_GetErrorCode; |
| 1876 | capi.GetErrorColumnNumber = XML_GetErrorColumnNumber; |
| 1877 | capi.GetErrorLineNumber = XML_GetErrorLineNumber; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1878 | capi.Parse = XML_Parse; |
| 1879 | capi.ParserCreate_MM = XML_ParserCreate_MM; |
| 1880 | capi.ParserFree = XML_ParserFree; |
| 1881 | capi.SetCharacterDataHandler = XML_SetCharacterDataHandler; |
| 1882 | capi.SetCommentHandler = XML_SetCommentHandler; |
| 1883 | capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand; |
| 1884 | capi.SetElementHandler = XML_SetElementHandler; |
| 1885 | capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler; |
| 1886 | capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler; |
| 1887 | capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler; |
| 1888 | capi.SetUserData = XML_SetUserData; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 1889 | capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 1890 | capi.SetEncoding = XML_SetEncoding; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1891 | capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1893 | /* export using capsule */ |
| 1894 | capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL); |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1895 | if (capi_object) |
| 1896 | PyModule_AddObject(m, "expat_CAPI", capi_object); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1897 | return m; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1900 | static void |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1901 | clear_handlers(xmlparseobject *self, int initial) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1902 | { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1903 | int i = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1904 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1905 | for (; handler_info[i].name != NULL; i++) { |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1906 | if (initial) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | self->handlers[i] = NULL; |
| 1908 | else { |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 1909 | Py_CLEAR(self->handlers[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | handler_info[i].setter(self->itself, NULL); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1911 | } |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1912 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1915 | static struct HandlerInfo handler_info[] = { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1916 | {"StartElementHandler", |
| 1917 | (xmlhandlersetter)XML_SetStartElementHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1918 | (xmlhandler)my_StartElementHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1919 | {"EndElementHandler", |
| 1920 | (xmlhandlersetter)XML_SetEndElementHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1921 | (xmlhandler)my_EndElementHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1922 | {"ProcessingInstructionHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1923 | (xmlhandlersetter)XML_SetProcessingInstructionHandler, |
| 1924 | (xmlhandler)my_ProcessingInstructionHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1925 | {"CharacterDataHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1926 | (xmlhandlersetter)XML_SetCharacterDataHandler, |
| 1927 | (xmlhandler)my_CharacterDataHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1928 | {"UnparsedEntityDeclHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1929 | (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1930 | (xmlhandler)my_UnparsedEntityDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1931 | {"NotationDeclHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1932 | (xmlhandlersetter)XML_SetNotationDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1933 | (xmlhandler)my_NotationDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1934 | {"StartNamespaceDeclHandler", |
| 1935 | (xmlhandlersetter)XML_SetStartNamespaceDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1936 | (xmlhandler)my_StartNamespaceDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1937 | {"EndNamespaceDeclHandler", |
| 1938 | (xmlhandlersetter)XML_SetEndNamespaceDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1939 | (xmlhandler)my_EndNamespaceDeclHandler}, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1940 | {"CommentHandler", |
| 1941 | (xmlhandlersetter)XML_SetCommentHandler, |
| 1942 | (xmlhandler)my_CommentHandler}, |
| 1943 | {"StartCdataSectionHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1944 | (xmlhandlersetter)XML_SetStartCdataSectionHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1945 | (xmlhandler)my_StartCdataSectionHandler}, |
| 1946 | {"EndCdataSectionHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1947 | (xmlhandlersetter)XML_SetEndCdataSectionHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1948 | (xmlhandler)my_EndCdataSectionHandler}, |
| 1949 | {"DefaultHandler", |
| 1950 | (xmlhandlersetter)XML_SetDefaultHandler, |
| 1951 | (xmlhandler)my_DefaultHandler}, |
| 1952 | {"DefaultHandlerExpand", |
| 1953 | (xmlhandlersetter)XML_SetDefaultHandlerExpand, |
| 1954 | (xmlhandler)my_DefaultHandlerExpandHandler}, |
| 1955 | {"NotStandaloneHandler", |
| 1956 | (xmlhandlersetter)XML_SetNotStandaloneHandler, |
| 1957 | (xmlhandler)my_NotStandaloneHandler}, |
| 1958 | {"ExternalEntityRefHandler", |
| 1959 | (xmlhandlersetter)XML_SetExternalEntityRefHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1960 | (xmlhandler)my_ExternalEntityRefHandler}, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1961 | {"StartDoctypeDeclHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1962 | (xmlhandlersetter)XML_SetStartDoctypeDeclHandler, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1963 | (xmlhandler)my_StartDoctypeDeclHandler}, |
| 1964 | {"EndDoctypeDeclHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1965 | (xmlhandlersetter)XML_SetEndDoctypeDeclHandler, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1966 | (xmlhandler)my_EndDoctypeDeclHandler}, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1967 | {"EntityDeclHandler", |
| 1968 | (xmlhandlersetter)XML_SetEntityDeclHandler, |
| 1969 | (xmlhandler)my_EntityDeclHandler}, |
| 1970 | {"XmlDeclHandler", |
| 1971 | (xmlhandlersetter)XML_SetXmlDeclHandler, |
| 1972 | (xmlhandler)my_XmlDeclHandler}, |
| 1973 | {"ElementDeclHandler", |
| 1974 | (xmlhandlersetter)XML_SetElementDeclHandler, |
| 1975 | (xmlhandler)my_ElementDeclHandler}, |
| 1976 | {"AttlistDeclHandler", |
| 1977 | (xmlhandlersetter)XML_SetAttlistDeclHandler, |
| 1978 | (xmlhandler)my_AttlistDeclHandler}, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1979 | #if XML_COMBINED_VERSION >= 19504 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1980 | {"SkippedEntityHandler", |
| 1981 | (xmlhandlersetter)XML_SetSkippedEntityHandler, |
| 1982 | (xmlhandler)my_SkippedEntityHandler}, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1983 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1984 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1985 | {NULL, NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1986 | }; |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1987 | |
| 1988 | /*[clinic input] |
| 1989 | dump buffer |
| 1990 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1991 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |