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