[Lxml-checkins] r47665 - in lxml/trunk: doc src/lxml src/lxml/tests
scoder at codespeak.net
scoder at codespeak.net
Sun Oct 21 09:23:56 CEST 2007
Author: scoder
Date: Sun Oct 21 09:23:56 2007
New Revision: 47665
Modified:
lxml/trunk/doc/parsing.txt
lxml/trunk/src/lxml/docloader.pxi
lxml/trunk/src/lxml/etree.pyx
lxml/trunk/src/lxml/objectify.pyx
lxml/trunk/src/lxml/parser.pxi
lxml/trunk/src/lxml/tests/test_elementtree.py
lxml/trunk/src/lxml/tests/test_etree.py
lxml/trunk/src/lxml/tests/test_htmlparser.py
Log:
use keyword-only arguments in API
Modified: lxml/trunk/doc/parsing.txt
==============================================================================
--- lxml/trunk/doc/parsing.txt (original)
+++ lxml/trunk/doc/parsing.txt Sun Oct 21 09:23:56 2007
@@ -495,7 +495,7 @@
>>> etree.tostring(root)
'<test>  +  </test>'
- >>> etree.tostring(root, 'UTF-8', xml_declaration=False)
+ >>> etree.tostring(root, encoding='UTF-8', xml_declaration=False)
'<test> \xef\xa3\x91 + \xef\xa3\x92 </test>'
As an extension, lxml.etree has a new ``tounicode()`` function that you can
Modified: lxml/trunk/src/lxml/docloader.pxi
==============================================================================
--- lxml/trunk/src/lxml/docloader.pxi (original)
+++ lxml/trunk/src/lxml/docloader.pxi Sun Oct 21 09:23:56 2007
@@ -33,7 +33,7 @@
doc_ref._type = PARSER_DATA_EMPTY
return doc_ref
- def resolve_string(self, string, context, base_url=None):
+ def resolve_string(self, string, context, *, base_url=None):
"""Return a parsable string as input document.
Pass data string and context as parameters.
Modified: lxml/trunk/src/lxml/etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/etree.pyx (original)
+++ lxml/trunk/src/lxml/etree.pyx Sun Oct 21 09:23:56 2007
@@ -1383,7 +1383,7 @@
return self._doc._parser
return None
- def write(self, file, encoding=None, method="xml",
+ def write(self, file, *, encoding=None, method="xml",
pretty_print=False, xml_declaration=None):
"""Write the tree to a file or file-like object.
@@ -2061,7 +2061,7 @@
"""
return _makeSubElement(_parent, _tag, None, None, attrib, nsmap, _extra)
-def ElementTree(_Element element=None, file=None, _BaseParser parser=None):
+def ElementTree(_Element element=None, *, file=None, _BaseParser parser=None):
"""ElementTree wrapper class.
"""
cdef xmlNode* c_next
@@ -2084,7 +2084,7 @@
return _elementTreeFactory(doc, element)
-def HTML(text, _BaseParser parser=None, base_url=None):
+def HTML(text, _BaseParser parser=None, *, base_url=None):
"""Parses an HTML document from a string constant. This function can be used
to embed "HTML literals" in Python code.
@@ -2106,7 +2106,7 @@
except _TargetParserResult, result_container:
return result_container.result
-def XML(text, _BaseParser parser=None, base_url=None):
+def XML(text, _BaseParser parser=None, *, base_url=None):
"""Parses an XML document from a string constant. This function can be used
to embed "XML literals" in Python code, like in
@@ -2130,7 +2130,7 @@
except _TargetParserResult, result_container:
return result_container.result
-def fromstring(text, _BaseParser parser=None, base_url=None):
+def fromstring(text, _BaseParser parser=None, *, base_url=None):
"""Parses an XML document from a string.
To override the default parser with a different parser you can pass it to
@@ -2168,13 +2168,13 @@
"""
return isinstance(element, _Element)
-def dump(_Element elem not None, pretty_print=True):
+def dump(_Element elem not None, *, pretty_print=True):
"""Writes an element tree or element structure to sys.stdout. This function
should be used for debugging only.
"""
_dumpToFile(sys.stdout, elem._c_node, pretty_print)
-def tostring(element_or_tree, encoding=None, method="xml",
+def tostring(element_or_tree, *, encoding=None, method="xml",
xml_declaration=None, pretty_print=False):
"""Serialize an element to an encoded string representation of its XML
tree.
@@ -2217,7 +2217,7 @@
"""
return [tostring(element_or_tree, *args, **kwargs)]
-def tounicode(element_or_tree, method="xml", pretty_print=False):
+def tounicode(element_or_tree, *, method="xml", pretty_print=False):
"""Serialize an element to the Python unicode representation of its XML
tree.
Modified: lxml/trunk/src/lxml/objectify.pyx
==============================================================================
--- lxml/trunk/src/lxml/objectify.pyx (original)
+++ lxml/trunk/src/lxml/objectify.pyx Sun Oct 21 09:23:56 2007
@@ -1043,7 +1043,7 @@
cdef object _namespace
cdef object _nsmap
cdef bint _annotate
- def __init__(self, namespace=None, nsmap=None, annotate=True,
+ def __init__(self, *, namespace=None, nsmap=None, annotate=True,
makeelement=None):
if nsmap is None:
nsmap = _DEFAULT_NSMAP
@@ -1301,7 +1301,7 @@
pass
return None
-def pyannotate(element_or_tree, ignore_old=False, ignore_xsi=False,
+def pyannotate(element_or_tree, *, ignore_old=False, ignore_xsi=False,
empty_pytype=None):
"""Recursively annotates the elements of an XML tree with 'pytype'
attributes.
@@ -1322,7 +1322,7 @@
element = cetree.rootNodeOrRaise(element_or_tree)
_annotate(element, 0, 1, ignore_xsi, ignore_old, None, empty_pytype)
-def xsiannotate(element_or_tree, ignore_old=False, ignore_pytype=False,
+def xsiannotate(element_or_tree, *, ignore_old=False, ignore_pytype=False,
empty_type=None):
"""Recursively annotates the elements of an XML tree with 'xsi:type'
attributes.
@@ -1348,7 +1348,7 @@
element = cetree.rootNodeOrRaise(element_or_tree)
_annotate(element, 1, 0, ignore_old, ignore_pytype, empty_type, None)
-def annotate(element_or_tree, ignore_old=True, ignore_xsi=False,
+def annotate(element_or_tree, *, ignore_old=True, ignore_xsi=False,
empty_pytype=None, empty_type=None, annotate_xsi=0,
annotate_pytype=1):
"""Recursively annotates the elements of an XML tree with 'xsi:type'
@@ -1536,7 +1536,7 @@
tree.xmlSetNsProp(c_node, c_ns, "nil", "true")
tree.END_FOR_EACH_ELEMENT_FROM(c_node)
-def deannotate(element_or_tree, pytype=True, xsi=True):
+def deannotate(element_or_tree, *, pytype=True, xsi=True):
"""Recursively de-annotate the elements of an XML tree by removing 'pytype'
and/or 'type' attributes.
@@ -1640,7 +1640,7 @@
E = ElementMaker()
-def Element(_tag, attrib=None, nsmap=None, _pytype=None, **_attributes):
+def Element(_tag, attrib=None, nsmap=None, *, _pytype=None, **_attributes):
"""Objectify specific version of the lxml.etree Element() factory that
always creates a structural (tree) element.
@@ -1657,7 +1657,7 @@
_attributes[PYTYPE_ATTRIBUTE] = _pytype
return _makeElement(_tag, None, _attributes, nsmap)
-def DataElement(_value, attrib=None, nsmap=None, _pytype=None, _xsi=None,
+def DataElement(_value, attrib=None, nsmap=None, *, _pytype=None, _xsi=None,
**_attributes):
"""Create a new element from a Python value and XML attributes taken from
keyword arguments or a dictionary passed as second argument.
Modified: lxml/trunk/src/lxml/parser.pxi
==============================================================================
--- lxml/trunk/src/lxml/parser.pxi (original)
+++ lxml/trunk/src/lxml/parser.pxi Sun Oct 21 09:23:56 2007
@@ -1037,7 +1037,7 @@
not harmful, it is more efficient to use separate parsers. This does not
apply to the default parser.
"""
- def __init__(self, attribute_defaults=False, dtd_validation=False,
+ def __init__(self, *, attribute_defaults=False, dtd_validation=False,
load_dtd=False, no_network=True, ns_clean=False,
recover=False, remove_blank_text=False, compact=True,
resolve_entities=True, remove_comments=False,
@@ -1076,7 +1076,7 @@
This parser has ``remove_comments`` and ``remove_pis`` enabled by default
and thus ignores comments and processing instructions.
"""
- def __init__(self, attribute_defaults=False, dtd_validation=False,
+ def __init__(self, *, attribute_defaults=False, dtd_validation=False,
load_dtd=False, no_network=True, ns_clean=False,
recover=False, remove_blank_text=False, compact=True,
resolve_entities=True, remove_comments=True,
Modified: lxml/trunk/src/lxml/tests/test_elementtree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_elementtree.py (original)
+++ lxml/trunk/src/lxml/tests/test_elementtree.py Sun Oct 21 09:23:56 2007
@@ -43,13 +43,13 @@
tree = ElementTree(element)
self.buildNodes(element, 10, 3)
f = open(self.getTestFilePath('testdump.xml'), 'w')
- tree.write(f, 'UTF-8')
+ tree.write(f, encoding='UTF-8')
f.close()
f = open(self.getTestFilePath('testdump.xml'), 'r')
tree = ElementTree(file=f)
f.close()
f = open(self.getTestFilePath('testdump2.xml'), 'w')
- tree.write(f, 'UTF-8')
+ tree.write(f, encoding='UTF-8')
f.close()
f = open(self.getTestFilePath('testdump.xml'), 'r')
data1 = f.read()
@@ -2358,7 +2358,7 @@
f = StringIO()
tree = ElementTree(element=a)
- tree.write(f, 'utf-8')
+ tree.write(f, encoding='utf-8')
self.assertEquals(u'<a>Søk på nettet</a>'.encode('UTF-8'),
f.getvalue())
@@ -2389,7 +2389,7 @@
f = StringIO()
tree = ElementTree(element=a)
- tree.write(f, 'iso-8859-1')
+ tree.write(f, encoding='iso-8859-1')
result = f.getvalue()
declaration = "<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>"
self.assertEncodingDeclaration(result,'iso-8859-1')
@@ -2460,7 +2460,7 @@
a = Element('a')
a.text = u'Søk på nettet'
self.assertEquals(u'<a>Søk på nettet</a>'.encode('UTF-8'),
- tostring(a, 'utf-8'))
+ tostring(a, encoding='utf-8'))
def test_encoding_tostring_unknown(self):
Element = self.etree.Element
@@ -2468,7 +2468,8 @@
a = Element('a')
a.text = u'Søk på nettet'
- self.assertRaises(LookupError, tostring, a, 'Invalid Encoding')
+ self.assertRaises(LookupError, tostring, a,
+ encoding='Invalid Encoding')
def test_encoding_tostring_sub(self):
Element = self.etree.Element
@@ -2479,7 +2480,7 @@
b = SubElement(a, 'b')
b.text = u'Søk på nettet'
self.assertEquals(u'<b>Søk på nettet</b>'.encode('UTF-8'),
- tostring(b, 'utf-8'))
+ tostring(b, encoding='utf-8'))
def test_encoding_tostring_sub_tail(self):
Element = self.etree.Element
@@ -2491,7 +2492,7 @@
b.text = u'Søk på nettet'
b.tail = u'Søk'
self.assertEquals(u'<b>Søk på nettet</b>Søk'.encode('UTF-8'),
- tostring(b, 'utf-8'))
+ tostring(b, encoding='utf-8'))
def test_encoding_tostring_default_encoding(self):
Element = self.etree.Element
@@ -2919,7 +2920,7 @@
try:
f = open(filename, 'wb')
tree = ElementTree(element=element)
- tree.write(f, encoding)
+ tree.write(f, encoding=encoding)
f.close()
f = open(filename, 'rb')
data = f.read()
Modified: lxml/trunk/src/lxml/tests/test_etree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_etree.py (original)
+++ lxml/trunk/src/lxml/tests/test_etree.py Sun Oct 21 09:23:56 2007
@@ -1777,7 +1777,7 @@
b = SubElement(a, 'b')
c = SubElement(a, 'c')
- result = unicode(tostring(a, 'UTF-16'), 'UTF-16')
+ result = unicode(tostring(a, encoding='UTF-16'), 'UTF-16')
self.assertEquals('<a><b></b><c></c></a>',
canonicalize(result))
Modified: lxml/trunk/src/lxml/tests/test_htmlparser.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_htmlparser.py (original)
+++ lxml/trunk/src/lxml/tests/test_htmlparser.py Sun Oct 21 09:23:56 2007
@@ -16,6 +16,11 @@
etree = etree
html_str = "<html><head><title>test</title></head><body><h1>page title</h1></body></html>"
+ html_str_pretty = """\
+<html>
+<head><title>test</title></head>
+<body><h1>page title</h1></body>
+</html>"""
broken_html_str = "<html><head><title>test<body><h1>page title</h3></p></html>"
uhtml_str = u"<html><head><title>test á\uF8D2</title></head><body><h1>page á\uF8D2 title</h1></body></html>"
@@ -29,9 +34,14 @@
def test_module_HTML_unicode(self):
element = self.etree.HTML(self.uhtml_str)
- self.assertEqual(unicode(self.etree.tostring(element, 'UTF8'), 'UTF8'),
+ self.assertEqual(unicode(self.etree.tostring(element, encoding='UTF8'), 'UTF8'),
unicode(self.uhtml_str.encode('UTF8'), 'UTF8'))
+ def test_module_HTML_pretty_print(self):
+ element = self.etree.HTML(self.html_str)
+ self.assertEqual(self.etree.tostring(element, method="html", pretty_print=True),
+ self.html_str_pretty)
+
def test_module_parse_html_error(self):
parser = self.etree.HTMLParser(recover=False)
parse = self.etree.parse
@@ -202,14 +212,14 @@
parser = self.etree.HTMLParser()
f = SillyFileLike(self.html_str)
tree = self.etree.parse(f, parser)
- html = self.etree.tostring(tree.getroot(), 'UTF-8')
+ html = self.etree.tostring(tree.getroot(), encoding='UTF-8')
self.assertEqual(html, self.html_str)
## def test_module_parse_html_filelike_unicode(self):
## parser = self.etree.HTMLParser()
## f = SillyFileLike(self.uhtml_str)
## tree = self.etree.parse(f, parser)
-## html = self.etree.tostring(tree.getroot(), 'UTF-8')
+## html = self.etree.tostring(tree.getroot(), encoding='UTF-8')
## self.assertEqual(unicode(html, 'UTF-8'), self.uhtml_str)
def test_html_file_error(self):
More information about the lxml-checkins
mailing list