Home | History | Annotate | Download | only in python
      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 (at) veillard.com
      8  */
      9 #include "libxml_wrap.h"
     10 #include <libxml/xpathInternals.h>
     11 
     12 #if PY_MAJOR_VERSION >= 3
     13 #define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
     14 #define PY_IMPORT_STRING PyUnicode_FromString
     15 #define PY_IMPORT_INT PyLong_FromLong
     16 #else
     17 #define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize
     18 #define PY_IMPORT_STRING PyString_FromString
     19 #define PY_IMPORT_INT PyInt_FromLong
     20 #endif
     21 
     22 #if PY_MAJOR_VERSION >= 3
     23 #include <stdio.h>
     24 #include <unistd.h>
     25 #include <fcntl.h>
     26 
     27 FILE *
     28 libxml_PyFileGet(PyObject *f) {
     29     int fd, flags;
     30     FILE *res;
     31     const char *mode;
     32 
     33     fd = PyObject_AsFileDescriptor(f);
     34     /*
     35      * Get the flags on the fd to understand how it was opened
     36      */
     37     flags = fcntl(fd, F_GETFL, 0);
     38     switch (flags & O_ACCMODE) {
     39         case O_RDWR:
     40 	    if (flags & O_APPEND)
     41 	        mode = "a+";
     42 	    else
     43 	        mode = "rw";
     44 	    break;
     45         case O_RDONLY:
     46 	    if (flags & O_APPEND)
     47 	        mode = "r+";
     48 	    else
     49 	        mode = "r";
     50 	    break;
     51 	case O_WRONLY:
     52 	    if (flags & O_APPEND)
     53 	        mode = "a";
     54 	    else
     55 	        mode = "w";
     56 	    break;
     57 	default:
     58 	    return(NULL);
     59     }
     60 
     61     /*
     62      * the FILE struct gets a new fd, so that it can be closed
     63      * independently of the file descriptor given. The risk though is
     64      * lack of sync. So at the python level sync must be implemented
     65      * before and after a conversion took place. No way around it
     66      * in the Python3 infrastructure !
     67      * The duplicated fd and FILE * will be released in the subsequent
     68      * call to libxml_PyFileRelease() which must be genrated accodingly
     69      */
     70     fd = dup(fd);
     71     if (fd == -1)
     72         return(NULL);
     73     res = fdopen(fd, mode);
     74     if (res == NULL) {
     75         close(fd);
     76 	return(NULL);
     77     }
     78     return(res);
     79 }
     80 
     81 void libxml_PyFileRelease(FILE *f) {
     82     if (f != NULL)
     83         fclose(f);
     84 }
     85 #endif
     86 
     87 PyObject *
     88 libxml_intWrap(int val)
     89 {
     90     PyObject *ret;
     91 
     92 #ifdef DEBUG
     93     printf("libxml_intWrap: val = %d\n", val);
     94 #endif
     95     ret = PY_IMPORT_INT((long) val);
     96     return (ret);
     97 }
     98 
     99 PyObject *
    100 libxml_longWrap(long val)
    101 {
    102     PyObject *ret;
    103 
    104 #ifdef DEBUG
    105     printf("libxml_longWrap: val = %ld\n", val);
    106 #endif
    107     ret = PyLong_FromLong(val);
    108     return (ret);
    109 }
    110 
    111 PyObject *
    112 libxml_doubleWrap(double val)
    113 {
    114     PyObject *ret;
    115 
    116 #ifdef DEBUG
    117     printf("libxml_doubleWrap: val = %f\n", val);
    118 #endif
    119     ret = PyFloat_FromDouble((double) val);
    120     return (ret);
    121 }
    122 
    123 PyObject *
    124 libxml_charPtrWrap(char *str)
    125 {
    126     PyObject *ret;
    127 
    128 #ifdef DEBUG
    129     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
    130 #endif
    131     if (str == NULL) {
    132         Py_INCREF(Py_None);
    133         return (Py_None);
    134     }
    135     ret = PY_IMPORT_STRING(str);
    136     xmlFree(str);
    137     return (ret);
    138 }
    139 
    140 PyObject *
    141 libxml_charPtrConstWrap(const char *str)
    142 {
    143     PyObject *ret;
    144 
    145 #ifdef DEBUG
    146     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
    147 #endif
    148     if (str == NULL) {
    149         Py_INCREF(Py_None);
    150         return (Py_None);
    151     }
    152     ret = PY_IMPORT_STRING(str);
    153     return (ret);
    154 }
    155 
    156 PyObject *
    157 libxml_xmlCharPtrWrap(xmlChar * str)
    158 {
    159     PyObject *ret;
    160 
    161 #ifdef DEBUG
    162     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
    163 #endif
    164     if (str == NULL) {
    165         Py_INCREF(Py_None);
    166         return (Py_None);
    167     }
    168     ret = PY_IMPORT_STRING((char *) str);
    169     xmlFree(str);
    170     return (ret);
    171 }
    172 
    173 PyObject *
    174 libxml_xmlCharPtrConstWrap(const xmlChar * str)
    175 {
    176     PyObject *ret;
    177 
    178 #ifdef DEBUG
    179     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
    180 #endif
    181     if (str == NULL) {
    182         Py_INCREF(Py_None);
    183         return (Py_None);
    184     }
    185     ret = PY_IMPORT_STRING((char *) str);
    186     return (ret);
    187 }
    188 
    189 PyObject *
    190 libxml_constcharPtrWrap(const char *str)
    191 {
    192     PyObject *ret;
    193 
    194 #ifdef DEBUG
    195     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
    196 #endif
    197     if (str == NULL) {
    198         Py_INCREF(Py_None);
    199         return (Py_None);
    200     }
    201     ret = PY_IMPORT_STRING(str);
    202     return (ret);
    203 }
    204 
    205 PyObject *
    206 libxml_constxmlCharPtrWrap(const xmlChar * str)
    207 {
    208     PyObject *ret;
    209 
    210 #ifdef DEBUG
    211     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
    212 #endif
    213     if (str == NULL) {
    214         Py_INCREF(Py_None);
    215         return (Py_None);
    216     }
    217     ret = PY_IMPORT_STRING((char *) str);
    218     return (ret);
    219 }
    220 
    221 PyObject *
    222 libxml_xmlDocPtrWrap(xmlDocPtr doc)
    223 {
    224     PyObject *ret;
    225 
    226 #ifdef DEBUG
    227     printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
    228 #endif
    229     if (doc == NULL) {
    230         Py_INCREF(Py_None);
    231         return (Py_None);
    232     }
    233     /* TODO: look at deallocation */
    234     ret = PyCapsule_New((void *) doc, (char *) "xmlDocPtr", NULL);
    235     return (ret);
    236 }
    237 
    238 PyObject *
    239 libxml_xmlNodePtrWrap(xmlNodePtr node)
    240 {
    241     PyObject *ret;
    242 
    243 #ifdef DEBUG
    244     printf("libxml_xmlNodePtrWrap: node = %p\n", node);
    245 #endif
    246     if (node == NULL) {
    247         Py_INCREF(Py_None);
    248         return (Py_None);
    249     }
    250     ret = PyCapsule_New((void *) node, (char *) "xmlNodePtr", NULL);
    251     return (ret);
    252 }
    253 
    254 PyObject *
    255 libxml_xmlURIPtrWrap(xmlURIPtr uri)
    256 {
    257     PyObject *ret;
    258 
    259 #ifdef DEBUG
    260     printf("libxml_xmlURIPtrWrap: uri = %p\n", uri);
    261 #endif
    262     if (uri == NULL) {
    263         Py_INCREF(Py_None);
    264         return (Py_None);
    265     }
    266     ret = PyCapsule_New((void *) uri, (char *) "xmlURIPtr", NULL);
    267     return (ret);
    268 }
    269 
    270 PyObject *
    271 libxml_xmlNsPtrWrap(xmlNsPtr ns)
    272 {
    273     PyObject *ret;
    274 
    275 #ifdef DEBUG
    276     printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
    277 #endif
    278     if (ns == NULL) {
    279         Py_INCREF(Py_None);
    280         return (Py_None);
    281     }
    282     ret = PyCapsule_New((void *) ns, (char *) "xmlNsPtr", NULL);
    283     return (ret);
    284 }
    285 
    286 PyObject *
    287 libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
    288 {
    289     PyObject *ret;
    290 
    291 #ifdef DEBUG
    292     printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
    293 #endif
    294     if (attr == NULL) {
    295         Py_INCREF(Py_None);
    296         return (Py_None);
    297     }
    298     ret = PyCapsule_New((void *) attr, (char *) "xmlAttrPtr", NULL);
    299     return (ret);
    300 }
    301 
    302 PyObject *
    303 libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
    304 {
    305     PyObject *ret;
    306 
    307 #ifdef DEBUG
    308     printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
    309 #endif
    310     if (attr == NULL) {
    311         Py_INCREF(Py_None);
    312         return (Py_None);
    313     }
    314     ret = PyCapsule_New((void *) attr, (char *) "xmlAttributePtr", NULL);
    315     return (ret);
    316 }
    317 
    318 PyObject *
    319 libxml_xmlElementPtrWrap(xmlElementPtr elem)
    320 {
    321     PyObject *ret;
    322 
    323 #ifdef DEBUG
    324     printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
    325 #endif
    326     if (elem == NULL) {
    327         Py_INCREF(Py_None);
    328         return (Py_None);
    329     }
    330     ret = PyCapsule_New((void *) elem, (char *) "xmlElementPtr", NULL);
    331     return (ret);
    332 }
    333 
    334 PyObject *
    335 libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
    336 {
    337     PyObject *ret;
    338 
    339 #ifdef DEBUG
    340     printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
    341 #endif
    342     if (ctxt == NULL) {
    343         Py_INCREF(Py_None);
    344         return (Py_None);
    345     }
    346     ret = PyCapsule_New((void *) ctxt, (char *) "xmlXPathContextPtr", NULL);
    347     return (ret);
    348 }
    349 
    350 PyObject *
    351 libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
    352 {
    353     PyObject *ret;
    354 
    355 #ifdef DEBUG
    356     printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
    357 #endif
    358     if (ctxt == NULL) {
    359         Py_INCREF(Py_None);
    360         return (Py_None);
    361     }
    362     ret = PyCapsule_New((void *)ctxt, (char *)"xmlXPathParserContextPtr", NULL);
    363     return (ret);
    364 }
    365 
    366 PyObject *
    367 libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
    368 {
    369     PyObject *ret;
    370 
    371 #ifdef DEBUG
    372     printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
    373 #endif
    374     if (ctxt == NULL) {
    375         Py_INCREF(Py_None);
    376         return (Py_None);
    377     }
    378 
    379     ret = PyCapsule_New((void *) ctxt, (char *) "xmlParserCtxtPtr", NULL);
    380     return (ret);
    381 }
    382 
    383 /**
    384  * libxml_xmlXPathDestructNsNode:
    385  * cap: xmlNsPtr namespace node capsule object
    386  *
    387  * This function is called if and when a namespace node returned in
    388  * an XPath node set is to be destroyed. That's the only kind of
    389  * object returned in node set not directly linked to the original
    390  * xmlDoc document, see xmlXPathNodeSetDupNs.
    391  */
    392 #if PY_VERSION_HEX < 0x02070000
    393 static void
    394 libxml_xmlXPathDestructNsNode(void *cap, void *desc ATTRIBUTE_UNUSED)
    395 #else
    396 static void
    397 libxml_xmlXPathDestructNsNode(PyObject *cap)
    398 #endif
    399 {
    400 #ifdef DEBUG
    401     fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cap);
    402 #endif
    403 #if PY_VERSION_HEX < 0x02070000
    404     xmlXPathNodeSetFreeNs((xmlNsPtr) cap);
    405 #else
    406     xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
    407 #endif
    408 }
    409 
    410 PyObject *
    411 libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
    412 {
    413     PyObject *ret;
    414 
    415 #ifdef DEBUG
    416     printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
    417 #endif
    418     if (obj == NULL) {
    419         Py_INCREF(Py_None);
    420         return (Py_None);
    421     }
    422     switch (obj->type) {
    423         case XPATH_XSLT_TREE: {
    424             if ((obj->nodesetval == NULL) ||
    425 		(obj->nodesetval->nodeNr == 0) ||
    426 		(obj->nodesetval->nodeTab == NULL)) {
    427                 ret = PyList_New(0);
    428 	    } else {
    429 		int i, len = 0;
    430 		xmlNodePtr node;
    431 
    432 		node = obj->nodesetval->nodeTab[0]->children;
    433 		while (node != NULL) {
    434 		    len++;
    435 		    node = node->next;
    436 		}
    437 		ret = PyList_New(len);
    438 		node = obj->nodesetval->nodeTab[0]->children;
    439 		for (i = 0;i < len;i++) {
    440                     PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
    441 		    node = node->next;
    442 		}
    443 	    }
    444 	    /*
    445 	     * Return now, do not free the object passed down
    446 	     */
    447 	    return (ret);
    448 	}
    449         case XPATH_NODESET:
    450             if ((obj->nodesetval == NULL)
    451                 || (obj->nodesetval->nodeNr == 0)) {
    452                 ret = PyList_New(0);
    453 	    } else {
    454                 int i;
    455                 xmlNodePtr node;
    456 
    457                 ret = PyList_New(obj->nodesetval->nodeNr);
    458                 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
    459                     node = obj->nodesetval->nodeTab[i];
    460                     if (node->type == XML_NAMESPACE_DECL) {
    461 		        PyObject *ns = PyCapsule_New((void *) node,
    462                                      (char *) "xmlNsPtr",
    463 				     libxml_xmlXPathDestructNsNode);
    464 			PyList_SetItem(ret, i, ns);
    465 			/* make sure the xmlNsPtr is not destroyed now */
    466 			obj->nodesetval->nodeTab[i] = NULL;
    467 		    } else {
    468 			PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
    469 		    }
    470                 }
    471             }
    472             break;
    473         case XPATH_BOOLEAN:
    474             ret = PY_IMPORT_INT((long) obj->boolval);
    475             break;
    476         case XPATH_NUMBER:
    477             ret = PyFloat_FromDouble(obj->floatval);
    478             break;
    479         case XPATH_STRING:
    480 	    ret = PY_IMPORT_STRING((char *) obj->stringval);
    481             break;
    482         case XPATH_POINT:
    483         {
    484             PyObject *node;
    485             PyObject *indexIntoNode;
    486             PyObject *tuple;
    487 
    488             node = libxml_xmlNodePtrWrap(obj->user);
    489             indexIntoNode = PY_IMPORT_INT((long) obj->index);
    490 
    491             tuple = PyTuple_New(2);
    492             PyTuple_SetItem(tuple, 0, node);
    493             PyTuple_SetItem(tuple, 1, indexIntoNode);
    494 
    495             ret = tuple;
    496             break;
    497         }
    498         case XPATH_RANGE:
    499         {
    500             unsigned short bCollapsedRange;
    501 
    502             bCollapsedRange = ( (obj->user2 == NULL) ||
    503 		                ((obj->user2 == obj->user) && (obj->index == obj->index2)) );
    504             if ( bCollapsedRange ) {
    505                 PyObject *node;
    506                 PyObject *indexIntoNode;
    507                 PyObject *tuple;
    508                 PyObject *list;
    509 
    510                 list = PyList_New(1);
    511 
    512                 node = libxml_xmlNodePtrWrap(obj->user);
    513                 indexIntoNode = PY_IMPORT_INT((long) obj->index);
    514 
    515                 tuple = PyTuple_New(2);
    516                 PyTuple_SetItem(tuple, 0, node);
    517                 PyTuple_SetItem(tuple, 1, indexIntoNode);
    518 
    519                 PyList_SetItem(list, 0, tuple);
    520 
    521                 ret = list;
    522             } else {
    523                 PyObject *node;
    524                 PyObject *indexIntoNode;
    525                 PyObject *tuple;
    526                 PyObject *list;
    527 
    528                 list = PyList_New(2);
    529 
    530                 node = libxml_xmlNodePtrWrap(obj->user);
    531                 indexIntoNode = PY_IMPORT_INT((long) obj->index);
    532 
    533                 tuple = PyTuple_New(2);
    534                 PyTuple_SetItem(tuple, 0, node);
    535                 PyTuple_SetItem(tuple, 1, indexIntoNode);
    536 
    537                 PyList_SetItem(list, 0, tuple);
    538 
    539                 node = libxml_xmlNodePtrWrap(obj->user2);
    540                 indexIntoNode = PY_IMPORT_INT((long) obj->index2);
    541 
    542                 tuple = PyTuple_New(2);
    543                 PyTuple_SetItem(tuple, 0, node);
    544                 PyTuple_SetItem(tuple, 1, indexIntoNode);
    545 
    546                 PyList_SetItem(list, 1, tuple);
    547 
    548                 ret = list;
    549             }
    550             break;
    551         }
    552         case XPATH_LOCATIONSET:
    553         {
    554             xmlLocationSetPtr set;
    555 
    556             set = obj->user;
    557             if ( set && set->locNr > 0 ) {
    558                 int i;
    559                 PyObject *list;
    560 
    561                 list = PyList_New(set->locNr);
    562 
    563                 for (i=0; i<set->locNr; i++) {
    564                     xmlXPathObjectPtr setobj;
    565                     PyObject *pyobj;
    566 
    567                     setobj = set->locTab[i]; /*xmlXPathObjectPtr setobj*/
    568 
    569                     pyobj = libxml_xmlXPathObjectPtrWrap(setobj);
    570                     /* xmlXPathFreeObject(setobj) is called */
    571                     set->locTab[i] = NULL;
    572 
    573                     PyList_SetItem(list, i, pyobj);
    574                 }
    575                 set->locNr = 0;
    576                 ret = list;
    577             } else {
    578                 Py_INCREF(Py_None);
    579                 ret = Py_None;
    580             }
    581             break;
    582         }
    583         default:
    584 #ifdef DEBUG
    585             printf("Unable to convert XPath object type %d\n", obj->type);
    586 #endif
    587             Py_INCREF(Py_None);
    588             ret = Py_None;
    589     }
    590     xmlXPathFreeObject(obj);
    591     return (ret);
    592 }
    593 
    594 xmlXPathObjectPtr
    595 libxml_xmlXPathObjectPtrConvert(PyObject *obj)
    596 {
    597     xmlXPathObjectPtr ret = NULL;
    598 
    599 #ifdef DEBUG
    600     printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
    601 #endif
    602     if (obj == NULL) {
    603         return (NULL);
    604     }
    605     if PyFloat_Check (obj) {
    606         ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
    607     } else if PyLong_Check(obj) {
    608 #ifdef PyLong_AS_LONG
    609         ret = xmlXPathNewFloat((double) PyLong_AS_LONG(obj));
    610 #else
    611         ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
    612 #endif
    613 #ifdef PyBool_Check
    614     } else if PyBool_Check (obj) {
    615 
    616         if (obj == Py_True) {
    617           ret = xmlXPathNewBoolean(1);
    618         }
    619         else {
    620           ret = xmlXPathNewBoolean(0);
    621         }
    622 #endif
    623     } else if PyBytes_Check (obj) {
    624         xmlChar *str;
    625 
    626         str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(obj),
    627                          PyBytes_GET_SIZE(obj));
    628         ret = xmlXPathWrapString(str);
    629 #ifdef PyUnicode_Check
    630     } else if PyUnicode_Check (obj) {
    631 #if PY_VERSION_HEX >= 0x03030000
    632         xmlChar *str;
    633 	const char *tmp;
    634 	Py_ssize_t size;
    635 
    636 	/* tmp doesn't need to be deallocated */
    637         tmp = PyUnicode_AsUTF8AndSize(obj, &size);
    638         str = xmlStrndup((const xmlChar *) tmp, (int) size);
    639         ret = xmlXPathWrapString(str);
    640 #else
    641         xmlChar *str = NULL;
    642         PyObject *b;
    643 
    644 	b = PyUnicode_AsUTF8String(obj);
    645 	if (b != NULL) {
    646 	    str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(b),
    647 			     PyBytes_GET_SIZE(b));
    648 	    Py_DECREF(b);
    649 	}
    650 	ret = xmlXPathWrapString(str);
    651 #endif
    652 #endif
    653     } else if PyList_Check (obj) {
    654         int i;
    655         PyObject *node;
    656         xmlNodePtr cur;
    657         xmlNodeSetPtr set;
    658 
    659         set = xmlXPathNodeSetCreate(NULL);
    660 
    661         for (i = 0; i < PyList_Size(obj); i++) {
    662             node = PyList_GetItem(obj, i);
    663             if ((node == NULL) || (node->ob_type == NULL))
    664                 continue;
    665 
    666             cur = NULL;
    667             if (PyCapsule_CheckExact(node)) {
    668 #ifdef DEBUG
    669                 printf("Got a Capsule\n");
    670 #endif
    671                 cur = PyxmlNode_Get(node);
    672             } else if ((PyObject_HasAttrString(node, (char *) "_o")) &&
    673 	               (PyObject_HasAttrString(node, (char *) "get_doc"))) {
    674 		PyObject *wrapper;
    675 
    676 		wrapper = PyObject_GetAttrString(node, (char *) "_o");
    677 		if (wrapper != NULL)
    678 		    cur = PyxmlNode_Get(wrapper);
    679             } else {
    680 #ifdef DEBUG
    681                 printf("Unknown object in Python return list\n");
    682 #endif
    683             }
    684             if (cur != NULL) {
    685                 xmlXPathNodeSetAdd(set, cur);
    686             }
    687         }
    688         ret = xmlXPathWrapNodeSet(set);
    689     } else {
    690 #ifdef DEBUG
    691         printf("Unable to convert Python Object to XPath");
    692 #endif
    693     }
    694     return (ret);
    695 }
    696 
    697 PyObject *
    698 libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
    699 {
    700 	PyObject *ret;
    701 
    702 #ifdef DEBUG
    703 	printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
    704 #endif
    705 	if (valid == NULL) {
    706 		Py_INCREF(Py_None);
    707 		return (Py_None);
    708 	}
    709 
    710 	ret =
    711 		PyCapsule_New((void *) valid,
    712 									 (char *) "xmlValidCtxtPtr", NULL);
    713 
    714 	return (ret);
    715 }
    716 
    717 PyObject *
    718 libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
    719 {
    720     PyObject *ret;
    721 
    722 #ifdef DEBUG
    723     printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
    724 #endif
    725     if (catal == NULL) {
    726         Py_INCREF(Py_None);
    727         return (Py_None);
    728     }
    729     ret =
    730         PyCapsule_New((void *) catal,
    731                                      (char *) "xmlCatalogPtr", NULL);
    732     return (ret);
    733 }
    734 
    735 PyObject *
    736 libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
    737 {
    738     PyObject *ret;
    739 
    740 #ifdef DEBUG
    741     printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer);
    742 #endif
    743     if (buffer == NULL) {
    744         Py_INCREF(Py_None);
    745         return (Py_None);
    746     }
    747     ret =
    748         PyCapsule_New((void *) buffer,
    749                                      (char *) "xmlOutputBufferPtr", NULL);
    750     return (ret);
    751 }
    752 
    753 PyObject *
    754 libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
    755 {
    756     PyObject *ret;
    757 
    758 #ifdef DEBUG
    759     printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer);
    760 #endif
    761     if (buffer == NULL) {
    762         Py_INCREF(Py_None);
    763         return (Py_None);
    764     }
    765     ret =
    766         PyCapsule_New((void *) buffer,
    767                                      (char *) "xmlParserInputBufferPtr", NULL);
    768     return (ret);
    769 }
    770 
    771 #ifdef LIBXML_REGEXP_ENABLED
    772 PyObject *
    773 libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
    774 {
    775     PyObject *ret;
    776 
    777 #ifdef DEBUG
    778     printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp);
    779 #endif
    780     if (regexp == NULL) {
    781         Py_INCREF(Py_None);
    782         return (Py_None);
    783     }
    784     ret =
    785         PyCapsule_New((void *) regexp,
    786                                      (char *) "xmlRegexpPtr", NULL);
    787     return (ret);
    788 }
    789 #endif /* LIBXML_REGEXP_ENABLED */
    790 
    791 #ifdef LIBXML_READER_ENABLED
    792 PyObject *
    793 libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
    794 {
    795     PyObject *ret;
    796 
    797 #ifdef DEBUG
    798     printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader);
    799 #endif
    800     if (reader == NULL) {
    801         Py_INCREF(Py_None);
    802         return (Py_None);
    803     }
    804     ret =
    805         PyCapsule_New((void *) reader,
    806                                      (char *) "xmlTextReaderPtr", NULL);
    807     return (ret);
    808 }
    809 
    810 PyObject *
    811 libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
    812 {
    813     PyObject *ret;
    814 
    815 #ifdef DEBUG
    816     printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator);
    817 #endif
    818     if (locator == NULL) {
    819         Py_INCREF(Py_None);
    820         return (Py_None);
    821     }
    822     ret =
    823         PyCapsule_New((void *) locator,
    824                                      (char *) "xmlTextReaderLocatorPtr", NULL);
    825     return (ret);
    826 }
    827 #endif /* LIBXML_READER_ENABLED */
    828 
    829 #ifdef LIBXML_SCHEMAS_ENABLED
    830 PyObject *
    831 libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
    832 {
    833     PyObject *ret;
    834 
    835 #ifdef DEBUG
    836     printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt);
    837 #endif
    838     if (ctxt == NULL) {
    839         Py_INCREF(Py_None);
    840         return (Py_None);
    841     }
    842     ret =
    843         PyCapsule_New((void *) ctxt,
    844                                      (char *) "xmlRelaxNGPtr", NULL);
    845     return (ret);
    846 }
    847 
    848 PyObject *
    849 libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
    850 {
    851     PyObject *ret;
    852 
    853 #ifdef DEBUG
    854     printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt);
    855 #endif
    856     if (ctxt == NULL) {
    857         Py_INCREF(Py_None);
    858         return (Py_None);
    859     }
    860     ret =
    861         PyCapsule_New((void *) ctxt,
    862                                      (char *) "xmlRelaxNGParserCtxtPtr", NULL);
    863     return (ret);
    864 }
    865 PyObject *
    866 libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
    867 {
    868     PyObject *ret;
    869 
    870 #ifdef DEBUG
    871     printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid);
    872 #endif
    873     if (valid == NULL) {
    874         Py_INCREF(Py_None);
    875         return (Py_None);
    876     }
    877     ret =
    878         PyCapsule_New((void *) valid,
    879                                      (char *) "xmlRelaxNGValidCtxtPtr", NULL);
    880     return (ret);
    881 }
    882 
    883 PyObject *
    884 libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)
    885 {
    886 	PyObject *ret;
    887 
    888 #ifdef DEBUG
    889 	printf("libxml_xmlSchemaPtrWrap: ctxt = %p\n", ctxt);
    890 #endif
    891 	if (ctxt == NULL) {
    892 		Py_INCREF(Py_None);
    893 		return (Py_None);
    894 	}
    895 	ret =
    896 		PyCapsule_New((void *) ctxt,
    897 									 (char *) "xmlSchemaPtr", NULL);
    898 	return (ret);
    899 }
    900 
    901 PyObject *
    902 libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)
    903 {
    904 	PyObject *ret;
    905 
    906 #ifdef DEBUG
    907 	printf("libxml_xmlSchemaParserCtxtPtrWrap: ctxt = %p\n", ctxt);
    908 #endif
    909 	if (ctxt == NULL) {
    910 		Py_INCREF(Py_None);
    911 		return (Py_None);
    912 	}
    913 	ret =
    914 		PyCapsule_New((void *) ctxt,
    915 									 (char *) "xmlSchemaParserCtxtPtr", NULL);
    916 
    917 	return (ret);
    918 }
    919 
    920 PyObject *
    921 libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
    922 {
    923 	PyObject *ret;
    924 
    925 #ifdef DEBUG
    926 	printf("libxml_xmlSchemaValidCtxtPtrWrap: valid = %p\n", valid);
    927 #endif
    928 	if (valid == NULL) {
    929 		Py_INCREF(Py_None);
    930 		return (Py_None);
    931 	}
    932 
    933 	ret =
    934 		PyCapsule_New((void *) valid,
    935 									 (char *) "xmlSchemaValidCtxtPtr", NULL);
    936 
    937 	return (ret);
    938 }
    939 #endif /* LIBXML_SCHEMAS_ENABLED */
    940 
    941 PyObject *
    942 libxml_xmlErrorPtrWrap(xmlErrorPtr error)
    943 {
    944     PyObject *ret;
    945 
    946 #ifdef DEBUG
    947     printf("libxml_xmlErrorPtrWrap: error = %p\n", error);
    948 #endif
    949     if (error == NULL) {
    950         Py_INCREF(Py_None);
    951         return (Py_None);
    952     }
    953     ret = PyCapsule_New((void *) error, (char *) "xmlErrorPtr", NULL);
    954     return (ret);
    955 }
    956