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