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