[Lxml-checkins] r43227 - in lxml/trunk: . src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Fri May 11 18:54:16 CEST 2007


Author: scoder
Date: Fri May 11 18:54:14 2007
New Revision: 43227

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/src/lxml/etree.pyx
   lxml/trunk/src/lxml/tests/test_elementtree.py
Log:
clear() method on Element.attrib

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Fri May 11 18:54:14 2007
@@ -38,6 +38,10 @@
 Bugs fixed
 ----------
 
+* More ET compatible behaviour when writing out XML declarations or not
+
+* ``Element.attrib`` was missing ``clear()`` method
+
 * More robust error handling in ``iterparse()``
 
 * Documents lost their top-level PIs and comments on serialisation

Modified: lxml/trunk/src/lxml/etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/etree.pyx	(original)
+++ lxml/trunk/src/lxml/etree.pyx	Fri May 11 18:54:14 2007
@@ -1467,6 +1467,12 @@
             _delAttribute(self._element, key)
             return result
 
+    def clear(self):
+        cdef xmlNode* c_node
+        c_node = self._element._c_node
+        while c_node.properties is not NULL:
+            tree.xmlRemoveProp(c_node.properties)
+
     # ACCESSORS
     def __repr__(self):
         return repr(dict( _attributeIteratorFactory(self._element, 3) ))
@@ -1882,17 +1888,15 @@
     """
     cdef int write_declaration
     cdef int c_pretty_print
-    if encoding is None:
-        encoding = 'ASCII'
-    else:
-        encoding = encoding.upper()
     c_pretty_print = bool(pretty_print)
     if xml_declaration is None:
         # by default, write an XML declaration only for non-standard encodings
-        write_declaration = encoding not in \
+        write_declaration = encoding is not None and encoding.upper() not in \
                             ('ASCII', 'UTF-8', 'UTF8', 'US-ASCII')
     else:
         write_declaration = bool(xml_declaration)
+    if encoding is None:
+        encoding = 'ASCII'
 
     if isinstance(element_or_tree, _Element):
         return _tostring(<_Element>element_or_tree,

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	Fri May 11 18:54:14 2007
@@ -290,6 +290,27 @@
         self.assertEquals(None, root.get('three'))
         self.assertEquals('foo', root.get('three', 'foo'))
 
+    def test_attrib_clear(self):
+        XML = self.etree.XML
+        
+        root = XML('<doc one="One" two="Two"/>')
+        self.assertEquals('One', root.get('one'))
+        self.assertEquals('Two', root.get('two'))
+        root.attrib.clear()
+        self.assertEquals(None, root.get('one'))
+        self.assertEquals(None, root.get('two'))
+
+    def test_attrib_set_clear(self):
+        Element = self.etree.Element
+        
+        root = Element("root", one="One")
+        root.set("two", "Two")
+        self.assertEquals('One', root.get('one'))
+        self.assertEquals('Two', root.get('two'))
+        root.attrib.clear()
+        self.assertEquals(None, root.get('one'))
+        self.assertEquals(None, root.get('two'))
+
     def test_attribute_update_dict(self):
         XML = self.etree.XML
         


More information about the lxml-checkins mailing list