| 1 |
============================== |
| 2 |
The public C-API of lxml.etree |
| 3 |
============================== |
| 4 |
|
| 5 |
As of version 1.1, lxml.etree provides a public C-API. This allows external |
| 6 |
C extensions to efficiently access public functions and classes of lxml, |
| 7 |
without going through the Python API. |
| 8 |
|
| 9 |
The API is described in the file `etreepublic.pxd`_, which is directly |
| 10 |
c-importable by extension modules implemented in Cython_. |
| 11 |
|
| 12 |
.. _`etreepublic.pxd`: https://github.com/lxml/lxml/blob/master/src/lxml/includes/etreepublic.pxd |
| 13 |
.. _Cython: https://cython.org |
| 14 |
|
| 15 |
.. contents:: |
| 16 |
.. |
| 17 |
1 Passing generated trees through Python |
| 18 |
2 Writing external modules in Cython |
| 19 |
3 Writing external modules in C |
| 20 |
|
| 21 |
|
| 22 |
Passing generated trees through Python |
| 23 |
-------------------------------------- |
| 24 |
|
| 25 |
This is the simplest way to integrate with lxml. It does not require |
| 26 |
any C-level integration but uses a Python function to wrap an externally |
| 27 |
generated libxml2 document in lxml. |
| 28 |
|
| 29 |
The external module that creates the libxml2 tree must pack the document |
| 30 |
pointer into a `PyCapsule <https://docs.python.org/3/c-api/capsule.html>`_ |
| 31 |
object. This can then be passed into lxml with the function |
| 32 |
``lxml.etree.adopt_external_document()``. It also takes an optional lxml |
| 33 |
parser instance to associate with the document, in order to configure the |
| 34 |
Element class lookup, relative URL lookups, etc. |
| 35 |
|
| 36 |
See the `API reference <api/lxml.etree-module.html#adopt_external_document>`_ |
| 37 |
for further details. |
| 38 |
|
| 39 |
The same functionality is available as part of the public C-API in form |
| 40 |
of the C function ``adoptExternalDocument()``. |
| 41 |
|
| 42 |
|
| 43 |
Writing external modules in Cython |
| 44 |
---------------------------------- |
| 45 |
|
| 46 |
This is the easiest way of extending lxml at the C level. A Cython_ |
| 47 |
module should start like this:: |
| 48 |
|
| 49 |
# My Cython extension |
| 50 |
|
| 51 |
# directive pointing compiler to lxml header files; |
| 52 |
# use ``aliases={"LXML_PACKAGE_DIR": lxml.__path__}`` |
| 53 |
# argument to cythonize in setup.py to dynamically |
| 54 |
# determine dir at compile time |
| 55 |
# distutils: include_dirs = LXML_PACKAGE_DIR |
| 56 |
|
| 57 |
# import the public functions and classes of lxml.etree |
| 58 |
cimport lxml.includes.etreepublic as cetree |
| 59 |
|
| 60 |
# import the lxml.etree module in Python |
| 61 |
cdef object etree |
| 62 |
from lxml import etree |
| 63 |
|
| 64 |
# initialize the access to the C-API of lxml.etree |
| 65 |
cetree.import_lxml__etree() |
| 66 |
|
| 67 |
From this line on, you can access all public functions of lxml.etree |
| 68 |
from the ``cetree`` namespace like this:: |
| 69 |
|
| 70 |
# build a tag name from namespace and element name |
| 71 |
py_tag = cetree.namespacedNameFromNsName("http://some/url", "myelement") |
| 72 |
|
| 73 |
Public lxml classes are easily subclassed. For example, to implement |
| 74 |
and set a new default element class, you can write Cython code like |
| 75 |
the following:: |
| 76 |
|
| 77 |
from lxml.includes.etreepublic cimport ElementBase |
| 78 |
cdef class NewElementClass(ElementBase): |
| 79 |
def set_value(self, myval): |
| 80 |
self.set("my_attribute", myval) |
| 81 |
|
| 82 |
etree.set_element_class_lookup( |
| 83 |
etree.ElementDefaultClassLookup(element=NewElementClass)) |
| 84 |
|
| 85 |
|
| 86 |
Writing external modules in C |
| 87 |
----------------------------- |
| 88 |
|
| 89 |
If you really feel like it, you can also interface with lxml.etree straight |
| 90 |
from C code. All you have to do is include the header file for the public |
| 91 |
API, import the ``lxml.etree`` module and then call the import function: |
| 92 |
|
| 93 |
.. sourcecode:: c |
| 94 |
|
| 95 |
/* My C extension */ |
| 96 |
|
| 97 |
/* common includes */ |
| 98 |
#include "Python.h" |
| 99 |
#include "stdio.h" |
| 100 |
#include "string.h" |
| 101 |
#include "stdarg.h" |
| 102 |
#include "libxml/xmlversion.h" |
| 103 |
#include "libxml/encoding.h" |
| 104 |
#include "libxml/hash.h" |
| 105 |
#include "libxml/tree.h" |
| 106 |
#include "libxml/xmlIO.h" |
| 107 |
#include "libxml/xmlsave.h" |
| 108 |
#include "libxml/globals.h" |
| 109 |
#include "libxml/xmlstring.h" |
| 110 |
|
| 111 |
/* lxml.etree specific includes */ |
| 112 |
#include "lxml-version.h" |
| 113 |
#include "etree_defs.h" |
| 114 |
#include "etree.h" |
| 115 |
|
| 116 |
/* setup code */ |
| 117 |
import_lxml__etree() |
| 118 |
|
| 119 |
Note that including ``etree.h`` does not automatically include the |
| 120 |
header files it requires. Note also that the above list of common |
| 121 |
includes may not be sufficient. |