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