| 1 |
============================== |
| 2 |
How to read the source of lxml |
| 3 |
============================== |
| 4 |
|
| 5 |
:Author: |
| 6 |
Stefan Behnel |
| 7 |
|
| 8 |
.. meta:: |
| 9 |
:description: How to read and work on the source code of lxml |
| 10 |
:keywords: lxml, XML, Cython, source code, develop, comprehend, understand |
| 11 |
|
| 12 |
This document describes how to read the source code of lxml_ and how |
| 13 |
to start working on it. You might also be interested in the companion |
| 14 |
document that describes `how to build lxml from sources`_. |
| 15 |
|
| 16 |
.. _lxml: https://lxml.de/ |
| 17 |
.. _`how to build lxml from sources`: build.html |
| 18 |
.. _`ReStructured Text`: http://docutils.sourceforge.net/rst.html |
| 19 |
.. _epydoc: http://epydoc.sourceforge.net/ |
| 20 |
.. _docutils: http://docutils.sourceforge.net/ |
| 21 |
.. _`C-level API`: capi.html |
| 22 |
|
| 23 |
.. contents:: |
| 24 |
.. |
| 25 |
1 What is Cython? |
| 26 |
2 Where to start? |
| 27 |
2.1 Concepts |
| 28 |
2.2 The documentation |
| 29 |
3 lxml.etree |
| 30 |
4 lxml.objectify |
| 31 |
5 lxml.html |
| 32 |
|
| 33 |
|
| 34 |
What is Cython? |
| 35 |
=============== |
| 36 |
|
| 37 |
.. _Cython: http://cython.org/ |
| 38 |
.. _Pyrex: http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ |
| 39 |
|
| 40 |
Cython_ is the language that lxml is written in. It is a very |
| 41 |
Python-like language that was specifically designed for writing Python |
| 42 |
extension modules. |
| 43 |
|
| 44 |
The reason why Cython (or actually its predecessor Pyrex_ at the time) |
| 45 |
was chosen as an implementation language for lxml, is that it makes it |
| 46 |
very easy to interface with both the Python world and external C code. |
| 47 |
Cython generates all the necessary glue code for the Python API, |
| 48 |
including Python types, calling conventions and reference counting. |
| 49 |
On the other side of the table, calling into C code is not more than |
| 50 |
declaring the signature of the function and maybe some variables as |
| 51 |
being C types, pointers or structs, and then calling it. The rest of |
| 52 |
the code is just plain Python code. |
| 53 |
|
| 54 |
The Cython language is so close to Python that the Cython compiler can |
| 55 |
actually compile many, many Python programs to C without major |
| 56 |
modifications. But the real speed gains of a C compilation come from |
| 57 |
type annotations that were added to the language and that allow Cython |
| 58 |
to generate very efficient C code. |
| 59 |
|
| 60 |
Even if you are not familiar with Cython, you should keep in mind that |
| 61 |
a slow implementation of a feature is better than none. So, if you |
| 62 |
want to contribute and have an idea what code you want to write, feel |
| 63 |
free to start with a pure Python implementation. Chances are, if you |
| 64 |
get the change officially accepted and integrated, others will take |
| 65 |
the time to optimise it so that it runs fast in Cython. |
| 66 |
|
| 67 |
|
| 68 |
Where to start? |
| 69 |
=============== |
| 70 |
|
| 71 |
First of all, read `how to build lxml from sources`_ to learn how to |
| 72 |
retrieve the source code from the GitHub repository and how to |
| 73 |
build it. The source code lives in the subdirectory ``src`` of the |
| 74 |
checkout. |
| 75 |
|
| 76 |
The main extension modules in lxml are ``lxml.etree`` and |
| 77 |
``lxml.objectify``. All main modules have the file extension |
| 78 |
``.pyx``, which shows the descent from Pyrex. As usual in Python, |
| 79 |
the main files start with a short description and a couple of imports. |
| 80 |
Cython distinguishes between the run-time ``import`` statement (as |
| 81 |
known from Python) and the compile-time ``cimport`` statement, which |
| 82 |
imports C declarations, either from external libraries or from other |
| 83 |
Cython modules. |
| 84 |
|
| 85 |
|
| 86 |
Concepts |
| 87 |
-------- |
| 88 |
|
| 89 |
lxml's tree API is based on proxy objects. That means, every Element |
| 90 |
object (or rather ``_Element`` object) is a proxy for a libxml2 node |
| 91 |
structure. The class declaration is (mainly):: |
| 92 |
|
| 93 |
cdef class _Element: |
| 94 |
cdef _Document _doc |
| 95 |
cdef xmlNode* _c_node |
| 96 |
|
| 97 |
It is a naming convention that C variables and C level class members |
| 98 |
that are passed into libxml2 start with a prefixed ``c_`` (commonly |
| 99 |
libxml2 struct pointers), and that C level class members are prefixed |
| 100 |
with an underscore. So you will often see names like ``c_doc`` for an |
| 101 |
``xmlDoc*`` variable (or ``c_node`` for an ``xmlNode*``), or the above |
| 102 |
``_c_node`` for a class member that points to an ``xmlNode`` struct |
| 103 |
(or ``_c_doc`` for an ``xmlDoc*``). |
| 104 |
|
| 105 |
It is important to know that every proxy in lxml has a factory |
| 106 |
function that properly sets up C level members. Proxy objects must |
| 107 |
*never* be instantiated outside of that factory. For example, to |
| 108 |
instantiate an _Element object or its subclasses, you must always call |
| 109 |
its factory function:: |
| 110 |
|
| 111 |
cdef xmlNode* c_node |
| 112 |
cdef _Document doc |
| 113 |
cdef _Element element |
| 114 |
... |
| 115 |
element = _elementFactory(doc, c_node) |
| 116 |
|
| 117 |
A good place to see how this factory is used are the Element methods |
| 118 |
``getparent()``, ``getnext()`` and ``getprevious()``. |
| 119 |
|
| 120 |
|
| 121 |
The documentation |
| 122 |
----------------- |
| 123 |
|
| 124 |
An important part of lxml is the documentation that lives in the |
| 125 |
``doc`` directory. It describes a large part of the API and comprises |
| 126 |
a lot of example code in the form of doctests. |
| 127 |
|
| 128 |
The documentation is written in the `ReStructured Text`_ format, a |
| 129 |
very powerful text markup language that looks almost like plain text. |
| 130 |
It is part of the docutils_ package. |
| 131 |
|
| 132 |
The project web site of lxml_ is completely generated from these text |
| 133 |
documents. Even the side menu is just collected from the table of |
| 134 |
contents that the ReST processor writes into each HTML page. |
| 135 |
Obviously, we use lxml for this. |
| 136 |
|
| 137 |
The easiest way to generate the HTML pages is by calling:: |
| 138 |
|
| 139 |
make html |
| 140 |
|
| 141 |
This will call the script ``doc/mkhtml.py`` to run the ReST processor |
| 142 |
on the files. After generating an HTML page, the script parses it back |
| 143 |
in to build the side menu, and injects the complete menu into each |
| 144 |
page at the very end. |
| 145 |
|
| 146 |
Running the ``make`` command will also generate the API documentation |
| 147 |
if you have epydoc_ installed. The epydoc package will import and |
| 148 |
introspect the extension modules and also introspect and parse the |
| 149 |
Python modules of lxml. The aggregated information will then be |
| 150 |
written out into an HTML documentation site. |
| 151 |
|
| 152 |
|
| 153 |
lxml.etree |
| 154 |
========== |
| 155 |
|
| 156 |
The main module, ``lxml.etree``, is in the file `lxml.etree.pyx |
| 157 |
<https://github.com/lxml/lxml/blob/master/src/lxml/etree.pyx>`_. It |
| 158 |
implements the main functions and types of the ElementTree API, as |
| 159 |
well as all the factory functions for proxies. It is the best place |
| 160 |
to start if you want to find out how a specific feature is |
| 161 |
implemented. |
| 162 |
|
| 163 |
At the very end of the file, it contains a series of ``include`` |
| 164 |
statements that merge the rest of the implementation into the |
| 165 |
generated C code. Yes, you read right: no importing, no source file |
| 166 |
namespacing, just plain good old include and a huge C code result of |
| 167 |
more than 100,000 lines that we throw right into the C compiler. |
| 168 |
|
| 169 |
The main include files are: |
| 170 |
|
| 171 |
apihelpers.pxi |
| 172 |
Private C helper functions. Except for the factory functions, |
| 173 |
most of the little functions that are used all over the place are |
| 174 |
defined here. This includes things like reading out the text |
| 175 |
content of a libxml2 tree node, checking input from the API level, |
| 176 |
creating a new Element node or handling attribute values. If you |
| 177 |
want to work on the lxml code, you should keep these functions in |
| 178 |
the back of your head, as they will definitely make your life |
| 179 |
easier. |
| 180 |
|
| 181 |
classlookup.pxi |
| 182 |
Element class lookup mechanisms. The main API and engines for |
| 183 |
those who want to define custom Element classes and inject them |
| 184 |
into lxml. |
| 185 |
|
| 186 |
docloader.pxi |
| 187 |
Support for custom document loaders. Base class and registry for |
| 188 |
custom document resolvers. |
| 189 |
|
| 190 |
extensions.pxi |
| 191 |
Infrastructure for extension functions in XPath/XSLT, including |
| 192 |
XPath value conversion and function registration. |
| 193 |
|
| 194 |
iterparse.pxi |
| 195 |
Incremental XML parsing. An iterator class that builds iterparse |
| 196 |
events while parsing. |
| 197 |
|
| 198 |
nsclasses.pxi |
| 199 |
Namespace implementation and registry. The registry and engine |
| 200 |
for Element classes that use the ElementNamespaceClassLookup |
| 201 |
scheme. |
| 202 |
|
| 203 |
parser.pxi |
| 204 |
Parsers for XML and HTML. This is the main parser engine. It's |
| 205 |
the reason why you can parse a document from various sources in |
| 206 |
two lines of Python code. It's definitely not the right place to |
| 207 |
start reading lxml's source code. |
| 208 |
|
| 209 |
parsertarget.pxi |
| 210 |
An ElementTree compatible parser target implementation based on |
| 211 |
the SAX2 interface of libxml2. |
| 212 |
|
| 213 |
proxy.pxi |
| 214 |
Very low-level functions for memory allocation/deallocation |
| 215 |
and Element proxy handling. Ignoring this for the beginning |
| 216 |
will save your head from exploding. |
| 217 |
|
| 218 |
public-api.pxi |
| 219 |
The set of C functions that are exported to other extension |
| 220 |
modules at the C level. For example, ``lxml.objectify`` makes use |
| 221 |
of these. See the `C-level API` documentation. |
| 222 |
|
| 223 |
readonlytree.pxi |
| 224 |
A separate read-only implementation of the Element API. This is |
| 225 |
used in places where non-intrusive access to a tree is required, |
| 226 |
such as the ``PythonElementClassLookup`` or XSLT extension |
| 227 |
elements. |
| 228 |
|
| 229 |
saxparser.pxi |
| 230 |
SAX-like parser interfaces as known from ElementTree's TreeBuilder. |
| 231 |
|
| 232 |
serializer.pxi |
| 233 |
XML output functions. Basically everything that creates byte |
| 234 |
sequences from XML trees. |
| 235 |
|
| 236 |
xinclude.pxi |
| 237 |
XInclude support. |
| 238 |
|
| 239 |
xmlerror.pxi |
| 240 |
Error log handling. All error messages that libxml2 generates |
| 241 |
internally walk through the code in this file to end up in lxml's |
| 242 |
Python level error logs. |
| 243 |
|
| 244 |
At the end of the file, you will find a long list of named error |
| 245 |
codes. It is generated from the libxml2 HTML documentation (using |
| 246 |
lxml, of course). See the script ``update-error-constants.py`` |
| 247 |
for this. |
| 248 |
|
| 249 |
xmlid.pxi |
| 250 |
XMLID and IDDict, a dictionary-like way to find Elements by their |
| 251 |
XML-ID attribute. |
| 252 |
|
| 253 |
xpath.pxi |
| 254 |
XPath evaluators. |
| 255 |
|
| 256 |
xslt.pxi |
| 257 |
XSL transformations, including the ``XSLT`` class, document lookup |
| 258 |
handling and access control. |
| 259 |
|
| 260 |
The different schema languages (DTD, RelaxNG, XML Schema and |
| 261 |
Schematron) are implemented in the following include files: |
| 262 |
|
| 263 |
* dtd.pxi |
| 264 |
* relaxng.pxi |
| 265 |
* schematron.pxi |
| 266 |
* xmlschema.pxi |
| 267 |
|
| 268 |
|
| 269 |
Python modules |
| 270 |
============== |
| 271 |
|
| 272 |
The ``lxml`` package also contains a number of pure Python modules: |
| 273 |
|
| 274 |
builder.py |
| 275 |
The E-factory and the ElementBuilder class. These provide a |
| 276 |
simple interface to XML tree generation. |
| 277 |
|
| 278 |
cssselect.py |
| 279 |
A CSS selector implementation based on XPath. The main class is |
| 280 |
called ``CSSSelector``. |
| 281 |
|
| 282 |
doctestcompare.py |
| 283 |
A relaxed comparison scheme for XML/HTML markup in doctest. |
| 284 |
|
| 285 |
ElementInclude.py |
| 286 |
XInclude-like document inclusion, compatible with ElementTree. |
| 287 |
|
| 288 |
_elementpath.py |
| 289 |
XPath-like path language, compatible with ElementTree. |
| 290 |
|
| 291 |
sax.py |
| 292 |
SAX2 compatible interfaces to copy lxml trees from/to SAX compatible |
| 293 |
tools. |
| 294 |
|
| 295 |
usedoctest.py |
| 296 |
Wrapper module for ``doctestcompare.py`` that simplifies its usage |
| 297 |
from inside a doctest. |
| 298 |
|
| 299 |
|
| 300 |
lxml.objectify |
| 301 |
============== |
| 302 |
|
| 303 |
A Cython implemented extension module that uses the public C-API of |
| 304 |
lxml.etree. It provides a Python object-like interface to XML trees. |
| 305 |
The implementation resides in the file `lxml.objectify.pyx |
| 306 |
<https://github.com/lxml/lxml/blob/master/src/lxml/objectify.pyx>`_. |
| 307 |
|
| 308 |
|
| 309 |
lxml.html |
| 310 |
========= |
| 311 |
|
| 312 |
A specialised toolkit for HTML handling, based on lxml.etree. This is |
| 313 |
implemented in pure Python. |