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