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