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