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 #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 <stdint.h>
25
26 #ifdef _WIN32
27
28 #include <windows.h>
29 #include <crtdbg.h>
30
31 /* Taken from info on MSDN site, as we may not have the Windows WDK/DDK headers */
32 typedef struct _IO_STATUS_BLOCK {
33 union {
34 NTSTATUS Status;
35 PVOID Pointer;
36 } DUMMYUNIONNAME;
37 ULONG_PTR Information;
38 } IO_STATUS_BLOCK;
39
40 typedef struct _FILE_ACCESS_INFORMATION {
41 ACCESS_MASK AccessFlags;
42 } FILE_ACCESS_INFORMATION;
43
44 typedef NTSTATUS (*t_NtQueryInformationFile) (HANDLE FileHandle,
45 IO_STATUS_BLOCK *IoStatusBlock,
46 PVOID FileInformation,
47 ULONG Length,
48 int FileInformationClass); /* this is an Enum */
49
50 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
51 /*
52 * This is the (empty) invalid parameter handler
53 * that is used for Visual C++ 2005 (and later) builds
54 * so that we can use this instead of the system automatically
55 * aborting the process.
56 *
57 * This is necessary as we use _get_oshandle() to check the validity
58 * of the file descriptors as we close them, so when an invalid file
59 * descriptor is passed into that function as we check on it, we get
60 * -1 as the result, instead of the gspawn helper program aborting.
61 *
62 * Please see http://msdn.microsoft.com/zh-tw/library/ks2530z6%28v=vs.80%29.aspx
63 * for an explanation on this.
64 */
65 void
myInvalidParameterHandler(const wchar_t * expression,const wchar_t * function,const wchar_t * file,unsigned int line,uintptr_t pReserved)66 myInvalidParameterHandler(const wchar_t *expression,
67 const wchar_t *function,
68 const wchar_t *file,
69 unsigned int line,
70 uintptr_t pReserved)
71 {
72 }
73 #endif
74 #else
75 #include <unistd.h>
76 #include <fcntl.h>
77 #endif
78
79 FILE *
libxml_PyFileGet(PyObject * f)80 libxml_PyFileGet(PyObject *f) {
81 FILE *res;
82 const char *mode;
83 int fd = PyObject_AsFileDescriptor(f);
84
85 #ifdef _WIN32
86 intptr_t w_fh = -1;
87 HMODULE hntdll = NULL;
88 IO_STATUS_BLOCK status_block;
89 FILE_ACCESS_INFORMATION ai;
90 t_NtQueryInformationFile NtQueryInformationFile;
91 BOOL is_read = FALSE;
92 BOOL is_write = FALSE;
93 BOOL is_append = FALSE;
94
95 #if (defined (_MSC_VER) && _MSC_VER >= 1400)
96 /* set up our empty invalid parameter handler */
97 _invalid_parameter_handler oldHandler, newHandler;
98 newHandler = myInvalidParameterHandler;
99 oldHandler = _set_invalid_parameter_handler(newHandler);
100
101 /* Disable the message box for assertions. */
102 _CrtSetReportMode(_CRT_ASSERT, 0);
103 #endif
104
105 w_fh = _get_osfhandle(fd);
106
107 if (w_fh == -1)
108 return(NULL);
109
110 hntdll = GetModuleHandleW(L"ntdll.dll");
111
112 if (hntdll == NULL)
113 return(NULL);
114 XML_IGNORE_FPTR_CAST_WARNINGS
115 NtQueryInformationFile = (t_NtQueryInformationFile)GetProcAddress(hntdll, "NtQueryInformationFile");
116 XML_POP_WARNINGS
117
118 if (NtQueryInformationFile != NULL &&
119 (NtQueryInformationFile((HANDLE)w_fh,
120 &status_block,
121 &ai,
122 sizeof(FILE_ACCESS_INFORMATION),
123 8) == 0)) /* 8 means "FileAccessInformation" */
124 {
125 if (ai.AccessFlags & FILE_READ_DATA)
126 is_read = TRUE;
127 if (ai.AccessFlags & FILE_WRITE_DATA)
128 is_write = TRUE;
129 if (ai.AccessFlags & FILE_APPEND_DATA)
130 is_append = TRUE;
131
132 if (is_write) {
133 if (is_read) {
134 if (is_append)
135 mode = "a+";
136 else
137 mode = "rw";
138 } else {
139 if (is_append)
140 mode = "a";
141 else
142 mode = "w";
143 }
144 } else {
145 if (is_append)
146 mode = "r+";
147 else
148 mode = "r";
149 }
150 }
151
152 FreeLibrary(hntdll);
153
154 if (!is_write && !is_read) /* also happens if we did not load or run NtQueryInformationFile() successfully */
155 return(NULL);
156 #else
157 int flags;
158
159 /*
160 * macOS returns O_RDWR for standard streams, but fails to write to
161 * stdout or stderr when opened with fdopen(dup_fd, "rw").
162 */
163 switch (fd) {
164 case STDIN_FILENO:
165 mode = "r";
166 break;
167 case STDOUT_FILENO:
168 case STDERR_FILENO:
169 mode = "w";
170 break;
171 default:
172 /*
173 * Get the flags on the fd to understand how it was opened
174 */
175 flags = fcntl(fd, F_GETFL, 0);
176 switch (flags & O_ACCMODE) {
177 case O_RDWR:
178 if (flags & O_APPEND)
179 mode = "a+";
180 else
181 mode = "rw";
182 break;
183 case O_RDONLY:
184 if (flags & O_APPEND)
185 mode = "r+";
186 else
187 mode = "r";
188 break;
189 case O_WRONLY:
190 if (flags & O_APPEND)
191 mode = "a";
192 else
193 mode = "w";
194 break;
195 default:
196 return(NULL);
197 }
198 }
199 #endif
200
201 /*
202 * the FILE struct gets a new fd, so that it can be closed
203 * independently of the file descriptor given. The risk though is
204 * lack of sync. So at the python level sync must be implemented
205 * before and after a conversion took place. No way around it
206 * in the Python3 infrastructure !
207 * The duplicated fd and FILE * will be released in the subsequent
208 * call to libxml_PyFileRelease() which must be generated accordingly
209 */
210 fd = dup(fd);
211 if (fd == -1)
212 return(NULL);
213 res = fdopen(fd, mode);
214 if (res == NULL) {
215 close(fd);
216 return(NULL);
217 }
218 return(res);
219 }
220
libxml_PyFileRelease(FILE * f)221 void libxml_PyFileRelease(FILE *f) {
222 if (f != NULL)
223 fclose(f);
224 }
225 #endif
226
227 PyObject *
libxml_intWrap(int val)228 libxml_intWrap(int val)
229 {
230 PyObject *ret;
231
232 #ifdef DEBUG
233 printf("libxml_intWrap: val = %d\n", val);
234 #endif
235 ret = PY_IMPORT_INT((long) val);
236 return (ret);
237 }
238
239 PyObject *
libxml_longWrap(long val)240 libxml_longWrap(long val)
241 {
242 PyObject *ret;
243
244 #ifdef DEBUG
245 printf("libxml_longWrap: val = %ld\n", val);
246 #endif
247 ret = PyLong_FromLong(val);
248 return (ret);
249 }
250
251 PyObject *
libxml_doubleWrap(double val)252 libxml_doubleWrap(double val)
253 {
254 PyObject *ret;
255
256 #ifdef DEBUG
257 printf("libxml_doubleWrap: val = %f\n", val);
258 #endif
259 ret = PyFloat_FromDouble((double) val);
260 return (ret);
261 }
262
263 PyObject *
libxml_charPtrWrap(char * str)264 libxml_charPtrWrap(char *str)
265 {
266 PyObject *ret;
267
268 #ifdef DEBUG
269 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
270 #endif
271 if (str == NULL) {
272 Py_INCREF(Py_None);
273 return (Py_None);
274 }
275 ret = PY_IMPORT_STRING(str);
276 xmlFree(str);
277 return (ret);
278 }
279
280 PyObject *
libxml_charPtrConstWrap(const char * str)281 libxml_charPtrConstWrap(const char *str)
282 {
283 PyObject *ret;
284
285 #ifdef DEBUG
286 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
287 #endif
288 if (str == NULL) {
289 Py_INCREF(Py_None);
290 return (Py_None);
291 }
292 ret = PY_IMPORT_STRING(str);
293 return (ret);
294 }
295
296 PyObject *
libxml_xmlCharPtrWrap(xmlChar * str)297 libxml_xmlCharPtrWrap(xmlChar * str)
298 {
299 PyObject *ret;
300
301 #ifdef DEBUG
302 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
303 #endif
304 if (str == NULL) {
305 Py_INCREF(Py_None);
306 return (Py_None);
307 }
308 ret = PY_IMPORT_STRING((char *) str);
309 xmlFree(str);
310 return (ret);
311 }
312
313 PyObject *
libxml_xmlCharPtrConstWrap(const xmlChar * str)314 libxml_xmlCharPtrConstWrap(const xmlChar * str)
315 {
316 PyObject *ret;
317
318 #ifdef DEBUG
319 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
320 #endif
321 if (str == NULL) {
322 Py_INCREF(Py_None);
323 return (Py_None);
324 }
325 ret = PY_IMPORT_STRING((char *) str);
326 return (ret);
327 }
328
329 PyObject *
libxml_constcharPtrWrap(const char * str)330 libxml_constcharPtrWrap(const char *str)
331 {
332 PyObject *ret;
333
334 #ifdef DEBUG
335 printf("libxml_xmlcharPtrWrap: str = %s\n", str);
336 #endif
337 if (str == NULL) {
338 Py_INCREF(Py_None);
339 return (Py_None);
340 }
341 ret = PY_IMPORT_STRING(str);
342 return (ret);
343 }
344
345 PyObject *
libxml_constxmlCharPtrWrap(const xmlChar * str)346 libxml_constxmlCharPtrWrap(const xmlChar * str)
347 {
348 PyObject *ret;
349
350 #ifdef DEBUG
351 printf("libxml_xmlCharPtrWrap: str = %s\n", str);
352 #endif
353 if (str == NULL) {
354 Py_INCREF(Py_None);
355 return (Py_None);
356 }
357 ret = PY_IMPORT_STRING((char *) str);
358 return (ret);
359 }
360
361 PyObject *
libxml_xmlDocPtrWrap(xmlDocPtr doc)362 libxml_xmlDocPtrWrap(xmlDocPtr doc)
363 {
364 PyObject *ret;
365
366 #ifdef DEBUG
367 printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
368 #endif
369 if (doc == NULL) {
370 Py_INCREF(Py_None);
371 return (Py_None);
372 }
373 /* TODO: look at deallocation */
374 ret = PyCapsule_New((void *) doc, (char *) "xmlDocPtr", NULL);
375 return (ret);
376 }
377
378 PyObject *
libxml_xmlNodePtrWrap(xmlNodePtr node)379 libxml_xmlNodePtrWrap(xmlNodePtr node)
380 {
381 PyObject *ret;
382
383 #ifdef DEBUG
384 printf("libxml_xmlNodePtrWrap: node = %p\n", node);
385 #endif
386 if (node == NULL) {
387 Py_INCREF(Py_None);
388 return (Py_None);
389 }
390 ret = PyCapsule_New((void *) node, (char *) "xmlNodePtr", NULL);
391 return (ret);
392 }
393
394 PyObject *
libxml_xmlURIPtrWrap(xmlURIPtr uri)395 libxml_xmlURIPtrWrap(xmlURIPtr uri)
396 {
397 PyObject *ret;
398
399 #ifdef DEBUG
400 printf("libxml_xmlURIPtrWrap: uri = %p\n", uri);
401 #endif
402 if (uri == NULL) {
403 Py_INCREF(Py_None);
404 return (Py_None);
405 }
406 ret = PyCapsule_New((void *) uri, (char *) "xmlURIPtr", NULL);
407 return (ret);
408 }
409
410 PyObject *
libxml_xmlNsPtrWrap(xmlNsPtr ns)411 libxml_xmlNsPtrWrap(xmlNsPtr ns)
412 {
413 PyObject *ret;
414
415 #ifdef DEBUG
416 printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
417 #endif
418 if (ns == NULL) {
419 Py_INCREF(Py_None);
420 return (Py_None);
421 }
422 ret = PyCapsule_New((void *) ns, (char *) "xmlNsPtr", NULL);
423 return (ret);
424 }
425
426 PyObject *
libxml_xmlAttrPtrWrap(xmlAttrPtr attr)427 libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
428 {
429 PyObject *ret;
430
431 #ifdef DEBUG
432 printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
433 #endif
434 if (attr == NULL) {
435 Py_INCREF(Py_None);
436 return (Py_None);
437 }
438 ret = PyCapsule_New((void *) attr, (char *) "xmlAttrPtr", NULL);
439 return (ret);
440 }
441
442 PyObject *
libxml_xmlAttributePtrWrap(xmlAttributePtr attr)443 libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
444 {
445 PyObject *ret;
446
447 #ifdef DEBUG
448 printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
449 #endif
450 if (attr == NULL) {
451 Py_INCREF(Py_None);
452 return (Py_None);
453 }
454 ret = PyCapsule_New((void *) attr, (char *) "xmlAttributePtr", NULL);
455 return (ret);
456 }
457
458 PyObject *
libxml_xmlElementPtrWrap(xmlElementPtr elem)459 libxml_xmlElementPtrWrap(xmlElementPtr elem)
460 {
461 PyObject *ret;
462
463 #ifdef DEBUG
464 printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
465 #endif
466 if (elem == NULL) {
467 Py_INCREF(Py_None);
468 return (Py_None);
469 }
470 ret = PyCapsule_New((void *) elem, (char *) "xmlElementPtr", NULL);
471 return (ret);
472 }
473
474 PyObject *
libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)475 libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
476 {
477 PyObject *ret;
478
479 #ifdef DEBUG
480 printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
481 #endif
482 if (ctxt == NULL) {
483 Py_INCREF(Py_None);
484 return (Py_None);
485 }
486 ret = PyCapsule_New((void *) ctxt, (char *) "xmlXPathContextPtr", NULL);
487 return (ret);
488 }
489
490 PyObject *
libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)491 libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
492 {
493 PyObject *ret;
494
495 #ifdef DEBUG
496 printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
497 #endif
498 if (ctxt == NULL) {
499 Py_INCREF(Py_None);
500 return (Py_None);
501 }
502 ret = PyCapsule_New((void *)ctxt, (char *)"xmlXPathParserContextPtr", NULL);
503 return (ret);
504 }
505
506 PyObject *
libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)507 libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
508 {
509 PyObject *ret;
510
511 #ifdef DEBUG
512 printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
513 #endif
514 if (ctxt == NULL) {
515 Py_INCREF(Py_None);
516 return (Py_None);
517 }
518
519 ret = PyCapsule_New((void *) ctxt, (char *) "xmlParserCtxtPtr", NULL);
520 return (ret);
521 }
522
523 /**
524 * libxml_xmlXPathDestructNsNode:
525 * cap: xmlNsPtr namespace node capsule object
526 *
527 * This function is called if and when a namespace node returned in
528 * an XPath node set is to be destroyed. That's the only kind of
529 * object returned in node set not directly linked to the original
530 * xmlDoc document, see xmlXPathNodeSetDupNs.
531 */
532 #if PY_VERSION_HEX < 0x02070000
533 static void
libxml_xmlXPathDestructNsNode(void * cap,void * desc ATTRIBUTE_UNUSED)534 libxml_xmlXPathDestructNsNode(void *cap, void *desc ATTRIBUTE_UNUSED)
535 #else
536 static void
537 libxml_xmlXPathDestructNsNode(PyObject *cap)
538 #endif
539 {
540 #ifdef DEBUG
541 fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cap);
542 #endif
543 #if PY_VERSION_HEX < 0x02070000
544 xmlXPathNodeSetFreeNs((xmlNsPtr) cap);
545 #else
546 xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
547 #endif
548 }
549
550 PyObject *
libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)551 libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
552 {
553 PyObject *ret;
554
555 #ifdef DEBUG
556 printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
557 #endif
558 if (obj == NULL) {
559 Py_INCREF(Py_None);
560 return (Py_None);
561 }
562 switch (obj->type) {
563 case XPATH_XSLT_TREE: {
564 if ((obj->nodesetval == NULL) ||
565 (obj->nodesetval->nodeNr == 0) ||
566 (obj->nodesetval->nodeTab == NULL)) {
567 ret = PyList_New(0);
568 } else {
569 int i, len = 0;
570 xmlNodePtr node;
571
572 node = obj->nodesetval->nodeTab[0]->children;
573 while (node != NULL) {
574 len++;
575 node = node->next;
576 }
577 ret = PyList_New(len);
578 node = obj->nodesetval->nodeTab[0]->children;
579 for (i = 0;i < len;i++) {
580 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
581 node = node->next;
582 }
583 }
584 /*
585 * Return now, do not free the object passed down
586 */
587 return (ret);
588 }
589 case XPATH_NODESET:
590 if ((obj->nodesetval == NULL)
591 || (obj->nodesetval->nodeNr == 0)) {
592 ret = PyList_New(0);
593 } else {
594 int i;
595 xmlNodePtr node;
596
597 ret = PyList_New(obj->nodesetval->nodeNr);
598 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
599 node = obj->nodesetval->nodeTab[i];
600 if (node->type == XML_NAMESPACE_DECL) {
601 PyObject *ns = PyCapsule_New((void *) node,
602 (char *) "xmlNsPtr",
603 libxml_xmlXPathDestructNsNode);
604 PyList_SetItem(ret, i, ns);
605 /* make sure the xmlNsPtr is not destroyed now */
606 obj->nodesetval->nodeTab[i] = NULL;
607 } else {
608 PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
609 }
610 }
611 }
612 break;
613 case XPATH_BOOLEAN:
614 ret = PY_IMPORT_INT((long) obj->boolval);
615 break;
616 case XPATH_NUMBER:
617 ret = PyFloat_FromDouble(obj->floatval);
618 break;
619 case XPATH_STRING:
620 ret = PY_IMPORT_STRING((char *) obj->stringval);
621 break;
622 #ifdef LIBXML_XPTR_LOCS_ENABLED
623 case XPATH_POINT:
624 {
625 PyObject *node;
626 PyObject *indexIntoNode;
627 PyObject *tuple;
628
629 node = libxml_xmlNodePtrWrap(obj->user);
630 indexIntoNode = PY_IMPORT_INT((long) obj->index);
631
632 tuple = PyTuple_New(2);
633 PyTuple_SetItem(tuple, 0, node);
634 PyTuple_SetItem(tuple, 1, indexIntoNode);
635
636 ret = tuple;
637 break;
638 }
639 case XPATH_RANGE:
640 {
641 unsigned short bCollapsedRange;
642
643 bCollapsedRange = ( (obj->user2 == NULL) ||
644 ((obj->user2 == obj->user) && (obj->index == obj->index2)) );
645 if ( bCollapsedRange ) {
646 PyObject *node;
647 PyObject *indexIntoNode;
648 PyObject *tuple;
649 PyObject *list;
650
651 list = PyList_New(1);
652
653 node = libxml_xmlNodePtrWrap(obj->user);
654 indexIntoNode = PY_IMPORT_INT((long) obj->index);
655
656 tuple = PyTuple_New(2);
657 PyTuple_SetItem(tuple, 0, node);
658 PyTuple_SetItem(tuple, 1, indexIntoNode);
659
660 PyList_SetItem(list, 0, tuple);
661
662 ret = list;
663 } else {
664 PyObject *node;
665 PyObject *indexIntoNode;
666 PyObject *tuple;
667 PyObject *list;
668
669 list = PyList_New(2);
670
671 node = libxml_xmlNodePtrWrap(obj->user);
672 indexIntoNode = PY_IMPORT_INT((long) obj->index);
673
674 tuple = PyTuple_New(2);
675 PyTuple_SetItem(tuple, 0, node);
676 PyTuple_SetItem(tuple, 1, indexIntoNode);
677
678 PyList_SetItem(list, 0, tuple);
679
680 node = libxml_xmlNodePtrWrap(obj->user2);
681 indexIntoNode = PY_IMPORT_INT((long) obj->index2);
682
683 tuple = PyTuple_New(2);
684 PyTuple_SetItem(tuple, 0, node);
685 PyTuple_SetItem(tuple, 1, indexIntoNode);
686
687 PyList_SetItem(list, 1, tuple);
688
689 ret = list;
690 }
691 break;
692 }
693 case XPATH_LOCATIONSET:
694 {
695 xmlLocationSetPtr set;
696
697 set = obj->user;
698 if ( set && set->locNr > 0 ) {
699 int i;
700 PyObject *list;
701
702 list = PyList_New(set->locNr);
703
704 for (i=0; i<set->locNr; i++) {
705 xmlXPathObjectPtr setobj;
706 PyObject *pyobj;
707
708 setobj = set->locTab[i]; /*xmlXPathObjectPtr setobj*/
709
710 pyobj = libxml_xmlXPathObjectPtrWrap(setobj);
711 /* xmlXPathFreeObject(setobj) is called */
712 set->locTab[i] = NULL;
713
714 PyList_SetItem(list, i, pyobj);
715 }
716 set->locNr = 0;
717 ret = list;
718 } else {
719 Py_INCREF(Py_None);
720 ret = Py_None;
721 }
722 break;
723 }
724 #endif /* LIBXML_XPTR_LOCS_ENABLED */
725 default:
726 #ifdef DEBUG
727 printf("Unable to convert XPath object type %d\n", obj->type);
728 #endif
729 Py_INCREF(Py_None);
730 ret = Py_None;
731 }
732 xmlXPathFreeObject(obj);
733 return (ret);
734 }
735
736 xmlXPathObjectPtr
libxml_xmlXPathObjectPtrConvert(PyObject * obj)737 libxml_xmlXPathObjectPtrConvert(PyObject *obj)
738 {
739 xmlXPathObjectPtr ret = NULL;
740
741 #ifdef DEBUG
742 printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
743 #endif
744 if (obj == NULL) {
745 return (NULL);
746 }
747 if (PyFloat_Check (obj)) {
748 ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
749 } else if (PyLong_Check(obj)) {
750 #ifdef PyLong_AS_LONG
751 ret = xmlXPathNewFloat((double) PyLong_AS_LONG(obj));
752 #else
753 ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
754 #endif
755 #ifdef PyBool_Check
756 } else if (PyBool_Check (obj)) {
757
758 if (obj == Py_True) {
759 ret = xmlXPathNewBoolean(1);
760 }
761 else {
762 ret = xmlXPathNewBoolean(0);
763 }
764 #endif
765 } else if (PyBytes_Check (obj)) {
766 xmlChar *str;
767
768 str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(obj),
769 PyBytes_GET_SIZE(obj));
770 ret = xmlXPathWrapString(str);
771 #ifdef PyUnicode_Check
772 } else if (PyUnicode_Check (obj)) {
773 #if PY_VERSION_HEX >= 0x03030000
774 xmlChar *str;
775 const char *tmp;
776 Py_ssize_t size;
777
778 /* tmp doesn't need to be deallocated */
779 tmp = PyUnicode_AsUTF8AndSize(obj, &size);
780 str = xmlStrndup((const xmlChar *) tmp, (int) size);
781 ret = xmlXPathWrapString(str);
782 #else
783 xmlChar *str = NULL;
784 PyObject *b;
785
786 b = PyUnicode_AsUTF8String(obj);
787 if (b != NULL) {
788 str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(b),
789 PyBytes_GET_SIZE(b));
790 Py_DECREF(b);
791 }
792 ret = xmlXPathWrapString(str);
793 #endif
794 #endif
795 } else if (PyList_Check (obj)) {
796 int i;
797 PyObject *node;
798 xmlNodePtr cur;
799 xmlNodeSetPtr set;
800
801 set = xmlXPathNodeSetCreate(NULL);
802
803 for (i = 0; i < PyList_Size(obj); i++) {
804 node = PyList_GetItem(obj, i);
805 if ((node == NULL) || (node->ob_type == NULL))
806 continue;
807
808 cur = NULL;
809 if (PyCapsule_CheckExact(node)) {
810 #ifdef DEBUG
811 printf("Got a Capsule\n");
812 #endif
813 cur = PyxmlNode_Get(node);
814 } else if ((PyObject_HasAttrString(node, (char *) "_o")) &&
815 (PyObject_HasAttrString(node, (char *) "get_doc"))) {
816 PyObject *wrapper;
817
818 wrapper = PyObject_GetAttrString(node, (char *) "_o");
819 if (wrapper != NULL)
820 cur = PyxmlNode_Get(wrapper);
821 } else {
822 #ifdef DEBUG
823 printf("Unknown object in Python return list\n");
824 #endif
825 }
826 if (cur != NULL) {
827 xmlXPathNodeSetAdd(set, cur);
828 }
829 }
830 ret = xmlXPathWrapNodeSet(set);
831 } else {
832 #ifdef DEBUG
833 printf("Unable to convert Python Object to XPath");
834 #endif
835 }
836 return (ret);
837 }
838
839 PyObject *
libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)840 libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
841 {
842 PyObject *ret;
843
844 #ifdef DEBUG
845 printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
846 #endif
847 if (valid == NULL) {
848 Py_INCREF(Py_None);
849 return (Py_None);
850 }
851
852 ret =
853 PyCapsule_New((void *) valid,
854 (char *) "xmlValidCtxtPtr", NULL);
855
856 return (ret);
857 }
858
859 PyObject *
libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)860 libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
861 {
862 PyObject *ret;
863
864 #ifdef DEBUG
865 printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
866 #endif
867 if (catal == NULL) {
868 Py_INCREF(Py_None);
869 return (Py_None);
870 }
871 ret =
872 PyCapsule_New((void *) catal,
873 (char *) "xmlCatalogPtr", NULL);
874 return (ret);
875 }
876
877 PyObject *
libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)878 libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
879 {
880 PyObject *ret;
881
882 #ifdef DEBUG
883 printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer);
884 #endif
885 if (buffer == NULL) {
886 Py_INCREF(Py_None);
887 return (Py_None);
888 }
889 ret =
890 PyCapsule_New((void *) buffer,
891 (char *) "xmlOutputBufferPtr", NULL);
892 return (ret);
893 }
894
895 PyObject *
libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)896 libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
897 {
898 PyObject *ret;
899
900 #ifdef DEBUG
901 printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer);
902 #endif
903 if (buffer == NULL) {
904 Py_INCREF(Py_None);
905 return (Py_None);
906 }
907 ret =
908 PyCapsule_New((void *) buffer,
909 (char *) "xmlParserInputBufferPtr", NULL);
910 return (ret);
911 }
912
913 #ifdef LIBXML_REGEXP_ENABLED
914 PyObject *
libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)915 libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
916 {
917 PyObject *ret;
918
919 #ifdef DEBUG
920 printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp);
921 #endif
922 if (regexp == NULL) {
923 Py_INCREF(Py_None);
924 return (Py_None);
925 }
926 ret =
927 PyCapsule_New((void *) regexp,
928 (char *) "xmlRegexpPtr", NULL);
929 return (ret);
930 }
931 #endif /* LIBXML_REGEXP_ENABLED */
932
933 #ifdef LIBXML_READER_ENABLED
934 PyObject *
libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)935 libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
936 {
937 PyObject *ret;
938
939 #ifdef DEBUG
940 printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader);
941 #endif
942 if (reader == NULL) {
943 Py_INCREF(Py_None);
944 return (Py_None);
945 }
946 ret =
947 PyCapsule_New((void *) reader,
948 (char *) "xmlTextReaderPtr", NULL);
949 return (ret);
950 }
951
952 PyObject *
libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)953 libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
954 {
955 PyObject *ret;
956
957 #ifdef DEBUG
958 printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator);
959 #endif
960 if (locator == NULL) {
961 Py_INCREF(Py_None);
962 return (Py_None);
963 }
964 ret =
965 PyCapsule_New((void *) locator,
966 (char *) "xmlTextReaderLocatorPtr", NULL);
967 return (ret);
968 }
969 #endif /* LIBXML_READER_ENABLED */
970
971 #ifdef LIBXML_SCHEMAS_ENABLED
972 PyObject *
libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)973 libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
974 {
975 PyObject *ret;
976
977 #ifdef DEBUG
978 printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt);
979 #endif
980 if (ctxt == NULL) {
981 Py_INCREF(Py_None);
982 return (Py_None);
983 }
984 ret =
985 PyCapsule_New((void *) ctxt,
986 (char *) "xmlRelaxNGPtr", NULL);
987 return (ret);
988 }
989
990 PyObject *
libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)991 libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
992 {
993 PyObject *ret;
994
995 #ifdef DEBUG
996 printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt);
997 #endif
998 if (ctxt == NULL) {
999 Py_INCREF(Py_None);
1000 return (Py_None);
1001 }
1002 ret =
1003 PyCapsule_New((void *) ctxt,
1004 (char *) "xmlRelaxNGParserCtxtPtr", NULL);
1005 return (ret);
1006 }
1007 PyObject *
libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)1008 libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
1009 {
1010 PyObject *ret;
1011
1012 #ifdef DEBUG
1013 printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid);
1014 #endif
1015 if (valid == NULL) {
1016 Py_INCREF(Py_None);
1017 return (Py_None);
1018 }
1019 ret =
1020 PyCapsule_New((void *) valid,
1021 (char *) "xmlRelaxNGValidCtxtPtr", NULL);
1022 return (ret);
1023 }
1024
1025 PyObject *
libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)1026 libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)
1027 {
1028 PyObject *ret;
1029
1030 #ifdef DEBUG
1031 printf("libxml_xmlSchemaPtrWrap: ctxt = %p\n", ctxt);
1032 #endif
1033 if (ctxt == NULL) {
1034 Py_INCREF(Py_None);
1035 return (Py_None);
1036 }
1037 ret =
1038 PyCapsule_New((void *) ctxt,
1039 (char *) "xmlSchemaPtr", NULL);
1040 return (ret);
1041 }
1042
1043 PyObject *
libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)1044 libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)
1045 {
1046 PyObject *ret;
1047
1048 #ifdef DEBUG
1049 printf("libxml_xmlSchemaParserCtxtPtrWrap: ctxt = %p\n", ctxt);
1050 #endif
1051 if (ctxt == NULL) {
1052 Py_INCREF(Py_None);
1053 return (Py_None);
1054 }
1055 ret =
1056 PyCapsule_New((void *) ctxt,
1057 (char *) "xmlSchemaParserCtxtPtr", NULL);
1058
1059 return (ret);
1060 }
1061
1062 PyObject *
libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)1063 libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
1064 {
1065 PyObject *ret;
1066
1067 #ifdef DEBUG
1068 printf("libxml_xmlSchemaValidCtxtPtrWrap: valid = %p\n", valid);
1069 #endif
1070 if (valid == NULL) {
1071 Py_INCREF(Py_None);
1072 return (Py_None);
1073 }
1074
1075 ret =
1076 PyCapsule_New((void *) valid,
1077 (char *) "xmlSchemaValidCtxtPtr", NULL);
1078
1079 return (ret);
1080 }
1081 #endif /* LIBXML_SCHEMAS_ENABLED */
1082
1083 PyObject *
libxml_xmlErrorPtrWrap(xmlErrorPtr error)1084 libxml_xmlErrorPtrWrap(xmlErrorPtr error)
1085 {
1086 PyObject *ret;
1087
1088 #ifdef DEBUG
1089 printf("libxml_xmlErrorPtrWrap: error = %p\n", error);
1090 #endif
1091 if (error == NULL) {
1092 Py_INCREF(Py_None);
1093 return (Py_None);
1094 }
1095 ret = PyCapsule_New((void *) error, (char *) "xmlErrorPtr", NULL);
1096 return (ret);
1097 }
1098