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