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