2
* types.c: converter functions between the internal representation
3
* and the Python objects
5
* See Copyright for the status of this software.
9
#include "libxml_wrap.h"
10
#include <libxml/xpathInternals.h>
13
libxml_intWrap(int val)
18
printf("libxml_intWrap: val = %d\n", val);
20
ret = PyInt_FromLong((long) val);
25
libxml_longWrap(long val)
30
printf("libxml_longWrap: val = %ld\n", val);
32
ret = PyInt_FromLong(val);
37
libxml_doubleWrap(double val)
42
printf("libxml_doubleWrap: val = %f\n", val);
44
ret = PyFloat_FromDouble((double) val);
49
libxml_charPtrWrap(char *str)
54
printf("libxml_xmlcharPtrWrap: str = %s\n", str);
60
/* TODO: look at deallocation */
61
ret = PyString_FromString(str);
67
libxml_charPtrConstWrap(const char *str)
72
printf("libxml_xmlcharPtrWrap: str = %s\n", str);
78
/* TODO: look at deallocation */
79
ret = PyString_FromString(str);
84
libxml_xmlCharPtrWrap(xmlChar * str)
89
printf("libxml_xmlCharPtrWrap: str = %s\n", str);
95
/* TODO: look at deallocation */
96
ret = PyString_FromString((char *) str);
102
libxml_xmlCharPtrConstWrap(const xmlChar * str)
107
printf("libxml_xmlCharPtrWrap: str = %s\n", str);
113
/* TODO: look at deallocation */
114
ret = PyString_FromString((char *) str);
119
libxml_constcharPtrWrap(const char *str)
124
printf("libxml_xmlcharPtrWrap: str = %s\n", str);
130
/* TODO: look at deallocation */
131
ret = PyString_FromString(str);
136
libxml_constxmlCharPtrWrap(const xmlChar * str)
141
printf("libxml_xmlCharPtrWrap: str = %s\n", str);
147
/* TODO: look at deallocation */
148
ret = PyString_FromString((char *) str);
153
libxml_xmlDocPtrWrap(xmlDocPtr doc)
158
printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
164
/* TODO: look at deallocation */
166
PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr",
172
libxml_xmlNodePtrWrap(xmlNodePtr node)
177
printf("libxml_xmlNodePtrWrap: node = %p\n", node);
184
PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr",
190
libxml_xmlURIPtrWrap(xmlURIPtr uri)
195
printf("libxml_xmlURIPtrWrap: uri = %p\n", uri);
202
PyCObject_FromVoidPtrAndDesc((void *) uri, (char *) "xmlURIPtr",
208
libxml_xmlNsPtrWrap(xmlNsPtr ns)
213
printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
220
PyCObject_FromVoidPtrAndDesc((void *) ns, (char *) "xmlNsPtr",
226
libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
231
printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
238
PyCObject_FromVoidPtrAndDesc((void *) attr, (char *) "xmlAttrPtr",
244
libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
249
printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
256
PyCObject_FromVoidPtrAndDesc((void *) attr,
257
(char *) "xmlAttributePtr", NULL);
262
libxml_xmlElementPtrWrap(xmlElementPtr elem)
267
printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
274
PyCObject_FromVoidPtrAndDesc((void *) elem,
275
(char *) "xmlElementPtr", NULL);
280
libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
285
printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
292
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
293
(char *) "xmlXPathContextPtr", NULL);
298
libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
303
printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
309
ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
310
(char *) "xmlXPathParserContextPtr",
316
libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
321
printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
329
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
330
(char *) "xmlParserCtxtPtr", NULL);
335
* libxml_xmlXPathDestructNsNode:
336
* cobj: xmlNsPtr namespace node
337
* desc: ignored string
339
* This function is called if and when a namespace node returned in
340
* an XPath node set is to be destroyed. That's the only kind of
341
* object returned in node set not directly linked to the original
342
* xmlDoc document, see xmlXPathNodeSetDupNs.
345
libxml_xmlXPathDestructNsNode(void *cobj, void *desc ATTRIBUTE_UNUSED) {
347
fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj);
349
xmlXPathNodeSetFreeNs((xmlNsPtr) cobj);
353
libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
358
printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
365
case XPATH_XSLT_TREE: {
366
if ((obj->nodesetval == NULL) ||
367
(obj->nodesetval->nodeNr == 0) ||
368
(obj->nodesetval->nodeTab == NULL)) {
374
node = obj->nodesetval->nodeTab[0]->children;
375
while (node != NULL) {
379
ret = PyList_New(len);
380
node = obj->nodesetval->nodeTab[0]->children;
381
for (i = 0;i < len;i++) {
382
PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
387
* Return now, do not free the object passed down
392
if ((obj->nodesetval == NULL)
393
|| (obj->nodesetval->nodeNr == 0)) {
399
ret = PyList_New(obj->nodesetval->nodeNr);
400
for (i = 0; i < obj->nodesetval->nodeNr; i++) {
401
node = obj->nodesetval->nodeTab[i];
402
if (node->type == XML_NAMESPACE_DECL) {
404
PyCObject_FromVoidPtrAndDesc((void *) node,
406
libxml_xmlXPathDestructNsNode);
407
PyList_SetItem(ret, i, ns);
408
/* make sure the xmlNsPtr is not destroyed now */
409
obj->nodesetval->nodeTab[i] = NULL;
411
PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
417
ret = PyInt_FromLong((long) obj->boolval);
420
ret = PyFloat_FromDouble(obj->floatval);
423
ret = PyString_FromString((char *) obj->stringval);
428
PyObject *indexIntoNode;
431
node = libxml_xmlNodePtrWrap(obj->user);
432
indexIntoNode = PyInt_FromLong((long) obj->index);
434
tuple = PyTuple_New(2);
435
PyTuple_SetItem(tuple, 0, node);
436
PyTuple_SetItem(tuple, 1, indexIntoNode);
443
unsigned short bCollapsedRange;
445
bCollapsedRange = ( (obj->user2 == NULL) ||
446
((obj->user2 == obj->user) && (obj->index == obj->index2)) );
447
if ( bCollapsedRange ) {
449
PyObject *indexIntoNode;
453
list = PyList_New(1);
455
node = libxml_xmlNodePtrWrap(obj->user);
456
indexIntoNode = PyInt_FromLong((long) obj->index);
458
tuple = PyTuple_New(2);
459
PyTuple_SetItem(tuple, 0, node);
460
PyTuple_SetItem(tuple, 1, indexIntoNode);
462
PyList_SetItem(list, 0, tuple);
467
PyObject *indexIntoNode;
471
list = PyList_New(2);
473
node = libxml_xmlNodePtrWrap(obj->user);
474
indexIntoNode = PyInt_FromLong((long) obj->index);
476
tuple = PyTuple_New(2);
477
PyTuple_SetItem(tuple, 0, node);
478
PyTuple_SetItem(tuple, 1, indexIntoNode);
480
PyList_SetItem(list, 0, tuple);
482
node = libxml_xmlNodePtrWrap(obj->user2);
483
indexIntoNode = PyInt_FromLong((long) obj->index2);
485
tuple = PyTuple_New(2);
486
PyTuple_SetItem(tuple, 0, node);
487
PyTuple_SetItem(tuple, 1, indexIntoNode);
489
PyList_SetItem(list, 1, tuple);
495
case XPATH_LOCATIONSET:
497
xmlLocationSetPtr set;
500
if ( set && set->locNr > 0 ) {
504
list = PyList_New(set->locNr);
506
for (i=0; i<set->locNr; i++) {
507
xmlXPathObjectPtr setobj;
510
setobj = set->locTab[i]; /*xmlXPathObjectPtr setobj*/
512
pyobj = libxml_xmlXPathObjectPtrWrap(setobj);
513
/* xmlXPathFreeObject(setobj) is called */
514
set->locTab[i] = NULL;
516
PyList_SetItem(list, i, pyobj);
528
printf("Unable to convert XPath object type %d\n", obj->type);
533
xmlXPathFreeObject(obj);
538
libxml_xmlXPathObjectPtrConvert(PyObject * obj)
540
xmlXPathObjectPtr ret = NULL;
543
printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
550
ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
552
} else if PyInt_Check(obj) {
554
ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
557
} else if PyBool_Check (obj) {
559
if (obj == Py_True) {
560
ret = xmlXPathNewBoolean(1);
563
ret = xmlXPathNewBoolean(0);
566
} else if PyString_Check
570
str = xmlStrndup((const xmlChar *) PyString_AS_STRING(obj),
571
PyString_GET_SIZE(obj));
572
ret = xmlXPathWrapString(str);
573
} else if PyList_Check
580
set = xmlXPathNodeSetCreate(NULL);
582
for (i = 0; i < PyList_Size(obj); i++) {
583
node = PyList_GetItem(obj, i);
584
if ((node == NULL) || (node->ob_type == NULL))
588
if (PyCObject_Check(node)) {
590
printf("Got a CObject\n");
592
cur = PyxmlNode_Get(node);
593
} else if (PyInstance_Check(node)) {
594
PyInstanceObject *inst = (PyInstanceObject *) node;
595
PyObject *name = inst->in_class->cl_name;
599
char *type = PyString_AS_STRING(name);
602
if (!strcmp(type, "xmlNode")) {
604
PyObject_GetAttrString(node, (char *) "_o");
605
if (wrapper != NULL) {
606
cur = PyxmlNode_Get(wrapper);
612
printf("Unknown object in Python return list\n");
616
xmlXPathNodeSetAdd(set, cur);
619
ret = xmlXPathWrapNodeSet(set);
622
printf("Unable to convert Python Object to XPath");
630
libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
635
printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
643
PyCObject_FromVoidPtrAndDesc((void *) valid,
644
(char *) "xmlValidCtxtPtr", NULL);
650
libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
655
printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
662
PyCObject_FromVoidPtrAndDesc((void *) catal,
663
(char *) "xmlCatalogPtr", NULL);
668
libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
673
printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer);
675
if (buffer == NULL) {
680
PyCObject_FromVoidPtrAndDesc((void *) buffer,
681
(char *) "xmlOutputBufferPtr", NULL);
686
libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
691
printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer);
693
if (buffer == NULL) {
698
PyCObject_FromVoidPtrAndDesc((void *) buffer,
699
(char *) "xmlParserInputBufferPtr", NULL);
703
#ifdef LIBXML_REGEXP_ENABLED
705
libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
710
printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp);
712
if (regexp == NULL) {
717
PyCObject_FromVoidPtrAndDesc((void *) regexp,
718
(char *) "xmlRegexpPtr", NULL);
721
#endif /* LIBXML_REGEXP_ENABLED */
724
libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
729
printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader);
731
if (reader == NULL) {
736
PyCObject_FromVoidPtrAndDesc((void *) reader,
737
(char *) "xmlTextReaderPtr", NULL);
742
libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
747
printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator);
749
if (locator == NULL) {
754
PyCObject_FromVoidPtrAndDesc((void *) locator,
755
(char *) "xmlTextReaderLocatorPtr", NULL);
759
#ifdef LIBXML_SCHEMAS_ENABLED
761
libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
766
printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt);
773
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
774
(char *) "xmlRelaxNGPtr", NULL);
779
libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
784
printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt);
791
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
792
(char *) "xmlRelaxNGParserCtxtPtr", NULL);
796
libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
801
printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid);
808
PyCObject_FromVoidPtrAndDesc((void *) valid,
809
(char *) "xmlRelaxNGValidCtxtPtr", NULL);
814
libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)
819
printf("libxml_xmlSchemaPtrWrap: ctxt = %p\n", ctxt);
826
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
827
(char *) "xmlSchemaPtr", NULL);
832
libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)
837
printf("libxml_xmlSchemaParserCtxtPtrWrap: ctxt = %p\n", ctxt);
844
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
845
(char *) "xmlSchemaParserCtxtPtr", NULL);
851
libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
856
printf("libxml_xmlSchemaValidCtxtPtrWrap: valid = %p\n", valid);
864
PyCObject_FromVoidPtrAndDesc((void *) valid,
865
(char *) "xmlSchemaValidCtxtPtr", NULL);
869
#endif /* LIBXML_SCHEMAS_ENABLED */
872
libxml_xmlErrorPtrWrap(xmlErrorPtr error)
877
printf("libxml_xmlErrorPtrWrap: error = %p\n", error);
884
PyCObject_FromVoidPtrAndDesc((void *) error,
885
(char *) "xmlErrorPtr", NULL);