Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/lxml/includes/etree_defs.h
811 views
1
#ifndef HAS_ETREE_DEFS_H
2
#define HAS_ETREE_DEFS_H
3
4
/* quick check for Python/libxml2/libxslt devel setup */
5
#include "Python.h"
6
#ifndef PY_VERSION_HEX
7
# error the development package of Python (header files etc.) is not installed correctly
8
#else
9
# if PY_VERSION_HEX < 0x02070000 || PY_MAJOR_VERSION >= 3 && PY_VERSION_HEX < 0x03050000
10
# error this version of lxml requires Python 2.7, 3.5 or later
11
# endif
12
#endif
13
14
#include "libxml/xmlversion.h"
15
#ifndef LIBXML_VERSION
16
# error the development package of libxml2 (header files etc.) is not installed correctly
17
#else
18
#if LIBXML_VERSION < 20700
19
# error minimum required version of libxml2 is 2.7.0
20
#endif
21
#endif
22
23
#include "libxslt/xsltconfig.h"
24
#ifndef LIBXSLT_VERSION
25
# error the development package of libxslt (header files etc.) is not installed correctly
26
#else
27
#if LIBXSLT_VERSION < 10123
28
# error minimum required version of libxslt is 1.1.23
29
#endif
30
#endif
31
32
33
/* v_arg functions */
34
#define va_int(ap) va_arg(ap, int)
35
#define va_charptr(ap) va_arg(ap, char *)
36
37
#ifdef PYPY_VERSION
38
# define IS_PYPY 1
39
#else
40
# define IS_PYPY 0
41
#endif
42
43
#if PY_MAJOR_VERSION >= 3
44
# define IS_PYTHON2 0 /* prefer for special casing Python 2.x */
45
# define IS_PYTHON3 1 /* avoid */
46
#else
47
# define IS_PYTHON2 1
48
# define IS_PYTHON3 0
49
#endif
50
51
#if IS_PYTHON2
52
#ifndef LXML_UNICODE_STRINGS
53
#define LXML_UNICODE_STRINGS 0
54
#endif
55
#else
56
#undef LXML_UNICODE_STRINGS
57
#define LXML_UNICODE_STRINGS 1
58
#endif
59
60
#if !IS_PYPY
61
# define PyWeakref_LockObject(obj) (NULL)
62
#endif
63
64
/* Threading is not currently supported by PyPy */
65
#if IS_PYPY
66
# ifndef WITHOUT_THREADING
67
# define WITHOUT_THREADING
68
# endif
69
#endif
70
71
#if IS_PYPY
72
# undef PyFile_AsFile
73
# define PyFile_AsFile(o) (NULL)
74
# undef PyByteArray_Check
75
# define PyByteArray_Check(o) (0)
76
#elif !IS_PYTHON2
77
/* Python 3+ doesn't have PyFile_*() anymore */
78
# define PyFile_AsFile(o) (NULL)
79
#endif
80
81
#if PY_VERSION_HEX <= 0x03030000 && !(defined(CYTHON_PEP393_ENABLED) && CYTHON_PEP393_ENABLED)
82
#define PyUnicode_IS_READY(op) (0)
83
#define PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u)
84
#define PyUnicode_KIND(u) (sizeof(Py_UNICODE))
85
#define PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u))
86
#endif
87
88
#if IS_PYPY
89
# ifndef PyUnicode_FromFormat
90
# define PyUnicode_FromFormat PyString_FromFormat
91
# endif
92
# if !IS_PYTHON2 && !defined(PyBytes_FromFormat)
93
# ifdef PyString_FromFormat
94
# define PyBytes_FromFormat PyString_FromFormat
95
# else
96
#include <stdarg.h>
97
static PyObject* PyBytes_FromFormat(const char* format, ...) {
98
PyObject *string;
99
va_list vargs;
100
#ifdef HAVE_STDARG_PROTOTYPES
101
va_start(vargs, format);
102
#else
103
va_start(vargs);
104
#endif
105
string = PyUnicode_FromFormatV(format, vargs);
106
va_end(vargs);
107
if (string && PyUnicode_Check(string)) {
108
PyObject *bstring = PyUnicode_AsUTF8String(string);
109
Py_DECREF(string);
110
string = bstring;
111
}
112
if (string && !PyBytes_CheckExact(string)) {
113
Py_DECREF(string);
114
string = NULL;
115
PyErr_SetString(PyExc_TypeError, "String formatting and encoding failed to return bytes object");
116
}
117
return string;
118
}
119
# endif
120
# endif
121
#endif
122
123
/* PySlice_GetIndicesEx() has wrong signature in Py<=3.1 */
124
#if PY_VERSION_HEX >= 0x03020000
125
# define _lx_PySlice_GetIndicesEx(o, l, b, e, s, sl) PySlice_GetIndicesEx(o, l, b, e, s, sl)
126
#else
127
# define _lx_PySlice_GetIndicesEx(o, l, b, e, s, sl) PySlice_GetIndicesEx(((PySliceObject*)o), l, b, e, s, sl)
128
#endif
129
130
#ifdef WITHOUT_THREADING
131
# undef PyEval_SaveThread
132
# define PyEval_SaveThread() (NULL)
133
# undef PyEval_RestoreThread
134
# define PyEval_RestoreThread(state) if (state); else {}
135
# undef PyGILState_Ensure
136
# define PyGILState_Ensure() (PyGILState_UNLOCKED)
137
# undef PyGILState_Release
138
# define PyGILState_Release(state) if (state); else {}
139
# undef Py_UNBLOCK_THREADS
140
# define Py_UNBLOCK_THREADS _save = NULL;
141
# undef Py_BLOCK_THREADS
142
# define Py_BLOCK_THREADS if (_save); else {}
143
#endif
144
145
#ifdef WITHOUT_THREADING
146
# define ENABLE_THREADING 0
147
#else
148
# define ENABLE_THREADING 1
149
#endif
150
151
#if LIBXML_VERSION < 20704
152
/* FIXME: hack to make new error reporting compile in old libxml2 versions */
153
# define xmlStructuredErrorContext NULL
154
# define xmlXIncludeProcessTreeFlagsData(n,o,d) xmlXIncludeProcessTreeFlags(n,o)
155
#endif
156
157
/* schematron was added in libxml2 2.6.21 */
158
#ifdef LIBXML_SCHEMATRON_ENABLED
159
# define ENABLE_SCHEMATRON 1
160
#else
161
# define ENABLE_SCHEMATRON 0
162
# define XML_SCHEMATRON_OUT_QUIET 0
163
# define XML_SCHEMATRON_OUT_XML 0
164
# define XML_SCHEMATRON_OUT_ERROR 0
165
typedef void xmlSchematron;
166
typedef void xmlSchematronParserCtxt;
167
typedef void xmlSchematronValidCtxt;
168
# define xmlSchematronNewDocParserCtxt(doc) NULL
169
# define xmlSchematronNewParserCtxt(file) NULL
170
# define xmlSchematronParse(ctxt) NULL
171
# define xmlSchematronFreeParserCtxt(ctxt)
172
# define xmlSchematronFree(schema)
173
# define xmlSchematronNewValidCtxt(schema, options) NULL
174
# define xmlSchematronValidateDoc(ctxt, doc) 0
175
# define xmlSchematronFreeValidCtxt(ctxt)
176
# define xmlSchematronSetValidStructuredErrors(ctxt, errorfunc, data)
177
#endif
178
179
#if LIBXML_VERSION < 20708
180
# define HTML_PARSE_NODEFDTD 4
181
#endif
182
#if LIBXML_VERSION < 20900
183
# define XML_PARSE_BIG_LINES 4194304
184
#endif
185
186
#include "libxml/tree.h"
187
#ifndef LIBXML2_NEW_BUFFER
188
typedef xmlBuffer xmlBuf;
189
# define xmlBufContent(buf) xmlBufferContent(buf)
190
# define xmlBufUse(buf) xmlBufferLength(buf)
191
#endif
192
193
/* libexslt 1.1.25+ support EXSLT functions in XPath */
194
#if LIBXSLT_VERSION < 10125
195
#define exsltDateXpathCtxtRegister(ctxt, prefix)
196
#define exsltSetsXpathCtxtRegister(ctxt, prefix)
197
#define exsltMathXpathCtxtRegister(ctxt, prefix)
198
#define exsltStrXpathCtxtRegister(ctxt, prefix)
199
#endif
200
201
#define LXML_GET_XSLT_ENCODING(result_var, style) XSLT_GET_IMPORT_PTR(result_var, style, encoding)
202
203
/* work around MSDEV 6.0 */
204
#if (_MSC_VER == 1200) && (WINVER < 0x0500)
205
long _ftol( double ); //defined by VC6 C libs
206
long _ftol2( double dblSource ) { return _ftol( dblSource ); }
207
#endif
208
209
#ifdef __GNUC__
210
/* Test for GCC > 2.95 */
211
#if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
212
#define unlikely_condition(x) __builtin_expect((x), 0)
213
#else /* __GNUC__ > 2 ... */
214
#define unlikely_condition(x) (x)
215
#endif /* __GNUC__ > 2 ... */
216
#else /* __GNUC__ */
217
#define unlikely_condition(x) (x)
218
#endif /* __GNUC__ */
219
220
#ifndef Py_TYPE
221
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
222
#endif
223
224
#define PY_NEW(T) \
225
(((PyTypeObject*)(T))->tp_new( \
226
(PyTypeObject*)(T), __pyx_empty_tuple, NULL))
227
228
#define _fqtypename(o) ((Py_TYPE(o))->tp_name)
229
230
#define lxml_malloc(count, item_size) \
231
(unlikely_condition((size_t)(count) > (size_t) (PY_SSIZE_T_MAX / item_size)) ? NULL : \
232
(PyMem_Malloc((count) * item_size)))
233
234
#define lxml_realloc(mem, count, item_size) \
235
(unlikely_condition((size_t)(count) > (size_t) (PY_SSIZE_T_MAX / item_size)) ? NULL : \
236
(PyMem_Realloc(mem, (count) * item_size)))
237
238
#define lxml_free(mem) PyMem_Free(mem)
239
240
#if PY_MAJOR_VERSION < 3
241
#define _isString(obj) (PyString_CheckExact(obj) || \
242
PyUnicode_CheckExact(obj) || \
243
PyType_IsSubtype(Py_TYPE(obj), &PyBaseString_Type))
244
#else
245
/* builtin subtype type checks are almost as fast as exact checks in Py2.7+
246
* and Unicode is more common in Py3 */
247
#define _isString(obj) (PyUnicode_Check(obj) || PyBytes_Check(obj))
248
#endif
249
250
#define _isElement(c_node) \
251
(((c_node)->type == XML_ELEMENT_NODE) || \
252
((c_node)->type == XML_COMMENT_NODE) || \
253
((c_node)->type == XML_ENTITY_REF_NODE) || \
254
((c_node)->type == XML_PI_NODE))
255
256
#define _isElementOrXInclude(c_node) \
257
(_isElement(c_node) || \
258
((c_node)->type == XML_XINCLUDE_START) || \
259
((c_node)->type == XML_XINCLUDE_END))
260
261
#define _getNs(c_node) \
262
(((c_node)->ns == 0) ? 0 : ((c_node)->ns->href))
263
264
265
#include "string.h"
266
static void* lxml_unpack_xmldoc_capsule(PyObject* capsule, int* is_owned) {
267
xmlDoc *c_doc;
268
void *context;
269
*is_owned = 0;
270
if (unlikely_condition(!PyCapsule_IsValid(capsule, (const char*)"libxml2:xmlDoc"))) {
271
PyErr_SetString(
272
PyExc_TypeError,
273
"Not a valid capsule. The capsule argument must be a capsule object with name libxml2:xmlDoc");
274
return NULL;
275
}
276
c_doc = (xmlDoc*) PyCapsule_GetPointer(capsule, (const char*)"libxml2:xmlDoc");
277
if (unlikely_condition(!c_doc)) return NULL;
278
279
if (unlikely_condition(c_doc->type != XML_DOCUMENT_NODE && c_doc->type != XML_HTML_DOCUMENT_NODE)) {
280
PyErr_Format(
281
PyExc_ValueError,
282
"Illegal document provided: expected XML or HTML, found %d", (int)c_doc->type);
283
return NULL;
284
}
285
286
context = PyCapsule_GetContext(capsule);
287
if (unlikely_condition(!context && PyErr_Occurred())) return NULL;
288
if (context && strcmp((const char*) context, "destructor:xmlFreeDoc") == 0) {
289
/* take ownership by setting destructor to NULL */
290
if (PyCapsule_SetDestructor(capsule, NULL) == 0) {
291
/* ownership transferred => invalidate capsule by clearing its name */
292
if (unlikely_condition(PyCapsule_SetName(capsule, NULL))) {
293
/* this should never happen since everything above succeeded */
294
xmlFreeDoc(c_doc);
295
return NULL;
296
}
297
*is_owned = 1;
298
}
299
}
300
return c_doc;
301
}
302
303
/* Macro pair implementation of a depth first tree walker
304
*
305
* Calls the code block between the BEGIN and END macros for all elements
306
* below c_tree_top (exclusively), starting at c_node (inclusively iff
307
* 'inclusive' is 1). The _ELEMENT_ variants will only stop on nodes
308
* that match _isElement(), the normal variant will stop on every node
309
* except text nodes.
310
*
311
* To traverse the node and all of its children and siblings in Pyrex, call
312
* cdef xmlNode* some_node
313
* BEGIN_FOR_EACH_ELEMENT_FROM(some_node.parent, some_node, 1)
314
* # do something with some_node
315
* END_FOR_EACH_ELEMENT_FROM(some_node)
316
*
317
* To traverse only the children and siblings of a node, call
318
* cdef xmlNode* some_node
319
* BEGIN_FOR_EACH_ELEMENT_FROM(some_node.parent, some_node, 0)
320
* # do something with some_node
321
* END_FOR_EACH_ELEMENT_FROM(some_node)
322
*
323
* To traverse only the children, do:
324
* cdef xmlNode* some_node
325
* some_node = parent_node.children
326
* BEGIN_FOR_EACH_ELEMENT_FROM(parent_node, some_node, 1)
327
* # do something with some_node
328
* END_FOR_EACH_ELEMENT_FROM(some_node)
329
*
330
* NOTE: 'some_node' MUST be a plain 'xmlNode*' !
331
*
332
* NOTE: parent modification during the walk can divert the iterator, but
333
* should not segfault !
334
*/
335
336
#define _LX__ELEMENT_MATCH(c_node, only_elements) \
337
((only_elements) ? (_isElement(c_node)) : 1)
338
339
#define _LX__ADVANCE_TO_NEXT(c_node, only_elements) \
340
while ((c_node != 0) && (!_LX__ELEMENT_MATCH(c_node, only_elements))) \
341
c_node = c_node->next;
342
343
#define _LX__TRAVERSE_TO_NEXT(c_stop_node, c_node, only_elements) \
344
{ \
345
/* walk through children first */ \
346
xmlNode* _lx__next = c_node->children; \
347
if (_lx__next != 0) { \
348
if (c_node->type == XML_ENTITY_REF_NODE || c_node->type == XML_DTD_NODE) { \
349
_lx__next = 0; \
350
} else { \
351
_LX__ADVANCE_TO_NEXT(_lx__next, only_elements) \
352
} \
353
} \
354
if ((_lx__next == 0) && (c_node != c_stop_node)) { \
355
/* try siblings */ \
356
_lx__next = c_node->next; \
357
_LX__ADVANCE_TO_NEXT(_lx__next, only_elements) \
358
/* back off through parents */ \
359
while (_lx__next == 0) { \
360
c_node = c_node->parent; \
361
if (c_node == 0) \
362
break; \
363
if (c_node == c_stop_node) \
364
break; \
365
if ((only_elements) && !_isElement(c_node)) \
366
break; \
367
/* we already traversed the parents -> siblings */ \
368
_lx__next = c_node->next; \
369
_LX__ADVANCE_TO_NEXT(_lx__next, only_elements) \
370
} \
371
} \
372
c_node = _lx__next; \
373
}
374
375
#define _LX__BEGIN_FOR_EACH_FROM(c_tree_top, c_node, inclusive, only_elements) \
376
{ \
377
if (c_node != 0) { \
378
const xmlNode* _lx__tree_top = (c_tree_top); \
379
const int _lx__only_elements = (only_elements); \
380
/* make sure we start at an element */ \
381
if (!_LX__ELEMENT_MATCH(c_node, _lx__only_elements)) { \
382
/* we skip the node, so 'inclusive' is irrelevant */ \
383
if (c_node == _lx__tree_top) \
384
c_node = 0; /* nothing to traverse */ \
385
else { \
386
c_node = c_node->next; \
387
_LX__ADVANCE_TO_NEXT(c_node, _lx__only_elements) \
388
} \
389
} else if (! (inclusive)) { \
390
/* skip the first node */ \
391
_LX__TRAVERSE_TO_NEXT(_lx__tree_top, c_node, _lx__only_elements) \
392
} \
393
\
394
/* now run the user code on the elements we find */ \
395
while (c_node != 0) { \
396
/* here goes the code to be run for each element */
397
398
#define _LX__END_FOR_EACH_FROM(c_node) \
399
_LX__TRAVERSE_TO_NEXT(_lx__tree_top, c_node, _lx__only_elements) \
400
} \
401
} \
402
}
403
404
405
#define BEGIN_FOR_EACH_ELEMENT_FROM(c_tree_top, c_node, inclusive) \
406
_LX__BEGIN_FOR_EACH_FROM(c_tree_top, c_node, inclusive, 1)
407
408
#define END_FOR_EACH_ELEMENT_FROM(c_node) \
409
_LX__END_FOR_EACH_FROM(c_node)
410
411
#define BEGIN_FOR_EACH_FROM(c_tree_top, c_node, inclusive) \
412
_LX__BEGIN_FOR_EACH_FROM(c_tree_top, c_node, inclusive, 0)
413
414
#define END_FOR_EACH_FROM(c_node) \
415
_LX__END_FOR_EACH_FROM(c_node)
416
417
418
#endif /* HAS_ETREE_DEFS_H */
419
420