Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Adjust XML parse error types for 2.6 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ayust committed Oct 7, 2014
1 parent d7f2672 commit 700f38e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
11 changes: 9 additions & 2 deletions evelink/api.py
Expand Up @@ -14,6 +14,13 @@

_log = logging.getLogger('evelink.api')

# Python 2.6's ElementTree raises xml.parsers.expat.ExpatError instead
# of ElementTree.ParseError
_xml_error = getattr(ElementTree, 'ParseError', None)
if _xml_error is None:
import xml.parsers.expat
_xml_error = xml.parsers.expat.ExpatError

# Allows zlib.decompress to decompress gzip-compressed strings as well.
# From zlib.h header file, not documented in Python.
ZLIB_DECODE_AUTO = 32 + zlib.MAX_WBITS
Expand Down Expand Up @@ -254,7 +261,7 @@ def get(self, path, params=None):

try:
tree = ElementTree.fromstring(response)
except ElementTree.ParseError as e:
except _xml_error as e:
# If this is due to an HTTP error, raise the HTTP error
self.maybe_raise_http_error(robj)
# otherwise, raise the parse error
Expand All @@ -281,7 +288,7 @@ def get(self, path, params=None):
return APIResult(result, current_time, expires_time)

def maybe_raise_http_error(self, response):
"""Called if a ParseError is raised for the response.
"""Called if a XML parse error is raised for the response.
Raises an error if the response itself was an error - we try
to parse it as XML first to see if it's an API error, but if
Expand Down
9 changes: 8 additions & 1 deletion tests/test_api.py
Expand Up @@ -9,6 +9,13 @@
from evelink.thirdparty.six.moves import urllib
import evelink.api as evelink_api

# Python 2.6's ElementTree raises xml.parsers.expat.ExpatError instead
# of ElementTree.ParseError
_xml_error = getattr(ElementTree, 'ParseError', None)
if _xml_error is None:
import xml.parsers.expat
_xml_error = xml.parsers.expat.ExpatError

def compress(s):
return zlib.compress(s)

Expand Down Expand Up @@ -234,7 +241,7 @@ def test_get_with_parse_error(self, mock_urlopen):
mock_urlopen.return_value.read.return_value = "Not good xml"
self.cache.get.return_value = None

self.assertRaises(ElementTree.ParseError, self.api.get, 'foo/Bar')
self.assertRaises(_xml_error, self.api.get, 'foo/Bar')

@mock.patch('evelink.thirdparty.six.moves.urllib.request.urlopen')
def test_get_with_compressed_error(self, mock_urlopen):
Expand Down
9 changes: 8 additions & 1 deletion tests/test_requests_api.py
Expand Up @@ -6,6 +6,13 @@
from evelink.thirdparty.six.moves.urllib.parse import parse_qs
import evelink.api as evelink_api

# Python 2.6's ElementTree raises xml.parsers.expat.ExpatError instead
# of ElementTree.ParseError
_xml_error = getattr(ElementTree, 'ParseError', None)
if _xml_error is None:
import xml.parsers.expat
_xml_error = xml.parsers.expat.ExpatError

class DummyException(Exception): pass

class DummyResponse(object):
Expand Down Expand Up @@ -156,7 +163,7 @@ def test_get_with_parse_error(self):
self.mock_sessions.get.return_value.status_code = 200
self.cache.get.return_value = None

self.assertRaises(ElementTree.ParseError,
self.assertRaises(_xml_error,
self.api.get, 'eve/Error')

def test_cached_get_with_error(self):
Expand Down

0 comments on commit 700f38e

Please sign in to comment.