From 4df244465c3db3a8e9e624d17ed2982f595e2b8a Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 2 Jul 2012 16:03:53 -0400 Subject: BUG: do not "own" the FID for GzipFile and file if provided to load already opened (ticket #2178) Also made all assignments of own_file go in pair with assignments to fid to make things clearer --- numpy/lib/npyio.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 221529929..e16485ff0 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -2,6 +2,9 @@ __all__ = ['savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource'] +# Price to pay for overloading standard keywords +import __builtin__ + import numpy as np import format import sys @@ -353,14 +356,19 @@ def load(file, mmap_mode=None): """ import gzip - own_fid = False if isinstance(file, basestring): - fid = open(file, "rb") own_fid = True + fid = open(file, "rb") elif isinstance(file, gzip.GzipFile): + # we were provided an existing handle which we should close + # only if it was closed already + own_fid = file.closed fid = seek_gzip_factory(file) - own_fid = True + elif isinstance(file, __builtin__.file): + own_fid = file.closed + fid = file else: + own_fid = False fid = file try: @@ -371,7 +379,7 @@ def load(file, mmap_mode=None): fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) own_fid = False - return NpzFile(fid, own_fid=True) + return NpzFile(fid, own_fid=own_fid) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) -- cgit v1.2.1 From 4219824f899d31a0fb205536bf1477d68bcc5d71 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 2 Jul 2012 19:00:14 -0400 Subject: BF: PY3 and PY2 < 2.7 compatibility fixes for prev 2 commits --- numpy/lib/npyio.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index e16485ff0..b8564be03 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -2,9 +2,6 @@ __all__ = ['savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource'] -# Price to pay for overloading standard keywords -import __builtin__ - import numpy as np import format import sys @@ -25,13 +22,20 @@ from _iotools import LineSplitter, NameValidator, StringConverter, \ _is_string_like, has_nested_fields, flatten_dtype, \ easy_dtype, _bytes_to_name -from numpy.compat import asbytes, asstr, asbytes_nested, bytes +from numpy.compat import asbytes, asstr, asbytes_nested, bytes, isfileobj if sys.version_info[0] >= 3: from io import BytesIO else: from cStringIO import StringIO as BytesIO +if sys.version_info[:2] >= (2, 7): + def _isgzipclosed(f): + return f.closed +else: + def _isgzipclosed(f): + return getattr(f, 'myfileobj', None) is not None + _string_like = _is_string_like def seek_gzip_factory(f): @@ -362,9 +366,9 @@ def load(file, mmap_mode=None): elif isinstance(file, gzip.GzipFile): # we were provided an existing handle which we should close # only if it was closed already - own_fid = file.closed + own_fid = _isgzipclosed(file) fid = seek_gzip_factory(file) - elif isinstance(file, __builtin__.file): + elif isfileobj(file): own_fid = file.closed fid = file else: -- cgit v1.2.1 From 153f764db0a8bb15d52c76be44058922916abef5 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 5 Jul 2012 09:34:42 -0400 Subject: ENH: Since file handle could not be reopened (during load()) -- no need for "isclosed" logic --- numpy/lib/npyio.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index b8564be03..695341da4 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -22,20 +22,13 @@ from _iotools import LineSplitter, NameValidator, StringConverter, \ _is_string_like, has_nested_fields, flatten_dtype, \ easy_dtype, _bytes_to_name -from numpy.compat import asbytes, asstr, asbytes_nested, bytes, isfileobj +from numpy.compat import asbytes, asstr, asbytes_nested, bytes if sys.version_info[0] >= 3: from io import BytesIO else: from cStringIO import StringIO as BytesIO -if sys.version_info[:2] >= (2, 7): - def _isgzipclosed(f): - return f.closed -else: - def _isgzipclosed(f): - return getattr(f, 'myfileobj', None) is not None - _string_like = _is_string_like def seek_gzip_factory(f): @@ -360,19 +353,13 @@ def load(file, mmap_mode=None): """ import gzip + own_fid = False if isinstance(file, basestring): - own_fid = True fid = open(file, "rb") + own_fid = True elif isinstance(file, gzip.GzipFile): - # we were provided an existing handle which we should close - # only if it was closed already - own_fid = _isgzipclosed(file) fid = seek_gzip_factory(file) - elif isfileobj(file): - own_fid = file.closed - fid = file else: - own_fid = False fid = file try: @@ -382,7 +369,6 @@ def load(file, mmap_mode=None): magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) - own_fid = False return NpzFile(fid, own_fid=own_fid) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: -- cgit v1.2.1 From c35c83c2e7b9f0cd51606d5a63c36cc68cf4556a Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 5 Jul 2012 10:18:09 -0400 Subject: BF: removed too much -- own_fid should be False while working with .npz --- numpy/lib/npyio.py | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 695341da4..cb14e4963 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -369,6 +369,7 @@ def load(file, mmap_mode=None): magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) + own_fid = False return NpzFile(fid, own_fid=own_fid) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: -- cgit v1.2.1