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