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