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