Martin v. Löwis | 7090ed1 | 2001-09-19 10:37:50 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Fred Drake | 4113b13 | 2001-03-24 19:58:26 +0000 | [diff] [blame] | 2 | #include <ctype.h> |
| 3 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 4 | #include "frameobject.h" |
Fred Drake | a77254a | 2000-09-29 19:23:29 +0000 | [diff] [blame] | 5 | #include "expat.h" |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 6 | |
Fredrik Lundh | c334504 | 2005-12-13 19:49:55 +0000 | [diff] [blame] | 7 | #include "pyexpat.h" |
| 8 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 9 | /* Do not emit Clinic output to a file as that wreaks havoc with conditionally |
| 10 | included methods. */ |
| 11 | /*[clinic input] |
| 12 | module pyexpat |
| 13 | [clinic start generated code]*/ |
| 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b168d503a4490c15]*/ |
| 15 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 16 | #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION) |
| 17 | |
Christian Heimes | fa535f5 | 2013-07-07 17:35:11 +0200 | [diff] [blame] | 18 | static XML_Memory_Handling_Suite ExpatMemoryHandler = { |
| 19 | PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
| 20 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 21 | enum HandlerTypes { |
| 22 | StartElement, |
| 23 | EndElement, |
| 24 | ProcessingInstruction, |
| 25 | CharacterData, |
| 26 | UnparsedEntityDecl, |
| 27 | NotationDecl, |
| 28 | StartNamespaceDecl, |
| 29 | EndNamespaceDecl, |
| 30 | Comment, |
| 31 | StartCdataSection, |
| 32 | EndCdataSection, |
| 33 | Default, |
| 34 | DefaultHandlerExpand, |
| 35 | NotStandalone, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 36 | ExternalEntityRef, |
| 37 | StartDoctypeDecl, |
| 38 | EndDoctypeDecl, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 39 | EntityDecl, |
| 40 | XmlDecl, |
| 41 | ElementDecl, |
| 42 | AttlistDecl, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 43 | #if XML_COMBINED_VERSION >= 19504 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 44 | SkippedEntity, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 45 | #endif |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 46 | _DummyDecl |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static PyObject *ErrorObject; |
| 50 | |
| 51 | /* ----------------------------------------------------- */ |
| 52 | |
| 53 | /* Declarations for objects of type xmlparser */ |
| 54 | |
| 55 | typedef struct { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 56 | PyObject_HEAD |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 57 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 58 | XML_Parser itself; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 59 | int ordered_attributes; /* Return attributes as a list. */ |
| 60 | int specified_attributes; /* Report only specified attributes. */ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 61 | int in_callback; /* Is a callback active? */ |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 62 | int ns_prefixes; /* Namespace-triplets mode? */ |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 63 | XML_Char *buffer; /* Buffer used when accumulating characters */ |
| 64 | /* NULL if not enabled */ |
| 65 | int buffer_size; /* Size of buffer, in XML_Char units */ |
| 66 | int buffer_used; /* Buffer units in use */ |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 67 | PyObject *intern; /* Dictionary to intern strings */ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 68 | PyObject **handlers; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 69 | } xmlparseobject; |
| 70 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 71 | #include "clinic/pyexpat.c.h" |
| 72 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 73 | #define CHARACTER_DATA_BUFFER_SIZE 8192 |
| 74 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 75 | static PyTypeObject Xmlparsetype; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 76 | |
Fred Drake | 117ac85 | 2002-09-24 16:24:54 +0000 | [diff] [blame] | 77 | typedef void (*xmlhandlersetter)(XML_Parser self, void *meth); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 78 | typedef void* xmlhandler; |
| 79 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 80 | struct HandlerInfo { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 81 | const char *name; |
| 82 | xmlhandlersetter setter; |
| 83 | xmlhandler handler; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 84 | PyCodeObject *tb_code; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 85 | PyObject *nameobj; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 88 | static struct HandlerInfo handler_info[64]; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 89 | |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 90 | /* Set an integer attribute on the error object; return true on success, |
| 91 | * false on an exception. |
| 92 | */ |
| 93 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 94 | set_error_attr(PyObject *err, const char *name, int value) |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 95 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 96 | PyObject *v = PyLong_FromLong(value); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 97 | |
Neal Norwitz | 2f5e990 | 2006-03-08 06:36:45 +0000 | [diff] [blame] | 98 | if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) { |
| 99 | Py_XDECREF(v); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Michael W. Hudson | 0bb8454 | 2004-08-03 11:31:31 +0000 | [diff] [blame] | 102 | Py_DECREF(v); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | /* Build and set an Expat exception, including positioning |
| 107 | * information. Always returns NULL. |
| 108 | */ |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 109 | static PyObject * |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 110 | set_error(xmlparseobject *self, enum XML_Error code) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 111 | { |
| 112 | PyObject *err; |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 113 | PyObject *buffer; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 114 | XML_Parser parser = self->itself; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 115 | int lineno = XML_GetErrorLineNumber(parser); |
| 116 | int column = XML_GetErrorColumnNumber(parser); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 117 | |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 118 | buffer = PyUnicode_FromFormat("%s: line %i, column %i", |
| 119 | XML_ErrorString(code), lineno, column); |
| 120 | if (buffer == NULL) |
| 121 | return NULL; |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 122 | err = PyObject_CallFunctionObjArgs(ErrorObject, buffer, NULL); |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 123 | Py_DECREF(buffer); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 124 | if ( err != NULL |
| 125 | && set_error_attr(err, "code", code) |
| 126 | && set_error_attr(err, "offset", column) |
| 127 | && set_error_attr(err, "lineno", lineno)) { |
| 128 | PyErr_SetObject(ErrorObject, err); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 129 | } |
Neal Norwitz | 2f5e990 | 2006-03-08 06:36:45 +0000 | [diff] [blame] | 130 | Py_XDECREF(err); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 134 | static int |
| 135 | have_handler(xmlparseobject *self, int type) |
| 136 | { |
| 137 | PyObject *handler = self->handlers[type]; |
| 138 | return handler != NULL; |
| 139 | } |
| 140 | |
| 141 | static PyObject * |
| 142 | get_handler_name(struct HandlerInfo *hinfo) |
| 143 | { |
| 144 | PyObject *name = hinfo->nameobj; |
| 145 | if (name == NULL) { |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 146 | name = PyUnicode_FromString(hinfo->name); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 147 | hinfo->nameobj = name; |
| 148 | } |
| 149 | Py_XINCREF(name); |
| 150 | return name; |
| 151 | } |
| 152 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 153 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 154 | /* Convert a string of XML_Chars into a Unicode string. |
| 155 | Returns None if str is a null pointer. */ |
| 156 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 157 | static PyObject * |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 158 | conv_string_to_unicode(const XML_Char *str) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 159 | { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 160 | /* XXX currently this code assumes that XML_Char is 8-bit, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 161 | and hence in UTF-8. */ |
| 162 | /* UTF-8 from Expat, Unicode desired */ |
| 163 | if (str == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 164 | Py_RETURN_NONE; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 165 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 166 | return PyUnicode_DecodeUTF8(str, strlen(str), "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 169 | static PyObject * |
| 170 | conv_string_len_to_unicode(const XML_Char *str, int len) |
| 171 | { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 172 | /* XXX currently this code assumes that XML_Char is 8-bit, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 173 | and hence in UTF-8. */ |
| 174 | /* UTF-8 from Expat, Unicode desired */ |
| 175 | if (str == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 176 | Py_RETURN_NONE; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 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* |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 219 | call_with_frame(const 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 |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 708 | isfinal: bool(accept={int}) = False |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 709 | / |
| 710 | |
| 711 | Parse XML data. |
| 712 | |
| 713 | `isfinal' should be true at end of input. |
| 714 | [clinic start generated code]*/ |
| 715 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 716 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 717 | pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data, |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 718 | int isfinal) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 719 | /*[clinic end generated code: output=f4db843dd1f4ed4b input=eb616027bfa9847f]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 720 | { |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 721 | const char *s; |
| 722 | Py_ssize_t slen; |
| 723 | Py_buffer view; |
| 724 | int rc; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 725 | |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 726 | if (PyUnicode_Check(data)) { |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 727 | view.buf = NULL; |
Serhiy Storchaka | 36b365c | 2013-02-04 18:28:01 +0200 | [diff] [blame] | 728 | s = PyUnicode_AsUTF8AndSize(data, &slen); |
| 729 | if (s == NULL) |
| 730 | return NULL; |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 731 | /* Explicitly set UTF-8 encoding. Return code ignored. */ |
| 732 | (void)XML_SetEncoding(self->itself, "utf-8"); |
| 733 | } |
| 734 | else { |
| 735 | if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0) |
| 736 | return NULL; |
| 737 | s = view.buf; |
| 738 | slen = view.len; |
| 739 | } |
| 740 | |
| 741 | while (slen > MAX_CHUNK_SIZE) { |
| 742 | rc = XML_Parse(self->itself, s, MAX_CHUNK_SIZE, 0); |
| 743 | if (!rc) |
| 744 | goto done; |
| 745 | s += MAX_CHUNK_SIZE; |
| 746 | slen -= MAX_CHUNK_SIZE; |
| 747 | } |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 748 | Py_BUILD_ASSERT(MAX_CHUNK_SIZE <= INT_MAX); |
| 749 | assert(slen <= INT_MAX); |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 750 | rc = XML_Parse(self->itself, s, (int)slen, isfinal); |
Serhiy Storchaka | 43536e9 | 2013-02-04 18:26:15 +0200 | [diff] [blame] | 751 | |
| 752 | done: |
| 753 | if (view.buf != NULL) |
| 754 | PyBuffer_Release(&view); |
| 755 | return get_parse_result(self, rc); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 758 | /* File reading copied from cPickle */ |
| 759 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 760 | #define BUF_SIZE 2048 |
| 761 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 762 | static int |
| 763 | readinst(char *buf, int buf_size, PyObject *meth) |
| 764 | { |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 765 | PyObject *str; |
| 766 | Py_ssize_t len; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 767 | const char *ptr; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 768 | |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 769 | str = PyObject_CallFunction(meth, "n", buf_size); |
Martin v. Löwis | 9171f02 | 2004-10-13 19:50:11 +0000 | [diff] [blame] | 770 | if (str == NULL) |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 771 | goto error; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 772 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 773 | if (PyBytes_Check(str)) |
| 774 | ptr = PyBytes_AS_STRING(str); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 775 | else if (PyByteArray_Check(str)) |
| 776 | ptr = PyByteArray_AS_STRING(str); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 777 | else { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 778 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 779 | "read() did not return a bytes object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 780 | Py_TYPE(str)->tp_name); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 781 | goto error; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 782 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 783 | len = Py_SIZE(str); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 784 | if (len > buf_size) { |
| 785 | PyErr_Format(PyExc_ValueError, |
| 786 | "read() returned too much data: " |
Victor Stinner | 9d6f936 | 2011-01-04 22:00:04 +0000 | [diff] [blame] | 787 | "%i bytes requested, %zd returned", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 788 | buf_size, len); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 789 | goto error; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 790 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 791 | memcpy(buf, ptr, len); |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 792 | Py_DECREF(str); |
| 793 | /* len <= buf_size <= INT_MAX */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 794 | return (int)len; |
Victor Stinner | 95f1dfc | 2011-01-10 23:00:36 +0000 | [diff] [blame] | 795 | |
| 796 | error: |
| 797 | Py_XDECREF(str); |
| 798 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 801 | /*[clinic input] |
| 802 | pyexpat.xmlparser.ParseFile |
| 803 | |
| 804 | file: object |
| 805 | / |
| 806 | |
| 807 | Parse XML data from file-like object. |
| 808 | [clinic start generated code]*/ |
| 809 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 810 | static PyObject * |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 811 | pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 812 | /*[clinic end generated code: output=2adc6a13100cc42b input=fbb5a12b6038d735]*/ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 813 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 814 | int rv = 1; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 815 | PyObject *readmethod = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 816 | _Py_IDENTIFIER(read); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 817 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 818 | readmethod = _PyObject_GetAttrId(file, &PyId_read); |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 819 | if (readmethod == NULL) { |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 820 | PyErr_SetString(PyExc_TypeError, |
| 821 | "argument must have 'read' attribute"); |
| 822 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 823 | } |
| 824 | for (;;) { |
| 825 | int bytes_read; |
| 826 | void *buf = XML_GetBuffer(self->itself, BUF_SIZE); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 827 | if (buf == NULL) { |
Fred Drake | f239c6d | 2003-07-21 17:22:43 +0000 | [diff] [blame] | 828 | Py_XDECREF(readmethod); |
Ned Deily | e7d532f | 2014-03-27 16:39:58 -0700 | [diff] [blame] | 829 | return get_parse_result(self, 0); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 830 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 831 | |
Benjamin Peterson | 4e7f285 | 2010-08-08 16:54:58 +0000 | [diff] [blame] | 832 | bytes_read = readinst(buf, BUF_SIZE, readmethod); |
| 833 | if (bytes_read < 0) { |
| 834 | Py_DECREF(readmethod); |
| 835 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 836 | } |
| 837 | rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 838 | if (PyErr_Occurred()) { |
| 839 | Py_XDECREF(readmethod); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 840 | return NULL; |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 841 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 842 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 843 | if (!rv || bytes_read == 0) |
| 844 | break; |
| 845 | } |
Fred Drake | 7b6caff | 2003-07-21 17:05:56 +0000 | [diff] [blame] | 846 | Py_XDECREF(readmethod); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 847 | return get_parse_result(self, rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 850 | /*[clinic input] |
| 851 | pyexpat.xmlparser.SetBase |
| 852 | |
| 853 | base: str |
| 854 | / |
| 855 | |
| 856 | Set the base URL for the parser. |
| 857 | [clinic start generated code]*/ |
| 858 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 859 | static PyObject * |
| 860 | pyexpat_xmlparser_SetBase_impl(xmlparseobject *self, const char *base) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 861 | /*[clinic end generated code: output=c212ddceb607b539 input=c684e5de895ee1a8]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 862 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 863 | if (!XML_SetBase(self->itself, base)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 865 | } |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 866 | Py_RETURN_NONE; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 869 | /*[clinic input] |
| 870 | pyexpat.xmlparser.GetBase |
| 871 | |
| 872 | Return base URL string for the parser. |
| 873 | [clinic start generated code]*/ |
| 874 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 875 | static PyObject * |
| 876 | pyexpat_xmlparser_GetBase_impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 877 | /*[clinic end generated code: output=2886cb21f9a8739a input=918d71c38009620e]*/ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 878 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 879 | return Py_BuildValue("z", XML_GetBase(self->itself)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 882 | /*[clinic input] |
| 883 | pyexpat.xmlparser.GetInputContext |
| 884 | |
| 885 | Return the untranslated text of the input that caused the current event. |
| 886 | |
| 887 | If the event was generated by a large amount of text (such as a start tag |
| 888 | for an element with many attributes), not all of the text may be available. |
| 889 | [clinic start generated code]*/ |
| 890 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 891 | static PyObject * |
| 892 | pyexpat_xmlparser_GetInputContext_impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 893 | /*[clinic end generated code: output=a88026d683fc22cc input=034df8712db68379]*/ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 894 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 895 | if (self->in_callback) { |
| 896 | int offset, size; |
| 897 | const char *buffer |
| 898 | = XML_GetInputContext(self->itself, &offset, &size); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 899 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 900 | if (buffer != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 901 | return PyBytes_FromStringAndSize(buffer + offset, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 902 | size - offset); |
| 903 | else |
| 904 | Py_RETURN_NONE; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 905 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 906 | else |
| 907 | Py_RETURN_NONE; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 908 | } |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 909 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 910 | /*[clinic input] |
| 911 | pyexpat.xmlparser.ExternalEntityParserCreate |
| 912 | |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 913 | context: str(accept={str, NoneType}) |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 914 | encoding: str = NULL |
| 915 | / |
| 916 | |
| 917 | Create a parser for parsing an external entity based on the information passed to the ExternalEntityRefHandler. |
| 918 | [clinic start generated code]*/ |
| 919 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 920 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 921 | pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, |
| 922 | const char *context, |
| 923 | const char *encoding) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 924 | /*[clinic end generated code: output=535cda9d7a0fbcd6 input=b906714cc122c322]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 925 | { |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 926 | xmlparseobject *new_parser; |
| 927 | int i; |
| 928 | |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 929 | new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 930 | if (new_parser == NULL) |
| 931 | return NULL; |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 932 | new_parser->buffer_size = self->buffer_size; |
| 933 | new_parser->buffer_used = 0; |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 934 | new_parser->buffer = NULL; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 935 | new_parser->ordered_attributes = self->ordered_attributes; |
| 936 | new_parser->specified_attributes = self->specified_attributes; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 937 | new_parser->in_callback = 0; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 938 | new_parser->ns_prefixes = self->ns_prefixes; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 939 | new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | encoding); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 941 | new_parser->handlers = 0; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 942 | new_parser->intern = self->intern; |
| 943 | Py_XINCREF(new_parser->intern); |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 944 | PyObject_GC_Track(new_parser); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 945 | |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 946 | if (self->buffer != NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 947 | new_parser->buffer = PyMem_Malloc(new_parser->buffer_size); |
Victor Stinner | b4ba986 | 2010-09-10 22:25:19 +0000 | [diff] [blame] | 948 | if (new_parser->buffer == NULL) { |
| 949 | Py_DECREF(new_parser); |
| 950 | return PyErr_NoMemory(); |
| 951 | } |
| 952 | } |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 953 | if (!new_parser->itself) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 954 | Py_DECREF(new_parser); |
| 955 | return PyErr_NoMemory(); |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | XML_SetUserData(new_parser->itself, (void *)new_parser); |
| 959 | |
| 960 | /* allocate and clear handlers first */ |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 961 | for (i = 0; handler_info[i].name != NULL; i++) |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 962 | /* do nothing */; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 963 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 964 | new_parser->handlers = PyMem_New(PyObject *, i); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 965 | if (!new_parser->handlers) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 966 | Py_DECREF(new_parser); |
| 967 | return PyErr_NoMemory(); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 968 | } |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 969 | clear_handlers(new_parser, 1); |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 970 | |
| 971 | /* then copy handlers from self */ |
| 972 | for (i = 0; handler_info[i].name != NULL; i++) { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 973 | PyObject *handler = self->handlers[i]; |
| 974 | if (handler != NULL) { |
| 975 | Py_INCREF(handler); |
| 976 | new_parser->handlers[i] = handler; |
| 977 | handler_info[i].setter(new_parser->itself, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 978 | handler_info[i].handler); |
| 979 | } |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 980 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 981 | return (PyObject *)new_parser; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 984 | /*[clinic input] |
| 985 | pyexpat.xmlparser.SetParamEntityParsing |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 986 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 987 | flag: int |
| 988 | / |
| 989 | |
| 990 | Controls parsing of parameter entities (including the external DTD subset). |
| 991 | |
| 992 | Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER, |
| 993 | XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and |
| 994 | XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag |
| 995 | was successful. |
| 996 | [clinic start generated code]*/ |
| 997 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 998 | static PyObject * |
| 999 | pyexpat_xmlparser_SetParamEntityParsing_impl(xmlparseobject *self, int flag) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1000 | /*[clinic end generated code: output=18668ee8e760d64c input=8aea19b4b15e9af1]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1001 | { |
| 1002 | flag = XML_SetParamEntityParsing(self->itself, flag); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1003 | return PyLong_FromLong(flag); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1006 | |
| 1007 | #if XML_COMBINED_VERSION >= 19505 |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1008 | /*[clinic input] |
| 1009 | pyexpat.xmlparser.UseForeignDTD |
| 1010 | |
| 1011 | flag: bool = True |
| 1012 | / |
| 1013 | |
| 1014 | Allows the application to provide an artificial external subset if one is not specified as part of the document instance. |
| 1015 | |
| 1016 | This readily allows the use of a 'default' document type controlled by the |
| 1017 | application, while still getting the advantage of providing document type |
| 1018 | information to the parser. 'flag' defaults to True if not provided. |
| 1019 | [clinic start generated code]*/ |
| 1020 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1021 | static PyObject * |
| 1022 | pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, int flag) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1023 | /*[clinic end generated code: output=cfaa9aa50bb0f65c input=78144c519d116a6e]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1024 | { |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1025 | enum XML_Error rc; |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1026 | |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1027 | rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE); |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1028 | if (rc != XML_ERROR_NONE) { |
| 1029 | return set_error(self, rc); |
| 1030 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1031 | Py_RETURN_NONE; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1032 | } |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1033 | #endif |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1034 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1035 | /*[clinic input] |
| 1036 | pyexpat.xmlparser.__dir__ |
| 1037 | [clinic start generated code]*/ |
| 1038 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1039 | static PyObject * |
| 1040 | pyexpat_xmlparser___dir___impl(xmlparseobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1041 | /*[clinic end generated code: output=bc22451efb9e4d17 input=76aa455f2a661384]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1042 | { |
| 1043 | #define APPEND(list, str) \ |
| 1044 | do { \ |
| 1045 | PyObject *o = PyUnicode_FromString(str); \ |
| 1046 | if (o != NULL) \ |
| 1047 | PyList_Append(list, o); \ |
| 1048 | Py_XDECREF(o); \ |
| 1049 | } while (0) |
| 1050 | |
| 1051 | int i; |
| 1052 | PyObject *rc = PyList_New(0); |
| 1053 | if (!rc) |
| 1054 | return NULL; |
| 1055 | for (i = 0; handler_info[i].name != NULL; i++) { |
| 1056 | PyObject *o = get_handler_name(&handler_info[i]); |
| 1057 | if (o != NULL) |
| 1058 | PyList_Append(rc, o); |
| 1059 | Py_XDECREF(o); |
| 1060 | } |
| 1061 | APPEND(rc, "ErrorCode"); |
| 1062 | APPEND(rc, "ErrorLineNumber"); |
| 1063 | APPEND(rc, "ErrorColumnNumber"); |
| 1064 | APPEND(rc, "ErrorByteIndex"); |
| 1065 | APPEND(rc, "CurrentLineNumber"); |
| 1066 | APPEND(rc, "CurrentColumnNumber"); |
| 1067 | APPEND(rc, "CurrentByteIndex"); |
| 1068 | APPEND(rc, "buffer_size"); |
| 1069 | APPEND(rc, "buffer_text"); |
| 1070 | APPEND(rc, "buffer_used"); |
| 1071 | APPEND(rc, "namespace_prefixes"); |
| 1072 | APPEND(rc, "ordered_attributes"); |
| 1073 | APPEND(rc, "specified_attributes"); |
| 1074 | APPEND(rc, "intern"); |
| 1075 | |
| 1076 | #undef APPEND |
| 1077 | |
| 1078 | if (PyErr_Occurred()) { |
| 1079 | Py_DECREF(rc); |
| 1080 | rc = NULL; |
| 1081 | } |
| 1082 | |
| 1083 | return rc; |
| 1084 | } |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1085 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1086 | static struct PyMethodDef xmlparse_methods[] = { |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1087 | PYEXPAT_XMLPARSER_PARSE_METHODDEF |
| 1088 | PYEXPAT_XMLPARSER_PARSEFILE_METHODDEF |
| 1089 | PYEXPAT_XMLPARSER_SETBASE_METHODDEF |
| 1090 | PYEXPAT_XMLPARSER_GETBASE_METHODDEF |
| 1091 | PYEXPAT_XMLPARSER_GETINPUTCONTEXT_METHODDEF |
| 1092 | PYEXPAT_XMLPARSER_EXTERNALENTITYPARSERCREATE_METHODDEF |
| 1093 | PYEXPAT_XMLPARSER_SETPARAMENTITYPARSING_METHODDEF |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1094 | #if XML_COMBINED_VERSION >= 19505 |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1095 | PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1096 | #endif |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1097 | PYEXPAT_XMLPARSER___DIR___METHODDEF |
| 1098 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1099 | }; |
| 1100 | |
| 1101 | /* ---------- */ |
| 1102 | |
| 1103 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1104 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1105 | /* pyexpat international encoding support. |
| 1106 | Make it as simple as possible. |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1107 | */ |
| 1108 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1109 | static int |
| 1110 | PyUnknownEncodingHandler(void *encodingHandlerData, |
| 1111 | const XML_Char *name, |
| 1112 | XML_Encoding *info) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1113 | { |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1114 | static unsigned char template_buffer[256] = {0}; |
| 1115 | PyObject* u; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1116 | int i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 | void *data; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1118 | unsigned int kind; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1119 | |
Victor Stinner | 9e09c26 | 2013-07-18 23:17:01 +0200 | [diff] [blame] | 1120 | if (PyErr_Occurred()) |
| 1121 | return XML_STATUS_ERROR; |
| 1122 | |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1123 | if (template_buffer[1] == 0) { |
| 1124 | for (i = 0; i < 256; i++) |
| 1125 | template_buffer[i] = i; |
Tim Peters | 63cb99e | 2001-02-17 18:12:50 +0000 | [diff] [blame] | 1126 | } |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1127 | |
| 1128 | u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace"); |
Christian Heimes | b582155 | 2013-06-29 20:43:13 +0200 | [diff] [blame] | 1129 | if (u == NULL || PyUnicode_READY(u)) { |
Christian Heimes | 7217242 | 2013-06-29 21:49:27 +0200 | [diff] [blame] | 1130 | Py_XDECREF(u); |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1131 | return XML_STATUS_ERROR; |
Christian Heimes | b582155 | 2013-06-29 20:43:13 +0200 | [diff] [blame] | 1132 | } |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1133 | |
| 1134 | if (PyUnicode_GET_LENGTH(u) != 256) { |
| 1135 | Py_DECREF(u); |
| 1136 | PyErr_SetString(PyExc_ValueError, |
| 1137 | "multi-byte encodings are not supported"); |
| 1138 | return XML_STATUS_ERROR; |
| 1139 | } |
| 1140 | |
| 1141 | kind = PyUnicode_KIND(u); |
| 1142 | data = PyUnicode_DATA(u); |
| 1143 | for (i = 0; i < 256; i++) { |
| 1144 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 1145 | if (ch != Py_UNICODE_REPLACEMENT_CHARACTER) |
| 1146 | info->map[i] = ch; |
| 1147 | else |
| 1148 | info->map[i] = -1; |
| 1149 | } |
| 1150 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1151 | info->data = NULL; |
| 1152 | info->convert = NULL; |
| 1153 | info->release = NULL; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1154 | Py_DECREF(u); |
| 1155 | |
| 1156 | return XML_STATUS_OK; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1159 | |
| 1160 | static PyObject * |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1161 | newxmlparseobject(const char *encoding, const char *namespace_separator, PyObject *intern) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1162 | { |
| 1163 | int i; |
| 1164 | xmlparseobject *self; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1165 | |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1166 | self = PyObject_GC_New(xmlparseobject, &Xmlparsetype); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1167 | if (self == NULL) |
| 1168 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1169 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1170 | self->buffer = NULL; |
| 1171 | self->buffer_size = CHARACTER_DATA_BUFFER_SIZE; |
| 1172 | self->buffer_used = 0; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1173 | self->ordered_attributes = 0; |
| 1174 | self->specified_attributes = 0; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1175 | self->in_callback = 0; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1176 | self->ns_prefixes = 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1177 | self->handlers = NULL; |
Victor Stinner | 54b2d2e | 2013-07-15 17:15:57 +0200 | [diff] [blame] | 1178 | self->intern = intern; |
| 1179 | Py_XINCREF(self->intern); |
| 1180 | PyObject_GC_Track(self); |
| 1181 | |
Christian Heimes | fa535f5 | 2013-07-07 17:35:11 +0200 | [diff] [blame] | 1182 | /* namespace_separator is either NULL or contains one char + \0 */ |
| 1183 | self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler, |
| 1184 | namespace_separator); |
Victor Stinner | 54b2d2e | 2013-07-15 17:15:57 +0200 | [diff] [blame] | 1185 | if (self->itself == NULL) { |
| 1186 | PyErr_SetString(PyExc_RuntimeError, |
| 1187 | "XML_ParserCreate failed"); |
| 1188 | Py_DECREF(self); |
| 1189 | return NULL; |
| 1190 | } |
Victor Stinner | 23ec4b5 | 2017-06-15 00:54:36 +0200 | [diff] [blame] | 1191 | #if XML_COMBINED_VERSION >= 20100 |
| 1192 | /* This feature was added upstream in libexpat 2.1.0. */ |
Gregory P. Smith | 8e91cf6 | 2012-03-14 14:26:55 -0700 | [diff] [blame] | 1193 | XML_SetHashSalt(self->itself, |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 1194 | (unsigned long)_Py_HashSecret.expat.hashsalt); |
Gregory P. Smith | 2522771 | 2012-03-14 18:10:37 -0700 | [diff] [blame] | 1195 | #endif |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1196 | XML_SetUserData(self->itself, (void *)self); |
Fred Drake | 7c75bf2 | 2002-07-01 14:02:31 +0000 | [diff] [blame] | 1197 | XML_SetUnknownEncodingHandler(self->itself, |
| 1198 | (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1199 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1200 | for (i = 0; handler_info[i].name != NULL; i++) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1201 | /* do nothing */; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1202 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1203 | self->handlers = PyMem_New(PyObject *, i); |
Fred Drake | 7c75bf2 | 2002-07-01 14:02:31 +0000 | [diff] [blame] | 1204 | if (!self->handlers) { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1205 | Py_DECREF(self); |
| 1206 | return PyErr_NoMemory(); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1207 | } |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1208 | clear_handlers(self, 1); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1209 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1210 | return (PyObject*)self; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | static void |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1215 | xmlparse_dealloc(xmlparseobject *self) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1216 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1217 | int i; |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1218 | PyObject_GC_UnTrack(self); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1219 | if (self->itself != NULL) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1220 | XML_ParserFree(self->itself); |
| 1221 | self->itself = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1222 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1223 | if (self->handlers != NULL) { |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 1224 | for (i = 0; handler_info[i].name != NULL; i++) |
| 1225 | Py_CLEAR(self->handlers[i]); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1226 | PyMem_Free(self->handlers); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1227 | self->handlers = NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1228 | } |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1229 | if (self->buffer != NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1230 | PyMem_Free(self->buffer); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1231 | self->buffer = NULL; |
| 1232 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1233 | Py_XDECREF(self->intern); |
Martin v. Löwis | 894258c | 2001-09-23 10:20:10 +0000 | [diff] [blame] | 1234 | PyObject_GC_Del(self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1237 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1238 | handlername2int(PyObject *name) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1239 | { |
| 1240 | int i; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1241 | for (i = 0; handler_info[i].name != NULL; i++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1242 | if (_PyUnicode_EqualToASCIIString(name, handler_info[i].name)) { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1243 | return i; |
| 1244 | } |
| 1245 | } |
| 1246 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | static PyObject * |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1250 | get_pybool(int istrue) |
| 1251 | { |
| 1252 | PyObject *result = istrue ? Py_True : Py_False; |
| 1253 | Py_INCREF(result); |
| 1254 | return result; |
| 1255 | } |
| 1256 | |
| 1257 | static PyObject * |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1258 | xmlparse_getattro(xmlparseobject *self, PyObject *nameobj) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1259 | { |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1260 | Py_UCS4 first_char; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1261 | int handlernum = -1; |
| 1262 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1263 | if (!PyUnicode_Check(nameobj)) |
| 1264 | goto generic; |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1265 | if (PyUnicode_READY(nameobj)) |
| 1266 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1268 | handlernum = handlername2int(nameobj); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (handlernum != -1) { |
| 1271 | PyObject *result = self->handlers[handlernum]; |
| 1272 | if (result == NULL) |
| 1273 | result = Py_None; |
| 1274 | Py_INCREF(result); |
| 1275 | return result; |
| 1276 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1277 | |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1278 | first_char = PyUnicode_READ_CHAR(nameobj, 0); |
| 1279 | if (first_char == 'E') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1280 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorCode")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1281 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1282 | XML_GetErrorCode(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1283 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorLineNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1284 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1285 | XML_GetErrorLineNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1286 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorColumnNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1287 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1288 | XML_GetErrorColumnNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1289 | if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorByteIndex")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1290 | return PyLong_FromLong((long) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1291 | XML_GetErrorByteIndex(self->itself)); |
| 1292 | } |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1293 | if (first_char == 'C') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1294 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentLineNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1295 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1296 | XML_GetCurrentLineNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1297 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentColumnNumber")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1298 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1299 | XML_GetCurrentColumnNumber(self->itself)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1300 | if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentByteIndex")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1301 | return PyLong_FromLong((long) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 1302 | XML_GetCurrentByteIndex(self->itself)); |
| 1303 | } |
Victor Stinner | 9e5bd6c | 2011-10-01 01:05:40 +0200 | [diff] [blame] | 1304 | if (first_char == 'b') { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1305 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_size")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1306 | return PyLong_FromLong((long) self->buffer_size); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1307 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_text")) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1308 | return get_pybool(self->buffer != NULL); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1309 | if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_used")) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1310 | return PyLong_FromLong((long) self->buffer_used); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1311 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1312 | if (_PyUnicode_EqualToASCIIString(nameobj, "namespace_prefixes")) |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1313 | return get_pybool(self->ns_prefixes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1314 | if (_PyUnicode_EqualToASCIIString(nameobj, "ordered_attributes")) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1315 | return get_pybool(self->ordered_attributes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1316 | if (_PyUnicode_EqualToASCIIString(nameobj, "specified_attributes")) |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1317 | return get_pybool((long) self->specified_attributes); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1318 | if (_PyUnicode_EqualToASCIIString(nameobj, "intern")) { |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1319 | if (self->intern == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1320 | Py_RETURN_NONE; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1321 | } |
| 1322 | else { |
| 1323 | Py_INCREF(self->intern); |
| 1324 | return self->intern; |
| 1325 | } |
| 1326 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1327 | generic: |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1328 | return PyObject_GenericGetAttr((PyObject*)self, nameobj); |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1331 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1332 | sethandler(xmlparseobject *self, PyObject *name, PyObject* v) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int handlernum = handlername2int(name); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1335 | if (handlernum >= 0) { |
| 1336 | xmlhandler c_handler = NULL; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1337 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1338 | if (v == Py_None) { |
| 1339 | /* If this is the character data handler, and a character |
| 1340 | data handler is already active, we need to be more |
| 1341 | careful. What we can safely do is replace the existing |
| 1342 | character data handler callback function with a no-op |
| 1343 | function that will refuse to call Python. The downside |
| 1344 | is that this doesn't completely remove the character |
| 1345 | data handler from the C layer if there's any callback |
| 1346 | active, so Expat does a little more work than it |
| 1347 | otherwise would, but that's really an odd case. A more |
| 1348 | elaborate system of handlers and state could remove the |
| 1349 | C handler more effectively. */ |
| 1350 | if (handlernum == CharacterData && self->in_callback) |
| 1351 | c_handler = noop_character_data_handler; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1352 | v = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1353 | } |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1354 | else if (v != NULL) { |
| 1355 | Py_INCREF(v); |
| 1356 | c_handler = handler_info[handlernum].handler; |
| 1357 | } |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 1358 | Py_XSETREF(self->handlers[handlernum], v); |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1359 | handler_info[handlernum].setter(self->itself, c_handler); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1360 | return 1; |
| 1361 | } |
| 1362 | return 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static int |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1366 | xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1367 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1368 | /* Set attribute 'name' to value 'v'. v==NULL means delete */ |
Serhiy Storchaka | 931331a | 2015-09-07 22:37:02 +0300 | [diff] [blame] | 1369 | if (!PyUnicode_Check(name)) { |
| 1370 | PyErr_Format(PyExc_TypeError, |
| 1371 | "attribute name must be string, not '%.200s'", |
| 1372 | name->ob_type->tp_name); |
| 1373 | return -1; |
| 1374 | } |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1375 | if (v == NULL) { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1376 | PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute"); |
| 1377 | return -1; |
| 1378 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1379 | if (_PyUnicode_EqualToASCIIString(name, "buffer_text")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1380 | int b = PyObject_IsTrue(v); |
| 1381 | if (b < 0) |
| 1382 | return -1; |
| 1383 | if (b) { |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1384 | if (self->buffer == NULL) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1385 | self->buffer = PyMem_Malloc(self->buffer_size); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1386 | if (self->buffer == NULL) { |
| 1387 | PyErr_NoMemory(); |
| 1388 | return -1; |
| 1389 | } |
| 1390 | self->buffer_used = 0; |
| 1391 | } |
| 1392 | } |
| 1393 | else if (self->buffer != NULL) { |
| 1394 | if (flush_character_buffer(self) < 0) |
| 1395 | return -1; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1396 | PyMem_Free(self->buffer); |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1397 | self->buffer = NULL; |
| 1398 | } |
| 1399 | return 0; |
| 1400 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1401 | if (_PyUnicode_EqualToASCIIString(name, "namespace_prefixes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1402 | int b = PyObject_IsTrue(v); |
| 1403 | if (b < 0) |
| 1404 | return -1; |
| 1405 | self->ns_prefixes = b; |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1406 | XML_SetReturnNSTriplet(self->itself, self->ns_prefixes); |
| 1407 | return 0; |
| 1408 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1409 | if (_PyUnicode_EqualToASCIIString(name, "ordered_attributes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1410 | int b = PyObject_IsTrue(v); |
| 1411 | if (b < 0) |
| 1412 | return -1; |
| 1413 | self->ordered_attributes = b; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1414 | return 0; |
| 1415 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1416 | if (_PyUnicode_EqualToASCIIString(name, "specified_attributes")) { |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1417 | int b = PyObject_IsTrue(v); |
| 1418 | if (b < 0) |
| 1419 | return -1; |
| 1420 | self->specified_attributes = b; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1423 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1424 | if (_PyUnicode_EqualToASCIIString(name, "buffer_size")) { |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1425 | long new_buffer_size; |
| 1426 | if (!PyLong_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer"); |
| 1428 | return -1; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Serhiy Storchaka | de5f9f4 | 2015-09-07 22:51:56 +0300 | [diff] [blame] | 1431 | new_buffer_size = PyLong_AsLong(v); |
| 1432 | if (new_buffer_size <= 0) { |
| 1433 | if (!PyErr_Occurred()) |
| 1434 | PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero"); |
| 1435 | return -1; |
| 1436 | } |
| 1437 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1438 | /* trivial case -- no change */ |
| 1439 | if (new_buffer_size == self->buffer_size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | return 0; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1443 | /* check maximum */ |
| 1444 | if (new_buffer_size > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | char errmsg[100]; |
| 1446 | sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX); |
| 1447 | PyErr_SetString(PyExc_ValueError, errmsg); |
| 1448 | return -1; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | if (self->buffer != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | /* there is already a buffer */ |
| 1453 | if (self->buffer_used != 0) { |
Christian Heimes | 09994a9 | 2013-07-20 22:41:58 +0200 | [diff] [blame] | 1454 | if (flush_character_buffer(self) < 0) { |
| 1455 | return -1; |
| 1456 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | } |
| 1458 | /* free existing buffer */ |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1459 | PyMem_Free(self->buffer); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1460 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1461 | self->buffer = PyMem_Malloc(new_buffer_size); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1462 | if (self->buffer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | PyErr_NoMemory(); |
| 1464 | return -1; |
| 1465 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1466 | self->buffer_size = new_buffer_size; |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1470 | if (_PyUnicode_EqualToASCIIString(name, "CharacterDataHandler")) { |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1471 | /* If we're changing the character data handler, flush all |
| 1472 | * cached data with the old handler. Not sure there's a |
| 1473 | * "right" thing to do, though, but this probably won't |
| 1474 | * happen. |
| 1475 | */ |
| 1476 | if (flush_character_buffer(self) < 0) |
| 1477 | return -1; |
| 1478 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1479 | if (sethandler(self, name, v)) { |
| 1480 | return 0; |
| 1481 | } |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1482 | PyErr_SetObject(PyExc_AttributeError, name); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1483 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1486 | static int |
| 1487 | xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg) |
| 1488 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1489 | int i; |
| 1490 | for (i = 0; handler_info[i].name != NULL; i++) |
| 1491 | Py_VISIT(op->handlers[i]); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1492 | return 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static int |
| 1496 | xmlparse_clear(xmlparseobject *op) |
| 1497 | { |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1498 | clear_handlers(op, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1499 | Py_CLEAR(op->intern); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1500 | return 0; |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1501 | } |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1502 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1503 | PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser"); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1504 | |
| 1505 | static PyTypeObject Xmlparsetype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1507 | "pyexpat.xmlparser", /*tp_name*/ |
Antoine Pitrou | 23683ef | 2011-01-04 00:00:31 +0000 | [diff] [blame] | 1508 | sizeof(xmlparseobject), /*tp_basicsize*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | 0, /*tp_itemsize*/ |
| 1510 | /* methods */ |
| 1511 | (destructor)xmlparse_dealloc, /*tp_dealloc*/ |
| 1512 | (printfunc)0, /*tp_print*/ |
| 1513 | 0, /*tp_getattr*/ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1514 | 0, /*tp_setattr*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | 0, /*tp_reserved*/ |
| 1516 | (reprfunc)0, /*tp_repr*/ |
| 1517 | 0, /*tp_as_number*/ |
| 1518 | 0, /*tp_as_sequence*/ |
| 1519 | 0, /*tp_as_mapping*/ |
| 1520 | (hashfunc)0, /*tp_hash*/ |
| 1521 | (ternaryfunc)0, /*tp_call*/ |
| 1522 | (reprfunc)0, /*tp_str*/ |
| 1523 | (getattrofunc)xmlparse_getattro, /* tp_getattro */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1524 | (setattrofunc)xmlparse_setattro, /* tp_setattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | 0, /* tp_as_buffer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | Xmlparsetype__doc__, /* tp_doc - Documentation string */ |
| 1528 | (traverseproc)xmlparse_traverse, /* tp_traverse */ |
| 1529 | (inquiry)xmlparse_clear, /* tp_clear */ |
| 1530 | 0, /* tp_richcompare */ |
| 1531 | 0, /* tp_weaklistoffset */ |
| 1532 | 0, /* tp_iter */ |
| 1533 | 0, /* tp_iternext */ |
| 1534 | xmlparse_methods, /* tp_methods */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1535 | }; |
| 1536 | |
| 1537 | /* End of code for xmlparser objects */ |
| 1538 | /* -------------------------------------------------------- */ |
| 1539 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1540 | /*[clinic input] |
| 1541 | pyexpat.ParserCreate |
| 1542 | |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 1543 | encoding: str(accept={str, NoneType}) = NULL |
| 1544 | namespace_separator: str(accept={str, NoneType}) = NULL |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1545 | intern: object = NULL |
| 1546 | |
| 1547 | Return a new XML parser object. |
| 1548 | [clinic start generated code]*/ |
| 1549 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1550 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1551 | pyexpat_ParserCreate_impl(PyObject *module, const char *encoding, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1552 | const char *namespace_separator, PyObject *intern) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1553 | /*[clinic end generated code: output=295c0cf01ab1146c input=23d29704acad385d]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1554 | { |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1555 | PyObject *result; |
| 1556 | int intern_decref = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1557 | |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1558 | if (namespace_separator != NULL |
| 1559 | && strlen(namespace_separator) > 1) { |
| 1560 | PyErr_SetString(PyExc_ValueError, |
| 1561 | "namespace_separator must be at most one" |
| 1562 | " character, omitted, or None"); |
| 1563 | return NULL; |
| 1564 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1565 | /* Explicitly passing None means no interning is desired. |
| 1566 | Not passing anything means that a new dictionary is used. */ |
| 1567 | if (intern == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | intern = NULL; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1569 | else if (intern == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | intern = PyDict_New(); |
| 1571 | if (!intern) |
| 1572 | return NULL; |
| 1573 | intern_decref = 1; |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1574 | } |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1575 | else if (!PyDict_Check(intern)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | PyErr_SetString(PyExc_TypeError, "intern must be a dictionary"); |
| 1577 | return NULL; |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1580 | result = newxmlparseobject(encoding, namespace_separator, intern); |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1581 | if (intern_decref) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | Py_DECREF(intern); |
Fred Drake | b91a36b | 2002-06-27 19:40:48 +0000 | [diff] [blame] | 1583 | } |
| 1584 | return result; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1587 | /*[clinic input] |
| 1588 | pyexpat.ErrorString |
| 1589 | |
| 1590 | code: long |
| 1591 | / |
| 1592 | |
| 1593 | Returns string error for given number. |
| 1594 | [clinic start generated code]*/ |
| 1595 | |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1596 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1597 | pyexpat_ErrorString_impl(PyObject *module, long code) |
| 1598 | /*[clinic end generated code: output=2feae50d166f2174 input=cc67de010d9e62b3]*/ |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1599 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1600 | return Py_BuildValue("z", XML_ErrorString((int)code)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* List of methods defined in the module */ |
| 1604 | |
| 1605 | static struct PyMethodDef pyexpat_methods[] = { |
Brett Cannon | d0aeda8 | 2014-08-22 14:23:20 -0400 | [diff] [blame] | 1606 | PYEXPAT_PARSERCREATE_METHODDEF |
| 1607 | PYEXPAT_ERRORSTRING_METHODDEF |
| 1608 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1609 | }; |
| 1610 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 1611 | /* Module docstring */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1612 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1613 | PyDoc_STRVAR(pyexpat_module_documentation, |
| 1614 | "Python wrapper for Expat parser."); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1615 | |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1616 | /* Initialization function for the module */ |
| 1617 | |
| 1618 | #ifndef MODULE_NAME |
| 1619 | #define MODULE_NAME "pyexpat" |
| 1620 | #endif |
| 1621 | |
| 1622 | #ifndef MODULE_INITFUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1623 | #define MODULE_INITFUNC PyInit_pyexpat |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1624 | #endif |
| 1625 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1626 | static struct PyModuleDef pyexpatmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | PyModuleDef_HEAD_INIT, |
| 1628 | MODULE_NAME, |
| 1629 | pyexpat_module_documentation, |
| 1630 | -1, |
| 1631 | pyexpat_methods, |
| 1632 | NULL, |
| 1633 | NULL, |
| 1634 | NULL, |
| 1635 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1636 | }; |
| 1637 | |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1638 | PyMODINIT_FUNC |
| 1639 | MODULE_INITFUNC(void) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1640 | { |
| 1641 | PyObject *m, *d; |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 1642 | PyObject *errmod_name = PyUnicode_FromString(MODULE_NAME ".errors"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1643 | PyObject *errors_module; |
| 1644 | PyObject *modelmod_name; |
| 1645 | PyObject *model_module; |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1646 | PyObject *tmpnum, *tmpstr; |
| 1647 | PyObject *codes_dict; |
| 1648 | PyObject *rev_codes_dict; |
| 1649 | int res; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1650 | static struct PyExpat_CAPI capi; |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1651 | PyObject *capi_object; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1652 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1653 | if (errmod_name == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1654 | return NULL; |
Neal Norwitz | 392c5be | 2007-08-25 17:20:32 +0000 | [diff] [blame] | 1655 | modelmod_name = PyUnicode_FromString(MODULE_NAME ".model"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1656 | if (modelmod_name == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1657 | return NULL; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1658 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1659 | if (PyType_Ready(&Xmlparsetype) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1661 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1662 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1663 | m = PyModule_Create(&pyexpatmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1664 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1666 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1667 | /* Add some symbolic constants to the module */ |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1668 | if (ErrorObject == NULL) { |
| 1669 | ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError", |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1670 | NULL, NULL); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1671 | if (ErrorObject == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1672 | return NULL; |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1673 | } |
| 1674 | Py_INCREF(ErrorObject); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1675 | PyModule_AddObject(m, "error", ErrorObject); |
Fred Drake | bd6101c | 2001-02-14 18:29:45 +0000 | [diff] [blame] | 1676 | Py_INCREF(ErrorObject); |
| 1677 | PyModule_AddObject(m, "ExpatError", ErrorObject); |
Fred Drake | 4ba298c | 2000-10-29 04:57:53 +0000 | [diff] [blame] | 1678 | Py_INCREF(&Xmlparsetype); |
| 1679 | PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1680 | |
Fred Drake | 738293d | 2000-12-21 17:25:07 +0000 | [diff] [blame] | 1681 | PyModule_AddStringConstant(m, "EXPAT_VERSION", |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1682 | XML_ExpatVersion()); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1683 | { |
| 1684 | XML_Expat_Version info = XML_ExpatVersionInfo(); |
| 1685 | PyModule_AddObject(m, "version_info", |
| 1686 | Py_BuildValue("(iii)", info.major, |
| 1687 | info.minor, info.micro)); |
| 1688 | } |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1689 | /* XXX When Expat supports some way of figuring out how it was |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1690 | compiled, this should check and set native_encoding |
| 1691 | appropriately. |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1692 | */ |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1693 | PyModule_AddStringConstant(m, "native_encoding", "UTF-8"); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 1694 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1695 | d = PyModule_GetDict(m); |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1696 | if (d == NULL) { |
| 1697 | Py_DECREF(m); |
| 1698 | return NULL; |
| 1699 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1700 | errors_module = PyDict_GetItem(d, errmod_name); |
| 1701 | if (errors_module == NULL) { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1702 | errors_module = PyModule_New(MODULE_NAME ".errors"); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1703 | if (errors_module != NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1704 | _PyImport_SetModule(errmod_name, errors_module); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1705 | /* gives away the reference to errors_module */ |
| 1706 | PyModule_AddObject(m, "errors", errors_module); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1709 | Py_DECREF(errmod_name); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1710 | model_module = PyDict_GetItem(d, modelmod_name); |
| 1711 | if (model_module == NULL) { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1712 | model_module = PyModule_New(MODULE_NAME ".model"); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1713 | if (model_module != NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1714 | _PyImport_SetModule(modelmod_name, model_module); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1715 | /* gives away the reference to model_module */ |
| 1716 | PyModule_AddObject(m, "model", model_module); |
| 1717 | } |
| 1718 | } |
| 1719 | Py_DECREF(modelmod_name); |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1720 | if (errors_module == NULL || model_module == NULL) { |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1721 | /* Don't core dump later! */ |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1722 | Py_DECREF(m); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1723 | return NULL; |
Christian Heimes | 7a5457b | 2016-09-09 00:13:35 +0200 | [diff] [blame] | 1724 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1726 | #if XML_COMBINED_VERSION > 19505 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1727 | { |
| 1728 | const XML_Feature *features = XML_GetFeatureList(); |
| 1729 | PyObject *list = PyList_New(0); |
| 1730 | if (list == NULL) |
| 1731 | /* just ignore it */ |
| 1732 | PyErr_Clear(); |
| 1733 | else { |
| 1734 | int i = 0; |
| 1735 | for (; features[i].feature != XML_FEATURE_END; ++i) { |
| 1736 | int ok; |
| 1737 | PyObject *item = Py_BuildValue("si", features[i].name, |
| 1738 | features[i].value); |
| 1739 | if (item == NULL) { |
| 1740 | Py_DECREF(list); |
| 1741 | list = NULL; |
| 1742 | break; |
| 1743 | } |
| 1744 | ok = PyList_Append(list, item); |
| 1745 | Py_DECREF(item); |
| 1746 | if (ok < 0) { |
| 1747 | PyErr_Clear(); |
| 1748 | break; |
| 1749 | } |
| 1750 | } |
| 1751 | if (list != NULL) |
| 1752 | PyModule_AddObject(m, "features", list); |
| 1753 | } |
| 1754 | } |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1755 | #endif |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1756 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1757 | codes_dict = PyDict_New(); |
| 1758 | rev_codes_dict = PyDict_New(); |
| 1759 | if (codes_dict == NULL || rev_codes_dict == NULL) { |
| 1760 | Py_XDECREF(codes_dict); |
| 1761 | Py_XDECREF(rev_codes_dict); |
| 1762 | return NULL; |
| 1763 | } |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1764 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1765 | #define MYCONST(name) \ |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1766 | if (PyModule_AddStringConstant(errors_module, #name, \ |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 1767 | XML_ErrorString(name)) < 0) \ |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1768 | return NULL; \ |
| 1769 | tmpnum = PyLong_FromLong(name); \ |
| 1770 | if (tmpnum == NULL) return NULL; \ |
| 1771 | res = PyDict_SetItemString(codes_dict, \ |
| 1772 | XML_ErrorString(name), tmpnum); \ |
| 1773 | if (res < 0) return NULL; \ |
| 1774 | tmpstr = PyUnicode_FromString(XML_ErrorString(name)); \ |
| 1775 | if (tmpstr == NULL) return NULL; \ |
| 1776 | res = PyDict_SetItem(rev_codes_dict, tmpnum, tmpstr); \ |
| 1777 | Py_DECREF(tmpstr); \ |
| 1778 | Py_DECREF(tmpnum); \ |
| 1779 | if (res < 0) return NULL; \ |
Fred Drake | 7bd9f41 | 2000-07-04 23:51:31 +0000 | [diff] [blame] | 1780 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1781 | MYCONST(XML_ERROR_NO_MEMORY); |
| 1782 | MYCONST(XML_ERROR_SYNTAX); |
| 1783 | MYCONST(XML_ERROR_NO_ELEMENTS); |
| 1784 | MYCONST(XML_ERROR_INVALID_TOKEN); |
| 1785 | MYCONST(XML_ERROR_UNCLOSED_TOKEN); |
| 1786 | MYCONST(XML_ERROR_PARTIAL_CHAR); |
| 1787 | MYCONST(XML_ERROR_TAG_MISMATCH); |
| 1788 | MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE); |
| 1789 | MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT); |
| 1790 | MYCONST(XML_ERROR_PARAM_ENTITY_REF); |
| 1791 | MYCONST(XML_ERROR_UNDEFINED_ENTITY); |
| 1792 | MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF); |
| 1793 | MYCONST(XML_ERROR_ASYNC_ENTITY); |
| 1794 | MYCONST(XML_ERROR_BAD_CHAR_REF); |
| 1795 | MYCONST(XML_ERROR_BINARY_ENTITY_REF); |
| 1796 | MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF); |
| 1797 | MYCONST(XML_ERROR_MISPLACED_XML_PI); |
| 1798 | MYCONST(XML_ERROR_UNKNOWN_ENCODING); |
| 1799 | MYCONST(XML_ERROR_INCORRECT_ENCODING); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1800 | MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION); |
| 1801 | MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING); |
| 1802 | MYCONST(XML_ERROR_NOT_STANDALONE); |
Fred Drake | 283b670 | 2004-08-04 22:28:16 +0000 | [diff] [blame] | 1803 | MYCONST(XML_ERROR_UNEXPECTED_STATE); |
| 1804 | MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE); |
| 1805 | MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD); |
| 1806 | MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING); |
| 1807 | /* Added in Expat 1.95.7. */ |
| 1808 | MYCONST(XML_ERROR_UNBOUND_PREFIX); |
| 1809 | /* Added in Expat 1.95.8. */ |
| 1810 | MYCONST(XML_ERROR_UNDECLARING_PREFIX); |
| 1811 | MYCONST(XML_ERROR_INCOMPLETE_PE); |
| 1812 | MYCONST(XML_ERROR_XML_DECL); |
| 1813 | MYCONST(XML_ERROR_TEXT_DECL); |
| 1814 | MYCONST(XML_ERROR_PUBLICID); |
| 1815 | MYCONST(XML_ERROR_SUSPENDED); |
| 1816 | MYCONST(XML_ERROR_NOT_SUSPENDED); |
| 1817 | MYCONST(XML_ERROR_ABORTED); |
| 1818 | MYCONST(XML_ERROR_FINISHED); |
| 1819 | MYCONST(XML_ERROR_SUSPEND_PE); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1820 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1821 | if (PyModule_AddStringConstant(errors_module, "__doc__", |
| 1822 | "Constants used to describe " |
| 1823 | "error conditions.") < 0) |
| 1824 | return NULL; |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1825 | |
Georg Brandl | b4dac71 | 2010-10-15 14:46:48 +0000 | [diff] [blame] | 1826 | if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0) |
| 1827 | return NULL; |
| 1828 | if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0) |
| 1829 | return NULL; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1830 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 1831 | #undef MYCONST |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1832 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1833 | #define MYCONST(c) PyModule_AddIntConstant(m, #c, c) |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1834 | MYCONST(XML_PARAM_ENTITY_PARSING_NEVER); |
| 1835 | MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); |
| 1836 | MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS); |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1837 | #undef MYCONST |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1838 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1839 | #define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c) |
| 1840 | PyModule_AddStringConstant(model_module, "__doc__", |
| 1841 | "Constants used to interpret content model information."); |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1842 | |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1843 | MYCONST(XML_CTYPE_EMPTY); |
| 1844 | MYCONST(XML_CTYPE_ANY); |
| 1845 | MYCONST(XML_CTYPE_MIXED); |
| 1846 | MYCONST(XML_CTYPE_NAME); |
| 1847 | MYCONST(XML_CTYPE_CHOICE); |
| 1848 | MYCONST(XML_CTYPE_SEQ); |
| 1849 | |
| 1850 | MYCONST(XML_CQUANT_NONE); |
| 1851 | MYCONST(XML_CQUANT_OPT); |
| 1852 | MYCONST(XML_CQUANT_REP); |
| 1853 | MYCONST(XML_CQUANT_PLUS); |
| 1854 | #undef MYCONST |
Fredrik Lundh | c334504 | 2005-12-13 19:49:55 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* initialize pyexpat dispatch table */ |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1857 | capi.size = sizeof(capi); |
Fredrik Lundh | cc117db | 2005-12-13 21:55:36 +0000 | [diff] [blame] | 1858 | capi.magic = PyExpat_CAPI_MAGIC; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1859 | capi.MAJOR_VERSION = XML_MAJOR_VERSION; |
| 1860 | capi.MINOR_VERSION = XML_MINOR_VERSION; |
| 1861 | capi.MICRO_VERSION = XML_MICRO_VERSION; |
| 1862 | capi.ErrorString = XML_ErrorString; |
Fredrik Lundh | cc117db | 2005-12-13 21:55:36 +0000 | [diff] [blame] | 1863 | capi.GetErrorCode = XML_GetErrorCode; |
| 1864 | capi.GetErrorColumnNumber = XML_GetErrorColumnNumber; |
| 1865 | capi.GetErrorLineNumber = XML_GetErrorLineNumber; |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1866 | capi.Parse = XML_Parse; |
| 1867 | capi.ParserCreate_MM = XML_ParserCreate_MM; |
| 1868 | capi.ParserFree = XML_ParserFree; |
| 1869 | capi.SetCharacterDataHandler = XML_SetCharacterDataHandler; |
| 1870 | capi.SetCommentHandler = XML_SetCommentHandler; |
| 1871 | capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand; |
| 1872 | capi.SetElementHandler = XML_SetElementHandler; |
| 1873 | capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler; |
| 1874 | capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler; |
| 1875 | capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler; |
| 1876 | capi.SetUserData = XML_SetUserData; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 1877 | capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 1878 | capi.SetEncoding = XML_SetEncoding; |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 1879 | capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1881 | /* export using capsule */ |
| 1882 | capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL); |
Fredrik Lundh | d7a4288 | 2005-12-13 20:43:04 +0000 | [diff] [blame] | 1883 | if (capi_object) |
| 1884 | PyModule_AddObject(m, "expat_CAPI", capi_object); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1885 | return m; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1888 | static void |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1889 | clear_handlers(xmlparseobject *self, int initial) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1890 | { |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1891 | int i = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1892 | |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1893 | for (; handler_info[i].name != NULL; i++) { |
Martin v. Löwis | 5b68ce3 | 2001-10-21 08:53:52 +0000 | [diff] [blame] | 1894 | if (initial) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | self->handlers[i] = NULL; |
| 1896 | else { |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 1897 | Py_CLEAR(self->handlers[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | handler_info[i].setter(self->itself, NULL); |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1899 | } |
Fred Drake | cde7913 | 2001-04-25 16:01:30 +0000 | [diff] [blame] | 1900 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1903 | static struct HandlerInfo handler_info[] = { |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1904 | {"StartElementHandler", |
| 1905 | (xmlhandlersetter)XML_SetStartElementHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1906 | (xmlhandler)my_StartElementHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1907 | {"EndElementHandler", |
| 1908 | (xmlhandlersetter)XML_SetEndElementHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1909 | (xmlhandler)my_EndElementHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1910 | {"ProcessingInstructionHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1911 | (xmlhandlersetter)XML_SetProcessingInstructionHandler, |
| 1912 | (xmlhandler)my_ProcessingInstructionHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1913 | {"CharacterDataHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1914 | (xmlhandlersetter)XML_SetCharacterDataHandler, |
| 1915 | (xmlhandler)my_CharacterDataHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1916 | {"UnparsedEntityDeclHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1917 | (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1918 | (xmlhandler)my_UnparsedEntityDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1919 | {"NotationDeclHandler", |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1920 | (xmlhandlersetter)XML_SetNotationDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1921 | (xmlhandler)my_NotationDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1922 | {"StartNamespaceDeclHandler", |
| 1923 | (xmlhandlersetter)XML_SetStartNamespaceDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1924 | (xmlhandler)my_StartNamespaceDeclHandler}, |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1925 | {"EndNamespaceDeclHandler", |
| 1926 | (xmlhandlersetter)XML_SetEndNamespaceDeclHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1927 | (xmlhandler)my_EndNamespaceDeclHandler}, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1928 | {"CommentHandler", |
| 1929 | (xmlhandlersetter)XML_SetCommentHandler, |
| 1930 | (xmlhandler)my_CommentHandler}, |
| 1931 | {"StartCdataSectionHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1932 | (xmlhandlersetter)XML_SetStartCdataSectionHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1933 | (xmlhandler)my_StartCdataSectionHandler}, |
| 1934 | {"EndCdataSectionHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1935 | (xmlhandlersetter)XML_SetEndCdataSectionHandler, |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1936 | (xmlhandler)my_EndCdataSectionHandler}, |
| 1937 | {"DefaultHandler", |
| 1938 | (xmlhandlersetter)XML_SetDefaultHandler, |
| 1939 | (xmlhandler)my_DefaultHandler}, |
| 1940 | {"DefaultHandlerExpand", |
| 1941 | (xmlhandlersetter)XML_SetDefaultHandlerExpand, |
| 1942 | (xmlhandler)my_DefaultHandlerExpandHandler}, |
| 1943 | {"NotStandaloneHandler", |
| 1944 | (xmlhandlersetter)XML_SetNotStandaloneHandler, |
| 1945 | (xmlhandler)my_NotStandaloneHandler}, |
| 1946 | {"ExternalEntityRefHandler", |
| 1947 | (xmlhandlersetter)XML_SetExternalEntityRefHandler, |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 1948 | (xmlhandler)my_ExternalEntityRefHandler}, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1949 | {"StartDoctypeDeclHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1950 | (xmlhandlersetter)XML_SetStartDoctypeDeclHandler, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1951 | (xmlhandler)my_StartDoctypeDeclHandler}, |
| 1952 | {"EndDoctypeDeclHandler", |
Fred Drake | 71b63ff | 2002-06-28 22:29:01 +0000 | [diff] [blame] | 1953 | (xmlhandlersetter)XML_SetEndDoctypeDeclHandler, |
Martin v. Löwis | 0078f6c | 2001-01-21 10:18:10 +0000 | [diff] [blame] | 1954 | (xmlhandler)my_EndDoctypeDeclHandler}, |
Fred Drake | 85d835f | 2001-02-08 15:39:08 +0000 | [diff] [blame] | 1955 | {"EntityDeclHandler", |
| 1956 | (xmlhandlersetter)XML_SetEntityDeclHandler, |
| 1957 | (xmlhandler)my_EntityDeclHandler}, |
| 1958 | {"XmlDeclHandler", |
| 1959 | (xmlhandlersetter)XML_SetXmlDeclHandler, |
| 1960 | (xmlhandler)my_XmlDeclHandler}, |
| 1961 | {"ElementDeclHandler", |
| 1962 | (xmlhandlersetter)XML_SetElementDeclHandler, |
| 1963 | (xmlhandler)my_ElementDeclHandler}, |
| 1964 | {"AttlistDeclHandler", |
| 1965 | (xmlhandlersetter)XML_SetAttlistDeclHandler, |
| 1966 | (xmlhandler)my_AttlistDeclHandler}, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1967 | #if XML_COMBINED_VERSION >= 19504 |
Martin v. Löwis | 069dde2 | 2003-01-21 10:58:18 +0000 | [diff] [blame] | 1968 | {"SkippedEntityHandler", |
| 1969 | (xmlhandlersetter)XML_SetSkippedEntityHandler, |
| 1970 | (xmlhandler)my_SkippedEntityHandler}, |
Martin v. Löwis | c847f40 | 2003-01-21 11:09:21 +0000 | [diff] [blame] | 1971 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1972 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1973 | {NULL, NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1974 | }; |