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