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