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