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