[Lxml-checkins] r49553 - in lxml/trunk: . doc src/lxml

scoder at codespeak.net scoder at codespeak.net
Sat Dec 8 16:06:46 CET 2007


Author: scoder
Date: Sat Dec  8 16:06:45 2007
New Revision: 49553

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/doc/extensions.txt
   lxml/trunk/src/lxml/extensions.pxi
Log:


Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Sat Dec  8 16:06:45 2007
@@ -18,6 +18,12 @@
 
 * Result document memory leak in target parser.
 
+Other changes
+-------------
+
+* Second argument to ``lxml.etree.Extension()`` helper is no longer
+  required, third argument is now a keyword-only argument ``ns``.
+
 
 2.0alpha5 (2007-11-24)
 ======================

Modified: lxml/trunk/doc/extensions.txt
==============================================================================
--- lxml/trunk/doc/extensions.txt	(original)
+++ lxml/trunk/doc/extensions.txt	Sat Dec  8 16:06:45 2007
@@ -217,20 +217,21 @@
 
   >>> ext_module = MyExt()
   >>> functions = ('function1', 'function2')
-  >>> extensions = etree.Extension( ext_module, functions, 'local-ns' )
+  >>> extensions = etree.Extension( ext_module, functions, ns='local-ns' )
 
   >>> e = etree.XPathEvaluator(doc, namespaces=namespaces, extensions=extensions)
   >>> print e.evaluate('l:function1(string(b))')
   1Haegar
 
-The second argument to ``Extension`` can either be be a sequence of names to
-select from the module, a dictionary that explicitly maps function names to
-their XPath alter-ego or ``None`` (explicitly passed) to take all available
-functions under their original name (if their name does not start with '_').
-
-The third argument takes a namespace URI or ``None`` (also if left out) for
-the default namespace.  The following examples will therefore all do the same
-thing::
+The optional second argument to ``Extension`` can either be be a
+sequence of names to select from the module, a dictionary that
+explicitly maps function names to their XPath alter-ego or ``None``
+(explicitly passed) to take all available functions under their
+original name (if their name does not start with '_').
+
+The additional ``ns`` keyword argument takes a namespace URI or
+``None`` (also if left out) for the default namespace.  The following
+examples will therefore all do the same thing::
 
   >>> functions = ('function1', 'function2', 'function3')
   >>> extensions = etree.Extension( ext_module, functions )
@@ -238,12 +239,12 @@
   >>> print e.evaluate('function1(function2(function3(string(b))))')
   123Haegar
 
-  >>> extensions = etree.Extension( ext_module, functions, None )
+  >>> extensions = etree.Extension( ext_module, functions, ns=None )
   >>> e = etree.XPathEvaluator(doc, extensions=extensions)
   >>> print e.evaluate('function1(function2(function3(string(b))))')
   123Haegar
 
-  >>> extensions = etree.Extension( ext_module, None )
+  >>> extensions = etree.Extension(ext_module)
   >>> e = etree.XPathEvaluator(doc, extensions=extensions)
   >>> print e.evaluate('function1(function2(function3(string(b))))')
   123Haegar
@@ -253,15 +254,15 @@
   ...     'function2' : 'function2',
   ...     'function3' : 'function3'
   ...     }
-  >>> extensions = etree.Extension( ext_module, functions )
+  >>> extensions = etree.Extension(ext_module, functions)
   >>> e = etree.XPathEvaluator(doc, extensions=extensions)
   >>> print e.evaluate('function1(function2(function3(string(b))))')
   123Haegar
 
 For convenience, you can also pass a sequence of extensions::
 
-  >>> extensions1 = etree.Extension( ext_module, None )
-  >>> extensions2 = etree.Extension( ext_module, None, 'local-ns' )
+  >>> extensions1 = etree.Extension(ext_module)
+  >>> extensions2 = etree.Extension(ext_module, ns='local-ns')
   >>> e = etree.XPathEvaluator(doc, extensions=[extensions1, extensions2],
   ...                          namespaces=namespaces)
   >>> print e.evaluate('function1(l:function2(function3(string(b))))')

Modified: lxml/trunk/src/lxml/extensions.pxi
==============================================================================
--- lxml/trunk/src/lxml/extensions.pxi	(original)
+++ lxml/trunk/src/lxml/extensions.pxi	Sat Dec  8 16:06:45 2007
@@ -323,8 +323,17 @@
                 #print "Holding document:", <int>element._doc._c_doc
                 self._temp_refs.add((<_Element>o)._doc)
 
+def Extension(module, function_mapping=None, *, ns=None):
+    """Build a dictionary of extension functions from the functions
+    defined in a module or the methods of an object.
+
+    As second argument, you can pass an additional mapping of
+    attribute names to XPath function names, or a list of function
+    names that should be taken.
 
-def Extension(module, function_mapping, ns=None):
+    The ``ns`` keyword argument accepts a namespace URI for the XPath
+    functions.
+    """
     functions = {}
     if python.PyDict_Check(function_mapping):
         for function_name, xpath_name in function_mapping.items():
@@ -332,16 +341,13 @@
                                   getattr(module, function_name))
     else:
         if function_mapping is None:
-            function_mapping = []
-            for name in dir(module):
-                if not name.startswith('_'):
-                    python.PyList_Append(function_mapping, name)
+            function_mapping = [ name for name in dir(module)
+                                 if not name.startswith('_') ]
         for function_name in function_mapping:
             python.PyDict_SetItem(functions, (ns, function_name),
                                   getattr(module, function_name))
     return functions
 
-
 ################################################################################
 # EXSLT regexp implementation
 


More information about the lxml-checkins mailing list