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