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