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