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