Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Fred Drake | a77254a | 2000-09-29 19:23:29 +0000 | [diff] [blame] | 2 | #ifdef HAVE_EXPAT_H |
| 3 | #include "expat.h" |
| 4 | #else |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 5 | #include "xmlparse.h" |
Fred Drake | a77254a | 2000-09-29 19:23:29 +0000 | [diff] [blame] | 6 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 7 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 8 | enum HandlerTypes { |
| 9 | StartElement, |
| 10 | EndElement, |
| 11 | ProcessingInstruction, |
| 12 | CharacterData, |
| 13 | UnparsedEntityDecl, |
| 14 | NotationDecl, |
| 15 | StartNamespaceDecl, |
| 16 | EndNamespaceDecl, |
| 17 | Comment, |
| 18 | StartCdataSection, |
| 19 | EndCdataSection, |
| 20 | Default, |
| 21 | DefaultHandlerExpand, |
| 22 | NotStandalone, |
| 23 | ExternalEntityRef |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static PyObject *ErrorObject; |
| 27 | |
| 28 | /* ----------------------------------------------------- */ |
| 29 | |
| 30 | /* Declarations for objects of type xmlparser */ |
| 31 | |
| 32 | typedef struct { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 33 | PyObject_HEAD |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 34 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 35 | XML_Parser itself; |
| 36 | int returns_unicode; /* True if Unicode strings are returned; |
| 37 | if false, UTF-8 strings are returned */ |
| 38 | PyObject **handlers; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 39 | } xmlparseobject; |
| 40 | |
| 41 | staticforward PyTypeObject Xmlparsetype; |
| 42 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 43 | typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 44 | typedef void* xmlhandler; |
| 45 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 46 | struct HandlerInfo { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 47 | const char *name; |
| 48 | xmlhandlersetter setter; |
| 49 | xmlhandler handler; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Andrew M. Kuchling | 637f664 | 2000-07-04 14:53:43 +0000 | [diff] [blame] | 52 | staticforward struct HandlerInfo handler_info[64]; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 54 | /* Convert an array of attributes and their values into a Python dict */ |
| 55 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 56 | static PyObject * |
| 57 | conv_atts_using_string(XML_Char **atts) |
Andrew M. Kuchling | a4e75d7 | 2000-07-12 00:53:41 +0000 | [diff] [blame] | 58 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 59 | PyObject *attrs_obj = NULL; |
| 60 | XML_Char **attrs_p, **attrs_k = NULL; |
| 61 | int attrs_len; |
| 62 | PyObject *rv; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 63 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 64 | if ((attrs_obj = PyDict_New()) == NULL) |
| 65 | goto finally; |
| 66 | for (attrs_len = 0, attrs_p = atts; |
| 67 | *attrs_p; |
| 68 | attrs_p++, attrs_len++) { |
| 69 | if (attrs_len % 2) { |
| 70 | rv = PyString_FromString(*attrs_p); |
| 71 | if (!rv) { |
| 72 | Py_DECREF(attrs_obj); |
| 73 | attrs_obj = NULL; |
| 74 | goto finally; |
| 75 | } |
| 76 | if (PyDict_SetItemString(attrs_obj, |
| 77 | (char*)*attrs_k, rv) < 0) { |
| 78 | Py_DECREF(attrs_obj); |
| 79 | attrs_obj = NULL; |
| 80 | goto finally; |
| 81 | } |
| 82 | Py_DECREF(rv); |
| 83 | } |
| 84 | else |
| 85 | attrs_k = attrs_p; |
| 86 | } |
| 87 | finally: |
| 88 | return attrs_obj; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 92 | static PyObject * |
| 93 | conv_atts_using_unicode(XML_Char **atts) |
| 94 | { |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 95 | PyObject *attrs_obj; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 96 | XML_Char **attrs_p, **attrs_k = NULL; |
| 97 | int attrs_len; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 98 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 99 | if ((attrs_obj = PyDict_New()) == NULL) |
| 100 | goto finally; |
| 101 | for (attrs_len = 0, attrs_p = atts; |
| 102 | *attrs_p; |
| 103 | attrs_p++, attrs_len++) { |
| 104 | if (attrs_len % 2) { |
| 105 | PyObject *attr_str, *value_str; |
| 106 | const char *p = (const char *) (*attrs_k); |
| 107 | attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict"); |
| 108 | if (!attr_str) { |
| 109 | Py_DECREF(attrs_obj); |
| 110 | attrs_obj = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 111 | goto finally; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 112 | } |
| 113 | p = (const char *) *attrs_p; |
| 114 | value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict"); |
| 115 | if (!value_str) { |
| 116 | Py_DECREF(attrs_obj); |
| 117 | Py_DECREF(attr_str); |
| 118 | attrs_obj = NULL; |
| 119 | goto finally; |
| 120 | } |
| 121 | if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) { |
| 122 | Py_DECREF(attrs_obj); |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 123 | Py_DECREF(attr_str); |
| 124 | Py_DECREF(value_str); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 125 | attrs_obj = NULL; |
| 126 | goto finally; |
| 127 | } |
| 128 | Py_DECREF(attr_str); |
| 129 | Py_DECREF(value_str); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 130 | } |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 131 | else |
| 132 | attrs_k = attrs_p; |
| 133 | } |
| 134 | finally: |
| 135 | return attrs_obj; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 138 | /* Convert a string of XML_Chars into a Unicode string. |
| 139 | Returns None if str is a null pointer. */ |
| 140 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 141 | static PyObject * |
| 142 | conv_string_to_unicode(XML_Char *str) |
| 143 | { |
| 144 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 145 | and hence in UTF-8. */ |
| 146 | /* UTF-8 from Expat, Unicode desired */ |
| 147 | if (str == NULL) { |
| 148 | Py_INCREF(Py_None); |
| 149 | return Py_None; |
| 150 | } |
| 151 | return PyUnicode_DecodeUTF8((const char *)str, |
| 152 | strlen((const char *)str), |
| 153 | "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 156 | static PyObject * |
| 157 | conv_string_len_to_unicode(const XML_Char *str, int len) |
| 158 | { |
| 159 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 160 | and hence in UTF-8. */ |
| 161 | /* UTF-8 from Expat, Unicode desired */ |
| 162 | if (str == NULL) { |
| 163 | Py_INCREF(Py_None); |
| 164 | return Py_None; |
| 165 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 166 | return PyUnicode_DecodeUTF8((const char *)str, len, "strict"); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 167 | } |
| 168 | #endif |
| 169 | |
| 170 | /* Convert a string of XML_Chars into an 8-bit Python string. |
| 171 | Returns None if str is a null pointer. */ |
| 172 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 173 | static PyObject * |
| 174 | conv_string_to_utf8(XML_Char *str) |
| 175 | { |
| 176 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 177 | and hence in UTF-8. */ |
| 178 | /* UTF-8 from Expat, UTF-8 desired */ |
| 179 | if (str == NULL) { |
| 180 | Py_INCREF(Py_None); |
| 181 | return Py_None; |
| 182 | } |
| 183 | return PyString_FromString((const char *)str); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 186 | static PyObject * |
| 187 | conv_string_len_to_utf8(const XML_Char *str, int len) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 188 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 189 | /* XXX currently this code assumes that XML_Char is 8-bit, |
| 190 | and hence in UTF-8. */ |
| 191 | /* UTF-8 from Expat, UTF-8 desired */ |
| 192 | if (str == NULL) { |
| 193 | Py_INCREF(Py_None); |
| 194 | return Py_None; |
| 195 | } |
| 196 | return PyString_FromStringAndSize((const char *)str, len); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 199 | /* Callback routines */ |
| 200 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 201 | static void clear_handlers(xmlparseobject *self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 202 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 203 | static void |
| 204 | flag_error(xmlparseobject *self) |
| 205 | { |
| 206 | clear_handlers(self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 209 | #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 210 | RETURN, GETUSERDATA) \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 211 | \ |
| 212 | static RC my_##NAME##Handler PARAMS {\ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 213 | xmlparseobject *self = GETUSERDATA ; \ |
| 214 | PyObject *args=NULL; \ |
| 215 | PyObject *rv=NULL; \ |
| 216 | INIT \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 217 | \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 218 | if (self->handlers[NAME] \ |
| 219 | && self->handlers[NAME] != Py_None) { \ |
| 220 | args = Py_BuildValue PARAM_FORMAT ;\ |
| 221 | if (!args) return RETURN; \ |
| 222 | rv = PyEval_CallObject(self->handlers[NAME], args); \ |
| 223 | Py_DECREF(args); \ |
| 224 | if (rv == NULL) { \ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 225 | flag_error(self); \ |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 226 | return RETURN; \ |
| 227 | } \ |
| 228 | CONVERSION \ |
| 229 | Py_DECREF(rv); \ |
| 230 | } \ |
| 231 | return RETURN; \ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 234 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
| 235 | #define STRING_CONV_FUNC conv_string_to_utf8 |
| 236 | #else |
| 237 | /* Python 1.6 and later versions */ |
| 238 | #define STRING_CONV_FUNC (self->returns_unicode \ |
| 239 | ? conv_string_to_unicode : conv_string_to_utf8) |
| 240 | #endif |
Guido van Rossum | 5961f5a | 2000-03-31 16:18:11 +0000 | [diff] [blame] | 241 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 242 | #define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \ |
| 243 | RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\ |
| 244 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 245 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 246 | #define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\ |
| 247 | RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \ |
| 248 | rc = PyInt_AsLong(rv);, rc, \ |
| 249 | (xmlparseobject *)userData) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 250 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 251 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 252 | VOID_HANDLER(StartElement, |
| 253 | (void *userData, const XML_Char *name, const XML_Char **atts), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 254 | ("(O&O&)", STRING_CONV_FUNC, name, |
| 255 | conv_atts_using_string, atts ) ) |
| 256 | #else |
| 257 | /* Python 1.6 and later */ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 258 | VOID_HANDLER(StartElement, |
| 259 | (void *userData, const XML_Char *name, const XML_Char **atts), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 260 | ("(O&O&)", STRING_CONV_FUNC, name, |
| 261 | (self->returns_unicode |
| 262 | ? conv_atts_using_unicode |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 263 | : conv_atts_using_string), atts)) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 264 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 265 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 266 | VOID_HANDLER(EndElement, |
| 267 | (void *userData, const XML_Char *name), |
| 268 | ("(O&)", STRING_CONV_FUNC, name)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 269 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 270 | VOID_HANDLER(ProcessingInstruction, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 271 | (void *userData, |
| 272 | const XML_Char *target, |
| 273 | const XML_Char *data), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 274 | ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 275 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 276 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 277 | VOID_HANDLER(CharacterData, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 278 | (void *userData, const XML_Char *data, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 279 | ("(N)", conv_string_len_to_utf8(data,len))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 280 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 281 | VOID_HANDLER(CharacterData, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 282 | (void *userData, const XML_Char *data, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 283 | ("(N)", (self->returns_unicode |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 284 | ? conv_string_len_to_unicode(data,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 285 | : conv_string_len_to_utf8(data,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 286 | #endif |
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 | VOID_HANDLER(UnparsedEntityDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 289 | (void *userData, |
| 290 | const XML_Char *entityName, |
| 291 | const XML_Char *base, |
| 292 | const XML_Char *systemId, |
| 293 | const XML_Char *publicId, |
| 294 | const XML_Char *notationName), |
| 295 | ("(O&O&O&O&O&)", |
| 296 | STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base, |
| 297 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId, |
| 298 | STRING_CONV_FUNC,notationName)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 299 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 300 | VOID_HANDLER(NotationDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 301 | (void *userData, |
| 302 | const XML_Char *notationName, |
| 303 | const XML_Char *base, |
| 304 | const XML_Char *systemId, |
| 305 | const XML_Char *publicId), |
| 306 | ("(O&O&O&O&)", |
| 307 | STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base, |
| 308 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 309 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 310 | VOID_HANDLER(StartNamespaceDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 311 | (void *userData, |
| 312 | const XML_Char *prefix, |
| 313 | const XML_Char *uri), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 314 | ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 315 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 316 | VOID_HANDLER(EndNamespaceDecl, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 317 | (void *userData, |
| 318 | const XML_Char *prefix), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 319 | ("(O&)", STRING_CONV_FUNC,prefix)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 320 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 321 | VOID_HANDLER(Comment, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 322 | (void *userData, const XML_Char *prefix), |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 323 | ("(O&)", STRING_CONV_FUNC,prefix)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 324 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 325 | VOID_HANDLER(StartCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 326 | (void *userData), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 327 | ("()")) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 328 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 329 | VOID_HANDLER(EndCdataSection, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 330 | (void *userData), |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 331 | ("()")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 332 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 333 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 334 | VOID_HANDLER(Default, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 335 | (void *userData, const XML_Char *s, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 336 | ("(N)", conv_string_len_to_utf8(s,len))) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 337 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 338 | VOID_HANDLER(DefaultHandlerExpand, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 339 | (void *userData, const XML_Char *s, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 340 | ("(N)", conv_string_len_to_utf8(s,len))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 341 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 342 | VOID_HANDLER(Default, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 343 | (void *userData, const XML_Char *s, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 344 | ("(N)", (self->returns_unicode |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 345 | ? conv_string_len_to_unicode(s,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 346 | : conv_string_len_to_utf8(s,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 347 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 348 | VOID_HANDLER(DefaultHandlerExpand, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 349 | (void *userData, const XML_Char *s, int len), |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 350 | ("(N)", (self->returns_unicode |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 351 | ? conv_string_len_to_unicode(s,len) |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 352 | : conv_string_len_to_utf8(s,len)))) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 353 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 354 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 355 | INT_HANDLER(NotStandalone, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 356 | (void *userData), |
| 357 | ("()")) |
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 | RC_HANDLER(int, ExternalEntityRef, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 360 | (XML_Parser parser, |
| 361 | const XML_Char *context, |
| 362 | const XML_Char *base, |
| 363 | const XML_Char *systemId, |
| 364 | const XML_Char *publicId), |
| 365 | int rc=0;, |
| 366 | ("(O&O&O&O&)", |
| 367 | STRING_CONV_FUNC,context, STRING_CONV_FUNC,base, |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 368 | STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId), |
| 369 | rc = PyInt_AsLong(rv);, rc, |
| 370 | XML_GetUserData(parser)) |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 371 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 372 | |
| 373 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 374 | /* ---------------------------------------------------------------- */ |
| 375 | |
| 376 | static char xmlparse_Parse__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 377 | "Parse(data[, isfinal])\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 378 | Parse XML data. `isfinal' should be true at end of input."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 379 | |
| 380 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 381 | xmlparse_Parse(xmlparseobject *self, PyObject *args) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 382 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 383 | char *s; |
| 384 | int slen; |
| 385 | int isFinal = 0; |
| 386 | int rv; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 387 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 388 | if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal)) |
| 389 | return NULL; |
| 390 | rv = XML_Parse(self->itself, s, slen, isFinal); |
| 391 | if (PyErr_Occurred()) { |
| 392 | return NULL; |
| 393 | } |
| 394 | else if (rv == 0) { |
| 395 | PyErr_Format(ErrorObject, "%.200s: line %i, column %i", |
| 396 | XML_ErrorString(XML_GetErrorCode(self->itself)), |
| 397 | XML_GetErrorLineNumber(self->itself), |
| 398 | XML_GetErrorColumnNumber(self->itself)); |
| 399 | return NULL; |
| 400 | } |
| 401 | return PyInt_FromLong(rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 404 | /* File reading copied from cPickle */ |
| 405 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 406 | #define BUF_SIZE 2048 |
| 407 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 408 | static int |
| 409 | readinst(char *buf, int buf_size, PyObject *meth) |
| 410 | { |
| 411 | PyObject *arg = NULL; |
| 412 | PyObject *bytes = NULL; |
| 413 | PyObject *str = NULL; |
| 414 | int len = -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 415 | |
Fred Drake | 676940b | 2000-09-22 15:21:31 +0000 | [diff] [blame] | 416 | if ((bytes = PyInt_FromLong(buf_size)) == NULL) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 417 | goto finally; |
Fred Drake | 676940b | 2000-09-22 15:21:31 +0000 | [diff] [blame] | 418 | |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 419 | if ((arg = PyTuple_New(1)) == NULL) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 420 | goto finally; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 421 | |
Tim Peters | 954eef7 | 2000-09-22 06:01:11 +0000 | [diff] [blame] | 422 | PyTuple_SET_ITEM(arg, 0, bytes); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 423 | |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 424 | if ((str = PyObject_CallObject(meth, arg)) == NULL) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 425 | goto finally; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 426 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 427 | /* XXX what to do if it returns a Unicode string? */ |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 428 | if (!PyString_Check(str)) { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 429 | PyErr_Format(PyExc_TypeError, |
| 430 | "read() did not return a string object (type=%.400s)", |
| 431 | str->ob_type->tp_name); |
| 432 | goto finally; |
| 433 | } |
| 434 | len = PyString_GET_SIZE(str); |
| 435 | if (len > buf_size) { |
| 436 | PyErr_Format(PyExc_ValueError, |
| 437 | "read() returned too much data: " |
| 438 | "%i bytes requested, %i returned", |
| 439 | buf_size, len); |
| 440 | Py_DECREF(str); |
| 441 | goto finally; |
| 442 | } |
| 443 | memcpy(buf, PyString_AsString(str), len); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 444 | finally: |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 445 | Py_XDECREF(arg); |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 446 | Py_XDECREF(str); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 447 | return len; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static char xmlparse_ParseFile__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 451 | "ParseFile(file)\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 452 | Parse XML data from file-like object."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 453 | |
| 454 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 455 | xmlparse_ParseFile(xmlparseobject *self, PyObject *args) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 456 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 457 | int rv = 1; |
| 458 | PyObject *f; |
| 459 | FILE *fp; |
| 460 | PyObject *readmethod = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 461 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 462 | if (!PyArg_ParseTuple(args, "O:ParseFile", &f)) |
| 463 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 464 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 465 | if (PyFile_Check(f)) { |
| 466 | fp = PyFile_AsFile(f); |
| 467 | } |
| 468 | else{ |
| 469 | fp = NULL; |
Fred Drake | ca1f426 | 2000-09-21 20:10:23 +0000 | [diff] [blame] | 470 | readmethod = PyObject_GetAttrString(f, "read"); |
| 471 | if (readmethod == NULL) { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 472 | PyErr_Clear(); |
| 473 | PyErr_SetString(PyExc_TypeError, |
| 474 | "argument must have 'read' attribute"); |
| 475 | return 0; |
| 476 | } |
| 477 | } |
| 478 | for (;;) { |
| 479 | int bytes_read; |
| 480 | void *buf = XML_GetBuffer(self->itself, BUF_SIZE); |
| 481 | if (buf == NULL) |
| 482 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 483 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 484 | if (fp) { |
| 485 | bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp); |
| 486 | if (bytes_read < 0) { |
| 487 | PyErr_SetFromErrno(PyExc_IOError); |
| 488 | return NULL; |
| 489 | } |
| 490 | } |
| 491 | else { |
| 492 | bytes_read = readinst(buf, BUF_SIZE, readmethod); |
| 493 | if (bytes_read < 0) |
| 494 | return NULL; |
| 495 | } |
| 496 | rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); |
| 497 | if (PyErr_Occurred()) |
| 498 | return NULL; |
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 | if (!rv || bytes_read == 0) |
| 501 | break; |
| 502 | } |
| 503 | return Py_BuildValue("i", rv); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static char xmlparse_SetBase__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 507 | "SetBase(base_url)\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 508 | Set the base URL for the parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 509 | |
| 510 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 511 | xmlparse_SetBase(xmlparseobject *self, PyObject *args) |
| 512 | { |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 513 | char *base; |
| 514 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 515 | if (!PyArg_ParseTuple(args, "s:SetBase", &base)) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 516 | return NULL; |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 517 | if (!XML_SetBase(self->itself, base)) { |
| 518 | return PyErr_NoMemory(); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 519 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 520 | Py_INCREF(Py_None); |
| 521 | return Py_None; |
| 522 | } |
| 523 | |
| 524 | static char xmlparse_GetBase__doc__[] = |
Thomas Wouters | 3531730 | 2000-07-22 16:34:15 +0000 | [diff] [blame] | 525 | "GetBase() -> url\n\ |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 526 | Return base URL string for the parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 527 | |
| 528 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 529 | xmlparse_GetBase(xmlparseobject *self, PyObject *args) |
| 530 | { |
| 531 | if (!PyArg_ParseTuple(args, ":GetBase")) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 532 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 533 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 534 | return Py_BuildValue("z", XML_GetBase(self->itself)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 537 | static char xmlparse_ExternalEntityParserCreate__doc__[] = |
| 538 | "ExternalEntityParserCreate(context, encoding)\n\ |
Tim Peters | 51dc968 | 2000-09-24 22:12:45 +0000 | [diff] [blame] | 539 | 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] | 540 | information passed to the ExternalEntityRefHandler."; |
| 541 | |
| 542 | static PyObject * |
| 543 | xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) |
| 544 | { |
| 545 | char *context; |
| 546 | char *encoding = NULL; |
| 547 | xmlparseobject *new_parser; |
| 548 | int i; |
| 549 | |
| 550 | if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context, |
| 551 | &encoding)) { |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
| 556 | new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype); |
| 557 | if (new_parser == NULL) |
| 558 | return NULL; |
| 559 | |
| 560 | new_parser->returns_unicode = 0; |
| 561 | #else |
| 562 | /* Code for versions 1.6 and later */ |
| 563 | new_parser = PyObject_New(xmlparseobject, &Xmlparsetype); |
| 564 | if (new_parser == NULL) |
| 565 | return NULL; |
| 566 | |
| 567 | new_parser->returns_unicode = 1; |
| 568 | #endif |
| 569 | |
| 570 | new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context, |
| 571 | encoding); |
| 572 | if (!new_parser) { |
| 573 | Py_DECREF(new_parser); |
| 574 | return PyErr_NoMemory(); |
| 575 | } |
| 576 | |
| 577 | XML_SetUserData(new_parser->itself, (void *)new_parser); |
| 578 | |
| 579 | /* allocate and clear handlers first */ |
| 580 | for(i = 0; handler_info[i].name != NULL; i++) |
| 581 | /* do nothing */; |
| 582 | |
| 583 | new_parser->handlers = malloc(sizeof(PyObject *)*i); |
| 584 | clear_handlers(new_parser); |
| 585 | |
| 586 | /* then copy handlers from self */ |
| 587 | for (i = 0; handler_info[i].name != NULL; i++) { |
| 588 | if (self->handlers[i]) { |
| 589 | Py_INCREF(self->handlers[i]); |
| 590 | new_parser->handlers[i] = self->handlers[i]; |
| 591 | handler_info[i].setter(new_parser->itself, |
| 592 | handler_info[i].handler); |
| 593 | } |
| 594 | } |
| 595 | |
Fred Drake | 28adf52 | 2000-09-24 22:07:59 +0000 | [diff] [blame] | 596 | return (PyObject *)new_parser; |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
| 600 | |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 601 | static struct PyMethodDef xmlparse_methods[] = { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 602 | {"Parse", (PyCFunction)xmlparse_Parse, |
| 603 | METH_VARARGS, xmlparse_Parse__doc__}, |
| 604 | {"ParseFile", (PyCFunction)xmlparse_ParseFile, |
| 605 | METH_VARARGS, xmlparse_ParseFile__doc__}, |
| 606 | {"SetBase", (PyCFunction)xmlparse_SetBase, |
| 607 | METH_VARARGS, xmlparse_SetBase__doc__}, |
| 608 | {"GetBase", (PyCFunction)xmlparse_GetBase, |
| 609 | METH_VARARGS, xmlparse_GetBase__doc__}, |
Lars Gustäbel | 4a30a07 | 2000-09-24 20:50:52 +0000 | [diff] [blame] | 610 | {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate, |
| 611 | METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__}, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 612 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | /* ---------- */ |
| 616 | |
| 617 | |
| 618 | static xmlparseobject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 619 | newxmlparseobject(char *encoding, char *namespace_separator) |
| 620 | { |
| 621 | int i; |
| 622 | xmlparseobject *self; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 623 | |
| 624 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 625 | self = PyObject_NEW(xmlparseobject, &Xmlparsetype); |
| 626 | if (self == NULL) |
| 627 | return NULL; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 628 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 629 | self->returns_unicode = 0; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 630 | #else |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 631 | /* Code for versions 1.6 and later */ |
| 632 | self = PyObject_New(xmlparseobject, &Xmlparsetype); |
| 633 | if (self == NULL) |
| 634 | return NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 635 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 636 | self->returns_unicode = 1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 637 | #endif |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 638 | if (namespace_separator) { |
| 639 | self->itself = XML_ParserCreateNS(encoding, *namespace_separator); |
| 640 | } |
| 641 | else{ |
| 642 | self->itself = XML_ParserCreate(encoding); |
| 643 | } |
| 644 | if (self->itself == NULL) { |
| 645 | PyErr_SetString(PyExc_RuntimeError, |
| 646 | "XML_ParserCreate failed"); |
| 647 | Py_DECREF(self); |
| 648 | return NULL; |
| 649 | } |
| 650 | XML_SetUserData(self->itself, (void *)self); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 651 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 652 | for(i = 0; handler_info[i].name != NULL; i++) |
| 653 | /* do nothing */; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 654 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 655 | self->handlers = malloc(sizeof(PyObject *)*i); |
| 656 | clear_handlers(self); |
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 self; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
| 662 | static void |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 663 | xmlparse_dealloc(xmlparseobject *self) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 664 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 665 | int i; |
| 666 | if (self->itself) |
| 667 | XML_ParserFree(self->itself); |
| 668 | self->itself = NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 669 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 670 | for (i=0; handler_info[i].name != NULL; i++) { |
| 671 | Py_XDECREF(self->handlers[i]); |
| 672 | } |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 673 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 674 | /* Code for versions before 1.6 */ |
| 675 | free(self); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 676 | #else |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 677 | /* Code for versions 1.6 and later */ |
| 678 | PyObject_Del(self); |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 679 | #endif |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 682 | static int |
| 683 | handlername2int(const char *name) |
| 684 | { |
| 685 | int i; |
| 686 | for (i=0; handler_info[i].name != NULL; i++) { |
| 687 | if (strcmp(name, handler_info[i].name) == 0) { |
| 688 | return i; |
| 689 | } |
| 690 | } |
| 691 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static PyObject * |
| 695 | xmlparse_getattr(xmlparseobject *self, char *name) |
| 696 | { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 697 | int handlernum; |
| 698 | if (strcmp(name, "ErrorCode") == 0) |
| 699 | return Py_BuildValue("l", |
| 700 | (long)XML_GetErrorCode(self->itself)); |
| 701 | if (strcmp(name, "ErrorLineNumber") == 0) |
| 702 | return Py_BuildValue("l", |
| 703 | (long)XML_GetErrorLineNumber(self->itself)); |
| 704 | if (strcmp(name, "ErrorColumnNumber") == 0) |
| 705 | return Py_BuildValue("l", |
| 706 | (long)XML_GetErrorColumnNumber(self->itself)); |
| 707 | if (strcmp(name, "ErrorByteIndex") == 0) |
| 708 | return Py_BuildValue("l", |
| 709 | XML_GetErrorByteIndex(self->itself)); |
| 710 | if (strcmp(name, "returns_unicode") == 0) |
| 711 | return Py_BuildValue("i", self->returns_unicode); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 712 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 713 | handlernum = handlername2int(name); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 714 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 715 | if (handlernum != -1 && self->handlers[handlernum] != NULL) { |
| 716 | Py_INCREF(self->handlers[handlernum]); |
| 717 | return self->handlers[handlernum]; |
| 718 | } |
| 719 | if (strcmp(name, "__members__") == 0) { |
| 720 | int i; |
| 721 | PyObject *rc = PyList_New(0); |
| 722 | for(i = 0; handler_info[i].name!=NULL; i++) { |
| 723 | PyList_Append(rc, |
| 724 | PyString_FromString(handler_info[i].name)); |
| 725 | } |
| 726 | PyList_Append(rc, PyString_FromString("ErrorCode")); |
| 727 | PyList_Append(rc, PyString_FromString("ErrorLineNumber")); |
| 728 | PyList_Append(rc, PyString_FromString("ErrorColumnNumber")); |
| 729 | PyList_Append(rc, PyString_FromString("ErrorByteIndex")); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 730 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 731 | return rc; |
| 732 | } |
| 733 | return Py_FindMethod(xmlparse_methods, (PyObject *)self, name); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 736 | static int |
| 737 | sethandler(xmlparseobject *self, const char *name, PyObject* v) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 738 | { |
| 739 | int handlernum = handlername2int(name); |
| 740 | if (handlernum != -1) { |
| 741 | Py_INCREF(v); |
| 742 | Py_XDECREF(self->handlers[handlernum]); |
| 743 | self->handlers[handlernum] = v; |
| 744 | handler_info[handlernum].setter(self->itself, |
| 745 | handler_info[handlernum].handler); |
| 746 | return 1; |
| 747 | } |
| 748 | return 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static int |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 752 | xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v) |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 753 | { |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 754 | /* Set attribute 'name' to value 'v'. v==NULL means delete */ |
| 755 | if (v==NULL) { |
| 756 | PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute"); |
| 757 | return -1; |
| 758 | } |
| 759 | if (strcmp(name, "returns_unicode") == 0) { |
| 760 | PyObject *intobj = PyNumber_Int(v); |
| 761 | if (intobj == NULL) return -1; |
| 762 | if (PyInt_AsLong(intobj)) { |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 763 | #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 764 | PyErr_SetString(PyExc_ValueError, |
| 765 | "Cannot return Unicode strings in Python 1.5"); |
| 766 | return -1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 767 | #else |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 768 | self->returns_unicode = 1; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 769 | #endif |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 770 | } |
| 771 | else |
| 772 | self->returns_unicode = 0; |
| 773 | Py_DECREF(intobj); |
| 774 | return 0; |
| 775 | } |
| 776 | if (sethandler(self, name, v)) { |
| 777 | return 0; |
| 778 | } |
| 779 | PyErr_SetString(PyExc_AttributeError, name); |
| 780 | return -1; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static char Xmlparsetype__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 784 | "XML parser"; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 785 | |
| 786 | static PyTypeObject Xmlparsetype = { |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 787 | PyObject_HEAD_INIT(NULL) |
| 788 | 0, /*ob_size*/ |
| 789 | "xmlparser", /*tp_name*/ |
| 790 | sizeof(xmlparseobject), /*tp_basicsize*/ |
| 791 | 0, /*tp_itemsize*/ |
| 792 | /* methods */ |
| 793 | (destructor)xmlparse_dealloc, /*tp_dealloc*/ |
| 794 | (printfunc)0, /*tp_print*/ |
| 795 | (getattrfunc)xmlparse_getattr, /*tp_getattr*/ |
| 796 | (setattrfunc)xmlparse_setattr, /*tp_setattr*/ |
| 797 | (cmpfunc)0, /*tp_compare*/ |
| 798 | (reprfunc)0, /*tp_repr*/ |
| 799 | 0, /*tp_as_number*/ |
| 800 | 0, /*tp_as_sequence*/ |
| 801 | 0, /*tp_as_mapping*/ |
| 802 | (hashfunc)0, /*tp_hash*/ |
| 803 | (ternaryfunc)0, /*tp_call*/ |
| 804 | (reprfunc)0, /*tp_str*/ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 805 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 806 | /* Space for future expansion */ |
| 807 | 0L,0L,0L,0L, |
| 808 | Xmlparsetype__doc__ /* Documentation string */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | /* End of code for xmlparser objects */ |
| 812 | /* -------------------------------------------------------- */ |
| 813 | |
| 814 | |
| 815 | static char pyexpat_ParserCreate__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 816 | "ParserCreate([encoding[, namespace_separator]]) -> parser\n\ |
| 817 | Return a new XML parser object."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 818 | |
| 819 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 820 | pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw) |
| 821 | { |
| 822 | char *encoding = NULL; |
| 823 | char *namespace_separator = NULL; |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 824 | static char *kwlist[] = {"encoding", "namespace_separator", NULL}; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 825 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 826 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist, |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 827 | &encoding, &namespace_separator)) |
| 828 | return NULL; |
| 829 | return (PyObject *)newxmlparseobject(encoding, namespace_separator); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static char pyexpat_ErrorString__doc__[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 833 | "ErrorString(errno) -> string\n\ |
| 834 | Returns string error for given number."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 835 | |
| 836 | static PyObject * |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 837 | pyexpat_ErrorString(PyObject *self, PyObject *args) |
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 | long code = 0; |
| 840 | |
| 841 | if (!PyArg_ParseTuple(args, "l:ErrorString", &code)) |
| 842 | return NULL; |
| 843 | return Py_BuildValue("z", XML_ErrorString((int)code)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* List of methods defined in the module */ |
| 847 | |
| 848 | static struct PyMethodDef pyexpat_methods[] = { |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 849 | {"ParserCreate", (PyCFunction)pyexpat_ParserCreate, |
| 850 | METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__}, |
| 851 | {"ErrorString", (PyCFunction)pyexpat_ErrorString, |
| 852 | METH_VARARGS, pyexpat_ErrorString__doc__}, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 853 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 854 | {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 855 | }; |
| 856 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 857 | /* Module docstring */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 858 | |
| 859 | static char pyexpat_module_documentation[] = |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 860 | "Python wrapper for Expat parser."; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 861 | |
Andrew M. Kuchling | beba056 | 2000-06-27 00:33:30 +0000 | [diff] [blame] | 862 | /* Initialization function for the module */ |
| 863 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 864 | void initpyexpat(void); /* avoid compiler warnings */ |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 865 | |
Martin v. Löwis | c0718eb | 2000-09-29 19:05:48 +0000 | [diff] [blame] | 866 | #if PY_VERSION_HEX < 0x2000000 |
| 867 | |
| 868 | /* 1.5 compatibility: PyModule_AddObject */ |
| 869 | static int |
| 870 | PyModule_AddObject(PyObject *m, char *name, PyObject *o) |
| 871 | { |
| 872 | PyObject *dict; |
| 873 | if (!PyModule_Check(m) || o == NULL) |
| 874 | return -1; |
| 875 | dict = PyModule_GetDict(m); |
| 876 | if (dict == NULL) |
| 877 | return -1; |
| 878 | if (PyDict_SetItemString(dict, name, o)) |
| 879 | return -1; |
| 880 | Py_DECREF(o); |
| 881 | return 0; |
| 882 | } |
| 883 | |
Fred Drake | a77254a | 2000-09-29 19:23:29 +0000 | [diff] [blame] | 884 | static int |
Martin v. Löwis | c0718eb | 2000-09-29 19:05:48 +0000 | [diff] [blame] | 885 | PyModule_AddStringConstant(PyObject *m, char *name, char *value) |
| 886 | { |
| 887 | return PyModule_AddObject(m, name, PyString_FromString(value)); |
| 888 | } |
| 889 | |
| 890 | #endif |
| 891 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 892 | DL_EXPORT(void) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 893 | initpyexpat(void) |
| 894 | { |
| 895 | PyObject *m, *d; |
| 896 | char *rev = "$Revision$"; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 897 | PyObject *errmod_name = PyString_FromString("pyexpat.errors"); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 898 | PyObject *errors_module, *errors_dict; |
| 899 | PyObject *sys_modules; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 900 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 901 | if (errmod_name == NULL) |
| 902 | return; |
| 903 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 904 | Xmlparsetype.ob_type = &PyType_Type; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 905 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 906 | /* Create the module and add the functions */ |
| 907 | m = Py_InitModule4("pyexpat", pyexpat_methods, |
| 908 | pyexpat_module_documentation, |
| 909 | (PyObject*)NULL, PYTHON_API_VERSION); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 910 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 911 | /* Add some symbolic constants to the module */ |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 912 | if (ErrorObject == NULL) |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 913 | ErrorObject = PyErr_NewException("xml.parsers.expat.error", |
| 914 | NULL, NULL); |
| 915 | PyModule_AddObject(m, "error", ErrorObject); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 916 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 917 | PyModule_AddObject(m, "__version__", |
| 918 | PyString_FromStringAndSize(rev+11, strlen(rev+11)-2)); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 919 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 920 | /* XXX When Expat supports some way of figuring out how it was |
| 921 | compiled, this should check and set native_encoding |
| 922 | appropriately. |
| 923 | */ |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 924 | PyModule_AddStringConstant(m, "native_encoding", "UTF-8"); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 925 | |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 926 | d = PyModule_GetDict(m); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 927 | errors_module = PyDict_GetItem(d, errmod_name); |
| 928 | if (errors_module == NULL) { |
| 929 | errors_module = PyModule_New("pyexpat.errors"); |
| 930 | if (errors_module != NULL) { |
| 931 | sys_modules = PySys_GetObject("modules"); |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 932 | PyDict_SetItem(sys_modules, errmod_name, errors_module); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 933 | /* gives away the reference to errors_module */ |
| 934 | PyModule_AddObject(m, "errors", errors_module); |
Fred Drake | c23b523 | 2000-08-24 21:57:43 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 937 | Py_DECREF(errmod_name); |
| 938 | if (errors_module == NULL) |
| 939 | /* Don't code dump later! */ |
| 940 | return; |
| 941 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 942 | errors_dict = PyModule_GetDict(errors_module); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 943 | |
| 944 | #define MYCONST(name) \ |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 945 | PyModule_AddStringConstant(errors_module, #name, \ |
| 946 | (char*)XML_ErrorString(name)) |
Fred Drake | 7bd9f41 | 2000-07-04 23:51:31 +0000 | [diff] [blame] | 947 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 948 | MYCONST(XML_ERROR_NO_MEMORY); |
| 949 | MYCONST(XML_ERROR_SYNTAX); |
| 950 | MYCONST(XML_ERROR_NO_ELEMENTS); |
| 951 | MYCONST(XML_ERROR_INVALID_TOKEN); |
| 952 | MYCONST(XML_ERROR_UNCLOSED_TOKEN); |
| 953 | MYCONST(XML_ERROR_PARTIAL_CHAR); |
| 954 | MYCONST(XML_ERROR_TAG_MISMATCH); |
| 955 | MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE); |
| 956 | MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT); |
| 957 | MYCONST(XML_ERROR_PARAM_ENTITY_REF); |
| 958 | MYCONST(XML_ERROR_UNDEFINED_ENTITY); |
| 959 | MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF); |
| 960 | MYCONST(XML_ERROR_ASYNC_ENTITY); |
| 961 | MYCONST(XML_ERROR_BAD_CHAR_REF); |
| 962 | MYCONST(XML_ERROR_BINARY_ENTITY_REF); |
| 963 | MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF); |
| 964 | MYCONST(XML_ERROR_MISPLACED_XML_PI); |
| 965 | MYCONST(XML_ERROR_UNKNOWN_ENCODING); |
| 966 | MYCONST(XML_ERROR_INCORRECT_ENCODING); |
Fred Drake | 93adb69 | 2000-09-23 04:55:48 +0000 | [diff] [blame] | 967 | #undef MYCONST |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 970 | static void |
| 971 | clear_handlers(xmlparseobject *self) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 972 | { |
| 973 | int i = 0; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 974 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 975 | for (; handler_info[i].name!=NULL; i++) { |
| 976 | self->handlers[i]=NULL; |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 977 | handler_info[i].setter(self->itself, NULL); |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 978 | } |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 981 | typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 982 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 983 | static void |
| 984 | pyxml_UpdatePairedHandlers(xmlparseobject *self, |
| 985 | int startHandler, |
| 986 | int endHandler, |
| 987 | pairsetter setter) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 988 | { |
| 989 | void *start_handler=NULL; |
| 990 | void *end_handler=NULL; |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 991 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 992 | if (self->handlers[startHandler] |
| 993 | && self->handlers[endHandler]!=Py_None) { |
| 994 | start_handler=handler_info[startHandler].handler; |
| 995 | } |
| 996 | if (self->handlers[EndElement] |
| 997 | && self->handlers[EndElement] !=Py_None) { |
| 998 | end_handler=handler_info[endHandler].handler; |
| 999 | } |
| 1000 | setter(self->itself, start_handler, end_handler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1003 | static void |
| 1004 | pyxml_SetStartElementHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1005 | { |
| 1006 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1007 | StartElement, EndElement, |
| 1008 | (pairsetter)XML_SetElementHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1011 | static void |
| 1012 | pyxml_SetEndElementHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1013 | { |
| 1014 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1015 | StartElement, EndElement, |
| 1016 | (pairsetter)XML_SetElementHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1019 | static void |
| 1020 | pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1021 | { |
| 1022 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1023 | StartNamespaceDecl, EndNamespaceDecl, |
| 1024 | (pairsetter)XML_SetNamespaceDeclHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1027 | static void |
| 1028 | pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1029 | { |
| 1030 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1031 | StartNamespaceDecl, EndNamespaceDecl, |
| 1032 | (pairsetter)XML_SetNamespaceDeclHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1035 | static void |
| 1036 | pyxml_SetStartCdataSection(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1037 | { |
| 1038 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1039 | StartCdataSection, EndCdataSection, |
| 1040 | (pairsetter)XML_SetCdataSectionHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Fred Drake | 6f98762 | 2000-08-25 18:03:30 +0000 | [diff] [blame] | 1043 | static void |
| 1044 | pyxml_SetEndCdataSection(XML_Parser *parser, void *junk) |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1045 | { |
| 1046 | pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser), |
| 1047 | StartCdataSection, EndCdataSection, |
| 1048 | (pairsetter)XML_SetCdataSectionHandler); |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1051 | statichere struct HandlerInfo handler_info[] = { |
| 1052 | {"StartElementHandler", |
| 1053 | pyxml_SetStartElementHandler, |
| 1054 | (xmlhandler)my_StartElementHandler}, |
| 1055 | {"EndElementHandler", |
| 1056 | pyxml_SetEndElementHandler, |
| 1057 | (xmlhandler)my_EndElementHandler}, |
| 1058 | {"ProcessingInstructionHandler", |
| 1059 | (xmlhandlersetter)XML_SetProcessingInstructionHandler, |
| 1060 | (xmlhandler)my_ProcessingInstructionHandler}, |
| 1061 | {"CharacterDataHandler", |
| 1062 | (xmlhandlersetter)XML_SetCharacterDataHandler, |
| 1063 | (xmlhandler)my_CharacterDataHandler}, |
| 1064 | {"UnparsedEntityDeclHandler", |
| 1065 | (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler, |
| 1066 | (xmlhandler)my_UnparsedEntityDeclHandler }, |
| 1067 | {"NotationDeclHandler", |
| 1068 | (xmlhandlersetter)XML_SetNotationDeclHandler, |
| 1069 | (xmlhandler)my_NotationDeclHandler }, |
| 1070 | {"StartNamespaceDeclHandler", |
| 1071 | pyxml_SetStartNamespaceDeclHandler, |
| 1072 | (xmlhandler)my_StartNamespaceDeclHandler }, |
| 1073 | {"EndNamespaceDeclHandler", |
| 1074 | pyxml_SetEndNamespaceDeclHandler, |
| 1075 | (xmlhandler)my_EndNamespaceDeclHandler }, |
| 1076 | {"CommentHandler", |
| 1077 | (xmlhandlersetter)XML_SetCommentHandler, |
| 1078 | (xmlhandler)my_CommentHandler}, |
| 1079 | {"StartCdataSectionHandler", |
| 1080 | pyxml_SetStartCdataSection, |
| 1081 | (xmlhandler)my_StartCdataSectionHandler}, |
| 1082 | {"EndCdataSectionHandler", |
| 1083 | pyxml_SetEndCdataSection, |
| 1084 | (xmlhandler)my_EndCdataSectionHandler}, |
| 1085 | {"DefaultHandler", |
| 1086 | (xmlhandlersetter)XML_SetDefaultHandler, |
| 1087 | (xmlhandler)my_DefaultHandler}, |
| 1088 | {"DefaultHandlerExpand", |
| 1089 | (xmlhandlersetter)XML_SetDefaultHandlerExpand, |
| 1090 | (xmlhandler)my_DefaultHandlerExpandHandler}, |
| 1091 | {"NotStandaloneHandler", |
| 1092 | (xmlhandlersetter)XML_SetNotStandaloneHandler, |
| 1093 | (xmlhandler)my_NotStandaloneHandler}, |
| 1094 | {"ExternalEntityRefHandler", |
| 1095 | (xmlhandlersetter)XML_SetExternalEntityRefHandler, |
| 1096 | (xmlhandler)my_ExternalEntityRefHandler }, |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1097 | |
Fred Drake | 0582df9 | 2000-07-12 04:49:00 +0000 | [diff] [blame] | 1098 | {NULL, NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | b7f1053 | 2000-03-31 15:43:31 +0000 | [diff] [blame] | 1099 | }; |