[merge] python 2.5 fixes

Martin Pool mbp at canonical.com
Mon Sep 25 14:33:37 BST 2006


On 25 Sep 2006, James Henstridge <james at jamesh.id.au> wrote:
> On 22/09/06, Martin Pool <mbp at canonical.com> wrote:
> >Here's an updated patch including some (much) earlier work.
> >
> >This also fixes a missing import of TestSkipped.
> >
> >I'm +1 on merging Marien's ElementTree fixes too -- my python2.5 install
> >doesn't have celementtree.
> 
> The ElementTree module was renamed to xml.etree.ElementTree when
> merged into Python 2.5.  Are you sure it wasn't available?

Well, this is cElementTree, but that's apparently under
xml.etree.cElementTree too.  But I think without his fix we would still
have had trouble on 2.4.

This patch makes bzr look in the new location.

It also cleans up some code that tries to access elementtree's
ElementTree class, as opposed othte one from cElementTree.  As far as I
can see they're the same thing - is there some reason why we can't just
import the name once?  Anyhow, with this change it passes tests on both
2.4 and 2.5.


=== modified file 'bzrlib/xml_serializer.py'
--- bzrlib/xml_serializer.py	2006-09-22 00:51:36 +0000
+++ bzrlib/xml_serializer.py	2006-09-25 13:23:54 +0000
@@ -25,17 +25,21 @@
 from bzrlib.trace import mutter, warning
 
 try:
-    from cElementTree import (ElementTree, SubElement, Element,
-                              XMLTreeBuilder, fromstring, tostring)
-    import elementtree
+    try:
+        # it's in this package in python2.5
+        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
+            XMLTreeBuilder, fromstring, tostring)
+    except ImportError:
+        from cElementTree import (ElementTree, SubElement, Element,
+                                  XMLTreeBuilder, fromstring, tostring)
     ParseError = SyntaxError
 except ImportError:
     mutter('WARNING: using slower ElementTree; consider installing cElementTree'
            " and make sure it's on your PYTHONPATH")
+    # this copy is shipped with bzr
     from util.elementtree.ElementTree import (ElementTree, SubElement,
                                               Element, XMLTreeBuilder,
                                               fromstring, tostring)
-    import util.elementtree as elementtree
     from xml.parsers.expat import ExpatError as ParseError
 
 from bzrlib import errors
@@ -103,9 +107,9 @@
     try:
         if encoding:
             try:
-                text = elementtree.ElementTree._encode(text, encoding)
+                text = ElementTree._encode(text, encoding)
             except UnicodeError:
-                return elementtree.ElementTree._encode_entity(text)
+                return ElementTree._encode_entity(text)
         if replace is None:
             return escape_re.sub(_escape_replace, text)
         else:
@@ -116,9 +120,9 @@
             text = replace(text, ">", "&gt;")
             return text
     except (TypeError, AttributeError):
-        elementtree.ElementTree._raise_serialization_error(text)
+        ElementTree._raise_serialization_error(text)
 
-elementtree.ElementTree._escape_attrib = _escape_attrib
+ElementTree._escape_attrib = _escape_attrib
 
 escape_cdata_re = re.compile("[&<>]")
 escape_cdata_map = {
@@ -134,9 +138,9 @@
     try:
         if encoding:
             try:
-                text = elementtree.ElementTree._encode(text, encoding)
+                text = ElementTree._encode(text, encoding)
             except UnicodeError:
-                return elementtree.ElementTree._encode_entity(text)
+                return ElementTree._encode_entity(text)
         if replace is None:
             return escape_cdata_re.sub(_escape_cdata_replace, text)
         else:
@@ -145,6 +149,6 @@
             text = replace(text, ">", "&gt;")
             return text
     except (TypeError, AttributeError):
-        elementtree.ElementTree._raise_serialization_error(text)
+        ElementTree._raise_serialization_error(text)
 
-elementtree.ElementTree._escape_cdata = _escape_cdata
+ElementTree._escape_cdata = _escape_cdata


-- 
Martin




More information about the bazaar mailing list