1 | n/a | /*-------------------------------------------------------------------- |
---|
2 | n/a | * Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | * See http://www.python.org/psf/license for licensing details. |
---|
4 | n/a | * |
---|
5 | n/a | * _elementtree - C accelerator for xml.etree.ElementTree |
---|
6 | n/a | * Copyright (c) 1999-2009 by Secret Labs AB. All rights reserved. |
---|
7 | n/a | * Copyright (c) 1999-2009 by Fredrik Lundh. |
---|
8 | n/a | * |
---|
9 | n/a | * info@pythonware.com |
---|
10 | n/a | * http://www.pythonware.com |
---|
11 | n/a | *-------------------------------------------------------------------- |
---|
12 | n/a | */ |
---|
13 | n/a | |
---|
14 | n/a | #define PY_SSIZE_T_CLEAN |
---|
15 | n/a | |
---|
16 | n/a | #include "Python.h" |
---|
17 | n/a | #include "structmember.h" |
---|
18 | n/a | |
---|
19 | n/a | /* -------------------------------------------------------------------- */ |
---|
20 | n/a | /* configuration */ |
---|
21 | n/a | |
---|
22 | n/a | /* An element can hold this many children without extra memory |
---|
23 | n/a | allocations. */ |
---|
24 | n/a | #define STATIC_CHILDREN 4 |
---|
25 | n/a | |
---|
26 | n/a | /* For best performance, chose a value so that 80-90% of all nodes |
---|
27 | n/a | have no more than the given number of children. Set this to zero |
---|
28 | n/a | to minimize the size of the element structure itself (this only |
---|
29 | n/a | helps if you have lots of leaf nodes with attributes). */ |
---|
30 | n/a | |
---|
31 | n/a | /* Also note that pymalloc always allocates blocks in multiples of |
---|
32 | n/a | eight bytes. For the current C version of ElementTree, this means |
---|
33 | n/a | that the number of children should be an even number, at least on |
---|
34 | n/a | 32-bit platforms. */ |
---|
35 | n/a | |
---|
36 | n/a | /* -------------------------------------------------------------------- */ |
---|
37 | n/a | |
---|
38 | n/a | #if 0 |
---|
39 | n/a | static int memory = 0; |
---|
40 | n/a | #define ALLOC(size, comment)\ |
---|
41 | n/a | do { memory += size; printf("%8d - %s\n", memory, comment); } while (0) |
---|
42 | n/a | #define RELEASE(size, comment)\ |
---|
43 | n/a | do { memory -= size; printf("%8d - %s\n", memory, comment); } while (0) |
---|
44 | n/a | #else |
---|
45 | n/a | #define ALLOC(size, comment) |
---|
46 | n/a | #define RELEASE(size, comment) |
---|
47 | n/a | #endif |
---|
48 | n/a | |
---|
49 | n/a | /* compiler tweaks */ |
---|
50 | n/a | #if defined(_MSC_VER) |
---|
51 | n/a | #define LOCAL(type) static __inline type __fastcall |
---|
52 | n/a | #else |
---|
53 | n/a | #define LOCAL(type) static type |
---|
54 | n/a | #endif |
---|
55 | n/a | |
---|
56 | n/a | /* macros used to store 'join' flags in string object pointers. note |
---|
57 | n/a | that all use of text and tail as object pointers must be wrapped in |
---|
58 | n/a | JOIN_OBJ. see comments in the ElementObject definition for more |
---|
59 | n/a | info. */ |
---|
60 | n/a | #define JOIN_GET(p) ((uintptr_t) (p) & 1) |
---|
61 | n/a | #define JOIN_SET(p, flag) ((void*) ((uintptr_t) (JOIN_OBJ(p)) | (flag))) |
---|
62 | n/a | #define JOIN_OBJ(p) ((PyObject*) ((uintptr_t) (p) & ~(uintptr_t)1)) |
---|
63 | n/a | |
---|
64 | n/a | /* Py_CLEAR for a PyObject* that uses a join flag. Pass the pointer by |
---|
65 | n/a | * reference since this function sets it to NULL. |
---|
66 | n/a | */ |
---|
67 | n/a | static void _clear_joined_ptr(PyObject **p) |
---|
68 | n/a | { |
---|
69 | n/a | if (*p) { |
---|
70 | n/a | PyObject *tmp = JOIN_OBJ(*p); |
---|
71 | n/a | *p = NULL; |
---|
72 | n/a | Py_DECREF(tmp); |
---|
73 | n/a | } |
---|
74 | n/a | } |
---|
75 | n/a | |
---|
76 | n/a | /* Types defined by this extension */ |
---|
77 | n/a | static PyTypeObject Element_Type; |
---|
78 | n/a | static PyTypeObject ElementIter_Type; |
---|
79 | n/a | static PyTypeObject TreeBuilder_Type; |
---|
80 | n/a | static PyTypeObject XMLParser_Type; |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | /* Per-module state; PEP 3121 */ |
---|
84 | n/a | typedef struct { |
---|
85 | n/a | PyObject *parseerror_obj; |
---|
86 | n/a | PyObject *deepcopy_obj; |
---|
87 | n/a | PyObject *elementpath_obj; |
---|
88 | n/a | } elementtreestate; |
---|
89 | n/a | |
---|
90 | n/a | static struct PyModuleDef elementtreemodule; |
---|
91 | n/a | |
---|
92 | n/a | /* Given a module object (assumed to be _elementtree), get its per-module |
---|
93 | n/a | * state. |
---|
94 | n/a | */ |
---|
95 | n/a | #define ET_STATE(mod) ((elementtreestate *) PyModule_GetState(mod)) |
---|
96 | n/a | |
---|
97 | n/a | /* Find the module instance imported in the currently running sub-interpreter |
---|
98 | n/a | * and get its state. |
---|
99 | n/a | */ |
---|
100 | n/a | #define ET_STATE_GLOBAL \ |
---|
101 | n/a | ((elementtreestate *) PyModule_GetState(PyState_FindModule(&elementtreemodule))) |
---|
102 | n/a | |
---|
103 | n/a | static int |
---|
104 | n/a | elementtree_clear(PyObject *m) |
---|
105 | n/a | { |
---|
106 | n/a | elementtreestate *st = ET_STATE(m); |
---|
107 | n/a | Py_CLEAR(st->parseerror_obj); |
---|
108 | n/a | Py_CLEAR(st->deepcopy_obj); |
---|
109 | n/a | Py_CLEAR(st->elementpath_obj); |
---|
110 | n/a | return 0; |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | static int |
---|
114 | n/a | elementtree_traverse(PyObject *m, visitproc visit, void *arg) |
---|
115 | n/a | { |
---|
116 | n/a | elementtreestate *st = ET_STATE(m); |
---|
117 | n/a | Py_VISIT(st->parseerror_obj); |
---|
118 | n/a | Py_VISIT(st->deepcopy_obj); |
---|
119 | n/a | Py_VISIT(st->elementpath_obj); |
---|
120 | n/a | return 0; |
---|
121 | n/a | } |
---|
122 | n/a | |
---|
123 | n/a | static void |
---|
124 | n/a | elementtree_free(void *m) |
---|
125 | n/a | { |
---|
126 | n/a | elementtree_clear((PyObject *)m); |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | /* helpers */ |
---|
130 | n/a | |
---|
131 | n/a | LOCAL(PyObject*) |
---|
132 | n/a | list_join(PyObject* list) |
---|
133 | n/a | { |
---|
134 | n/a | /* join list elements (destroying the list in the process) */ |
---|
135 | n/a | PyObject* joiner; |
---|
136 | n/a | PyObject* result; |
---|
137 | n/a | |
---|
138 | n/a | joiner = PyUnicode_FromStringAndSize("", 0); |
---|
139 | n/a | if (!joiner) |
---|
140 | n/a | return NULL; |
---|
141 | n/a | result = PyUnicode_Join(joiner, list); |
---|
142 | n/a | Py_DECREF(joiner); |
---|
143 | n/a | if (result) |
---|
144 | n/a | Py_DECREF(list); |
---|
145 | n/a | return result; |
---|
146 | n/a | } |
---|
147 | n/a | |
---|
148 | n/a | /* Is the given object an empty dictionary? |
---|
149 | n/a | */ |
---|
150 | n/a | static int |
---|
151 | n/a | is_empty_dict(PyObject *obj) |
---|
152 | n/a | { |
---|
153 | n/a | return PyDict_CheckExact(obj) && PyDict_GET_SIZE(obj) == 0; |
---|
154 | n/a | } |
---|
155 | n/a | |
---|
156 | n/a | |
---|
157 | n/a | /* -------------------------------------------------------------------- */ |
---|
158 | n/a | /* the Element type */ |
---|
159 | n/a | |
---|
160 | n/a | typedef struct { |
---|
161 | n/a | |
---|
162 | n/a | /* attributes (a dictionary object), or None if no attributes */ |
---|
163 | n/a | PyObject* attrib; |
---|
164 | n/a | |
---|
165 | n/a | /* child elements */ |
---|
166 | n/a | Py_ssize_t length; /* actual number of items */ |
---|
167 | n/a | Py_ssize_t allocated; /* allocated items */ |
---|
168 | n/a | |
---|
169 | n/a | /* this either points to _children or to a malloced buffer */ |
---|
170 | n/a | PyObject* *children; |
---|
171 | n/a | |
---|
172 | n/a | PyObject* _children[STATIC_CHILDREN]; |
---|
173 | n/a | |
---|
174 | n/a | } ElementObjectExtra; |
---|
175 | n/a | |
---|
176 | n/a | typedef struct { |
---|
177 | n/a | PyObject_HEAD |
---|
178 | n/a | |
---|
179 | n/a | /* element tag (a string). */ |
---|
180 | n/a | PyObject* tag; |
---|
181 | n/a | |
---|
182 | n/a | /* text before first child. note that this is a tagged pointer; |
---|
183 | n/a | use JOIN_OBJ to get the object pointer. the join flag is used |
---|
184 | n/a | to distinguish lists created by the tree builder from lists |
---|
185 | n/a | assigned to the attribute by application code; the former |
---|
186 | n/a | should be joined before being returned to the user, the latter |
---|
187 | n/a | should be left intact. */ |
---|
188 | n/a | PyObject* text; |
---|
189 | n/a | |
---|
190 | n/a | /* text after this element, in parent. note that this is a tagged |
---|
191 | n/a | pointer; use JOIN_OBJ to get the object pointer. */ |
---|
192 | n/a | PyObject* tail; |
---|
193 | n/a | |
---|
194 | n/a | ElementObjectExtra* extra; |
---|
195 | n/a | |
---|
196 | n/a | PyObject *weakreflist; /* For tp_weaklistoffset */ |
---|
197 | n/a | |
---|
198 | n/a | } ElementObject; |
---|
199 | n/a | |
---|
200 | n/a | |
---|
201 | n/a | #define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type) |
---|
202 | n/a | |
---|
203 | n/a | /* -------------------------------------------------------------------- */ |
---|
204 | n/a | /* Element constructors and destructor */ |
---|
205 | n/a | |
---|
206 | n/a | LOCAL(int) |
---|
207 | n/a | create_extra(ElementObject* self, PyObject* attrib) |
---|
208 | n/a | { |
---|
209 | n/a | self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); |
---|
210 | n/a | if (!self->extra) { |
---|
211 | n/a | PyErr_NoMemory(); |
---|
212 | n/a | return -1; |
---|
213 | n/a | } |
---|
214 | n/a | |
---|
215 | n/a | if (!attrib) |
---|
216 | n/a | attrib = Py_None; |
---|
217 | n/a | |
---|
218 | n/a | Py_INCREF(attrib); |
---|
219 | n/a | self->extra->attrib = attrib; |
---|
220 | n/a | |
---|
221 | n/a | self->extra->length = 0; |
---|
222 | n/a | self->extra->allocated = STATIC_CHILDREN; |
---|
223 | n/a | self->extra->children = self->extra->_children; |
---|
224 | n/a | |
---|
225 | n/a | return 0; |
---|
226 | n/a | } |
---|
227 | n/a | |
---|
228 | n/a | LOCAL(void) |
---|
229 | n/a | dealloc_extra(ElementObject* self) |
---|
230 | n/a | { |
---|
231 | n/a | ElementObjectExtra *myextra; |
---|
232 | n/a | Py_ssize_t i; |
---|
233 | n/a | |
---|
234 | n/a | if (!self->extra) |
---|
235 | n/a | return; |
---|
236 | n/a | |
---|
237 | n/a | /* Avoid DECREFs calling into this code again (cycles, etc.) |
---|
238 | n/a | */ |
---|
239 | n/a | myextra = self->extra; |
---|
240 | n/a | self->extra = NULL; |
---|
241 | n/a | |
---|
242 | n/a | Py_DECREF(myextra->attrib); |
---|
243 | n/a | |
---|
244 | n/a | for (i = 0; i < myextra->length; i++) |
---|
245 | n/a | Py_DECREF(myextra->children[i]); |
---|
246 | n/a | |
---|
247 | n/a | if (myextra->children != myextra->_children) |
---|
248 | n/a | PyObject_Free(myextra->children); |
---|
249 | n/a | |
---|
250 | n/a | PyObject_Free(myextra); |
---|
251 | n/a | } |
---|
252 | n/a | |
---|
253 | n/a | /* Convenience internal function to create new Element objects with the given |
---|
254 | n/a | * tag and attributes. |
---|
255 | n/a | */ |
---|
256 | n/a | LOCAL(PyObject*) |
---|
257 | n/a | create_new_element(PyObject* tag, PyObject* attrib) |
---|
258 | n/a | { |
---|
259 | n/a | ElementObject* self; |
---|
260 | n/a | |
---|
261 | n/a | self = PyObject_GC_New(ElementObject, &Element_Type); |
---|
262 | n/a | if (self == NULL) |
---|
263 | n/a | return NULL; |
---|
264 | n/a | self->extra = NULL; |
---|
265 | n/a | |
---|
266 | n/a | Py_INCREF(tag); |
---|
267 | n/a | self->tag = tag; |
---|
268 | n/a | |
---|
269 | n/a | Py_INCREF(Py_None); |
---|
270 | n/a | self->text = Py_None; |
---|
271 | n/a | |
---|
272 | n/a | Py_INCREF(Py_None); |
---|
273 | n/a | self->tail = Py_None; |
---|
274 | n/a | |
---|
275 | n/a | self->weakreflist = NULL; |
---|
276 | n/a | |
---|
277 | n/a | ALLOC(sizeof(ElementObject), "create element"); |
---|
278 | n/a | PyObject_GC_Track(self); |
---|
279 | n/a | |
---|
280 | n/a | if (attrib != Py_None && !is_empty_dict(attrib)) { |
---|
281 | n/a | if (create_extra(self, attrib) < 0) { |
---|
282 | n/a | Py_DECREF(self); |
---|
283 | n/a | return NULL; |
---|
284 | n/a | } |
---|
285 | n/a | } |
---|
286 | n/a | |
---|
287 | n/a | return (PyObject*) self; |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | static PyObject * |
---|
291 | n/a | element_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
292 | n/a | { |
---|
293 | n/a | ElementObject *e = (ElementObject *)type->tp_alloc(type, 0); |
---|
294 | n/a | if (e != NULL) { |
---|
295 | n/a | Py_INCREF(Py_None); |
---|
296 | n/a | e->tag = Py_None; |
---|
297 | n/a | |
---|
298 | n/a | Py_INCREF(Py_None); |
---|
299 | n/a | e->text = Py_None; |
---|
300 | n/a | |
---|
301 | n/a | Py_INCREF(Py_None); |
---|
302 | n/a | e->tail = Py_None; |
---|
303 | n/a | |
---|
304 | n/a | e->extra = NULL; |
---|
305 | n/a | e->weakreflist = NULL; |
---|
306 | n/a | } |
---|
307 | n/a | return (PyObject *)e; |
---|
308 | n/a | } |
---|
309 | n/a | |
---|
310 | n/a | /* Helper function for extracting the attrib dictionary from a keywords dict. |
---|
311 | n/a | * This is required by some constructors/functions in this module that can |
---|
312 | n/a | * either accept attrib as a keyword argument or all attributes splashed |
---|
313 | n/a | * directly into *kwds. |
---|
314 | n/a | * |
---|
315 | n/a | * Return a dictionary with the content of kwds merged into the content of |
---|
316 | n/a | * attrib. If there is no attrib keyword, return a copy of kwds. |
---|
317 | n/a | */ |
---|
318 | n/a | static PyObject* |
---|
319 | n/a | get_attrib_from_keywords(PyObject *kwds) |
---|
320 | n/a | { |
---|
321 | n/a | PyObject *attrib_str = PyUnicode_FromString("attrib"); |
---|
322 | n/a | PyObject *attrib = PyDict_GetItem(kwds, attrib_str); |
---|
323 | n/a | |
---|
324 | n/a | if (attrib) { |
---|
325 | n/a | /* If attrib was found in kwds, copy its value and remove it from |
---|
326 | n/a | * kwds |
---|
327 | n/a | */ |
---|
328 | n/a | if (!PyDict_Check(attrib)) { |
---|
329 | n/a | Py_DECREF(attrib_str); |
---|
330 | n/a | PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s", |
---|
331 | n/a | Py_TYPE(attrib)->tp_name); |
---|
332 | n/a | return NULL; |
---|
333 | n/a | } |
---|
334 | n/a | attrib = PyDict_Copy(attrib); |
---|
335 | n/a | PyDict_DelItem(kwds, attrib_str); |
---|
336 | n/a | } else { |
---|
337 | n/a | attrib = PyDict_New(); |
---|
338 | n/a | } |
---|
339 | n/a | |
---|
340 | n/a | Py_DECREF(attrib_str); |
---|
341 | n/a | |
---|
342 | n/a | /* attrib can be NULL if PyDict_New failed */ |
---|
343 | n/a | if (attrib) |
---|
344 | n/a | if (PyDict_Update(attrib, kwds) < 0) |
---|
345 | n/a | return NULL; |
---|
346 | n/a | return attrib; |
---|
347 | n/a | } |
---|
348 | n/a | |
---|
349 | n/a | /*[clinic input] |
---|
350 | n/a | module _elementtree |
---|
351 | n/a | class _elementtree.Element "ElementObject *" "&Element_Type" |
---|
352 | n/a | class _elementtree.TreeBuilder "TreeBuilderObject *" "&TreeBuilder_Type" |
---|
353 | n/a | class _elementtree.XMLParser "XMLParserObject *" "&XMLParser_Type" |
---|
354 | n/a | [clinic start generated code]*/ |
---|
355 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=159aa50a54061c22]*/ |
---|
356 | n/a | |
---|
357 | n/a | static int |
---|
358 | n/a | element_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
359 | n/a | { |
---|
360 | n/a | PyObject *tag; |
---|
361 | n/a | PyObject *tmp; |
---|
362 | n/a | PyObject *attrib = NULL; |
---|
363 | n/a | ElementObject *self_elem; |
---|
364 | n/a | |
---|
365 | n/a | if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib)) |
---|
366 | n/a | return -1; |
---|
367 | n/a | |
---|
368 | n/a | if (attrib) { |
---|
369 | n/a | /* attrib passed as positional arg */ |
---|
370 | n/a | attrib = PyDict_Copy(attrib); |
---|
371 | n/a | if (!attrib) |
---|
372 | n/a | return -1; |
---|
373 | n/a | if (kwds) { |
---|
374 | n/a | if (PyDict_Update(attrib, kwds) < 0) { |
---|
375 | n/a | Py_DECREF(attrib); |
---|
376 | n/a | return -1; |
---|
377 | n/a | } |
---|
378 | n/a | } |
---|
379 | n/a | } else if (kwds) { |
---|
380 | n/a | /* have keywords args */ |
---|
381 | n/a | attrib = get_attrib_from_keywords(kwds); |
---|
382 | n/a | if (!attrib) |
---|
383 | n/a | return -1; |
---|
384 | n/a | } |
---|
385 | n/a | |
---|
386 | n/a | self_elem = (ElementObject *)self; |
---|
387 | n/a | |
---|
388 | n/a | if (attrib != NULL && !is_empty_dict(attrib)) { |
---|
389 | n/a | if (create_extra(self_elem, attrib) < 0) { |
---|
390 | n/a | Py_DECREF(attrib); |
---|
391 | n/a | return -1; |
---|
392 | n/a | } |
---|
393 | n/a | } |
---|
394 | n/a | |
---|
395 | n/a | /* We own a reference to attrib here and it's no longer needed. */ |
---|
396 | n/a | Py_XDECREF(attrib); |
---|
397 | n/a | |
---|
398 | n/a | /* Replace the objects already pointed to by tag, text and tail. */ |
---|
399 | n/a | Py_INCREF(tag); |
---|
400 | n/a | Py_XSETREF(self_elem->tag, tag); |
---|
401 | n/a | |
---|
402 | n/a | tmp = self_elem->text; |
---|
403 | n/a | Py_INCREF(Py_None); |
---|
404 | n/a | self_elem->text = Py_None; |
---|
405 | n/a | Py_DECREF(JOIN_OBJ(tmp)); |
---|
406 | n/a | |
---|
407 | n/a | tmp = self_elem->tail; |
---|
408 | n/a | Py_INCREF(Py_None); |
---|
409 | n/a | self_elem->tail = Py_None; |
---|
410 | n/a | Py_DECREF(JOIN_OBJ(tmp)); |
---|
411 | n/a | |
---|
412 | n/a | return 0; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | LOCAL(int) |
---|
416 | n/a | element_resize(ElementObject* self, Py_ssize_t extra) |
---|
417 | n/a | { |
---|
418 | n/a | Py_ssize_t size; |
---|
419 | n/a | PyObject* *children; |
---|
420 | n/a | |
---|
421 | n/a | /* make sure self->children can hold the given number of extra |
---|
422 | n/a | elements. set an exception and return -1 if allocation failed */ |
---|
423 | n/a | |
---|
424 | n/a | if (!self->extra) { |
---|
425 | n/a | if (create_extra(self, NULL) < 0) |
---|
426 | n/a | return -1; |
---|
427 | n/a | } |
---|
428 | n/a | |
---|
429 | n/a | size = self->extra->length + extra; /* never overflows */ |
---|
430 | n/a | |
---|
431 | n/a | if (size > self->extra->allocated) { |
---|
432 | n/a | /* use Python 2.4's list growth strategy */ |
---|
433 | n/a | size = (size >> 3) + (size < 9 ? 3 : 6) + size; |
---|
434 | n/a | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer "children" |
---|
435 | n/a | * which needs at least 4 bytes. |
---|
436 | n/a | * Although it's a false alarm always assume at least one child to |
---|
437 | n/a | * be safe. |
---|
438 | n/a | */ |
---|
439 | n/a | size = size ? size : 1; |
---|
440 | n/a | if ((size_t)size > PY_SSIZE_T_MAX/sizeof(PyObject*)) |
---|
441 | n/a | goto nomemory; |
---|
442 | n/a | if (self->extra->children != self->extra->_children) { |
---|
443 | n/a | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer |
---|
444 | n/a | * "children", which needs at least 4 bytes. Although it's a |
---|
445 | n/a | * false alarm always assume at least one child to be safe. |
---|
446 | n/a | */ |
---|
447 | n/a | children = PyObject_Realloc(self->extra->children, |
---|
448 | n/a | size * sizeof(PyObject*)); |
---|
449 | n/a | if (!children) |
---|
450 | n/a | goto nomemory; |
---|
451 | n/a | } else { |
---|
452 | n/a | children = PyObject_Malloc(size * sizeof(PyObject*)); |
---|
453 | n/a | if (!children) |
---|
454 | n/a | goto nomemory; |
---|
455 | n/a | /* copy existing children from static area to malloc buffer */ |
---|
456 | n/a | memcpy(children, self->extra->children, |
---|
457 | n/a | self->extra->length * sizeof(PyObject*)); |
---|
458 | n/a | } |
---|
459 | n/a | self->extra->children = children; |
---|
460 | n/a | self->extra->allocated = size; |
---|
461 | n/a | } |
---|
462 | n/a | |
---|
463 | n/a | return 0; |
---|
464 | n/a | |
---|
465 | n/a | nomemory: |
---|
466 | n/a | PyErr_NoMemory(); |
---|
467 | n/a | return -1; |
---|
468 | n/a | } |
---|
469 | n/a | |
---|
470 | n/a | LOCAL(int) |
---|
471 | n/a | element_add_subelement(ElementObject* self, PyObject* element) |
---|
472 | n/a | { |
---|
473 | n/a | /* add a child element to a parent */ |
---|
474 | n/a | |
---|
475 | n/a | if (element_resize(self, 1) < 0) |
---|
476 | n/a | return -1; |
---|
477 | n/a | |
---|
478 | n/a | Py_INCREF(element); |
---|
479 | n/a | self->extra->children[self->extra->length] = element; |
---|
480 | n/a | |
---|
481 | n/a | self->extra->length++; |
---|
482 | n/a | |
---|
483 | n/a | return 0; |
---|
484 | n/a | } |
---|
485 | n/a | |
---|
486 | n/a | LOCAL(PyObject*) |
---|
487 | n/a | element_get_attrib(ElementObject* self) |
---|
488 | n/a | { |
---|
489 | n/a | /* return borrowed reference to attrib dictionary */ |
---|
490 | n/a | /* note: this function assumes that the extra section exists */ |
---|
491 | n/a | |
---|
492 | n/a | PyObject* res = self->extra->attrib; |
---|
493 | n/a | |
---|
494 | n/a | if (res == Py_None) { |
---|
495 | n/a | /* create missing dictionary */ |
---|
496 | n/a | res = PyDict_New(); |
---|
497 | n/a | if (!res) |
---|
498 | n/a | return NULL; |
---|
499 | n/a | Py_DECREF(Py_None); |
---|
500 | n/a | self->extra->attrib = res; |
---|
501 | n/a | } |
---|
502 | n/a | |
---|
503 | n/a | return res; |
---|
504 | n/a | } |
---|
505 | n/a | |
---|
506 | n/a | LOCAL(PyObject*) |
---|
507 | n/a | element_get_text(ElementObject* self) |
---|
508 | n/a | { |
---|
509 | n/a | /* return borrowed reference to text attribute */ |
---|
510 | n/a | |
---|
511 | n/a | PyObject* res = self->text; |
---|
512 | n/a | |
---|
513 | n/a | if (JOIN_GET(res)) { |
---|
514 | n/a | res = JOIN_OBJ(res); |
---|
515 | n/a | if (PyList_CheckExact(res)) { |
---|
516 | n/a | res = list_join(res); |
---|
517 | n/a | if (!res) |
---|
518 | n/a | return NULL; |
---|
519 | n/a | self->text = res; |
---|
520 | n/a | } |
---|
521 | n/a | } |
---|
522 | n/a | |
---|
523 | n/a | return res; |
---|
524 | n/a | } |
---|
525 | n/a | |
---|
526 | n/a | LOCAL(PyObject*) |
---|
527 | n/a | element_get_tail(ElementObject* self) |
---|
528 | n/a | { |
---|
529 | n/a | /* return borrowed reference to text attribute */ |
---|
530 | n/a | |
---|
531 | n/a | PyObject* res = self->tail; |
---|
532 | n/a | |
---|
533 | n/a | if (JOIN_GET(res)) { |
---|
534 | n/a | res = JOIN_OBJ(res); |
---|
535 | n/a | if (PyList_CheckExact(res)) { |
---|
536 | n/a | res = list_join(res); |
---|
537 | n/a | if (!res) |
---|
538 | n/a | return NULL; |
---|
539 | n/a | self->tail = res; |
---|
540 | n/a | } |
---|
541 | n/a | } |
---|
542 | n/a | |
---|
543 | n/a | return res; |
---|
544 | n/a | } |
---|
545 | n/a | |
---|
546 | n/a | static PyObject* |
---|
547 | n/a | subelement(PyObject *self, PyObject *args, PyObject *kwds) |
---|
548 | n/a | { |
---|
549 | n/a | PyObject* elem; |
---|
550 | n/a | |
---|
551 | n/a | ElementObject* parent; |
---|
552 | n/a | PyObject* tag; |
---|
553 | n/a | PyObject* attrib = NULL; |
---|
554 | n/a | if (!PyArg_ParseTuple(args, "O!O|O!:SubElement", |
---|
555 | n/a | &Element_Type, &parent, &tag, |
---|
556 | n/a | &PyDict_Type, &attrib)) { |
---|
557 | n/a | return NULL; |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | if (attrib) { |
---|
561 | n/a | /* attrib passed as positional arg */ |
---|
562 | n/a | attrib = PyDict_Copy(attrib); |
---|
563 | n/a | if (!attrib) |
---|
564 | n/a | return NULL; |
---|
565 | n/a | if (kwds) { |
---|
566 | n/a | if (PyDict_Update(attrib, kwds) < 0) { |
---|
567 | n/a | return NULL; |
---|
568 | n/a | } |
---|
569 | n/a | } |
---|
570 | n/a | } else if (kwds) { |
---|
571 | n/a | /* have keyword args */ |
---|
572 | n/a | attrib = get_attrib_from_keywords(kwds); |
---|
573 | n/a | if (!attrib) |
---|
574 | n/a | return NULL; |
---|
575 | n/a | } else { |
---|
576 | n/a | /* no attrib arg, no kwds, so no attribute */ |
---|
577 | n/a | Py_INCREF(Py_None); |
---|
578 | n/a | attrib = Py_None; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | elem = create_new_element(tag, attrib); |
---|
582 | n/a | Py_DECREF(attrib); |
---|
583 | n/a | if (elem == NULL) |
---|
584 | n/a | return NULL; |
---|
585 | n/a | |
---|
586 | n/a | if (element_add_subelement(parent, elem) < 0) { |
---|
587 | n/a | Py_DECREF(elem); |
---|
588 | n/a | return NULL; |
---|
589 | n/a | } |
---|
590 | n/a | |
---|
591 | n/a | return elem; |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | static int |
---|
595 | n/a | element_gc_traverse(ElementObject *self, visitproc visit, void *arg) |
---|
596 | n/a | { |
---|
597 | n/a | Py_VISIT(self->tag); |
---|
598 | n/a | Py_VISIT(JOIN_OBJ(self->text)); |
---|
599 | n/a | Py_VISIT(JOIN_OBJ(self->tail)); |
---|
600 | n/a | |
---|
601 | n/a | if (self->extra) { |
---|
602 | n/a | Py_ssize_t i; |
---|
603 | n/a | Py_VISIT(self->extra->attrib); |
---|
604 | n/a | |
---|
605 | n/a | for (i = 0; i < self->extra->length; ++i) |
---|
606 | n/a | Py_VISIT(self->extra->children[i]); |
---|
607 | n/a | } |
---|
608 | n/a | return 0; |
---|
609 | n/a | } |
---|
610 | n/a | |
---|
611 | n/a | static int |
---|
612 | n/a | element_gc_clear(ElementObject *self) |
---|
613 | n/a | { |
---|
614 | n/a | Py_CLEAR(self->tag); |
---|
615 | n/a | _clear_joined_ptr(&self->text); |
---|
616 | n/a | _clear_joined_ptr(&self->tail); |
---|
617 | n/a | |
---|
618 | n/a | /* After dropping all references from extra, it's no longer valid anyway, |
---|
619 | n/a | * so fully deallocate it. |
---|
620 | n/a | */ |
---|
621 | n/a | dealloc_extra(self); |
---|
622 | n/a | return 0; |
---|
623 | n/a | } |
---|
624 | n/a | |
---|
625 | n/a | static void |
---|
626 | n/a | element_dealloc(ElementObject* self) |
---|
627 | n/a | { |
---|
628 | n/a | PyObject_GC_UnTrack(self); |
---|
629 | n/a | Py_TRASHCAN_SAFE_BEGIN(self) |
---|
630 | n/a | |
---|
631 | n/a | if (self->weakreflist != NULL) |
---|
632 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
633 | n/a | |
---|
634 | n/a | /* element_gc_clear clears all references and deallocates extra |
---|
635 | n/a | */ |
---|
636 | n/a | element_gc_clear(self); |
---|
637 | n/a | |
---|
638 | n/a | RELEASE(sizeof(ElementObject), "destroy element"); |
---|
639 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
---|
640 | n/a | Py_TRASHCAN_SAFE_END(self) |
---|
641 | n/a | } |
---|
642 | n/a | |
---|
643 | n/a | /* -------------------------------------------------------------------- */ |
---|
644 | n/a | |
---|
645 | n/a | /*[clinic input] |
---|
646 | n/a | _elementtree.Element.append |
---|
647 | n/a | |
---|
648 | n/a | subelement: object(subclass_of='&Element_Type') |
---|
649 | n/a | / |
---|
650 | n/a | |
---|
651 | n/a | [clinic start generated code]*/ |
---|
652 | n/a | |
---|
653 | n/a | static PyObject * |
---|
654 | n/a | _elementtree_Element_append_impl(ElementObject *self, PyObject *subelement) |
---|
655 | n/a | /*[clinic end generated code: output=54a884b7cf2295f4 input=3ed648beb5bfa22a]*/ |
---|
656 | n/a | { |
---|
657 | n/a | if (element_add_subelement(self, subelement) < 0) |
---|
658 | n/a | return NULL; |
---|
659 | n/a | |
---|
660 | n/a | Py_RETURN_NONE; |
---|
661 | n/a | } |
---|
662 | n/a | |
---|
663 | n/a | /*[clinic input] |
---|
664 | n/a | _elementtree.Element.clear |
---|
665 | n/a | |
---|
666 | n/a | [clinic start generated code]*/ |
---|
667 | n/a | |
---|
668 | n/a | static PyObject * |
---|
669 | n/a | _elementtree_Element_clear_impl(ElementObject *self) |
---|
670 | n/a | /*[clinic end generated code: output=8bcd7a51f94cfff6 input=3c719ff94bf45dd6]*/ |
---|
671 | n/a | { |
---|
672 | n/a | dealloc_extra(self); |
---|
673 | n/a | |
---|
674 | n/a | Py_INCREF(Py_None); |
---|
675 | n/a | Py_DECREF(JOIN_OBJ(self->text)); |
---|
676 | n/a | self->text = Py_None; |
---|
677 | n/a | |
---|
678 | n/a | Py_INCREF(Py_None); |
---|
679 | n/a | Py_DECREF(JOIN_OBJ(self->tail)); |
---|
680 | n/a | self->tail = Py_None; |
---|
681 | n/a | |
---|
682 | n/a | Py_RETURN_NONE; |
---|
683 | n/a | } |
---|
684 | n/a | |
---|
685 | n/a | /*[clinic input] |
---|
686 | n/a | _elementtree.Element.__copy__ |
---|
687 | n/a | |
---|
688 | n/a | [clinic start generated code]*/ |
---|
689 | n/a | |
---|
690 | n/a | static PyObject * |
---|
691 | n/a | _elementtree_Element___copy___impl(ElementObject *self) |
---|
692 | n/a | /*[clinic end generated code: output=2c701ebff7247781 input=ad87aaebe95675bf]*/ |
---|
693 | n/a | { |
---|
694 | n/a | Py_ssize_t i; |
---|
695 | n/a | ElementObject* element; |
---|
696 | n/a | |
---|
697 | n/a | element = (ElementObject*) create_new_element( |
---|
698 | n/a | self->tag, (self->extra) ? self->extra->attrib : Py_None); |
---|
699 | n/a | if (!element) |
---|
700 | n/a | return NULL; |
---|
701 | n/a | |
---|
702 | n/a | Py_DECREF(JOIN_OBJ(element->text)); |
---|
703 | n/a | element->text = self->text; |
---|
704 | n/a | Py_INCREF(JOIN_OBJ(element->text)); |
---|
705 | n/a | |
---|
706 | n/a | Py_DECREF(JOIN_OBJ(element->tail)); |
---|
707 | n/a | element->tail = self->tail; |
---|
708 | n/a | Py_INCREF(JOIN_OBJ(element->tail)); |
---|
709 | n/a | |
---|
710 | n/a | if (self->extra) { |
---|
711 | n/a | if (element_resize(element, self->extra->length) < 0) { |
---|
712 | n/a | Py_DECREF(element); |
---|
713 | n/a | return NULL; |
---|
714 | n/a | } |
---|
715 | n/a | |
---|
716 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
717 | n/a | Py_INCREF(self->extra->children[i]); |
---|
718 | n/a | element->extra->children[i] = self->extra->children[i]; |
---|
719 | n/a | } |
---|
720 | n/a | |
---|
721 | n/a | element->extra->length = self->extra->length; |
---|
722 | n/a | } |
---|
723 | n/a | |
---|
724 | n/a | return (PyObject*) element; |
---|
725 | n/a | } |
---|
726 | n/a | |
---|
727 | n/a | /* Helper for a deep copy. */ |
---|
728 | n/a | LOCAL(PyObject *) deepcopy(PyObject *, PyObject *); |
---|
729 | n/a | |
---|
730 | n/a | /*[clinic input] |
---|
731 | n/a | _elementtree.Element.__deepcopy__ |
---|
732 | n/a | |
---|
733 | n/a | memo: object |
---|
734 | n/a | / |
---|
735 | n/a | |
---|
736 | n/a | [clinic start generated code]*/ |
---|
737 | n/a | |
---|
738 | n/a | static PyObject * |
---|
739 | n/a | _elementtree_Element___deepcopy__(ElementObject *self, PyObject *memo) |
---|
740 | n/a | /*[clinic end generated code: output=d1f19851d17bf239 input=df24c2b602430b77]*/ |
---|
741 | n/a | { |
---|
742 | n/a | Py_ssize_t i; |
---|
743 | n/a | ElementObject* element; |
---|
744 | n/a | PyObject* tag; |
---|
745 | n/a | PyObject* attrib; |
---|
746 | n/a | PyObject* text; |
---|
747 | n/a | PyObject* tail; |
---|
748 | n/a | PyObject* id; |
---|
749 | n/a | |
---|
750 | n/a | tag = deepcopy(self->tag, memo); |
---|
751 | n/a | if (!tag) |
---|
752 | n/a | return NULL; |
---|
753 | n/a | |
---|
754 | n/a | if (self->extra) { |
---|
755 | n/a | attrib = deepcopy(self->extra->attrib, memo); |
---|
756 | n/a | if (!attrib) { |
---|
757 | n/a | Py_DECREF(tag); |
---|
758 | n/a | return NULL; |
---|
759 | n/a | } |
---|
760 | n/a | } else { |
---|
761 | n/a | Py_INCREF(Py_None); |
---|
762 | n/a | attrib = Py_None; |
---|
763 | n/a | } |
---|
764 | n/a | |
---|
765 | n/a | element = (ElementObject*) create_new_element(tag, attrib); |
---|
766 | n/a | |
---|
767 | n/a | Py_DECREF(tag); |
---|
768 | n/a | Py_DECREF(attrib); |
---|
769 | n/a | |
---|
770 | n/a | if (!element) |
---|
771 | n/a | return NULL; |
---|
772 | n/a | |
---|
773 | n/a | text = deepcopy(JOIN_OBJ(self->text), memo); |
---|
774 | n/a | if (!text) |
---|
775 | n/a | goto error; |
---|
776 | n/a | Py_DECREF(element->text); |
---|
777 | n/a | element->text = JOIN_SET(text, JOIN_GET(self->text)); |
---|
778 | n/a | |
---|
779 | n/a | tail = deepcopy(JOIN_OBJ(self->tail), memo); |
---|
780 | n/a | if (!tail) |
---|
781 | n/a | goto error; |
---|
782 | n/a | Py_DECREF(element->tail); |
---|
783 | n/a | element->tail = JOIN_SET(tail, JOIN_GET(self->tail)); |
---|
784 | n/a | |
---|
785 | n/a | if (self->extra) { |
---|
786 | n/a | if (element_resize(element, self->extra->length) < 0) |
---|
787 | n/a | goto error; |
---|
788 | n/a | |
---|
789 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
790 | n/a | PyObject* child = deepcopy(self->extra->children[i], memo); |
---|
791 | n/a | if (!child) { |
---|
792 | n/a | element->extra->length = i; |
---|
793 | n/a | goto error; |
---|
794 | n/a | } |
---|
795 | n/a | element->extra->children[i] = child; |
---|
796 | n/a | } |
---|
797 | n/a | |
---|
798 | n/a | element->extra->length = self->extra->length; |
---|
799 | n/a | } |
---|
800 | n/a | |
---|
801 | n/a | /* add object to memo dictionary (so deepcopy won't visit it again) */ |
---|
802 | n/a | id = PyLong_FromSsize_t((uintptr_t) self); |
---|
803 | n/a | if (!id) |
---|
804 | n/a | goto error; |
---|
805 | n/a | |
---|
806 | n/a | i = PyDict_SetItem(memo, id, (PyObject*) element); |
---|
807 | n/a | |
---|
808 | n/a | Py_DECREF(id); |
---|
809 | n/a | |
---|
810 | n/a | if (i < 0) |
---|
811 | n/a | goto error; |
---|
812 | n/a | |
---|
813 | n/a | return (PyObject*) element; |
---|
814 | n/a | |
---|
815 | n/a | error: |
---|
816 | n/a | Py_DECREF(element); |
---|
817 | n/a | return NULL; |
---|
818 | n/a | } |
---|
819 | n/a | |
---|
820 | n/a | LOCAL(PyObject *) |
---|
821 | n/a | deepcopy(PyObject *object, PyObject *memo) |
---|
822 | n/a | { |
---|
823 | n/a | /* do a deep copy of the given object */ |
---|
824 | n/a | elementtreestate *st; |
---|
825 | n/a | PyObject *stack[2]; |
---|
826 | n/a | |
---|
827 | n/a | /* Fast paths */ |
---|
828 | n/a | if (object == Py_None || PyUnicode_CheckExact(object)) { |
---|
829 | n/a | Py_INCREF(object); |
---|
830 | n/a | return object; |
---|
831 | n/a | } |
---|
832 | n/a | |
---|
833 | n/a | if (Py_REFCNT(object) == 1) { |
---|
834 | n/a | if (PyDict_CheckExact(object)) { |
---|
835 | n/a | PyObject *key, *value; |
---|
836 | n/a | Py_ssize_t pos = 0; |
---|
837 | n/a | int simple = 1; |
---|
838 | n/a | while (PyDict_Next(object, &pos, &key, &value)) { |
---|
839 | n/a | if (!PyUnicode_CheckExact(key) || !PyUnicode_CheckExact(value)) { |
---|
840 | n/a | simple = 0; |
---|
841 | n/a | break; |
---|
842 | n/a | } |
---|
843 | n/a | } |
---|
844 | n/a | if (simple) |
---|
845 | n/a | return PyDict_Copy(object); |
---|
846 | n/a | /* Fall through to general case */ |
---|
847 | n/a | } |
---|
848 | n/a | else if (Element_CheckExact(object)) { |
---|
849 | n/a | return _elementtree_Element___deepcopy__((ElementObject *)object, memo); |
---|
850 | n/a | } |
---|
851 | n/a | } |
---|
852 | n/a | |
---|
853 | n/a | /* General case */ |
---|
854 | n/a | st = ET_STATE_GLOBAL; |
---|
855 | n/a | if (!st->deepcopy_obj) { |
---|
856 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
857 | n/a | "deepcopy helper not found"); |
---|
858 | n/a | return NULL; |
---|
859 | n/a | } |
---|
860 | n/a | |
---|
861 | n/a | stack[0] = object; |
---|
862 | n/a | stack[1] = memo; |
---|
863 | n/a | return _PyObject_FastCall(st->deepcopy_obj, stack, 2); |
---|
864 | n/a | } |
---|
865 | n/a | |
---|
866 | n/a | |
---|
867 | n/a | /*[clinic input] |
---|
868 | n/a | _elementtree.Element.__sizeof__ -> Py_ssize_t |
---|
869 | n/a | |
---|
870 | n/a | [clinic start generated code]*/ |
---|
871 | n/a | |
---|
872 | n/a | static Py_ssize_t |
---|
873 | n/a | _elementtree_Element___sizeof___impl(ElementObject *self) |
---|
874 | n/a | /*[clinic end generated code: output=bf73867721008000 input=70f4b323d55a17c1]*/ |
---|
875 | n/a | { |
---|
876 | n/a | Py_ssize_t result = _PyObject_SIZE(Py_TYPE(self)); |
---|
877 | n/a | if (self->extra) { |
---|
878 | n/a | result += sizeof(ElementObjectExtra); |
---|
879 | n/a | if (self->extra->children != self->extra->_children) |
---|
880 | n/a | result += sizeof(PyObject*) * self->extra->allocated; |
---|
881 | n/a | } |
---|
882 | n/a | return result; |
---|
883 | n/a | } |
---|
884 | n/a | |
---|
885 | n/a | /* dict keys for getstate/setstate. */ |
---|
886 | n/a | #define PICKLED_TAG "tag" |
---|
887 | n/a | #define PICKLED_CHILDREN "_children" |
---|
888 | n/a | #define PICKLED_ATTRIB "attrib" |
---|
889 | n/a | #define PICKLED_TAIL "tail" |
---|
890 | n/a | #define PICKLED_TEXT "text" |
---|
891 | n/a | |
---|
892 | n/a | /* __getstate__ returns a fabricated instance dict as in the pure-Python |
---|
893 | n/a | * Element implementation, for interoperability/interchangeability. This |
---|
894 | n/a | * makes the pure-Python implementation details an API, but (a) there aren't |
---|
895 | n/a | * any unnecessary structures there; and (b) it buys compatibility with 3.2 |
---|
896 | n/a | * pickles. See issue #16076. |
---|
897 | n/a | */ |
---|
898 | n/a | /*[clinic input] |
---|
899 | n/a | _elementtree.Element.__getstate__ |
---|
900 | n/a | |
---|
901 | n/a | [clinic start generated code]*/ |
---|
902 | n/a | |
---|
903 | n/a | static PyObject * |
---|
904 | n/a | _elementtree_Element___getstate___impl(ElementObject *self) |
---|
905 | n/a | /*[clinic end generated code: output=37279aeeb6bb5b04 input=f0d16d7ec2f7adc1]*/ |
---|
906 | n/a | { |
---|
907 | n/a | Py_ssize_t i, noattrib; |
---|
908 | n/a | PyObject *instancedict = NULL, *children; |
---|
909 | n/a | |
---|
910 | n/a | /* Build a list of children. */ |
---|
911 | n/a | children = PyList_New(self->extra ? self->extra->length : 0); |
---|
912 | n/a | if (!children) |
---|
913 | n/a | return NULL; |
---|
914 | n/a | for (i = 0; i < PyList_GET_SIZE(children); i++) { |
---|
915 | n/a | PyObject *child = self->extra->children[i]; |
---|
916 | n/a | Py_INCREF(child); |
---|
917 | n/a | PyList_SET_ITEM(children, i, child); |
---|
918 | n/a | } |
---|
919 | n/a | |
---|
920 | n/a | /* Construct the state object. */ |
---|
921 | n/a | noattrib = (self->extra == NULL || self->extra->attrib == Py_None); |
---|
922 | n/a | if (noattrib) |
---|
923 | n/a | instancedict = Py_BuildValue("{sOsOs{}sOsO}", |
---|
924 | n/a | PICKLED_TAG, self->tag, |
---|
925 | n/a | PICKLED_CHILDREN, children, |
---|
926 | n/a | PICKLED_ATTRIB, |
---|
927 | n/a | PICKLED_TEXT, JOIN_OBJ(self->text), |
---|
928 | n/a | PICKLED_TAIL, JOIN_OBJ(self->tail)); |
---|
929 | n/a | else |
---|
930 | n/a | instancedict = Py_BuildValue("{sOsOsOsOsO}", |
---|
931 | n/a | PICKLED_TAG, self->tag, |
---|
932 | n/a | PICKLED_CHILDREN, children, |
---|
933 | n/a | PICKLED_ATTRIB, self->extra->attrib, |
---|
934 | n/a | PICKLED_TEXT, JOIN_OBJ(self->text), |
---|
935 | n/a | PICKLED_TAIL, JOIN_OBJ(self->tail)); |
---|
936 | n/a | if (instancedict) { |
---|
937 | n/a | Py_DECREF(children); |
---|
938 | n/a | return instancedict; |
---|
939 | n/a | } |
---|
940 | n/a | else { |
---|
941 | n/a | for (i = 0; i < PyList_GET_SIZE(children); i++) |
---|
942 | n/a | Py_DECREF(PyList_GET_ITEM(children, i)); |
---|
943 | n/a | Py_DECREF(children); |
---|
944 | n/a | |
---|
945 | n/a | return NULL; |
---|
946 | n/a | } |
---|
947 | n/a | } |
---|
948 | n/a | |
---|
949 | n/a | static PyObject * |
---|
950 | n/a | element_setstate_from_attributes(ElementObject *self, |
---|
951 | n/a | PyObject *tag, |
---|
952 | n/a | PyObject *attrib, |
---|
953 | n/a | PyObject *text, |
---|
954 | n/a | PyObject *tail, |
---|
955 | n/a | PyObject *children) |
---|
956 | n/a | { |
---|
957 | n/a | Py_ssize_t i, nchildren; |
---|
958 | n/a | |
---|
959 | n/a | if (!tag) { |
---|
960 | n/a | PyErr_SetString(PyExc_TypeError, "tag may not be NULL"); |
---|
961 | n/a | return NULL; |
---|
962 | n/a | } |
---|
963 | n/a | |
---|
964 | n/a | Py_INCREF(tag); |
---|
965 | n/a | Py_XSETREF(self->tag, tag); |
---|
966 | n/a | |
---|
967 | n/a | _clear_joined_ptr(&self->text); |
---|
968 | n/a | self->text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None; |
---|
969 | n/a | Py_INCREF(JOIN_OBJ(self->text)); |
---|
970 | n/a | |
---|
971 | n/a | _clear_joined_ptr(&self->tail); |
---|
972 | n/a | self->tail = tail ? JOIN_SET(tail, PyList_CheckExact(tail)) : Py_None; |
---|
973 | n/a | Py_INCREF(JOIN_OBJ(self->tail)); |
---|
974 | n/a | |
---|
975 | n/a | /* Handle ATTRIB and CHILDREN. */ |
---|
976 | n/a | if (!children && !attrib) |
---|
977 | n/a | Py_RETURN_NONE; |
---|
978 | n/a | |
---|
979 | n/a | /* Compute 'nchildren'. */ |
---|
980 | n/a | if (children) { |
---|
981 | n/a | if (!PyList_Check(children)) { |
---|
982 | n/a | PyErr_SetString(PyExc_TypeError, "'_children' is not a list"); |
---|
983 | n/a | return NULL; |
---|
984 | n/a | } |
---|
985 | n/a | nchildren = PyList_Size(children); |
---|
986 | n/a | } |
---|
987 | n/a | else { |
---|
988 | n/a | nchildren = 0; |
---|
989 | n/a | } |
---|
990 | n/a | |
---|
991 | n/a | /* Allocate 'extra'. */ |
---|
992 | n/a | if (element_resize(self, nchildren)) { |
---|
993 | n/a | return NULL; |
---|
994 | n/a | } |
---|
995 | n/a | assert(self->extra && self->extra->allocated >= nchildren); |
---|
996 | n/a | |
---|
997 | n/a | /* Copy children */ |
---|
998 | n/a | for (i = 0; i < nchildren; i++) { |
---|
999 | n/a | self->extra->children[i] = PyList_GET_ITEM(children, i); |
---|
1000 | n/a | Py_INCREF(self->extra->children[i]); |
---|
1001 | n/a | } |
---|
1002 | n/a | |
---|
1003 | n/a | self->extra->length = nchildren; |
---|
1004 | n/a | self->extra->allocated = nchildren; |
---|
1005 | n/a | |
---|
1006 | n/a | /* Stash attrib. */ |
---|
1007 | n/a | if (attrib) { |
---|
1008 | n/a | Py_INCREF(attrib); |
---|
1009 | n/a | Py_XSETREF(self->extra->attrib, attrib); |
---|
1010 | n/a | } |
---|
1011 | n/a | |
---|
1012 | n/a | Py_RETURN_NONE; |
---|
1013 | n/a | } |
---|
1014 | n/a | |
---|
1015 | n/a | /* __setstate__ for Element instance from the Python implementation. |
---|
1016 | n/a | * 'state' should be the instance dict. |
---|
1017 | n/a | */ |
---|
1018 | n/a | |
---|
1019 | n/a | static PyObject * |
---|
1020 | n/a | element_setstate_from_Python(ElementObject *self, PyObject *state) |
---|
1021 | n/a | { |
---|
1022 | n/a | static char *kwlist[] = {PICKLED_TAG, PICKLED_ATTRIB, PICKLED_TEXT, |
---|
1023 | n/a | PICKLED_TAIL, PICKLED_CHILDREN, 0}; |
---|
1024 | n/a | PyObject *args; |
---|
1025 | n/a | PyObject *tag, *attrib, *text, *tail, *children; |
---|
1026 | n/a | PyObject *retval; |
---|
1027 | n/a | |
---|
1028 | n/a | tag = attrib = text = tail = children = NULL; |
---|
1029 | n/a | args = PyTuple_New(0); |
---|
1030 | n/a | if (!args) |
---|
1031 | n/a | return NULL; |
---|
1032 | n/a | |
---|
1033 | n/a | if (PyArg_ParseTupleAndKeywords(args, state, "|$OOOOO", kwlist, &tag, |
---|
1034 | n/a | &attrib, &text, &tail, &children)) |
---|
1035 | n/a | retval = element_setstate_from_attributes(self, tag, attrib, text, |
---|
1036 | n/a | tail, children); |
---|
1037 | n/a | else |
---|
1038 | n/a | retval = NULL; |
---|
1039 | n/a | |
---|
1040 | n/a | Py_DECREF(args); |
---|
1041 | n/a | return retval; |
---|
1042 | n/a | } |
---|
1043 | n/a | |
---|
1044 | n/a | /*[clinic input] |
---|
1045 | n/a | _elementtree.Element.__setstate__ |
---|
1046 | n/a | |
---|
1047 | n/a | state: object |
---|
1048 | n/a | / |
---|
1049 | n/a | |
---|
1050 | n/a | [clinic start generated code]*/ |
---|
1051 | n/a | |
---|
1052 | n/a | static PyObject * |
---|
1053 | n/a | _elementtree_Element___setstate__(ElementObject *self, PyObject *state) |
---|
1054 | n/a | /*[clinic end generated code: output=ea28bf3491b1f75e input=aaf80abea7c1e3b9]*/ |
---|
1055 | n/a | { |
---|
1056 | n/a | if (!PyDict_CheckExact(state)) { |
---|
1057 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1058 | n/a | "Don't know how to unpickle \"%.200R\" as an Element", |
---|
1059 | n/a | state); |
---|
1060 | n/a | return NULL; |
---|
1061 | n/a | } |
---|
1062 | n/a | else |
---|
1063 | n/a | return element_setstate_from_Python(self, state); |
---|
1064 | n/a | } |
---|
1065 | n/a | |
---|
1066 | n/a | LOCAL(int) |
---|
1067 | n/a | checkpath(PyObject* tag) |
---|
1068 | n/a | { |
---|
1069 | n/a | Py_ssize_t i; |
---|
1070 | n/a | int check = 1; |
---|
1071 | n/a | |
---|
1072 | n/a | /* check if a tag contains an xpath character */ |
---|
1073 | n/a | |
---|
1074 | n/a | #define PATHCHAR(ch) \ |
---|
1075 | n/a | (ch == '/' || ch == '*' || ch == '[' || ch == '@' || ch == '.') |
---|
1076 | n/a | |
---|
1077 | n/a | if (PyUnicode_Check(tag)) { |
---|
1078 | n/a | const Py_ssize_t len = PyUnicode_GET_LENGTH(tag); |
---|
1079 | n/a | void *data = PyUnicode_DATA(tag); |
---|
1080 | n/a | unsigned int kind = PyUnicode_KIND(tag); |
---|
1081 | n/a | for (i = 0; i < len; i++) { |
---|
1082 | n/a | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
---|
1083 | n/a | if (ch == '{') |
---|
1084 | n/a | check = 0; |
---|
1085 | n/a | else if (ch == '}') |
---|
1086 | n/a | check = 1; |
---|
1087 | n/a | else if (check && PATHCHAR(ch)) |
---|
1088 | n/a | return 1; |
---|
1089 | n/a | } |
---|
1090 | n/a | return 0; |
---|
1091 | n/a | } |
---|
1092 | n/a | if (PyBytes_Check(tag)) { |
---|
1093 | n/a | char *p = PyBytes_AS_STRING(tag); |
---|
1094 | n/a | for (i = 0; i < PyBytes_GET_SIZE(tag); i++) { |
---|
1095 | n/a | if (p[i] == '{') |
---|
1096 | n/a | check = 0; |
---|
1097 | n/a | else if (p[i] == '}') |
---|
1098 | n/a | check = 1; |
---|
1099 | n/a | else if (check && PATHCHAR(p[i])) |
---|
1100 | n/a | return 1; |
---|
1101 | n/a | } |
---|
1102 | n/a | return 0; |
---|
1103 | n/a | } |
---|
1104 | n/a | |
---|
1105 | n/a | return 1; /* unknown type; might be path expression */ |
---|
1106 | n/a | } |
---|
1107 | n/a | |
---|
1108 | n/a | /*[clinic input] |
---|
1109 | n/a | _elementtree.Element.extend |
---|
1110 | n/a | |
---|
1111 | n/a | elements: object |
---|
1112 | n/a | / |
---|
1113 | n/a | |
---|
1114 | n/a | [clinic start generated code]*/ |
---|
1115 | n/a | |
---|
1116 | n/a | static PyObject * |
---|
1117 | n/a | _elementtree_Element_extend(ElementObject *self, PyObject *elements) |
---|
1118 | n/a | /*[clinic end generated code: output=f6e67fc2ff529191 input=807bc4f31c69f7c0]*/ |
---|
1119 | n/a | { |
---|
1120 | n/a | PyObject* seq; |
---|
1121 | n/a | Py_ssize_t i; |
---|
1122 | n/a | |
---|
1123 | n/a | seq = PySequence_Fast(elements, ""); |
---|
1124 | n/a | if (!seq) { |
---|
1125 | n/a | PyErr_Format( |
---|
1126 | n/a | PyExc_TypeError, |
---|
1127 | n/a | "expected sequence, not \"%.200s\"", Py_TYPE(elements)->tp_name |
---|
1128 | n/a | ); |
---|
1129 | n/a | return NULL; |
---|
1130 | n/a | } |
---|
1131 | n/a | |
---|
1132 | n/a | for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) { |
---|
1133 | n/a | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
---|
1134 | n/a | Py_INCREF(element); |
---|
1135 | n/a | if (!PyObject_TypeCheck(element, (PyTypeObject *)&Element_Type)) { |
---|
1136 | n/a | PyErr_Format( |
---|
1137 | n/a | PyExc_TypeError, |
---|
1138 | n/a | "expected an Element, not \"%.200s\"", |
---|
1139 | n/a | Py_TYPE(element)->tp_name); |
---|
1140 | n/a | Py_DECREF(seq); |
---|
1141 | n/a | Py_DECREF(element); |
---|
1142 | n/a | return NULL; |
---|
1143 | n/a | } |
---|
1144 | n/a | |
---|
1145 | n/a | if (element_add_subelement(self, element) < 0) { |
---|
1146 | n/a | Py_DECREF(seq); |
---|
1147 | n/a | Py_DECREF(element); |
---|
1148 | n/a | return NULL; |
---|
1149 | n/a | } |
---|
1150 | n/a | Py_DECREF(element); |
---|
1151 | n/a | } |
---|
1152 | n/a | |
---|
1153 | n/a | Py_DECREF(seq); |
---|
1154 | n/a | |
---|
1155 | n/a | Py_RETURN_NONE; |
---|
1156 | n/a | } |
---|
1157 | n/a | |
---|
1158 | n/a | /*[clinic input] |
---|
1159 | n/a | _elementtree.Element.find |
---|
1160 | n/a | |
---|
1161 | n/a | path: object |
---|
1162 | n/a | namespaces: object = None |
---|
1163 | n/a | |
---|
1164 | n/a | [clinic start generated code]*/ |
---|
1165 | n/a | |
---|
1166 | n/a | static PyObject * |
---|
1167 | n/a | _elementtree_Element_find_impl(ElementObject *self, PyObject *path, |
---|
1168 | n/a | PyObject *namespaces) |
---|
1169 | n/a | /*[clinic end generated code: output=41b43f0f0becafae input=359b6985f6489d2e]*/ |
---|
1170 | n/a | { |
---|
1171 | n/a | Py_ssize_t i; |
---|
1172 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
1173 | n/a | |
---|
1174 | n/a | if (checkpath(path) || namespaces != Py_None) { |
---|
1175 | n/a | _Py_IDENTIFIER(find); |
---|
1176 | n/a | return _PyObject_CallMethodIdObjArgs( |
---|
1177 | n/a | st->elementpath_obj, &PyId_find, self, path, namespaces, NULL |
---|
1178 | n/a | ); |
---|
1179 | n/a | } |
---|
1180 | n/a | |
---|
1181 | n/a | if (!self->extra) |
---|
1182 | n/a | Py_RETURN_NONE; |
---|
1183 | n/a | |
---|
1184 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
1185 | n/a | PyObject* item = self->extra->children[i]; |
---|
1186 | n/a | int rc; |
---|
1187 | n/a | if (!Element_CheckExact(item)) |
---|
1188 | n/a | continue; |
---|
1189 | n/a | Py_INCREF(item); |
---|
1190 | n/a | rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ); |
---|
1191 | n/a | if (rc > 0) |
---|
1192 | n/a | return item; |
---|
1193 | n/a | Py_DECREF(item); |
---|
1194 | n/a | if (rc < 0) |
---|
1195 | n/a | return NULL; |
---|
1196 | n/a | } |
---|
1197 | n/a | |
---|
1198 | n/a | Py_RETURN_NONE; |
---|
1199 | n/a | } |
---|
1200 | n/a | |
---|
1201 | n/a | /*[clinic input] |
---|
1202 | n/a | _elementtree.Element.findtext |
---|
1203 | n/a | |
---|
1204 | n/a | path: object |
---|
1205 | n/a | default: object = None |
---|
1206 | n/a | namespaces: object = None |
---|
1207 | n/a | |
---|
1208 | n/a | [clinic start generated code]*/ |
---|
1209 | n/a | |
---|
1210 | n/a | static PyObject * |
---|
1211 | n/a | _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path, |
---|
1212 | n/a | PyObject *default_value, |
---|
1213 | n/a | PyObject *namespaces) |
---|
1214 | n/a | /*[clinic end generated code: output=83b3ba4535d308d2 input=b53a85aa5aa2a916]*/ |
---|
1215 | n/a | { |
---|
1216 | n/a | Py_ssize_t i; |
---|
1217 | n/a | _Py_IDENTIFIER(findtext); |
---|
1218 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
1219 | n/a | |
---|
1220 | n/a | if (checkpath(path) || namespaces != Py_None) |
---|
1221 | n/a | return _PyObject_CallMethodIdObjArgs( |
---|
1222 | n/a | st->elementpath_obj, &PyId_findtext, |
---|
1223 | n/a | self, path, default_value, namespaces, NULL |
---|
1224 | n/a | ); |
---|
1225 | n/a | |
---|
1226 | n/a | if (!self->extra) { |
---|
1227 | n/a | Py_INCREF(default_value); |
---|
1228 | n/a | return default_value; |
---|
1229 | n/a | } |
---|
1230 | n/a | |
---|
1231 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
1232 | n/a | ElementObject* item = (ElementObject*) self->extra->children[i]; |
---|
1233 | n/a | int rc; |
---|
1234 | n/a | if (!Element_CheckExact(item)) |
---|
1235 | n/a | continue; |
---|
1236 | n/a | Py_INCREF(item); |
---|
1237 | n/a | rc = PyObject_RichCompareBool(item->tag, path, Py_EQ); |
---|
1238 | n/a | if (rc > 0) { |
---|
1239 | n/a | PyObject* text = element_get_text(item); |
---|
1240 | n/a | if (text == Py_None) { |
---|
1241 | n/a | Py_DECREF(item); |
---|
1242 | n/a | return PyUnicode_New(0, 0); |
---|
1243 | n/a | } |
---|
1244 | n/a | Py_XINCREF(text); |
---|
1245 | n/a | Py_DECREF(item); |
---|
1246 | n/a | return text; |
---|
1247 | n/a | } |
---|
1248 | n/a | Py_DECREF(item); |
---|
1249 | n/a | if (rc < 0) |
---|
1250 | n/a | return NULL; |
---|
1251 | n/a | } |
---|
1252 | n/a | |
---|
1253 | n/a | Py_INCREF(default_value); |
---|
1254 | n/a | return default_value; |
---|
1255 | n/a | } |
---|
1256 | n/a | |
---|
1257 | n/a | /*[clinic input] |
---|
1258 | n/a | _elementtree.Element.findall |
---|
1259 | n/a | |
---|
1260 | n/a | path: object |
---|
1261 | n/a | namespaces: object = None |
---|
1262 | n/a | |
---|
1263 | n/a | [clinic start generated code]*/ |
---|
1264 | n/a | |
---|
1265 | n/a | static PyObject * |
---|
1266 | n/a | _elementtree_Element_findall_impl(ElementObject *self, PyObject *path, |
---|
1267 | n/a | PyObject *namespaces) |
---|
1268 | n/a | /*[clinic end generated code: output=1a0bd9f5541b711d input=4d9e6505a638550c]*/ |
---|
1269 | n/a | { |
---|
1270 | n/a | Py_ssize_t i; |
---|
1271 | n/a | PyObject* out; |
---|
1272 | n/a | PyObject* tag = path; |
---|
1273 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
1274 | n/a | |
---|
1275 | n/a | if (checkpath(tag) || namespaces != Py_None) { |
---|
1276 | n/a | _Py_IDENTIFIER(findall); |
---|
1277 | n/a | return _PyObject_CallMethodIdObjArgs( |
---|
1278 | n/a | st->elementpath_obj, &PyId_findall, self, tag, namespaces, NULL |
---|
1279 | n/a | ); |
---|
1280 | n/a | } |
---|
1281 | n/a | |
---|
1282 | n/a | out = PyList_New(0); |
---|
1283 | n/a | if (!out) |
---|
1284 | n/a | return NULL; |
---|
1285 | n/a | |
---|
1286 | n/a | if (!self->extra) |
---|
1287 | n/a | return out; |
---|
1288 | n/a | |
---|
1289 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
1290 | n/a | PyObject* item = self->extra->children[i]; |
---|
1291 | n/a | int rc; |
---|
1292 | n/a | if (!Element_CheckExact(item)) |
---|
1293 | n/a | continue; |
---|
1294 | n/a | Py_INCREF(item); |
---|
1295 | n/a | rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ); |
---|
1296 | n/a | if (rc != 0 && (rc < 0 || PyList_Append(out, item) < 0)) { |
---|
1297 | n/a | Py_DECREF(item); |
---|
1298 | n/a | Py_DECREF(out); |
---|
1299 | n/a | return NULL; |
---|
1300 | n/a | } |
---|
1301 | n/a | Py_DECREF(item); |
---|
1302 | n/a | } |
---|
1303 | n/a | |
---|
1304 | n/a | return out; |
---|
1305 | n/a | } |
---|
1306 | n/a | |
---|
1307 | n/a | /*[clinic input] |
---|
1308 | n/a | _elementtree.Element.iterfind |
---|
1309 | n/a | |
---|
1310 | n/a | path: object |
---|
1311 | n/a | namespaces: object = None |
---|
1312 | n/a | |
---|
1313 | n/a | [clinic start generated code]*/ |
---|
1314 | n/a | |
---|
1315 | n/a | static PyObject * |
---|
1316 | n/a | _elementtree_Element_iterfind_impl(ElementObject *self, PyObject *path, |
---|
1317 | n/a | PyObject *namespaces) |
---|
1318 | n/a | /*[clinic end generated code: output=ecdd56d63b19d40f input=abb974e350fb65c7]*/ |
---|
1319 | n/a | { |
---|
1320 | n/a | PyObject* tag = path; |
---|
1321 | n/a | _Py_IDENTIFIER(iterfind); |
---|
1322 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
1323 | n/a | |
---|
1324 | n/a | return _PyObject_CallMethodIdObjArgs( |
---|
1325 | n/a | st->elementpath_obj, &PyId_iterfind, self, tag, namespaces, NULL); |
---|
1326 | n/a | } |
---|
1327 | n/a | |
---|
1328 | n/a | /*[clinic input] |
---|
1329 | n/a | _elementtree.Element.get |
---|
1330 | n/a | |
---|
1331 | n/a | key: object |
---|
1332 | n/a | default: object = None |
---|
1333 | n/a | |
---|
1334 | n/a | [clinic start generated code]*/ |
---|
1335 | n/a | |
---|
1336 | n/a | static PyObject * |
---|
1337 | n/a | _elementtree_Element_get_impl(ElementObject *self, PyObject *key, |
---|
1338 | n/a | PyObject *default_value) |
---|
1339 | n/a | /*[clinic end generated code: output=523c614142595d75 input=ee153bbf8cdb246e]*/ |
---|
1340 | n/a | { |
---|
1341 | n/a | PyObject* value; |
---|
1342 | n/a | |
---|
1343 | n/a | if (!self->extra || self->extra->attrib == Py_None) |
---|
1344 | n/a | value = default_value; |
---|
1345 | n/a | else { |
---|
1346 | n/a | value = PyDict_GetItem(self->extra->attrib, key); |
---|
1347 | n/a | if (!value) |
---|
1348 | n/a | value = default_value; |
---|
1349 | n/a | } |
---|
1350 | n/a | |
---|
1351 | n/a | Py_INCREF(value); |
---|
1352 | n/a | return value; |
---|
1353 | n/a | } |
---|
1354 | n/a | |
---|
1355 | n/a | /*[clinic input] |
---|
1356 | n/a | _elementtree.Element.getchildren |
---|
1357 | n/a | |
---|
1358 | n/a | [clinic start generated code]*/ |
---|
1359 | n/a | |
---|
1360 | n/a | static PyObject * |
---|
1361 | n/a | _elementtree_Element_getchildren_impl(ElementObject *self) |
---|
1362 | n/a | /*[clinic end generated code: output=e50ffe118637b14f input=0f754dfded150d5f]*/ |
---|
1363 | n/a | { |
---|
1364 | n/a | Py_ssize_t i; |
---|
1365 | n/a | PyObject* list; |
---|
1366 | n/a | |
---|
1367 | n/a | /* FIXME: report as deprecated? */ |
---|
1368 | n/a | |
---|
1369 | n/a | if (!self->extra) |
---|
1370 | n/a | return PyList_New(0); |
---|
1371 | n/a | |
---|
1372 | n/a | list = PyList_New(self->extra->length); |
---|
1373 | n/a | if (!list) |
---|
1374 | n/a | return NULL; |
---|
1375 | n/a | |
---|
1376 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
1377 | n/a | PyObject* item = self->extra->children[i]; |
---|
1378 | n/a | Py_INCREF(item); |
---|
1379 | n/a | PyList_SET_ITEM(list, i, item); |
---|
1380 | n/a | } |
---|
1381 | n/a | |
---|
1382 | n/a | return list; |
---|
1383 | n/a | } |
---|
1384 | n/a | |
---|
1385 | n/a | |
---|
1386 | n/a | static PyObject * |
---|
1387 | n/a | create_elementiter(ElementObject *self, PyObject *tag, int gettext); |
---|
1388 | n/a | |
---|
1389 | n/a | |
---|
1390 | n/a | /*[clinic input] |
---|
1391 | n/a | _elementtree.Element.iter |
---|
1392 | n/a | |
---|
1393 | n/a | tag: object = None |
---|
1394 | n/a | |
---|
1395 | n/a | [clinic start generated code]*/ |
---|
1396 | n/a | |
---|
1397 | n/a | static PyObject * |
---|
1398 | n/a | _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag) |
---|
1399 | n/a | /*[clinic end generated code: output=3f49f9a862941cc5 input=774d5b12e573aedd]*/ |
---|
1400 | n/a | { |
---|
1401 | n/a | if (PyUnicode_Check(tag)) { |
---|
1402 | n/a | if (PyUnicode_READY(tag) < 0) |
---|
1403 | n/a | return NULL; |
---|
1404 | n/a | if (PyUnicode_GET_LENGTH(tag) == 1 && PyUnicode_READ_CHAR(tag, 0) == '*') |
---|
1405 | n/a | tag = Py_None; |
---|
1406 | n/a | } |
---|
1407 | n/a | else if (PyBytes_Check(tag)) { |
---|
1408 | n/a | if (PyBytes_GET_SIZE(tag) == 1 && *PyBytes_AS_STRING(tag) == '*') |
---|
1409 | n/a | tag = Py_None; |
---|
1410 | n/a | } |
---|
1411 | n/a | |
---|
1412 | n/a | return create_elementiter(self, tag, 0); |
---|
1413 | n/a | } |
---|
1414 | n/a | |
---|
1415 | n/a | |
---|
1416 | n/a | /*[clinic input] |
---|
1417 | n/a | _elementtree.Element.itertext |
---|
1418 | n/a | |
---|
1419 | n/a | [clinic start generated code]*/ |
---|
1420 | n/a | |
---|
1421 | n/a | static PyObject * |
---|
1422 | n/a | _elementtree_Element_itertext_impl(ElementObject *self) |
---|
1423 | n/a | /*[clinic end generated code: output=5fa34b2fbcb65df6 input=af8f0e42cb239c89]*/ |
---|
1424 | n/a | { |
---|
1425 | n/a | return create_elementiter(self, Py_None, 1); |
---|
1426 | n/a | } |
---|
1427 | n/a | |
---|
1428 | n/a | |
---|
1429 | n/a | static PyObject* |
---|
1430 | n/a | element_getitem(PyObject* self_, Py_ssize_t index) |
---|
1431 | n/a | { |
---|
1432 | n/a | ElementObject* self = (ElementObject*) self_; |
---|
1433 | n/a | |
---|
1434 | n/a | if (!self->extra || index < 0 || index >= self->extra->length) { |
---|
1435 | n/a | PyErr_SetString( |
---|
1436 | n/a | PyExc_IndexError, |
---|
1437 | n/a | "child index out of range" |
---|
1438 | n/a | ); |
---|
1439 | n/a | return NULL; |
---|
1440 | n/a | } |
---|
1441 | n/a | |
---|
1442 | n/a | Py_INCREF(self->extra->children[index]); |
---|
1443 | n/a | return self->extra->children[index]; |
---|
1444 | n/a | } |
---|
1445 | n/a | |
---|
1446 | n/a | /*[clinic input] |
---|
1447 | n/a | _elementtree.Element.insert |
---|
1448 | n/a | |
---|
1449 | n/a | index: Py_ssize_t |
---|
1450 | n/a | subelement: object(subclass_of='&Element_Type') |
---|
1451 | n/a | / |
---|
1452 | n/a | |
---|
1453 | n/a | [clinic start generated code]*/ |
---|
1454 | n/a | |
---|
1455 | n/a | static PyObject * |
---|
1456 | n/a | _elementtree_Element_insert_impl(ElementObject *self, Py_ssize_t index, |
---|
1457 | n/a | PyObject *subelement) |
---|
1458 | n/a | /*[clinic end generated code: output=990adfef4d424c0b input=cd6fbfcdab52d7a8]*/ |
---|
1459 | n/a | { |
---|
1460 | n/a | Py_ssize_t i; |
---|
1461 | n/a | |
---|
1462 | n/a | if (!self->extra) { |
---|
1463 | n/a | if (create_extra(self, NULL) < 0) |
---|
1464 | n/a | return NULL; |
---|
1465 | n/a | } |
---|
1466 | n/a | |
---|
1467 | n/a | if (index < 0) { |
---|
1468 | n/a | index += self->extra->length; |
---|
1469 | n/a | if (index < 0) |
---|
1470 | n/a | index = 0; |
---|
1471 | n/a | } |
---|
1472 | n/a | if (index > self->extra->length) |
---|
1473 | n/a | index = self->extra->length; |
---|
1474 | n/a | |
---|
1475 | n/a | if (element_resize(self, 1) < 0) |
---|
1476 | n/a | return NULL; |
---|
1477 | n/a | |
---|
1478 | n/a | for (i = self->extra->length; i > index; i--) |
---|
1479 | n/a | self->extra->children[i] = self->extra->children[i-1]; |
---|
1480 | n/a | |
---|
1481 | n/a | Py_INCREF(subelement); |
---|
1482 | n/a | self->extra->children[index] = subelement; |
---|
1483 | n/a | |
---|
1484 | n/a | self->extra->length++; |
---|
1485 | n/a | |
---|
1486 | n/a | Py_RETURN_NONE; |
---|
1487 | n/a | } |
---|
1488 | n/a | |
---|
1489 | n/a | /*[clinic input] |
---|
1490 | n/a | _elementtree.Element.items |
---|
1491 | n/a | |
---|
1492 | n/a | [clinic start generated code]*/ |
---|
1493 | n/a | |
---|
1494 | n/a | static PyObject * |
---|
1495 | n/a | _elementtree_Element_items_impl(ElementObject *self) |
---|
1496 | n/a | /*[clinic end generated code: output=6db2c778ce3f5a4d input=adbe09aaea474447]*/ |
---|
1497 | n/a | { |
---|
1498 | n/a | if (!self->extra || self->extra->attrib == Py_None) |
---|
1499 | n/a | return PyList_New(0); |
---|
1500 | n/a | |
---|
1501 | n/a | return PyDict_Items(self->extra->attrib); |
---|
1502 | n/a | } |
---|
1503 | n/a | |
---|
1504 | n/a | /*[clinic input] |
---|
1505 | n/a | _elementtree.Element.keys |
---|
1506 | n/a | |
---|
1507 | n/a | [clinic start generated code]*/ |
---|
1508 | n/a | |
---|
1509 | n/a | static PyObject * |
---|
1510 | n/a | _elementtree_Element_keys_impl(ElementObject *self) |
---|
1511 | n/a | /*[clinic end generated code: output=bc5bfabbf20eeb3c input=f02caf5b496b5b0b]*/ |
---|
1512 | n/a | { |
---|
1513 | n/a | if (!self->extra || self->extra->attrib == Py_None) |
---|
1514 | n/a | return PyList_New(0); |
---|
1515 | n/a | |
---|
1516 | n/a | return PyDict_Keys(self->extra->attrib); |
---|
1517 | n/a | } |
---|
1518 | n/a | |
---|
1519 | n/a | static Py_ssize_t |
---|
1520 | n/a | element_length(ElementObject* self) |
---|
1521 | n/a | { |
---|
1522 | n/a | if (!self->extra) |
---|
1523 | n/a | return 0; |
---|
1524 | n/a | |
---|
1525 | n/a | return self->extra->length; |
---|
1526 | n/a | } |
---|
1527 | n/a | |
---|
1528 | n/a | /*[clinic input] |
---|
1529 | n/a | _elementtree.Element.makeelement |
---|
1530 | n/a | |
---|
1531 | n/a | tag: object |
---|
1532 | n/a | attrib: object |
---|
1533 | n/a | / |
---|
1534 | n/a | |
---|
1535 | n/a | [clinic start generated code]*/ |
---|
1536 | n/a | |
---|
1537 | n/a | static PyObject * |
---|
1538 | n/a | _elementtree_Element_makeelement_impl(ElementObject *self, PyObject *tag, |
---|
1539 | n/a | PyObject *attrib) |
---|
1540 | n/a | /*[clinic end generated code: output=4109832d5bb789ef input=9480d1d2e3e68235]*/ |
---|
1541 | n/a | { |
---|
1542 | n/a | PyObject* elem; |
---|
1543 | n/a | |
---|
1544 | n/a | attrib = PyDict_Copy(attrib); |
---|
1545 | n/a | if (!attrib) |
---|
1546 | n/a | return NULL; |
---|
1547 | n/a | |
---|
1548 | n/a | elem = create_new_element(tag, attrib); |
---|
1549 | n/a | |
---|
1550 | n/a | Py_DECREF(attrib); |
---|
1551 | n/a | |
---|
1552 | n/a | return elem; |
---|
1553 | n/a | } |
---|
1554 | n/a | |
---|
1555 | n/a | /*[clinic input] |
---|
1556 | n/a | _elementtree.Element.remove |
---|
1557 | n/a | |
---|
1558 | n/a | subelement: object(subclass_of='&Element_Type') |
---|
1559 | n/a | / |
---|
1560 | n/a | |
---|
1561 | n/a | [clinic start generated code]*/ |
---|
1562 | n/a | |
---|
1563 | n/a | static PyObject * |
---|
1564 | n/a | _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement) |
---|
1565 | n/a | /*[clinic end generated code: output=38fe6c07d6d87d1f input=d52fc28ededc0bd8]*/ |
---|
1566 | n/a | { |
---|
1567 | n/a | Py_ssize_t i; |
---|
1568 | n/a | int rc; |
---|
1569 | n/a | PyObject *found; |
---|
1570 | n/a | |
---|
1571 | n/a | if (!self->extra) { |
---|
1572 | n/a | /* element has no children, so raise exception */ |
---|
1573 | n/a | PyErr_SetString( |
---|
1574 | n/a | PyExc_ValueError, |
---|
1575 | n/a | "list.remove(x): x not in list" |
---|
1576 | n/a | ); |
---|
1577 | n/a | return NULL; |
---|
1578 | n/a | } |
---|
1579 | n/a | |
---|
1580 | n/a | for (i = 0; i < self->extra->length; i++) { |
---|
1581 | n/a | if (self->extra->children[i] == subelement) |
---|
1582 | n/a | break; |
---|
1583 | n/a | rc = PyObject_RichCompareBool(self->extra->children[i], subelement, Py_EQ); |
---|
1584 | n/a | if (rc > 0) |
---|
1585 | n/a | break; |
---|
1586 | n/a | if (rc < 0) |
---|
1587 | n/a | return NULL; |
---|
1588 | n/a | } |
---|
1589 | n/a | |
---|
1590 | n/a | if (i >= self->extra->length) { |
---|
1591 | n/a | /* subelement is not in children, so raise exception */ |
---|
1592 | n/a | PyErr_SetString( |
---|
1593 | n/a | PyExc_ValueError, |
---|
1594 | n/a | "list.remove(x): x not in list" |
---|
1595 | n/a | ); |
---|
1596 | n/a | return NULL; |
---|
1597 | n/a | } |
---|
1598 | n/a | |
---|
1599 | n/a | found = self->extra->children[i]; |
---|
1600 | n/a | |
---|
1601 | n/a | self->extra->length--; |
---|
1602 | n/a | for (; i < self->extra->length; i++) |
---|
1603 | n/a | self->extra->children[i] = self->extra->children[i+1]; |
---|
1604 | n/a | |
---|
1605 | n/a | Py_DECREF(found); |
---|
1606 | n/a | Py_RETURN_NONE; |
---|
1607 | n/a | } |
---|
1608 | n/a | |
---|
1609 | n/a | static PyObject* |
---|
1610 | n/a | element_repr(ElementObject* self) |
---|
1611 | n/a | { |
---|
1612 | n/a | int status; |
---|
1613 | n/a | |
---|
1614 | n/a | if (self->tag == NULL) |
---|
1615 | n/a | return PyUnicode_FromFormat("<Element at %p>", self); |
---|
1616 | n/a | |
---|
1617 | n/a | status = Py_ReprEnter((PyObject *)self); |
---|
1618 | n/a | if (status == 0) { |
---|
1619 | n/a | PyObject *res; |
---|
1620 | n/a | res = PyUnicode_FromFormat("<Element %R at %p>", self->tag, self); |
---|
1621 | n/a | Py_ReprLeave((PyObject *)self); |
---|
1622 | n/a | return res; |
---|
1623 | n/a | } |
---|
1624 | n/a | if (status > 0) |
---|
1625 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
1626 | n/a | "reentrant call inside %s.__repr__", |
---|
1627 | n/a | Py_TYPE(self)->tp_name); |
---|
1628 | n/a | return NULL; |
---|
1629 | n/a | } |
---|
1630 | n/a | |
---|
1631 | n/a | /*[clinic input] |
---|
1632 | n/a | _elementtree.Element.set |
---|
1633 | n/a | |
---|
1634 | n/a | key: object |
---|
1635 | n/a | value: object |
---|
1636 | n/a | / |
---|
1637 | n/a | |
---|
1638 | n/a | [clinic start generated code]*/ |
---|
1639 | n/a | |
---|
1640 | n/a | static PyObject * |
---|
1641 | n/a | _elementtree_Element_set_impl(ElementObject *self, PyObject *key, |
---|
1642 | n/a | PyObject *value) |
---|
1643 | n/a | /*[clinic end generated code: output=fb938806be3c5656 input=1efe90f7d82b3fe9]*/ |
---|
1644 | n/a | { |
---|
1645 | n/a | PyObject* attrib; |
---|
1646 | n/a | |
---|
1647 | n/a | if (!self->extra) { |
---|
1648 | n/a | if (create_extra(self, NULL) < 0) |
---|
1649 | n/a | return NULL; |
---|
1650 | n/a | } |
---|
1651 | n/a | |
---|
1652 | n/a | attrib = element_get_attrib(self); |
---|
1653 | n/a | if (!attrib) |
---|
1654 | n/a | return NULL; |
---|
1655 | n/a | |
---|
1656 | n/a | if (PyDict_SetItem(attrib, key, value) < 0) |
---|
1657 | n/a | return NULL; |
---|
1658 | n/a | |
---|
1659 | n/a | Py_RETURN_NONE; |
---|
1660 | n/a | } |
---|
1661 | n/a | |
---|
1662 | n/a | static int |
---|
1663 | n/a | element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item) |
---|
1664 | n/a | { |
---|
1665 | n/a | ElementObject* self = (ElementObject*) self_; |
---|
1666 | n/a | Py_ssize_t i; |
---|
1667 | n/a | PyObject* old; |
---|
1668 | n/a | |
---|
1669 | n/a | if (!self->extra || index < 0 || index >= self->extra->length) { |
---|
1670 | n/a | PyErr_SetString( |
---|
1671 | n/a | PyExc_IndexError, |
---|
1672 | n/a | "child assignment index out of range"); |
---|
1673 | n/a | return -1; |
---|
1674 | n/a | } |
---|
1675 | n/a | |
---|
1676 | n/a | old = self->extra->children[index]; |
---|
1677 | n/a | |
---|
1678 | n/a | if (item) { |
---|
1679 | n/a | Py_INCREF(item); |
---|
1680 | n/a | self->extra->children[index] = item; |
---|
1681 | n/a | } else { |
---|
1682 | n/a | self->extra->length--; |
---|
1683 | n/a | for (i = index; i < self->extra->length; i++) |
---|
1684 | n/a | self->extra->children[i] = self->extra->children[i+1]; |
---|
1685 | n/a | } |
---|
1686 | n/a | |
---|
1687 | n/a | Py_DECREF(old); |
---|
1688 | n/a | |
---|
1689 | n/a | return 0; |
---|
1690 | n/a | } |
---|
1691 | n/a | |
---|
1692 | n/a | static PyObject* |
---|
1693 | n/a | element_subscr(PyObject* self_, PyObject* item) |
---|
1694 | n/a | { |
---|
1695 | n/a | ElementObject* self = (ElementObject*) self_; |
---|
1696 | n/a | |
---|
1697 | n/a | if (PyIndex_Check(item)) { |
---|
1698 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
---|
1699 | n/a | |
---|
1700 | n/a | if (i == -1 && PyErr_Occurred()) { |
---|
1701 | n/a | return NULL; |
---|
1702 | n/a | } |
---|
1703 | n/a | if (i < 0 && self->extra) |
---|
1704 | n/a | i += self->extra->length; |
---|
1705 | n/a | return element_getitem(self_, i); |
---|
1706 | n/a | } |
---|
1707 | n/a | else if (PySlice_Check(item)) { |
---|
1708 | n/a | Py_ssize_t start, stop, step, slicelen, cur, i; |
---|
1709 | n/a | PyObject* list; |
---|
1710 | n/a | |
---|
1711 | n/a | if (!self->extra) |
---|
1712 | n/a | return PyList_New(0); |
---|
1713 | n/a | |
---|
1714 | n/a | if (PySlice_GetIndicesEx(item, |
---|
1715 | n/a | self->extra->length, |
---|
1716 | n/a | &start, &stop, &step, &slicelen) < 0) { |
---|
1717 | n/a | return NULL; |
---|
1718 | n/a | } |
---|
1719 | n/a | |
---|
1720 | n/a | if (slicelen <= 0) |
---|
1721 | n/a | return PyList_New(0); |
---|
1722 | n/a | else { |
---|
1723 | n/a | list = PyList_New(slicelen); |
---|
1724 | n/a | if (!list) |
---|
1725 | n/a | return NULL; |
---|
1726 | n/a | |
---|
1727 | n/a | for (cur = start, i = 0; i < slicelen; |
---|
1728 | n/a | cur += step, i++) { |
---|
1729 | n/a | PyObject* item = self->extra->children[cur]; |
---|
1730 | n/a | Py_INCREF(item); |
---|
1731 | n/a | PyList_SET_ITEM(list, i, item); |
---|
1732 | n/a | } |
---|
1733 | n/a | |
---|
1734 | n/a | return list; |
---|
1735 | n/a | } |
---|
1736 | n/a | } |
---|
1737 | n/a | else { |
---|
1738 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1739 | n/a | "element indices must be integers"); |
---|
1740 | n/a | return NULL; |
---|
1741 | n/a | } |
---|
1742 | n/a | } |
---|
1743 | n/a | |
---|
1744 | n/a | static int |
---|
1745 | n/a | element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) |
---|
1746 | n/a | { |
---|
1747 | n/a | ElementObject* self = (ElementObject*) self_; |
---|
1748 | n/a | |
---|
1749 | n/a | if (PyIndex_Check(item)) { |
---|
1750 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
---|
1751 | n/a | |
---|
1752 | n/a | if (i == -1 && PyErr_Occurred()) { |
---|
1753 | n/a | return -1; |
---|
1754 | n/a | } |
---|
1755 | n/a | if (i < 0 && self->extra) |
---|
1756 | n/a | i += self->extra->length; |
---|
1757 | n/a | return element_setitem(self_, i, value); |
---|
1758 | n/a | } |
---|
1759 | n/a | else if (PySlice_Check(item)) { |
---|
1760 | n/a | Py_ssize_t start, stop, step, slicelen, newlen, cur, i; |
---|
1761 | n/a | |
---|
1762 | n/a | PyObject* recycle = NULL; |
---|
1763 | n/a | PyObject* seq; |
---|
1764 | n/a | |
---|
1765 | n/a | if (!self->extra) { |
---|
1766 | n/a | if (create_extra(self, NULL) < 0) |
---|
1767 | n/a | return -1; |
---|
1768 | n/a | } |
---|
1769 | n/a | |
---|
1770 | n/a | if (PySlice_GetIndicesEx(item, |
---|
1771 | n/a | self->extra->length, |
---|
1772 | n/a | &start, &stop, &step, &slicelen) < 0) { |
---|
1773 | n/a | return -1; |
---|
1774 | n/a | } |
---|
1775 | n/a | |
---|
1776 | n/a | if (value == NULL) { |
---|
1777 | n/a | /* Delete slice */ |
---|
1778 | n/a | size_t cur; |
---|
1779 | n/a | Py_ssize_t i; |
---|
1780 | n/a | |
---|
1781 | n/a | if (slicelen <= 0) |
---|
1782 | n/a | return 0; |
---|
1783 | n/a | |
---|
1784 | n/a | /* Since we're deleting, the direction of the range doesn't matter, |
---|
1785 | n/a | * so for simplicity make it always ascending. |
---|
1786 | n/a | */ |
---|
1787 | n/a | if (step < 0) { |
---|
1788 | n/a | stop = start + 1; |
---|
1789 | n/a | start = stop + step * (slicelen - 1) - 1; |
---|
1790 | n/a | step = -step; |
---|
1791 | n/a | } |
---|
1792 | n/a | |
---|
1793 | n/a | assert((size_t)slicelen <= SIZE_MAX / sizeof(PyObject *)); |
---|
1794 | n/a | |
---|
1795 | n/a | /* recycle is a list that will contain all the children |
---|
1796 | n/a | * scheduled for removal. |
---|
1797 | n/a | */ |
---|
1798 | n/a | if (!(recycle = PyList_New(slicelen))) { |
---|
1799 | n/a | PyErr_NoMemory(); |
---|
1800 | n/a | return -1; |
---|
1801 | n/a | } |
---|
1802 | n/a | |
---|
1803 | n/a | /* This loop walks over all the children that have to be deleted, |
---|
1804 | n/a | * with cur pointing at them. num_moved is the amount of children |
---|
1805 | n/a | * until the next deleted child that have to be "shifted down" to |
---|
1806 | n/a | * occupy the deleted's places. |
---|
1807 | n/a | * Note that in the ith iteration, shifting is done i+i places down |
---|
1808 | n/a | * because i children were already removed. |
---|
1809 | n/a | */ |
---|
1810 | n/a | for (cur = start, i = 0; cur < (size_t)stop; cur += step, ++i) { |
---|
1811 | n/a | /* Compute how many children have to be moved, clipping at the |
---|
1812 | n/a | * list end. |
---|
1813 | n/a | */ |
---|
1814 | n/a | Py_ssize_t num_moved = step - 1; |
---|
1815 | n/a | if (cur + step >= (size_t)self->extra->length) { |
---|
1816 | n/a | num_moved = self->extra->length - cur - 1; |
---|
1817 | n/a | } |
---|
1818 | n/a | |
---|
1819 | n/a | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
---|
1820 | n/a | |
---|
1821 | n/a | memmove( |
---|
1822 | n/a | self->extra->children + cur - i, |
---|
1823 | n/a | self->extra->children + cur + 1, |
---|
1824 | n/a | num_moved * sizeof(PyObject *)); |
---|
1825 | n/a | } |
---|
1826 | n/a | |
---|
1827 | n/a | /* Leftover "tail" after the last removed child */ |
---|
1828 | n/a | cur = start + (size_t)slicelen * step; |
---|
1829 | n/a | if (cur < (size_t)self->extra->length) { |
---|
1830 | n/a | memmove( |
---|
1831 | n/a | self->extra->children + cur - slicelen, |
---|
1832 | n/a | self->extra->children + cur, |
---|
1833 | n/a | (self->extra->length - cur) * sizeof(PyObject *)); |
---|
1834 | n/a | } |
---|
1835 | n/a | |
---|
1836 | n/a | self->extra->length -= slicelen; |
---|
1837 | n/a | |
---|
1838 | n/a | /* Discard the recycle list with all the deleted sub-elements */ |
---|
1839 | n/a | Py_XDECREF(recycle); |
---|
1840 | n/a | return 0; |
---|
1841 | n/a | } |
---|
1842 | n/a | |
---|
1843 | n/a | /* A new slice is actually being assigned */ |
---|
1844 | n/a | seq = PySequence_Fast(value, ""); |
---|
1845 | n/a | if (!seq) { |
---|
1846 | n/a | PyErr_Format( |
---|
1847 | n/a | PyExc_TypeError, |
---|
1848 | n/a | "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name |
---|
1849 | n/a | ); |
---|
1850 | n/a | return -1; |
---|
1851 | n/a | } |
---|
1852 | n/a | newlen = PySequence_Size(seq); |
---|
1853 | n/a | |
---|
1854 | n/a | if (step != 1 && newlen != slicelen) |
---|
1855 | n/a | { |
---|
1856 | n/a | Py_DECREF(seq); |
---|
1857 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1858 | n/a | "attempt to assign sequence of size %zd " |
---|
1859 | n/a | "to extended slice of size %zd", |
---|
1860 | n/a | newlen, slicelen |
---|
1861 | n/a | ); |
---|
1862 | n/a | return -1; |
---|
1863 | n/a | } |
---|
1864 | n/a | |
---|
1865 | n/a | /* Resize before creating the recycle bin, to prevent refleaks. */ |
---|
1866 | n/a | if (newlen > slicelen) { |
---|
1867 | n/a | if (element_resize(self, newlen - slicelen) < 0) { |
---|
1868 | n/a | Py_DECREF(seq); |
---|
1869 | n/a | return -1; |
---|
1870 | n/a | } |
---|
1871 | n/a | } |
---|
1872 | n/a | |
---|
1873 | n/a | if (slicelen > 0) { |
---|
1874 | n/a | /* to avoid recursive calls to this method (via decref), move |
---|
1875 | n/a | old items to the recycle bin here, and get rid of them when |
---|
1876 | n/a | we're done modifying the element */ |
---|
1877 | n/a | recycle = PyList_New(slicelen); |
---|
1878 | n/a | if (!recycle) { |
---|
1879 | n/a | Py_DECREF(seq); |
---|
1880 | n/a | return -1; |
---|
1881 | n/a | } |
---|
1882 | n/a | for (cur = start, i = 0; i < slicelen; |
---|
1883 | n/a | cur += step, i++) |
---|
1884 | n/a | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
---|
1885 | n/a | } |
---|
1886 | n/a | |
---|
1887 | n/a | if (newlen < slicelen) { |
---|
1888 | n/a | /* delete slice */ |
---|
1889 | n/a | for (i = stop; i < self->extra->length; i++) |
---|
1890 | n/a | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
---|
1891 | n/a | } else if (newlen > slicelen) { |
---|
1892 | n/a | /* insert slice */ |
---|
1893 | n/a | for (i = self->extra->length-1; i >= stop; i--) |
---|
1894 | n/a | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
---|
1895 | n/a | } |
---|
1896 | n/a | |
---|
1897 | n/a | /* replace the slice */ |
---|
1898 | n/a | for (cur = start, i = 0; i < newlen; |
---|
1899 | n/a | cur += step, i++) { |
---|
1900 | n/a | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
---|
1901 | n/a | Py_INCREF(element); |
---|
1902 | n/a | self->extra->children[cur] = element; |
---|
1903 | n/a | } |
---|
1904 | n/a | |
---|
1905 | n/a | self->extra->length += newlen - slicelen; |
---|
1906 | n/a | |
---|
1907 | n/a | Py_DECREF(seq); |
---|
1908 | n/a | |
---|
1909 | n/a | /* discard the recycle bin, and everything in it */ |
---|
1910 | n/a | Py_XDECREF(recycle); |
---|
1911 | n/a | |
---|
1912 | n/a | return 0; |
---|
1913 | n/a | } |
---|
1914 | n/a | else { |
---|
1915 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1916 | n/a | "element indices must be integers"); |
---|
1917 | n/a | return -1; |
---|
1918 | n/a | } |
---|
1919 | n/a | } |
---|
1920 | n/a | |
---|
1921 | n/a | static PyObject* |
---|
1922 | n/a | element_tag_getter(ElementObject *self, void *closure) |
---|
1923 | n/a | { |
---|
1924 | n/a | PyObject *res = self->tag; |
---|
1925 | n/a | Py_INCREF(res); |
---|
1926 | n/a | return res; |
---|
1927 | n/a | } |
---|
1928 | n/a | |
---|
1929 | n/a | static PyObject* |
---|
1930 | n/a | element_text_getter(ElementObject *self, void *closure) |
---|
1931 | n/a | { |
---|
1932 | n/a | PyObject *res = element_get_text(self); |
---|
1933 | n/a | Py_XINCREF(res); |
---|
1934 | n/a | return res; |
---|
1935 | n/a | } |
---|
1936 | n/a | |
---|
1937 | n/a | static PyObject* |
---|
1938 | n/a | element_tail_getter(ElementObject *self, void *closure) |
---|
1939 | n/a | { |
---|
1940 | n/a | PyObject *res = element_get_tail(self); |
---|
1941 | n/a | Py_XINCREF(res); |
---|
1942 | n/a | return res; |
---|
1943 | n/a | } |
---|
1944 | n/a | |
---|
1945 | n/a | static PyObject* |
---|
1946 | n/a | element_attrib_getter(ElementObject *self, void *closure) |
---|
1947 | n/a | { |
---|
1948 | n/a | PyObject *res; |
---|
1949 | n/a | if (!self->extra) { |
---|
1950 | n/a | if (create_extra(self, NULL) < 0) |
---|
1951 | n/a | return NULL; |
---|
1952 | n/a | } |
---|
1953 | n/a | res = element_get_attrib(self); |
---|
1954 | n/a | Py_XINCREF(res); |
---|
1955 | n/a | return res; |
---|
1956 | n/a | } |
---|
1957 | n/a | |
---|
1958 | n/a | /* macro for setter validation */ |
---|
1959 | n/a | #define _VALIDATE_ATTR_VALUE(V) \ |
---|
1960 | n/a | if ((V) == NULL) { \ |
---|
1961 | n/a | PyErr_SetString( \ |
---|
1962 | n/a | PyExc_AttributeError, \ |
---|
1963 | n/a | "can't delete element attribute"); \ |
---|
1964 | n/a | return -1; \ |
---|
1965 | n/a | } |
---|
1966 | n/a | |
---|
1967 | n/a | static int |
---|
1968 | n/a | element_tag_setter(ElementObject *self, PyObject *value, void *closure) |
---|
1969 | n/a | { |
---|
1970 | n/a | _VALIDATE_ATTR_VALUE(value); |
---|
1971 | n/a | Py_INCREF(value); |
---|
1972 | n/a | Py_SETREF(self->tag, value); |
---|
1973 | n/a | return 0; |
---|
1974 | n/a | } |
---|
1975 | n/a | |
---|
1976 | n/a | static int |
---|
1977 | n/a | element_text_setter(ElementObject *self, PyObject *value, void *closure) |
---|
1978 | n/a | { |
---|
1979 | n/a | _VALIDATE_ATTR_VALUE(value); |
---|
1980 | n/a | Py_INCREF(value); |
---|
1981 | n/a | Py_DECREF(JOIN_OBJ(self->text)); |
---|
1982 | n/a | self->text = value; |
---|
1983 | n/a | return 0; |
---|
1984 | n/a | } |
---|
1985 | n/a | |
---|
1986 | n/a | static int |
---|
1987 | n/a | element_tail_setter(ElementObject *self, PyObject *value, void *closure) |
---|
1988 | n/a | { |
---|
1989 | n/a | _VALIDATE_ATTR_VALUE(value); |
---|
1990 | n/a | Py_INCREF(value); |
---|
1991 | n/a | Py_DECREF(JOIN_OBJ(self->tail)); |
---|
1992 | n/a | self->tail = value; |
---|
1993 | n/a | return 0; |
---|
1994 | n/a | } |
---|
1995 | n/a | |
---|
1996 | n/a | static int |
---|
1997 | n/a | element_attrib_setter(ElementObject *self, PyObject *value, void *closure) |
---|
1998 | n/a | { |
---|
1999 | n/a | _VALIDATE_ATTR_VALUE(value); |
---|
2000 | n/a | if (!self->extra) { |
---|
2001 | n/a | if (create_extra(self, NULL) < 0) |
---|
2002 | n/a | return -1; |
---|
2003 | n/a | } |
---|
2004 | n/a | Py_INCREF(value); |
---|
2005 | n/a | Py_SETREF(self->extra->attrib, value); |
---|
2006 | n/a | return 0; |
---|
2007 | n/a | } |
---|
2008 | n/a | |
---|
2009 | n/a | static PySequenceMethods element_as_sequence = { |
---|
2010 | n/a | (lenfunc) element_length, |
---|
2011 | n/a | 0, /* sq_concat */ |
---|
2012 | n/a | 0, /* sq_repeat */ |
---|
2013 | n/a | element_getitem, |
---|
2014 | n/a | 0, |
---|
2015 | n/a | element_setitem, |
---|
2016 | n/a | 0, |
---|
2017 | n/a | }; |
---|
2018 | n/a | |
---|
2019 | n/a | /******************************* Element iterator ****************************/ |
---|
2020 | n/a | |
---|
2021 | n/a | /* ElementIterObject represents the iteration state over an XML element in |
---|
2022 | n/a | * pre-order traversal. To keep track of which sub-element should be returned |
---|
2023 | n/a | * next, a stack of parents is maintained. This is a standard stack-based |
---|
2024 | n/a | * iterative pre-order traversal of a tree. |
---|
2025 | n/a | * The stack is managed using a continuous array. |
---|
2026 | n/a | * Each stack item contains the saved parent to which we should return after |
---|
2027 | n/a | * the current one is exhausted, and the next child to examine in that parent. |
---|
2028 | n/a | */ |
---|
2029 | n/a | typedef struct ParentLocator_t { |
---|
2030 | n/a | ElementObject *parent; |
---|
2031 | n/a | Py_ssize_t child_index; |
---|
2032 | n/a | } ParentLocator; |
---|
2033 | n/a | |
---|
2034 | n/a | typedef struct { |
---|
2035 | n/a | PyObject_HEAD |
---|
2036 | n/a | ParentLocator *parent_stack; |
---|
2037 | n/a | Py_ssize_t parent_stack_used; |
---|
2038 | n/a | Py_ssize_t parent_stack_size; |
---|
2039 | n/a | ElementObject *root_element; |
---|
2040 | n/a | PyObject *sought_tag; |
---|
2041 | n/a | int gettext; |
---|
2042 | n/a | } ElementIterObject; |
---|
2043 | n/a | |
---|
2044 | n/a | |
---|
2045 | n/a | static void |
---|
2046 | n/a | elementiter_dealloc(ElementIterObject *it) |
---|
2047 | n/a | { |
---|
2048 | n/a | Py_ssize_t i = it->parent_stack_used; |
---|
2049 | n/a | it->parent_stack_used = 0; |
---|
2050 | n/a | while (i--) |
---|
2051 | n/a | Py_XDECREF(it->parent_stack[i].parent); |
---|
2052 | n/a | PyMem_Free(it->parent_stack); |
---|
2053 | n/a | |
---|
2054 | n/a | Py_XDECREF(it->sought_tag); |
---|
2055 | n/a | Py_XDECREF(it->root_element); |
---|
2056 | n/a | |
---|
2057 | n/a | PyObject_GC_UnTrack(it); |
---|
2058 | n/a | PyObject_GC_Del(it); |
---|
2059 | n/a | } |
---|
2060 | n/a | |
---|
2061 | n/a | static int |
---|
2062 | n/a | elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg) |
---|
2063 | n/a | { |
---|
2064 | n/a | Py_ssize_t i = it->parent_stack_used; |
---|
2065 | n/a | while (i--) |
---|
2066 | n/a | Py_VISIT(it->parent_stack[i].parent); |
---|
2067 | n/a | |
---|
2068 | n/a | Py_VISIT(it->root_element); |
---|
2069 | n/a | Py_VISIT(it->sought_tag); |
---|
2070 | n/a | return 0; |
---|
2071 | n/a | } |
---|
2072 | n/a | |
---|
2073 | n/a | /* Helper function for elementiter_next. Add a new parent to the parent stack. |
---|
2074 | n/a | */ |
---|
2075 | n/a | static int |
---|
2076 | n/a | parent_stack_push_new(ElementIterObject *it, ElementObject *parent) |
---|
2077 | n/a | { |
---|
2078 | n/a | ParentLocator *item; |
---|
2079 | n/a | |
---|
2080 | n/a | if (it->parent_stack_used >= it->parent_stack_size) { |
---|
2081 | n/a | Py_ssize_t new_size = it->parent_stack_size * 2; /* never overflow */ |
---|
2082 | n/a | ParentLocator *parent_stack = it->parent_stack; |
---|
2083 | n/a | PyMem_Resize(parent_stack, ParentLocator, new_size); |
---|
2084 | n/a | if (parent_stack == NULL) |
---|
2085 | n/a | return -1; |
---|
2086 | n/a | it->parent_stack = parent_stack; |
---|
2087 | n/a | it->parent_stack_size = new_size; |
---|
2088 | n/a | } |
---|
2089 | n/a | item = it->parent_stack + it->parent_stack_used++; |
---|
2090 | n/a | Py_INCREF(parent); |
---|
2091 | n/a | item->parent = parent; |
---|
2092 | n/a | item->child_index = 0; |
---|
2093 | n/a | return 0; |
---|
2094 | n/a | } |
---|
2095 | n/a | |
---|
2096 | n/a | static PyObject * |
---|
2097 | n/a | elementiter_next(ElementIterObject *it) |
---|
2098 | n/a | { |
---|
2099 | n/a | /* Sub-element iterator. |
---|
2100 | n/a | * |
---|
2101 | n/a | * A short note on gettext: this function serves both the iter() and |
---|
2102 | n/a | * itertext() methods to avoid code duplication. However, there are a few |
---|
2103 | n/a | * small differences in the way these iterations work. Namely: |
---|
2104 | n/a | * - itertext() only yields text from nodes that have it, and continues |
---|
2105 | n/a | * iterating when a node doesn't have text (so it doesn't return any |
---|
2106 | n/a | * node like iter()) |
---|
2107 | n/a | * - itertext() also has to handle tail, after finishing with all the |
---|
2108 | n/a | * children of a node. |
---|
2109 | n/a | */ |
---|
2110 | n/a | int rc; |
---|
2111 | n/a | ElementObject *elem; |
---|
2112 | n/a | PyObject *text; |
---|
2113 | n/a | |
---|
2114 | n/a | while (1) { |
---|
2115 | n/a | /* Handle the case reached in the beginning and end of iteration, where |
---|
2116 | n/a | * the parent stack is empty. If root_element is NULL and we're here, the |
---|
2117 | n/a | * iterator is exhausted. |
---|
2118 | n/a | */ |
---|
2119 | n/a | if (!it->parent_stack_used) { |
---|
2120 | n/a | if (!it->root_element) { |
---|
2121 | n/a | PyErr_SetNone(PyExc_StopIteration); |
---|
2122 | n/a | return NULL; |
---|
2123 | n/a | } |
---|
2124 | n/a | |
---|
2125 | n/a | elem = it->root_element; /* steals a reference */ |
---|
2126 | n/a | it->root_element = NULL; |
---|
2127 | n/a | } |
---|
2128 | n/a | else { |
---|
2129 | n/a | /* See if there are children left to traverse in the current parent. If |
---|
2130 | n/a | * yes, visit the next child. If not, pop the stack and try again. |
---|
2131 | n/a | */ |
---|
2132 | n/a | ParentLocator *item = &it->parent_stack[it->parent_stack_used - 1]; |
---|
2133 | n/a | Py_ssize_t child_index = item->child_index; |
---|
2134 | n/a | ElementObjectExtra *extra; |
---|
2135 | n/a | elem = item->parent; |
---|
2136 | n/a | extra = elem->extra; |
---|
2137 | n/a | if (!extra || child_index >= extra->length) { |
---|
2138 | n/a | it->parent_stack_used--; |
---|
2139 | n/a | /* Note that extra condition on it->parent_stack_used here; |
---|
2140 | n/a | * this is because itertext() is supposed to only return *inner* |
---|
2141 | n/a | * text, not text following the element it began iteration with. |
---|
2142 | n/a | */ |
---|
2143 | n/a | if (it->gettext && it->parent_stack_used) { |
---|
2144 | n/a | text = element_get_tail(elem); |
---|
2145 | n/a | goto gettext; |
---|
2146 | n/a | } |
---|
2147 | n/a | Py_DECREF(elem); |
---|
2148 | n/a | continue; |
---|
2149 | n/a | } |
---|
2150 | n/a | |
---|
2151 | n/a | elem = (ElementObject *)extra->children[child_index]; |
---|
2152 | n/a | item->child_index++; |
---|
2153 | n/a | Py_INCREF(elem); |
---|
2154 | n/a | } |
---|
2155 | n/a | |
---|
2156 | n/a | if (parent_stack_push_new(it, elem) < 0) { |
---|
2157 | n/a | Py_DECREF(elem); |
---|
2158 | n/a | PyErr_NoMemory(); |
---|
2159 | n/a | return NULL; |
---|
2160 | n/a | } |
---|
2161 | n/a | if (it->gettext) { |
---|
2162 | n/a | text = element_get_text(elem); |
---|
2163 | n/a | goto gettext; |
---|
2164 | n/a | } |
---|
2165 | n/a | |
---|
2166 | n/a | if (it->sought_tag == Py_None) |
---|
2167 | n/a | return (PyObject *)elem; |
---|
2168 | n/a | |
---|
2169 | n/a | rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ); |
---|
2170 | n/a | if (rc > 0) |
---|
2171 | n/a | return (PyObject *)elem; |
---|
2172 | n/a | |
---|
2173 | n/a | Py_DECREF(elem); |
---|
2174 | n/a | if (rc < 0) |
---|
2175 | n/a | return NULL; |
---|
2176 | n/a | continue; |
---|
2177 | n/a | |
---|
2178 | n/a | gettext: |
---|
2179 | n/a | if (!text) { |
---|
2180 | n/a | Py_DECREF(elem); |
---|
2181 | n/a | return NULL; |
---|
2182 | n/a | } |
---|
2183 | n/a | if (text == Py_None) { |
---|
2184 | n/a | Py_DECREF(elem); |
---|
2185 | n/a | } |
---|
2186 | n/a | else { |
---|
2187 | n/a | Py_INCREF(text); |
---|
2188 | n/a | Py_DECREF(elem); |
---|
2189 | n/a | rc = PyObject_IsTrue(text); |
---|
2190 | n/a | if (rc > 0) |
---|
2191 | n/a | return text; |
---|
2192 | n/a | Py_DECREF(text); |
---|
2193 | n/a | if (rc < 0) |
---|
2194 | n/a | return NULL; |
---|
2195 | n/a | } |
---|
2196 | n/a | } |
---|
2197 | n/a | |
---|
2198 | n/a | return NULL; |
---|
2199 | n/a | } |
---|
2200 | n/a | |
---|
2201 | n/a | |
---|
2202 | n/a | static PyTypeObject ElementIter_Type = { |
---|
2203 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
2204 | n/a | /* Using the module's name since the pure-Python implementation does not |
---|
2205 | n/a | have such a type. */ |
---|
2206 | n/a | "_elementtree._element_iterator", /* tp_name */ |
---|
2207 | n/a | sizeof(ElementIterObject), /* tp_basicsize */ |
---|
2208 | n/a | 0, /* tp_itemsize */ |
---|
2209 | n/a | /* methods */ |
---|
2210 | n/a | (destructor)elementiter_dealloc, /* tp_dealloc */ |
---|
2211 | n/a | 0, /* tp_print */ |
---|
2212 | n/a | 0, /* tp_getattr */ |
---|
2213 | n/a | 0, /* tp_setattr */ |
---|
2214 | n/a | 0, /* tp_reserved */ |
---|
2215 | n/a | 0, /* tp_repr */ |
---|
2216 | n/a | 0, /* tp_as_number */ |
---|
2217 | n/a | 0, /* tp_as_sequence */ |
---|
2218 | n/a | 0, /* tp_as_mapping */ |
---|
2219 | n/a | 0, /* tp_hash */ |
---|
2220 | n/a | 0, /* tp_call */ |
---|
2221 | n/a | 0, /* tp_str */ |
---|
2222 | n/a | 0, /* tp_getattro */ |
---|
2223 | n/a | 0, /* tp_setattro */ |
---|
2224 | n/a | 0, /* tp_as_buffer */ |
---|
2225 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
2226 | n/a | 0, /* tp_doc */ |
---|
2227 | n/a | (traverseproc)elementiter_traverse, /* tp_traverse */ |
---|
2228 | n/a | 0, /* tp_clear */ |
---|
2229 | n/a | 0, /* tp_richcompare */ |
---|
2230 | n/a | 0, /* tp_weaklistoffset */ |
---|
2231 | n/a | PyObject_SelfIter, /* tp_iter */ |
---|
2232 | n/a | (iternextfunc)elementiter_next, /* tp_iternext */ |
---|
2233 | n/a | 0, /* tp_methods */ |
---|
2234 | n/a | 0, /* tp_members */ |
---|
2235 | n/a | 0, /* tp_getset */ |
---|
2236 | n/a | 0, /* tp_base */ |
---|
2237 | n/a | 0, /* tp_dict */ |
---|
2238 | n/a | 0, /* tp_descr_get */ |
---|
2239 | n/a | 0, /* tp_descr_set */ |
---|
2240 | n/a | 0, /* tp_dictoffset */ |
---|
2241 | n/a | 0, /* tp_init */ |
---|
2242 | n/a | 0, /* tp_alloc */ |
---|
2243 | n/a | 0, /* tp_new */ |
---|
2244 | n/a | }; |
---|
2245 | n/a | |
---|
2246 | n/a | #define INIT_PARENT_STACK_SIZE 8 |
---|
2247 | n/a | |
---|
2248 | n/a | static PyObject * |
---|
2249 | n/a | create_elementiter(ElementObject *self, PyObject *tag, int gettext) |
---|
2250 | n/a | { |
---|
2251 | n/a | ElementIterObject *it; |
---|
2252 | n/a | |
---|
2253 | n/a | it = PyObject_GC_New(ElementIterObject, &ElementIter_Type); |
---|
2254 | n/a | if (!it) |
---|
2255 | n/a | return NULL; |
---|
2256 | n/a | |
---|
2257 | n/a | Py_INCREF(tag); |
---|
2258 | n/a | it->sought_tag = tag; |
---|
2259 | n/a | it->gettext = gettext; |
---|
2260 | n/a | Py_INCREF(self); |
---|
2261 | n/a | it->root_element = self; |
---|
2262 | n/a | |
---|
2263 | n/a | PyObject_GC_Track(it); |
---|
2264 | n/a | |
---|
2265 | n/a | it->parent_stack = PyMem_New(ParentLocator, INIT_PARENT_STACK_SIZE); |
---|
2266 | n/a | if (it->parent_stack == NULL) { |
---|
2267 | n/a | Py_DECREF(it); |
---|
2268 | n/a | PyErr_NoMemory(); |
---|
2269 | n/a | return NULL; |
---|
2270 | n/a | } |
---|
2271 | n/a | it->parent_stack_used = 0; |
---|
2272 | n/a | it->parent_stack_size = INIT_PARENT_STACK_SIZE; |
---|
2273 | n/a | |
---|
2274 | n/a | return (PyObject *)it; |
---|
2275 | n/a | } |
---|
2276 | n/a | |
---|
2277 | n/a | |
---|
2278 | n/a | /* ==================================================================== */ |
---|
2279 | n/a | /* the tree builder type */ |
---|
2280 | n/a | |
---|
2281 | n/a | typedef struct { |
---|
2282 | n/a | PyObject_HEAD |
---|
2283 | n/a | |
---|
2284 | n/a | PyObject *root; /* root node (first created node) */ |
---|
2285 | n/a | |
---|
2286 | n/a | PyObject *this; /* current node */ |
---|
2287 | n/a | PyObject *last; /* most recently created node */ |
---|
2288 | n/a | |
---|
2289 | n/a | PyObject *data; /* data collector (string or list), or NULL */ |
---|
2290 | n/a | |
---|
2291 | n/a | PyObject *stack; /* element stack */ |
---|
2292 | n/a | Py_ssize_t index; /* current stack size (0 means empty) */ |
---|
2293 | n/a | |
---|
2294 | n/a | PyObject *element_factory; |
---|
2295 | n/a | |
---|
2296 | n/a | /* element tracing */ |
---|
2297 | n/a | PyObject *events_append; /* the append method of the list of events, or NULL */ |
---|
2298 | n/a | PyObject *start_event_obj; /* event objects (NULL to ignore) */ |
---|
2299 | n/a | PyObject *end_event_obj; |
---|
2300 | n/a | PyObject *start_ns_event_obj; |
---|
2301 | n/a | PyObject *end_ns_event_obj; |
---|
2302 | n/a | } TreeBuilderObject; |
---|
2303 | n/a | |
---|
2304 | n/a | #define TreeBuilder_CheckExact(op) (Py_TYPE(op) == &TreeBuilder_Type) |
---|
2305 | n/a | |
---|
2306 | n/a | /* -------------------------------------------------------------------- */ |
---|
2307 | n/a | /* constructor and destructor */ |
---|
2308 | n/a | |
---|
2309 | n/a | static PyObject * |
---|
2310 | n/a | treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
2311 | n/a | { |
---|
2312 | n/a | TreeBuilderObject *t = (TreeBuilderObject *)type->tp_alloc(type, 0); |
---|
2313 | n/a | if (t != NULL) { |
---|
2314 | n/a | t->root = NULL; |
---|
2315 | n/a | |
---|
2316 | n/a | Py_INCREF(Py_None); |
---|
2317 | n/a | t->this = Py_None; |
---|
2318 | n/a | Py_INCREF(Py_None); |
---|
2319 | n/a | t->last = Py_None; |
---|
2320 | n/a | |
---|
2321 | n/a | t->data = NULL; |
---|
2322 | n/a | t->element_factory = NULL; |
---|
2323 | n/a | t->stack = PyList_New(20); |
---|
2324 | n/a | if (!t->stack) { |
---|
2325 | n/a | Py_DECREF(t->this); |
---|
2326 | n/a | Py_DECREF(t->last); |
---|
2327 | n/a | Py_DECREF((PyObject *) t); |
---|
2328 | n/a | return NULL; |
---|
2329 | n/a | } |
---|
2330 | n/a | t->index = 0; |
---|
2331 | n/a | |
---|
2332 | n/a | t->events_append = NULL; |
---|
2333 | n/a | t->start_event_obj = t->end_event_obj = NULL; |
---|
2334 | n/a | t->start_ns_event_obj = t->end_ns_event_obj = NULL; |
---|
2335 | n/a | } |
---|
2336 | n/a | return (PyObject *)t; |
---|
2337 | n/a | } |
---|
2338 | n/a | |
---|
2339 | n/a | /*[clinic input] |
---|
2340 | n/a | _elementtree.TreeBuilder.__init__ |
---|
2341 | n/a | |
---|
2342 | n/a | element_factory: object = NULL |
---|
2343 | n/a | |
---|
2344 | n/a | [clinic start generated code]*/ |
---|
2345 | n/a | |
---|
2346 | n/a | static int |
---|
2347 | n/a | _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self, |
---|
2348 | n/a | PyObject *element_factory) |
---|
2349 | n/a | /*[clinic end generated code: output=91cfa7558970ee96 input=1b424eeefc35249c]*/ |
---|
2350 | n/a | { |
---|
2351 | n/a | if (element_factory) { |
---|
2352 | n/a | Py_INCREF(element_factory); |
---|
2353 | n/a | Py_XSETREF(self->element_factory, element_factory); |
---|
2354 | n/a | } |
---|
2355 | n/a | |
---|
2356 | n/a | return 0; |
---|
2357 | n/a | } |
---|
2358 | n/a | |
---|
2359 | n/a | static int |
---|
2360 | n/a | treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg) |
---|
2361 | n/a | { |
---|
2362 | n/a | Py_VISIT(self->root); |
---|
2363 | n/a | Py_VISIT(self->this); |
---|
2364 | n/a | Py_VISIT(self->last); |
---|
2365 | n/a | Py_VISIT(self->data); |
---|
2366 | n/a | Py_VISIT(self->stack); |
---|
2367 | n/a | Py_VISIT(self->element_factory); |
---|
2368 | n/a | return 0; |
---|
2369 | n/a | } |
---|
2370 | n/a | |
---|
2371 | n/a | static int |
---|
2372 | n/a | treebuilder_gc_clear(TreeBuilderObject *self) |
---|
2373 | n/a | { |
---|
2374 | n/a | Py_CLEAR(self->end_ns_event_obj); |
---|
2375 | n/a | Py_CLEAR(self->start_ns_event_obj); |
---|
2376 | n/a | Py_CLEAR(self->end_event_obj); |
---|
2377 | n/a | Py_CLEAR(self->start_event_obj); |
---|
2378 | n/a | Py_CLEAR(self->events_append); |
---|
2379 | n/a | Py_CLEAR(self->stack); |
---|
2380 | n/a | Py_CLEAR(self->data); |
---|
2381 | n/a | Py_CLEAR(self->last); |
---|
2382 | n/a | Py_CLEAR(self->this); |
---|
2383 | n/a | Py_CLEAR(self->element_factory); |
---|
2384 | n/a | Py_CLEAR(self->root); |
---|
2385 | n/a | return 0; |
---|
2386 | n/a | } |
---|
2387 | n/a | |
---|
2388 | n/a | static void |
---|
2389 | n/a | treebuilder_dealloc(TreeBuilderObject *self) |
---|
2390 | n/a | { |
---|
2391 | n/a | PyObject_GC_UnTrack(self); |
---|
2392 | n/a | treebuilder_gc_clear(self); |
---|
2393 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
---|
2394 | n/a | } |
---|
2395 | n/a | |
---|
2396 | n/a | /* -------------------------------------------------------------------- */ |
---|
2397 | n/a | /* helpers for handling of arbitrary element-like objects */ |
---|
2398 | n/a | |
---|
2399 | n/a | static int |
---|
2400 | n/a | treebuilder_set_element_text_or_tail(PyObject *element, PyObject *data, |
---|
2401 | n/a | PyObject **dest, _Py_Identifier *name) |
---|
2402 | n/a | { |
---|
2403 | n/a | if (Element_CheckExact(element)) { |
---|
2404 | n/a | Py_DECREF(JOIN_OBJ(*dest)); |
---|
2405 | n/a | *dest = JOIN_SET(data, PyList_CheckExact(data)); |
---|
2406 | n/a | return 0; |
---|
2407 | n/a | } |
---|
2408 | n/a | else { |
---|
2409 | n/a | PyObject *joined = list_join(data); |
---|
2410 | n/a | int r; |
---|
2411 | n/a | if (joined == NULL) |
---|
2412 | n/a | return -1; |
---|
2413 | n/a | r = _PyObject_SetAttrId(element, name, joined); |
---|
2414 | n/a | Py_DECREF(joined); |
---|
2415 | n/a | return r; |
---|
2416 | n/a | } |
---|
2417 | n/a | } |
---|
2418 | n/a | |
---|
2419 | n/a | /* These two functions steal a reference to data */ |
---|
2420 | n/a | static int |
---|
2421 | n/a | treebuilder_set_element_text(PyObject *element, PyObject *data) |
---|
2422 | n/a | { |
---|
2423 | n/a | _Py_IDENTIFIER(text); |
---|
2424 | n/a | return treebuilder_set_element_text_or_tail( |
---|
2425 | n/a | element, data, &((ElementObject *) element)->text, &PyId_text); |
---|
2426 | n/a | } |
---|
2427 | n/a | |
---|
2428 | n/a | static int |
---|
2429 | n/a | treebuilder_set_element_tail(PyObject *element, PyObject *data) |
---|
2430 | n/a | { |
---|
2431 | n/a | _Py_IDENTIFIER(tail); |
---|
2432 | n/a | return treebuilder_set_element_text_or_tail( |
---|
2433 | n/a | element, data, &((ElementObject *) element)->tail, &PyId_tail); |
---|
2434 | n/a | } |
---|
2435 | n/a | |
---|
2436 | n/a | static int |
---|
2437 | n/a | treebuilder_add_subelement(PyObject *element, PyObject *child) |
---|
2438 | n/a | { |
---|
2439 | n/a | _Py_IDENTIFIER(append); |
---|
2440 | n/a | if (Element_CheckExact(element)) { |
---|
2441 | n/a | ElementObject *elem = (ElementObject *) element; |
---|
2442 | n/a | return element_add_subelement(elem, child); |
---|
2443 | n/a | } |
---|
2444 | n/a | else { |
---|
2445 | n/a | PyObject *res; |
---|
2446 | n/a | res = _PyObject_CallMethodIdObjArgs(element, &PyId_append, child, NULL); |
---|
2447 | n/a | if (res == NULL) |
---|
2448 | n/a | return -1; |
---|
2449 | n/a | Py_DECREF(res); |
---|
2450 | n/a | return 0; |
---|
2451 | n/a | } |
---|
2452 | n/a | } |
---|
2453 | n/a | |
---|
2454 | n/a | LOCAL(int) |
---|
2455 | n/a | treebuilder_append_event(TreeBuilderObject *self, PyObject *action, |
---|
2456 | n/a | PyObject *node) |
---|
2457 | n/a | { |
---|
2458 | n/a | if (action != NULL) { |
---|
2459 | n/a | PyObject *res; |
---|
2460 | n/a | PyObject *event = PyTuple_Pack(2, action, node); |
---|
2461 | n/a | if (event == NULL) |
---|
2462 | n/a | return -1; |
---|
2463 | n/a | res = PyObject_CallFunctionObjArgs(self->events_append, event, NULL); |
---|
2464 | n/a | Py_DECREF(event); |
---|
2465 | n/a | if (res == NULL) |
---|
2466 | n/a | return -1; |
---|
2467 | n/a | Py_DECREF(res); |
---|
2468 | n/a | } |
---|
2469 | n/a | return 0; |
---|
2470 | n/a | } |
---|
2471 | n/a | |
---|
2472 | n/a | /* -------------------------------------------------------------------- */ |
---|
2473 | n/a | /* handlers */ |
---|
2474 | n/a | |
---|
2475 | n/a | LOCAL(PyObject*) |
---|
2476 | n/a | treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, |
---|
2477 | n/a | PyObject* attrib) |
---|
2478 | n/a | { |
---|
2479 | n/a | PyObject* node; |
---|
2480 | n/a | PyObject* this; |
---|
2481 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
2482 | n/a | |
---|
2483 | n/a | if (self->data) { |
---|
2484 | n/a | if (self->this == self->last) { |
---|
2485 | n/a | if (treebuilder_set_element_text(self->last, self->data)) |
---|
2486 | n/a | return NULL; |
---|
2487 | n/a | } |
---|
2488 | n/a | else { |
---|
2489 | n/a | if (treebuilder_set_element_tail(self->last, self->data)) |
---|
2490 | n/a | return NULL; |
---|
2491 | n/a | } |
---|
2492 | n/a | self->data = NULL; |
---|
2493 | n/a | } |
---|
2494 | n/a | |
---|
2495 | n/a | if (!self->element_factory || self->element_factory == Py_None) { |
---|
2496 | n/a | node = create_new_element(tag, attrib); |
---|
2497 | n/a | } else if (attrib == Py_None) { |
---|
2498 | n/a | attrib = PyDict_New(); |
---|
2499 | n/a | if (!attrib) |
---|
2500 | n/a | return NULL; |
---|
2501 | n/a | node = PyObject_CallFunctionObjArgs(self->element_factory, |
---|
2502 | n/a | tag, attrib, NULL); |
---|
2503 | n/a | Py_DECREF(attrib); |
---|
2504 | n/a | } |
---|
2505 | n/a | else { |
---|
2506 | n/a | node = PyObject_CallFunctionObjArgs(self->element_factory, |
---|
2507 | n/a | tag, attrib, NULL); |
---|
2508 | n/a | } |
---|
2509 | n/a | if (!node) { |
---|
2510 | n/a | return NULL; |
---|
2511 | n/a | } |
---|
2512 | n/a | |
---|
2513 | n/a | this = self->this; |
---|
2514 | n/a | |
---|
2515 | n/a | if (this != Py_None) { |
---|
2516 | n/a | if (treebuilder_add_subelement(this, node) < 0) |
---|
2517 | n/a | goto error; |
---|
2518 | n/a | } else { |
---|
2519 | n/a | if (self->root) { |
---|
2520 | n/a | PyErr_SetString( |
---|
2521 | n/a | st->parseerror_obj, |
---|
2522 | n/a | "multiple elements on top level" |
---|
2523 | n/a | ); |
---|
2524 | n/a | goto error; |
---|
2525 | n/a | } |
---|
2526 | n/a | Py_INCREF(node); |
---|
2527 | n/a | self->root = node; |
---|
2528 | n/a | } |
---|
2529 | n/a | |
---|
2530 | n/a | if (self->index < PyList_GET_SIZE(self->stack)) { |
---|
2531 | n/a | if (PyList_SetItem(self->stack, self->index, this) < 0) |
---|
2532 | n/a | goto error; |
---|
2533 | n/a | Py_INCREF(this); |
---|
2534 | n/a | } else { |
---|
2535 | n/a | if (PyList_Append(self->stack, this) < 0) |
---|
2536 | n/a | goto error; |
---|
2537 | n/a | } |
---|
2538 | n/a | self->index++; |
---|
2539 | n/a | |
---|
2540 | n/a | Py_INCREF(node); |
---|
2541 | n/a | Py_SETREF(self->this, node); |
---|
2542 | n/a | Py_INCREF(node); |
---|
2543 | n/a | Py_SETREF(self->last, node); |
---|
2544 | n/a | |
---|
2545 | n/a | if (treebuilder_append_event(self, self->start_event_obj, node) < 0) |
---|
2546 | n/a | goto error; |
---|
2547 | n/a | |
---|
2548 | n/a | return node; |
---|
2549 | n/a | |
---|
2550 | n/a | error: |
---|
2551 | n/a | Py_DECREF(node); |
---|
2552 | n/a | return NULL; |
---|
2553 | n/a | } |
---|
2554 | n/a | |
---|
2555 | n/a | LOCAL(PyObject*) |
---|
2556 | n/a | treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) |
---|
2557 | n/a | { |
---|
2558 | n/a | if (!self->data) { |
---|
2559 | n/a | if (self->last == Py_None) { |
---|
2560 | n/a | /* ignore calls to data before the first call to start */ |
---|
2561 | n/a | Py_RETURN_NONE; |
---|
2562 | n/a | } |
---|
2563 | n/a | /* store the first item as is */ |
---|
2564 | n/a | Py_INCREF(data); self->data = data; |
---|
2565 | n/a | } else { |
---|
2566 | n/a | /* more than one item; use a list to collect items */ |
---|
2567 | n/a | if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 && |
---|
2568 | n/a | PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) { |
---|
2569 | n/a | /* XXX this code path unused in Python 3? */ |
---|
2570 | n/a | /* expat often generates single character data sections; handle |
---|
2571 | n/a | the most common case by resizing the existing string... */ |
---|
2572 | n/a | Py_ssize_t size = PyBytes_GET_SIZE(self->data); |
---|
2573 | n/a | if (_PyBytes_Resize(&self->data, size + 1) < 0) |
---|
2574 | n/a | return NULL; |
---|
2575 | n/a | PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0]; |
---|
2576 | n/a | } else if (PyList_CheckExact(self->data)) { |
---|
2577 | n/a | if (PyList_Append(self->data, data) < 0) |
---|
2578 | n/a | return NULL; |
---|
2579 | n/a | } else { |
---|
2580 | n/a | PyObject* list = PyList_New(2); |
---|
2581 | n/a | if (!list) |
---|
2582 | n/a | return NULL; |
---|
2583 | n/a | PyList_SET_ITEM(list, 0, self->data); |
---|
2584 | n/a | Py_INCREF(data); PyList_SET_ITEM(list, 1, data); |
---|
2585 | n/a | self->data = list; |
---|
2586 | n/a | } |
---|
2587 | n/a | } |
---|
2588 | n/a | |
---|
2589 | n/a | Py_RETURN_NONE; |
---|
2590 | n/a | } |
---|
2591 | n/a | |
---|
2592 | n/a | LOCAL(PyObject*) |
---|
2593 | n/a | treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag) |
---|
2594 | n/a | { |
---|
2595 | n/a | PyObject* item; |
---|
2596 | n/a | |
---|
2597 | n/a | if (self->data) { |
---|
2598 | n/a | if (self->this == self->last) { |
---|
2599 | n/a | if (treebuilder_set_element_text(self->last, self->data)) |
---|
2600 | n/a | return NULL; |
---|
2601 | n/a | } else { |
---|
2602 | n/a | if (treebuilder_set_element_tail(self->last, self->data)) |
---|
2603 | n/a | return NULL; |
---|
2604 | n/a | } |
---|
2605 | n/a | self->data = NULL; |
---|
2606 | n/a | } |
---|
2607 | n/a | |
---|
2608 | n/a | if (self->index == 0) { |
---|
2609 | n/a | PyErr_SetString( |
---|
2610 | n/a | PyExc_IndexError, |
---|
2611 | n/a | "pop from empty stack" |
---|
2612 | n/a | ); |
---|
2613 | n/a | return NULL; |
---|
2614 | n/a | } |
---|
2615 | n/a | |
---|
2616 | n/a | item = self->last; |
---|
2617 | n/a | self->last = self->this; |
---|
2618 | n/a | self->index--; |
---|
2619 | n/a | self->this = PyList_GET_ITEM(self->stack, self->index); |
---|
2620 | n/a | Py_INCREF(self->this); |
---|
2621 | n/a | Py_DECREF(item); |
---|
2622 | n/a | |
---|
2623 | n/a | if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0) |
---|
2624 | n/a | return NULL; |
---|
2625 | n/a | |
---|
2626 | n/a | Py_INCREF(self->last); |
---|
2627 | n/a | return (PyObject*) self->last; |
---|
2628 | n/a | } |
---|
2629 | n/a | |
---|
2630 | n/a | /* -------------------------------------------------------------------- */ |
---|
2631 | n/a | /* methods (in alphabetical order) */ |
---|
2632 | n/a | |
---|
2633 | n/a | /*[clinic input] |
---|
2634 | n/a | _elementtree.TreeBuilder.data |
---|
2635 | n/a | |
---|
2636 | n/a | data: object |
---|
2637 | n/a | / |
---|
2638 | n/a | |
---|
2639 | n/a | [clinic start generated code]*/ |
---|
2640 | n/a | |
---|
2641 | n/a | static PyObject * |
---|
2642 | n/a | _elementtree_TreeBuilder_data(TreeBuilderObject *self, PyObject *data) |
---|
2643 | n/a | /*[clinic end generated code: output=69144c7100795bb2 input=a0540c532b284d29]*/ |
---|
2644 | n/a | { |
---|
2645 | n/a | return treebuilder_handle_data(self, data); |
---|
2646 | n/a | } |
---|
2647 | n/a | |
---|
2648 | n/a | /*[clinic input] |
---|
2649 | n/a | _elementtree.TreeBuilder.end |
---|
2650 | n/a | |
---|
2651 | n/a | tag: object |
---|
2652 | n/a | / |
---|
2653 | n/a | |
---|
2654 | n/a | [clinic start generated code]*/ |
---|
2655 | n/a | |
---|
2656 | n/a | static PyObject * |
---|
2657 | n/a | _elementtree_TreeBuilder_end(TreeBuilderObject *self, PyObject *tag) |
---|
2658 | n/a | /*[clinic end generated code: output=9a98727cc691cd9d input=22dc3674236f5745]*/ |
---|
2659 | n/a | { |
---|
2660 | n/a | return treebuilder_handle_end(self, tag); |
---|
2661 | n/a | } |
---|
2662 | n/a | |
---|
2663 | n/a | LOCAL(PyObject*) |
---|
2664 | n/a | treebuilder_done(TreeBuilderObject* self) |
---|
2665 | n/a | { |
---|
2666 | n/a | PyObject* res; |
---|
2667 | n/a | |
---|
2668 | n/a | /* FIXME: check stack size? */ |
---|
2669 | n/a | |
---|
2670 | n/a | if (self->root) |
---|
2671 | n/a | res = self->root; |
---|
2672 | n/a | else |
---|
2673 | n/a | res = Py_None; |
---|
2674 | n/a | |
---|
2675 | n/a | Py_INCREF(res); |
---|
2676 | n/a | return res; |
---|
2677 | n/a | } |
---|
2678 | n/a | |
---|
2679 | n/a | /*[clinic input] |
---|
2680 | n/a | _elementtree.TreeBuilder.close |
---|
2681 | n/a | |
---|
2682 | n/a | [clinic start generated code]*/ |
---|
2683 | n/a | |
---|
2684 | n/a | static PyObject * |
---|
2685 | n/a | _elementtree_TreeBuilder_close_impl(TreeBuilderObject *self) |
---|
2686 | n/a | /*[clinic end generated code: output=b441fee3202f61ee input=f7c9c65dc718de14]*/ |
---|
2687 | n/a | { |
---|
2688 | n/a | return treebuilder_done(self); |
---|
2689 | n/a | } |
---|
2690 | n/a | |
---|
2691 | n/a | /*[clinic input] |
---|
2692 | n/a | _elementtree.TreeBuilder.start |
---|
2693 | n/a | |
---|
2694 | n/a | tag: object |
---|
2695 | n/a | attrs: object = None |
---|
2696 | n/a | / |
---|
2697 | n/a | |
---|
2698 | n/a | [clinic start generated code]*/ |
---|
2699 | n/a | |
---|
2700 | n/a | static PyObject * |
---|
2701 | n/a | _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag, |
---|
2702 | n/a | PyObject *attrs) |
---|
2703 | n/a | /*[clinic end generated code: output=e7e9dc2861349411 input=95fc1758dd042c65]*/ |
---|
2704 | n/a | { |
---|
2705 | n/a | return treebuilder_handle_start(self, tag, attrs); |
---|
2706 | n/a | } |
---|
2707 | n/a | |
---|
2708 | n/a | /* ==================================================================== */ |
---|
2709 | n/a | /* the expat interface */ |
---|
2710 | n/a | |
---|
2711 | n/a | #include "expat.h" |
---|
2712 | n/a | #include "pyexpat.h" |
---|
2713 | n/a | |
---|
2714 | n/a | /* The PyExpat_CAPI structure is an immutable dispatch table, so it can be |
---|
2715 | n/a | * cached globally without being in per-module state. |
---|
2716 | n/a | */ |
---|
2717 | n/a | static struct PyExpat_CAPI *expat_capi; |
---|
2718 | n/a | #define EXPAT(func) (expat_capi->func) |
---|
2719 | n/a | |
---|
2720 | n/a | static XML_Memory_Handling_Suite ExpatMemoryHandler = { |
---|
2721 | n/a | PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
---|
2722 | n/a | |
---|
2723 | n/a | typedef struct { |
---|
2724 | n/a | PyObject_HEAD |
---|
2725 | n/a | |
---|
2726 | n/a | XML_Parser parser; |
---|
2727 | n/a | |
---|
2728 | n/a | PyObject *target; |
---|
2729 | n/a | PyObject *entity; |
---|
2730 | n/a | |
---|
2731 | n/a | PyObject *names; |
---|
2732 | n/a | |
---|
2733 | n/a | PyObject *handle_start; |
---|
2734 | n/a | PyObject *handle_data; |
---|
2735 | n/a | PyObject *handle_end; |
---|
2736 | n/a | |
---|
2737 | n/a | PyObject *handle_comment; |
---|
2738 | n/a | PyObject *handle_pi; |
---|
2739 | n/a | PyObject *handle_doctype; |
---|
2740 | n/a | |
---|
2741 | n/a | PyObject *handle_close; |
---|
2742 | n/a | |
---|
2743 | n/a | } XMLParserObject; |
---|
2744 | n/a | |
---|
2745 | n/a | static PyObject* |
---|
2746 | n/a | _elementtree_XMLParser_doctype(XMLParserObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames); |
---|
2747 | n/a | static PyObject * |
---|
2748 | n/a | _elementtree_XMLParser_doctype_impl(XMLParserObject *self, PyObject *name, |
---|
2749 | n/a | PyObject *pubid, PyObject *system); |
---|
2750 | n/a | |
---|
2751 | n/a | /* helpers */ |
---|
2752 | n/a | |
---|
2753 | n/a | LOCAL(PyObject*) |
---|
2754 | n/a | makeuniversal(XMLParserObject* self, const char* string) |
---|
2755 | n/a | { |
---|
2756 | n/a | /* convert a UTF-8 tag/attribute name from the expat parser |
---|
2757 | n/a | to a universal name string */ |
---|
2758 | n/a | |
---|
2759 | n/a | Py_ssize_t size = (Py_ssize_t) strlen(string); |
---|
2760 | n/a | PyObject* key; |
---|
2761 | n/a | PyObject* value; |
---|
2762 | n/a | |
---|
2763 | n/a | /* look the 'raw' name up in the names dictionary */ |
---|
2764 | n/a | key = PyBytes_FromStringAndSize(string, size); |
---|
2765 | n/a | if (!key) |
---|
2766 | n/a | return NULL; |
---|
2767 | n/a | |
---|
2768 | n/a | value = PyDict_GetItem(self->names, key); |
---|
2769 | n/a | |
---|
2770 | n/a | if (value) { |
---|
2771 | n/a | Py_INCREF(value); |
---|
2772 | n/a | } else { |
---|
2773 | n/a | /* new name. convert to universal name, and decode as |
---|
2774 | n/a | necessary */ |
---|
2775 | n/a | |
---|
2776 | n/a | PyObject* tag; |
---|
2777 | n/a | char* p; |
---|
2778 | n/a | Py_ssize_t i; |
---|
2779 | n/a | |
---|
2780 | n/a | /* look for namespace separator */ |
---|
2781 | n/a | for (i = 0; i < size; i++) |
---|
2782 | n/a | if (string[i] == '}') |
---|
2783 | n/a | break; |
---|
2784 | n/a | if (i != size) { |
---|
2785 | n/a | /* convert to universal name */ |
---|
2786 | n/a | tag = PyBytes_FromStringAndSize(NULL, size+1); |
---|
2787 | n/a | if (tag == NULL) { |
---|
2788 | n/a | Py_DECREF(key); |
---|
2789 | n/a | return NULL; |
---|
2790 | n/a | } |
---|
2791 | n/a | p = PyBytes_AS_STRING(tag); |
---|
2792 | n/a | p[0] = '{'; |
---|
2793 | n/a | memcpy(p+1, string, size); |
---|
2794 | n/a | size++; |
---|
2795 | n/a | } else { |
---|
2796 | n/a | /* plain name; use key as tag */ |
---|
2797 | n/a | Py_INCREF(key); |
---|
2798 | n/a | tag = key; |
---|
2799 | n/a | } |
---|
2800 | n/a | |
---|
2801 | n/a | /* decode universal name */ |
---|
2802 | n/a | p = PyBytes_AS_STRING(tag); |
---|
2803 | n/a | value = PyUnicode_DecodeUTF8(p, size, "strict"); |
---|
2804 | n/a | Py_DECREF(tag); |
---|
2805 | n/a | if (!value) { |
---|
2806 | n/a | Py_DECREF(key); |
---|
2807 | n/a | return NULL; |
---|
2808 | n/a | } |
---|
2809 | n/a | |
---|
2810 | n/a | /* add to names dictionary */ |
---|
2811 | n/a | if (PyDict_SetItem(self->names, key, value) < 0) { |
---|
2812 | n/a | Py_DECREF(key); |
---|
2813 | n/a | Py_DECREF(value); |
---|
2814 | n/a | return NULL; |
---|
2815 | n/a | } |
---|
2816 | n/a | } |
---|
2817 | n/a | |
---|
2818 | n/a | Py_DECREF(key); |
---|
2819 | n/a | return value; |
---|
2820 | n/a | } |
---|
2821 | n/a | |
---|
2822 | n/a | /* Set the ParseError exception with the given parameters. |
---|
2823 | n/a | * If message is not NULL, it's used as the error string. Otherwise, the |
---|
2824 | n/a | * message string is the default for the given error_code. |
---|
2825 | n/a | */ |
---|
2826 | n/a | static void |
---|
2827 | n/a | expat_set_error(enum XML_Error error_code, Py_ssize_t line, Py_ssize_t column, |
---|
2828 | n/a | const char *message) |
---|
2829 | n/a | { |
---|
2830 | n/a | PyObject *errmsg, *error, *position, *code; |
---|
2831 | n/a | elementtreestate *st = ET_STATE_GLOBAL; |
---|
2832 | n/a | |
---|
2833 | n/a | errmsg = PyUnicode_FromFormat("%s: line %zd, column %zd", |
---|
2834 | n/a | message ? message : EXPAT(ErrorString)(error_code), |
---|
2835 | n/a | line, column); |
---|
2836 | n/a | if (errmsg == NULL) |
---|
2837 | n/a | return; |
---|
2838 | n/a | |
---|
2839 | n/a | error = PyObject_CallFunctionObjArgs(st->parseerror_obj, errmsg, NULL); |
---|
2840 | n/a | Py_DECREF(errmsg); |
---|
2841 | n/a | if (!error) |
---|
2842 | n/a | return; |
---|
2843 | n/a | |
---|
2844 | n/a | /* Add code and position attributes */ |
---|
2845 | n/a | code = PyLong_FromLong((long)error_code); |
---|
2846 | n/a | if (!code) { |
---|
2847 | n/a | Py_DECREF(error); |
---|
2848 | n/a | return; |
---|
2849 | n/a | } |
---|
2850 | n/a | if (PyObject_SetAttrString(error, "code", code) == -1) { |
---|
2851 | n/a | Py_DECREF(error); |
---|
2852 | n/a | Py_DECREF(code); |
---|
2853 | n/a | return; |
---|
2854 | n/a | } |
---|
2855 | n/a | Py_DECREF(code); |
---|
2856 | n/a | |
---|
2857 | n/a | position = Py_BuildValue("(nn)", line, column); |
---|
2858 | n/a | if (!position) { |
---|
2859 | n/a | Py_DECREF(error); |
---|
2860 | n/a | return; |
---|
2861 | n/a | } |
---|
2862 | n/a | if (PyObject_SetAttrString(error, "position", position) == -1) { |
---|
2863 | n/a | Py_DECREF(error); |
---|
2864 | n/a | Py_DECREF(position); |
---|
2865 | n/a | return; |
---|
2866 | n/a | } |
---|
2867 | n/a | Py_DECREF(position); |
---|
2868 | n/a | |
---|
2869 | n/a | PyErr_SetObject(st->parseerror_obj, error); |
---|
2870 | n/a | Py_DECREF(error); |
---|
2871 | n/a | } |
---|
2872 | n/a | |
---|
2873 | n/a | /* -------------------------------------------------------------------- */ |
---|
2874 | n/a | /* handlers */ |
---|
2875 | n/a | |
---|
2876 | n/a | static void |
---|
2877 | n/a | expat_default_handler(XMLParserObject* self, const XML_Char* data_in, |
---|
2878 | n/a | int data_len) |
---|
2879 | n/a | { |
---|
2880 | n/a | PyObject* key; |
---|
2881 | n/a | PyObject* value; |
---|
2882 | n/a | PyObject* res; |
---|
2883 | n/a | |
---|
2884 | n/a | if (data_len < 2 || data_in[0] != '&') |
---|
2885 | n/a | return; |
---|
2886 | n/a | |
---|
2887 | n/a | if (PyErr_Occurred()) |
---|
2888 | n/a | return; |
---|
2889 | n/a | |
---|
2890 | n/a | key = PyUnicode_DecodeUTF8(data_in + 1, data_len - 2, "strict"); |
---|
2891 | n/a | if (!key) |
---|
2892 | n/a | return; |
---|
2893 | n/a | |
---|
2894 | n/a | value = PyDict_GetItem(self->entity, key); |
---|
2895 | n/a | |
---|
2896 | n/a | if (value) { |
---|
2897 | n/a | if (TreeBuilder_CheckExact(self->target)) |
---|
2898 | n/a | res = treebuilder_handle_data( |
---|
2899 | n/a | (TreeBuilderObject*) self->target, value |
---|
2900 | n/a | ); |
---|
2901 | n/a | else if (self->handle_data) |
---|
2902 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_data, value, NULL); |
---|
2903 | n/a | else |
---|
2904 | n/a | res = NULL; |
---|
2905 | n/a | Py_XDECREF(res); |
---|
2906 | n/a | } else if (!PyErr_Occurred()) { |
---|
2907 | n/a | /* Report the first error, not the last */ |
---|
2908 | n/a | char message[128] = "undefined entity "; |
---|
2909 | n/a | strncat(message, data_in, data_len < 100?data_len:100); |
---|
2910 | n/a | expat_set_error( |
---|
2911 | n/a | XML_ERROR_UNDEFINED_ENTITY, |
---|
2912 | n/a | EXPAT(GetErrorLineNumber)(self->parser), |
---|
2913 | n/a | EXPAT(GetErrorColumnNumber)(self->parser), |
---|
2914 | n/a | message |
---|
2915 | n/a | ); |
---|
2916 | n/a | } |
---|
2917 | n/a | |
---|
2918 | n/a | Py_DECREF(key); |
---|
2919 | n/a | } |
---|
2920 | n/a | |
---|
2921 | n/a | static void |
---|
2922 | n/a | expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, |
---|
2923 | n/a | const XML_Char **attrib_in) |
---|
2924 | n/a | { |
---|
2925 | n/a | PyObject* res; |
---|
2926 | n/a | PyObject* tag; |
---|
2927 | n/a | PyObject* attrib; |
---|
2928 | n/a | int ok; |
---|
2929 | n/a | |
---|
2930 | n/a | if (PyErr_Occurred()) |
---|
2931 | n/a | return; |
---|
2932 | n/a | |
---|
2933 | n/a | /* tag name */ |
---|
2934 | n/a | tag = makeuniversal(self, tag_in); |
---|
2935 | n/a | if (!tag) |
---|
2936 | n/a | return; /* parser will look for errors */ |
---|
2937 | n/a | |
---|
2938 | n/a | /* attributes */ |
---|
2939 | n/a | if (attrib_in[0]) { |
---|
2940 | n/a | attrib = PyDict_New(); |
---|
2941 | n/a | if (!attrib) { |
---|
2942 | n/a | Py_DECREF(tag); |
---|
2943 | n/a | return; |
---|
2944 | n/a | } |
---|
2945 | n/a | while (attrib_in[0] && attrib_in[1]) { |
---|
2946 | n/a | PyObject* key = makeuniversal(self, attrib_in[0]); |
---|
2947 | n/a | PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); |
---|
2948 | n/a | if (!key || !value) { |
---|
2949 | n/a | Py_XDECREF(value); |
---|
2950 | n/a | Py_XDECREF(key); |
---|
2951 | n/a | Py_DECREF(attrib); |
---|
2952 | n/a | Py_DECREF(tag); |
---|
2953 | n/a | return; |
---|
2954 | n/a | } |
---|
2955 | n/a | ok = PyDict_SetItem(attrib, key, value); |
---|
2956 | n/a | Py_DECREF(value); |
---|
2957 | n/a | Py_DECREF(key); |
---|
2958 | n/a | if (ok < 0) { |
---|
2959 | n/a | Py_DECREF(attrib); |
---|
2960 | n/a | Py_DECREF(tag); |
---|
2961 | n/a | return; |
---|
2962 | n/a | } |
---|
2963 | n/a | attrib_in += 2; |
---|
2964 | n/a | } |
---|
2965 | n/a | } else { |
---|
2966 | n/a | Py_INCREF(Py_None); |
---|
2967 | n/a | attrib = Py_None; |
---|
2968 | n/a | } |
---|
2969 | n/a | |
---|
2970 | n/a | if (TreeBuilder_CheckExact(self->target)) { |
---|
2971 | n/a | /* shortcut */ |
---|
2972 | n/a | res = treebuilder_handle_start((TreeBuilderObject*) self->target, |
---|
2973 | n/a | tag, attrib); |
---|
2974 | n/a | } |
---|
2975 | n/a | else if (self->handle_start) { |
---|
2976 | n/a | if (attrib == Py_None) { |
---|
2977 | n/a | Py_DECREF(attrib); |
---|
2978 | n/a | attrib = PyDict_New(); |
---|
2979 | n/a | if (!attrib) { |
---|
2980 | n/a | Py_DECREF(tag); |
---|
2981 | n/a | return; |
---|
2982 | n/a | } |
---|
2983 | n/a | } |
---|
2984 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_start, |
---|
2985 | n/a | tag, attrib, NULL); |
---|
2986 | n/a | } else |
---|
2987 | n/a | res = NULL; |
---|
2988 | n/a | |
---|
2989 | n/a | Py_DECREF(tag); |
---|
2990 | n/a | Py_DECREF(attrib); |
---|
2991 | n/a | |
---|
2992 | n/a | Py_XDECREF(res); |
---|
2993 | n/a | } |
---|
2994 | n/a | |
---|
2995 | n/a | static void |
---|
2996 | n/a | expat_data_handler(XMLParserObject* self, const XML_Char* data_in, |
---|
2997 | n/a | int data_len) |
---|
2998 | n/a | { |
---|
2999 | n/a | PyObject* data; |
---|
3000 | n/a | PyObject* res; |
---|
3001 | n/a | |
---|
3002 | n/a | if (PyErr_Occurred()) |
---|
3003 | n/a | return; |
---|
3004 | n/a | |
---|
3005 | n/a | data = PyUnicode_DecodeUTF8(data_in, data_len, "strict"); |
---|
3006 | n/a | if (!data) |
---|
3007 | n/a | return; /* parser will look for errors */ |
---|
3008 | n/a | |
---|
3009 | n/a | if (TreeBuilder_CheckExact(self->target)) |
---|
3010 | n/a | /* shortcut */ |
---|
3011 | n/a | res = treebuilder_handle_data((TreeBuilderObject*) self->target, data); |
---|
3012 | n/a | else if (self->handle_data) |
---|
3013 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_data, data, NULL); |
---|
3014 | n/a | else |
---|
3015 | n/a | res = NULL; |
---|
3016 | n/a | |
---|
3017 | n/a | Py_DECREF(data); |
---|
3018 | n/a | |
---|
3019 | n/a | Py_XDECREF(res); |
---|
3020 | n/a | } |
---|
3021 | n/a | |
---|
3022 | n/a | static void |
---|
3023 | n/a | expat_end_handler(XMLParserObject* self, const XML_Char* tag_in) |
---|
3024 | n/a | { |
---|
3025 | n/a | PyObject* tag; |
---|
3026 | n/a | PyObject* res = NULL; |
---|
3027 | n/a | |
---|
3028 | n/a | if (PyErr_Occurred()) |
---|
3029 | n/a | return; |
---|
3030 | n/a | |
---|
3031 | n/a | if (TreeBuilder_CheckExact(self->target)) |
---|
3032 | n/a | /* shortcut */ |
---|
3033 | n/a | /* the standard tree builder doesn't look at the end tag */ |
---|
3034 | n/a | res = treebuilder_handle_end( |
---|
3035 | n/a | (TreeBuilderObject*) self->target, Py_None |
---|
3036 | n/a | ); |
---|
3037 | n/a | else if (self->handle_end) { |
---|
3038 | n/a | tag = makeuniversal(self, tag_in); |
---|
3039 | n/a | if (tag) { |
---|
3040 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_end, tag, NULL); |
---|
3041 | n/a | Py_DECREF(tag); |
---|
3042 | n/a | } |
---|
3043 | n/a | } |
---|
3044 | n/a | |
---|
3045 | n/a | Py_XDECREF(res); |
---|
3046 | n/a | } |
---|
3047 | n/a | |
---|
3048 | n/a | static void |
---|
3049 | n/a | expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix, |
---|
3050 | n/a | const XML_Char *uri) |
---|
3051 | n/a | { |
---|
3052 | n/a | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
---|
3053 | n/a | PyObject *parcel; |
---|
3054 | n/a | |
---|
3055 | n/a | if (PyErr_Occurred()) |
---|
3056 | n/a | return; |
---|
3057 | n/a | |
---|
3058 | n/a | if (!target->events_append || !target->start_ns_event_obj) |
---|
3059 | n/a | return; |
---|
3060 | n/a | |
---|
3061 | n/a | if (!uri) |
---|
3062 | n/a | uri = ""; |
---|
3063 | n/a | if (!prefix) |
---|
3064 | n/a | prefix = ""; |
---|
3065 | n/a | |
---|
3066 | n/a | parcel = Py_BuildValue("ss", prefix, uri); |
---|
3067 | n/a | if (!parcel) |
---|
3068 | n/a | return; |
---|
3069 | n/a | treebuilder_append_event(target, target->start_ns_event_obj, parcel); |
---|
3070 | n/a | Py_DECREF(parcel); |
---|
3071 | n/a | } |
---|
3072 | n/a | |
---|
3073 | n/a | static void |
---|
3074 | n/a | expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in) |
---|
3075 | n/a | { |
---|
3076 | n/a | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
---|
3077 | n/a | |
---|
3078 | n/a | if (PyErr_Occurred()) |
---|
3079 | n/a | return; |
---|
3080 | n/a | |
---|
3081 | n/a | if (!target->events_append) |
---|
3082 | n/a | return; |
---|
3083 | n/a | |
---|
3084 | n/a | treebuilder_append_event(target, target->end_ns_event_obj, Py_None); |
---|
3085 | n/a | } |
---|
3086 | n/a | |
---|
3087 | n/a | static void |
---|
3088 | n/a | expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in) |
---|
3089 | n/a | { |
---|
3090 | n/a | PyObject* comment; |
---|
3091 | n/a | PyObject* res; |
---|
3092 | n/a | |
---|
3093 | n/a | if (PyErr_Occurred()) |
---|
3094 | n/a | return; |
---|
3095 | n/a | |
---|
3096 | n/a | if (self->handle_comment) { |
---|
3097 | n/a | comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict"); |
---|
3098 | n/a | if (comment) { |
---|
3099 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_comment, |
---|
3100 | n/a | comment, NULL); |
---|
3101 | n/a | Py_XDECREF(res); |
---|
3102 | n/a | Py_DECREF(comment); |
---|
3103 | n/a | } |
---|
3104 | n/a | } |
---|
3105 | n/a | } |
---|
3106 | n/a | |
---|
3107 | n/a | static void |
---|
3108 | n/a | expat_start_doctype_handler(XMLParserObject *self, |
---|
3109 | n/a | const XML_Char *doctype_name, |
---|
3110 | n/a | const XML_Char *sysid, |
---|
3111 | n/a | const XML_Char *pubid, |
---|
3112 | n/a | int has_internal_subset) |
---|
3113 | n/a | { |
---|
3114 | n/a | PyObject *self_pyobj = (PyObject *)self; |
---|
3115 | n/a | PyObject *doctype_name_obj, *sysid_obj, *pubid_obj; |
---|
3116 | n/a | PyObject *parser_doctype = NULL; |
---|
3117 | n/a | PyObject *res = NULL; |
---|
3118 | n/a | |
---|
3119 | n/a | if (PyErr_Occurred()) |
---|
3120 | n/a | return; |
---|
3121 | n/a | |
---|
3122 | n/a | doctype_name_obj = makeuniversal(self, doctype_name); |
---|
3123 | n/a | if (!doctype_name_obj) |
---|
3124 | n/a | return; |
---|
3125 | n/a | |
---|
3126 | n/a | if (sysid) { |
---|
3127 | n/a | sysid_obj = makeuniversal(self, sysid); |
---|
3128 | n/a | if (!sysid_obj) { |
---|
3129 | n/a | Py_DECREF(doctype_name_obj); |
---|
3130 | n/a | return; |
---|
3131 | n/a | } |
---|
3132 | n/a | } else { |
---|
3133 | n/a | Py_INCREF(Py_None); |
---|
3134 | n/a | sysid_obj = Py_None; |
---|
3135 | n/a | } |
---|
3136 | n/a | |
---|
3137 | n/a | if (pubid) { |
---|
3138 | n/a | pubid_obj = makeuniversal(self, pubid); |
---|
3139 | n/a | if (!pubid_obj) { |
---|
3140 | n/a | Py_DECREF(doctype_name_obj); |
---|
3141 | n/a | Py_DECREF(sysid_obj); |
---|
3142 | n/a | return; |
---|
3143 | n/a | } |
---|
3144 | n/a | } else { |
---|
3145 | n/a | Py_INCREF(Py_None); |
---|
3146 | n/a | pubid_obj = Py_None; |
---|
3147 | n/a | } |
---|
3148 | n/a | |
---|
3149 | n/a | /* If the target has a handler for doctype, call it. */ |
---|
3150 | n/a | if (self->handle_doctype) { |
---|
3151 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_doctype, |
---|
3152 | n/a | doctype_name_obj, pubid_obj, |
---|
3153 | n/a | sysid_obj, NULL); |
---|
3154 | n/a | Py_CLEAR(res); |
---|
3155 | n/a | } |
---|
3156 | n/a | else { |
---|
3157 | n/a | /* Now see if the parser itself has a doctype method. If yes and it's |
---|
3158 | n/a | * a custom method, call it but warn about deprecation. If it's only |
---|
3159 | n/a | * the vanilla XMLParser method, do nothing. |
---|
3160 | n/a | */ |
---|
3161 | n/a | parser_doctype = PyObject_GetAttrString(self_pyobj, "doctype"); |
---|
3162 | n/a | if (parser_doctype && |
---|
3163 | n/a | !(PyCFunction_Check(parser_doctype) && |
---|
3164 | n/a | PyCFunction_GET_SELF(parser_doctype) == self_pyobj && |
---|
3165 | n/a | PyCFunction_GET_FUNCTION(parser_doctype) == |
---|
3166 | n/a | (PyCFunction) _elementtree_XMLParser_doctype)) { |
---|
3167 | n/a | res = _elementtree_XMLParser_doctype_impl(self, doctype_name_obj, |
---|
3168 | n/a | pubid_obj, sysid_obj); |
---|
3169 | n/a | if (!res) |
---|
3170 | n/a | goto clear; |
---|
3171 | n/a | Py_DECREF(res); |
---|
3172 | n/a | res = PyObject_CallFunctionObjArgs(parser_doctype, |
---|
3173 | n/a | doctype_name_obj, pubid_obj, |
---|
3174 | n/a | sysid_obj, NULL); |
---|
3175 | n/a | Py_CLEAR(res); |
---|
3176 | n/a | } |
---|
3177 | n/a | } |
---|
3178 | n/a | |
---|
3179 | n/a | clear: |
---|
3180 | n/a | Py_XDECREF(parser_doctype); |
---|
3181 | n/a | Py_DECREF(doctype_name_obj); |
---|
3182 | n/a | Py_DECREF(pubid_obj); |
---|
3183 | n/a | Py_DECREF(sysid_obj); |
---|
3184 | n/a | } |
---|
3185 | n/a | |
---|
3186 | n/a | static void |
---|
3187 | n/a | expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, |
---|
3188 | n/a | const XML_Char* data_in) |
---|
3189 | n/a | { |
---|
3190 | n/a | PyObject* target; |
---|
3191 | n/a | PyObject* data; |
---|
3192 | n/a | PyObject* res; |
---|
3193 | n/a | |
---|
3194 | n/a | if (PyErr_Occurred()) |
---|
3195 | n/a | return; |
---|
3196 | n/a | |
---|
3197 | n/a | if (self->handle_pi) { |
---|
3198 | n/a | target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict"); |
---|
3199 | n/a | data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict"); |
---|
3200 | n/a | if (target && data) { |
---|
3201 | n/a | res = PyObject_CallFunctionObjArgs(self->handle_pi, |
---|
3202 | n/a | target, data, NULL); |
---|
3203 | n/a | Py_XDECREF(res); |
---|
3204 | n/a | Py_DECREF(data); |
---|
3205 | n/a | Py_DECREF(target); |
---|
3206 | n/a | } else { |
---|
3207 | n/a | Py_XDECREF(data); |
---|
3208 | n/a | Py_XDECREF(target); |
---|
3209 | n/a | } |
---|
3210 | n/a | } |
---|
3211 | n/a | } |
---|
3212 | n/a | |
---|
3213 | n/a | /* -------------------------------------------------------------------- */ |
---|
3214 | n/a | |
---|
3215 | n/a | static PyObject * |
---|
3216 | n/a | xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
3217 | n/a | { |
---|
3218 | n/a | XMLParserObject *self = (XMLParserObject *)type->tp_alloc(type, 0); |
---|
3219 | n/a | if (self) { |
---|
3220 | n/a | self->parser = NULL; |
---|
3221 | n/a | self->target = self->entity = self->names = NULL; |
---|
3222 | n/a | self->handle_start = self->handle_data = self->handle_end = NULL; |
---|
3223 | n/a | self->handle_comment = self->handle_pi = self->handle_close = NULL; |
---|
3224 | n/a | self->handle_doctype = NULL; |
---|
3225 | n/a | } |
---|
3226 | n/a | return (PyObject *)self; |
---|
3227 | n/a | } |
---|
3228 | n/a | |
---|
3229 | n/a | /*[clinic input] |
---|
3230 | n/a | _elementtree.XMLParser.__init__ |
---|
3231 | n/a | |
---|
3232 | n/a | html: object = NULL |
---|
3233 | n/a | target: object = NULL |
---|
3234 | n/a | encoding: str(accept={str, NoneType}) = NULL |
---|
3235 | n/a | |
---|
3236 | n/a | [clinic start generated code]*/ |
---|
3237 | n/a | |
---|
3238 | n/a | static int |
---|
3239 | n/a | _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html, |
---|
3240 | n/a | PyObject *target, const char *encoding) |
---|
3241 | n/a | /*[clinic end generated code: output=d6a16c63dda54441 input=155bc5695baafffd]*/ |
---|
3242 | n/a | { |
---|
3243 | n/a | self->entity = PyDict_New(); |
---|
3244 | n/a | if (!self->entity) |
---|
3245 | n/a | return -1; |
---|
3246 | n/a | |
---|
3247 | n/a | self->names = PyDict_New(); |
---|
3248 | n/a | if (!self->names) { |
---|
3249 | n/a | Py_CLEAR(self->entity); |
---|
3250 | n/a | return -1; |
---|
3251 | n/a | } |
---|
3252 | n/a | |
---|
3253 | n/a | self->parser = EXPAT(ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}"); |
---|
3254 | n/a | if (!self->parser) { |
---|
3255 | n/a | Py_CLEAR(self->entity); |
---|
3256 | n/a | Py_CLEAR(self->names); |
---|
3257 | n/a | PyErr_NoMemory(); |
---|
3258 | n/a | return -1; |
---|
3259 | n/a | } |
---|
3260 | n/a | |
---|
3261 | n/a | if (target) { |
---|
3262 | n/a | Py_INCREF(target); |
---|
3263 | n/a | } else { |
---|
3264 | n/a | target = treebuilder_new(&TreeBuilder_Type, NULL, NULL); |
---|
3265 | n/a | if (!target) { |
---|
3266 | n/a | Py_CLEAR(self->entity); |
---|
3267 | n/a | Py_CLEAR(self->names); |
---|
3268 | n/a | EXPAT(ParserFree)(self->parser); |
---|
3269 | n/a | return -1; |
---|
3270 | n/a | } |
---|
3271 | n/a | } |
---|
3272 | n/a | self->target = target; |
---|
3273 | n/a | |
---|
3274 | n/a | self->handle_start = PyObject_GetAttrString(target, "start"); |
---|
3275 | n/a | self->handle_data = PyObject_GetAttrString(target, "data"); |
---|
3276 | n/a | self->handle_end = PyObject_GetAttrString(target, "end"); |
---|
3277 | n/a | self->handle_comment = PyObject_GetAttrString(target, "comment"); |
---|
3278 | n/a | self->handle_pi = PyObject_GetAttrString(target, "pi"); |
---|
3279 | n/a | self->handle_close = PyObject_GetAttrString(target, "close"); |
---|
3280 | n/a | self->handle_doctype = PyObject_GetAttrString(target, "doctype"); |
---|
3281 | n/a | |
---|
3282 | n/a | PyErr_Clear(); |
---|
3283 | n/a | |
---|
3284 | n/a | /* configure parser */ |
---|
3285 | n/a | EXPAT(SetUserData)(self->parser, self); |
---|
3286 | n/a | EXPAT(SetElementHandler)( |
---|
3287 | n/a | self->parser, |
---|
3288 | n/a | (XML_StartElementHandler) expat_start_handler, |
---|
3289 | n/a | (XML_EndElementHandler) expat_end_handler |
---|
3290 | n/a | ); |
---|
3291 | n/a | EXPAT(SetDefaultHandlerExpand)( |
---|
3292 | n/a | self->parser, |
---|
3293 | n/a | (XML_DefaultHandler) expat_default_handler |
---|
3294 | n/a | ); |
---|
3295 | n/a | EXPAT(SetCharacterDataHandler)( |
---|
3296 | n/a | self->parser, |
---|
3297 | n/a | (XML_CharacterDataHandler) expat_data_handler |
---|
3298 | n/a | ); |
---|
3299 | n/a | if (self->handle_comment) |
---|
3300 | n/a | EXPAT(SetCommentHandler)( |
---|
3301 | n/a | self->parser, |
---|
3302 | n/a | (XML_CommentHandler) expat_comment_handler |
---|
3303 | n/a | ); |
---|
3304 | n/a | if (self->handle_pi) |
---|
3305 | n/a | EXPAT(SetProcessingInstructionHandler)( |
---|
3306 | n/a | self->parser, |
---|
3307 | n/a | (XML_ProcessingInstructionHandler) expat_pi_handler |
---|
3308 | n/a | ); |
---|
3309 | n/a | EXPAT(SetStartDoctypeDeclHandler)( |
---|
3310 | n/a | self->parser, |
---|
3311 | n/a | (XML_StartDoctypeDeclHandler) expat_start_doctype_handler |
---|
3312 | n/a | ); |
---|
3313 | n/a | EXPAT(SetUnknownEncodingHandler)( |
---|
3314 | n/a | self->parser, |
---|
3315 | n/a | EXPAT(DefaultUnknownEncodingHandler), NULL |
---|
3316 | n/a | ); |
---|
3317 | n/a | |
---|
3318 | n/a | return 0; |
---|
3319 | n/a | } |
---|
3320 | n/a | |
---|
3321 | n/a | static int |
---|
3322 | n/a | xmlparser_gc_traverse(XMLParserObject *self, visitproc visit, void *arg) |
---|
3323 | n/a | { |
---|
3324 | n/a | Py_VISIT(self->handle_close); |
---|
3325 | n/a | Py_VISIT(self->handle_pi); |
---|
3326 | n/a | Py_VISIT(self->handle_comment); |
---|
3327 | n/a | Py_VISIT(self->handle_end); |
---|
3328 | n/a | Py_VISIT(self->handle_data); |
---|
3329 | n/a | Py_VISIT(self->handle_start); |
---|
3330 | n/a | |
---|
3331 | n/a | Py_VISIT(self->target); |
---|
3332 | n/a | Py_VISIT(self->entity); |
---|
3333 | n/a | Py_VISIT(self->names); |
---|
3334 | n/a | |
---|
3335 | n/a | return 0; |
---|
3336 | n/a | } |
---|
3337 | n/a | |
---|
3338 | n/a | static int |
---|
3339 | n/a | xmlparser_gc_clear(XMLParserObject *self) |
---|
3340 | n/a | { |
---|
3341 | n/a | EXPAT(ParserFree)(self->parser); |
---|
3342 | n/a | |
---|
3343 | n/a | Py_CLEAR(self->handle_close); |
---|
3344 | n/a | Py_CLEAR(self->handle_pi); |
---|
3345 | n/a | Py_CLEAR(self->handle_comment); |
---|
3346 | n/a | Py_CLEAR(self->handle_end); |
---|
3347 | n/a | Py_CLEAR(self->handle_data); |
---|
3348 | n/a | Py_CLEAR(self->handle_start); |
---|
3349 | n/a | Py_CLEAR(self->handle_doctype); |
---|
3350 | n/a | |
---|
3351 | n/a | Py_CLEAR(self->target); |
---|
3352 | n/a | Py_CLEAR(self->entity); |
---|
3353 | n/a | Py_CLEAR(self->names); |
---|
3354 | n/a | |
---|
3355 | n/a | return 0; |
---|
3356 | n/a | } |
---|
3357 | n/a | |
---|
3358 | n/a | static void |
---|
3359 | n/a | xmlparser_dealloc(XMLParserObject* self) |
---|
3360 | n/a | { |
---|
3361 | n/a | PyObject_GC_UnTrack(self); |
---|
3362 | n/a | xmlparser_gc_clear(self); |
---|
3363 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
---|
3364 | n/a | } |
---|
3365 | n/a | |
---|
3366 | n/a | LOCAL(PyObject*) |
---|
3367 | n/a | expat_parse(XMLParserObject* self, const char* data, int data_len, int final) |
---|
3368 | n/a | { |
---|
3369 | n/a | int ok; |
---|
3370 | n/a | |
---|
3371 | n/a | assert(!PyErr_Occurred()); |
---|
3372 | n/a | ok = EXPAT(Parse)(self->parser, data, data_len, final); |
---|
3373 | n/a | |
---|
3374 | n/a | if (PyErr_Occurred()) |
---|
3375 | n/a | return NULL; |
---|
3376 | n/a | |
---|
3377 | n/a | if (!ok) { |
---|
3378 | n/a | expat_set_error( |
---|
3379 | n/a | EXPAT(GetErrorCode)(self->parser), |
---|
3380 | n/a | EXPAT(GetErrorLineNumber)(self->parser), |
---|
3381 | n/a | EXPAT(GetErrorColumnNumber)(self->parser), |
---|
3382 | n/a | NULL |
---|
3383 | n/a | ); |
---|
3384 | n/a | return NULL; |
---|
3385 | n/a | } |
---|
3386 | n/a | |
---|
3387 | n/a | Py_RETURN_NONE; |
---|
3388 | n/a | } |
---|
3389 | n/a | |
---|
3390 | n/a | /*[clinic input] |
---|
3391 | n/a | _elementtree.XMLParser.close |
---|
3392 | n/a | |
---|
3393 | n/a | [clinic start generated code]*/ |
---|
3394 | n/a | |
---|
3395 | n/a | static PyObject * |
---|
3396 | n/a | _elementtree_XMLParser_close_impl(XMLParserObject *self) |
---|
3397 | n/a | /*[clinic end generated code: output=d68d375dd23bc7fb input=ca7909ca78c3abfe]*/ |
---|
3398 | n/a | { |
---|
3399 | n/a | /* end feeding data to parser */ |
---|
3400 | n/a | |
---|
3401 | n/a | PyObject* res; |
---|
3402 | n/a | res = expat_parse(self, "", 0, 1); |
---|
3403 | n/a | if (!res) |
---|
3404 | n/a | return NULL; |
---|
3405 | n/a | |
---|
3406 | n/a | if (TreeBuilder_CheckExact(self->target)) { |
---|
3407 | n/a | Py_DECREF(res); |
---|
3408 | n/a | return treebuilder_done((TreeBuilderObject*) self->target); |
---|
3409 | n/a | } |
---|
3410 | n/a | else if (self->handle_close) { |
---|
3411 | n/a | Py_DECREF(res); |
---|
3412 | n/a | return _PyObject_CallNoArg(self->handle_close); |
---|
3413 | n/a | } |
---|
3414 | n/a | else { |
---|
3415 | n/a | return res; |
---|
3416 | n/a | } |
---|
3417 | n/a | } |
---|
3418 | n/a | |
---|
3419 | n/a | /*[clinic input] |
---|
3420 | n/a | _elementtree.XMLParser.feed |
---|
3421 | n/a | |
---|
3422 | n/a | data: object |
---|
3423 | n/a | / |
---|
3424 | n/a | |
---|
3425 | n/a | [clinic start generated code]*/ |
---|
3426 | n/a | |
---|
3427 | n/a | static PyObject * |
---|
3428 | n/a | _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data) |
---|
3429 | n/a | /*[clinic end generated code: output=e42b6a78eec7446d input=fe231b6b8de3ce1f]*/ |
---|
3430 | n/a | { |
---|
3431 | n/a | /* feed data to parser */ |
---|
3432 | n/a | |
---|
3433 | n/a | if (PyUnicode_Check(data)) { |
---|
3434 | n/a | Py_ssize_t data_len; |
---|
3435 | n/a | const char *data_ptr = PyUnicode_AsUTF8AndSize(data, &data_len); |
---|
3436 | n/a | if (data_ptr == NULL) |
---|
3437 | n/a | return NULL; |
---|
3438 | n/a | if (data_len > INT_MAX) { |
---|
3439 | n/a | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
---|
3440 | n/a | return NULL; |
---|
3441 | n/a | } |
---|
3442 | n/a | /* Explicitly set UTF-8 encoding. Return code ignored. */ |
---|
3443 | n/a | (void)EXPAT(SetEncoding)(self->parser, "utf-8"); |
---|
3444 | n/a | return expat_parse(self, data_ptr, (int)data_len, 0); |
---|
3445 | n/a | } |
---|
3446 | n/a | else { |
---|
3447 | n/a | Py_buffer view; |
---|
3448 | n/a | PyObject *res; |
---|
3449 | n/a | if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0) |
---|
3450 | n/a | return NULL; |
---|
3451 | n/a | if (view.len > INT_MAX) { |
---|
3452 | n/a | PyBuffer_Release(&view); |
---|
3453 | n/a | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
---|
3454 | n/a | return NULL; |
---|
3455 | n/a | } |
---|
3456 | n/a | res = expat_parse(self, view.buf, (int)view.len, 0); |
---|
3457 | n/a | PyBuffer_Release(&view); |
---|
3458 | n/a | return res; |
---|
3459 | n/a | } |
---|
3460 | n/a | } |
---|
3461 | n/a | |
---|
3462 | n/a | /*[clinic input] |
---|
3463 | n/a | _elementtree.XMLParser._parse_whole |
---|
3464 | n/a | |
---|
3465 | n/a | file: object |
---|
3466 | n/a | / |
---|
3467 | n/a | |
---|
3468 | n/a | [clinic start generated code]*/ |
---|
3469 | n/a | |
---|
3470 | n/a | static PyObject * |
---|
3471 | n/a | _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file) |
---|
3472 | n/a | /*[clinic end generated code: output=f797197bb818dda3 input=19ecc893b6f3e752]*/ |
---|
3473 | n/a | { |
---|
3474 | n/a | /* (internal) parse the whole input, until end of stream */ |
---|
3475 | n/a | PyObject* reader; |
---|
3476 | n/a | PyObject* buffer; |
---|
3477 | n/a | PyObject* temp; |
---|
3478 | n/a | PyObject* res; |
---|
3479 | n/a | |
---|
3480 | n/a | reader = PyObject_GetAttrString(file, "read"); |
---|
3481 | n/a | if (!reader) |
---|
3482 | n/a | return NULL; |
---|
3483 | n/a | |
---|
3484 | n/a | /* read from open file object */ |
---|
3485 | n/a | for (;;) { |
---|
3486 | n/a | |
---|
3487 | n/a | buffer = PyObject_CallFunction(reader, "i", 64*1024); |
---|
3488 | n/a | |
---|
3489 | n/a | if (!buffer) { |
---|
3490 | n/a | /* read failed (e.g. due to KeyboardInterrupt) */ |
---|
3491 | n/a | Py_DECREF(reader); |
---|
3492 | n/a | return NULL; |
---|
3493 | n/a | } |
---|
3494 | n/a | |
---|
3495 | n/a | if (PyUnicode_CheckExact(buffer)) { |
---|
3496 | n/a | /* A unicode object is encoded into bytes using UTF-8 */ |
---|
3497 | n/a | if (PyUnicode_GET_LENGTH(buffer) == 0) { |
---|
3498 | n/a | Py_DECREF(buffer); |
---|
3499 | n/a | break; |
---|
3500 | n/a | } |
---|
3501 | n/a | temp = PyUnicode_AsEncodedString(buffer, "utf-8", "surrogatepass"); |
---|
3502 | n/a | Py_DECREF(buffer); |
---|
3503 | n/a | if (!temp) { |
---|
3504 | n/a | /* Propagate exception from PyUnicode_AsEncodedString */ |
---|
3505 | n/a | Py_DECREF(reader); |
---|
3506 | n/a | return NULL; |
---|
3507 | n/a | } |
---|
3508 | n/a | buffer = temp; |
---|
3509 | n/a | } |
---|
3510 | n/a | else if (!PyBytes_CheckExact(buffer) || PyBytes_GET_SIZE(buffer) == 0) { |
---|
3511 | n/a | Py_DECREF(buffer); |
---|
3512 | n/a | break; |
---|
3513 | n/a | } |
---|
3514 | n/a | |
---|
3515 | n/a | if (PyBytes_GET_SIZE(buffer) > INT_MAX) { |
---|
3516 | n/a | Py_DECREF(buffer); |
---|
3517 | n/a | Py_DECREF(reader); |
---|
3518 | n/a | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
---|
3519 | n/a | return NULL; |
---|
3520 | n/a | } |
---|
3521 | n/a | res = expat_parse( |
---|
3522 | n/a | self, PyBytes_AS_STRING(buffer), (int)PyBytes_GET_SIZE(buffer), 0 |
---|
3523 | n/a | ); |
---|
3524 | n/a | |
---|
3525 | n/a | Py_DECREF(buffer); |
---|
3526 | n/a | |
---|
3527 | n/a | if (!res) { |
---|
3528 | n/a | Py_DECREF(reader); |
---|
3529 | n/a | return NULL; |
---|
3530 | n/a | } |
---|
3531 | n/a | Py_DECREF(res); |
---|
3532 | n/a | |
---|
3533 | n/a | } |
---|
3534 | n/a | |
---|
3535 | n/a | Py_DECREF(reader); |
---|
3536 | n/a | |
---|
3537 | n/a | res = expat_parse(self, "", 0, 1); |
---|
3538 | n/a | |
---|
3539 | n/a | if (res && TreeBuilder_CheckExact(self->target)) { |
---|
3540 | n/a | Py_DECREF(res); |
---|
3541 | n/a | return treebuilder_done((TreeBuilderObject*) self->target); |
---|
3542 | n/a | } |
---|
3543 | n/a | |
---|
3544 | n/a | return res; |
---|
3545 | n/a | } |
---|
3546 | n/a | |
---|
3547 | n/a | /*[clinic input] |
---|
3548 | n/a | _elementtree.XMLParser.doctype |
---|
3549 | n/a | |
---|
3550 | n/a | name: object |
---|
3551 | n/a | pubid: object |
---|
3552 | n/a | system: object |
---|
3553 | n/a | / |
---|
3554 | n/a | |
---|
3555 | n/a | [clinic start generated code]*/ |
---|
3556 | n/a | |
---|
3557 | n/a | static PyObject * |
---|
3558 | n/a | _elementtree_XMLParser_doctype_impl(XMLParserObject *self, PyObject *name, |
---|
3559 | n/a | PyObject *pubid, PyObject *system) |
---|
3560 | n/a | /*[clinic end generated code: output=10fb50c2afded88d input=84050276cca045e1]*/ |
---|
3561 | n/a | { |
---|
3562 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
---|
3563 | n/a | "This method of XMLParser is deprecated. Define" |
---|
3564 | n/a | " doctype() method on the TreeBuilder target.", |
---|
3565 | n/a | 1) < 0) { |
---|
3566 | n/a | return NULL; |
---|
3567 | n/a | } |
---|
3568 | n/a | Py_RETURN_NONE; |
---|
3569 | n/a | } |
---|
3570 | n/a | |
---|
3571 | n/a | /*[clinic input] |
---|
3572 | n/a | _elementtree.XMLParser._setevents |
---|
3573 | n/a | |
---|
3574 | n/a | events_queue: object |
---|
3575 | n/a | events_to_report: object = None |
---|
3576 | n/a | / |
---|
3577 | n/a | |
---|
3578 | n/a | [clinic start generated code]*/ |
---|
3579 | n/a | |
---|
3580 | n/a | static PyObject * |
---|
3581 | n/a | _elementtree_XMLParser__setevents_impl(XMLParserObject *self, |
---|
3582 | n/a | PyObject *events_queue, |
---|
3583 | n/a | PyObject *events_to_report) |
---|
3584 | n/a | /*[clinic end generated code: output=1440092922b13ed1 input=abf90830a1c3b0fc]*/ |
---|
3585 | n/a | { |
---|
3586 | n/a | /* activate element event reporting */ |
---|
3587 | n/a | Py_ssize_t i; |
---|
3588 | n/a | TreeBuilderObject *target; |
---|
3589 | n/a | PyObject *events_append, *events_seq; |
---|
3590 | n/a | |
---|
3591 | n/a | if (!TreeBuilder_CheckExact(self->target)) { |
---|
3592 | n/a | PyErr_SetString( |
---|
3593 | n/a | PyExc_TypeError, |
---|
3594 | n/a | "event handling only supported for ElementTree.TreeBuilder " |
---|
3595 | n/a | "targets" |
---|
3596 | n/a | ); |
---|
3597 | n/a | return NULL; |
---|
3598 | n/a | } |
---|
3599 | n/a | |
---|
3600 | n/a | target = (TreeBuilderObject*) self->target; |
---|
3601 | n/a | |
---|
3602 | n/a | events_append = PyObject_GetAttrString(events_queue, "append"); |
---|
3603 | n/a | if (events_append == NULL) |
---|
3604 | n/a | return NULL; |
---|
3605 | n/a | Py_XSETREF(target->events_append, events_append); |
---|
3606 | n/a | |
---|
3607 | n/a | /* clear out existing events */ |
---|
3608 | n/a | Py_CLEAR(target->start_event_obj); |
---|
3609 | n/a | Py_CLEAR(target->end_event_obj); |
---|
3610 | n/a | Py_CLEAR(target->start_ns_event_obj); |
---|
3611 | n/a | Py_CLEAR(target->end_ns_event_obj); |
---|
3612 | n/a | |
---|
3613 | n/a | if (events_to_report == Py_None) { |
---|
3614 | n/a | /* default is "end" only */ |
---|
3615 | n/a | target->end_event_obj = PyUnicode_FromString("end"); |
---|
3616 | n/a | Py_RETURN_NONE; |
---|
3617 | n/a | } |
---|
3618 | n/a | |
---|
3619 | n/a | if (!(events_seq = PySequence_Fast(events_to_report, |
---|
3620 | n/a | "events must be a sequence"))) { |
---|
3621 | n/a | return NULL; |
---|
3622 | n/a | } |
---|
3623 | n/a | |
---|
3624 | n/a | for (i = 0; i < PySequence_Size(events_seq); ++i) { |
---|
3625 | n/a | PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i); |
---|
3626 | n/a | const char *event_name = NULL; |
---|
3627 | n/a | if (PyUnicode_Check(event_name_obj)) { |
---|
3628 | n/a | event_name = PyUnicode_AsUTF8(event_name_obj); |
---|
3629 | n/a | } else if (PyBytes_Check(event_name_obj)) { |
---|
3630 | n/a | event_name = PyBytes_AS_STRING(event_name_obj); |
---|
3631 | n/a | } |
---|
3632 | n/a | if (event_name == NULL) { |
---|
3633 | n/a | Py_DECREF(events_seq); |
---|
3634 | n/a | PyErr_Format(PyExc_ValueError, "invalid events sequence"); |
---|
3635 | n/a | return NULL; |
---|
3636 | n/a | } |
---|
3637 | n/a | |
---|
3638 | n/a | Py_INCREF(event_name_obj); |
---|
3639 | n/a | if (strcmp(event_name, "start") == 0) { |
---|
3640 | n/a | Py_XSETREF(target->start_event_obj, event_name_obj); |
---|
3641 | n/a | } else if (strcmp(event_name, "end") == 0) { |
---|
3642 | n/a | Py_XSETREF(target->end_event_obj, event_name_obj); |
---|
3643 | n/a | } else if (strcmp(event_name, "start-ns") == 0) { |
---|
3644 | n/a | Py_XSETREF(target->start_ns_event_obj, event_name_obj); |
---|
3645 | n/a | EXPAT(SetNamespaceDeclHandler)( |
---|
3646 | n/a | self->parser, |
---|
3647 | n/a | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
---|
3648 | n/a | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
---|
3649 | n/a | ); |
---|
3650 | n/a | } else if (strcmp(event_name, "end-ns") == 0) { |
---|
3651 | n/a | Py_XSETREF(target->end_ns_event_obj, event_name_obj); |
---|
3652 | n/a | EXPAT(SetNamespaceDeclHandler)( |
---|
3653 | n/a | self->parser, |
---|
3654 | n/a | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
---|
3655 | n/a | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
---|
3656 | n/a | ); |
---|
3657 | n/a | } else { |
---|
3658 | n/a | Py_DECREF(event_name_obj); |
---|
3659 | n/a | Py_DECREF(events_seq); |
---|
3660 | n/a | PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name); |
---|
3661 | n/a | return NULL; |
---|
3662 | n/a | } |
---|
3663 | n/a | } |
---|
3664 | n/a | |
---|
3665 | n/a | Py_DECREF(events_seq); |
---|
3666 | n/a | Py_RETURN_NONE; |
---|
3667 | n/a | } |
---|
3668 | n/a | |
---|
3669 | n/a | static PyObject* |
---|
3670 | n/a | xmlparser_getattro(XMLParserObject* self, PyObject* nameobj) |
---|
3671 | n/a | { |
---|
3672 | n/a | if (PyUnicode_Check(nameobj)) { |
---|
3673 | n/a | PyObject* res; |
---|
3674 | n/a | if (_PyUnicode_EqualToASCIIString(nameobj, "entity")) |
---|
3675 | n/a | res = self->entity; |
---|
3676 | n/a | else if (_PyUnicode_EqualToASCIIString(nameobj, "target")) |
---|
3677 | n/a | res = self->target; |
---|
3678 | n/a | else if (_PyUnicode_EqualToASCIIString(nameobj, "version")) { |
---|
3679 | n/a | return PyUnicode_FromFormat( |
---|
3680 | n/a | "Expat %d.%d.%d", XML_MAJOR_VERSION, |
---|
3681 | n/a | XML_MINOR_VERSION, XML_MICRO_VERSION); |
---|
3682 | n/a | } |
---|
3683 | n/a | else |
---|
3684 | n/a | goto generic; |
---|
3685 | n/a | |
---|
3686 | n/a | Py_INCREF(res); |
---|
3687 | n/a | return res; |
---|
3688 | n/a | } |
---|
3689 | n/a | generic: |
---|
3690 | n/a | return PyObject_GenericGetAttr((PyObject*) self, nameobj); |
---|
3691 | n/a | } |
---|
3692 | n/a | |
---|
3693 | n/a | #include "clinic/_elementtree.c.h" |
---|
3694 | n/a | |
---|
3695 | n/a | static PyMethodDef element_methods[] = { |
---|
3696 | n/a | |
---|
3697 | n/a | _ELEMENTTREE_ELEMENT_CLEAR_METHODDEF |
---|
3698 | n/a | |
---|
3699 | n/a | _ELEMENTTREE_ELEMENT_GET_METHODDEF |
---|
3700 | n/a | _ELEMENTTREE_ELEMENT_SET_METHODDEF |
---|
3701 | n/a | |
---|
3702 | n/a | _ELEMENTTREE_ELEMENT_FIND_METHODDEF |
---|
3703 | n/a | _ELEMENTTREE_ELEMENT_FINDTEXT_METHODDEF |
---|
3704 | n/a | _ELEMENTTREE_ELEMENT_FINDALL_METHODDEF |
---|
3705 | n/a | |
---|
3706 | n/a | _ELEMENTTREE_ELEMENT_APPEND_METHODDEF |
---|
3707 | n/a | _ELEMENTTREE_ELEMENT_EXTEND_METHODDEF |
---|
3708 | n/a | _ELEMENTTREE_ELEMENT_INSERT_METHODDEF |
---|
3709 | n/a | _ELEMENTTREE_ELEMENT_REMOVE_METHODDEF |
---|
3710 | n/a | |
---|
3711 | n/a | _ELEMENTTREE_ELEMENT_ITER_METHODDEF |
---|
3712 | n/a | _ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF |
---|
3713 | n/a | _ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF |
---|
3714 | n/a | |
---|
3715 | n/a | {"getiterator", (PyCFunction)_elementtree_Element_iter, METH_FASTCALL, _elementtree_Element_iter__doc__}, |
---|
3716 | n/a | _ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF |
---|
3717 | n/a | |
---|
3718 | n/a | _ELEMENTTREE_ELEMENT_ITEMS_METHODDEF |
---|
3719 | n/a | _ELEMENTTREE_ELEMENT_KEYS_METHODDEF |
---|
3720 | n/a | |
---|
3721 | n/a | _ELEMENTTREE_ELEMENT_MAKEELEMENT_METHODDEF |
---|
3722 | n/a | |
---|
3723 | n/a | _ELEMENTTREE_ELEMENT___COPY___METHODDEF |
---|
3724 | n/a | _ELEMENTTREE_ELEMENT___DEEPCOPY___METHODDEF |
---|
3725 | n/a | _ELEMENTTREE_ELEMENT___SIZEOF___METHODDEF |
---|
3726 | n/a | _ELEMENTTREE_ELEMENT___GETSTATE___METHODDEF |
---|
3727 | n/a | _ELEMENTTREE_ELEMENT___SETSTATE___METHODDEF |
---|
3728 | n/a | |
---|
3729 | n/a | {NULL, NULL} |
---|
3730 | n/a | }; |
---|
3731 | n/a | |
---|
3732 | n/a | static PyMappingMethods element_as_mapping = { |
---|
3733 | n/a | (lenfunc) element_length, |
---|
3734 | n/a | (binaryfunc) element_subscr, |
---|
3735 | n/a | (objobjargproc) element_ass_subscr, |
---|
3736 | n/a | }; |
---|
3737 | n/a | |
---|
3738 | n/a | static PyGetSetDef element_getsetlist[] = { |
---|
3739 | n/a | {"tag", |
---|
3740 | n/a | (getter)element_tag_getter, |
---|
3741 | n/a | (setter)element_tag_setter, |
---|
3742 | n/a | "A string identifying what kind of data this element represents"}, |
---|
3743 | n/a | {"text", |
---|
3744 | n/a | (getter)element_text_getter, |
---|
3745 | n/a | (setter)element_text_setter, |
---|
3746 | n/a | "A string of text directly after the start tag, or None"}, |
---|
3747 | n/a | {"tail", |
---|
3748 | n/a | (getter)element_tail_getter, |
---|
3749 | n/a | (setter)element_tail_setter, |
---|
3750 | n/a | "A string of text directly after the end tag, or None"}, |
---|
3751 | n/a | {"attrib", |
---|
3752 | n/a | (getter)element_attrib_getter, |
---|
3753 | n/a | (setter)element_attrib_setter, |
---|
3754 | n/a | "A dictionary containing the element's attributes"}, |
---|
3755 | n/a | {NULL}, |
---|
3756 | n/a | }; |
---|
3757 | n/a | |
---|
3758 | n/a | static PyTypeObject Element_Type = { |
---|
3759 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
3760 | n/a | "xml.etree.ElementTree.Element", sizeof(ElementObject), 0, |
---|
3761 | n/a | /* methods */ |
---|
3762 | n/a | (destructor)element_dealloc, /* tp_dealloc */ |
---|
3763 | n/a | 0, /* tp_print */ |
---|
3764 | n/a | 0, /* tp_getattr */ |
---|
3765 | n/a | 0, /* tp_setattr */ |
---|
3766 | n/a | 0, /* tp_reserved */ |
---|
3767 | n/a | (reprfunc)element_repr, /* tp_repr */ |
---|
3768 | n/a | 0, /* tp_as_number */ |
---|
3769 | n/a | &element_as_sequence, /* tp_as_sequence */ |
---|
3770 | n/a | &element_as_mapping, /* tp_as_mapping */ |
---|
3771 | n/a | 0, /* tp_hash */ |
---|
3772 | n/a | 0, /* tp_call */ |
---|
3773 | n/a | 0, /* tp_str */ |
---|
3774 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
3775 | n/a | 0, /* tp_setattro */ |
---|
3776 | n/a | 0, /* tp_as_buffer */ |
---|
3777 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
---|
3778 | n/a | /* tp_flags */ |
---|
3779 | n/a | 0, /* tp_doc */ |
---|
3780 | n/a | (traverseproc)element_gc_traverse, /* tp_traverse */ |
---|
3781 | n/a | (inquiry)element_gc_clear, /* tp_clear */ |
---|
3782 | n/a | 0, /* tp_richcompare */ |
---|
3783 | n/a | offsetof(ElementObject, weakreflist), /* tp_weaklistoffset */ |
---|
3784 | n/a | 0, /* tp_iter */ |
---|
3785 | n/a | 0, /* tp_iternext */ |
---|
3786 | n/a | element_methods, /* tp_methods */ |
---|
3787 | n/a | 0, /* tp_members */ |
---|
3788 | n/a | element_getsetlist, /* tp_getset */ |
---|
3789 | n/a | 0, /* tp_base */ |
---|
3790 | n/a | 0, /* tp_dict */ |
---|
3791 | n/a | 0, /* tp_descr_get */ |
---|
3792 | n/a | 0, /* tp_descr_set */ |
---|
3793 | n/a | 0, /* tp_dictoffset */ |
---|
3794 | n/a | (initproc)element_init, /* tp_init */ |
---|
3795 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
3796 | n/a | element_new, /* tp_new */ |
---|
3797 | n/a | 0, /* tp_free */ |
---|
3798 | n/a | }; |
---|
3799 | n/a | |
---|
3800 | n/a | static PyMethodDef treebuilder_methods[] = { |
---|
3801 | n/a | _ELEMENTTREE_TREEBUILDER_DATA_METHODDEF |
---|
3802 | n/a | _ELEMENTTREE_TREEBUILDER_START_METHODDEF |
---|
3803 | n/a | _ELEMENTTREE_TREEBUILDER_END_METHODDEF |
---|
3804 | n/a | _ELEMENTTREE_TREEBUILDER_CLOSE_METHODDEF |
---|
3805 | n/a | {NULL, NULL} |
---|
3806 | n/a | }; |
---|
3807 | n/a | |
---|
3808 | n/a | static PyTypeObject TreeBuilder_Type = { |
---|
3809 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
3810 | n/a | "xml.etree.ElementTree.TreeBuilder", sizeof(TreeBuilderObject), 0, |
---|
3811 | n/a | /* methods */ |
---|
3812 | n/a | (destructor)treebuilder_dealloc, /* tp_dealloc */ |
---|
3813 | n/a | 0, /* tp_print */ |
---|
3814 | n/a | 0, /* tp_getattr */ |
---|
3815 | n/a | 0, /* tp_setattr */ |
---|
3816 | n/a | 0, /* tp_reserved */ |
---|
3817 | n/a | 0, /* tp_repr */ |
---|
3818 | n/a | 0, /* tp_as_number */ |
---|
3819 | n/a | 0, /* tp_as_sequence */ |
---|
3820 | n/a | 0, /* tp_as_mapping */ |
---|
3821 | n/a | 0, /* tp_hash */ |
---|
3822 | n/a | 0, /* tp_call */ |
---|
3823 | n/a | 0, /* tp_str */ |
---|
3824 | n/a | 0, /* tp_getattro */ |
---|
3825 | n/a | 0, /* tp_setattro */ |
---|
3826 | n/a | 0, /* tp_as_buffer */ |
---|
3827 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
---|
3828 | n/a | /* tp_flags */ |
---|
3829 | n/a | 0, /* tp_doc */ |
---|
3830 | n/a | (traverseproc)treebuilder_gc_traverse, /* tp_traverse */ |
---|
3831 | n/a | (inquiry)treebuilder_gc_clear, /* tp_clear */ |
---|
3832 | n/a | 0, /* tp_richcompare */ |
---|
3833 | n/a | 0, /* tp_weaklistoffset */ |
---|
3834 | n/a | 0, /* tp_iter */ |
---|
3835 | n/a | 0, /* tp_iternext */ |
---|
3836 | n/a | treebuilder_methods, /* tp_methods */ |
---|
3837 | n/a | 0, /* tp_members */ |
---|
3838 | n/a | 0, /* tp_getset */ |
---|
3839 | n/a | 0, /* tp_base */ |
---|
3840 | n/a | 0, /* tp_dict */ |
---|
3841 | n/a | 0, /* tp_descr_get */ |
---|
3842 | n/a | 0, /* tp_descr_set */ |
---|
3843 | n/a | 0, /* tp_dictoffset */ |
---|
3844 | n/a | _elementtree_TreeBuilder___init__, /* tp_init */ |
---|
3845 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
3846 | n/a | treebuilder_new, /* tp_new */ |
---|
3847 | n/a | 0, /* tp_free */ |
---|
3848 | n/a | }; |
---|
3849 | n/a | |
---|
3850 | n/a | static PyMethodDef xmlparser_methods[] = { |
---|
3851 | n/a | _ELEMENTTREE_XMLPARSER_FEED_METHODDEF |
---|
3852 | n/a | _ELEMENTTREE_XMLPARSER_CLOSE_METHODDEF |
---|
3853 | n/a | _ELEMENTTREE_XMLPARSER__PARSE_WHOLE_METHODDEF |
---|
3854 | n/a | _ELEMENTTREE_XMLPARSER__SETEVENTS_METHODDEF |
---|
3855 | n/a | _ELEMENTTREE_XMLPARSER_DOCTYPE_METHODDEF |
---|
3856 | n/a | {NULL, NULL} |
---|
3857 | n/a | }; |
---|
3858 | n/a | |
---|
3859 | n/a | static PyTypeObject XMLParser_Type = { |
---|
3860 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
3861 | n/a | "xml.etree.ElementTree.XMLParser", sizeof(XMLParserObject), 0, |
---|
3862 | n/a | /* methods */ |
---|
3863 | n/a | (destructor)xmlparser_dealloc, /* tp_dealloc */ |
---|
3864 | n/a | 0, /* tp_print */ |
---|
3865 | n/a | 0, /* tp_getattr */ |
---|
3866 | n/a | 0, /* tp_setattr */ |
---|
3867 | n/a | 0, /* tp_reserved */ |
---|
3868 | n/a | 0, /* tp_repr */ |
---|
3869 | n/a | 0, /* tp_as_number */ |
---|
3870 | n/a | 0, /* tp_as_sequence */ |
---|
3871 | n/a | 0, /* tp_as_mapping */ |
---|
3872 | n/a | 0, /* tp_hash */ |
---|
3873 | n/a | 0, /* tp_call */ |
---|
3874 | n/a | 0, /* tp_str */ |
---|
3875 | n/a | (getattrofunc)xmlparser_getattro, /* tp_getattro */ |
---|
3876 | n/a | 0, /* tp_setattro */ |
---|
3877 | n/a | 0, /* tp_as_buffer */ |
---|
3878 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
---|
3879 | n/a | /* tp_flags */ |
---|
3880 | n/a | 0, /* tp_doc */ |
---|
3881 | n/a | (traverseproc)xmlparser_gc_traverse, /* tp_traverse */ |
---|
3882 | n/a | (inquiry)xmlparser_gc_clear, /* tp_clear */ |
---|
3883 | n/a | 0, /* tp_richcompare */ |
---|
3884 | n/a | 0, /* tp_weaklistoffset */ |
---|
3885 | n/a | 0, /* tp_iter */ |
---|
3886 | n/a | 0, /* tp_iternext */ |
---|
3887 | n/a | xmlparser_methods, /* tp_methods */ |
---|
3888 | n/a | 0, /* tp_members */ |
---|
3889 | n/a | 0, /* tp_getset */ |
---|
3890 | n/a | 0, /* tp_base */ |
---|
3891 | n/a | 0, /* tp_dict */ |
---|
3892 | n/a | 0, /* tp_descr_get */ |
---|
3893 | n/a | 0, /* tp_descr_set */ |
---|
3894 | n/a | 0, /* tp_dictoffset */ |
---|
3895 | n/a | _elementtree_XMLParser___init__, /* tp_init */ |
---|
3896 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
3897 | n/a | xmlparser_new, /* tp_new */ |
---|
3898 | n/a | 0, /* tp_free */ |
---|
3899 | n/a | }; |
---|
3900 | n/a | |
---|
3901 | n/a | /* ==================================================================== */ |
---|
3902 | n/a | /* python module interface */ |
---|
3903 | n/a | |
---|
3904 | n/a | static PyMethodDef _functions[] = { |
---|
3905 | n/a | {"SubElement", (PyCFunction) subelement, METH_VARARGS | METH_KEYWORDS}, |
---|
3906 | n/a | {NULL, NULL} |
---|
3907 | n/a | }; |
---|
3908 | n/a | |
---|
3909 | n/a | |
---|
3910 | n/a | static struct PyModuleDef elementtreemodule = { |
---|
3911 | n/a | PyModuleDef_HEAD_INIT, |
---|
3912 | n/a | "_elementtree", |
---|
3913 | n/a | NULL, |
---|
3914 | n/a | sizeof(elementtreestate), |
---|
3915 | n/a | _functions, |
---|
3916 | n/a | NULL, |
---|
3917 | n/a | elementtree_traverse, |
---|
3918 | n/a | elementtree_clear, |
---|
3919 | n/a | elementtree_free |
---|
3920 | n/a | }; |
---|
3921 | n/a | |
---|
3922 | n/a | PyMODINIT_FUNC |
---|
3923 | n/a | PyInit__elementtree(void) |
---|
3924 | n/a | { |
---|
3925 | n/a | PyObject *m, *temp; |
---|
3926 | n/a | elementtreestate *st; |
---|
3927 | n/a | |
---|
3928 | n/a | m = PyState_FindModule(&elementtreemodule); |
---|
3929 | n/a | if (m) { |
---|
3930 | n/a | Py_INCREF(m); |
---|
3931 | n/a | return m; |
---|
3932 | n/a | } |
---|
3933 | n/a | |
---|
3934 | n/a | /* Initialize object types */ |
---|
3935 | n/a | if (PyType_Ready(&ElementIter_Type) < 0) |
---|
3936 | n/a | return NULL; |
---|
3937 | n/a | if (PyType_Ready(&TreeBuilder_Type) < 0) |
---|
3938 | n/a | return NULL; |
---|
3939 | n/a | if (PyType_Ready(&Element_Type) < 0) |
---|
3940 | n/a | return NULL; |
---|
3941 | n/a | if (PyType_Ready(&XMLParser_Type) < 0) |
---|
3942 | n/a | return NULL; |
---|
3943 | n/a | |
---|
3944 | n/a | m = PyModule_Create(&elementtreemodule); |
---|
3945 | n/a | if (!m) |
---|
3946 | n/a | return NULL; |
---|
3947 | n/a | st = ET_STATE(m); |
---|
3948 | n/a | |
---|
3949 | n/a | if (!(temp = PyImport_ImportModule("copy"))) |
---|
3950 | n/a | return NULL; |
---|
3951 | n/a | st->deepcopy_obj = PyObject_GetAttrString(temp, "deepcopy"); |
---|
3952 | n/a | Py_XDECREF(temp); |
---|
3953 | n/a | |
---|
3954 | n/a | if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath"))) |
---|
3955 | n/a | return NULL; |
---|
3956 | n/a | |
---|
3957 | n/a | /* link against pyexpat */ |
---|
3958 | n/a | expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); |
---|
3959 | n/a | if (expat_capi) { |
---|
3960 | n/a | /* check that it's usable */ |
---|
3961 | n/a | if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 || |
---|
3962 | n/a | (size_t)expat_capi->size < sizeof(struct PyExpat_CAPI) || |
---|
3963 | n/a | expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || |
---|
3964 | n/a | expat_capi->MINOR_VERSION != XML_MINOR_VERSION || |
---|
3965 | n/a | expat_capi->MICRO_VERSION != XML_MICRO_VERSION) { |
---|
3966 | n/a | PyErr_SetString(PyExc_ImportError, |
---|
3967 | n/a | "pyexpat version is incompatible"); |
---|
3968 | n/a | return NULL; |
---|
3969 | n/a | } |
---|
3970 | n/a | } else { |
---|
3971 | n/a | return NULL; |
---|
3972 | n/a | } |
---|
3973 | n/a | |
---|
3974 | n/a | st->parseerror_obj = PyErr_NewException( |
---|
3975 | n/a | "xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL |
---|
3976 | n/a | ); |
---|
3977 | n/a | Py_INCREF(st->parseerror_obj); |
---|
3978 | n/a | PyModule_AddObject(m, "ParseError", st->parseerror_obj); |
---|
3979 | n/a | |
---|
3980 | n/a | Py_INCREF((PyObject *)&Element_Type); |
---|
3981 | n/a | PyModule_AddObject(m, "Element", (PyObject *)&Element_Type); |
---|
3982 | n/a | |
---|
3983 | n/a | Py_INCREF((PyObject *)&TreeBuilder_Type); |
---|
3984 | n/a | PyModule_AddObject(m, "TreeBuilder", (PyObject *)&TreeBuilder_Type); |
---|
3985 | n/a | |
---|
3986 | n/a | Py_INCREF((PyObject *)&XMLParser_Type); |
---|
3987 | n/a | PyModule_AddObject(m, "XMLParser", (PyObject *)&XMLParser_Type); |
---|
3988 | n/a | |
---|
3989 | n/a | return m; |
---|
3990 | n/a | } |
---|