| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#include "Python.h" |
| 51 |
|
| 52 |
#define VERSION "1.0.6" |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#define USE_EXPAT |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define STATIC_CHILDREN 4 |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#if 0 |
| 80 |
static int memory = 0; |
| 81 |
#define ALLOC(size, comment)\ |
| 82 |
do { memory += size; printf("%8d - %s\n", memory, comment); } while (0) |
| 83 |
#define RELEASE(size, comment)\ |
| 84 |
do { memory -= size; printf("%8d - %s\n", memory, comment); } while (0) |
| 85 |
#else |
| 86 |
#define ALLOC(size, comment) |
| 87 |
#define RELEASE(size, comment) |
| 88 |
#endif |
| 89 |
|
| 90 |
|
| 91 |
#if defined(_MSC_VER) |
| 92 |
#define LOCAL(type) static __inline type __fastcall |
| 93 |
#else |
| 94 |
#define LOCAL(type) static type |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
#if (PY_VERSION_HEX < 0x02060000) |
| 99 |
#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) |
| 100 |
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) |
| 101 |
#endif |
| 102 |
|
| 103 |
#if (PY_VERSION_HEX < 0x02050000) |
| 104 |
typedef int Py_ssize_t; |
| 105 |
#define lenfunc inquiry |
| 106 |
#endif |
| 107 |
|
| 108 |
#if (PY_VERSION_HEX < 0x02040000) |
| 109 |
#define PyDict_CheckExact PyDict_Check |
| 110 |
|
| 111 |
#if !defined(Py_RETURN_NONE) |
| 112 |
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None |
| 113 |
#endif |
| 114 |
#endif |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#define JOIN_GET(p) ((Py_uintptr_t) (p) & 1) |
| 121 |
#define JOIN_SET(p, flag) ((void*) ((Py_uintptr_t) (JOIN_OBJ(p)) | (flag))) |
| 122 |
#define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~1)) |
| 123 |
|
| 124 |
|
| 125 |
static PyObject* elementtree_parseerror_obj; |
| 126 |
static PyObject* elementtree_copyelement_obj; |
| 127 |
static PyObject* elementtree_deepcopy_obj; |
| 128 |
static PyObject* elementtree_iter_obj; |
| 129 |
static PyObject* elementtree_itertext_obj; |
| 130 |
static PyObject* elementpath_obj; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
Py_LOCAL_INLINE(void) |
| 136 |
_set_joined_ptr(PyObject **p, PyObject *new_joined_ptr) |
| 137 |
{ |
| 138 |
PyObject *tmp = JOIN_OBJ(*p); |
| 139 |
*p = new_joined_ptr; |
| 140 |
Py_DECREF(tmp); |
| 141 |
} |
| 142 |
|
| 143 |
LOCAL(PyObject*) |
| 144 |
deepcopy(PyObject* object, PyObject* memo) |
| 145 |
{ |
| 146 |
|
| 147 |
|
| 148 |
PyObject* args; |
| 149 |
PyObject* result; |
| 150 |
|
| 151 |
if (!elementtree_deepcopy_obj) { |
| 152 |
PyErr_SetString( |
| 153 |
PyExc_RuntimeError, |
| 154 |
"deepcopy helper not found" |
| 155 |
); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
args = PyTuple_New(2); |
| 160 |
if (!args) |
| 161 |
return NULL; |
| 162 |
|
| 163 |
Py_INCREF(object); PyTuple_SET_ITEM(args, 0, (PyObject*) object); |
| 164 |
Py_INCREF(memo); PyTuple_SET_ITEM(args, 1, (PyObject*) memo); |
| 165 |
|
| 166 |
result = PyObject_CallObject(elementtree_deepcopy_obj, args); |
| 167 |
|
| 168 |
Py_DECREF(args); |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
LOCAL(PyObject*) |
| 174 |
list_join(PyObject* list) |
| 175 |
{ |
| 176 |
|
| 177 |
PyObject* joiner; |
| 178 |
PyObject* function; |
| 179 |
PyObject* args; |
| 180 |
PyObject* result; |
| 181 |
|
| 182 |
switch (PyList_GET_SIZE(list)) { |
| 183 |
case 0: |
| 184 |
return PyString_FromString(""); |
| 185 |
case 1: |
| 186 |
result = PyList_GET_ITEM(list, 0); |
| 187 |
Py_INCREF(result); |
| 188 |
return result; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
joiner = PySequence_GetSlice(PyList_GET_ITEM(list, 0), 0, 0); |
| 195 |
if (!joiner) |
| 196 |
return NULL; |
| 197 |
|
| 198 |
function = PyObject_GetAttrString(joiner, "join"); |
| 199 |
if (!function) { |
| 200 |
Py_DECREF(joiner); |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
|
| 204 |
args = PyTuple_New(1); |
| 205 |
if (!args) { |
| 206 |
Py_DECREF(function); |
| 207 |
Py_DECREF(joiner); |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
|
| 211 |
Py_INCREF(list); |
| 212 |
PyTuple_SET_ITEM(args, 0, list); |
| 213 |
|
| 214 |
result = PyObject_CallObject(function, args); |
| 215 |
|
| 216 |
Py_DECREF(args); |
| 217 |
Py_DECREF(function); |
| 218 |
Py_DECREF(joiner); |
| 219 |
|
| 220 |
return result; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
typedef struct { |
| 227 |
|
| 228 |
|
| 229 |
PyObject* attrib; |
| 230 |
|
| 231 |
|
| 232 |
int length; |
| 233 |
int allocated; |
| 234 |
|
| 235 |
|
| 236 |
PyObject* *children; |
| 237 |
|
| 238 |
PyObject* _children[STATIC_CHILDREN]; |
| 239 |
|
| 240 |
} ElementObjectExtra; |
| 241 |
|
| 242 |
typedef struct { |
| 243 |
PyObject_HEAD |
| 244 |
|
| 245 |
|
| 246 |
PyObject* tag; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
PyObject* text; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
PyObject* tail; |
| 259 |
|
| 260 |
ElementObjectExtra* extra; |
| 261 |
|
| 262 |
} ElementObject; |
| 263 |
|
| 264 |
staticforward PyTypeObject Element_Type; |
| 265 |
|
| 266 |
#define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
LOCAL(int) |
| 272 |
element_new_extra(ElementObject* self, PyObject* attrib) |
| 273 |
{ |
| 274 |
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); |
| 275 |
if (!self->extra) |
| 276 |
return -1; |
| 277 |
|
| 278 |
if (!attrib) |
| 279 |
attrib = Py_None; |
| 280 |
|
| 281 |
Py_INCREF(attrib); |
| 282 |
self->extra->attrib = attrib; |
| 283 |
|
| 284 |
self->extra->length = 0; |
| 285 |
self->extra->allocated = STATIC_CHILDREN; |
| 286 |
self->extra->children = self->extra->_children; |
| 287 |
|
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
LOCAL(void) |
| 292 |
element_dealloc_extra(ElementObject* self) |
| 293 |
{ |
| 294 |
int i; |
| 295 |
|
| 296 |
Py_DECREF(self->extra->attrib); |
| 297 |
|
| 298 |
for (i = 0; i < self->extra->length; i++) |
| 299 |
Py_DECREF(self->extra->children[i]); |
| 300 |
|
| 301 |
if (self->extra->children != self->extra->_children) |
| 302 |
PyObject_Free(self->extra->children); |
| 303 |
|
| 304 |
PyObject_Free(self->extra); |
| 305 |
} |
| 306 |
|
| 307 |
LOCAL(PyObject*) |
| 308 |
element_new(PyObject* tag, PyObject* attrib) |
| 309 |
{ |
| 310 |
ElementObject* self; |
| 311 |
|
| 312 |
self = PyObject_New(ElementObject, &Element_Type); |
| 313 |
if (self == NULL) |
| 314 |
return NULL; |
| 315 |
|
| 316 |
|
| 317 |
if (PyDict_CheckExact(attrib) && !PyDict_Size(attrib)) |
| 318 |
attrib = Py_None; |
| 319 |
|
| 320 |
self->extra = NULL; |
| 321 |
|
| 322 |
if (attrib != Py_None) { |
| 323 |
|
| 324 |
if (element_new_extra(self, attrib) < 0) { |
| 325 |
PyObject_Del(self); |
| 326 |
return NULL; |
| 327 |
} |
| 328 |
|
| 329 |
self->extra->length = 0; |
| 330 |
self->extra->allocated = STATIC_CHILDREN; |
| 331 |
self->extra->children = self->extra->_children; |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
Py_INCREF(tag); |
| 336 |
self->tag = tag; |
| 337 |
|
| 338 |
Py_INCREF(Py_None); |
| 339 |
self->text = Py_None; |
| 340 |
|
| 341 |
Py_INCREF(Py_None); |
| 342 |
self->tail = Py_None; |
| 343 |
|
| 344 |
ALLOC(sizeof(ElementObject), "create element"); |
| 345 |
|
| 346 |
return (PyObject*) self; |
| 347 |
} |
| 348 |
|
| 349 |
LOCAL(int) |
| 350 |
element_resize(ElementObject* self, Py_ssize_t extra) |
| 351 |
{ |
| 352 |
Py_ssize_t size; |
| 353 |
PyObject* *children; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if (!self->extra) |
| 359 |
element_new_extra(self, NULL); |
| 360 |
|
| 361 |
size = self->extra->length + extra; |
| 362 |
|
| 363 |
if (size > self->extra->allocated) { |
| 364 |
|
| 365 |
size = (size >> 3) + (size < 9 ? 3 : 6) + size; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
size = size ? size : 1; |
| 372 |
if ((size_t)size > PY_SSIZE_T_MAX/sizeof(PyObject*)) |
| 373 |
goto nomemory; |
| 374 |
if (size > INT_MAX) { |
| 375 |
PyErr_SetString(PyExc_OverflowError, |
| 376 |
"too many children"); |
| 377 |
return -1; |
| 378 |
} |
| 379 |
if (self->extra->children != self->extra->_children) { |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
children = PyObject_Realloc(self->extra->children, |
| 385 |
size * sizeof(PyObject*)); |
| 386 |
if (!children) |
| 387 |
goto nomemory; |
| 388 |
} else { |
| 389 |
children = PyObject_Malloc(size * sizeof(PyObject*)); |
| 390 |
if (!children) |
| 391 |
goto nomemory; |
| 392 |
|
| 393 |
memcpy(children, self->extra->children, |
| 394 |
self->extra->length * sizeof(PyObject*)); |
| 395 |
} |
| 396 |
self->extra->children = children; |
| 397 |
self->extra->allocated = size; |
| 398 |
} |
| 399 |
|
| 400 |
return 0; |
| 401 |
|
| 402 |
nomemory: |
| 403 |
PyErr_NoMemory(); |
| 404 |
return -1; |
| 405 |
} |
| 406 |
|
| 407 |
LOCAL(int) |
| 408 |
element_add_subelement(ElementObject* self, PyObject* element) |
| 409 |
{ |
| 410 |
|
| 411 |
|
| 412 |
if (element_resize(self, 1) < 0) |
| 413 |
return -1; |
| 414 |
|
| 415 |
Py_INCREF(element); |
| 416 |
self->extra->children[self->extra->length] = element; |
| 417 |
|
| 418 |
self->extra->length++; |
| 419 |
|
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
LOCAL(PyObject*) |
| 424 |
element_get_attrib(ElementObject* self) |
| 425 |
{ |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
PyObject* res = self->extra->attrib; |
| 430 |
|
| 431 |
if (res == Py_None) { |
| 432 |
Py_DECREF(res); |
| 433 |
|
| 434 |
res = PyDict_New(); |
| 435 |
if (!res) |
| 436 |
return NULL; |
| 437 |
self->extra->attrib = res; |
| 438 |
} |
| 439 |
|
| 440 |
return res; |
| 441 |
} |
| 442 |
|
| 443 |
LOCAL(PyObject*) |
| 444 |
element_get_text(ElementObject* self) |
| 445 |
{ |
| 446 |
|
| 447 |
|
| 448 |
PyObject *res = self->text; |
| 449 |
|
| 450 |
if (JOIN_GET(res)) { |
| 451 |
res = JOIN_OBJ(res); |
| 452 |
if (PyList_CheckExact(res)) { |
| 453 |
PyObject *tmp = list_join(res); |
| 454 |
if (!tmp) |
| 455 |
return NULL; |
| 456 |
self->text = tmp; |
| 457 |
Py_DECREF(res); |
| 458 |
res = tmp; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
return res; |
| 463 |
} |
| 464 |
|
| 465 |
LOCAL(PyObject*) |
| 466 |
element_get_tail(ElementObject* self) |
| 467 |
{ |
| 468 |
|
| 469 |
|
| 470 |
PyObject *res = self->tail; |
| 471 |
|
| 472 |
if (JOIN_GET(res)) { |
| 473 |
res = JOIN_OBJ(res); |
| 474 |
if (PyList_CheckExact(res)) { |
| 475 |
PyObject *tmp = list_join(res); |
| 476 |
if (!tmp) |
| 477 |
return NULL; |
| 478 |
self->tail = tmp; |
| 479 |
Py_DECREF(res); |
| 480 |
res = tmp; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
return res; |
| 485 |
} |
| 486 |
|
| 487 |
static PyObject* |
| 488 |
element(PyObject* self, PyObject* args, PyObject* kw) |
| 489 |
{ |
| 490 |
PyObject* elem; |
| 491 |
|
| 492 |
PyObject* tag; |
| 493 |
PyObject* attrib = NULL; |
| 494 |
if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, |
| 495 |
&PyDict_Type, &attrib)) |
| 496 |
return NULL; |
| 497 |
|
| 498 |
if (attrib || kw) { |
| 499 |
attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); |
| 500 |
if (!attrib) |
| 501 |
return NULL; |
| 502 |
if (kw != NULL && PyDict_Update(attrib, kw) < 0) { |
| 503 |
Py_DECREF(attrib); |
| 504 |
return NULL; |
| 505 |
} |
| 506 |
} else { |
| 507 |
Py_INCREF(Py_None); |
| 508 |
attrib = Py_None; |
| 509 |
} |
| 510 |
|
| 511 |
elem = element_new(tag, attrib); |
| 512 |
|
| 513 |
Py_DECREF(attrib); |
| 514 |
|
| 515 |
return elem; |
| 516 |
} |
| 517 |
|
| 518 |
static PyObject* |
| 519 |
subelement(PyObject* self, PyObject* args, PyObject* kw) |
| 520 |
{ |
| 521 |
PyObject* elem; |
| 522 |
|
| 523 |
ElementObject* parent; |
| 524 |
PyObject* tag; |
| 525 |
PyObject* attrib = NULL; |
| 526 |
if (!PyArg_ParseTuple(args, "O!O|O!:SubElement", |
| 527 |
&Element_Type, &parent, &tag, |
| 528 |
&PyDict_Type, &attrib)) |
| 529 |
return NULL; |
| 530 |
|
| 531 |
if (attrib || kw) { |
| 532 |
attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); |
| 533 |
if (!attrib) |
| 534 |
return NULL; |
| 535 |
if (kw != NULL && PyDict_Update(attrib, kw) < 0) { |
| 536 |
Py_DECREF(attrib); |
| 537 |
return NULL; |
| 538 |
} |
| 539 |
} else { |
| 540 |
Py_INCREF(Py_None); |
| 541 |
attrib = Py_None; |
| 542 |
} |
| 543 |
|
| 544 |
elem = element_new(tag, attrib); |
| 545 |
Py_DECREF(attrib); |
| 546 |
if (elem == NULL) |
| 547 |
return NULL; |
| 548 |
|
| 549 |
if (element_add_subelement(parent, elem) < 0) { |
| 550 |
Py_DECREF(elem); |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
|
| 554 |
return elem; |
| 555 |
} |
| 556 |
|
| 557 |
static void |
| 558 |
element_dealloc(ElementObject* self) |
| 559 |
{ |
| 560 |
if (self->extra) |
| 561 |
element_dealloc_extra(self); |
| 562 |
|
| 563 |
|
| 564 |
Py_DECREF(self->tag); |
| 565 |
Py_DECREF(JOIN_OBJ(self->text)); |
| 566 |
Py_DECREF(JOIN_OBJ(self->tail)); |
| 567 |
|
| 568 |
RELEASE(sizeof(ElementObject), "destroy element"); |
| 569 |
|
| 570 |
PyObject_Del(self); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
static PyObject* |
| 577 |
element_append(ElementObject* self, PyObject* args) |
| 578 |
{ |
| 579 |
PyObject* element; |
| 580 |
if (!PyArg_ParseTuple(args, "O!:append", &Element_Type, &element)) |
| 581 |
return NULL; |
| 582 |
|
| 583 |
if (element_add_subelement(self, element) < 0) |
| 584 |
return NULL; |
| 585 |
|
| 586 |
Py_RETURN_NONE; |
| 587 |
} |
| 588 |
|
| 589 |
static PyObject* |
| 590 |
element_clear(ElementObject* self, PyObject* args) |
| 591 |
{ |
| 592 |
if (!PyArg_ParseTuple(args, ":clear")) |
| 593 |
return NULL; |
| 594 |
|
| 595 |
if (self->extra) { |
| 596 |
element_dealloc_extra(self); |
| 597 |
self->extra = NULL; |
| 598 |
} |
| 599 |
|
| 600 |
Py_INCREF(Py_None); |
| 601 |
_set_joined_ptr(&self->text, Py_None); |
| 602 |
|
| 603 |
Py_INCREF(Py_None); |
| 604 |
_set_joined_ptr(&self->tail, Py_None); |
| 605 |
|
| 606 |
Py_RETURN_NONE; |
| 607 |
} |
| 608 |
|
| 609 |
static PyObject* |
| 610 |
element_copy(ElementObject* self, PyObject* args) |
| 611 |
{ |
| 612 |
int i; |
| 613 |
ElementObject* element; |
| 614 |
|
| 615 |
if (!PyArg_ParseTuple(args, ":__copy__")) |
| 616 |
return NULL; |
| 617 |
|
| 618 |
element = (ElementObject*) element_new( |
| 619 |
self->tag, (self->extra) ? self->extra->attrib : Py_None |
| 620 |
); |
| 621 |
if (!element) |
| 622 |
return NULL; |
| 623 |
|
| 624 |
Py_INCREF(JOIN_OBJ(self->text)); |
| 625 |
_set_joined_ptr(&element->text, self->text); |
| 626 |
|
| 627 |
Py_INCREF(JOIN_OBJ(self->tail)); |
| 628 |
_set_joined_ptr(&element->tail, self->tail); |
| 629 |
|
| 630 |
if (self->extra) { |
| 631 |
|
| 632 |
if (element_resize(element, self->extra->length) < 0) { |
| 633 |
Py_DECREF(element); |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
|
| 637 |
for (i = 0; i < self->extra->length; i++) { |
| 638 |
Py_INCREF(self->extra->children[i]); |
| 639 |
element->extra->children[i] = self->extra->children[i]; |
| 640 |
} |
| 641 |
|
| 642 |
element->extra->length = self->extra->length; |
| 643 |
|
| 644 |
} |
| 645 |
|
| 646 |
return (PyObject*) element; |
| 647 |
} |
| 648 |
|
| 649 |
static PyObject* |
| 650 |
element_deepcopy(ElementObject* self, PyObject* args) |
| 651 |
{ |
| 652 |
int i; |
| 653 |
ElementObject* element; |
| 654 |
PyObject* tag; |
| 655 |
PyObject* attrib; |
| 656 |
PyObject* text; |
| 657 |
PyObject* tail; |
| 658 |
PyObject* id; |
| 659 |
|
| 660 |
PyObject* memo; |
| 661 |
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo)) |
| 662 |
return NULL; |
| 663 |
|
| 664 |
tag = deepcopy(self->tag, memo); |
| 665 |
if (!tag) |
| 666 |
return NULL; |
| 667 |
|
| 668 |
if (self->extra) { |
| 669 |
attrib = deepcopy(self->extra->attrib, memo); |
| 670 |
if (!attrib) { |
| 671 |
Py_DECREF(tag); |
| 672 |
return NULL; |
| 673 |
} |
| 674 |
} else { |
| 675 |
Py_INCREF(Py_None); |
| 676 |
attrib = Py_None; |
| 677 |
} |
| 678 |
|
| 679 |
element = (ElementObject*) element_new(tag, attrib); |
| 680 |
|
| 681 |
Py_DECREF(tag); |
| 682 |
Py_DECREF(attrib); |
| 683 |
|
| 684 |
if (!element) |
| 685 |
return NULL; |
| 686 |
|
| 687 |
text = deepcopy(JOIN_OBJ(self->text), memo); |
| 688 |
if (!text) |
| 689 |
goto error; |
| 690 |
_set_joined_ptr(&element->text, JOIN_SET(text, JOIN_GET(self->text))); |
| 691 |
|
| 692 |
tail = deepcopy(JOIN_OBJ(self->tail), memo); |
| 693 |
if (!tail) |
| 694 |
goto error; |
| 695 |
_set_joined_ptr(&element->tail, JOIN_SET(tail, JOIN_GET(self->tail))); |
| 696 |
|
| 697 |
if (self->extra) { |
| 698 |
|
| 699 |
if (element_resize(element, self->extra->length) < 0) |
| 700 |
goto error; |
| 701 |
|
| 702 |
for (i = 0; i < self->extra->length; i++) { |
| 703 |
PyObject* child = deepcopy(self->extra->children[i], memo); |
| 704 |
if (!child) { |
| 705 |
element->extra->length = i; |
| 706 |
goto error; |
| 707 |
} |
| 708 |
element->extra->children[i] = child; |
| 709 |
} |
| 710 |
|
| 711 |
element->extra->length = self->extra->length; |
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
id = PyInt_FromLong((Py_uintptr_t) self); |
| 717 |
if (!id) |
| 718 |
goto error; |
| 719 |
|
| 720 |
i = PyDict_SetItem(memo, id, (PyObject*) element); |
| 721 |
|
| 722 |
Py_DECREF(id); |
| 723 |
|
| 724 |
if (i < 0) |
| 725 |
goto error; |
| 726 |
|
| 727 |
return (PyObject*) element; |
| 728 |
|
| 729 |
error: |
| 730 |
Py_DECREF(element); |
| 731 |
return NULL; |
| 732 |
} |
| 733 |
|
| 734 |
LOCAL(int) |
| 735 |
checkpath(PyObject* tag) |
| 736 |
{ |
| 737 |
Py_ssize_t i; |
| 738 |
int check = 1; |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
#define PATHCHAR(ch) \ |
| 743 |
(ch == '/' || ch == '*' || ch == '[' || ch == '@' || ch == '.') |
| 744 |
|
| 745 |
#if defined(Py_USING_UNICODE) |
| 746 |
if (PyUnicode_Check(tag)) { |
| 747 |
Py_UNICODE *p = PyUnicode_AS_UNICODE(tag); |
| 748 |
for (i = 0; i < PyUnicode_GET_SIZE(tag); i++) { |
| 749 |
if (p[i] == '{') |
| 750 |
check = 0; |
| 751 |
else if (p[i] == '}') |
| 752 |
check = 1; |
| 753 |
else if (check && PATHCHAR(p[i])) |
| 754 |
return 1; |
| 755 |
} |
| 756 |
return 0; |
| 757 |
} |
| 758 |
#endif |
| 759 |
if (PyString_Check(tag)) { |
| 760 |
char *p = PyString_AS_STRING(tag); |
| 761 |
for (i = 0; i < PyString_GET_SIZE(tag); i++) { |
| 762 |
if (p[i] == '{') |
| 763 |
check = 0; |
| 764 |
else if (p[i] == '}') |
| 765 |
check = 1; |
| 766 |
else if (check && PATHCHAR(p[i])) |
| 767 |
return 1; |
| 768 |
} |
| 769 |
return 0; |
| 770 |
} |
| 771 |
|
| 772 |
return 1; |
| 773 |
} |
| 774 |
|
| 775 |
static PyObject* |
| 776 |
element_extend(ElementObject* self, PyObject* args) |
| 777 |
{ |
| 778 |
PyObject* seq; |
| 779 |
Py_ssize_t i; |
| 780 |
|
| 781 |
PyObject* seq_in; |
| 782 |
if (!PyArg_ParseTuple(args, "O:extend", &seq_in)) |
| 783 |
return NULL; |
| 784 |
|
| 785 |
seq = PySequence_Fast(seq_in, ""); |
| 786 |
if (!seq) { |
| 787 |
PyErr_Format( |
| 788 |
PyExc_TypeError, |
| 789 |
"expected sequence, not \"%.200s\"", Py_TYPE(seq_in)->tp_name |
| 790 |
); |
| 791 |
return NULL; |
| 792 |
} |
| 793 |
|
| 794 |
for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) { |
| 795 |
PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 796 |
if (element_add_subelement(self, element) < 0) { |
| 797 |
Py_DECREF(seq); |
| 798 |
return NULL; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
Py_DECREF(seq); |
| 803 |
|
| 804 |
Py_RETURN_NONE; |
| 805 |
} |
| 806 |
|
| 807 |
static PyObject* |
| 808 |
element_find(ElementObject* self, PyObject* args) |
| 809 |
{ |
| 810 |
int i; |
| 811 |
|
| 812 |
PyObject* tag; |
| 813 |
PyObject* namespaces = Py_None; |
| 814 |
if (!PyArg_ParseTuple(args, "O|O:find", &tag, &namespaces)) |
| 815 |
return NULL; |
| 816 |
|
| 817 |
if (checkpath(tag) || namespaces != Py_None) |
| 818 |
return PyObject_CallMethod( |
| 819 |
elementpath_obj, "find", "OOO", self, tag, namespaces |
| 820 |
); |
| 821 |
|
| 822 |
if (!self->extra) |
| 823 |
Py_RETURN_NONE; |
| 824 |
|
| 825 |
for (i = 0; i < self->extra->length; i++) { |
| 826 |
PyObject* item = self->extra->children[i]; |
| 827 |
int rc; |
| 828 |
if (!Element_CheckExact(item)) |
| 829 |
continue; |
| 830 |
Py_INCREF(item); |
| 831 |
rc = PyObject_Compare(((ElementObject*)item)->tag, tag); |
| 832 |
if (rc == 0) |
| 833 |
return item; |
| 834 |
Py_DECREF(item); |
| 835 |
if (rc < 0 && PyErr_Occurred()) |
| 836 |
return NULL; |
| 837 |
} |
| 838 |
|
| 839 |
Py_RETURN_NONE; |
| 840 |
} |
| 841 |
|
| 842 |
static PyObject* |
| 843 |
element_findtext(ElementObject* self, PyObject* args) |
| 844 |
{ |
| 845 |
int i; |
| 846 |
|
| 847 |
PyObject* tag; |
| 848 |
PyObject* default_value = Py_None; |
| 849 |
PyObject* namespaces = Py_None; |
| 850 |
if (!PyArg_ParseTuple(args, "O|OO:findtext", &tag, &default_value, &namespaces)) |
| 851 |
return NULL; |
| 852 |
|
| 853 |
if (checkpath(tag) || namespaces != Py_None) |
| 854 |
return PyObject_CallMethod( |
| 855 |
elementpath_obj, "findtext", "OOOO", self, tag, default_value, namespaces |
| 856 |
); |
| 857 |
|
| 858 |
if (!self->extra) { |
| 859 |
Py_INCREF(default_value); |
| 860 |
return default_value; |
| 861 |
} |
| 862 |
|
| 863 |
for (i = 0; i < self->extra->length; i++) { |
| 864 |
ElementObject* item = (ElementObject*) self->extra->children[i]; |
| 865 |
int rc; |
| 866 |
if (!Element_CheckExact(item)) |
| 867 |
continue; |
| 868 |
Py_INCREF(item); |
| 869 |
rc = PyObject_Compare(item->tag, tag); |
| 870 |
if (rc == 0) { |
| 871 |
PyObject* text = element_get_text(item); |
| 872 |
if (text == Py_None) { |
| 873 |
Py_DECREF(item); |
| 874 |
return PyString_FromString(""); |
| 875 |
} |
| 876 |
Py_XINCREF(text); |
| 877 |
Py_DECREF(item); |
| 878 |
return text; |
| 879 |
} |
| 880 |
Py_DECREF(item); |
| 881 |
if (rc < 0 && PyErr_Occurred()) |
| 882 |
return NULL; |
| 883 |
} |
| 884 |
|
| 885 |
Py_INCREF(default_value); |
| 886 |
return default_value; |
| 887 |
} |
| 888 |
|
| 889 |
static PyObject* |
| 890 |
element_findall(ElementObject* self, PyObject* args) |
| 891 |
{ |
| 892 |
int i; |
| 893 |
PyObject* out; |
| 894 |
|
| 895 |
PyObject* tag; |
| 896 |
PyObject* namespaces = Py_None; |
| 897 |
if (!PyArg_ParseTuple(args, "O|O:findall", &tag, &namespaces)) |
| 898 |
return NULL; |
| 899 |
|
| 900 |
if (checkpath(tag) || namespaces != Py_None) |
| 901 |
return PyObject_CallMethod( |
| 902 |
elementpath_obj, "findall", "OOO", self, tag, namespaces |
| 903 |
); |
| 904 |
|
| 905 |
out = PyList_New(0); |
| 906 |
if (!out) |
| 907 |
return NULL; |
| 908 |
|
| 909 |
if (!self->extra) |
| 910 |
return out; |
| 911 |
|
| 912 |
for (i = 0; i < self->extra->length; i++) { |
| 913 |
PyObject* item = self->extra->children[i]; |
| 914 |
int rc; |
| 915 |
if (!Element_CheckExact(item)) |
| 916 |
continue; |
| 917 |
Py_INCREF(item); |
| 918 |
rc = PyObject_Compare(((ElementObject*)item)->tag, tag); |
| 919 |
if (rc == 0) |
| 920 |
rc = PyList_Append(out, item); |
| 921 |
Py_DECREF(item); |
| 922 |
if (rc < 0 && PyErr_Occurred()) { |
| 923 |
Py_DECREF(out); |
| 924 |
return NULL; |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
return out; |
| 929 |
} |
| 930 |
|
| 931 |
static PyObject* |
| 932 |
element_iterfind(ElementObject* self, PyObject* args) |
| 933 |
{ |
| 934 |
PyObject* tag; |
| 935 |
PyObject* namespaces = Py_None; |
| 936 |
if (!PyArg_ParseTuple(args, "O|O:iterfind", &tag, &namespaces)) |
| 937 |
return NULL; |
| 938 |
|
| 939 |
return PyObject_CallMethod( |
| 940 |
elementpath_obj, "iterfind", "OOO", self, tag, namespaces |
| 941 |
); |
| 942 |
} |
| 943 |
|
| 944 |
static PyObject* |
| 945 |
element_get(ElementObject* self, PyObject* args) |
| 946 |
{ |
| 947 |
PyObject* value; |
| 948 |
|
| 949 |
PyObject* key; |
| 950 |
PyObject* default_value = Py_None; |
| 951 |
if (!PyArg_ParseTuple(args, "O|O:get", &key, &default_value)) |
| 952 |
return NULL; |
| 953 |
|
| 954 |
if (!self->extra || self->extra->attrib == Py_None) |
| 955 |
value = default_value; |
| 956 |
else { |
| 957 |
value = PyDict_GetItem(self->extra->attrib, key); |
| 958 |
if (!value) |
| 959 |
value = default_value; |
| 960 |
} |
| 961 |
|
| 962 |
Py_INCREF(value); |
| 963 |
return value; |
| 964 |
} |
| 965 |
|
| 966 |
static PyObject* |
| 967 |
element_getchildren(ElementObject* self, PyObject* args) |
| 968 |
{ |
| 969 |
int i; |
| 970 |
PyObject* list; |
| 971 |
|
| 972 |
if (PyErr_WarnPy3k("This method will be removed in future versions. " |
| 973 |
"Use 'list(elem)' or iteration over elem instead.", |
| 974 |
1) < 0) { |
| 975 |
return NULL; |
| 976 |
} |
| 977 |
|
| 978 |
if (!PyArg_ParseTuple(args, ":getchildren")) |
| 979 |
return NULL; |
| 980 |
|
| 981 |
if (!self->extra) |
| 982 |
return PyList_New(0); |
| 983 |
|
| 984 |
list = PyList_New(self->extra->length); |
| 985 |
if (!list) |
| 986 |
return NULL; |
| 987 |
|
| 988 |
for (i = 0; i < self->extra->length; i++) { |
| 989 |
PyObject* item = self->extra->children[i]; |
| 990 |
Py_INCREF(item); |
| 991 |
PyList_SET_ITEM(list, i, item); |
| 992 |
} |
| 993 |
|
| 994 |
return list; |
| 995 |
} |
| 996 |
|
| 997 |
static PyObject* |
| 998 |
element_iter_impl(ElementObject* self, PyObject* tag) |
| 999 |
{ |
| 1000 |
PyObject* args; |
| 1001 |
PyObject* result; |
| 1002 |
|
| 1003 |
if (!elementtree_iter_obj) { |
| 1004 |
PyErr_SetString( |
| 1005 |
PyExc_RuntimeError, |
| 1006 |
"iter helper not found" |
| 1007 |
); |
| 1008 |
return NULL; |
| 1009 |
} |
| 1010 |
|
| 1011 |
args = PyTuple_New(2); |
| 1012 |
if (!args) |
| 1013 |
return NULL; |
| 1014 |
|
| 1015 |
Py_INCREF(self); PyTuple_SET_ITEM(args, 0, (PyObject*) self); |
| 1016 |
Py_INCREF(tag); PyTuple_SET_ITEM(args, 1, (PyObject*) tag); |
| 1017 |
|
| 1018 |
result = PyObject_CallObject(elementtree_iter_obj, args); |
| 1019 |
|
| 1020 |
Py_DECREF(args); |
| 1021 |
|
| 1022 |
return result; |
| 1023 |
} |
| 1024 |
|
| 1025 |
static PyObject* |
| 1026 |
element_iter(ElementObject* self, PyObject* args) |
| 1027 |
{ |
| 1028 |
PyObject* tag = Py_None; |
| 1029 |
if (!PyArg_ParseTuple(args, "|O:iter", &tag)) |
| 1030 |
return NULL; |
| 1031 |
|
| 1032 |
return element_iter_impl(self, tag); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static PyObject* |
| 1036 |
element_getiterator(ElementObject* self, PyObject* args) |
| 1037 |
{ |
| 1038 |
PyObject* tag = Py_None; |
| 1039 |
if (!PyArg_ParseTuple(args, "|O:getiterator", &tag)) |
| 1040 |
return NULL; |
| 1041 |
|
| 1042 |
|
| 1043 |
if (Py_Py3kWarningFlag && |
| 1044 |
PyErr_WarnEx(PyExc_PendingDeprecationWarning, |
| 1045 |
"This method will be removed in future versions. " |
| 1046 |
"Use 'tree.iter()' or 'list(tree.iter())' instead.", |
| 1047 |
1) < 0) { |
| 1048 |
return NULL; |
| 1049 |
} |
| 1050 |
return element_iter_impl(self, tag); |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
static PyObject* |
| 1055 |
element_itertext(ElementObject* self, PyObject* args) |
| 1056 |
{ |
| 1057 |
PyObject* result; |
| 1058 |
|
| 1059 |
if (!PyArg_ParseTuple(args, ":itertext")) |
| 1060 |
return NULL; |
| 1061 |
|
| 1062 |
if (!elementtree_itertext_obj) { |
| 1063 |
PyErr_SetString( |
| 1064 |
PyExc_RuntimeError, |
| 1065 |
"itertext helper not found" |
| 1066 |
); |
| 1067 |
return NULL; |
| 1068 |
} |
| 1069 |
|
| 1070 |
args = PyTuple_New(1); |
| 1071 |
if (!args) |
| 1072 |
return NULL; |
| 1073 |
|
| 1074 |
Py_INCREF(self); PyTuple_SET_ITEM(args, 0, (PyObject*) self); |
| 1075 |
|
| 1076 |
result = PyObject_CallObject(elementtree_itertext_obj, args); |
| 1077 |
|
| 1078 |
Py_DECREF(args); |
| 1079 |
|
| 1080 |
return result; |
| 1081 |
} |
| 1082 |
|
| 1083 |
static PyObject* |
| 1084 |
element_getitem(PyObject* self_, Py_ssize_t index) |
| 1085 |
{ |
| 1086 |
ElementObject* self = (ElementObject*) self_; |
| 1087 |
|
| 1088 |
if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1089 |
PyErr_SetString( |
| 1090 |
PyExc_IndexError, |
| 1091 |
"child index out of range" |
| 1092 |
); |
| 1093 |
return NULL; |
| 1094 |
} |
| 1095 |
|
| 1096 |
Py_INCREF(self->extra->children[index]); |
| 1097 |
return self->extra->children[index]; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static PyObject* |
| 1101 |
element_insert(ElementObject* self, PyObject* args) |
| 1102 |
{ |
| 1103 |
int i; |
| 1104 |
|
| 1105 |
int index; |
| 1106 |
PyObject* element; |
| 1107 |
if (!PyArg_ParseTuple(args, "iO!:insert", &index, |
| 1108 |
&Element_Type, &element)) |
| 1109 |
return NULL; |
| 1110 |
|
| 1111 |
if (!self->extra) |
| 1112 |
element_new_extra(self, NULL); |
| 1113 |
|
| 1114 |
if (index < 0) { |
| 1115 |
index += self->extra->length; |
| 1116 |
if (index < 0) |
| 1117 |
index = 0; |
| 1118 |
} |
| 1119 |
if (index > self->extra->length) |
| 1120 |
index = self->extra->length; |
| 1121 |
|
| 1122 |
if (element_resize(self, 1) < 0) |
| 1123 |
return NULL; |
| 1124 |
|
| 1125 |
for (i = self->extra->length; i > index; i--) |
| 1126 |
self->extra->children[i] = self->extra->children[i-1]; |
| 1127 |
|
| 1128 |
Py_INCREF(element); |
| 1129 |
self->extra->children[index] = element; |
| 1130 |
|
| 1131 |
self->extra->length++; |
| 1132 |
|
| 1133 |
Py_RETURN_NONE; |
| 1134 |
} |
| 1135 |
|
| 1136 |
static PyObject* |
| 1137 |
element_items(ElementObject* self, PyObject* args) |
| 1138 |
{ |
| 1139 |
if (!PyArg_ParseTuple(args, ":items")) |
| 1140 |
return NULL; |
| 1141 |
|
| 1142 |
if (!self->extra || self->extra->attrib == Py_None) |
| 1143 |
return PyList_New(0); |
| 1144 |
|
| 1145 |
return PyDict_Items(self->extra->attrib); |
| 1146 |
} |
| 1147 |
|
| 1148 |
static PyObject* |
| 1149 |
element_keys(ElementObject* self, PyObject* args) |
| 1150 |
{ |
| 1151 |
if (!PyArg_ParseTuple(args, ":keys")) |
| 1152 |
return NULL; |
| 1153 |
|
| 1154 |
if (!self->extra || self->extra->attrib == Py_None) |
| 1155 |
return PyList_New(0); |
| 1156 |
|
| 1157 |
return PyDict_Keys(self->extra->attrib); |
| 1158 |
} |
| 1159 |
|
| 1160 |
static Py_ssize_t |
| 1161 |
element_length(ElementObject* self) |
| 1162 |
{ |
| 1163 |
if (!self->extra) |
| 1164 |
return 0; |
| 1165 |
|
| 1166 |
return self->extra->length; |
| 1167 |
} |
| 1168 |
|
| 1169 |
static PyObject* |
| 1170 |
element_makeelement(PyObject* self, PyObject* args, PyObject* kw) |
| 1171 |
{ |
| 1172 |
PyObject* elem; |
| 1173 |
|
| 1174 |
PyObject* tag; |
| 1175 |
PyObject* attrib; |
| 1176 |
if (!PyArg_ParseTuple(args, "OO:makeelement", &tag, &attrib)) |
| 1177 |
return NULL; |
| 1178 |
|
| 1179 |
attrib = PyDict_Copy(attrib); |
| 1180 |
if (!attrib) |
| 1181 |
return NULL; |
| 1182 |
|
| 1183 |
elem = element_new(tag, attrib); |
| 1184 |
|
| 1185 |
Py_DECREF(attrib); |
| 1186 |
|
| 1187 |
return elem; |
| 1188 |
} |
| 1189 |
|
| 1190 |
static PyObject* |
| 1191 |
element_reduce(ElementObject* self, PyObject* args) |
| 1192 |
{ |
| 1193 |
if (!PyArg_ParseTuple(args, ":__reduce__")) |
| 1194 |
return NULL; |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
if (!elementtree_copyelement_obj) { |
| 1203 |
PyErr_SetString( |
| 1204 |
PyExc_RuntimeError, |
| 1205 |
"copyelement helper not found" |
| 1206 |
); |
| 1207 |
return NULL; |
| 1208 |
} |
| 1209 |
|
| 1210 |
return Py_BuildValue( |
| 1211 |
"O(N)", elementtree_copyelement_obj, element_copy(self, args) |
| 1212 |
); |
| 1213 |
} |
| 1214 |
|
| 1215 |
static PyObject* |
| 1216 |
element_remove(ElementObject* self, PyObject* args) |
| 1217 |
{ |
| 1218 |
int i; |
| 1219 |
int rc; |
| 1220 |
PyObject* element; |
| 1221 |
PyObject* found; |
| 1222 |
|
| 1223 |
if (!PyArg_ParseTuple(args, "O!:remove", &Element_Type, &element)) |
| 1224 |
return NULL; |
| 1225 |
|
| 1226 |
if (!self->extra) { |
| 1227 |
|
| 1228 |
PyErr_SetString( |
| 1229 |
PyExc_ValueError, |
| 1230 |
"list.remove(x): x not in list" |
| 1231 |
); |
| 1232 |
return NULL; |
| 1233 |
} |
| 1234 |
|
| 1235 |
for (i = 0; i < self->extra->length; i++) { |
| 1236 |
if (self->extra->children[i] == element) |
| 1237 |
break; |
| 1238 |
rc = PyObject_Compare(self->extra->children[i], element); |
| 1239 |
if (rc == 0) |
| 1240 |
break; |
| 1241 |
if (rc < 0 && PyErr_Occurred()) |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
|
| 1245 |
if (i >= self->extra->length) { |
| 1246 |
|
| 1247 |
PyErr_SetString( |
| 1248 |
PyExc_ValueError, |
| 1249 |
"list.remove(x): x not in list" |
| 1250 |
); |
| 1251 |
return NULL; |
| 1252 |
} |
| 1253 |
|
| 1254 |
found = self->extra->children[i]; |
| 1255 |
|
| 1256 |
self->extra->length--; |
| 1257 |
for (; i < self->extra->length; i++) |
| 1258 |
self->extra->children[i] = self->extra->children[i+1]; |
| 1259 |
|
| 1260 |
Py_DECREF(found); |
| 1261 |
Py_RETURN_NONE; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static PyObject* |
| 1265 |
element_repr(ElementObject* self) |
| 1266 |
{ |
| 1267 |
int status; |
| 1268 |
|
| 1269 |
if (self->tag == NULL) |
| 1270 |
return PyUnicode_FromFormat("<Element at %p>", self); |
| 1271 |
|
| 1272 |
status = Py_ReprEnter((PyObject *)self); |
| 1273 |
if (status == 0) { |
| 1274 |
PyObject *repr, *tag; |
| 1275 |
tag = PyObject_Repr(self->tag); |
| 1276 |
if (!tag) { |
| 1277 |
Py_ReprLeave((PyObject *)self); |
| 1278 |
return NULL; |
| 1279 |
} |
| 1280 |
|
| 1281 |
repr = PyString_FromFormat("<Element %s at %p>", |
| 1282 |
PyString_AS_STRING(tag), self); |
| 1283 |
Py_ReprLeave((PyObject *)self); |
| 1284 |
Py_DECREF(tag); |
| 1285 |
return repr; |
| 1286 |
} |
| 1287 |
if (status > 0) |
| 1288 |
PyErr_Format(PyExc_RuntimeError, |
| 1289 |
"reentrant call inside %s.__repr__", |
| 1290 |
Py_TYPE(self)->tp_name); |
| 1291 |
return NULL; |
| 1292 |
} |
| 1293 |
|
| 1294 |
static PyObject* |
| 1295 |
element_set(ElementObject* self, PyObject* args) |
| 1296 |
{ |
| 1297 |
PyObject* attrib; |
| 1298 |
|
| 1299 |
PyObject* key; |
| 1300 |
PyObject* value; |
| 1301 |
if (!PyArg_ParseTuple(args, "OO:set", &key, &value)) |
| 1302 |
return NULL; |
| 1303 |
|
| 1304 |
if (!self->extra) |
| 1305 |
element_new_extra(self, NULL); |
| 1306 |
|
| 1307 |
attrib = element_get_attrib(self); |
| 1308 |
if (!attrib) |
| 1309 |
return NULL; |
| 1310 |
|
| 1311 |
if (PyDict_SetItem(attrib, key, value) < 0) |
| 1312 |
return NULL; |
| 1313 |
|
| 1314 |
Py_RETURN_NONE; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int |
| 1318 |
element_setitem(PyObject* self_, Py_ssize_t index_, PyObject* item) |
| 1319 |
{ |
| 1320 |
ElementObject* self = (ElementObject*) self_; |
| 1321 |
int i, index; |
| 1322 |
PyObject* old; |
| 1323 |
|
| 1324 |
if (!self->extra || index_ < 0 || index_ >= self->extra->length) { |
| 1325 |
PyErr_SetString( |
| 1326 |
PyExc_IndexError, |
| 1327 |
"child assignment index out of range"); |
| 1328 |
return -1; |
| 1329 |
} |
| 1330 |
index = (int)index_; |
| 1331 |
|
| 1332 |
old = self->extra->children[index]; |
| 1333 |
|
| 1334 |
if (item) { |
| 1335 |
Py_INCREF(item); |
| 1336 |
self->extra->children[index] = item; |
| 1337 |
} else { |
| 1338 |
self->extra->length--; |
| 1339 |
for (i = index; i < self->extra->length; i++) |
| 1340 |
self->extra->children[i] = self->extra->children[i+1]; |
| 1341 |
} |
| 1342 |
|
| 1343 |
Py_DECREF(old); |
| 1344 |
|
| 1345 |
return 0; |
| 1346 |
} |
| 1347 |
|
| 1348 |
static PyObject* |
| 1349 |
element_subscr(PyObject* self_, PyObject* item) |
| 1350 |
{ |
| 1351 |
ElementObject* self = (ElementObject*) self_; |
| 1352 |
|
| 1353 |
#if (PY_VERSION_HEX < 0x02050000) |
| 1354 |
if (_PyAnyInt_Check(item)) { |
| 1355 |
long i = PyInt_AsLong(item); |
| 1356 |
#else |
| 1357 |
if (PyIndex_Check(item)) { |
| 1358 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1359 |
#endif |
| 1360 |
|
| 1361 |
if (i == -1 && PyErr_Occurred()) { |
| 1362 |
return NULL; |
| 1363 |
} |
| 1364 |
if (i < 0 && self->extra) |
| 1365 |
i += self->extra->length; |
| 1366 |
return element_getitem(self_, i); |
| 1367 |
} |
| 1368 |
else if (PySlice_Check(item)) { |
| 1369 |
Py_ssize_t start, stop, step, slicelen, cur, i; |
| 1370 |
PyObject* list; |
| 1371 |
|
| 1372 |
if (!self->extra) |
| 1373 |
return PyList_New(0); |
| 1374 |
|
| 1375 |
if (_PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 1376 |
return NULL; |
| 1377 |
} |
| 1378 |
slicelen = _PySlice_AdjustIndices(self->extra->length, &start, &stop, |
| 1379 |
step); |
| 1380 |
|
| 1381 |
if (slicelen <= 0) |
| 1382 |
return PyList_New(0); |
| 1383 |
else { |
| 1384 |
list = PyList_New(slicelen); |
| 1385 |
if (!list) |
| 1386 |
return NULL; |
| 1387 |
|
| 1388 |
for (cur = start, i = 0; i < slicelen; |
| 1389 |
cur += step, i++) { |
| 1390 |
PyObject* item = self->extra->children[cur]; |
| 1391 |
Py_INCREF(item); |
| 1392 |
PyList_SET_ITEM(list, i, item); |
| 1393 |
} |
| 1394 |
|
| 1395 |
return list; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
else { |
| 1399 |
PyErr_SetString(PyExc_TypeError, |
| 1400 |
"element indices must be integers"); |
| 1401 |
return NULL; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
static int |
| 1406 |
element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) |
| 1407 |
{ |
| 1408 |
ElementObject* self = (ElementObject*) self_; |
| 1409 |
|
| 1410 |
#if (PY_VERSION_HEX < 0x02050000) |
| 1411 |
if (_PyAnyInt_Check(item)) { |
| 1412 |
long i = PyInt_AsLong(item); |
| 1413 |
#else |
| 1414 |
if (PyIndex_Check(item)) { |
| 1415 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1416 |
#endif |
| 1417 |
|
| 1418 |
if (i == -1 && PyErr_Occurred()) { |
| 1419 |
return -1; |
| 1420 |
} |
| 1421 |
if (i < 0 && self->extra) |
| 1422 |
i += self->extra->length; |
| 1423 |
return element_setitem(self_, i, value); |
| 1424 |
} |
| 1425 |
else if (PySlice_Check(item)) { |
| 1426 |
Py_ssize_t start, stop, step, slicelen, newlen, cur, i; |
| 1427 |
|
| 1428 |
PyObject* recycle = NULL; |
| 1429 |
PyObject* seq = NULL; |
| 1430 |
|
| 1431 |
if (!self->extra) |
| 1432 |
element_new_extra(self, NULL); |
| 1433 |
|
| 1434 |
if (_PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 1435 |
return -1; |
| 1436 |
} |
| 1437 |
slicelen = _PySlice_AdjustIndices(self->extra->length, &start, &stop, |
| 1438 |
step); |
| 1439 |
assert(slicelen <= self->extra->length); |
| 1440 |
|
| 1441 |
if (value == NULL) |
| 1442 |
newlen = 0; |
| 1443 |
else { |
| 1444 |
seq = PySequence_Fast(value, ""); |
| 1445 |
if (!seq) { |
| 1446 |
PyErr_Format( |
| 1447 |
PyExc_TypeError, |
| 1448 |
"expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name |
| 1449 |
); |
| 1450 |
return -1; |
| 1451 |
} |
| 1452 |
newlen = PySequence_Size(seq); |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (step != 1 && newlen != slicelen) |
| 1456 |
{ |
| 1457 |
Py_XDECREF(seq); |
| 1458 |
PyErr_Format(PyExc_ValueError, |
| 1459 |
#if (PY_VERSION_HEX < 0x02050000) |
| 1460 |
"attempt to assign sequence of size %d " |
| 1461 |
"to extended slice of size %d", |
| 1462 |
(int)newlen, (int)slicelen |
| 1463 |
#else |
| 1464 |
"attempt to assign sequence of size %zd " |
| 1465 |
"to extended slice of size %zd", |
| 1466 |
newlen, slicelen |
| 1467 |
#endif |
| 1468 |
); |
| 1469 |
return -1; |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
if (newlen > slicelen) { |
| 1475 |
if (element_resize(self, newlen - slicelen) < 0) { |
| 1476 |
Py_XDECREF(seq); |
| 1477 |
return -1; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
assert(newlen - slicelen <= INT_MAX - self->extra->length); |
| 1481 |
assert(newlen - slicelen >= -self->extra->length); |
| 1482 |
|
| 1483 |
if (slicelen > 0) { |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
recycle = PyList_New(slicelen); |
| 1488 |
if (!recycle) { |
| 1489 |
Py_XDECREF(seq); |
| 1490 |
return -1; |
| 1491 |
} |
| 1492 |
for (cur = start, i = 0; i < slicelen; |
| 1493 |
cur += step, i++) |
| 1494 |
PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1495 |
} |
| 1496 |
|
| 1497 |
if (newlen < slicelen) { |
| 1498 |
|
| 1499 |
for (i = stop; i < self->extra->length; i++) |
| 1500 |
self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1501 |
} else if (newlen > slicelen) { |
| 1502 |
|
| 1503 |
for (i = self->extra->length-1; i >= stop; i--) |
| 1504 |
self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
for (cur = start, i = 0; i < newlen; |
| 1509 |
cur += step, i++) { |
| 1510 |
PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 1511 |
Py_INCREF(element); |
| 1512 |
self->extra->children[cur] = element; |
| 1513 |
} |
| 1514 |
|
| 1515 |
self->extra->length += (int)(newlen - slicelen); |
| 1516 |
|
| 1517 |
Py_XDECREF(seq); |
| 1518 |
|
| 1519 |
|
| 1520 |
Py_XDECREF(recycle); |
| 1521 |
|
| 1522 |
return 0; |
| 1523 |
} |
| 1524 |
else { |
| 1525 |
PyErr_SetString(PyExc_TypeError, |
| 1526 |
"element indices must be integers"); |
| 1527 |
return -1; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
static PyMethodDef element_methods[] = { |
| 1532 |
|
| 1533 |
{"clear", (PyCFunction) element_clear, METH_VARARGS}, |
| 1534 |
|
| 1535 |
{"get", (PyCFunction) element_get, METH_VARARGS}, |
| 1536 |
{"set", (PyCFunction) element_set, METH_VARARGS}, |
| 1537 |
|
| 1538 |
{"find", (PyCFunction) element_find, METH_VARARGS}, |
| 1539 |
{"findtext", (PyCFunction) element_findtext, METH_VARARGS}, |
| 1540 |
{"findall", (PyCFunction) element_findall, METH_VARARGS}, |
| 1541 |
|
| 1542 |
{"append", (PyCFunction) element_append, METH_VARARGS}, |
| 1543 |
{"extend", (PyCFunction) element_extend, METH_VARARGS}, |
| 1544 |
{"insert", (PyCFunction) element_insert, METH_VARARGS}, |
| 1545 |
{"remove", (PyCFunction) element_remove, METH_VARARGS}, |
| 1546 |
|
| 1547 |
{"iter", (PyCFunction) element_iter, METH_VARARGS}, |
| 1548 |
{"itertext", (PyCFunction) element_itertext, METH_VARARGS}, |
| 1549 |
{"iterfind", (PyCFunction) element_iterfind, METH_VARARGS}, |
| 1550 |
|
| 1551 |
{"getiterator", (PyCFunction) element_getiterator, METH_VARARGS}, |
| 1552 |
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS}, |
| 1553 |
|
| 1554 |
{"items", (PyCFunction) element_items, METH_VARARGS}, |
| 1555 |
{"keys", (PyCFunction) element_keys, METH_VARARGS}, |
| 1556 |
|
| 1557 |
{"makeelement", (PyCFunction) element_makeelement, METH_VARARGS}, |
| 1558 |
|
| 1559 |
{"__copy__", (PyCFunction) element_copy, METH_VARARGS}, |
| 1560 |
{"__deepcopy__", (PyCFunction) element_deepcopy, METH_VARARGS}, |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
{"!__reduce__", (PyCFunction) element_reduce, METH_VARARGS}, |
| 1573 |
|
| 1574 |
{NULL, NULL} |
| 1575 |
}; |
| 1576 |
|
| 1577 |
static PyObject* |
| 1578 |
element_getattr(ElementObject* self, char* name) |
| 1579 |
{ |
| 1580 |
PyObject* res; |
| 1581 |
|
| 1582 |
|
| 1583 |
if (strcmp(name, "tag") == 0) { |
| 1584 |
res = self->tag; |
| 1585 |
Py_INCREF(res); |
| 1586 |
return res; |
| 1587 |
} else if (strcmp(name, "text") == 0) { |
| 1588 |
res = element_get_text(self); |
| 1589 |
Py_XINCREF(res); |
| 1590 |
return res; |
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
res = Py_FindMethod(element_methods, (PyObject*) self, name); |
| 1595 |
if (res) |
| 1596 |
return res; |
| 1597 |
|
| 1598 |
PyErr_Clear(); |
| 1599 |
|
| 1600 |
|
| 1601 |
if (strcmp(name, "tail") == 0) { |
| 1602 |
res = element_get_tail(self); |
| 1603 |
} else if (strcmp(name, "attrib") == 0) { |
| 1604 |
if (!self->extra) |
| 1605 |
element_new_extra(self, NULL); |
| 1606 |
res = element_get_attrib(self); |
| 1607 |
} else { |
| 1608 |
PyErr_SetString(PyExc_AttributeError, name); |
| 1609 |
return NULL; |
| 1610 |
} |
| 1611 |
|
| 1612 |
if (!res) |
| 1613 |
return NULL; |
| 1614 |
|
| 1615 |
Py_INCREF(res); |
| 1616 |
return res; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static int |
| 1620 |
element_setattr(ElementObject* self, const char* name, PyObject* value) |
| 1621 |
{ |
| 1622 |
if (value == NULL) { |
| 1623 |
PyErr_SetString( |
| 1624 |
PyExc_AttributeError, |
| 1625 |
"can't delete element attributes" |
| 1626 |
); |
| 1627 |
return -1; |
| 1628 |
} |
| 1629 |
|
| 1630 |
if (strcmp(name, "tag") == 0) { |
| 1631 |
Py_INCREF(value); |
| 1632 |
Py_SETREF(self->tag, value); |
| 1633 |
} else if (strcmp(name, "text") == 0) { |
| 1634 |
Py_INCREF(value); |
| 1635 |
_set_joined_ptr(&self->text, value); |
| 1636 |
} else if (strcmp(name, "tail") == 0) { |
| 1637 |
Py_INCREF(value); |
| 1638 |
_set_joined_ptr(&self->tail, value); |
| 1639 |
} else if (strcmp(name, "attrib") == 0) { |
| 1640 |
if (!self->extra) |
| 1641 |
element_new_extra(self, NULL); |
| 1642 |
Py_INCREF(value); |
| 1643 |
Py_SETREF(self->extra->attrib, value); |
| 1644 |
} else { |
| 1645 |
PyErr_SetString(PyExc_AttributeError, name); |
| 1646 |
return -1; |
| 1647 |
} |
| 1648 |
|
| 1649 |
return 0; |
| 1650 |
} |
| 1651 |
|
| 1652 |
static PySequenceMethods element_as_sequence = { |
| 1653 |
(lenfunc) element_length, |
| 1654 |
0, |
| 1655 |
0, |
| 1656 |
element_getitem, |
| 1657 |
0, |
| 1658 |
element_setitem, |
| 1659 |
0, |
| 1660 |
}; |
| 1661 |
|
| 1662 |
static PyMappingMethods element_as_mapping = { |
| 1663 |
(lenfunc) element_length, |
| 1664 |
(binaryfunc) element_subscr, |
| 1665 |
(objobjargproc) element_ass_subscr, |
| 1666 |
}; |
| 1667 |
|
| 1668 |
statichere PyTypeObject Element_Type = { |
| 1669 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1670 |
"Element", sizeof(ElementObject), 0, |
| 1671 |
|
| 1672 |
(destructor)element_dealloc, |
| 1673 |
0, |
| 1674 |
(getattrfunc)element_getattr, |
| 1675 |
(setattrfunc)element_setattr, |
| 1676 |
0, |
| 1677 |
(reprfunc)element_repr, |
| 1678 |
0, |
| 1679 |
&element_as_sequence, |
| 1680 |
&element_as_mapping, |
| 1681 |
}; |
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
typedef struct { |
| 1687 |
PyObject_HEAD |
| 1688 |
|
| 1689 |
PyObject* root; |
| 1690 |
|
| 1691 |
ElementObject* this; |
| 1692 |
ElementObject* last; |
| 1693 |
|
| 1694 |
PyObject* data; |
| 1695 |
|
| 1696 |
PyObject* stack; |
| 1697 |
Py_ssize_t index; |
| 1698 |
|
| 1699 |
|
| 1700 |
PyObject* events; |
| 1701 |
PyObject* start_event_obj; |
| 1702 |
PyObject* end_event_obj; |
| 1703 |
PyObject* start_ns_event_obj; |
| 1704 |
PyObject* end_ns_event_obj; |
| 1705 |
|
| 1706 |
} TreeBuilderObject; |
| 1707 |
|
| 1708 |
staticforward PyTypeObject TreeBuilder_Type; |
| 1709 |
|
| 1710 |
#define TreeBuilder_CheckExact(op) (Py_TYPE(op) == &TreeBuilder_Type) |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
LOCAL(PyObject*) |
| 1716 |
treebuilder_new(void) |
| 1717 |
{ |
| 1718 |
TreeBuilderObject* self; |
| 1719 |
|
| 1720 |
self = PyObject_New(TreeBuilderObject, &TreeBuilder_Type); |
| 1721 |
if (self == NULL) |
| 1722 |
return NULL; |
| 1723 |
|
| 1724 |
self->root = NULL; |
| 1725 |
|
| 1726 |
Py_INCREF(Py_None); |
| 1727 |
self->this = (ElementObject*) Py_None; |
| 1728 |
|
| 1729 |
Py_INCREF(Py_None); |
| 1730 |
self->last = (ElementObject*) Py_None; |
| 1731 |
|
| 1732 |
self->data = NULL; |
| 1733 |
|
| 1734 |
self->index = 0; |
| 1735 |
|
| 1736 |
self->events = NULL; |
| 1737 |
self->start_event_obj = self->end_event_obj = NULL; |
| 1738 |
self->start_ns_event_obj = self->end_ns_event_obj = NULL; |
| 1739 |
self->stack = PyList_New(20); |
| 1740 |
if (self->stack == NULL) { |
| 1741 |
Py_DECREF(self); |
| 1742 |
return NULL; |
| 1743 |
} |
| 1744 |
|
| 1745 |
ALLOC(sizeof(TreeBuilderObject), "create treebuilder"); |
| 1746 |
|
| 1747 |
return (PyObject*) self; |
| 1748 |
} |
| 1749 |
|
| 1750 |
static PyObject* |
| 1751 |
treebuilder(PyObject* self_, PyObject* args) |
| 1752 |
{ |
| 1753 |
if (!PyArg_ParseTuple(args, ":TreeBuilder")) |
| 1754 |
return NULL; |
| 1755 |
|
| 1756 |
return treebuilder_new(); |
| 1757 |
} |
| 1758 |
|
| 1759 |
static void |
| 1760 |
treebuilder_dealloc(TreeBuilderObject* self) |
| 1761 |
{ |
| 1762 |
Py_XDECREF(self->end_ns_event_obj); |
| 1763 |
Py_XDECREF(self->start_ns_event_obj); |
| 1764 |
Py_XDECREF(self->end_event_obj); |
| 1765 |
Py_XDECREF(self->start_event_obj); |
| 1766 |
Py_XDECREF(self->events); |
| 1767 |
Py_XDECREF(self->stack); |
| 1768 |
Py_XDECREF(self->data); |
| 1769 |
Py_DECREF(self->last); |
| 1770 |
Py_DECREF(self->this); |
| 1771 |
Py_XDECREF(self->root); |
| 1772 |
|
| 1773 |
RELEASE(sizeof(TreeBuilderObject), "destroy treebuilder"); |
| 1774 |
|
| 1775 |
PyObject_Del(self); |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
static void |
| 1782 |
treebuilder_set_element_text_or_tail(PyObject **data, PyObject **dest) |
| 1783 |
{ |
| 1784 |
PyObject *tmp = JOIN_OBJ(*dest); |
| 1785 |
*dest = JOIN_SET(*data, PyList_CheckExact(*data)); |
| 1786 |
*data = NULL; |
| 1787 |
Py_DECREF(tmp); |
| 1788 |
} |
| 1789 |
|
| 1790 |
LOCAL(void) |
| 1791 |
treebuilder_flush_data(TreeBuilderObject* self) |
| 1792 |
{ |
| 1793 |
ElementObject *element = self->last; |
| 1794 |
|
| 1795 |
if (self->data) { |
| 1796 |
if (self->this == element) { |
| 1797 |
treebuilder_set_element_text_or_tail( |
| 1798 |
&self->data, |
| 1799 |
&element->text); |
| 1800 |
} |
| 1801 |
else { |
| 1802 |
treebuilder_set_element_text_or_tail( |
| 1803 |
&self->data, |
| 1804 |
&element->tail); |
| 1805 |
} |
| 1806 |
} |
| 1807 |
} |
| 1808 |
|
| 1809 |
LOCAL(int) |
| 1810 |
treebuilder_append_event(TreeBuilderObject *self, PyObject *action, |
| 1811 |
PyObject *node) |
| 1812 |
{ |
| 1813 |
if (action != NULL) { |
| 1814 |
PyObject *res = PyTuple_Pack(2, action, node); |
| 1815 |
if (res == NULL) |
| 1816 |
return -1; |
| 1817 |
if (PyList_Append(self->events, res) < 0) { |
| 1818 |
Py_DECREF(res); |
| 1819 |
return -1; |
| 1820 |
} |
| 1821 |
Py_DECREF(res); |
| 1822 |
} |
| 1823 |
return 0; |
| 1824 |
} |
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
LOCAL(PyObject*) |
| 1830 |
treebuilder_handle_xml(TreeBuilderObject* self, PyObject* encoding, |
| 1831 |
PyObject* standalone) |
| 1832 |
{ |
| 1833 |
Py_RETURN_NONE; |
| 1834 |
} |
| 1835 |
|
| 1836 |
LOCAL(PyObject*) |
| 1837 |
treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, |
| 1838 |
PyObject* attrib) |
| 1839 |
{ |
| 1840 |
PyObject* node; |
| 1841 |
PyObject* this; |
| 1842 |
|
| 1843 |
treebuilder_flush_data(self); |
| 1844 |
|
| 1845 |
node = element_new(tag, attrib); |
| 1846 |
if (!node) |
| 1847 |
return NULL; |
| 1848 |
|
| 1849 |
this = (PyObject*) self->this; |
| 1850 |
|
| 1851 |
if (this != Py_None) { |
| 1852 |
if (element_add_subelement((ElementObject*) this, node) < 0) |
| 1853 |
goto error; |
| 1854 |
} else { |
| 1855 |
if (self->root) { |
| 1856 |
PyErr_SetString( |
| 1857 |
elementtree_parseerror_obj, |
| 1858 |
"multiple elements on top level" |
| 1859 |
); |
| 1860 |
goto error; |
| 1861 |
} |
| 1862 |
Py_INCREF(node); |
| 1863 |
self->root = node; |
| 1864 |
} |
| 1865 |
|
| 1866 |
if (self->index < PyList_GET_SIZE(self->stack)) { |
| 1867 |
if (PyList_SetItem(self->stack, self->index, this) < 0) |
| 1868 |
goto error; |
| 1869 |
Py_INCREF(this); |
| 1870 |
} else { |
| 1871 |
if (PyList_Append(self->stack, this) < 0) |
| 1872 |
goto error; |
| 1873 |
} |
| 1874 |
self->index++; |
| 1875 |
|
| 1876 |
Py_INCREF(node); |
| 1877 |
Py_SETREF(self->this, (ElementObject*) node); |
| 1878 |
|
| 1879 |
Py_INCREF(node); |
| 1880 |
Py_SETREF(self->last, (ElementObject*) node); |
| 1881 |
|
| 1882 |
if (treebuilder_append_event(self, self->start_event_obj, node) < 0) |
| 1883 |
goto error; |
| 1884 |
|
| 1885 |
return node; |
| 1886 |
|
| 1887 |
error: |
| 1888 |
Py_DECREF(node); |
| 1889 |
return NULL; |
| 1890 |
} |
| 1891 |
|
| 1892 |
LOCAL(PyObject*) |
| 1893 |
treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) |
| 1894 |
{ |
| 1895 |
if (!self->data) { |
| 1896 |
if (self->last == (ElementObject*) Py_None) { |
| 1897 |
|
| 1898 |
Py_RETURN_NONE; |
| 1899 |
} |
| 1900 |
|
| 1901 |
Py_INCREF(data); self->data = data; |
| 1902 |
} else { |
| 1903 |
|
| 1904 |
if (PyString_CheckExact(self->data) && Py_REFCNT(self->data) == 1 && |
| 1905 |
PyString_CheckExact(data) && PyString_GET_SIZE(data) == 1) { |
| 1906 |
|
| 1907 |
|
| 1908 |
Py_ssize_t size = PyString_GET_SIZE(self->data); |
| 1909 |
if (_PyString_Resize(&self->data, size + 1) < 0) |
| 1910 |
return NULL; |
| 1911 |
PyString_AS_STRING(self->data)[size] = PyString_AS_STRING(data)[0]; |
| 1912 |
} else if (PyList_CheckExact(self->data)) { |
| 1913 |
if (PyList_Append(self->data, data) < 0) |
| 1914 |
return NULL; |
| 1915 |
} else { |
| 1916 |
PyObject* list = PyList_New(2); |
| 1917 |
if (!list) |
| 1918 |
return NULL; |
| 1919 |
PyList_SET_ITEM(list, 0, self->data); |
| 1920 |
Py_INCREF(data); PyList_SET_ITEM(list, 1, data); |
| 1921 |
self->data = list; |
| 1922 |
} |
| 1923 |
} |
| 1924 |
|
| 1925 |
Py_RETURN_NONE; |
| 1926 |
} |
| 1927 |
|
| 1928 |
LOCAL(PyObject*) |
| 1929 |
treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag) |
| 1930 |
{ |
| 1931 |
ElementObject *item; |
| 1932 |
|
| 1933 |
treebuilder_flush_data(self); |
| 1934 |
|
| 1935 |
if (self->index == 0) { |
| 1936 |
PyErr_SetString( |
| 1937 |
PyExc_IndexError, |
| 1938 |
"pop from empty stack" |
| 1939 |
); |
| 1940 |
return NULL; |
| 1941 |
} |
| 1942 |
|
| 1943 |
item = self->last; |
| 1944 |
self->last = self->this; |
| 1945 |
self->index--; |
| 1946 |
self->this = (ElementObject *) PyList_GET_ITEM(self->stack, self->index); |
| 1947 |
Py_INCREF(self->this); |
| 1948 |
Py_DECREF(item); |
| 1949 |
|
| 1950 |
if (treebuilder_append_event(self, self->end_event_obj, (PyObject*)self->last) < 0) |
| 1951 |
return NULL; |
| 1952 |
|
| 1953 |
Py_INCREF(self->last); |
| 1954 |
return (PyObject*) self->last; |
| 1955 |
} |
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
static PyObject* |
| 1961 |
treebuilder_data(TreeBuilderObject* self, PyObject* args) |
| 1962 |
{ |
| 1963 |
PyObject* data; |
| 1964 |
if (!PyArg_ParseTuple(args, "O:data", &data)) |
| 1965 |
return NULL; |
| 1966 |
|
| 1967 |
return treebuilder_handle_data(self, data); |
| 1968 |
} |
| 1969 |
|
| 1970 |
static PyObject* |
| 1971 |
treebuilder_end(TreeBuilderObject* self, PyObject* args) |
| 1972 |
{ |
| 1973 |
PyObject* tag; |
| 1974 |
if (!PyArg_ParseTuple(args, "O:end", &tag)) |
| 1975 |
return NULL; |
| 1976 |
|
| 1977 |
return treebuilder_handle_end(self, tag); |
| 1978 |
} |
| 1979 |
|
| 1980 |
LOCAL(PyObject*) |
| 1981 |
treebuilder_done(TreeBuilderObject* self) |
| 1982 |
{ |
| 1983 |
PyObject* res; |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
if (self->root) |
| 1988 |
res = self->root; |
| 1989 |
else |
| 1990 |
res = Py_None; |
| 1991 |
|
| 1992 |
Py_INCREF(res); |
| 1993 |
return res; |
| 1994 |
} |
| 1995 |
|
| 1996 |
static PyObject* |
| 1997 |
treebuilder_close(TreeBuilderObject* self, PyObject* args) |
| 1998 |
{ |
| 1999 |
if (!PyArg_ParseTuple(args, ":close")) |
| 2000 |
return NULL; |
| 2001 |
|
| 2002 |
return treebuilder_done(self); |
| 2003 |
} |
| 2004 |
|
| 2005 |
static PyObject* |
| 2006 |
treebuilder_start(TreeBuilderObject* self, PyObject* args) |
| 2007 |
{ |
| 2008 |
PyObject* tag; |
| 2009 |
PyObject* attrib = Py_None; |
| 2010 |
if (!PyArg_ParseTuple(args, "O|O:start", &tag, &attrib)) |
| 2011 |
return NULL; |
| 2012 |
|
| 2013 |
return treebuilder_handle_start(self, tag, attrib); |
| 2014 |
} |
| 2015 |
|
| 2016 |
static PyObject* |
| 2017 |
treebuilder_xml(TreeBuilderObject* self, PyObject* args) |
| 2018 |
{ |
| 2019 |
PyObject* encoding; |
| 2020 |
PyObject* standalone; |
| 2021 |
if (!PyArg_ParseTuple(args, "OO:xml", &encoding, &standalone)) |
| 2022 |
return NULL; |
| 2023 |
|
| 2024 |
return treebuilder_handle_xml(self, encoding, standalone); |
| 2025 |
} |
| 2026 |
|
| 2027 |
static PyMethodDef treebuilder_methods[] = { |
| 2028 |
{"data", (PyCFunction) treebuilder_data, METH_VARARGS}, |
| 2029 |
{"start", (PyCFunction) treebuilder_start, METH_VARARGS}, |
| 2030 |
{"end", (PyCFunction) treebuilder_end, METH_VARARGS}, |
| 2031 |
{"xml", (PyCFunction) treebuilder_xml, METH_VARARGS}, |
| 2032 |
{"close", (PyCFunction) treebuilder_close, METH_VARARGS}, |
| 2033 |
{NULL, NULL} |
| 2034 |
}; |
| 2035 |
|
| 2036 |
static PyObject* |
| 2037 |
treebuilder_getattr(TreeBuilderObject* self, char* name) |
| 2038 |
{ |
| 2039 |
return Py_FindMethod(treebuilder_methods, (PyObject*) self, name); |
| 2040 |
} |
| 2041 |
|
| 2042 |
statichere PyTypeObject TreeBuilder_Type = { |
| 2043 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2044 |
"TreeBuilder", sizeof(TreeBuilderObject), 0, |
| 2045 |
|
| 2046 |
(destructor)treebuilder_dealloc, |
| 2047 |
0, |
| 2048 |
(getattrfunc)treebuilder_getattr, |
| 2049 |
}; |
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
#if defined(USE_EXPAT) |
| 2055 |
|
| 2056 |
#include "expat.h" |
| 2057 |
|
| 2058 |
#if defined(USE_PYEXPAT_CAPI) |
| 2059 |
#include "pyexpat.h" |
| 2060 |
static struct PyExpat_CAPI* expat_capi; |
| 2061 |
#define EXPAT(func) (expat_capi->func) |
| 2062 |
#else |
| 2063 |
#define EXPAT(func) (XML_##func) |
| 2064 |
#endif |
| 2065 |
|
| 2066 |
typedef struct { |
| 2067 |
PyObject_HEAD |
| 2068 |
|
| 2069 |
XML_Parser parser; |
| 2070 |
|
| 2071 |
PyObject* target; |
| 2072 |
PyObject* entity; |
| 2073 |
|
| 2074 |
PyObject* names; |
| 2075 |
|
| 2076 |
PyObject* handle_xml; |
| 2077 |
|
| 2078 |
PyObject* handle_start; |
| 2079 |
PyObject* handle_data; |
| 2080 |
PyObject* handle_end; |
| 2081 |
|
| 2082 |
PyObject* handle_comment; |
| 2083 |
PyObject* handle_pi; |
| 2084 |
|
| 2085 |
PyObject* handle_close; |
| 2086 |
|
| 2087 |
} XMLParserObject; |
| 2088 |
|
| 2089 |
staticforward PyTypeObject XMLParser_Type; |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
#if defined(Py_USING_UNICODE) |
| 2094 |
LOCAL(int) |
| 2095 |
checkstring(const char* string, int size) |
| 2096 |
{ |
| 2097 |
int i; |
| 2098 |
|
| 2099 |
|
| 2100 |
for (i = 0; i < size; i++) |
| 2101 |
if (string[i] & 0x80) |
| 2102 |
return 1; |
| 2103 |
|
| 2104 |
return 0; |
| 2105 |
} |
| 2106 |
#endif |
| 2107 |
|
| 2108 |
LOCAL(PyObject*) |
| 2109 |
makestring(const char* string, int size) |
| 2110 |
{ |
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
#if defined(Py_USING_UNICODE) |
| 2115 |
if (checkstring(string, size)) |
| 2116 |
return PyUnicode_DecodeUTF8(string, size, "strict"); |
| 2117 |
#endif |
| 2118 |
|
| 2119 |
return PyString_FromStringAndSize(string, size); |
| 2120 |
} |
| 2121 |
|
| 2122 |
LOCAL(PyObject*) |
| 2123 |
makeuniversal(XMLParserObject* self, const char* string) |
| 2124 |
{ |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
int size = strlen(string); |
| 2129 |
PyObject* key; |
| 2130 |
PyObject* value; |
| 2131 |
|
| 2132 |
|
| 2133 |
key = PyString_FromStringAndSize(string, size); |
| 2134 |
if (!key) |
| 2135 |
return NULL; |
| 2136 |
|
| 2137 |
value = PyDict_GetItem(self->names, key); |
| 2138 |
|
| 2139 |
if (value) { |
| 2140 |
Py_INCREF(value); |
| 2141 |
} else { |
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
PyObject* tag; |
| 2146 |
char* p; |
| 2147 |
int i; |
| 2148 |
|
| 2149 |
|
| 2150 |
for (i = 0; i < size; i++) |
| 2151 |
if (string[i] == '}') |
| 2152 |
break; |
| 2153 |
if (i != size) { |
| 2154 |
|
| 2155 |
tag = PyString_FromStringAndSize(NULL, size+1); |
| 2156 |
p = PyString_AS_STRING(tag); |
| 2157 |
p[0] = '{'; |
| 2158 |
memcpy(p+1, string, size); |
| 2159 |
size++; |
| 2160 |
} else { |
| 2161 |
|
| 2162 |
Py_INCREF(key); |
| 2163 |
tag = key; |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
#if defined(Py_USING_UNICODE) |
| 2168 |
|
| 2169 |
|
| 2170 |
p = PyString_AS_STRING(tag); |
| 2171 |
if (checkstring(p, size)) { |
| 2172 |
value = PyUnicode_DecodeUTF8(p, size, "strict"); |
| 2173 |
Py_DECREF(tag); |
| 2174 |
if (!value) { |
| 2175 |
Py_DECREF(key); |
| 2176 |
return NULL; |
| 2177 |
} |
| 2178 |
} else |
| 2179 |
#endif |
| 2180 |
value = tag; |
| 2181 |
|
| 2182 |
|
| 2183 |
if (PyDict_SetItem(self->names, key, value) < 0) { |
| 2184 |
Py_DECREF(key); |
| 2185 |
Py_DECREF(value); |
| 2186 |
return NULL; |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
Py_DECREF(key); |
| 2191 |
return value; |
| 2192 |
} |
| 2193 |
|
| 2194 |
static void |
| 2195 |
expat_set_error(const char* message, int line, int column) |
| 2196 |
{ |
| 2197 |
PyObject *error; |
| 2198 |
PyObject *position; |
| 2199 |
char buffer[256]; |
| 2200 |
|
| 2201 |
sprintf(buffer, "%s: line %d, column %d", message, line, column); |
| 2202 |
|
| 2203 |
error = PyObject_CallFunction(elementtree_parseerror_obj, "s", buffer); |
| 2204 |
if (!error) |
| 2205 |
return; |
| 2206 |
|
| 2207 |
|
| 2208 |
position = Py_BuildValue("(ii)", line, column); |
| 2209 |
if (!position) { |
| 2210 |
Py_DECREF(error); |
| 2211 |
return; |
| 2212 |
} |
| 2213 |
if (PyObject_SetAttrString(error, "position", position) == -1) { |
| 2214 |
Py_DECREF(error); |
| 2215 |
Py_DECREF(position); |
| 2216 |
return; |
| 2217 |
} |
| 2218 |
Py_DECREF(position); |
| 2219 |
|
| 2220 |
PyErr_SetObject(elementtree_parseerror_obj, error); |
| 2221 |
Py_DECREF(error); |
| 2222 |
} |
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
static void |
| 2228 |
expat_default_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2229 |
int data_len) |
| 2230 |
{ |
| 2231 |
PyObject* key; |
| 2232 |
PyObject* value; |
| 2233 |
PyObject* res; |
| 2234 |
|
| 2235 |
if (data_len < 2 || data_in[0] != '&') |
| 2236 |
return; |
| 2237 |
|
| 2238 |
key = makestring(data_in + 1, data_len - 2); |
| 2239 |
if (!key) |
| 2240 |
return; |
| 2241 |
|
| 2242 |
value = PyDict_GetItem(self->entity, key); |
| 2243 |
|
| 2244 |
if (value) { |
| 2245 |
if (TreeBuilder_CheckExact(self->target)) |
| 2246 |
res = treebuilder_handle_data( |
| 2247 |
(TreeBuilderObject*) self->target, value |
| 2248 |
); |
| 2249 |
else if (self->handle_data) |
| 2250 |
res = PyObject_CallFunction(self->handle_data, "O", value); |
| 2251 |
else |
| 2252 |
res = NULL; |
| 2253 |
Py_XDECREF(res); |
| 2254 |
} else if (!PyErr_Occurred()) { |
| 2255 |
|
| 2256 |
char message[128]; |
| 2257 |
sprintf(message, "undefined entity &%.100s;", PyString_AS_STRING(key)); |
| 2258 |
expat_set_error( |
| 2259 |
message, |
| 2260 |
EXPAT(GetErrorLineNumber)(self->parser), |
| 2261 |
EXPAT(GetErrorColumnNumber)(self->parser) |
| 2262 |
); |
| 2263 |
} |
| 2264 |
|
| 2265 |
Py_DECREF(key); |
| 2266 |
} |
| 2267 |
|
| 2268 |
static void |
| 2269 |
expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, |
| 2270 |
const XML_Char **attrib_in) |
| 2271 |
{ |
| 2272 |
PyObject* res; |
| 2273 |
PyObject* tag; |
| 2274 |
PyObject* attrib; |
| 2275 |
int ok; |
| 2276 |
|
| 2277 |
|
| 2278 |
tag = makeuniversal(self, tag_in); |
| 2279 |
if (!tag) |
| 2280 |
return; |
| 2281 |
|
| 2282 |
|
| 2283 |
if (attrib_in[0]) { |
| 2284 |
attrib = PyDict_New(); |
| 2285 |
if (!attrib) { |
| 2286 |
Py_DECREF(tag); |
| 2287 |
return; |
| 2288 |
} |
| 2289 |
while (attrib_in[0] && attrib_in[1]) { |
| 2290 |
PyObject* key = makeuniversal(self, attrib_in[0]); |
| 2291 |
PyObject* value = makestring(attrib_in[1], strlen(attrib_in[1])); |
| 2292 |
if (!key || !value) { |
| 2293 |
Py_XDECREF(value); |
| 2294 |
Py_XDECREF(key); |
| 2295 |
Py_DECREF(attrib); |
| 2296 |
Py_DECREF(tag); |
| 2297 |
return; |
| 2298 |
} |
| 2299 |
ok = PyDict_SetItem(attrib, key, value); |
| 2300 |
Py_DECREF(value); |
| 2301 |
Py_DECREF(key); |
| 2302 |
if (ok < 0) { |
| 2303 |
Py_DECREF(attrib); |
| 2304 |
Py_DECREF(tag); |
| 2305 |
return; |
| 2306 |
} |
| 2307 |
attrib_in += 2; |
| 2308 |
} |
| 2309 |
} else { |
| 2310 |
Py_INCREF(Py_None); |
| 2311 |
attrib = Py_None; |
| 2312 |
} |
| 2313 |
|
| 2314 |
if (TreeBuilder_CheckExact(self->target)) |
| 2315 |
|
| 2316 |
res = treebuilder_handle_start((TreeBuilderObject*) self->target, |
| 2317 |
tag, attrib); |
| 2318 |
else if (self->handle_start) { |
| 2319 |
if (attrib == Py_None) { |
| 2320 |
Py_DECREF(attrib); |
| 2321 |
attrib = PyDict_New(); |
| 2322 |
if (!attrib) { |
| 2323 |
Py_DECREF(tag); |
| 2324 |
return; |
| 2325 |
} |
| 2326 |
} |
| 2327 |
res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib); |
| 2328 |
} else |
| 2329 |
res = NULL; |
| 2330 |
|
| 2331 |
Py_DECREF(tag); |
| 2332 |
Py_DECREF(attrib); |
| 2333 |
|
| 2334 |
Py_XDECREF(res); |
| 2335 |
} |
| 2336 |
|
| 2337 |
static void |
| 2338 |
expat_data_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2339 |
int data_len) |
| 2340 |
{ |
| 2341 |
PyObject* data; |
| 2342 |
PyObject* res; |
| 2343 |
|
| 2344 |
data = makestring(data_in, data_len); |
| 2345 |
if (!data) |
| 2346 |
return; |
| 2347 |
|
| 2348 |
if (TreeBuilder_CheckExact(self->target)) |
| 2349 |
|
| 2350 |
res = treebuilder_handle_data((TreeBuilderObject*) self->target, data); |
| 2351 |
else if (self->handle_data) |
| 2352 |
res = PyObject_CallFunction(self->handle_data, "O", data); |
| 2353 |
else |
| 2354 |
res = NULL; |
| 2355 |
|
| 2356 |
Py_DECREF(data); |
| 2357 |
|
| 2358 |
Py_XDECREF(res); |
| 2359 |
} |
| 2360 |
|
| 2361 |
static void |
| 2362 |
expat_end_handler(XMLParserObject* self, const XML_Char* tag_in) |
| 2363 |
{ |
| 2364 |
PyObject* tag; |
| 2365 |
PyObject* res = NULL; |
| 2366 |
|
| 2367 |
if (TreeBuilder_CheckExact(self->target)) |
| 2368 |
|
| 2369 |
|
| 2370 |
res = treebuilder_handle_end( |
| 2371 |
(TreeBuilderObject*) self->target, Py_None |
| 2372 |
); |
| 2373 |
else if (self->handle_end) { |
| 2374 |
tag = makeuniversal(self, tag_in); |
| 2375 |
if (tag) { |
| 2376 |
res = PyObject_CallFunction(self->handle_end, "O", tag); |
| 2377 |
Py_DECREF(tag); |
| 2378 |
} |
| 2379 |
} |
| 2380 |
|
| 2381 |
Py_XDECREF(res); |
| 2382 |
} |
| 2383 |
|
| 2384 |
static void |
| 2385 |
expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix, |
| 2386 |
const XML_Char *uri) |
| 2387 |
{ |
| 2388 |
TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
| 2389 |
PyObject *parcel; |
| 2390 |
PyObject *sprefix = NULL; |
| 2391 |
PyObject *suri = NULL; |
| 2392 |
|
| 2393 |
if (PyErr_Occurred()) |
| 2394 |
return; |
| 2395 |
|
| 2396 |
if (!target->events || !target->start_ns_event_obj) |
| 2397 |
return; |
| 2398 |
|
| 2399 |
if (uri) |
| 2400 |
suri = makestring(uri, strlen(uri)); |
| 2401 |
else |
| 2402 |
suri = PyString_FromStringAndSize("", 0); |
| 2403 |
if (!suri) |
| 2404 |
return; |
| 2405 |
|
| 2406 |
if (prefix) |
| 2407 |
sprefix = makestring(prefix, strlen(prefix)); |
| 2408 |
else |
| 2409 |
sprefix = PyString_FromStringAndSize("", 0); |
| 2410 |
if (!sprefix) { |
| 2411 |
Py_DECREF(suri); |
| 2412 |
return; |
| 2413 |
} |
| 2414 |
|
| 2415 |
parcel = PyTuple_Pack(2, sprefix, suri); |
| 2416 |
Py_DECREF(sprefix); |
| 2417 |
Py_DECREF(suri); |
| 2418 |
if (!parcel) |
| 2419 |
return; |
| 2420 |
treebuilder_append_event(target, target->start_ns_event_obj, parcel); |
| 2421 |
Py_DECREF(parcel); |
| 2422 |
} |
| 2423 |
|
| 2424 |
static void |
| 2425 |
expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in) |
| 2426 |
{ |
| 2427 |
TreeBuilderObject *target = (TreeBuilderObject*) self->target; |
| 2428 |
|
| 2429 |
if (PyErr_Occurred()) |
| 2430 |
return; |
| 2431 |
|
| 2432 |
if (!target->events) |
| 2433 |
return; |
| 2434 |
|
| 2435 |
treebuilder_append_event(target, target->end_ns_event_obj, Py_None); |
| 2436 |
} |
| 2437 |
|
| 2438 |
static void |
| 2439 |
expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in) |
| 2440 |
{ |
| 2441 |
PyObject* comment; |
| 2442 |
PyObject* res; |
| 2443 |
|
| 2444 |
if (self->handle_comment) { |
| 2445 |
comment = makestring(comment_in, strlen(comment_in)); |
| 2446 |
if (comment) { |
| 2447 |
res = PyObject_CallFunction(self->handle_comment, "O", comment); |
| 2448 |
Py_XDECREF(res); |
| 2449 |
Py_DECREF(comment); |
| 2450 |
} |
| 2451 |
} |
| 2452 |
} |
| 2453 |
|
| 2454 |
static void |
| 2455 |
expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, |
| 2456 |
const XML_Char* data_in) |
| 2457 |
{ |
| 2458 |
PyObject* target; |
| 2459 |
PyObject* data; |
| 2460 |
PyObject* res; |
| 2461 |
|
| 2462 |
if (self->handle_pi) { |
| 2463 |
target = makestring(target_in, strlen(target_in)); |
| 2464 |
data = makestring(data_in, strlen(data_in)); |
| 2465 |
if (target && data) { |
| 2466 |
res = PyObject_CallFunction(self->handle_pi, "OO", target, data); |
| 2467 |
Py_XDECREF(res); |
| 2468 |
Py_DECREF(data); |
| 2469 |
Py_DECREF(target); |
| 2470 |
} else { |
| 2471 |
Py_XDECREF(data); |
| 2472 |
Py_XDECREF(target); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
} |
| 2476 |
|
| 2477 |
#if defined(Py_USING_UNICODE) |
| 2478 |
static int |
| 2479 |
expat_unknown_encoding_handler(XMLParserObject *self, const XML_Char *name, |
| 2480 |
XML_Encoding *info) |
| 2481 |
{ |
| 2482 |
PyObject* u; |
| 2483 |
Py_UNICODE* p; |
| 2484 |
unsigned char s[256]; |
| 2485 |
int i; |
| 2486 |
|
| 2487 |
memset(info, 0, sizeof(XML_Encoding)); |
| 2488 |
|
| 2489 |
for (i = 0; i < 256; i++) |
| 2490 |
s[i] = i; |
| 2491 |
|
| 2492 |
u = PyUnicode_Decode((char*) s, 256, name, "replace"); |
| 2493 |
if (!u) |
| 2494 |
return XML_STATUS_ERROR; |
| 2495 |
|
| 2496 |
if (PyUnicode_GET_SIZE(u) != 256) { |
| 2497 |
Py_DECREF(u); |
| 2498 |
PyErr_SetString(PyExc_ValueError, |
| 2499 |
"multi-byte encodings are not supported"); |
| 2500 |
return XML_STATUS_ERROR; |
| 2501 |
} |
| 2502 |
|
| 2503 |
p = PyUnicode_AS_UNICODE(u); |
| 2504 |
|
| 2505 |
for (i = 0; i < 256; i++) { |
| 2506 |
if (p[i] != Py_UNICODE_REPLACEMENT_CHARACTER) |
| 2507 |
info->map[i] = p[i]; |
| 2508 |
else |
| 2509 |
info->map[i] = -1; |
| 2510 |
} |
| 2511 |
|
| 2512 |
Py_DECREF(u); |
| 2513 |
|
| 2514 |
return XML_STATUS_OK; |
| 2515 |
} |
| 2516 |
#endif |
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
static int |
| 2522 |
ignore_attribute_error(PyObject *value) |
| 2523 |
{ |
| 2524 |
if (value == NULL) { |
| 2525 |
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2526 |
return -1; |
| 2527 |
} |
| 2528 |
PyErr_Clear(); |
| 2529 |
} |
| 2530 |
return 0; |
| 2531 |
} |
| 2532 |
|
| 2533 |
static PyObject* |
| 2534 |
xmlparser(PyObject* self_, PyObject* args, PyObject* kw) |
| 2535 |
{ |
| 2536 |
XMLParserObject* self; |
| 2537 |
|
| 2538 |
static XML_Memory_Handling_Suite memory_handler; |
| 2539 |
|
| 2540 |
PyObject* target = NULL; |
| 2541 |
char* encoding = NULL; |
| 2542 |
static char* kwlist[] = { "target", "encoding", NULL }; |
| 2543 |
if (!PyArg_ParseTupleAndKeywords(args, kw, "|Oz:XMLParser", kwlist, |
| 2544 |
&target, &encoding)) |
| 2545 |
return NULL; |
| 2546 |
|
| 2547 |
#if defined(USE_PYEXPAT_CAPI) |
| 2548 |
if (!expat_capi) { |
| 2549 |
PyErr_SetString( |
| 2550 |
PyExc_RuntimeError, "cannot load dispatch table from pyexpat" |
| 2551 |
); |
| 2552 |
return NULL; |
| 2553 |
} |
| 2554 |
#endif |
| 2555 |
|
| 2556 |
self = PyObject_New(XMLParserObject, &XMLParser_Type); |
| 2557 |
if (self == NULL) |
| 2558 |
return NULL; |
| 2559 |
|
| 2560 |
|
| 2561 |
self->parser = NULL; |
| 2562 |
self->names = |
| 2563 |
self->target = |
| 2564 |
self->handle_xml = |
| 2565 |
self->handle_start = |
| 2566 |
self->handle_data = |
| 2567 |
self->handle_end = |
| 2568 |
self->handle_comment = |
| 2569 |
self->handle_pi = |
| 2570 |
self->handle_close = |
| 2571 |
NULL; |
| 2572 |
|
| 2573 |
self->entity = PyDict_New(); |
| 2574 |
if (!self->entity) { |
| 2575 |
Py_DECREF(self); |
| 2576 |
return NULL; |
| 2577 |
} |
| 2578 |
|
| 2579 |
self->names = PyDict_New(); |
| 2580 |
if (!self->names) { |
| 2581 |
Py_DECREF(self); |
| 2582 |
return NULL; |
| 2583 |
} |
| 2584 |
|
| 2585 |
memory_handler.malloc_fcn = PyObject_Malloc; |
| 2586 |
memory_handler.realloc_fcn = PyObject_Realloc; |
| 2587 |
memory_handler.free_fcn = PyObject_Free; |
| 2588 |
|
| 2589 |
self->parser = EXPAT(ParserCreate_MM)(encoding, &memory_handler, "}"); |
| 2590 |
if (!self->parser) { |
| 2591 |
Py_DECREF(self); |
| 2592 |
PyErr_NoMemory(); |
| 2593 |
return NULL; |
| 2594 |
} |
| 2595 |
|
| 2596 |
if (EXPAT(SetHashSalt) != NULL) { |
| 2597 |
EXPAT(SetHashSalt)(self->parser, |
| 2598 |
(unsigned long)_Py_HashSecret.prefix); |
| 2599 |
} |
| 2600 |
|
| 2601 |
ALLOC(sizeof(XMLParserObject), "create expatparser"); |
| 2602 |
|
| 2603 |
|
| 2604 |
if (!target) { |
| 2605 |
target = treebuilder_new(); |
| 2606 |
if (!target) { |
| 2607 |
Py_DECREF(self); |
| 2608 |
return NULL; |
| 2609 |
} |
| 2610 |
} else |
| 2611 |
Py_INCREF(target); |
| 2612 |
self->target = target; |
| 2613 |
|
| 2614 |
self->handle_xml = PyObject_GetAttrString(target, "xml"); |
| 2615 |
if (ignore_attribute_error(self->handle_xml)) { |
| 2616 |
Py_DECREF(self); |
| 2617 |
return NULL; |
| 2618 |
} |
| 2619 |
self->handle_start = PyObject_GetAttrString(target, "start"); |
| 2620 |
if (ignore_attribute_error(self->handle_start)) { |
| 2621 |
Py_DECREF(self); |
| 2622 |
return NULL; |
| 2623 |
} |
| 2624 |
self->handle_data = PyObject_GetAttrString(target, "data"); |
| 2625 |
if (ignore_attribute_error(self->handle_data)) { |
| 2626 |
Py_DECREF(self); |
| 2627 |
return NULL; |
| 2628 |
} |
| 2629 |
self->handle_end = PyObject_GetAttrString(target, "end"); |
| 2630 |
if (ignore_attribute_error(self->handle_end)) { |
| 2631 |
Py_DECREF(self); |
| 2632 |
return NULL; |
| 2633 |
} |
| 2634 |
self->handle_comment = PyObject_GetAttrString(target, "comment"); |
| 2635 |
if (ignore_attribute_error(self->handle_comment)) { |
| 2636 |
Py_DECREF(self); |
| 2637 |
return NULL; |
| 2638 |
} |
| 2639 |
self->handle_pi = PyObject_GetAttrString(target, "pi"); |
| 2640 |
if (ignore_attribute_error(self->handle_pi)) { |
| 2641 |
Py_DECREF(self); |
| 2642 |
return NULL; |
| 2643 |
} |
| 2644 |
self->handle_close = PyObject_GetAttrString(target, "close"); |
| 2645 |
if (ignore_attribute_error(self->handle_close)) { |
| 2646 |
Py_DECREF(self); |
| 2647 |
return NULL; |
| 2648 |
} |
| 2649 |
|
| 2650 |
|
| 2651 |
EXPAT(SetUserData)(self->parser, self); |
| 2652 |
EXPAT(SetElementHandler)( |
| 2653 |
self->parser, |
| 2654 |
(XML_StartElementHandler) expat_start_handler, |
| 2655 |
(XML_EndElementHandler) expat_end_handler |
| 2656 |
); |
| 2657 |
EXPAT(SetDefaultHandlerExpand)( |
| 2658 |
self->parser, |
| 2659 |
(XML_DefaultHandler) expat_default_handler |
| 2660 |
); |
| 2661 |
EXPAT(SetCharacterDataHandler)( |
| 2662 |
self->parser, |
| 2663 |
(XML_CharacterDataHandler) expat_data_handler |
| 2664 |
); |
| 2665 |
if (self->handle_comment) |
| 2666 |
EXPAT(SetCommentHandler)( |
| 2667 |
self->parser, |
| 2668 |
(XML_CommentHandler) expat_comment_handler |
| 2669 |
); |
| 2670 |
if (self->handle_pi) |
| 2671 |
EXPAT(SetProcessingInstructionHandler)( |
| 2672 |
self->parser, |
| 2673 |
(XML_ProcessingInstructionHandler) expat_pi_handler |
| 2674 |
); |
| 2675 |
#if defined(Py_USING_UNICODE) |
| 2676 |
EXPAT(SetUnknownEncodingHandler)( |
| 2677 |
self->parser, |
| 2678 |
(XML_UnknownEncodingHandler) expat_unknown_encoding_handler, NULL |
| 2679 |
); |
| 2680 |
#endif |
| 2681 |
|
| 2682 |
return (PyObject*) self; |
| 2683 |
} |
| 2684 |
|
| 2685 |
static void |
| 2686 |
xmlparser_dealloc(XMLParserObject* self) |
| 2687 |
{ |
| 2688 |
if (self->parser != NULL) { |
| 2689 |
EXPAT(ParserFree)(self->parser); |
| 2690 |
} |
| 2691 |
|
| 2692 |
Py_XDECREF(self->handle_close); |
| 2693 |
Py_XDECREF(self->handle_pi); |
| 2694 |
Py_XDECREF(self->handle_comment); |
| 2695 |
Py_XDECREF(self->handle_end); |
| 2696 |
Py_XDECREF(self->handle_data); |
| 2697 |
Py_XDECREF(self->handle_start); |
| 2698 |
Py_XDECREF(self->handle_xml); |
| 2699 |
|
| 2700 |
Py_XDECREF(self->target); |
| 2701 |
Py_XDECREF(self->entity); |
| 2702 |
Py_XDECREF(self->names); |
| 2703 |
|
| 2704 |
RELEASE(sizeof(XMLParserObject), "destroy expatparser"); |
| 2705 |
|
| 2706 |
PyObject_Del(self); |
| 2707 |
} |
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
LOCAL(PyObject*) |
| 2713 |
expat_parse(XMLParserObject* self, char* data, int data_len, int final) |
| 2714 |
{ |
| 2715 |
int ok; |
| 2716 |
|
| 2717 |
ok = EXPAT(Parse)(self->parser, data, data_len, final); |
| 2718 |
|
| 2719 |
if (PyErr_Occurred()) |
| 2720 |
return NULL; |
| 2721 |
|
| 2722 |
if (!ok) { |
| 2723 |
expat_set_error( |
| 2724 |
EXPAT(ErrorString)(EXPAT(GetErrorCode)(self->parser)), |
| 2725 |
EXPAT(GetErrorLineNumber)(self->parser), |
| 2726 |
EXPAT(GetErrorColumnNumber)(self->parser) |
| 2727 |
); |
| 2728 |
return NULL; |
| 2729 |
} |
| 2730 |
|
| 2731 |
Py_RETURN_NONE; |
| 2732 |
} |
| 2733 |
|
| 2734 |
static PyObject* |
| 2735 |
xmlparser_close(XMLParserObject* self, PyObject* args) |
| 2736 |
{ |
| 2737 |
|
| 2738 |
|
| 2739 |
PyObject* res; |
| 2740 |
if (!PyArg_ParseTuple(args, ":close")) |
| 2741 |
return NULL; |
| 2742 |
|
| 2743 |
res = expat_parse(self, "", 0, 1); |
| 2744 |
if (!res) |
| 2745 |
return NULL; |
| 2746 |
|
| 2747 |
if (TreeBuilder_CheckExact(self->target)) { |
| 2748 |
Py_DECREF(res); |
| 2749 |
return treebuilder_done((TreeBuilderObject*) self->target); |
| 2750 |
} if (self->handle_close) { |
| 2751 |
Py_DECREF(res); |
| 2752 |
return PyObject_CallFunction(self->handle_close, ""); |
| 2753 |
} else |
| 2754 |
return res; |
| 2755 |
} |
| 2756 |
|
| 2757 |
static PyObject* |
| 2758 |
xmlparser_feed(XMLParserObject* self, PyObject* args) |
| 2759 |
{ |
| 2760 |
|
| 2761 |
|
| 2762 |
char* data; |
| 2763 |
int data_len; |
| 2764 |
if (!PyArg_ParseTuple(args, "s#:feed", &data, &data_len)) |
| 2765 |
return NULL; |
| 2766 |
|
| 2767 |
return expat_parse(self, data, data_len, 0); |
| 2768 |
} |
| 2769 |
|
| 2770 |
static PyObject* |
| 2771 |
xmlparser_parse(XMLParserObject* self, PyObject* args) |
| 2772 |
{ |
| 2773 |
|
| 2774 |
|
| 2775 |
PyObject* reader; |
| 2776 |
PyObject* buffer; |
| 2777 |
PyObject* res; |
| 2778 |
|
| 2779 |
PyObject* fileobj; |
| 2780 |
if (!PyArg_ParseTuple(args, "O:_parse", &fileobj)) |
| 2781 |
return NULL; |
| 2782 |
|
| 2783 |
reader = PyObject_GetAttrString(fileobj, "read"); |
| 2784 |
if (!reader) |
| 2785 |
return NULL; |
| 2786 |
|
| 2787 |
|
| 2788 |
for (;;) { |
| 2789 |
|
| 2790 |
buffer = PyObject_CallFunction(reader, "i", 64*1024); |
| 2791 |
|
| 2792 |
if (!buffer) { |
| 2793 |
|
| 2794 |
Py_DECREF(reader); |
| 2795 |
return NULL; |
| 2796 |
} |
| 2797 |
|
| 2798 |
if (!PyString_CheckExact(buffer) || PyString_GET_SIZE(buffer) == 0) { |
| 2799 |
Py_DECREF(buffer); |
| 2800 |
break; |
| 2801 |
} |
| 2802 |
|
| 2803 |
if (PyString_GET_SIZE(buffer) > INT_MAX) { |
| 2804 |
Py_DECREF(buffer); |
| 2805 |
Py_DECREF(reader); |
| 2806 |
PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 2807 |
return NULL; |
| 2808 |
} |
| 2809 |
res = expat_parse( |
| 2810 |
self, PyString_AS_STRING(buffer), (int)PyString_GET_SIZE(buffer), 0 |
| 2811 |
); |
| 2812 |
|
| 2813 |
Py_DECREF(buffer); |
| 2814 |
|
| 2815 |
if (!res) { |
| 2816 |
Py_DECREF(reader); |
| 2817 |
return NULL; |
| 2818 |
} |
| 2819 |
Py_DECREF(res); |
| 2820 |
|
| 2821 |
} |
| 2822 |
|
| 2823 |
Py_DECREF(reader); |
| 2824 |
|
| 2825 |
res = expat_parse(self, "", 0, 1); |
| 2826 |
|
| 2827 |
if (res && TreeBuilder_CheckExact(self->target)) { |
| 2828 |
Py_DECREF(res); |
| 2829 |
return treebuilder_done((TreeBuilderObject*) self->target); |
| 2830 |
} |
| 2831 |
|
| 2832 |
return res; |
| 2833 |
} |
| 2834 |
|
| 2835 |
static PyObject* |
| 2836 |
xmlparser_setevents(XMLParserObject* self, PyObject* args) |
| 2837 |
{ |
| 2838 |
|
| 2839 |
|
| 2840 |
Py_ssize_t i; |
| 2841 |
TreeBuilderObject* target; |
| 2842 |
|
| 2843 |
PyObject* events; |
| 2844 |
PyObject* event_set = Py_None; |
| 2845 |
if (!PyArg_ParseTuple(args, "O!|O:_setevents", &PyList_Type, &events, |
| 2846 |
&event_set)) |
| 2847 |
return NULL; |
| 2848 |
|
| 2849 |
if (!TreeBuilder_CheckExact(self->target)) { |
| 2850 |
PyErr_SetString( |
| 2851 |
PyExc_TypeError, |
| 2852 |
"event handling only supported for cElementTree.Treebuilder " |
| 2853 |
"targets" |
| 2854 |
); |
| 2855 |
return NULL; |
| 2856 |
} |
| 2857 |
|
| 2858 |
target = (TreeBuilderObject*) self->target; |
| 2859 |
|
| 2860 |
Py_INCREF(events); |
| 2861 |
Py_XSETREF(target->events, events); |
| 2862 |
|
| 2863 |
|
| 2864 |
Py_CLEAR(target->start_event_obj); |
| 2865 |
Py_CLEAR(target->end_event_obj); |
| 2866 |
Py_CLEAR(target->start_ns_event_obj); |
| 2867 |
Py_CLEAR(target->end_ns_event_obj); |
| 2868 |
|
| 2869 |
if (event_set == Py_None) { |
| 2870 |
|
| 2871 |
target->end_event_obj = PyString_FromString("end"); |
| 2872 |
Py_RETURN_NONE; |
| 2873 |
} |
| 2874 |
|
| 2875 |
if (!PyTuple_Check(event_set)) |
| 2876 |
goto error; |
| 2877 |
|
| 2878 |
for (i = 0; i < PyTuple_GET_SIZE(event_set); i++) { |
| 2879 |
PyObject* item = PyTuple_GET_ITEM(event_set, i); |
| 2880 |
char* event; |
| 2881 |
if (!PyString_Check(item)) |
| 2882 |
goto error; |
| 2883 |
Py_INCREF(item); |
| 2884 |
event = PyString_AS_STRING(item); |
| 2885 |
if (strcmp(event, "start") == 0) { |
| 2886 |
Py_XSETREF(target->start_event_obj, item); |
| 2887 |
} else if (strcmp(event, "end") == 0) { |
| 2888 |
Py_XSETREF(target->end_event_obj, item); |
| 2889 |
} else if (strcmp(event, "start-ns") == 0) { |
| 2890 |
Py_XSETREF(target->start_ns_event_obj, item); |
| 2891 |
EXPAT(SetNamespaceDeclHandler)( |
| 2892 |
self->parser, |
| 2893 |
(XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 2894 |
(XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 2895 |
); |
| 2896 |
} else if (strcmp(event, "end-ns") == 0) { |
| 2897 |
Py_XSETREF(target->end_ns_event_obj, item); |
| 2898 |
EXPAT(SetNamespaceDeclHandler)( |
| 2899 |
self->parser, |
| 2900 |
(XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 2901 |
(XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 2902 |
); |
| 2903 |
} else { |
| 2904 |
Py_DECREF(item); |
| 2905 |
PyErr_Format( |
| 2906 |
PyExc_ValueError, |
| 2907 |
"unknown event '%s'", event |
| 2908 |
); |
| 2909 |
return NULL; |
| 2910 |
} |
| 2911 |
} |
| 2912 |
|
| 2913 |
Py_RETURN_NONE; |
| 2914 |
|
| 2915 |
error: |
| 2916 |
PyErr_SetString( |
| 2917 |
PyExc_TypeError, |
| 2918 |
"invalid event tuple" |
| 2919 |
); |
| 2920 |
return NULL; |
| 2921 |
} |
| 2922 |
|
| 2923 |
static PyMethodDef xmlparser_methods[] = { |
| 2924 |
{"feed", (PyCFunction) xmlparser_feed, METH_VARARGS}, |
| 2925 |
{"close", (PyCFunction) xmlparser_close, METH_VARARGS}, |
| 2926 |
{"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS}, |
| 2927 |
{"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS}, |
| 2928 |
{NULL, NULL} |
| 2929 |
}; |
| 2930 |
|
| 2931 |
static PyObject* |
| 2932 |
xmlparser_getattr(XMLParserObject* self, char* name) |
| 2933 |
{ |
| 2934 |
PyObject* res; |
| 2935 |
|
| 2936 |
res = Py_FindMethod(xmlparser_methods, (PyObject*) self, name); |
| 2937 |
if (res) |
| 2938 |
return res; |
| 2939 |
|
| 2940 |
PyErr_Clear(); |
| 2941 |
|
| 2942 |
if (strcmp(name, "entity") == 0) |
| 2943 |
res = self->entity; |
| 2944 |
else if (strcmp(name, "target") == 0) |
| 2945 |
res = self->target; |
| 2946 |
else if (strcmp(name, "version") == 0) { |
| 2947 |
char buffer[100]; |
| 2948 |
sprintf(buffer, "Expat %d.%d.%d", XML_MAJOR_VERSION, |
| 2949 |
XML_MINOR_VERSION, XML_MICRO_VERSION); |
| 2950 |
return PyString_FromString(buffer); |
| 2951 |
} else { |
| 2952 |
PyErr_SetString(PyExc_AttributeError, name); |
| 2953 |
return NULL; |
| 2954 |
} |
| 2955 |
|
| 2956 |
Py_INCREF(res); |
| 2957 |
return res; |
| 2958 |
} |
| 2959 |
|
| 2960 |
statichere PyTypeObject XMLParser_Type = { |
| 2961 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2962 |
"XMLParser", sizeof(XMLParserObject), 0, |
| 2963 |
|
| 2964 |
(destructor)xmlparser_dealloc, |
| 2965 |
0, |
| 2966 |
(getattrfunc)xmlparser_getattr, |
| 2967 |
}; |
| 2968 |
|
| 2969 |
#endif |
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
|
| 2974 |
static PyMethodDef _functions[] = { |
| 2975 |
{"Element", (PyCFunction) element, METH_VARARGS|METH_KEYWORDS}, |
| 2976 |
{"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS}, |
| 2977 |
{"TreeBuilder", (PyCFunction) treebuilder, METH_VARARGS}, |
| 2978 |
#if defined(USE_EXPAT) |
| 2979 |
{"XMLParser", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS}, |
| 2980 |
{"XMLTreeBuilder", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS}, |
| 2981 |
#endif |
| 2982 |
{NULL, NULL} |
| 2983 |
}; |
| 2984 |
|
| 2985 |
DL_EXPORT(void) |
| 2986 |
init_elementtree(void) |
| 2987 |
{ |
| 2988 |
PyObject* m; |
| 2989 |
PyObject* g; |
| 2990 |
char* bootstrap; |
| 2991 |
|
| 2992 |
|
| 2993 |
Py_TYPE(&Element_Type) = Py_TYPE(&TreeBuilder_Type) = &PyType_Type; |
| 2994 |
#if defined(USE_EXPAT) |
| 2995 |
Py_TYPE(&XMLParser_Type) = &PyType_Type; |
| 2996 |
#endif |
| 2997 |
|
| 2998 |
m = Py_InitModule("_elementtree", _functions); |
| 2999 |
if (!m) |
| 3000 |
return; |
| 3001 |
|
| 3002 |
|
| 3003 |
|
| 3004 |
g = PyDict_New(); |
| 3005 |
if (!g) |
| 3006 |
return; |
| 3007 |
|
| 3008 |
PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins()); |
| 3009 |
|
| 3010 |
bootstrap = ( |
| 3011 |
|
| 3012 |
"from copy import copy, deepcopy\n" |
| 3013 |
|
| 3014 |
"try:\n" |
| 3015 |
" from xml.etree import ElementTree\n" |
| 3016 |
"except ImportError:\n" |
| 3017 |
" import ElementTree\n" |
| 3018 |
"ET = ElementTree\n" |
| 3019 |
"del ElementTree\n" |
| 3020 |
|
| 3021 |
"import _elementtree as cElementTree\n" |
| 3022 |
|
| 3023 |
"try:\n" |
| 3024 |
" copy(cElementTree.Element('x'))\n" |
| 3025 |
"except:\n" |
| 3026 |
" def copyelement(elem):\n" |
| 3027 |
" return elem\n" |
| 3028 |
|
| 3029 |
"class CommentProxy:\n" |
| 3030 |
" def __call__(self, text=None):\n" |
| 3031 |
" element = cElementTree.Element(ET.Comment)\n" |
| 3032 |
" element.text = text\n" |
| 3033 |
" return element\n" |
| 3034 |
" def __cmp__(self, other):\n" |
| 3035 |
" return cmp(ET.Comment, other)\n" |
| 3036 |
"cElementTree.Comment = CommentProxy()\n" |
| 3037 |
|
| 3038 |
"class ElementTree(ET.ElementTree):\n" |
| 3039 |
" def parse(self, source, parser=None):\n" |
| 3040 |
" close_source = False\n" |
| 3041 |
" if not hasattr(source, 'read'):\n" |
| 3042 |
" source = open(source, 'rb')\n" |
| 3043 |
" close_source = False\n" |
| 3044 |
" try:\n" |
| 3045 |
" if parser is not None:\n" |
| 3046 |
" while 1:\n" |
| 3047 |
" data = source.read(65536)\n" |
| 3048 |
" if not data:\n" |
| 3049 |
" break\n" |
| 3050 |
" parser.feed(data)\n" |
| 3051 |
" self._root = parser.close()\n" |
| 3052 |
" else:\n" |
| 3053 |
" parser = cElementTree.XMLParser()\n" |
| 3054 |
" self._root = parser._parse(source)\n" |
| 3055 |
" return self._root\n" |
| 3056 |
" finally:\n" |
| 3057 |
" if close_source:\n" |
| 3058 |
" source.close()\n" |
| 3059 |
"cElementTree.ElementTree = ElementTree\n" |
| 3060 |
|
| 3061 |
"def iter(node, tag=None):\n" |
| 3062 |
" if tag == '*':\n" |
| 3063 |
" tag = None\n" |
| 3064 |
" if tag is None or node.tag == tag:\n" |
| 3065 |
" yield node\n" |
| 3066 |
" for node in node:\n" |
| 3067 |
" for node in iter(node, tag):\n" |
| 3068 |
" yield node\n" |
| 3069 |
|
| 3070 |
"def itertext(node):\n" |
| 3071 |
" if node.text:\n" |
| 3072 |
" yield node.text\n" |
| 3073 |
" for e in node:\n" |
| 3074 |
" for s in e.itertext():\n" |
| 3075 |
" yield s\n" |
| 3076 |
" if e.tail:\n" |
| 3077 |
" yield e.tail\n" |
| 3078 |
|
| 3079 |
"def parse(source, parser=None):\n" |
| 3080 |
" tree = ElementTree()\n" |
| 3081 |
" tree.parse(source, parser)\n" |
| 3082 |
" return tree\n" |
| 3083 |
"cElementTree.parse = parse\n" |
| 3084 |
|
| 3085 |
"class iterparse(object):\n" |
| 3086 |
" root = None\n" |
| 3087 |
" def __init__(self, file, events=None):\n" |
| 3088 |
" self._close_file = False\n" |
| 3089 |
" if not hasattr(file, 'read'):\n" |
| 3090 |
" file = open(file, 'rb')\n" |
| 3091 |
" self._close_file = True\n" |
| 3092 |
" self._file = file\n" |
| 3093 |
" self._events = []\n" |
| 3094 |
" self._index = 0\n" |
| 3095 |
" self._error = None\n" |
| 3096 |
" self.root = self._root = None\n" |
| 3097 |
" b = cElementTree.TreeBuilder()\n" |
| 3098 |
" self._parser = cElementTree.XMLParser(b)\n" |
| 3099 |
" self._parser._setevents(self._events, events)\n" |
| 3100 |
" def next(self):\n" |
| 3101 |
" while 1:\n" |
| 3102 |
" try:\n" |
| 3103 |
" item = self._events[self._index]\n" |
| 3104 |
" self._index += 1\n" |
| 3105 |
" return item\n" |
| 3106 |
" except IndexError:\n" |
| 3107 |
" pass\n" |
| 3108 |
" if self._error:\n" |
| 3109 |
" e = self._error\n" |
| 3110 |
" self._error = None\n" |
| 3111 |
" raise e\n" |
| 3112 |
" if self._parser is None:\n" |
| 3113 |
" self.root = self._root\n" |
| 3114 |
" if self._close_file:\n" |
| 3115 |
" self._file.close()\n" |
| 3116 |
" raise StopIteration\n" |
| 3117 |
" # load event buffer\n" |
| 3118 |
" del self._events[:]\n" |
| 3119 |
" self._index = 0\n" |
| 3120 |
" data = self._file.read(16384)\n" |
| 3121 |
" if data:\n" |
| 3122 |
" try:\n" |
| 3123 |
" self._parser.feed(data)\n" |
| 3124 |
" except SyntaxError as exc:\n" |
| 3125 |
" self._error = exc\n" |
| 3126 |
" else:\n" |
| 3127 |
" self._root = self._parser.close()\n" |
| 3128 |
" self._parser = None\n" |
| 3129 |
" def __iter__(self):\n" |
| 3130 |
" return self\n" |
| 3131 |
"cElementTree.iterparse = iterparse\n" |
| 3132 |
|
| 3133 |
"class PIProxy:\n" |
| 3134 |
" def __call__(self, target, text=None):\n" |
| 3135 |
" element = cElementTree.Element(ET.PI)\n" |
| 3136 |
" element.text = target\n" |
| 3137 |
" if text:\n" |
| 3138 |
" element.text = element.text + ' ' + text\n" |
| 3139 |
" return element\n" |
| 3140 |
" def __cmp__(self, other):\n" |
| 3141 |
" return cmp(ET.PI, other)\n" |
| 3142 |
"cElementTree.PI = cElementTree.ProcessingInstruction = PIProxy()\n" |
| 3143 |
|
| 3144 |
"def XML(text):\n" |
| 3145 |
" parser = cElementTree.XMLParser()\n" |
| 3146 |
" parser.feed(text)\n" |
| 3147 |
" return parser.close()\n" |
| 3148 |
"cElementTree.XML = cElementTree.fromstring = XML\n" |
| 3149 |
|
| 3150 |
"def XMLID(text):\n" |
| 3151 |
" tree = XML(text)\n" |
| 3152 |
" ids = {}\n" |
| 3153 |
" for elem in tree.iter():\n" |
| 3154 |
" id = elem.get('id')\n" |
| 3155 |
" if id:\n" |
| 3156 |
" ids[id] = elem\n" |
| 3157 |
" return tree, ids\n" |
| 3158 |
"cElementTree.XMLID = XMLID\n" |
| 3159 |
|
| 3160 |
"try:\n" |
| 3161 |
" register_namespace = ET.register_namespace\n" |
| 3162 |
"except AttributeError:\n" |
| 3163 |
" def register_namespace(prefix, uri):\n" |
| 3164 |
" ET._namespace_map[uri] = prefix\n" |
| 3165 |
"cElementTree.register_namespace = register_namespace\n" |
| 3166 |
|
| 3167 |
"cElementTree.dump = ET.dump\n" |
| 3168 |
"cElementTree.ElementPath = ElementPath = ET.ElementPath\n" |
| 3169 |
"cElementTree.iselement = ET.iselement\n" |
| 3170 |
"cElementTree.QName = ET.QName\n" |
| 3171 |
"cElementTree.tostring = ET.tostring\n" |
| 3172 |
"cElementTree.fromstringlist = ET.fromstringlist\n" |
| 3173 |
"cElementTree.tostringlist = ET.tostringlist\n" |
| 3174 |
"cElementTree.VERSION = '" VERSION "'\n" |
| 3175 |
"cElementTree.__version__ = '" VERSION "'\n" |
| 3176 |
|
| 3177 |
); |
| 3178 |
|
| 3179 |
if (!PyRun_String(bootstrap, Py_file_input, g, NULL)) |
| 3180 |
return; |
| 3181 |
|
| 3182 |
elementpath_obj = PyDict_GetItemString(g, "ElementPath"); |
| 3183 |
|
| 3184 |
elementtree_copyelement_obj = PyDict_GetItemString(g, "copyelement"); |
| 3185 |
if (elementtree_copyelement_obj) { |
| 3186 |
|
| 3187 |
PyMethodDef* mp; |
| 3188 |
for (mp = element_methods; mp->ml_name; mp++) |
| 3189 |
if (mp->ml_meth == (PyCFunction) element_reduce) { |
| 3190 |
mp->ml_name = "__reduce__"; |
| 3191 |
break; |
| 3192 |
} |
| 3193 |
} else |
| 3194 |
PyErr_Clear(); |
| 3195 |
|
| 3196 |
elementtree_deepcopy_obj = PyDict_GetItemString(g, "deepcopy"); |
| 3197 |
elementtree_iter_obj = PyDict_GetItemString(g, "iter"); |
| 3198 |
elementtree_itertext_obj = PyDict_GetItemString(g, "itertext"); |
| 3199 |
|
| 3200 |
#if defined(USE_PYEXPAT_CAPI) |
| 3201 |
|
| 3202 |
expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); |
| 3203 |
if (expat_capi) { |
| 3204 |
|
| 3205 |
if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 || |
| 3206 |
expat_capi->size < sizeof(struct PyExpat_CAPI) || |
| 3207 |
expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || |
| 3208 |
expat_capi->MINOR_VERSION != XML_MINOR_VERSION || |
| 3209 |
expat_capi->MICRO_VERSION != XML_MICRO_VERSION) |
| 3210 |
expat_capi = NULL; |
| 3211 |
} |
| 3212 |
#endif |
| 3213 |
|
| 3214 |
elementtree_parseerror_obj = PyErr_NewException( |
| 3215 |
"cElementTree.ParseError", PyExc_SyntaxError, NULL |
| 3216 |
); |
| 3217 |
Py_INCREF(elementtree_parseerror_obj); |
| 3218 |
PyModule_AddObject(m, "ParseError", elementtree_parseerror_obj); |
| 3219 |
} |