From 52b48f73eee2f4b173b5a0f52959d539c5c4440c Mon Sep 17 00:00:00 2001 From: Christopher Brannon Date: Mon, 19 Jul 2010 13:43:54 -0500 Subject: [PATCH] Fix XML parsing for Python 2.7. The xml.etree.ElementTree module changed between Python 2.6 and Python 2.7. The ForcedEncodingXMLTreeBuilder class that we use in bugzilla.py no longer works with Python 2.7. New members were added to its superclass, and these new members weren't being initialized by the custom __init__ method. After some research, I learned that we no longer need the ForcedEncodingXMLTreeBuilder class in Python 2.7, because ElementTree.XMLTreeBuilder does exactly what we want. See . --- bugz/bugzilla.py | 18 +++++++++++++++++- 1 files changed, 17 insertions(+), 1 deletions(-) diff --git a/bugz/bugzilla.py b/bugz/bugzilla.py index 0694983..ecbd271 100644 --- a/bugz/bugzilla.py +++ b/bugz/bugzilla.py @@ -7,6 +7,7 @@ import locale import mimetypes import os import re +import sys from cookielib import LWPCookieJar, CookieJar from cStringIO import StringIO @@ -84,6 +85,8 @@ def get_content_type(filename): # # Override the behaviour of elementtree and allow us to # force the encoding to utf-8 +# Not needed in Python 2.7, since ElementTree.XMLTreeBuilder uses the forced +# encoding. # class ForcedEncodingXMLTreeBuilder(ElementTree.XMLTreeBuilder): @@ -442,8 +445,21 @@ class Bugz: resp = self.opener.open(req) fd = StringIO(resp.read()) + # workaround for ill-defined XML templates in bugzilla 2.20.2 - parser = ForcedEncodingXMLTreeBuilder(encoding = 'utf-8') + (major_version, minor_version) = \ + (sys.version_info[0], sys.version_info[1]) + if major_version > 2 or \ + (major_version == 2 and minor_version >= 7): + # If this is 2.7 or greater, then XMLTreeBuilder + # does what we want. + parser_class = ElementTree.XMLParser + else: + # Running under Python 2.6, so we need to use our + # subclass of XMLTreeBuilder instead. + parser_class = ForcedEncodingXMLTreeBuilder + parser = parser_class(encoding = 'utf-8') + etree = ElementTree.parse(fd, parser) bug = etree.find('.//bug') if bug and bug.attrib.has_key('error'): -- 1.7.1.1