Skip to content

Commit

Permalink
fix py34 compatibility issue; other bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Nov 8, 2016
1 parent 66ede05 commit 6d81943
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_xopen_file(self):

@skipIf(no_internet(), "No internet connection")
def test_xopen_url(self):
badurl = 'http://blorf.blurp'
badurl = 'http://google.com/__badurl__'
with self.assertRaises(ValueError):
xopen(badurl)
url = 'https://github.com/jdidion/xphyle/blob/master/tests/foo.gz?raw=True'
Expand Down
4 changes: 3 additions & 1 deletion xphyle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def xopen(path : 'str', mode : 'str' = 'r', compression : 'bool|str' = None,
if not hasattr(fh, 'peek'):
fh = io.BufferedReader(fh)
guess = guess_format_from_buffer(fh)
else:
validate = False
if not (compression or guess):
is_stream = True
if 'b' in mode:
Expand Down Expand Up @@ -234,7 +236,7 @@ def xopen(path : 'str', mode : 'str' = 'r', compression : 'bool|str' = None,
guess = guess_format_from_file_header(path)
else:
path = check_writeable_file(path)
if guess_format:
if validate or guess_format:
guess = guess_compression_format(path)

if validate and guess != compression:
Expand Down
7 changes: 6 additions & 1 deletion xphyle/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Methods for handling URLs.
"""
import copy
import io
import re
from urllib.request import urlopen, Request
from urllib.parse import urlparse
Expand Down Expand Up @@ -41,7 +42,11 @@ def open_url(url, byte_range=None, headers={}, **kwargs):
headers['Range'] = 'bytes={}-{}'.format(*byte_range)
try:
request = Request(url, headers=headers, **kwargs)
return urlopen(request)
response = urlopen(request)
# HTTPResponse didn't have 'peek' until 3.5
if response and not hasattr(response, 'peek'):
response = io.BufferedReader(response)
return response
except:
return None

Expand Down

0 comments on commit 6d81943

Please sign in to comment.