Skip to content

Commit

Permalink
fix loading gzip compressed cached release with Python 3 (fix #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Dec 12, 2013
1 parent 3694878 commit 8f4121c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/rosdistro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
from io import BytesIO as StringIO
import yaml

logger = logging.getLogger('rosdistro')
Expand Down Expand Up @@ -151,10 +151,12 @@ def get_release_cache(index, dist_name):
if url.endswith('.yaml'):
yaml_str = load_url(url)
elif url.endswith('.yaml.gz'):
yaml_gz_str = load_url(url)
yaml_gz_str = load_url(url, skip_decode=True)
yaml_gz_stream = StringIO(yaml_gz_str)
f = gzip.GzipFile(fileobj=yaml_gz_stream, mode='rb')
yaml_str = f.read()
if not isinstance(yaml_str, str):
yaml_str = yaml_str.decode('utf-8')
f.close()
else:
raise NotImplementedError('The url of the cache must end with either ".yaml" or ".yaml.gz"')
Expand Down
4 changes: 2 additions & 2 deletions src/rosdistro/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from urllib2 import URLError


def load_url(url, retry=2, retry_period=1, timeout=10):
def load_url(url, retry=2, retry_period=1, timeout=10, skip_decode=False):
try:
fh = urlopen(url, timeout=timeout)
except HTTPError as e:
Expand All @@ -59,7 +59,7 @@ def load_url(url, retry=2, retry_period=1, timeout=10):
raise URLError(str(e) + ' (%s)' % url)
# Python 2/3 Compatibility
contents = fh.read()
if isinstance(contents, str):
if isinstance(contents, str) or skip_decode:
return contents
else:
return contents.decode('utf-8')

0 comments on commit 8f4121c

Please sign in to comment.