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