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