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); |
Serhiy Storchaka | 18f018c | 2016-12-21 12:32:56 +0200 | [diff] [blame] | 672 | Py_TRASHCAN_SAFE_BEGIN(self) |
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); |
Serhiy Storchaka | 18f018c | 2016-12-21 12:32:56 +0200 | [diff] [blame] | 683 | Py_TRASHCAN_SAFE_END(self) |
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); |
| 1152 | for (i = 0; i < len; i++) { |
| 1153 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 1154 | if (ch == '{') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1155 | check = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | else if (ch == '}') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1157 | check = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1158 | else if (check && PATHCHAR(ch)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1159 | return 1; |
| 1160 | } |
| 1161 | return 0; |
| 1162 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1163 | if (PyBytes_Check(tag)) { |
| 1164 | char *p = PyBytes_AS_STRING(tag); |
| 1165 | for (i = 0; i < PyBytes_GET_SIZE(tag); i++) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1166 | if (p[i] == '{') |
| 1167 | check = 0; |
| 1168 | else if (p[i] == '}') |
| 1169 | check = 1; |
| 1170 | else if (check && PATHCHAR(p[i])) |
| 1171 | return 1; |
| 1172 | } |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | return 1; /* unknown type; might be path expression */ |
| 1177 | } |
| 1178 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1179 | /*[clinic input] |
| 1180 | _elementtree.Element.extend |
| 1181 | |
| 1182 | elements: object |
| 1183 | / |
| 1184 | |
| 1185 | [clinic start generated code]*/ |
| 1186 | |
| 1187 | static PyObject * |
| 1188 | _elementtree_Element_extend(ElementObject *self, PyObject *elements) |
| 1189 | /*[clinic end generated code: output=f6e67fc2ff529191 input=807bc4f31c69f7c0]*/ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1190 | { |
| 1191 | PyObject* seq; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1192 | Py_ssize_t i; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1193 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1194 | seq = PySequence_Fast(elements, ""); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1195 | if (!seq) { |
| 1196 | PyErr_Format( |
| 1197 | PyExc_TypeError, |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1198 | "expected sequence, not \"%.200s\"", Py_TYPE(elements)->tp_name |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1199 | ); |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1203 | for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1204 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1205 | Py_INCREF(element); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1206 | if (element_add_subelement(self, element) < 0) { |
| 1207 | Py_DECREF(seq); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1208 | Py_DECREF(element); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1211 | Py_DECREF(element); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | Py_DECREF(seq); |
| 1215 | |
| 1216 | Py_RETURN_NONE; |
| 1217 | } |
| 1218 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1219 | /*[clinic input] |
| 1220 | _elementtree.Element.find |
| 1221 | |
| 1222 | path: object |
| 1223 | namespaces: object = None |
| 1224 | |
| 1225 | [clinic start generated code]*/ |
| 1226 | |
| 1227 | static PyObject * |
| 1228 | _elementtree_Element_find_impl(ElementObject *self, PyObject *path, |
| 1229 | PyObject *namespaces) |
| 1230 | /*[clinic end generated code: output=41b43f0f0becafae input=359b6985f6489d2e]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1231 | { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1232 | Py_ssize_t i; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1233 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1234 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1235 | if (checkpath(path) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1236 | _Py_IDENTIFIER(find); |
Victor Stinner | f561634 | 2016-12-09 15:26:00 +0100 | [diff] [blame] | 1237 | return _PyObject_CallMethodIdObjArgs( |
| 1238 | st->elementpath_obj, &PyId_find, self, path, namespaces, NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1239 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1240 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (!self->extra) |
| 1243 | Py_RETURN_NONE; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1244 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1245 | for (i = 0; i < self->extra->length; i++) { |
| 1246 | PyObject* item = self->extra->children[i]; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1247 | int rc; |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 1248 | assert(Element_Check(item)); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1249 | Py_INCREF(item); |
Serhiy Storchaka | a2c145c | 2015-05-18 18:33:31 +0300 | [diff] [blame] | 1250 | rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1251 | if (rc > 0) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1252 | return item; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1253 | Py_DECREF(item); |
| 1254 | if (rc < 0) |
| 1255 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | Py_RETURN_NONE; |
| 1259 | } |
| 1260 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1261 | /*[clinic input] |
| 1262 | _elementtree.Element.findtext |
| 1263 | |
| 1264 | path: object |
| 1265 | default: object = None |
| 1266 | namespaces: object = None |
| 1267 | |
| 1268 | [clinic start generated code]*/ |
| 1269 | |
| 1270 | static PyObject * |
| 1271 | _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path, |
| 1272 | PyObject *default_value, |
| 1273 | PyObject *namespaces) |
| 1274 | /*[clinic end generated code: output=83b3ba4535d308d2 input=b53a85aa5aa2a916]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1275 | { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1276 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1277 | _Py_IDENTIFIER(findtext); |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1278 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1279 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1280 | if (checkpath(path) || namespaces != Py_None) |
Victor Stinner | f561634 | 2016-12-09 15:26:00 +0100 | [diff] [blame] | 1281 | return _PyObject_CallMethodIdObjArgs( |
| 1282 | st->elementpath_obj, &PyId_findtext, |
| 1283 | self, path, default_value, namespaces, NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1284 | ); |
| 1285 | |
| 1286 | if (!self->extra) { |
| 1287 | Py_INCREF(default_value); |
| 1288 | return default_value; |
| 1289 | } |
| 1290 | |
| 1291 | for (i = 0; i < self->extra->length; i++) { |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1292 | PyObject *item = self->extra->children[i]; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1293 | int rc; |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 1294 | assert(Element_Check(item)); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1295 | Py_INCREF(item); |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1296 | rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1297 | if (rc > 0) { |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1298 | PyObject* text = element_get_text((ElementObject*)item); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1299 | if (text == Py_None) { |
| 1300 | Py_DECREF(item); |
Eli Bendersky | 25771b3 | 2013-01-13 05:26:07 -0800 | [diff] [blame] | 1301 | return PyUnicode_New(0, 0); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1302 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1303 | Py_XINCREF(text); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1304 | Py_DECREF(item); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1305 | return text; |
| 1306 | } |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1307 | Py_DECREF(item); |
| 1308 | if (rc < 0) |
| 1309 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | Py_INCREF(default_value); |
| 1313 | return default_value; |
| 1314 | } |
| 1315 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1316 | /*[clinic input] |
| 1317 | _elementtree.Element.findall |
| 1318 | |
| 1319 | path: object |
| 1320 | namespaces: object = None |
| 1321 | |
| 1322 | [clinic start generated code]*/ |
| 1323 | |
| 1324 | static PyObject * |
| 1325 | _elementtree_Element_findall_impl(ElementObject *self, PyObject *path, |
| 1326 | PyObject *namespaces) |
| 1327 | /*[clinic end generated code: output=1a0bd9f5541b711d input=4d9e6505a638550c]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1328 | { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1329 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1330 | PyObject* out; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1331 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1332 | |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1333 | if (checkpath(path) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1334 | _Py_IDENTIFIER(findall); |
Victor Stinner | f561634 | 2016-12-09 15:26:00 +0100 | [diff] [blame] | 1335 | return _PyObject_CallMethodIdObjArgs( |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1336 | st->elementpath_obj, &PyId_findall, self, path, namespaces, NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1337 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1338 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1339 | |
| 1340 | out = PyList_New(0); |
| 1341 | if (!out) |
| 1342 | return NULL; |
| 1343 | |
| 1344 | if (!self->extra) |
| 1345 | return out; |
| 1346 | |
| 1347 | for (i = 0; i < self->extra->length; i++) { |
| 1348 | PyObject* item = self->extra->children[i]; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1349 | int rc; |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 1350 | assert(Element_Check(item)); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1351 | Py_INCREF(item); |
Serhiy Storchaka | b11c566 | 2018-10-14 10:32:19 +0300 | [diff] [blame] | 1352 | rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1353 | if (rc != 0 && (rc < 0 || PyList_Append(out, item) < 0)) { |
| 1354 | Py_DECREF(item); |
| 1355 | Py_DECREF(out); |
| 1356 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1357 | } |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1358 | Py_DECREF(item); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | return out; |
| 1362 | } |
| 1363 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1364 | /*[clinic input] |
| 1365 | _elementtree.Element.iterfind |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1366 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1367 | path: object |
| 1368 | namespaces: object = None |
| 1369 | |
| 1370 | [clinic start generated code]*/ |
| 1371 | |
| 1372 | static PyObject * |
| 1373 | _elementtree_Element_iterfind_impl(ElementObject *self, PyObject *path, |
| 1374 | PyObject *namespaces) |
| 1375 | /*[clinic end generated code: output=ecdd56d63b19d40f input=abb974e350fb65c7]*/ |
| 1376 | { |
| 1377 | PyObject* tag = path; |
| 1378 | _Py_IDENTIFIER(iterfind); |
| 1379 | elementtreestate *st = ET_STATE_GLOBAL; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1380 | |
Victor Stinner | f561634 | 2016-12-09 15:26:00 +0100 | [diff] [blame] | 1381 | return _PyObject_CallMethodIdObjArgs( |
| 1382 | st->elementpath_obj, &PyId_iterfind, self, tag, namespaces, NULL); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1385 | /*[clinic input] |
| 1386 | _elementtree.Element.get |
| 1387 | |
| 1388 | key: object |
| 1389 | default: object = None |
| 1390 | |
| 1391 | [clinic start generated code]*/ |
| 1392 | |
| 1393 | static PyObject * |
| 1394 | _elementtree_Element_get_impl(ElementObject *self, PyObject *key, |
| 1395 | PyObject *default_value) |
| 1396 | /*[clinic end generated code: output=523c614142595d75 input=ee153bbf8cdb246e]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1397 | { |
| 1398 | PyObject* value; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (!self->extra || self->extra->attrib == Py_None) |
| 1401 | value = default_value; |
| 1402 | else { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1403 | value = PyDict_GetItemWithError(self->extra->attrib, key); |
| 1404 | if (!value) { |
| 1405 | if (PyErr_Occurred()) { |
| 1406 | return NULL; |
| 1407 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1408 | value = default_value; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1409 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | Py_INCREF(value); |
| 1413 | return value; |
| 1414 | } |
| 1415 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1416 | /*[clinic input] |
| 1417 | _elementtree.Element.getchildren |
| 1418 | |
| 1419 | [clinic start generated code]*/ |
| 1420 | |
| 1421 | static PyObject * |
| 1422 | _elementtree_Element_getchildren_impl(ElementObject *self) |
| 1423 | /*[clinic end generated code: output=e50ffe118637b14f input=0f754dfded150d5f]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1424 | { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1425 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1426 | PyObject* list; |
| 1427 | |
Serhiy Storchaka | 762ec97 | 2017-03-30 18:12:06 +0300 | [diff] [blame] | 1428 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1429 | "This method will be removed in future versions. " |
| 1430 | "Use 'list(elem)' or iteration over elem instead.", |
| 1431 | 1) < 0) { |
| 1432 | return NULL; |
| 1433 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1434 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1435 | if (!self->extra) |
| 1436 | return PyList_New(0); |
| 1437 | |
| 1438 | list = PyList_New(self->extra->length); |
| 1439 | if (!list) |
| 1440 | return NULL; |
| 1441 | |
| 1442 | for (i = 0; i < self->extra->length; i++) { |
| 1443 | PyObject* item = self->extra->children[i]; |
| 1444 | Py_INCREF(item); |
| 1445 | PyList_SET_ITEM(list, i, item); |
| 1446 | } |
| 1447 | |
| 1448 | return list; |
| 1449 | } |
| 1450 | |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1451 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1452 | static PyObject * |
| 1453 | create_elementiter(ElementObject *self, PyObject *tag, int gettext); |
| 1454 | |
| 1455 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1456 | /*[clinic input] |
| 1457 | _elementtree.Element.iter |
| 1458 | |
| 1459 | tag: object = None |
| 1460 | |
| 1461 | [clinic start generated code]*/ |
| 1462 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1463 | static PyObject * |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1464 | _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag) |
| 1465 | /*[clinic end generated code: output=3f49f9a862941cc5 input=774d5b12e573aedd]*/ |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1466 | { |
Serhiy Storchaka | d6a69d8 | 2015-12-09 11:27:07 +0200 | [diff] [blame] | 1467 | if (PyUnicode_Check(tag)) { |
| 1468 | if (PyUnicode_READY(tag) < 0) |
| 1469 | return NULL; |
| 1470 | if (PyUnicode_GET_LENGTH(tag) == 1 && PyUnicode_READ_CHAR(tag, 0) == '*') |
| 1471 | tag = Py_None; |
| 1472 | } |
| 1473 | else if (PyBytes_Check(tag)) { |
| 1474 | if (PyBytes_GET_SIZE(tag) == 1 && *PyBytes_AS_STRING(tag) == '*') |
| 1475 | tag = Py_None; |
| 1476 | } |
| 1477 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1478 | return create_elementiter(self, tag, 0); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1482 | /*[clinic input] |
Serhiy Storchaka | 762ec97 | 2017-03-30 18:12:06 +0300 | [diff] [blame] | 1483 | _elementtree.Element.getiterator |
| 1484 | |
| 1485 | tag: object = None |
| 1486 | |
| 1487 | [clinic start generated code]*/ |
| 1488 | |
| 1489 | static PyObject * |
| 1490 | _elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag) |
| 1491 | /*[clinic end generated code: output=cb69ff4a3742dfa1 input=500da1a03f7b9e28]*/ |
| 1492 | { |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 1493 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Serhiy Storchaka | 762ec97 | 2017-03-30 18:12:06 +0300 | [diff] [blame] | 1494 | "This method will be removed in future versions. " |
| 1495 | "Use 'tree.iter()' or 'list(tree.iter())' instead.", |
| 1496 | 1) < 0) { |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | return _elementtree_Element_iter_impl(self, tag); |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /*[clinic input] |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1504 | _elementtree.Element.itertext |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1505 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1506 | [clinic start generated code]*/ |
| 1507 | |
| 1508 | static PyObject * |
| 1509 | _elementtree_Element_itertext_impl(ElementObject *self) |
| 1510 | /*[clinic end generated code: output=5fa34b2fbcb65df6 input=af8f0e42cb239c89]*/ |
| 1511 | { |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1512 | return create_elementiter(self, Py_None, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1515 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1516 | static PyObject* |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1517 | element_getitem(PyObject* self_, Py_ssize_t index) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1518 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1519 | ElementObject* self = (ElementObject*) self_; |
| 1520 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1521 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1522 | PyErr_SetString( |
| 1523 | PyExc_IndexError, |
| 1524 | "child index out of range" |
| 1525 | ); |
| 1526 | return NULL; |
| 1527 | } |
| 1528 | |
| 1529 | Py_INCREF(self->extra->children[index]); |
| 1530 | return self->extra->children[index]; |
| 1531 | } |
| 1532 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1533 | /*[clinic input] |
| 1534 | _elementtree.Element.insert |
| 1535 | |
| 1536 | index: Py_ssize_t |
| 1537 | subelement: object(subclass_of='&Element_Type') |
| 1538 | / |
| 1539 | |
| 1540 | [clinic start generated code]*/ |
| 1541 | |
| 1542 | static PyObject * |
| 1543 | _elementtree_Element_insert_impl(ElementObject *self, Py_ssize_t index, |
| 1544 | PyObject *subelement) |
| 1545 | /*[clinic end generated code: output=990adfef4d424c0b input=cd6fbfcdab52d7a8]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1546 | { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1547 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1548 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1549 | if (!self->extra) { |
| 1550 | if (create_extra(self, NULL) < 0) |
| 1551 | return NULL; |
| 1552 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1553 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1554 | if (index < 0) { |
| 1555 | index += self->extra->length; |
| 1556 | if (index < 0) |
| 1557 | index = 0; |
| 1558 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1559 | if (index > self->extra->length) |
| 1560 | index = self->extra->length; |
| 1561 | |
| 1562 | if (element_resize(self, 1) < 0) |
| 1563 | return NULL; |
| 1564 | |
| 1565 | for (i = self->extra->length; i > index; i--) |
| 1566 | self->extra->children[i] = self->extra->children[i-1]; |
| 1567 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1568 | Py_INCREF(subelement); |
| 1569 | self->extra->children[index] = subelement; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1570 | |
| 1571 | self->extra->length++; |
| 1572 | |
| 1573 | Py_RETURN_NONE; |
| 1574 | } |
| 1575 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1576 | /*[clinic input] |
| 1577 | _elementtree.Element.items |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1578 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1579 | [clinic start generated code]*/ |
| 1580 | |
| 1581 | static PyObject * |
| 1582 | _elementtree_Element_items_impl(ElementObject *self) |
| 1583 | /*[clinic end generated code: output=6db2c778ce3f5a4d input=adbe09aaea474447]*/ |
| 1584 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1585 | if (!self->extra || self->extra->attrib == Py_None) |
| 1586 | return PyList_New(0); |
| 1587 | |
| 1588 | return PyDict_Items(self->extra->attrib); |
| 1589 | } |
| 1590 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1591 | /*[clinic input] |
| 1592 | _elementtree.Element.keys |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1593 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1594 | [clinic start generated code]*/ |
| 1595 | |
| 1596 | static PyObject * |
| 1597 | _elementtree_Element_keys_impl(ElementObject *self) |
| 1598 | /*[clinic end generated code: output=bc5bfabbf20eeb3c input=f02caf5b496b5b0b]*/ |
| 1599 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1600 | if (!self->extra || self->extra->attrib == Py_None) |
| 1601 | return PyList_New(0); |
| 1602 | |
| 1603 | return PyDict_Keys(self->extra->attrib); |
| 1604 | } |
| 1605 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1606 | static Py_ssize_t |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1607 | element_length(ElementObject* self) |
| 1608 | { |
| 1609 | if (!self->extra) |
| 1610 | return 0; |
| 1611 | |
| 1612 | return self->extra->length; |
| 1613 | } |
| 1614 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1615 | /*[clinic input] |
| 1616 | _elementtree.Element.makeelement |
| 1617 | |
| 1618 | tag: object |
| 1619 | attrib: object |
| 1620 | / |
| 1621 | |
| 1622 | [clinic start generated code]*/ |
| 1623 | |
| 1624 | static PyObject * |
| 1625 | _elementtree_Element_makeelement_impl(ElementObject *self, PyObject *tag, |
| 1626 | PyObject *attrib) |
| 1627 | /*[clinic end generated code: output=4109832d5bb789ef input=9480d1d2e3e68235]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1628 | { |
| 1629 | PyObject* elem; |
| 1630 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1631 | attrib = PyDict_Copy(attrib); |
| 1632 | if (!attrib) |
| 1633 | return NULL; |
| 1634 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1635 | elem = create_new_element(tag, attrib); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1636 | |
| 1637 | Py_DECREF(attrib); |
| 1638 | |
| 1639 | return elem; |
| 1640 | } |
| 1641 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1642 | /*[clinic input] |
| 1643 | _elementtree.Element.remove |
| 1644 | |
| 1645 | subelement: object(subclass_of='&Element_Type') |
| 1646 | / |
| 1647 | |
| 1648 | [clinic start generated code]*/ |
| 1649 | |
| 1650 | static PyObject * |
| 1651 | _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement) |
| 1652 | /*[clinic end generated code: output=38fe6c07d6d87d1f input=d52fc28ededc0bd8]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1653 | { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1654 | Py_ssize_t i; |
Serhiy Storchaka | a2c145c | 2015-05-18 18:33:31 +0300 | [diff] [blame] | 1655 | int rc; |
| 1656 | PyObject *found; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1657 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1658 | if (!self->extra) { |
| 1659 | /* element has no children, so raise exception */ |
| 1660 | PyErr_SetString( |
| 1661 | PyExc_ValueError, |
| 1662 | "list.remove(x): x not in list" |
| 1663 | ); |
| 1664 | return NULL; |
| 1665 | } |
| 1666 | |
| 1667 | for (i = 0; i < self->extra->length; i++) { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1668 | if (self->extra->children[i] == subelement) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1669 | break; |
Serhiy Storchaka | a2c145c | 2015-05-18 18:33:31 +0300 | [diff] [blame] | 1670 | rc = PyObject_RichCompareBool(self->extra->children[i], subelement, Py_EQ); |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1671 | if (rc > 0) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1672 | break; |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1673 | if (rc < 0) |
| 1674 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1677 | if (i >= self->extra->length) { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1678 | /* subelement is not in children, so raise exception */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1679 | PyErr_SetString( |
| 1680 | PyExc_ValueError, |
| 1681 | "list.remove(x): x not in list" |
| 1682 | ); |
| 1683 | return NULL; |
| 1684 | } |
| 1685 | |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1686 | found = self->extra->children[i]; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1687 | |
| 1688 | self->extra->length--; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1689 | for (; i < self->extra->length; i++) |
| 1690 | self->extra->children[i] = self->extra->children[i+1]; |
| 1691 | |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 1692 | Py_DECREF(found); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1693 | Py_RETURN_NONE; |
| 1694 | } |
| 1695 | |
| 1696 | static PyObject* |
| 1697 | element_repr(ElementObject* self) |
| 1698 | { |
Serhiy Storchaka | 9062c26 | 2016-06-12 09:43:55 +0300 | [diff] [blame] | 1699 | int status; |
| 1700 | |
| 1701 | if (self->tag == NULL) |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1702 | return PyUnicode_FromFormat("<Element at %p>", self); |
Serhiy Storchaka | 9062c26 | 2016-06-12 09:43:55 +0300 | [diff] [blame] | 1703 | |
| 1704 | status = Py_ReprEnter((PyObject *)self); |
| 1705 | if (status == 0) { |
| 1706 | PyObject *res; |
| 1707 | res = PyUnicode_FromFormat("<Element %R at %p>", self->tag, self); |
| 1708 | Py_ReprLeave((PyObject *)self); |
| 1709 | return res; |
| 1710 | } |
| 1711 | if (status > 0) |
| 1712 | PyErr_Format(PyExc_RuntimeError, |
| 1713 | "reentrant call inside %s.__repr__", |
| 1714 | Py_TYPE(self)->tp_name); |
| 1715 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 1718 | /*[clinic input] |
| 1719 | _elementtree.Element.set |
| 1720 | |
| 1721 | key: object |
| 1722 | value: object |
| 1723 | / |
| 1724 | |
| 1725 | [clinic start generated code]*/ |
| 1726 | |
| 1727 | static PyObject * |
| 1728 | _elementtree_Element_set_impl(ElementObject *self, PyObject *key, |
| 1729 | PyObject *value) |
| 1730 | /*[clinic end generated code: output=fb938806be3c5656 input=1efe90f7d82b3fe9]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1731 | { |
| 1732 | PyObject* attrib; |
| 1733 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1734 | if (!self->extra) { |
| 1735 | if (create_extra(self, NULL) < 0) |
| 1736 | return NULL; |
| 1737 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1738 | |
| 1739 | attrib = element_get_attrib(self); |
| 1740 | if (!attrib) |
| 1741 | return NULL; |
| 1742 | |
| 1743 | if (PyDict_SetItem(attrib, key, value) < 0) |
| 1744 | return NULL; |
| 1745 | |
| 1746 | Py_RETURN_NONE; |
| 1747 | } |
| 1748 | |
| 1749 | static int |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1750 | element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1751 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1752 | ElementObject* self = (ElementObject*) self_; |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1753 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1754 | PyObject* old; |
| 1755 | |
| 1756 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1757 | PyErr_SetString( |
| 1758 | PyExc_IndexError, |
| 1759 | "child assignment index out of range"); |
| 1760 | return -1; |
| 1761 | } |
| 1762 | |
| 1763 | old = self->extra->children[index]; |
| 1764 | |
| 1765 | if (item) { |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 1766 | if (!Element_Check(item)) { |
| 1767 | raise_type_error(item); |
| 1768 | return -1; |
| 1769 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1770 | Py_INCREF(item); |
| 1771 | self->extra->children[index] = item; |
| 1772 | } else { |
| 1773 | self->extra->length--; |
| 1774 | for (i = index; i < self->extra->length; i++) |
| 1775 | self->extra->children[i] = self->extra->children[i+1]; |
| 1776 | } |
| 1777 | |
| 1778 | Py_DECREF(old); |
| 1779 | |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1783 | static PyObject* |
| 1784 | element_subscr(PyObject* self_, PyObject* item) |
| 1785 | { |
| 1786 | ElementObject* self = (ElementObject*) self_; |
| 1787 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1788 | if (PyIndex_Check(item)) { |
| 1789 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1790 | |
| 1791 | if (i == -1 && PyErr_Occurred()) { |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | if (i < 0 && self->extra) |
| 1795 | i += self->extra->length; |
| 1796 | return element_getitem(self_, i); |
| 1797 | } |
| 1798 | else if (PySlice_Check(item)) { |
| 1799 | Py_ssize_t start, stop, step, slicelen, cur, i; |
| 1800 | PyObject* list; |
| 1801 | |
| 1802 | if (!self->extra) |
| 1803 | return PyList_New(0); |
| 1804 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1805 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1806 | return NULL; |
| 1807 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1808 | slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop, |
| 1809 | step); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (slicelen <= 0) |
| 1812 | return PyList_New(0); |
| 1813 | else { |
| 1814 | list = PyList_New(slicelen); |
| 1815 | if (!list) |
| 1816 | return NULL; |
| 1817 | |
| 1818 | for (cur = start, i = 0; i < slicelen; |
| 1819 | cur += step, i++) { |
| 1820 | PyObject* item = self->extra->children[cur]; |
| 1821 | Py_INCREF(item); |
| 1822 | PyList_SET_ITEM(list, i, item); |
| 1823 | } |
| 1824 | |
| 1825 | return list; |
| 1826 | } |
| 1827 | } |
| 1828 | else { |
| 1829 | PyErr_SetString(PyExc_TypeError, |
| 1830 | "element indices must be integers"); |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | static int |
| 1836 | element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) |
| 1837 | { |
| 1838 | ElementObject* self = (ElementObject*) self_; |
| 1839 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1840 | if (PyIndex_Check(item)) { |
| 1841 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1842 | |
| 1843 | if (i == -1 && PyErr_Occurred()) { |
| 1844 | return -1; |
| 1845 | } |
| 1846 | if (i < 0 && self->extra) |
| 1847 | i += self->extra->length; |
| 1848 | return element_setitem(self_, i, value); |
| 1849 | } |
| 1850 | else if (PySlice_Check(item)) { |
| 1851 | Py_ssize_t start, stop, step, slicelen, newlen, cur, i; |
| 1852 | |
| 1853 | PyObject* recycle = NULL; |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 1854 | PyObject* seq; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1855 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1856 | if (!self->extra) { |
| 1857 | if (create_extra(self, NULL) < 0) |
| 1858 | return -1; |
| 1859 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1860 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1861 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1862 | return -1; |
| 1863 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 1864 | slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop, |
| 1865 | step); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1866 | |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1867 | if (value == NULL) { |
| 1868 | /* Delete slice */ |
| 1869 | size_t cur; |
| 1870 | Py_ssize_t i; |
| 1871 | |
| 1872 | if (slicelen <= 0) |
| 1873 | return 0; |
| 1874 | |
| 1875 | /* Since we're deleting, the direction of the range doesn't matter, |
| 1876 | * so for simplicity make it always ascending. |
| 1877 | */ |
| 1878 | if (step < 0) { |
| 1879 | stop = start + 1; |
| 1880 | start = stop + step * (slicelen - 1) - 1; |
| 1881 | step = -step; |
| 1882 | } |
| 1883 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 1884 | assert((size_t)slicelen <= SIZE_MAX / sizeof(PyObject *)); |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1885 | |
| 1886 | /* recycle is a list that will contain all the children |
| 1887 | * scheduled for removal. |
| 1888 | */ |
| 1889 | if (!(recycle = PyList_New(slicelen))) { |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1890 | return -1; |
| 1891 | } |
| 1892 | |
| 1893 | /* This loop walks over all the children that have to be deleted, |
| 1894 | * with cur pointing at them. num_moved is the amount of children |
| 1895 | * until the next deleted child that have to be "shifted down" to |
| 1896 | * occupy the deleted's places. |
| 1897 | * Note that in the ith iteration, shifting is done i+i places down |
| 1898 | * because i children were already removed. |
| 1899 | */ |
| 1900 | for (cur = start, i = 0; cur < (size_t)stop; cur += step, ++i) { |
| 1901 | /* Compute how many children have to be moved, clipping at the |
| 1902 | * list end. |
| 1903 | */ |
| 1904 | Py_ssize_t num_moved = step - 1; |
| 1905 | if (cur + step >= (size_t)self->extra->length) { |
| 1906 | num_moved = self->extra->length - cur - 1; |
| 1907 | } |
| 1908 | |
| 1909 | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1910 | |
| 1911 | memmove( |
| 1912 | self->extra->children + cur - i, |
| 1913 | self->extra->children + cur + 1, |
| 1914 | num_moved * sizeof(PyObject *)); |
| 1915 | } |
| 1916 | |
| 1917 | /* Leftover "tail" after the last removed child */ |
| 1918 | cur = start + (size_t)slicelen * step; |
| 1919 | if (cur < (size_t)self->extra->length) { |
| 1920 | memmove( |
| 1921 | self->extra->children + cur - slicelen, |
| 1922 | self->extra->children + cur, |
| 1923 | (self->extra->length - cur) * sizeof(PyObject *)); |
| 1924 | } |
| 1925 | |
| 1926 | self->extra->length -= slicelen; |
| 1927 | |
| 1928 | /* Discard the recycle list with all the deleted sub-elements */ |
Zackery Spytz | 9f3ed3e | 2018-10-23 13:28:06 -0600 | [diff] [blame] | 1929 | Py_DECREF(recycle); |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 1932 | |
| 1933 | /* A new slice is actually being assigned */ |
| 1934 | seq = PySequence_Fast(value, ""); |
| 1935 | if (!seq) { |
| 1936 | PyErr_Format( |
| 1937 | PyExc_TypeError, |
| 1938 | "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name |
| 1939 | ); |
| 1940 | return -1; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1941 | } |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 1942 | newlen = PySequence_Fast_GET_SIZE(seq); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1943 | |
| 1944 | if (step != 1 && newlen != slicelen) |
| 1945 | { |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 1946 | Py_DECREF(seq); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1947 | PyErr_Format(PyExc_ValueError, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1948 | "attempt to assign sequence of size %zd " |
| 1949 | "to extended slice of size %zd", |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1950 | newlen, slicelen |
| 1951 | ); |
| 1952 | return -1; |
| 1953 | } |
| 1954 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1955 | /* Resize before creating the recycle bin, to prevent refleaks. */ |
| 1956 | if (newlen > slicelen) { |
| 1957 | if (element_resize(self, newlen - slicelen) < 0) { |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 1958 | Py_DECREF(seq); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1959 | return -1; |
| 1960 | } |
| 1961 | } |
| 1962 | |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 1963 | for (i = 0; i < newlen; i++) { |
| 1964 | PyObject *element = PySequence_Fast_GET_ITEM(seq, i); |
| 1965 | if (!Element_Check(element)) { |
| 1966 | raise_type_error(element); |
| 1967 | Py_DECREF(seq); |
| 1968 | return -1; |
| 1969 | } |
| 1970 | } |
| 1971 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1972 | if (slicelen > 0) { |
| 1973 | /* to avoid recursive calls to this method (via decref), move |
| 1974 | old items to the recycle bin here, and get rid of them when |
| 1975 | we're done modifying the element */ |
| 1976 | recycle = PyList_New(slicelen); |
| 1977 | if (!recycle) { |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 1978 | Py_DECREF(seq); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1979 | return -1; |
| 1980 | } |
| 1981 | for (cur = start, i = 0; i < slicelen; |
| 1982 | cur += step, i++) |
| 1983 | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1984 | } |
| 1985 | |
| 1986 | if (newlen < slicelen) { |
| 1987 | /* delete slice */ |
| 1988 | for (i = stop; i < self->extra->length; i++) |
| 1989 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1990 | } else if (newlen > slicelen) { |
| 1991 | /* insert slice */ |
| 1992 | for (i = self->extra->length-1; i >= stop; i--) |
| 1993 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1994 | } |
| 1995 | |
| 1996 | /* replace the slice */ |
| 1997 | for (cur = start, i = 0; i < newlen; |
| 1998 | cur += step, i++) { |
| 1999 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 2000 | Py_INCREF(element); |
| 2001 | self->extra->children[cur] = element; |
| 2002 | } |
| 2003 | |
| 2004 | self->extra->length += newlen - slicelen; |
| 2005 | |
Serhiy Storchaka | 04d759b | 2015-11-22 12:18:38 +0200 | [diff] [blame] | 2006 | Py_DECREF(seq); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2007 | |
| 2008 | /* discard the recycle bin, and everything in it */ |
| 2009 | Py_XDECREF(recycle); |
| 2010 | |
| 2011 | return 0; |
| 2012 | } |
| 2013 | else { |
| 2014 | PyErr_SetString(PyExc_TypeError, |
| 2015 | "element indices must be integers"); |
| 2016 | return -1; |
| 2017 | } |
| 2018 | } |
| 2019 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2020 | static PyObject* |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2021 | element_tag_getter(ElementObject *self, void *closure) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2022 | { |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2023 | PyObject *res = self->tag; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2024 | Py_INCREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2025 | return res; |
| 2026 | } |
| 2027 | |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2028 | static PyObject* |
| 2029 | element_text_getter(ElementObject *self, void *closure) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2030 | { |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2031 | PyObject *res = element_get_text(self); |
| 2032 | Py_XINCREF(res); |
| 2033 | return res; |
| 2034 | } |
Serhiy Storchaka | b6aa537 | 2015-11-23 08:42:25 +0200 | [diff] [blame] | 2035 | |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2036 | static PyObject* |
| 2037 | element_tail_getter(ElementObject *self, void *closure) |
| 2038 | { |
| 2039 | PyObject *res = element_get_tail(self); |
| 2040 | Py_XINCREF(res); |
| 2041 | return res; |
| 2042 | } |
| 2043 | |
| 2044 | static PyObject* |
| 2045 | element_attrib_getter(ElementObject *self, void *closure) |
| 2046 | { |
| 2047 | PyObject *res; |
| 2048 | if (!self->extra) { |
| 2049 | if (create_extra(self, NULL) < 0) |
| 2050 | return NULL; |
Serhiy Storchaka | b6aa537 | 2015-11-23 08:42:25 +0200 | [diff] [blame] | 2051 | } |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2052 | res = element_get_attrib(self); |
| 2053 | Py_XINCREF(res); |
| 2054 | return res; |
| 2055 | } |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 2056 | |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2057 | /* macro for setter validation */ |
| 2058 | #define _VALIDATE_ATTR_VALUE(V) \ |
| 2059 | if ((V) == NULL) { \ |
| 2060 | PyErr_SetString( \ |
| 2061 | PyExc_AttributeError, \ |
| 2062 | "can't delete element attribute"); \ |
| 2063 | return -1; \ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2066 | static int |
| 2067 | element_tag_setter(ElementObject *self, PyObject *value, void *closure) |
| 2068 | { |
| 2069 | _VALIDATE_ATTR_VALUE(value); |
| 2070 | Py_INCREF(value); |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 2071 | Py_SETREF(self->tag, value); |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static int |
| 2076 | element_text_setter(ElementObject *self, PyObject *value, void *closure) |
| 2077 | { |
| 2078 | _VALIDATE_ATTR_VALUE(value); |
| 2079 | Py_INCREF(value); |
Oren Milman | 39ecb9c | 2017-10-10 23:26:24 +0300 | [diff] [blame] | 2080 | _set_joined_ptr(&self->text, value); |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | static int |
| 2085 | element_tail_setter(ElementObject *self, PyObject *value, void *closure) |
| 2086 | { |
| 2087 | _VALIDATE_ATTR_VALUE(value); |
| 2088 | Py_INCREF(value); |
Oren Milman | 39ecb9c | 2017-10-10 23:26:24 +0300 | [diff] [blame] | 2089 | _set_joined_ptr(&self->tail, value); |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | static int |
| 2094 | element_attrib_setter(ElementObject *self, PyObject *value, void *closure) |
| 2095 | { |
| 2096 | _VALIDATE_ATTR_VALUE(value); |
| 2097 | if (!self->extra) { |
| 2098 | if (create_extra(self, NULL) < 0) |
| 2099 | return -1; |
| 2100 | } |
| 2101 | Py_INCREF(value); |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 2102 | Py_SETREF(self->extra->attrib, value); |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 2103 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | static PySequenceMethods element_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2107 | (lenfunc) element_length, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2108 | 0, /* sq_concat */ |
| 2109 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2110 | element_getitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2111 | 0, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2112 | element_setitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2113 | 0, |
| 2114 | }; |
| 2115 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2116 | /******************************* Element iterator ****************************/ |
| 2117 | |
| 2118 | /* ElementIterObject represents the iteration state over an XML element in |
| 2119 | * pre-order traversal. To keep track of which sub-element should be returned |
| 2120 | * next, a stack of parents is maintained. This is a standard stack-based |
| 2121 | * iterative pre-order traversal of a tree. |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2122 | * The stack is managed using a continuous array. |
| 2123 | * 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] | 2124 | * the current one is exhausted, and the next child to examine in that parent. |
| 2125 | */ |
| 2126 | typedef struct ParentLocator_t { |
| 2127 | ElementObject *parent; |
| 2128 | Py_ssize_t child_index; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2129 | } ParentLocator; |
| 2130 | |
| 2131 | typedef struct { |
| 2132 | PyObject_HEAD |
| 2133 | ParentLocator *parent_stack; |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2134 | Py_ssize_t parent_stack_used; |
| 2135 | Py_ssize_t parent_stack_size; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2136 | ElementObject *root_element; |
| 2137 | PyObject *sought_tag; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2138 | int gettext; |
| 2139 | } ElementIterObject; |
| 2140 | |
| 2141 | |
| 2142 | static void |
| 2143 | elementiter_dealloc(ElementIterObject *it) |
| 2144 | { |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2145 | Py_ssize_t i = it->parent_stack_used; |
| 2146 | it->parent_stack_used = 0; |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 2147 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 2148 | PyObject_GC_UnTrack(it); |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2149 | while (i--) |
| 2150 | Py_XDECREF(it->parent_stack[i].parent); |
| 2151 | PyMem_Free(it->parent_stack); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2152 | |
| 2153 | Py_XDECREF(it->sought_tag); |
| 2154 | Py_XDECREF(it->root_element); |
| 2155 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2156 | PyObject_GC_Del(it); |
| 2157 | } |
| 2158 | |
| 2159 | static int |
| 2160 | elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg) |
| 2161 | { |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2162 | Py_ssize_t i = it->parent_stack_used; |
| 2163 | while (i--) |
| 2164 | Py_VISIT(it->parent_stack[i].parent); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2165 | |
| 2166 | Py_VISIT(it->root_element); |
| 2167 | Py_VISIT(it->sought_tag); |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | /* Helper function for elementiter_next. Add a new parent to the parent stack. |
| 2172 | */ |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2173 | static int |
| 2174 | parent_stack_push_new(ElementIterObject *it, ElementObject *parent) |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2175 | { |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2176 | ParentLocator *item; |
| 2177 | |
| 2178 | if (it->parent_stack_used >= it->parent_stack_size) { |
| 2179 | Py_ssize_t new_size = it->parent_stack_size * 2; /* never overflow */ |
| 2180 | ParentLocator *parent_stack = it->parent_stack; |
| 2181 | PyMem_Resize(parent_stack, ParentLocator, new_size); |
| 2182 | if (parent_stack == NULL) |
| 2183 | return -1; |
| 2184 | it->parent_stack = parent_stack; |
| 2185 | it->parent_stack_size = new_size; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2186 | } |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2187 | item = it->parent_stack + it->parent_stack_used++; |
| 2188 | Py_INCREF(parent); |
| 2189 | item->parent = parent; |
| 2190 | item->child_index = 0; |
| 2191 | return 0; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | static PyObject * |
| 2195 | elementiter_next(ElementIterObject *it) |
| 2196 | { |
| 2197 | /* Sub-element iterator. |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 2198 | * |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2199 | * A short note on gettext: this function serves both the iter() and |
| 2200 | * itertext() methods to avoid code duplication. However, there are a few |
| 2201 | * small differences in the way these iterations work. Namely: |
| 2202 | * - itertext() only yields text from nodes that have it, and continues |
| 2203 | * iterating when a node doesn't have text (so it doesn't return any |
| 2204 | * node like iter()) |
| 2205 | * - itertext() also has to handle tail, after finishing with all the |
| 2206 | * children of a node. |
| 2207 | */ |
Serhiy Storchaka | 5bf3120 | 2015-05-18 18:29:33 +0300 | [diff] [blame] | 2208 | int rc; |
Serhiy Storchaka | 66c08d9 | 2015-12-21 11:09:48 +0200 | [diff] [blame] | 2209 | ElementObject *elem; |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2210 | PyObject *text; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2211 | |
| 2212 | while (1) { |
| 2213 | /* Handle the case reached in the beginning and end of iteration, where |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2214 | * 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] | 2215 | * iterator is exhausted. |
| 2216 | */ |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2217 | if (!it->parent_stack_used) { |
| 2218 | if (!it->root_element) { |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2219 | PyErr_SetNone(PyExc_StopIteration); |
| 2220 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2221 | } |
| 2222 | |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2223 | elem = it->root_element; /* steals a reference */ |
| 2224 | it->root_element = NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2225 | } |
| 2226 | else { |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2227 | /* See if there are children left to traverse in the current parent. If |
| 2228 | * 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] | 2229 | */ |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2230 | ParentLocator *item = &it->parent_stack[it->parent_stack_used - 1]; |
| 2231 | Py_ssize_t child_index = item->child_index; |
| 2232 | ElementObjectExtra *extra; |
| 2233 | elem = item->parent; |
| 2234 | extra = elem->extra; |
| 2235 | if (!extra || child_index >= extra->length) { |
| 2236 | it->parent_stack_used--; |
| 2237 | /* Note that extra condition on it->parent_stack_used here; |
| 2238 | * this is because itertext() is supposed to only return *inner* |
| 2239 | * text, not text following the element it began iteration with. |
| 2240 | */ |
| 2241 | if (it->gettext && it->parent_stack_used) { |
| 2242 | text = element_get_tail(elem); |
| 2243 | goto gettext; |
| 2244 | } |
| 2245 | Py_DECREF(elem); |
| 2246 | continue; |
Serhiy Storchaka | 66c08d9 | 2015-12-21 11:09:48 +0200 | [diff] [blame] | 2247 | } |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2248 | |
Serhiy Storchaka | f081fd8 | 2018-10-19 12:12:57 +0300 | [diff] [blame] | 2249 | assert(Element_Check(extra->children[child_index])); |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2250 | elem = (ElementObject *)extra->children[child_index]; |
| 2251 | item->child_index++; |
| 2252 | Py_INCREF(elem); |
| 2253 | } |
| 2254 | |
| 2255 | if (parent_stack_push_new(it, elem) < 0) { |
| 2256 | Py_DECREF(elem); |
| 2257 | PyErr_NoMemory(); |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | if (it->gettext) { |
| 2261 | text = element_get_text(elem); |
| 2262 | goto gettext; |
| 2263 | } |
| 2264 | |
| 2265 | if (it->sought_tag == Py_None) |
| 2266 | return (PyObject *)elem; |
| 2267 | |
| 2268 | rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ); |
| 2269 | if (rc > 0) |
| 2270 | return (PyObject *)elem; |
| 2271 | |
| 2272 | Py_DECREF(elem); |
| 2273 | if (rc < 0) |
| 2274 | return NULL; |
| 2275 | continue; |
| 2276 | |
| 2277 | gettext: |
| 2278 | if (!text) { |
| 2279 | Py_DECREF(elem); |
| 2280 | return NULL; |
| 2281 | } |
| 2282 | if (text == Py_None) { |
| 2283 | Py_DECREF(elem); |
| 2284 | } |
| 2285 | else { |
| 2286 | Py_INCREF(text); |
| 2287 | Py_DECREF(elem); |
| 2288 | rc = PyObject_IsTrue(text); |
| 2289 | if (rc > 0) |
| 2290 | return text; |
| 2291 | Py_DECREF(text); |
| 2292 | if (rc < 0) |
| 2293 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | return NULL; |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | static PyTypeObject ElementIter_Type = { |
| 2302 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 2303 | /* Using the module's name since the pure-Python implementation does not |
| 2304 | have such a type. */ |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2305 | "_elementtree._element_iterator", /* tp_name */ |
| 2306 | sizeof(ElementIterObject), /* tp_basicsize */ |
| 2307 | 0, /* tp_itemsize */ |
| 2308 | /* methods */ |
| 2309 | (destructor)elementiter_dealloc, /* tp_dealloc */ |
| 2310 | 0, /* tp_print */ |
| 2311 | 0, /* tp_getattr */ |
| 2312 | 0, /* tp_setattr */ |
| 2313 | 0, /* tp_reserved */ |
| 2314 | 0, /* tp_repr */ |
| 2315 | 0, /* tp_as_number */ |
| 2316 | 0, /* tp_as_sequence */ |
| 2317 | 0, /* tp_as_mapping */ |
| 2318 | 0, /* tp_hash */ |
| 2319 | 0, /* tp_call */ |
| 2320 | 0, /* tp_str */ |
| 2321 | 0, /* tp_getattro */ |
| 2322 | 0, /* tp_setattro */ |
| 2323 | 0, /* tp_as_buffer */ |
| 2324 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2325 | 0, /* tp_doc */ |
| 2326 | (traverseproc)elementiter_traverse, /* tp_traverse */ |
| 2327 | 0, /* tp_clear */ |
| 2328 | 0, /* tp_richcompare */ |
| 2329 | 0, /* tp_weaklistoffset */ |
| 2330 | PyObject_SelfIter, /* tp_iter */ |
| 2331 | (iternextfunc)elementiter_next, /* tp_iternext */ |
| 2332 | 0, /* tp_methods */ |
| 2333 | 0, /* tp_members */ |
| 2334 | 0, /* tp_getset */ |
| 2335 | 0, /* tp_base */ |
| 2336 | 0, /* tp_dict */ |
| 2337 | 0, /* tp_descr_get */ |
| 2338 | 0, /* tp_descr_set */ |
| 2339 | 0, /* tp_dictoffset */ |
| 2340 | 0, /* tp_init */ |
| 2341 | 0, /* tp_alloc */ |
| 2342 | 0, /* tp_new */ |
| 2343 | }; |
| 2344 | |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2345 | #define INIT_PARENT_STACK_SIZE 8 |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2346 | |
| 2347 | static PyObject * |
| 2348 | create_elementiter(ElementObject *self, PyObject *tag, int gettext) |
| 2349 | { |
| 2350 | ElementIterObject *it; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2351 | |
| 2352 | it = PyObject_GC_New(ElementIterObject, &ElementIter_Type); |
| 2353 | if (!it) |
| 2354 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2355 | |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 2356 | Py_INCREF(tag); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2357 | it->sought_tag = tag; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2358 | it->gettext = gettext; |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 2359 | Py_INCREF(self); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2360 | it->root_element = self; |
| 2361 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2362 | PyObject_GC_Track(it); |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 2363 | |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2364 | it->parent_stack = PyMem_New(ParentLocator, INIT_PARENT_STACK_SIZE); |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 2365 | if (it->parent_stack == NULL) { |
| 2366 | Py_DECREF(it); |
| 2367 | PyErr_NoMemory(); |
| 2368 | return NULL; |
| 2369 | } |
Serhiy Storchaka | 22adf2a | 2015-12-21 12:43:54 +0200 | [diff] [blame] | 2370 | it->parent_stack_used = 0; |
| 2371 | it->parent_stack_size = INIT_PARENT_STACK_SIZE; |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 2372 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2373 | return (PyObject *)it; |
| 2374 | } |
| 2375 | |
| 2376 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2377 | /* ==================================================================== */ |
| 2378 | /* the tree builder type */ |
| 2379 | |
| 2380 | typedef struct { |
| 2381 | PyObject_HEAD |
| 2382 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2383 | PyObject *root; /* root node (first created node) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2384 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2385 | PyObject *this; /* current node */ |
| 2386 | PyObject *last; /* most recently created node */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2387 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2388 | PyObject *data; /* data collector (string or list), or NULL */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2389 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2390 | PyObject *stack; /* element stack */ |
| 2391 | Py_ssize_t index; /* current stack size (0 means empty) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2392 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2393 | PyObject *element_factory; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2394 | PyObject *comment_factory; |
| 2395 | PyObject *pi_factory; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2396 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2397 | /* element tracing */ |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 2398 | 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] | 2399 | PyObject *start_event_obj; /* event objects (NULL to ignore) */ |
| 2400 | PyObject *end_event_obj; |
| 2401 | PyObject *start_ns_event_obj; |
| 2402 | PyObject *end_ns_event_obj; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2403 | PyObject *comment_event_obj; |
| 2404 | PyObject *pi_event_obj; |
| 2405 | |
| 2406 | char insert_comments; |
| 2407 | char insert_pis; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2408 | } TreeBuilderObject; |
| 2409 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2410 | #define TreeBuilder_CheckExact(op) (Py_TYPE(op) == &TreeBuilder_Type) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2411 | |
| 2412 | /* -------------------------------------------------------------------- */ |
| 2413 | /* constructor and destructor */ |
| 2414 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2415 | static PyObject * |
| 2416 | treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2417 | { |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2418 | TreeBuilderObject *t = (TreeBuilderObject *)type->tp_alloc(type, 0); |
| 2419 | if (t != NULL) { |
| 2420 | t->root = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2421 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2422 | Py_INCREF(Py_None); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2423 | t->this = Py_None; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2424 | Py_INCREF(Py_None); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2425 | t->last = Py_None; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2426 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2427 | t->data = NULL; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2428 | t->element_factory = NULL; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2429 | t->comment_factory = NULL; |
| 2430 | t->pi_factory = NULL; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2431 | t->stack = PyList_New(20); |
| 2432 | if (!t->stack) { |
| 2433 | Py_DECREF(t->this); |
| 2434 | Py_DECREF(t->last); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2435 | Py_DECREF((PyObject *) t); |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2436 | return NULL; |
| 2437 | } |
| 2438 | t->index = 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2439 | |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 2440 | t->events_append = NULL; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2441 | t->start_event_obj = t->end_event_obj = NULL; |
| 2442 | t->start_ns_event_obj = t->end_ns_event_obj = NULL; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2443 | t->comment_event_obj = t->pi_event_obj = NULL; |
| 2444 | t->insert_comments = t->insert_pis = 0; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2445 | } |
| 2446 | return (PyObject *)t; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2449 | /*[clinic input] |
| 2450 | _elementtree.TreeBuilder.__init__ |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2451 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2452 | element_factory: object = NULL |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2453 | * |
| 2454 | comment_factory: object = NULL |
| 2455 | pi_factory: object = NULL |
| 2456 | insert_comments: bool = False |
| 2457 | insert_pis: bool = False |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2458 | |
| 2459 | [clinic start generated code]*/ |
| 2460 | |
| 2461 | static int |
| 2462 | _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self, |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2463 | PyObject *element_factory, |
| 2464 | PyObject *comment_factory, |
| 2465 | PyObject *pi_factory, |
| 2466 | int insert_comments, int insert_pis) |
| 2467 | /*[clinic end generated code: output=8571d4dcadfdf952 input=1f967b5c245e0a71]*/ |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2468 | { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2469 | if (element_factory && element_factory != Py_None) { |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2470 | Py_INCREF(element_factory); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 2471 | Py_XSETREF(self->element_factory, element_factory); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2472 | } else { |
| 2473 | Py_CLEAR(self->element_factory); |
| 2474 | } |
| 2475 | |
| 2476 | if (!comment_factory || comment_factory == Py_None) { |
| 2477 | elementtreestate *st = ET_STATE_GLOBAL; |
| 2478 | comment_factory = st->comment_factory; |
| 2479 | } |
| 2480 | if (comment_factory) { |
| 2481 | Py_INCREF(comment_factory); |
| 2482 | Py_XSETREF(self->comment_factory, comment_factory); |
| 2483 | self->insert_comments = insert_comments; |
| 2484 | } else { |
| 2485 | Py_CLEAR(self->comment_factory); |
| 2486 | self->insert_comments = 0; |
| 2487 | } |
| 2488 | |
| 2489 | if (!pi_factory || pi_factory == Py_None) { |
| 2490 | elementtreestate *st = ET_STATE_GLOBAL; |
| 2491 | pi_factory = st->pi_factory; |
| 2492 | } |
| 2493 | if (pi_factory) { |
| 2494 | Py_INCREF(pi_factory); |
| 2495 | Py_XSETREF(self->pi_factory, pi_factory); |
| 2496 | self->insert_pis = insert_pis; |
| 2497 | } else { |
| 2498 | Py_CLEAR(self->pi_factory); |
| 2499 | self->insert_pis = 0; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2500 | } |
| 2501 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2502 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2505 | static int |
| 2506 | treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg) |
| 2507 | { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2508 | Py_VISIT(self->pi_event_obj); |
| 2509 | Py_VISIT(self->comment_event_obj); |
Serhiy Storchaka | d2a75c6 | 2018-12-18 22:29:14 +0200 | [diff] [blame] | 2510 | Py_VISIT(self->end_ns_event_obj); |
| 2511 | Py_VISIT(self->start_ns_event_obj); |
| 2512 | Py_VISIT(self->end_event_obj); |
| 2513 | Py_VISIT(self->start_event_obj); |
| 2514 | Py_VISIT(self->events_append); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2515 | Py_VISIT(self->root); |
| 2516 | Py_VISIT(self->this); |
| 2517 | Py_VISIT(self->last); |
| 2518 | Py_VISIT(self->data); |
| 2519 | Py_VISIT(self->stack); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2520 | Py_VISIT(self->pi_factory); |
| 2521 | Py_VISIT(self->comment_factory); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2522 | Py_VISIT(self->element_factory); |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | static int |
| 2527 | treebuilder_gc_clear(TreeBuilderObject *self) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2528 | { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2529 | Py_CLEAR(self->pi_event_obj); |
| 2530 | Py_CLEAR(self->comment_event_obj); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2531 | Py_CLEAR(self->end_ns_event_obj); |
| 2532 | Py_CLEAR(self->start_ns_event_obj); |
| 2533 | Py_CLEAR(self->end_event_obj); |
| 2534 | Py_CLEAR(self->start_event_obj); |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 2535 | Py_CLEAR(self->events_append); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2536 | Py_CLEAR(self->stack); |
| 2537 | Py_CLEAR(self->data); |
| 2538 | Py_CLEAR(self->last); |
| 2539 | Py_CLEAR(self->this); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2540 | Py_CLEAR(self->pi_factory); |
| 2541 | Py_CLEAR(self->comment_factory); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2542 | Py_CLEAR(self->element_factory); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2543 | Py_CLEAR(self->root); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2544 | return 0; |
| 2545 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2546 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2547 | static void |
| 2548 | treebuilder_dealloc(TreeBuilderObject *self) |
| 2549 | { |
| 2550 | PyObject_GC_UnTrack(self); |
| 2551 | treebuilder_gc_clear(self); |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2552 | Py_TYPE(self)->tp_free((PyObject *)self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /* -------------------------------------------------------------------- */ |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2556 | /* helpers for handling of arbitrary element-like objects */ |
| 2557 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2558 | /*[clinic input] |
| 2559 | _elementtree._set_factories |
| 2560 | |
| 2561 | comment_factory: object |
| 2562 | pi_factory: object |
| 2563 | / |
| 2564 | |
| 2565 | Change the factories used to create comments and processing instructions. |
| 2566 | |
| 2567 | For internal use only. |
| 2568 | [clinic start generated code]*/ |
| 2569 | |
| 2570 | static PyObject * |
| 2571 | _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory, |
| 2572 | PyObject *pi_factory) |
| 2573 | /*[clinic end generated code: output=813b408adee26535 input=99d17627aea7fb3b]*/ |
| 2574 | { |
| 2575 | elementtreestate *st = ET_STATE_GLOBAL; |
| 2576 | PyObject *old; |
| 2577 | |
| 2578 | if (!PyCallable_Check(comment_factory) && comment_factory != Py_None) { |
| 2579 | PyErr_Format(PyExc_TypeError, "Comment factory must be callable, not %.100s", |
| 2580 | Py_TYPE(comment_factory)->tp_name); |
| 2581 | return NULL; |
| 2582 | } |
| 2583 | if (!PyCallable_Check(pi_factory) && pi_factory != Py_None) { |
| 2584 | PyErr_Format(PyExc_TypeError, "PI factory must be callable, not %.100s", |
| 2585 | Py_TYPE(pi_factory)->tp_name); |
| 2586 | return NULL; |
| 2587 | } |
| 2588 | |
| 2589 | old = PyTuple_Pack(2, |
| 2590 | st->comment_factory ? st->comment_factory : Py_None, |
| 2591 | st->pi_factory ? st->pi_factory : Py_None); |
| 2592 | |
| 2593 | if (comment_factory == Py_None) { |
| 2594 | Py_CLEAR(st->comment_factory); |
| 2595 | } else { |
| 2596 | Py_INCREF(comment_factory); |
| 2597 | Py_XSETREF(st->comment_factory, comment_factory); |
| 2598 | } |
| 2599 | if (pi_factory == Py_None) { |
| 2600 | Py_CLEAR(st->pi_factory); |
| 2601 | } else { |
| 2602 | Py_INCREF(pi_factory); |
| 2603 | Py_XSETREF(st->pi_factory, pi_factory); |
| 2604 | } |
| 2605 | |
| 2606 | return old; |
| 2607 | } |
| 2608 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2609 | static int |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2610 | treebuilder_set_element_text_or_tail(PyObject *element, PyObject **data, |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2611 | PyObject **dest, _Py_Identifier *name) |
| 2612 | { |
| 2613 | if (Element_CheckExact(element)) { |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2614 | PyObject *tmp = JOIN_OBJ(*dest); |
| 2615 | *dest = JOIN_SET(*data, PyList_CheckExact(*data)); |
| 2616 | *data = NULL; |
| 2617 | Py_DECREF(tmp); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2618 | return 0; |
| 2619 | } |
| 2620 | else { |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2621 | PyObject *joined = list_join(*data); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2622 | int r; |
| 2623 | if (joined == NULL) |
| 2624 | return -1; |
| 2625 | r = _PyObject_SetAttrId(element, name, joined); |
| 2626 | Py_DECREF(joined); |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2627 | if (r < 0) |
| 2628 | return -1; |
| 2629 | Py_CLEAR(*data); |
| 2630 | return 0; |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2634 | LOCAL(int) |
| 2635 | treebuilder_flush_data(TreeBuilderObject* self) |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2636 | { |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2637 | PyObject *element = self->last; |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2638 | |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2639 | if (!self->data) { |
| 2640 | return 0; |
| 2641 | } |
| 2642 | |
| 2643 | if (self->this == element) { |
| 2644 | _Py_IDENTIFIER(text); |
| 2645 | return treebuilder_set_element_text_or_tail( |
| 2646 | element, &self->data, |
| 2647 | &((ElementObject *) element)->text, &PyId_text); |
| 2648 | } |
| 2649 | else { |
| 2650 | _Py_IDENTIFIER(tail); |
| 2651 | return treebuilder_set_element_text_or_tail( |
| 2652 | element, &self->data, |
| 2653 | &((ElementObject *) element)->tail, &PyId_tail); |
| 2654 | } |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | static int |
| 2658 | treebuilder_add_subelement(PyObject *element, PyObject *child) |
| 2659 | { |
| 2660 | _Py_IDENTIFIER(append); |
| 2661 | if (Element_CheckExact(element)) { |
| 2662 | ElementObject *elem = (ElementObject *) element; |
| 2663 | return element_add_subelement(elem, child); |
| 2664 | } |
| 2665 | else { |
| 2666 | PyObject *res; |
Victor Stinner | f561634 | 2016-12-09 15:26:00 +0100 | [diff] [blame] | 2667 | res = _PyObject_CallMethodIdObjArgs(element, &PyId_append, child, NULL); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2668 | if (res == NULL) |
| 2669 | return -1; |
| 2670 | Py_DECREF(res); |
| 2671 | return 0; |
| 2672 | } |
| 2673 | } |
| 2674 | |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 2675 | LOCAL(int) |
| 2676 | treebuilder_append_event(TreeBuilderObject *self, PyObject *action, |
| 2677 | PyObject *node) |
| 2678 | { |
| 2679 | if (action != NULL) { |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 2680 | PyObject *res; |
| 2681 | PyObject *event = PyTuple_Pack(2, action, node); |
| 2682 | if (event == NULL) |
| 2683 | return -1; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2684 | res = _PyObject_FastCall(self->events_append, &event, 1); |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 2685 | Py_DECREF(event); |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 2686 | if (res == NULL) |
| 2687 | return -1; |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 2688 | Py_DECREF(res); |
| 2689 | } |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2693 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2694 | /* handlers */ |
| 2695 | |
| 2696 | LOCAL(PyObject*) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2697 | treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, |
| 2698 | PyObject* attrib) |
| 2699 | { |
| 2700 | PyObject* node; |
| 2701 | PyObject* this; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2702 | elementtreestate *st = ET_STATE_GLOBAL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2703 | |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2704 | if (treebuilder_flush_data(self) < 0) { |
| 2705 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2708 | if (!self->element_factory) { |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2709 | node = create_new_element(tag, attrib); |
Serhiy Storchaka | 36ff997 | 2015-12-10 09:51:53 +0200 | [diff] [blame] | 2710 | } else if (attrib == Py_None) { |
| 2711 | attrib = PyDict_New(); |
| 2712 | if (!attrib) |
| 2713 | return NULL; |
Victor Stinner | 5abaa2b | 2016-12-09 16:22:32 +0100 | [diff] [blame] | 2714 | node = PyObject_CallFunctionObjArgs(self->element_factory, |
| 2715 | tag, attrib, NULL); |
Serhiy Storchaka | 36ff997 | 2015-12-10 09:51:53 +0200 | [diff] [blame] | 2716 | Py_DECREF(attrib); |
| 2717 | } |
| 2718 | else { |
Victor Stinner | 5abaa2b | 2016-12-09 16:22:32 +0100 | [diff] [blame] | 2719 | node = PyObject_CallFunctionObjArgs(self->element_factory, |
| 2720 | tag, attrib, NULL); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2721 | } |
| 2722 | if (!node) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2723 | return NULL; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2724 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2726 | this = self->this; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (this != Py_None) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2729 | if (treebuilder_add_subelement(this, node) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2730 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2731 | } else { |
| 2732 | if (self->root) { |
| 2733 | PyErr_SetString( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2734 | st->parseerror_obj, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2735 | "multiple elements on top level" |
| 2736 | ); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2737 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2738 | } |
| 2739 | Py_INCREF(node); |
| 2740 | self->root = node; |
| 2741 | } |
| 2742 | |
| 2743 | if (self->index < PyList_GET_SIZE(self->stack)) { |
| 2744 | if (PyList_SetItem(self->stack, self->index, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2745 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2746 | Py_INCREF(this); |
| 2747 | } else { |
| 2748 | if (PyList_Append(self->stack, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2749 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2750 | } |
| 2751 | self->index++; |
| 2752 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2753 | Py_INCREF(node); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2754 | Py_SETREF(self->this, node); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2755 | Py_INCREF(node); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2756 | Py_SETREF(self->last, node); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2757 | |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 2758 | if (treebuilder_append_event(self, self->start_event_obj, node) < 0) |
| 2759 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2760 | |
| 2761 | return node; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2762 | |
| 2763 | error: |
| 2764 | Py_DECREF(node); |
| 2765 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | LOCAL(PyObject*) |
| 2769 | treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) |
| 2770 | { |
| 2771 | if (!self->data) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2772 | if (self->last == Py_None) { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2773 | /* ignore calls to data before the first call to start */ |
| 2774 | Py_RETURN_NONE; |
| 2775 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2776 | /* store the first item as is */ |
| 2777 | Py_INCREF(data); self->data = data; |
| 2778 | } else { |
| 2779 | /* more than one item; use a list to collect items */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2780 | if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 && |
| 2781 | PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2782 | /* XXX this code path unused in Python 3? */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2783 | /* expat often generates single character data sections; handle |
| 2784 | the most common case by resizing the existing string... */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2785 | Py_ssize_t size = PyBytes_GET_SIZE(self->data); |
| 2786 | if (_PyBytes_Resize(&self->data, size + 1) < 0) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2787 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2788 | PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0]; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2789 | } else if (PyList_CheckExact(self->data)) { |
| 2790 | if (PyList_Append(self->data, data) < 0) |
| 2791 | return NULL; |
| 2792 | } else { |
| 2793 | PyObject* list = PyList_New(2); |
| 2794 | if (!list) |
| 2795 | return NULL; |
| 2796 | PyList_SET_ITEM(list, 0, self->data); |
| 2797 | Py_INCREF(data); PyList_SET_ITEM(list, 1, data); |
| 2798 | self->data = list; |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | Py_RETURN_NONE; |
| 2803 | } |
| 2804 | |
| 2805 | LOCAL(PyObject*) |
| 2806 | treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag) |
| 2807 | { |
| 2808 | PyObject* item; |
| 2809 | |
Serhiy Storchaka | 576def0 | 2017-03-30 09:47:31 +0300 | [diff] [blame] | 2810 | if (treebuilder_flush_data(self) < 0) { |
| 2811 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | if (self->index == 0) { |
| 2815 | PyErr_SetString( |
| 2816 | PyExc_IndexError, |
| 2817 | "pop from empty stack" |
| 2818 | ); |
| 2819 | return NULL; |
| 2820 | } |
| 2821 | |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 2822 | item = self->last; |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2823 | self->last = self->this; |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 2824 | self->index--; |
| 2825 | self->this = PyList_GET_ITEM(self->stack, self->index); |
| 2826 | Py_INCREF(self->this); |
| 2827 | Py_DECREF(item); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2828 | |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 2829 | if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0) |
| 2830 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2831 | |
| 2832 | Py_INCREF(self->last); |
| 2833 | return (PyObject*) self->last; |
| 2834 | } |
| 2835 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2836 | LOCAL(PyObject*) |
| 2837 | treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text) |
| 2838 | { |
| 2839 | PyObject* comment = NULL; |
| 2840 | PyObject* this; |
| 2841 | |
| 2842 | if (treebuilder_flush_data(self) < 0) { |
| 2843 | return NULL; |
| 2844 | } |
| 2845 | |
| 2846 | if (self->comment_factory) { |
| 2847 | comment = _PyObject_FastCall(self->comment_factory, &text, 1); |
| 2848 | if (!comment) |
| 2849 | return NULL; |
| 2850 | |
| 2851 | this = self->this; |
| 2852 | if (self->insert_comments && this != Py_None) { |
| 2853 | if (treebuilder_add_subelement(this, comment) < 0) |
| 2854 | goto error; |
| 2855 | } |
| 2856 | } else { |
| 2857 | Py_INCREF(text); |
| 2858 | comment = text; |
| 2859 | } |
| 2860 | |
| 2861 | if (self->events_append && self->comment_event_obj) { |
| 2862 | if (treebuilder_append_event(self, self->comment_event_obj, comment) < 0) |
| 2863 | goto error; |
| 2864 | } |
| 2865 | |
| 2866 | return comment; |
| 2867 | |
| 2868 | error: |
| 2869 | Py_DECREF(comment); |
| 2870 | return NULL; |
| 2871 | } |
| 2872 | |
| 2873 | LOCAL(PyObject*) |
| 2874 | treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text) |
| 2875 | { |
| 2876 | PyObject* pi = NULL; |
| 2877 | PyObject* this; |
| 2878 | PyObject* stack[2] = {target, text}; |
| 2879 | |
| 2880 | if (treebuilder_flush_data(self) < 0) { |
| 2881 | return NULL; |
| 2882 | } |
| 2883 | |
| 2884 | if (self->pi_factory) { |
| 2885 | pi = _PyObject_FastCall(self->pi_factory, stack, 2); |
| 2886 | if (!pi) { |
| 2887 | return NULL; |
| 2888 | } |
| 2889 | |
| 2890 | this = self->this; |
| 2891 | if (self->insert_pis && this != Py_None) { |
| 2892 | if (treebuilder_add_subelement(this, pi) < 0) |
| 2893 | goto error; |
| 2894 | } |
| 2895 | } else { |
| 2896 | pi = PyTuple_Pack(2, target, text); |
| 2897 | if (!pi) { |
| 2898 | return NULL; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | if (self->events_append && self->pi_event_obj) { |
| 2903 | if (treebuilder_append_event(self, self->pi_event_obj, pi) < 0) |
| 2904 | goto error; |
| 2905 | } |
| 2906 | |
| 2907 | return pi; |
| 2908 | |
| 2909 | error: |
| 2910 | Py_DECREF(pi); |
| 2911 | return NULL; |
| 2912 | } |
| 2913 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 2914 | LOCAL(PyObject*) |
| 2915 | treebuilder_handle_start_ns(TreeBuilderObject* self, PyObject* prefix, PyObject* uri) |
| 2916 | { |
| 2917 | PyObject* parcel; |
| 2918 | |
| 2919 | if (self->events_append && self->start_ns_event_obj) { |
| 2920 | parcel = PyTuple_Pack(2, prefix, uri); |
| 2921 | if (!parcel) { |
| 2922 | return NULL; |
| 2923 | } |
| 2924 | |
| 2925 | if (treebuilder_append_event(self, self->start_ns_event_obj, parcel) < 0) { |
| 2926 | Py_DECREF(parcel); |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | Py_DECREF(parcel); |
| 2930 | } |
| 2931 | |
| 2932 | Py_RETURN_NONE; |
| 2933 | } |
| 2934 | |
| 2935 | LOCAL(PyObject*) |
| 2936 | treebuilder_handle_end_ns(TreeBuilderObject* self, PyObject* prefix) |
| 2937 | { |
| 2938 | if (self->events_append && self->end_ns_event_obj) { |
| 2939 | if (treebuilder_append_event(self, self->end_ns_event_obj, prefix) < 0) { |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | Py_RETURN_NONE; |
| 2945 | } |
| 2946 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2947 | /* -------------------------------------------------------------------- */ |
| 2948 | /* methods (in alphabetical order) */ |
| 2949 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2950 | /*[clinic input] |
| 2951 | _elementtree.TreeBuilder.data |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2952 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2953 | data: object |
| 2954 | / |
| 2955 | |
| 2956 | [clinic start generated code]*/ |
| 2957 | |
| 2958 | static PyObject * |
| 2959 | _elementtree_TreeBuilder_data(TreeBuilderObject *self, PyObject *data) |
| 2960 | /*[clinic end generated code: output=69144c7100795bb2 input=a0540c532b284d29]*/ |
| 2961 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2962 | return treebuilder_handle_data(self, data); |
| 2963 | } |
| 2964 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2965 | /*[clinic input] |
| 2966 | _elementtree.TreeBuilder.end |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2967 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 2968 | tag: object |
| 2969 | / |
| 2970 | |
| 2971 | [clinic start generated code]*/ |
| 2972 | |
| 2973 | static PyObject * |
| 2974 | _elementtree_TreeBuilder_end(TreeBuilderObject *self, PyObject *tag) |
| 2975 | /*[clinic end generated code: output=9a98727cc691cd9d input=22dc3674236f5745]*/ |
| 2976 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2977 | return treebuilder_handle_end(self, tag); |
| 2978 | } |
| 2979 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 2980 | /*[clinic input] |
| 2981 | _elementtree.TreeBuilder.comment |
| 2982 | |
| 2983 | text: object |
| 2984 | / |
| 2985 | |
| 2986 | [clinic start generated code]*/ |
| 2987 | |
| 2988 | static PyObject * |
| 2989 | _elementtree_TreeBuilder_comment(TreeBuilderObject *self, PyObject *text) |
| 2990 | /*[clinic end generated code: output=22835be41deeaa27 input=47e7ebc48ed01dfa]*/ |
| 2991 | { |
| 2992 | return treebuilder_handle_comment(self, text); |
| 2993 | } |
| 2994 | |
| 2995 | /*[clinic input] |
| 2996 | _elementtree.TreeBuilder.pi |
| 2997 | |
| 2998 | target: object |
| 2999 | text: object = None |
| 3000 | / |
| 3001 | |
| 3002 | [clinic start generated code]*/ |
| 3003 | |
| 3004 | static PyObject * |
| 3005 | _elementtree_TreeBuilder_pi_impl(TreeBuilderObject *self, PyObject *target, |
| 3006 | PyObject *text) |
| 3007 | /*[clinic end generated code: output=21eb95ec9d04d1d9 input=349342bd79c35570]*/ |
| 3008 | { |
| 3009 | return treebuilder_handle_pi(self, target, text); |
| 3010 | } |
| 3011 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3012 | LOCAL(PyObject*) |
| 3013 | treebuilder_done(TreeBuilderObject* self) |
| 3014 | { |
| 3015 | PyObject* res; |
| 3016 | |
| 3017 | /* FIXME: check stack size? */ |
| 3018 | |
| 3019 | if (self->root) |
| 3020 | res = self->root; |
| 3021 | else |
| 3022 | res = Py_None; |
| 3023 | |
| 3024 | Py_INCREF(res); |
| 3025 | return res; |
| 3026 | } |
| 3027 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3028 | /*[clinic input] |
| 3029 | _elementtree.TreeBuilder.close |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3030 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3031 | [clinic start generated code]*/ |
| 3032 | |
| 3033 | static PyObject * |
| 3034 | _elementtree_TreeBuilder_close_impl(TreeBuilderObject *self) |
| 3035 | /*[clinic end generated code: output=b441fee3202f61ee input=f7c9c65dc718de14]*/ |
| 3036 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3037 | return treebuilder_done(self); |
| 3038 | } |
| 3039 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3040 | /*[clinic input] |
| 3041 | _elementtree.TreeBuilder.start |
| 3042 | |
| 3043 | tag: object |
| 3044 | attrs: object = None |
| 3045 | / |
| 3046 | |
| 3047 | [clinic start generated code]*/ |
| 3048 | |
| 3049 | static PyObject * |
| 3050 | _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag, |
| 3051 | PyObject *attrs) |
| 3052 | /*[clinic end generated code: output=e7e9dc2861349411 input=95fc1758dd042c65]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3053 | { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3054 | return treebuilder_handle_start(self, tag, attrs); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3057 | /* ==================================================================== */ |
| 3058 | /* the expat interface */ |
| 3059 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3060 | #include "expat.h" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3061 | #include "pyexpat.h" |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3062 | |
| 3063 | /* The PyExpat_CAPI structure is an immutable dispatch table, so it can be |
| 3064 | * cached globally without being in per-module state. |
| 3065 | */ |
Eli Bendersky | 20d4174 | 2012-06-01 09:48:37 +0300 | [diff] [blame] | 3066 | static struct PyExpat_CAPI *expat_capi; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3067 | #define EXPAT(func) (expat_capi->func) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3068 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3069 | static XML_Memory_Handling_Suite ExpatMemoryHandler = { |
| 3070 | PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
| 3071 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3072 | typedef struct { |
| 3073 | PyObject_HEAD |
| 3074 | |
| 3075 | XML_Parser parser; |
| 3076 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3077 | PyObject *target; |
| 3078 | PyObject *entity; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3079 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3080 | PyObject *names; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3081 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3082 | PyObject *handle_start_ns; |
| 3083 | PyObject *handle_end_ns; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3084 | PyObject *handle_start; |
| 3085 | PyObject *handle_data; |
| 3086 | PyObject *handle_end; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3087 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3088 | PyObject *handle_comment; |
| 3089 | PyObject *handle_pi; |
| 3090 | PyObject *handle_doctype; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3091 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3092 | PyObject *handle_close; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3093 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3094 | } XMLParserObject; |
| 3095 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3096 | /* helpers */ |
| 3097 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3098 | LOCAL(PyObject*) |
| 3099 | makeuniversal(XMLParserObject* self, const char* string) |
| 3100 | { |
| 3101 | /* convert a UTF-8 tag/attribute name from the expat parser |
| 3102 | to a universal name string */ |
| 3103 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3104 | Py_ssize_t size = (Py_ssize_t) strlen(string); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3105 | PyObject* key; |
| 3106 | PyObject* value; |
| 3107 | |
| 3108 | /* look the 'raw' name up in the names dictionary */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3109 | key = PyBytes_FromStringAndSize(string, size); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3110 | if (!key) |
| 3111 | return NULL; |
| 3112 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3113 | value = PyDict_GetItemWithError(self->names, key); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3114 | |
| 3115 | if (value) { |
| 3116 | Py_INCREF(value); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3117 | } |
| 3118 | else if (!PyErr_Occurred()) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3119 | /* new name. convert to universal name, and decode as |
| 3120 | necessary */ |
| 3121 | |
| 3122 | PyObject* tag; |
| 3123 | char* p; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3124 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3125 | |
| 3126 | /* look for namespace separator */ |
| 3127 | for (i = 0; i < size; i++) |
| 3128 | if (string[i] == '}') |
| 3129 | break; |
| 3130 | if (i != size) { |
| 3131 | /* convert to universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3132 | tag = PyBytes_FromStringAndSize(NULL, size+1); |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 3133 | if (tag == NULL) { |
| 3134 | Py_DECREF(key); |
| 3135 | return NULL; |
| 3136 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3137 | p = PyBytes_AS_STRING(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3138 | p[0] = '{'; |
| 3139 | memcpy(p+1, string, size); |
| 3140 | size++; |
| 3141 | } else { |
| 3142 | /* plain name; use key as tag */ |
| 3143 | Py_INCREF(key); |
| 3144 | tag = key; |
| 3145 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3146 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3147 | /* decode universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3148 | p = PyBytes_AS_STRING(tag); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3149 | value = PyUnicode_DecodeUTF8(p, size, "strict"); |
| 3150 | Py_DECREF(tag); |
| 3151 | if (!value) { |
| 3152 | Py_DECREF(key); |
| 3153 | return NULL; |
| 3154 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3155 | |
| 3156 | /* add to names dictionary */ |
| 3157 | if (PyDict_SetItem(self->names, key, value) < 0) { |
| 3158 | Py_DECREF(key); |
| 3159 | Py_DECREF(value); |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | Py_DECREF(key); |
| 3165 | return value; |
| 3166 | } |
| 3167 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3168 | /* Set the ParseError exception with the given parameters. |
| 3169 | * If message is not NULL, it's used as the error string. Otherwise, the |
| 3170 | * message string is the default for the given error_code. |
| 3171 | */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3172 | static void |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3173 | expat_set_error(enum XML_Error error_code, Py_ssize_t line, Py_ssize_t column, |
| 3174 | const char *message) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3175 | { |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3176 | PyObject *errmsg, *error, *position, *code; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3177 | elementtreestate *st = ET_STATE_GLOBAL; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3178 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3179 | errmsg = PyUnicode_FromFormat("%s: line %zd, column %zd", |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3180 | message ? message : EXPAT(ErrorString)(error_code), |
| 3181 | line, column); |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 3182 | if (errmsg == NULL) |
| 3183 | return; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3184 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3185 | error = _PyObject_FastCall(st->parseerror_obj, &errmsg, 1); |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 3186 | Py_DECREF(errmsg); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3187 | if (!error) |
| 3188 | return; |
| 3189 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3190 | /* Add code and position attributes */ |
| 3191 | code = PyLong_FromLong((long)error_code); |
| 3192 | if (!code) { |
| 3193 | Py_DECREF(error); |
| 3194 | return; |
| 3195 | } |
| 3196 | if (PyObject_SetAttrString(error, "code", code) == -1) { |
| 3197 | Py_DECREF(error); |
| 3198 | Py_DECREF(code); |
| 3199 | return; |
| 3200 | } |
| 3201 | Py_DECREF(code); |
| 3202 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3203 | position = Py_BuildValue("(nn)", line, column); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3204 | if (!position) { |
| 3205 | Py_DECREF(error); |
| 3206 | return; |
| 3207 | } |
| 3208 | if (PyObject_SetAttrString(error, "position", position) == -1) { |
| 3209 | Py_DECREF(error); |
| 3210 | Py_DECREF(position); |
| 3211 | return; |
| 3212 | } |
| 3213 | Py_DECREF(position); |
| 3214 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3215 | PyErr_SetObject(st->parseerror_obj, error); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3216 | Py_DECREF(error); |
| 3217 | } |
| 3218 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3219 | /* -------------------------------------------------------------------- */ |
| 3220 | /* handlers */ |
| 3221 | |
| 3222 | static void |
| 3223 | expat_default_handler(XMLParserObject* self, const XML_Char* data_in, |
| 3224 | int data_len) |
| 3225 | { |
| 3226 | PyObject* key; |
| 3227 | PyObject* value; |
| 3228 | PyObject* res; |
| 3229 | |
| 3230 | if (data_len < 2 || data_in[0] != '&') |
| 3231 | return; |
| 3232 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3233 | if (PyErr_Occurred()) |
| 3234 | return; |
| 3235 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3236 | key = PyUnicode_DecodeUTF8(data_in + 1, data_len - 2, "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3237 | if (!key) |
| 3238 | return; |
| 3239 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3240 | value = PyDict_GetItemWithError(self->entity, key); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3241 | |
| 3242 | if (value) { |
| 3243 | if (TreeBuilder_CheckExact(self->target)) |
| 3244 | res = treebuilder_handle_data( |
| 3245 | (TreeBuilderObject*) self->target, value |
| 3246 | ); |
| 3247 | else if (self->handle_data) |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3248 | res = _PyObject_FastCall(self->handle_data, &value, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3249 | else |
| 3250 | res = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3251 | Py_XDECREF(res); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3252 | } else if (!PyErr_Occurred()) { |
| 3253 | /* Report the first error, not the last */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 3254 | char message[128] = "undefined entity "; |
| 3255 | strncat(message, data_in, data_len < 100?data_len:100); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3256 | expat_set_error( |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3257 | XML_ERROR_UNDEFINED_ENTITY, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3258 | EXPAT(GetErrorLineNumber)(self->parser), |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3259 | EXPAT(GetErrorColumnNumber)(self->parser), |
| 3260 | message |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3261 | ); |
| 3262 | } |
| 3263 | |
| 3264 | Py_DECREF(key); |
| 3265 | } |
| 3266 | |
| 3267 | static void |
| 3268 | expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, |
| 3269 | const XML_Char **attrib_in) |
| 3270 | { |
| 3271 | PyObject* res; |
| 3272 | PyObject* tag; |
| 3273 | PyObject* attrib; |
| 3274 | int ok; |
| 3275 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3276 | if (PyErr_Occurred()) |
| 3277 | return; |
| 3278 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3279 | /* tag name */ |
| 3280 | tag = makeuniversal(self, tag_in); |
| 3281 | if (!tag) |
| 3282 | return; /* parser will look for errors */ |
| 3283 | |
| 3284 | /* attributes */ |
| 3285 | if (attrib_in[0]) { |
| 3286 | attrib = PyDict_New(); |
Serhiy Storchaka | a29eb08 | 2015-12-09 19:44:30 +0200 | [diff] [blame] | 3287 | if (!attrib) { |
| 3288 | Py_DECREF(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3289 | return; |
Serhiy Storchaka | a29eb08 | 2015-12-09 19:44:30 +0200 | [diff] [blame] | 3290 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3291 | while (attrib_in[0] && attrib_in[1]) { |
| 3292 | PyObject* key = makeuniversal(self, attrib_in[0]); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3293 | PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3294 | if (!key || !value) { |
| 3295 | Py_XDECREF(value); |
| 3296 | Py_XDECREF(key); |
| 3297 | Py_DECREF(attrib); |
Serhiy Storchaka | a29eb08 | 2015-12-09 19:44:30 +0200 | [diff] [blame] | 3298 | Py_DECREF(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3299 | return; |
| 3300 | } |
| 3301 | ok = PyDict_SetItem(attrib, key, value); |
| 3302 | Py_DECREF(value); |
| 3303 | Py_DECREF(key); |
| 3304 | if (ok < 0) { |
| 3305 | Py_DECREF(attrib); |
Serhiy Storchaka | a29eb08 | 2015-12-09 19:44:30 +0200 | [diff] [blame] | 3306 | Py_DECREF(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3307 | return; |
| 3308 | } |
| 3309 | attrib_in += 2; |
| 3310 | } |
| 3311 | } else { |
Serhiy Storchaka | 36ff997 | 2015-12-10 09:51:53 +0200 | [diff] [blame] | 3312 | Py_INCREF(Py_None); |
| 3313 | attrib = Py_None; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | if (TreeBuilder_CheckExact(self->target)) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3317 | /* shortcut */ |
| 3318 | res = treebuilder_handle_start((TreeBuilderObject*) self->target, |
| 3319 | tag, attrib); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 3320 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3321 | else if (self->handle_start) { |
Serhiy Storchaka | 36ff997 | 2015-12-10 09:51:53 +0200 | [diff] [blame] | 3322 | if (attrib == Py_None) { |
| 3323 | Py_DECREF(attrib); |
| 3324 | attrib = PyDict_New(); |
| 3325 | if (!attrib) { |
| 3326 | Py_DECREF(tag); |
| 3327 | return; |
| 3328 | } |
| 3329 | } |
Victor Stinner | 5abaa2b | 2016-12-09 16:22:32 +0100 | [diff] [blame] | 3330 | res = PyObject_CallFunctionObjArgs(self->handle_start, |
| 3331 | tag, attrib, NULL); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3332 | } else |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3333 | res = NULL; |
| 3334 | |
| 3335 | Py_DECREF(tag); |
| 3336 | Py_DECREF(attrib); |
| 3337 | |
| 3338 | Py_XDECREF(res); |
| 3339 | } |
| 3340 | |
| 3341 | static void |
| 3342 | expat_data_handler(XMLParserObject* self, const XML_Char* data_in, |
| 3343 | int data_len) |
| 3344 | { |
| 3345 | PyObject* data; |
| 3346 | PyObject* res; |
| 3347 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3348 | if (PyErr_Occurred()) |
| 3349 | return; |
| 3350 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3351 | data = PyUnicode_DecodeUTF8(data_in, data_len, "strict"); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 3352 | if (!data) |
| 3353 | return; /* parser will look for errors */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3354 | |
| 3355 | if (TreeBuilder_CheckExact(self->target)) |
| 3356 | /* shortcut */ |
| 3357 | res = treebuilder_handle_data((TreeBuilderObject*) self->target, data); |
| 3358 | else if (self->handle_data) |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3359 | res = _PyObject_FastCall(self->handle_data, &data, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3360 | else |
| 3361 | res = NULL; |
| 3362 | |
| 3363 | Py_DECREF(data); |
| 3364 | |
| 3365 | Py_XDECREF(res); |
| 3366 | } |
| 3367 | |
| 3368 | static void |
| 3369 | expat_end_handler(XMLParserObject* self, const XML_Char* tag_in) |
| 3370 | { |
| 3371 | PyObject* tag; |
| 3372 | PyObject* res = NULL; |
| 3373 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3374 | if (PyErr_Occurred()) |
| 3375 | return; |
| 3376 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3377 | if (TreeBuilder_CheckExact(self->target)) |
| 3378 | /* shortcut */ |
| 3379 | /* the standard tree builder doesn't look at the end tag */ |
| 3380 | res = treebuilder_handle_end( |
| 3381 | (TreeBuilderObject*) self->target, Py_None |
| 3382 | ); |
| 3383 | else if (self->handle_end) { |
| 3384 | tag = makeuniversal(self, tag_in); |
| 3385 | if (tag) { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3386 | res = _PyObject_FastCall(self->handle_end, &tag, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3387 | Py_DECREF(tag); |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | Py_XDECREF(res); |
| 3392 | } |
| 3393 | |
| 3394 | static void |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3395 | expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in, |
| 3396 | const XML_Char *uri_in) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3397 | { |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3398 | PyObject* res = NULL; |
| 3399 | PyObject* uri; |
| 3400 | PyObject* prefix; |
| 3401 | PyObject* stack[2]; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3402 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3403 | if (PyErr_Occurred()) |
| 3404 | return; |
| 3405 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3406 | if (!uri_in) |
| 3407 | uri_in = ""; |
| 3408 | if (!prefix_in) |
| 3409 | prefix_in = ""; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3410 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3411 | if (TreeBuilder_CheckExact(self->target)) { |
| 3412 | /* shortcut - TreeBuilder does not actually implement .start_ns() */ |
| 3413 | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 3414 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3415 | if (target->events_append && target->start_ns_event_obj) { |
| 3416 | prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict"); |
| 3417 | if (!prefix) |
| 3418 | return; |
| 3419 | uri = PyUnicode_DecodeUTF8(uri_in, strlen(uri_in), "strict"); |
| 3420 | if (!uri) { |
| 3421 | Py_DECREF(prefix); |
| 3422 | return; |
| 3423 | } |
| 3424 | |
| 3425 | res = treebuilder_handle_start_ns(target, prefix, uri); |
| 3426 | Py_DECREF(uri); |
| 3427 | Py_DECREF(prefix); |
| 3428 | } |
| 3429 | } else if (self->handle_start_ns) { |
| 3430 | prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict"); |
| 3431 | if (!prefix) |
| 3432 | return; |
| 3433 | uri = PyUnicode_DecodeUTF8(uri_in, strlen(uri_in), "strict"); |
| 3434 | if (!uri) { |
| 3435 | Py_DECREF(prefix); |
| 3436 | return; |
| 3437 | } |
| 3438 | |
| 3439 | stack[0] = prefix; |
| 3440 | stack[1] = uri; |
| 3441 | res = _PyObject_FastCall(self->handle_start_ns, stack, 2); |
| 3442 | Py_DECREF(uri); |
| 3443 | Py_DECREF(prefix); |
| 3444 | } |
| 3445 | |
| 3446 | Py_XDECREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | static void |
| 3450 | expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in) |
| 3451 | { |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3452 | PyObject *res = NULL; |
| 3453 | PyObject* prefix; |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 3454 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3455 | if (PyErr_Occurred()) |
| 3456 | return; |
| 3457 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3458 | if (!prefix_in) |
| 3459 | prefix_in = ""; |
Serhiy Storchaka | 7efaf95 | 2015-12-06 23:51:44 +0200 | [diff] [blame] | 3460 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3461 | if (TreeBuilder_CheckExact(self->target)) { |
| 3462 | /* shortcut - TreeBuilder does not actually implement .end_ns() */ |
| 3463 | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
| 3464 | |
| 3465 | if (target->events_append && target->end_ns_event_obj) { |
| 3466 | res = treebuilder_handle_end_ns(target, Py_None); |
| 3467 | } |
| 3468 | } else if (self->handle_end_ns) { |
| 3469 | prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict"); |
| 3470 | if (!prefix) |
| 3471 | return; |
| 3472 | |
| 3473 | res = _PyObject_FastCall(self->handle_end_ns, &prefix, 1); |
| 3474 | Py_DECREF(prefix); |
| 3475 | } |
| 3476 | |
| 3477 | Py_XDECREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | static void |
| 3481 | expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in) |
| 3482 | { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3483 | PyObject* comment = NULL; |
| 3484 | PyObject* res = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3485 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3486 | if (PyErr_Occurred()) |
| 3487 | return; |
| 3488 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3489 | if (TreeBuilder_CheckExact(self->target)) { |
| 3490 | /* shortcut */ |
| 3491 | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
| 3492 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3493 | comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict"); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3494 | if (!comment) |
| 3495 | return; /* parser will look for errors */ |
| 3496 | |
| 3497 | res = treebuilder_handle_comment(target, comment); |
| 3498 | } else if (self->handle_comment) { |
| 3499 | comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict"); |
| 3500 | if (!comment) |
| 3501 | return; |
| 3502 | |
| 3503 | res = _PyObject_FastCall(self->handle_comment, &comment, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3504 | } |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3505 | |
| 3506 | Py_XDECREF(res); |
| 3507 | Py_DECREF(comment); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 3510 | static void |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3511 | expat_start_doctype_handler(XMLParserObject *self, |
| 3512 | const XML_Char *doctype_name, |
| 3513 | const XML_Char *sysid, |
| 3514 | const XML_Char *pubid, |
| 3515 | int has_internal_subset) |
| 3516 | { |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3517 | _Py_IDENTIFIER(doctype); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3518 | PyObject *doctype_name_obj, *sysid_obj, *pubid_obj; |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3519 | PyObject *res; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3520 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3521 | if (PyErr_Occurred()) |
| 3522 | return; |
| 3523 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3524 | doctype_name_obj = makeuniversal(self, doctype_name); |
| 3525 | if (!doctype_name_obj) |
| 3526 | return; |
| 3527 | |
| 3528 | if (sysid) { |
| 3529 | sysid_obj = makeuniversal(self, sysid); |
| 3530 | if (!sysid_obj) { |
| 3531 | Py_DECREF(doctype_name_obj); |
| 3532 | return; |
| 3533 | } |
| 3534 | } else { |
| 3535 | Py_INCREF(Py_None); |
| 3536 | sysid_obj = Py_None; |
| 3537 | } |
| 3538 | |
| 3539 | if (pubid) { |
| 3540 | pubid_obj = makeuniversal(self, pubid); |
| 3541 | if (!pubid_obj) { |
| 3542 | Py_DECREF(doctype_name_obj); |
| 3543 | Py_DECREF(sysid_obj); |
| 3544 | return; |
| 3545 | } |
| 3546 | } else { |
| 3547 | Py_INCREF(Py_None); |
| 3548 | pubid_obj = Py_None; |
| 3549 | } |
| 3550 | |
| 3551 | /* If the target has a handler for doctype, call it. */ |
| 3552 | if (self->handle_doctype) { |
Victor Stinner | 5abaa2b | 2016-12-09 16:22:32 +0100 | [diff] [blame] | 3553 | res = PyObject_CallFunctionObjArgs(self->handle_doctype, |
| 3554 | doctype_name_obj, pubid_obj, |
| 3555 | sysid_obj, NULL); |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3556 | Py_XDECREF(res); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3557 | } |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3558 | else if (_PyObject_LookupAttrId((PyObject *)self, &PyId_doctype, &res) > 0) { |
| 3559 | (void)PyErr_WarnEx(PyExc_RuntimeWarning, |
| 3560 | "The doctype() method of XMLParser is ignored. " |
| 3561 | "Define doctype() method on the TreeBuilder target.", |
| 3562 | 1); |
Serhiy Storchaka | ee98e7b | 2018-07-25 14:52:45 +0300 | [diff] [blame] | 3563 | Py_DECREF(res); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3564 | } |
| 3565 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3566 | Py_DECREF(doctype_name_obj); |
| 3567 | Py_DECREF(pubid_obj); |
| 3568 | Py_DECREF(sysid_obj); |
| 3569 | } |
| 3570 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3571 | static void |
| 3572 | expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, |
| 3573 | const XML_Char* data_in) |
| 3574 | { |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3575 | PyObject* pi_target = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3576 | PyObject* data; |
| 3577 | PyObject* res; |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3578 | PyObject* stack[2]; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3579 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3580 | if (PyErr_Occurred()) |
| 3581 | return; |
| 3582 | |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3583 | if (TreeBuilder_CheckExact(self->target)) { |
| 3584 | /* shortcut */ |
| 3585 | TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
| 3586 | |
| 3587 | if (target->events_append && target->pi_event_obj) { |
| 3588 | pi_target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict"); |
| 3589 | if (!pi_target) |
| 3590 | goto error; |
| 3591 | data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict"); |
| 3592 | if (!data) |
| 3593 | goto error; |
| 3594 | res = treebuilder_handle_pi(target, pi_target, data); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3595 | Py_XDECREF(res); |
| 3596 | Py_DECREF(data); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3597 | Py_DECREF(pi_target); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3598 | } |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3599 | } else if (self->handle_pi) { |
| 3600 | pi_target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict"); |
| 3601 | if (!pi_target) |
| 3602 | goto error; |
| 3603 | data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict"); |
| 3604 | if (!data) |
| 3605 | goto error; |
| 3606 | |
| 3607 | stack[0] = pi_target; |
| 3608 | stack[1] = data; |
| 3609 | res = _PyObject_FastCall(self->handle_pi, stack, 2); |
| 3610 | Py_XDECREF(res); |
| 3611 | Py_DECREF(data); |
| 3612 | Py_DECREF(pi_target); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3613 | } |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 3614 | |
| 3615 | return; |
| 3616 | |
| 3617 | error: |
| 3618 | Py_XDECREF(pi_target); |
| 3619 | return; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3622 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3623 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3624 | static PyObject * |
| 3625 | xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3626 | { |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3627 | XMLParserObject *self = (XMLParserObject *)type->tp_alloc(type, 0); |
| 3628 | if (self) { |
| 3629 | self->parser = NULL; |
| 3630 | self->target = self->entity = self->names = NULL; |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3631 | self->handle_start_ns = self->handle_end_ns = NULL; |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3632 | self->handle_start = self->handle_data = self->handle_end = NULL; |
| 3633 | self->handle_comment = self->handle_pi = self->handle_close = NULL; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3634 | self->handle_doctype = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3635 | } |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3636 | return (PyObject *)self; |
| 3637 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3638 | |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3639 | static int |
| 3640 | ignore_attribute_error(PyObject *value) |
| 3641 | { |
| 3642 | if (value == NULL) { |
| 3643 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3644 | return -1; |
| 3645 | } |
| 3646 | PyErr_Clear(); |
| 3647 | } |
| 3648 | return 0; |
| 3649 | } |
| 3650 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3651 | /*[clinic input] |
| 3652 | _elementtree.XMLParser.__init__ |
| 3653 | |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3654 | * |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3655 | target: object = NULL |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 3656 | encoding: str(accept={str, NoneType}) = NULL |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3657 | |
| 3658 | [clinic start generated code]*/ |
| 3659 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3660 | static int |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 3661 | _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target, |
| 3662 | const char *encoding) |
| 3663 | /*[clinic end generated code: output=3ae45ec6cdf344e4 input=96288fcba916cfce]*/ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3664 | { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3665 | self->entity = PyDict_New(); |
| 3666 | if (!self->entity) |
| 3667 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3668 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3669 | self->names = PyDict_New(); |
| 3670 | if (!self->names) { |
| 3671 | Py_CLEAR(self->entity); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3672 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3673 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3674 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3675 | self->parser = EXPAT(ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}"); |
| 3676 | if (!self->parser) { |
| 3677 | Py_CLEAR(self->entity); |
| 3678 | Py_CLEAR(self->names); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3679 | PyErr_NoMemory(); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3680 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3681 | } |
Christian Heimes | cb5778f | 2018-09-18 14:38:58 +0200 | [diff] [blame] | 3682 | /* expat < 2.1.0 has no XML_SetHashSalt() */ |
| 3683 | if (EXPAT(SetHashSalt) != NULL) { |
| 3684 | EXPAT(SetHashSalt)(self->parser, |
| 3685 | (unsigned long)_Py_HashSecret.expat.hashsalt); |
| 3686 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3687 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3688 | if (target) { |
| 3689 | Py_INCREF(target); |
| 3690 | } else { |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 3691 | target = treebuilder_new(&TreeBuilder_Type, NULL, NULL); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3692 | if (!target) { |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3693 | Py_CLEAR(self->entity); |
| 3694 | Py_CLEAR(self->names); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3695 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3696 | } |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3697 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3698 | self->target = target; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3699 | |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3700 | self->handle_start_ns = PyObject_GetAttrString(target, "start_ns"); |
| 3701 | if (ignore_attribute_error(self->handle_start_ns)) { |
| 3702 | return -1; |
| 3703 | } |
| 3704 | self->handle_end_ns = PyObject_GetAttrString(target, "end_ns"); |
| 3705 | if (ignore_attribute_error(self->handle_end_ns)) { |
| 3706 | return -1; |
| 3707 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3708 | self->handle_start = PyObject_GetAttrString(target, "start"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3709 | if (ignore_attribute_error(self->handle_start)) { |
| 3710 | return -1; |
| 3711 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3712 | self->handle_data = PyObject_GetAttrString(target, "data"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3713 | if (ignore_attribute_error(self->handle_data)) { |
| 3714 | return -1; |
| 3715 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3716 | self->handle_end = PyObject_GetAttrString(target, "end"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3717 | if (ignore_attribute_error(self->handle_end)) { |
| 3718 | return -1; |
| 3719 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3720 | self->handle_comment = PyObject_GetAttrString(target, "comment"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3721 | if (ignore_attribute_error(self->handle_comment)) { |
| 3722 | return -1; |
| 3723 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3724 | self->handle_pi = PyObject_GetAttrString(target, "pi"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3725 | if (ignore_attribute_error(self->handle_pi)) { |
| 3726 | return -1; |
| 3727 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3728 | self->handle_close = PyObject_GetAttrString(target, "close"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3729 | if (ignore_attribute_error(self->handle_close)) { |
| 3730 | return -1; |
| 3731 | } |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3732 | self->handle_doctype = PyObject_GetAttrString(target, "doctype"); |
scoder | c8d8e15 | 2017-09-14 22:00:03 +0200 | [diff] [blame] | 3733 | if (ignore_attribute_error(self->handle_doctype)) { |
| 3734 | return -1; |
| 3735 | } |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 3736 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3737 | /* configure parser */ |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3738 | EXPAT(SetUserData)(self->parser, self); |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3739 | if (self->handle_start_ns || self->handle_end_ns) |
| 3740 | EXPAT(SetNamespaceDeclHandler)( |
| 3741 | self->parser, |
| 3742 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 3743 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 3744 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3745 | EXPAT(SetElementHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3746 | self->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3747 | (XML_StartElementHandler) expat_start_handler, |
| 3748 | (XML_EndElementHandler) expat_end_handler |
| 3749 | ); |
| 3750 | EXPAT(SetDefaultHandlerExpand)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3751 | self->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3752 | (XML_DefaultHandler) expat_default_handler |
| 3753 | ); |
| 3754 | EXPAT(SetCharacterDataHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3755 | self->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3756 | (XML_CharacterDataHandler) expat_data_handler |
| 3757 | ); |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3758 | if (self->handle_comment) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3759 | EXPAT(SetCommentHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3760 | self->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3761 | (XML_CommentHandler) expat_comment_handler |
| 3762 | ); |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3763 | if (self->handle_pi) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3764 | EXPAT(SetProcessingInstructionHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3765 | self->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3766 | (XML_ProcessingInstructionHandler) expat_pi_handler |
| 3767 | ); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3768 | EXPAT(SetStartDoctypeDeclHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3769 | self->parser, |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3770 | (XML_StartDoctypeDeclHandler) expat_start_doctype_handler |
| 3771 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3772 | EXPAT(SetUnknownEncodingHandler)( |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3773 | self->parser, |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 3774 | EXPAT(DefaultUnknownEncodingHandler), NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3775 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3776 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3777 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3780 | static int |
| 3781 | xmlparser_gc_traverse(XMLParserObject *self, visitproc visit, void *arg) |
| 3782 | { |
| 3783 | Py_VISIT(self->handle_close); |
| 3784 | Py_VISIT(self->handle_pi); |
| 3785 | Py_VISIT(self->handle_comment); |
| 3786 | Py_VISIT(self->handle_end); |
| 3787 | Py_VISIT(self->handle_data); |
| 3788 | Py_VISIT(self->handle_start); |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3789 | Py_VISIT(self->handle_start_ns); |
| 3790 | Py_VISIT(self->handle_end_ns); |
| 3791 | Py_VISIT(self->handle_doctype); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3792 | |
| 3793 | Py_VISIT(self->target); |
| 3794 | Py_VISIT(self->entity); |
| 3795 | Py_VISIT(self->names); |
| 3796 | |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
| 3800 | static int |
| 3801 | xmlparser_gc_clear(XMLParserObject *self) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3802 | { |
Victor Stinner | e727d41 | 2017-09-18 05:29:37 -0700 | [diff] [blame] | 3803 | if (self->parser != NULL) { |
| 3804 | XML_Parser parser = self->parser; |
| 3805 | self->parser = NULL; |
| 3806 | EXPAT(ParserFree)(parser); |
| 3807 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3809 | Py_CLEAR(self->handle_close); |
| 3810 | Py_CLEAR(self->handle_pi); |
| 3811 | Py_CLEAR(self->handle_comment); |
| 3812 | Py_CLEAR(self->handle_end); |
| 3813 | Py_CLEAR(self->handle_data); |
| 3814 | Py_CLEAR(self->handle_start); |
Stefan Behnel | dde3eeb | 2019-05-01 21:49:58 +0200 | [diff] [blame^] | 3815 | Py_CLEAR(self->handle_start_ns); |
| 3816 | Py_CLEAR(self->handle_end_ns); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3817 | Py_CLEAR(self->handle_doctype); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3818 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3819 | Py_CLEAR(self->target); |
| 3820 | Py_CLEAR(self->entity); |
| 3821 | Py_CLEAR(self->names); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3822 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3823 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3826 | static void |
| 3827 | xmlparser_dealloc(XMLParserObject* self) |
| 3828 | { |
| 3829 | PyObject_GC_UnTrack(self); |
| 3830 | xmlparser_gc_clear(self); |
| 3831 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3832 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3833 | |
| 3834 | LOCAL(PyObject*) |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3835 | expat_parse(XMLParserObject* self, const char* data, int data_len, int final) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3836 | { |
| 3837 | int ok; |
| 3838 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3839 | assert(!PyErr_Occurred()); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3840 | ok = EXPAT(Parse)(self->parser, data, data_len, final); |
| 3841 | |
| 3842 | if (PyErr_Occurred()) |
| 3843 | return NULL; |
| 3844 | |
| 3845 | if (!ok) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3846 | expat_set_error( |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3847 | EXPAT(GetErrorCode)(self->parser), |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3848 | EXPAT(GetErrorLineNumber)(self->parser), |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3849 | EXPAT(GetErrorColumnNumber)(self->parser), |
| 3850 | NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3851 | ); |
| 3852 | return NULL; |
| 3853 | } |
| 3854 | |
| 3855 | Py_RETURN_NONE; |
| 3856 | } |
| 3857 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3858 | /*[clinic input] |
| 3859 | _elementtree.XMLParser.close |
| 3860 | |
| 3861 | [clinic start generated code]*/ |
| 3862 | |
| 3863 | static PyObject * |
| 3864 | _elementtree_XMLParser_close_impl(XMLParserObject *self) |
| 3865 | /*[clinic end generated code: output=d68d375dd23bc7fb input=ca7909ca78c3abfe]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3866 | { |
| 3867 | /* end feeding data to parser */ |
| 3868 | |
| 3869 | PyObject* res; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3870 | res = expat_parse(self, "", 0, 1); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3871 | if (!res) |
| 3872 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3873 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3874 | if (TreeBuilder_CheckExact(self->target)) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3875 | Py_DECREF(res); |
| 3876 | return treebuilder_done((TreeBuilderObject*) self->target); |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3877 | } |
| 3878 | else if (self->handle_close) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3879 | Py_DECREF(res); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 3880 | return _PyObject_CallNoArg(self->handle_close); |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3881 | } |
| 3882 | else { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3883 | return res; |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3884 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3887 | /*[clinic input] |
| 3888 | _elementtree.XMLParser.feed |
| 3889 | |
| 3890 | data: object |
| 3891 | / |
| 3892 | |
| 3893 | [clinic start generated code]*/ |
| 3894 | |
| 3895 | static PyObject * |
| 3896 | _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data) |
| 3897 | /*[clinic end generated code: output=e42b6a78eec7446d input=fe231b6b8de3ce1f]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3898 | { |
| 3899 | /* feed data to parser */ |
| 3900 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3901 | if (PyUnicode_Check(data)) { |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3902 | Py_ssize_t data_len; |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3903 | const char *data_ptr = PyUnicode_AsUTF8AndSize(data, &data_len); |
| 3904 | if (data_ptr == NULL) |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3905 | return NULL; |
| 3906 | if (data_len > INT_MAX) { |
| 3907 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 3908 | return NULL; |
| 3909 | } |
| 3910 | /* Explicitly set UTF-8 encoding. Return code ignored. */ |
| 3911 | (void)EXPAT(SetEncoding)(self->parser, "utf-8"); |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3912 | return expat_parse(self, data_ptr, (int)data_len, 0); |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3913 | } |
| 3914 | else { |
| 3915 | Py_buffer view; |
| 3916 | PyObject *res; |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3917 | if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3918 | return NULL; |
| 3919 | if (view.len > INT_MAX) { |
| 3920 | PyBuffer_Release(&view); |
| 3921 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 3922 | return NULL; |
| 3923 | } |
| 3924 | res = expat_parse(self, view.buf, (int)view.len, 0); |
| 3925 | PyBuffer_Release(&view); |
| 3926 | return res; |
| 3927 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3930 | /*[clinic input] |
| 3931 | _elementtree.XMLParser._parse_whole |
| 3932 | |
| 3933 | file: object |
| 3934 | / |
| 3935 | |
| 3936 | [clinic start generated code]*/ |
| 3937 | |
| 3938 | static PyObject * |
| 3939 | _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file) |
| 3940 | /*[clinic end generated code: output=f797197bb818dda3 input=19ecc893b6f3e752]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3941 | { |
Eli Bendersky | a369923 | 2013-05-19 18:47:23 -0700 | [diff] [blame] | 3942 | /* (internal) parse the whole input, until end of stream */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3943 | PyObject* reader; |
| 3944 | PyObject* buffer; |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3945 | PyObject* temp; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3946 | PyObject* res; |
| 3947 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 3948 | reader = PyObject_GetAttrString(file, "read"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3949 | if (!reader) |
| 3950 | return NULL; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3951 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3952 | /* read from open file object */ |
| 3953 | for (;;) { |
| 3954 | |
| 3955 | buffer = PyObject_CallFunction(reader, "i", 64*1024); |
| 3956 | |
| 3957 | if (!buffer) { |
| 3958 | /* read failed (e.g. due to KeyboardInterrupt) */ |
| 3959 | Py_DECREF(reader); |
| 3960 | return NULL; |
| 3961 | } |
| 3962 | |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3963 | if (PyUnicode_CheckExact(buffer)) { |
| 3964 | /* A unicode object is encoded into bytes using UTF-8 */ |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 3965 | if (PyUnicode_GET_LENGTH(buffer) == 0) { |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3966 | Py_DECREF(buffer); |
| 3967 | break; |
| 3968 | } |
| 3969 | temp = PyUnicode_AsEncodedString(buffer, "utf-8", "surrogatepass"); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3970 | Py_DECREF(buffer); |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3971 | if (!temp) { |
| 3972 | /* Propagate exception from PyUnicode_AsEncodedString */ |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3973 | Py_DECREF(reader); |
| 3974 | return NULL; |
| 3975 | } |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3976 | buffer = temp; |
| 3977 | } |
| 3978 | else if (!PyBytes_CheckExact(buffer) || PyBytes_GET_SIZE(buffer) == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3979 | Py_DECREF(buffer); |
| 3980 | break; |
| 3981 | } |
| 3982 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3983 | if (PyBytes_GET_SIZE(buffer) > INT_MAX) { |
| 3984 | Py_DECREF(buffer); |
| 3985 | Py_DECREF(reader); |
| 3986 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 3987 | return NULL; |
| 3988 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3989 | res = expat_parse( |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3990 | self, PyBytes_AS_STRING(buffer), (int)PyBytes_GET_SIZE(buffer), 0 |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3991 | ); |
| 3992 | |
| 3993 | Py_DECREF(buffer); |
| 3994 | |
| 3995 | if (!res) { |
| 3996 | Py_DECREF(reader); |
| 3997 | return NULL; |
| 3998 | } |
| 3999 | Py_DECREF(res); |
| 4000 | |
| 4001 | } |
| 4002 | |
| 4003 | Py_DECREF(reader); |
| 4004 | |
| 4005 | res = expat_parse(self, "", 0, 1); |
| 4006 | |
| 4007 | if (res && TreeBuilder_CheckExact(self->target)) { |
| 4008 | Py_DECREF(res); |
| 4009 | return treebuilder_done((TreeBuilderObject*) self->target); |
| 4010 | } |
| 4011 | |
| 4012 | return res; |
| 4013 | } |
| 4014 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4015 | /*[clinic input] |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4016 | _elementtree.XMLParser._setevents |
| 4017 | |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 4018 | events_queue: object |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4019 | events_to_report: object = None |
| 4020 | / |
| 4021 | |
| 4022 | [clinic start generated code]*/ |
| 4023 | |
| 4024 | static PyObject * |
| 4025 | _elementtree_XMLParser__setevents_impl(XMLParserObject *self, |
| 4026 | PyObject *events_queue, |
| 4027 | PyObject *events_to_report) |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 4028 | /*[clinic end generated code: output=1440092922b13ed1 input=abf90830a1c3b0fc]*/ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4029 | { |
| 4030 | /* activate element event reporting */ |
Serhiy Storchaka | bc4ded95 | 2015-12-24 11:51:57 +0200 | [diff] [blame] | 4031 | Py_ssize_t i; |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4032 | TreeBuilderObject *target; |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 4033 | PyObject *events_append, *events_seq; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4034 | |
| 4035 | if (!TreeBuilder_CheckExact(self->target)) { |
| 4036 | PyErr_SetString( |
| 4037 | PyExc_TypeError, |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 4038 | "event handling only supported for ElementTree.TreeBuilder " |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4039 | "targets" |
| 4040 | ); |
| 4041 | return NULL; |
| 4042 | } |
| 4043 | |
| 4044 | target = (TreeBuilderObject*) self->target; |
| 4045 | |
Serhiy Storchaka | 9ec5e25 | 2015-12-07 02:31:11 +0200 | [diff] [blame] | 4046 | events_append = PyObject_GetAttrString(events_queue, "append"); |
| 4047 | if (events_append == NULL) |
| 4048 | return NULL; |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4049 | Py_XSETREF(target->events_append, events_append); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4050 | |
| 4051 | /* clear out existing events */ |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 4052 | Py_CLEAR(target->start_event_obj); |
| 4053 | Py_CLEAR(target->end_event_obj); |
| 4054 | Py_CLEAR(target->start_ns_event_obj); |
| 4055 | Py_CLEAR(target->end_ns_event_obj); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 4056 | Py_CLEAR(target->comment_event_obj); |
| 4057 | Py_CLEAR(target->pi_event_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4058 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4059 | if (events_to_report == Py_None) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4060 | /* default is "end" only */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4061 | target->end_event_obj = PyUnicode_FromString("end"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4062 | Py_RETURN_NONE; |
| 4063 | } |
| 4064 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4065 | if (!(events_seq = PySequence_Fast(events_to_report, |
| 4066 | "events must be a sequence"))) { |
| 4067 | return NULL; |
| 4068 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4069 | |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 4070 | for (i = 0; i < PySequence_Fast_GET_SIZE(events_seq); ++i) { |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4071 | PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i); |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 4072 | const char *event_name = NULL; |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4073 | if (PyUnicode_Check(event_name_obj)) { |
Serhiy Storchaka | bc4ded95 | 2015-12-24 11:51:57 +0200 | [diff] [blame] | 4074 | event_name = PyUnicode_AsUTF8(event_name_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4075 | } else if (PyBytes_Check(event_name_obj)) { |
| 4076 | event_name = PyBytes_AS_STRING(event_name_obj); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4077 | } |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4078 | if (event_name == NULL) { |
| 4079 | Py_DECREF(events_seq); |
| 4080 | PyErr_Format(PyExc_ValueError, "invalid events sequence"); |
| 4081 | return NULL; |
Serhiy Storchaka | bc4ded95 | 2015-12-24 11:51:57 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | Py_INCREF(event_name_obj); |
| 4085 | if (strcmp(event_name, "start") == 0) { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4086 | Py_XSETREF(target->start_event_obj, event_name_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4087 | } else if (strcmp(event_name, "end") == 0) { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4088 | Py_XSETREF(target->end_event_obj, event_name_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4089 | } else if (strcmp(event_name, "start-ns") == 0) { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4090 | Py_XSETREF(target->start_ns_event_obj, event_name_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4091 | EXPAT(SetNamespaceDeclHandler)( |
| 4092 | self->parser, |
| 4093 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 4094 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 4095 | ); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4096 | } else if (strcmp(event_name, "end-ns") == 0) { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4097 | Py_XSETREF(target->end_ns_event_obj, event_name_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4098 | EXPAT(SetNamespaceDeclHandler)( |
| 4099 | self->parser, |
| 4100 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 4101 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 4102 | ); |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 4103 | } else if (strcmp(event_name, "comment") == 0) { |
| 4104 | Py_XSETREF(target->comment_event_obj, event_name_obj); |
| 4105 | EXPAT(SetCommentHandler)( |
| 4106 | self->parser, |
| 4107 | (XML_CommentHandler) expat_comment_handler |
| 4108 | ); |
| 4109 | } else if (strcmp(event_name, "pi") == 0) { |
| 4110 | Py_XSETREF(target->pi_event_obj, event_name_obj); |
| 4111 | EXPAT(SetProcessingInstructionHandler)( |
| 4112 | self->parser, |
| 4113 | (XML_ProcessingInstructionHandler) expat_pi_handler |
| 4114 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4115 | } else { |
Serhiy Storchaka | bc4ded95 | 2015-12-24 11:51:57 +0200 | [diff] [blame] | 4116 | Py_DECREF(event_name_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4117 | Py_DECREF(events_seq); |
| 4118 | PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4119 | return NULL; |
| 4120 | } |
| 4121 | } |
| 4122 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 4123 | Py_DECREF(events_seq); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4124 | Py_RETURN_NONE; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Serhiy Storchaka | b2953fa | 2018-10-04 10:41:27 +0300 | [diff] [blame] | 4127 | static PyMemberDef xmlparser_members[] = { |
| 4128 | {"entity", T_OBJECT, offsetof(XMLParserObject, entity), READONLY, NULL}, |
| 4129 | {"target", T_OBJECT, offsetof(XMLParserObject, target), READONLY, NULL}, |
| 4130 | {NULL} |
| 4131 | }; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4132 | |
Serhiy Storchaka | b2953fa | 2018-10-04 10:41:27 +0300 | [diff] [blame] | 4133 | static PyObject* |
| 4134 | xmlparser_version_getter(XMLParserObject *self, void *closure) |
| 4135 | { |
| 4136 | return PyUnicode_FromFormat( |
| 4137 | "Expat %d.%d.%d", XML_MAJOR_VERSION, |
| 4138 | XML_MINOR_VERSION, XML_MICRO_VERSION); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Serhiy Storchaka | b2953fa | 2018-10-04 10:41:27 +0300 | [diff] [blame] | 4141 | static PyGetSetDef xmlparser_getsetlist[] = { |
| 4142 | {"version", (getter)xmlparser_version_getter, NULL, NULL}, |
| 4143 | {NULL}, |
| 4144 | }; |
| 4145 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4146 | #include "clinic/_elementtree.c.h" |
| 4147 | |
| 4148 | static PyMethodDef element_methods[] = { |
| 4149 | |
| 4150 | _ELEMENTTREE_ELEMENT_CLEAR_METHODDEF |
| 4151 | |
| 4152 | _ELEMENTTREE_ELEMENT_GET_METHODDEF |
| 4153 | _ELEMENTTREE_ELEMENT_SET_METHODDEF |
| 4154 | |
| 4155 | _ELEMENTTREE_ELEMENT_FIND_METHODDEF |
| 4156 | _ELEMENTTREE_ELEMENT_FINDTEXT_METHODDEF |
| 4157 | _ELEMENTTREE_ELEMENT_FINDALL_METHODDEF |
| 4158 | |
| 4159 | _ELEMENTTREE_ELEMENT_APPEND_METHODDEF |
| 4160 | _ELEMENTTREE_ELEMENT_EXTEND_METHODDEF |
| 4161 | _ELEMENTTREE_ELEMENT_INSERT_METHODDEF |
| 4162 | _ELEMENTTREE_ELEMENT_REMOVE_METHODDEF |
| 4163 | |
| 4164 | _ELEMENTTREE_ELEMENT_ITER_METHODDEF |
| 4165 | _ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF |
| 4166 | _ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF |
| 4167 | |
Serhiy Storchaka | 762ec97 | 2017-03-30 18:12:06 +0300 | [diff] [blame] | 4168 | _ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4169 | _ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF |
| 4170 | |
| 4171 | _ELEMENTTREE_ELEMENT_ITEMS_METHODDEF |
| 4172 | _ELEMENTTREE_ELEMENT_KEYS_METHODDEF |
| 4173 | |
| 4174 | _ELEMENTTREE_ELEMENT_MAKEELEMENT_METHODDEF |
| 4175 | |
| 4176 | _ELEMENTTREE_ELEMENT___COPY___METHODDEF |
| 4177 | _ELEMENTTREE_ELEMENT___DEEPCOPY___METHODDEF |
| 4178 | _ELEMENTTREE_ELEMENT___SIZEOF___METHODDEF |
| 4179 | _ELEMENTTREE_ELEMENT___GETSTATE___METHODDEF |
| 4180 | _ELEMENTTREE_ELEMENT___SETSTATE___METHODDEF |
| 4181 | |
| 4182 | {NULL, NULL} |
| 4183 | }; |
| 4184 | |
| 4185 | static PyMappingMethods element_as_mapping = { |
| 4186 | (lenfunc) element_length, |
| 4187 | (binaryfunc) element_subscr, |
| 4188 | (objobjargproc) element_ass_subscr, |
| 4189 | }; |
| 4190 | |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 4191 | static PyGetSetDef element_getsetlist[] = { |
| 4192 | {"tag", |
| 4193 | (getter)element_tag_getter, |
| 4194 | (setter)element_tag_setter, |
| 4195 | "A string identifying what kind of data this element represents"}, |
| 4196 | {"text", |
| 4197 | (getter)element_text_getter, |
| 4198 | (setter)element_text_setter, |
| 4199 | "A string of text directly after the start tag, or None"}, |
| 4200 | {"tail", |
| 4201 | (getter)element_tail_getter, |
| 4202 | (setter)element_tail_setter, |
| 4203 | "A string of text directly after the end tag, or None"}, |
| 4204 | {"attrib", |
| 4205 | (getter)element_attrib_getter, |
| 4206 | (setter)element_attrib_setter, |
| 4207 | "A dictionary containing the element's attributes"}, |
| 4208 | {NULL}, |
| 4209 | }; |
| 4210 | |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4211 | static PyTypeObject Element_Type = { |
| 4212 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4213 | "xml.etree.ElementTree.Element", sizeof(ElementObject), 0, |
| 4214 | /* methods */ |
| 4215 | (destructor)element_dealloc, /* tp_dealloc */ |
| 4216 | 0, /* tp_print */ |
| 4217 | 0, /* tp_getattr */ |
| 4218 | 0, /* tp_setattr */ |
| 4219 | 0, /* tp_reserved */ |
| 4220 | (reprfunc)element_repr, /* tp_repr */ |
| 4221 | 0, /* tp_as_number */ |
| 4222 | &element_as_sequence, /* tp_as_sequence */ |
| 4223 | &element_as_mapping, /* tp_as_mapping */ |
| 4224 | 0, /* tp_hash */ |
| 4225 | 0, /* tp_call */ |
| 4226 | 0, /* tp_str */ |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 4227 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4228 | 0, /* tp_setattro */ |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4229 | 0, /* tp_as_buffer */ |
| 4230 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4231 | /* tp_flags */ |
| 4232 | 0, /* tp_doc */ |
| 4233 | (traverseproc)element_gc_traverse, /* tp_traverse */ |
| 4234 | (inquiry)element_gc_clear, /* tp_clear */ |
| 4235 | 0, /* tp_richcompare */ |
| 4236 | offsetof(ElementObject, weakreflist), /* tp_weaklistoffset */ |
| 4237 | 0, /* tp_iter */ |
| 4238 | 0, /* tp_iternext */ |
| 4239 | element_methods, /* tp_methods */ |
| 4240 | 0, /* tp_members */ |
Serhiy Storchaka | dde0815 | 2015-11-25 15:28:13 +0200 | [diff] [blame] | 4241 | element_getsetlist, /* tp_getset */ |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4242 | 0, /* tp_base */ |
| 4243 | 0, /* tp_dict */ |
| 4244 | 0, /* tp_descr_get */ |
| 4245 | 0, /* tp_descr_set */ |
| 4246 | 0, /* tp_dictoffset */ |
| 4247 | (initproc)element_init, /* tp_init */ |
| 4248 | PyType_GenericAlloc, /* tp_alloc */ |
| 4249 | element_new, /* tp_new */ |
| 4250 | 0, /* tp_free */ |
| 4251 | }; |
| 4252 | |
| 4253 | static PyMethodDef treebuilder_methods[] = { |
| 4254 | _ELEMENTTREE_TREEBUILDER_DATA_METHODDEF |
| 4255 | _ELEMENTTREE_TREEBUILDER_START_METHODDEF |
| 4256 | _ELEMENTTREE_TREEBUILDER_END_METHODDEF |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 4257 | _ELEMENTTREE_TREEBUILDER_COMMENT_METHODDEF |
| 4258 | _ELEMENTTREE_TREEBUILDER_PI_METHODDEF |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4259 | _ELEMENTTREE_TREEBUILDER_CLOSE_METHODDEF |
| 4260 | {NULL, NULL} |
| 4261 | }; |
| 4262 | |
| 4263 | static PyTypeObject TreeBuilder_Type = { |
| 4264 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4265 | "xml.etree.ElementTree.TreeBuilder", sizeof(TreeBuilderObject), 0, |
| 4266 | /* methods */ |
| 4267 | (destructor)treebuilder_dealloc, /* tp_dealloc */ |
| 4268 | 0, /* tp_print */ |
| 4269 | 0, /* tp_getattr */ |
| 4270 | 0, /* tp_setattr */ |
| 4271 | 0, /* tp_reserved */ |
| 4272 | 0, /* tp_repr */ |
| 4273 | 0, /* tp_as_number */ |
| 4274 | 0, /* tp_as_sequence */ |
| 4275 | 0, /* tp_as_mapping */ |
| 4276 | 0, /* tp_hash */ |
| 4277 | 0, /* tp_call */ |
| 4278 | 0, /* tp_str */ |
| 4279 | 0, /* tp_getattro */ |
| 4280 | 0, /* tp_setattro */ |
| 4281 | 0, /* tp_as_buffer */ |
| 4282 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4283 | /* tp_flags */ |
| 4284 | 0, /* tp_doc */ |
| 4285 | (traverseproc)treebuilder_gc_traverse, /* tp_traverse */ |
| 4286 | (inquiry)treebuilder_gc_clear, /* tp_clear */ |
| 4287 | 0, /* tp_richcompare */ |
| 4288 | 0, /* tp_weaklistoffset */ |
| 4289 | 0, /* tp_iter */ |
| 4290 | 0, /* tp_iternext */ |
| 4291 | treebuilder_methods, /* tp_methods */ |
| 4292 | 0, /* tp_members */ |
| 4293 | 0, /* tp_getset */ |
| 4294 | 0, /* tp_base */ |
| 4295 | 0, /* tp_dict */ |
| 4296 | 0, /* tp_descr_get */ |
| 4297 | 0, /* tp_descr_set */ |
| 4298 | 0, /* tp_dictoffset */ |
| 4299 | _elementtree_TreeBuilder___init__, /* tp_init */ |
| 4300 | PyType_GenericAlloc, /* tp_alloc */ |
| 4301 | treebuilder_new, /* tp_new */ |
| 4302 | 0, /* tp_free */ |
| 4303 | }; |
| 4304 | |
| 4305 | static PyMethodDef xmlparser_methods[] = { |
| 4306 | _ELEMENTTREE_XMLPARSER_FEED_METHODDEF |
| 4307 | _ELEMENTTREE_XMLPARSER_CLOSE_METHODDEF |
| 4308 | _ELEMENTTREE_XMLPARSER__PARSE_WHOLE_METHODDEF |
| 4309 | _ELEMENTTREE_XMLPARSER__SETEVENTS_METHODDEF |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4310 | {NULL, NULL} |
| 4311 | }; |
| 4312 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4313 | static PyTypeObject XMLParser_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4314 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 4315 | "xml.etree.ElementTree.XMLParser", sizeof(XMLParserObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4316 | /* methods */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4317 | (destructor)xmlparser_dealloc, /* tp_dealloc */ |
| 4318 | 0, /* tp_print */ |
| 4319 | 0, /* tp_getattr */ |
| 4320 | 0, /* tp_setattr */ |
| 4321 | 0, /* tp_reserved */ |
| 4322 | 0, /* tp_repr */ |
| 4323 | 0, /* tp_as_number */ |
| 4324 | 0, /* tp_as_sequence */ |
| 4325 | 0, /* tp_as_mapping */ |
| 4326 | 0, /* tp_hash */ |
| 4327 | 0, /* tp_call */ |
| 4328 | 0, /* tp_str */ |
Serhiy Storchaka | b2953fa | 2018-10-04 10:41:27 +0300 | [diff] [blame] | 4329 | 0, /* tp_getattro */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4330 | 0, /* tp_setattro */ |
| 4331 | 0, /* tp_as_buffer */ |
| 4332 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4333 | /* tp_flags */ |
| 4334 | 0, /* tp_doc */ |
| 4335 | (traverseproc)xmlparser_gc_traverse, /* tp_traverse */ |
| 4336 | (inquiry)xmlparser_gc_clear, /* tp_clear */ |
| 4337 | 0, /* tp_richcompare */ |
| 4338 | 0, /* tp_weaklistoffset */ |
| 4339 | 0, /* tp_iter */ |
| 4340 | 0, /* tp_iternext */ |
| 4341 | xmlparser_methods, /* tp_methods */ |
Serhiy Storchaka | b2953fa | 2018-10-04 10:41:27 +0300 | [diff] [blame] | 4342 | xmlparser_members, /* tp_members */ |
| 4343 | xmlparser_getsetlist, /* tp_getset */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4344 | 0, /* tp_base */ |
| 4345 | 0, /* tp_dict */ |
| 4346 | 0, /* tp_descr_get */ |
| 4347 | 0, /* tp_descr_set */ |
| 4348 | 0, /* tp_dictoffset */ |
Serhiy Storchaka | cb98556 | 2015-05-04 15:32:48 +0300 | [diff] [blame] | 4349 | _elementtree_XMLParser___init__, /* tp_init */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4350 | PyType_GenericAlloc, /* tp_alloc */ |
| 4351 | xmlparser_new, /* tp_new */ |
| 4352 | 0, /* tp_free */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4353 | }; |
| 4354 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4355 | /* ==================================================================== */ |
| 4356 | /* python module interface */ |
| 4357 | |
| 4358 | static PyMethodDef _functions[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4359 | {"SubElement", (PyCFunction)(void(*)(void)) subelement, METH_VARARGS | METH_KEYWORDS}, |
Stefan Behnel | 43851a2 | 2019-05-01 21:20:38 +0200 | [diff] [blame] | 4360 | _ELEMENTTREE__SET_FACTORIES_METHODDEF |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4361 | {NULL, NULL} |
| 4362 | }; |
| 4363 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4364 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4365 | static struct PyModuleDef elementtreemodule = { |
| 4366 | PyModuleDef_HEAD_INIT, |
| 4367 | "_elementtree", |
| 4368 | NULL, |
| 4369 | sizeof(elementtreestate), |
| 4370 | _functions, |
| 4371 | NULL, |
| 4372 | elementtree_traverse, |
| 4373 | elementtree_clear, |
| 4374 | elementtree_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4375 | }; |
| 4376 | |
Neal Norwitz | f6657e6 | 2006-12-28 04:47:50 +0000 | [diff] [blame] | 4377 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4378 | PyInit__elementtree(void) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4379 | { |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 4380 | PyObject *m, *temp; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4381 | elementtreestate *st; |
| 4382 | |
| 4383 | m = PyState_FindModule(&elementtreemodule); |
| 4384 | if (m) { |
| 4385 | Py_INCREF(m); |
| 4386 | return m; |
| 4387 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4388 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 4389 | /* Initialize object types */ |
Ronald Oussoren | 138d080 | 2013-07-19 11:11:25 +0200 | [diff] [blame] | 4390 | if (PyType_Ready(&ElementIter_Type) < 0) |
| 4391 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 4392 | if (PyType_Ready(&TreeBuilder_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 4393 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 4394 | if (PyType_Ready(&Element_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 4395 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 4396 | if (PyType_Ready(&XMLParser_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 4397 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4398 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4399 | m = PyModule_Create(&elementtreemodule); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 4400 | if (!m) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4401 | return NULL; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4402 | st = ET_STATE(m); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4403 | |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 4404 | if (!(temp = PyImport_ImportModule("copy"))) |
| 4405 | return NULL; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4406 | st->deepcopy_obj = PyObject_GetAttrString(temp, "deepcopy"); |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 4407 | Py_XDECREF(temp); |
| 4408 | |
Victor Stinner | b136f11 | 2017-07-10 22:28:02 +0200 | [diff] [blame] | 4409 | if (st->deepcopy_obj == NULL) { |
| 4410 | return NULL; |
| 4411 | } |
| 4412 | |
| 4413 | assert(!PyErr_Occurred()); |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4414 | if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath"))) |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 4415 | return NULL; |
| 4416 | |
Eli Bendersky | 20d4174 | 2012-06-01 09:48:37 +0300 | [diff] [blame] | 4417 | /* link against pyexpat */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4418 | expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); |
| 4419 | if (expat_capi) { |
| 4420 | /* check that it's usable */ |
| 4421 | if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 || |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 4422 | (size_t)expat_capi->size < sizeof(struct PyExpat_CAPI) || |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4423 | expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || |
| 4424 | expat_capi->MINOR_VERSION != XML_MINOR_VERSION || |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4425 | expat_capi->MICRO_VERSION != XML_MICRO_VERSION) { |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 4426 | PyErr_SetString(PyExc_ImportError, |
| 4427 | "pyexpat version is incompatible"); |
| 4428 | return NULL; |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4429 | } |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 4430 | } else { |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4431 | return NULL; |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 4432 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4433 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4434 | st->parseerror_obj = PyErr_NewException( |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 4435 | "xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4436 | ); |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 4437 | Py_INCREF(st->parseerror_obj); |
| 4438 | PyModule_AddObject(m, "ParseError", st->parseerror_obj); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4439 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 4440 | Py_INCREF((PyObject *)&Element_Type); |
| 4441 | PyModule_AddObject(m, "Element", (PyObject *)&Element_Type); |
| 4442 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 4443 | Py_INCREF((PyObject *)&TreeBuilder_Type); |
| 4444 | PyModule_AddObject(m, "TreeBuilder", (PyObject *)&TreeBuilder_Type); |
| 4445 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4446 | Py_INCREF((PyObject *)&XMLParser_Type); |
| 4447 | PyModule_AddObject(m, "XMLParser", (PyObject *)&XMLParser_Type); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 4448 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 4449 | return m; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4450 | } |