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