PyObject * libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) { PyObject *ret; #ifdef DEBUG printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj); #endif if (obj == NULL) { Py_INCREF(Py_None); return (Py_None); } switch (obj->type) { case XPATH_XSLT_TREE: /* TODO !!!! Allocation problems */ case XPATH_NODESET: if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0)) ret = PyList_New(0); else { int i; xmlNodePtr node; ret = PyList_New(obj->nodesetval->nodeNr); for (i = 0; i < obj->nodesetval->nodeNr; i++) { node = obj->nodesetval->nodeTab[i]; /* TODO: try to cast directly to the proper node type */ PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); } } break; case XPATH_BOOLEAN: ret = PyInt_FromLong((long) obj->boolval); break; case XPATH_NUMBER: ret = PyFloat_FromDouble(obj->floatval); break; case XPATH_STRING: ret = PyString_FromString((char *) obj->stringval); break; case XPATH_POINT: case XPATH_RANGE: case XPATH_LOCATIONSET: default: printf("Unable to convert XPath object type %d\n", obj->type); Py_INCREF(Py_None); ret = Py_None; } xmlXPathFreeObject(obj); return (ret); }
PyObject * libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) { PyObject *ret; #ifdef DEBUG printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj); #endif if (obj == NULL) { Py_INCREF(Py_None); return (Py_None); } switch (obj->type) { case XPATH_XSLT_TREE: { if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0) || (obj->nodesetval->nodeTab == NULL)) { ret = PyList_New(0); } else { int i, len = 0; xmlNodePtr node; node = obj->nodesetval->nodeTab[0]->children; while (node != NULL) { len++; node = node->next; } ret = PyList_New(len); node = obj->nodesetval->nodeTab[0]->children; for (i = 0;i < len;i++) { PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); node = node->next; } } /* * Return now, do not free the object passed down */ return (ret); } case XPATH_NODESET: if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0)) { ret = PyList_New(0); } else { int i; xmlNodePtr node; ret = PyList_New(obj->nodesetval->nodeNr); for (i = 0; i < obj->nodesetval->nodeNr; i++) { node = obj->nodesetval->nodeTab[i]; /* TODO: try to cast directly to the proper node type */ PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); } } break; case XPATH_BOOLEAN: ret = PyInt_FromLong((long) obj->boolval); break; case XPATH_NUMBER: ret = PyFloat_FromDouble(obj->floatval); break; case XPATH_STRING: ret = PyString_FromString((char *) obj->stringval); break; case XPATH_POINT: case XPATH_RANGE: case XPATH_LOCATIONSET: default: #ifdef DEBUG printf("Unable to convert XPath object type %d\n", obj->type); #endif Py_INCREF(Py_None); ret = Py_None; } xmlXPathFreeObject(obj); return (ret); }
PyObject * libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj) { PyObject *ret; #ifdef DEBUG printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj); #endif if (obj == NULL) { Py_INCREF(Py_None); return (Py_None); } switch (obj->type) { case XPATH_XSLT_TREE: { if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0) || (obj->nodesetval->nodeTab == NULL)) { ret = PyList_New(0); } else { int i, len = 0; xmlNodePtr node; node = obj->nodesetval->nodeTab[0]->children; while (node != NULL) { len++; node = node->next; } ret = PyList_New(len); node = obj->nodesetval->nodeTab[0]->children; for (i = 0; i < len; i++) { PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); node = node->next; } } /* * Return now, do not free the object passed down */ return (ret); } case XPATH_NODESET: if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0)) { ret = PyList_New(0); } else { int i; xmlNodePtr node; ret = PyList_New(obj->nodesetval->nodeNr); for (i = 0; i < obj->nodesetval->nodeNr; i++) { node = obj->nodesetval->nodeTab[i]; if (node->type == XML_NAMESPACE_DECL) { PyObject *ns = PyCapsule_New((void *) node, (char *) "xmlNsPtr", libxml_xmlXPathDestructNsNode); PyList_SetItem(ret, i, ns); /* make sure the xmlNsPtr is not destroyed now */ obj->nodesetval->nodeTab[i] = NULL; } else { PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node)); } } } break; case XPATH_BOOLEAN: ret = PY_IMPORT_INT((long) obj->boolval); break; case XPATH_NUMBER: ret = PyFloat_FromDouble(obj->floatval); break; case XPATH_STRING: ret = PY_IMPORT_STRING((char *) obj->stringval); break; case XPATH_POINT: { PyObject *node; PyObject *indexIntoNode; PyObject *tuple; node = libxml_xmlNodePtrWrap(obj->user); indexIntoNode = PY_IMPORT_INT((long) obj->index); tuple = PyTuple_New(2); PyTuple_SetItem(tuple, 0, node); PyTuple_SetItem(tuple, 1, indexIntoNode); ret = tuple; break; } case XPATH_RANGE: { unsigned short bCollapsedRange; bCollapsedRange = ( (obj->user2 == NULL) || ((obj->user2 == obj->user) && (obj->index == obj->index2)) ); if ( bCollapsedRange ) { PyObject *node; PyObject *indexIntoNode; PyObject *tuple; PyObject *list; list = PyList_New(1); node = libxml_xmlNodePtrWrap(obj->user); indexIntoNode = PY_IMPORT_INT((long) obj->index); tuple = PyTuple_New(2); PyTuple_SetItem(tuple, 0, node); PyTuple_SetItem(tuple, 1, indexIntoNode); PyList_SetItem(list, 0, tuple); ret = list; } else { PyObject *node; PyObject *indexIntoNode; PyObject *tuple; PyObject *list; list = PyList_New(2); node = libxml_xmlNodePtrWrap(obj->user); indexIntoNode = PY_IMPORT_INT((long) obj->index); tuple = PyTuple_New(2); PyTuple_SetItem(tuple, 0, node); PyTuple_SetItem(tuple, 1, indexIntoNode); PyList_SetItem(list, 0, tuple); node = libxml_xmlNodePtrWrap(obj->user2); indexIntoNode = PY_IMPORT_INT((long) obj->index2); tuple = PyTuple_New(2); PyTuple_SetItem(tuple, 0, node); PyTuple_SetItem(tuple, 1, indexIntoNode); PyList_SetItem(list, 1, tuple); ret = list; } break; } case XPATH_LOCATIONSET: { xmlLocationSetPtr set; set = obj->user; if ( set && set->locNr > 0 ) { int i; PyObject *list; list = PyList_New(set->locNr); for (i=0; i<set->locNr; i++) { xmlXPathObjectPtr setobj; PyObject *pyobj; setobj = set->locTab[i]; /*xmlXPathObjectPtr setobj*/ pyobj = libxml_xmlXPathObjectPtrWrap(setobj); /* xmlXPathFreeObject(setobj) is called */ set->locTab[i] = NULL; PyList_SetItem(list, i, pyobj); } set->locNr = 0; ret = list; } else { Py_INCREF(Py_None); ret = Py_None; } break; } default: #ifdef DEBUG printf("Unable to convert XPath object type %d\n", obj->type); #endif Py_INCREF(Py_None); ret = Py_None; } xmlXPathFreeObject(obj); return (ret); }