types.c revision 634ec27a804bbdf13b5baa7d5883524c5a4bd189
1/* 2 * types.c: converter functions between the internal representation 3 * and the Python objects 4 * 5 * See Copyright for the status of this software. 6 * 7 * daniel@veillard.com 8 */ 9#include "libxml_wrap.h" 10 11PyObject * 12libxml_intWrap(int val) 13{ 14 PyObject *ret; 15 16#ifdef DEBUG 17 printf("libxml_intWrap: val = %d\n", val); 18#endif 19 ret = PyInt_FromLong((long) val); 20 return (ret); 21} 22 23PyObject * 24libxml_longWrap(long val) 25{ 26 PyObject *ret; 27 28#ifdef DEBUG 29 printf("libxml_longWrap: val = %ld\n", val); 30#endif 31 ret = PyInt_FromLong(val); 32 return (ret); 33} 34 35PyObject * 36libxml_doubleWrap(double val) 37{ 38 PyObject *ret; 39 40#ifdef DEBUG 41 printf("libxml_doubleWrap: val = %f\n", val); 42#endif 43 ret = PyFloat_FromDouble((double) val); 44 return (ret); 45} 46 47PyObject * 48libxml_charPtrWrap(char *str) 49{ 50 PyObject *ret; 51 52#ifdef DEBUG 53 printf("libxml_xmlcharPtrWrap: str = %s\n", str); 54#endif 55 if (str == NULL) { 56 Py_INCREF(Py_None); 57 return (Py_None); 58 } 59 /* TODO: look at deallocation */ 60 ret = PyString_FromString(str); 61 xmlFree(str); 62 return (ret); 63} 64 65PyObject * 66libxml_charPtrConstWrap(const char *str) 67{ 68 PyObject *ret; 69 70#ifdef DEBUG 71 printf("libxml_xmlcharPtrWrap: str = %s\n", str); 72#endif 73 if (str == NULL) { 74 Py_INCREF(Py_None); 75 return (Py_None); 76 } 77 /* TODO: look at deallocation */ 78 ret = PyString_FromString(str); 79 return (ret); 80} 81 82PyObject * 83libxml_xmlCharPtrWrap(xmlChar * str) 84{ 85 PyObject *ret; 86 87#ifdef DEBUG 88 printf("libxml_xmlCharPtrWrap: str = %s\n", str); 89#endif 90 if (str == NULL) { 91 Py_INCREF(Py_None); 92 return (Py_None); 93 } 94 /* TODO: look at deallocation */ 95 ret = PyString_FromString((char *) str); 96 xmlFree(str); 97 return (ret); 98} 99 100PyObject * 101libxml_xmlCharPtrConstWrap(const xmlChar * str) 102{ 103 PyObject *ret; 104 105#ifdef DEBUG 106 printf("libxml_xmlCharPtrWrap: str = %s\n", str); 107#endif 108 if (str == NULL) { 109 Py_INCREF(Py_None); 110 return (Py_None); 111 } 112 /* TODO: look at deallocation */ 113 ret = PyString_FromString((char *) str); 114 return (ret); 115} 116 117PyObject * 118libxml_constcharPtrWrap(const char *str) 119{ 120 PyObject *ret; 121 122#ifdef DEBUG 123 printf("libxml_xmlcharPtrWrap: str = %s\n", str); 124#endif 125 if (str == NULL) { 126 Py_INCREF(Py_None); 127 return (Py_None); 128 } 129 /* TODO: look at deallocation */ 130 ret = PyString_FromString(str); 131 return (ret); 132} 133 134PyObject * 135libxml_constxmlCharPtrWrap(const xmlChar * str) 136{ 137 PyObject *ret; 138 139#ifdef DEBUG 140 printf("libxml_xmlCharPtrWrap: str = %s\n", str); 141#endif 142 if (str == NULL) { 143 Py_INCREF(Py_None); 144 return (Py_None); 145 } 146 /* TODO: look at deallocation */ 147 ret = PyString_FromString((char *) str); 148 return (ret); 149} 150 151PyObject * 152libxml_xmlDocPtrWrap(xmlDocPtr doc) 153{ 154 PyObject *ret; 155 156#ifdef DEBUG 157 printf("libxml_xmlDocPtrWrap: doc = %p\n", doc); 158#endif 159 if (doc == NULL) { 160 Py_INCREF(Py_None); 161 return (Py_None); 162 } 163 /* TODO: look at deallocation */ 164 ret = 165 PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr", 166 NULL); 167 return (ret); 168} 169 170PyObject * 171libxml_xmlNodePtrWrap(xmlNodePtr node) 172{ 173 PyObject *ret; 174 175#ifdef DEBUG 176 printf("libxml_xmlNodePtrWrap: node = %p\n", node); 177#endif 178 if (node == NULL) { 179 Py_INCREF(Py_None); 180 return (Py_None); 181 } 182 ret = 183 PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr", 184 NULL); 185 return (ret); 186} 187 188PyObject * 189libxml_xmlURIPtrWrap(xmlURIPtr uri) 190{ 191 PyObject *ret; 192 193#ifdef DEBUG 194 printf("libxml_xmlURIPtrWrap: uri = %p\n", uri); 195#endif 196 if (uri == NULL) { 197 Py_INCREF(Py_None); 198 return (Py_None); 199 } 200 ret = 201 PyCObject_FromVoidPtrAndDesc((void *) uri, (char *) "xmlURIPtr", 202 NULL); 203 return (ret); 204} 205 206PyObject * 207libxml_xmlNsPtrWrap(xmlNsPtr ns) 208{ 209 PyObject *ret; 210 211#ifdef DEBUG 212 printf("libxml_xmlNsPtrWrap: node = %p\n", ns); 213#endif 214 if (ns == NULL) { 215 Py_INCREF(Py_None); 216 return (Py_None); 217 } 218 ret = 219 PyCObject_FromVoidPtrAndDesc((void *) ns, (char *) "xmlNsPtr", 220 NULL); 221 return (ret); 222} 223 224PyObject * 225libxml_xmlAttrPtrWrap(xmlAttrPtr attr) 226{ 227 PyObject *ret; 228 229#ifdef DEBUG 230 printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr); 231#endif 232 if (attr == NULL) { 233 Py_INCREF(Py_None); 234 return (Py_None); 235 } 236 ret = 237 PyCObject_FromVoidPtrAndDesc((void *) attr, (char *) "xmlAttrPtr", 238 NULL); 239 return (ret); 240} 241 242PyObject * 243libxml_xmlAttributePtrWrap(xmlAttributePtr attr) 244{ 245 PyObject *ret; 246 247#ifdef DEBUG 248 printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr); 249#endif 250 if (attr == NULL) { 251 Py_INCREF(Py_None); 252 return (Py_None); 253 } 254 ret = 255 PyCObject_FromVoidPtrAndDesc((void *) attr, 256 (char *) "xmlAttributePtr", NULL); 257 return (ret); 258} 259 260PyObject * 261libxml_xmlElementPtrWrap(xmlElementPtr elem) 262{ 263 PyObject *ret; 264 265#ifdef DEBUG 266 printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem); 267#endif 268 if (elem == NULL) { 269 Py_INCREF(Py_None); 270 return (Py_None); 271 } 272 ret = 273 PyCObject_FromVoidPtrAndDesc((void *) elem, 274 (char *) "xmlElementPtr", NULL); 275 return (ret); 276} 277 278PyObject * 279libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt) 280{ 281 PyObject *ret; 282 283#ifdef DEBUG 284 printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt); 285#endif 286 if (ctxt == NULL) { 287 Py_INCREF(Py_None); 288 return (Py_None); 289 } 290 ret = 291 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 292 (char *) "xmlXPathContextPtr", NULL); 293 return (ret); 294} 295 296PyObject * 297libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt) 298{ 299 PyObject *ret; 300 301#ifdef DEBUG 302 printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt); 303#endif 304 if (ctxt == NULL) { 305 Py_INCREF(Py_None); 306 return (Py_None); 307 } 308 ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt, 309 (char *) "xmlXPathParserContextPtr", 310 NULL); 311 return (ret); 312} 313 314PyObject * 315libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt) 316{ 317 PyObject *ret; 318 319#ifdef DEBUG 320 printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt); 321#endif 322 if (ctxt == NULL) { 323 Py_INCREF(Py_None); 324 return (Py_None); 325 } 326 327 ret = 328 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 329 (char *) "xmlParserCtxtPtr", NULL); 330 return (ret); 331} 332 333PyObject * 334libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) 335{ 336 PyObject *ret; 337 338#ifdef DEBUG 339 printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj); 340#endif 341 if (obj == NULL) { 342 Py_INCREF(Py_None); 343 return (Py_None); 344 } 345 switch (obj->type) { 346 case XPATH_XSLT_TREE: { 347 if ((obj->nodesetval == NULL) || 348 (obj->nodesetval->nodeNr == 0) || 349 (obj->nodesetval->nodeTab == NULL)) { 350 ret = PyList_New(0); 351 } else { 352 int i, len = 0; 353 xmlNodePtr node; 354 355 node = obj->nodesetval->nodeTab[0]->children; 356 while (node != NULL) { 357 len++; 358 node = node->next; 359 } 360 ret = PyList_New(len); 361 node = obj->nodesetval->nodeTab[0]->children; 362 for (i = 0;i < len;i++) { 363 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); 364 node = node->next; 365 } 366 } 367 /* 368 * Return now, do not free the object passed down 369 */ 370 return (ret); 371 } 372 case XPATH_NODESET: 373 if ((obj->nodesetval == NULL) 374 || (obj->nodesetval->nodeNr == 0)) { 375 ret = PyList_New(0); 376 } else { 377 int i; 378 xmlNodePtr node; 379 380 ret = PyList_New(obj->nodesetval->nodeNr); 381 for (i = 0; i < obj->nodesetval->nodeNr; i++) { 382 node = obj->nodesetval->nodeTab[i]; 383 /* TODO: try to cast directly to the proper node type */ 384 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); 385 } 386 } 387 break; 388 case XPATH_BOOLEAN: 389 ret = PyInt_FromLong((long) obj->boolval); 390 break; 391 case XPATH_NUMBER: 392 ret = PyFloat_FromDouble(obj->floatval); 393 break; 394 case XPATH_STRING: 395 ret = PyString_FromString((char *) obj->stringval); 396 break; 397 case XPATH_POINT: 398 case XPATH_RANGE: 399 case XPATH_LOCATIONSET: 400 default: 401#ifdef DEBUG 402 printf("Unable to convert XPath object type %d\n", obj->type); 403#endif 404 Py_INCREF(Py_None); 405 ret = Py_None; 406 } 407 xmlXPathFreeObject(obj); 408 return (ret); 409} 410 411xmlXPathObjectPtr 412libxml_xmlXPathObjectPtrConvert(PyObject * obj) 413{ 414 xmlXPathObjectPtr ret = NULL; 415 416#ifdef DEBUG 417 printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj); 418#endif 419 if (obj == NULL) { 420 return (NULL); 421 } 422 if PyFloat_Check 423 (obj) { 424 ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj)); 425 } else if PyString_Check 426 (obj) { 427 xmlChar *str; 428 429 str = xmlStrndup((const xmlChar *) PyString_AS_STRING(obj), 430 PyString_GET_SIZE(obj)); 431 ret = xmlXPathWrapString(str); 432 } else if PyList_Check 433 (obj) { 434 int i; 435 PyObject *node; 436 xmlNodePtr cur; 437 xmlNodeSetPtr set; 438 439 set = xmlXPathNodeSetCreate(NULL); 440 441 for (i = 0; i < PyList_Size(obj); i++) { 442 node = PyList_GetItem(obj, i); 443 if ((node == NULL) || (node->ob_type == NULL)) 444 continue; 445 446 cur = NULL; 447 if (PyCObject_Check(node)) { 448#ifdef DEBUG 449 printf("Got a CObject\n"); 450#endif 451 cur = PyxmlNode_Get(node); 452 } else if (PyInstance_Check(node)) { 453 PyInstanceObject *inst = (PyInstanceObject *) node; 454 PyObject *name = inst->in_class->cl_name; 455 456 if PyString_Check 457 (name) { 458 char *type = PyString_AS_STRING(name); 459 PyObject *wrapper; 460 461 if (!strcmp(type, "xmlNode")) { 462 wrapper = 463 PyObject_GetAttrString(node, (char *) "_o"); 464 if (wrapper != NULL) { 465 cur = PyxmlNode_Get(wrapper); 466 } 467 } 468 } 469 } else { 470#ifdef DEBUG 471 printf("Unknown object in Python return list\n"); 472#endif 473 } 474 if (cur != NULL) { 475 xmlXPathNodeSetAdd(set, cur); 476 } 477 } 478 ret = xmlXPathWrapNodeSet(set); 479 } else { 480#ifdef DEBUG 481 printf("Unable to convert Python Object to XPath"); 482#endif 483 } 484 Py_DECREF(obj); 485 return (ret); 486} 487 488PyObject * 489libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid) 490{ 491 PyObject *ret; 492 493#ifdef DEBUG 494 printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid); 495#endif 496 if (valid == NULL) { 497 Py_INCREF(Py_None); 498 return (Py_None); 499 } 500 501 ret = 502 PyCObject_FromVoidPtrAndDesc((void *) valid, 503 (char *) "xmlValidCtxtPtr", NULL); 504 505 return (ret); 506} 507 508PyObject * 509libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal) 510{ 511 PyObject *ret; 512 513#ifdef DEBUG 514 printf("libxml_xmlNodePtrWrap: catal = %p\n", catal); 515#endif 516 if (catal == NULL) { 517 Py_INCREF(Py_None); 518 return (Py_None); 519 } 520 ret = 521 PyCObject_FromVoidPtrAndDesc((void *) catal, 522 (char *) "xmlCatalogPtr", NULL); 523 return (ret); 524} 525 526PyObject * 527libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer) 528{ 529 PyObject *ret; 530 531#ifdef DEBUG 532 printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer); 533#endif 534 if (buffer == NULL) { 535 Py_INCREF(Py_None); 536 return (Py_None); 537 } 538 ret = 539 PyCObject_FromVoidPtrAndDesc((void *) buffer, 540 (char *) "xmlOutputBufferPtr", NULL); 541 return (ret); 542} 543 544PyObject * 545libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer) 546{ 547 PyObject *ret; 548 549#ifdef DEBUG 550 printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer); 551#endif 552 if (buffer == NULL) { 553 Py_INCREF(Py_None); 554 return (Py_None); 555 } 556 ret = 557 PyCObject_FromVoidPtrAndDesc((void *) buffer, 558 (char *) "xmlParserInputBufferPtr", NULL); 559 return (ret); 560} 561 562#ifdef LIBXML_REGEXP_ENABLED 563PyObject * 564libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp) 565{ 566 PyObject *ret; 567 568#ifdef DEBUG 569 printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp); 570#endif 571 if (regexp == NULL) { 572 Py_INCREF(Py_None); 573 return (Py_None); 574 } 575 ret = 576 PyCObject_FromVoidPtrAndDesc((void *) regexp, 577 (char *) "xmlRegexpPtr", NULL); 578 return (ret); 579} 580#endif /* LIBXML_REGEXP_ENABLED */ 581 582PyObject * 583libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader) 584{ 585 PyObject *ret; 586 587#ifdef DEBUG 588 printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader); 589#endif 590 if (reader == NULL) { 591 Py_INCREF(Py_None); 592 return (Py_None); 593 } 594 ret = 595 PyCObject_FromVoidPtrAndDesc((void *) reader, 596 (char *) "xmlTextReaderPtr", NULL); 597 return (ret); 598} 599 600PyObject * 601libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator) 602{ 603 PyObject *ret; 604 605#ifdef DEBUG 606 printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator); 607#endif 608 if (locator == NULL) { 609 Py_INCREF(Py_None); 610 return (Py_None); 611 } 612 ret = 613 PyCObject_FromVoidPtrAndDesc((void *) locator, 614 (char *) "xmlTextReaderLocatorPtr", NULL); 615 return (ret); 616} 617 618#ifdef LIBXML_SCHEMAS_ENABLED 619PyObject * 620libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt) 621{ 622 PyObject *ret; 623 624#ifdef DEBUG 625 printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt); 626#endif 627 if (ctxt == NULL) { 628 Py_INCREF(Py_None); 629 return (Py_None); 630 } 631 ret = 632 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 633 (char *) "xmlRelaxNGPtr", NULL); 634 return (ret); 635} 636 637PyObject * 638libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt) 639{ 640 PyObject *ret; 641 642#ifdef DEBUG 643 printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt); 644#endif 645 if (ctxt == NULL) { 646 Py_INCREF(Py_None); 647 return (Py_None); 648 } 649 ret = 650 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 651 (char *) "xmlRelaxNGParserCtxtPtr", NULL); 652 return (ret); 653} 654PyObject * 655libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid) 656{ 657 PyObject *ret; 658 659#ifdef DEBUG 660 printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid); 661#endif 662 if (valid == NULL) { 663 Py_INCREF(Py_None); 664 return (Py_None); 665 } 666 ret = 667 PyCObject_FromVoidPtrAndDesc((void *) valid, 668 (char *) "xmlRelaxNGValidCtxtPtr", NULL); 669 return (ret); 670} 671 672PyObject * 673libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt) 674{ 675 PyObject *ret; 676 677#ifdef DEBUG 678 printf("libxml_xmlSchemaPtrWrap: ctxt = %p\n", ctxt); 679#endif 680 if (ctxt == NULL) { 681 Py_INCREF(Py_None); 682 return (Py_None); 683 } 684 ret = 685 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 686 (char *) "xmlSchemaPtr", NULL); 687 return (ret); 688} 689 690PyObject * 691libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt) 692{ 693 PyObject *ret; 694 695#ifdef DEBUG 696 printf("libxml_xmlSchemaParserCtxtPtrWrap: ctxt = %p\n", ctxt); 697#endif 698 if (ctxt == NULL) { 699 Py_INCREF(Py_None); 700 return (Py_None); 701 } 702 ret = 703 PyCObject_FromVoidPtrAndDesc((void *) ctxt, 704 (char *) "xmlSchemaParserCtxtPtr", NULL); 705 706 return (ret); 707} 708 709PyObject * 710libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid) 711{ 712 PyObject *ret; 713 714#ifdef DEBUG 715 printf("libxml_xmlSchemaValidCtxtPtrWrap: valid = %p\n", valid); 716#endif 717 if (valid == NULL) { 718 Py_INCREF(Py_None); 719 return (Py_None); 720 } 721 722 ret = 723 PyCObject_FromVoidPtrAndDesc((void *) valid, 724 (char *) "xmlSchemaValidCtxtPtr", NULL); 725 726 return (ret); 727} 728#endif /* LIBXML_SCHEMAS_ENABLED */ 729 730PyObject * 731libxml_xmlErrorPtrWrap(xmlErrorPtr error) 732{ 733 PyObject *ret; 734 735#ifdef DEBUG 736 printf("libxml_xmlErrorPtrWrap: error = %p\n", error); 737#endif 738 if (error == NULL) { 739 Py_INCREF(Py_None); 740 return (Py_None); 741 } 742 ret = 743 PyCObject_FromVoidPtrAndDesc((void *) error, 744 (char *) "xmlErrorPtr", NULL); 745 return (ret); 746} 747