You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
15 KiB

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