summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYaroslav Halchenko <debian@onerussian.com>2012-07-02 16:03:53 -0400
committerYaroslav Halchenko <debian@onerussian.com>2012-07-02 16:13:03 -0400
commit4df244465c3db3a8e9e624d17ed2982f595e2b8a (patch)
tree1d793718767d6ae6480d6a055f37661fafd44a73
parente15d0bdd3cc0bc0928e1f4d0b419a2fb3de02af9 (diff)
downloadnumpy-4df244465c3db3a8e9e624d17ed2982f595e2b8a.tar.gz
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
-rw-r--r--numpy/lib/npyio.py16
1 files changed, 12 insertions, 4 deletions
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)