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