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