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