Skip to content

Fix for issue #1356 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,35 @@ def seek_gzip_factory(f):
"""
import gzip

def seek(self, offset, whence=0):
# figure out new position (we can only seek forwards)
if whence == 1:
offset = self.offset + offset
class GzipFile(gzip.GzipFile):

if whence not in [0, 1]:
raise IOError, "Illegal argument"
def seek(self, offset, whence=0):
# figure out new position (we can only seek forwards)
if whence == 1:
offset = self.offset + offset

if offset < self.offset:
# for negative seek, rewind and do positive seek
self.rewind()
count = offset - self.offset
for i in range(count // 1024):
self.read(1024)
self.read(count % 1024)
if whence not in [0, 1]:
raise IOError, "Illegal argument"

def tell(self):
return self.offset
if offset < self.offset:
# for negative seek, rewind and do positive seek
self.rewind()
count = offset - self.offset
for i in range(count // 1024):
self.read(1024)
self.read(count % 1024)

if isinstance(f, str):
f = gzip.GzipFile(f)
def tell(self):
return self.offset

if sys.version_info[0] >= 3:
import types
f.seek = types.MethodType(seek, f)
f.tell = types.MethodType(tell, f)
else:
import new
f.seek = new.instancemethod(seek, f)
f.tell = new.instancemethod(tell, f)

if isinstance(f, str):
f = GzipFile(f)
elif isinstance(f, gzip.GzipFile):
# cast if its a gzip.GzipFile
mode = f.mode
f = GzipFile(filename=f.filename, fileobj=f.fileobj)
f.mode = mode

return f

Expand Down Expand Up @@ -664,7 +663,6 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
if _is_string_like(fname):
own_fh = True
if fname.endswith('.gz'):
import gzip
fh = seek_gzip_factory(fname)
elif fname.endswith('.bz2'):
import bz2
Expand Down