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