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