Skip to content

Commit f12c09d

Browse files
committed
rec2csv does not close filehandles passed in open
svn path=/trunk/matplotlib/; revision=4748
1 parent f2213e8 commit f12c09d

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2007-12-16 rec2csv saves doubles without losing precision. Also, it
2+
does not close filehandles passed in open. - JDH,ADS
3+
14
2007-12-13 Moved rec2gtk to matplotlib.toolkits.gtktools and rec2excel
25
to matplotlib.toolkits.exceltools - JDH
36

lib/matplotlib/cbook.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def is_numlike(obj):
224224
except TypeError: return False
225225
else: return True
226226

227-
def to_filehandle(fname, flag='r'):
227+
def to_filehandle(fname, flag='r', return_opened=False):
228228
"""
229229
fname can be a filename or a file handle. Support for gzipped
230230
files is automatic, if the filename ends in .gz. flag is a
@@ -236,10 +236,14 @@ def to_filehandle(fname, flag='r'):
236236
fh = gzip.open(fname, flag)
237237
else:
238238
fh = file(fname, flag)
239+
opened = True
239240
elif hasattr(fname, 'seek'):
240241
fh = fname
242+
opened = False
241243
else:
242244
raise ValueError('fname must be a string or file handle')
245+
if return_opened:
246+
return fh, opened
243247
return fh
244248

245249
def flatten(seq, scalarp=is_scalar):

lib/matplotlib/mlab.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,13 +2335,14 @@ def rec2csv(r, fname, delimiter=',', formatd=None):
23352335
for i, name in enumerate(r.dtype.names):
23362336
funcs.append(csvformat_factory(formatd[name]).tostr)
23372337

2338-
fh = cbook.to_filehandle(fname, 'w')
2338+
fh, opened = cbook.to_filehandle(fname, 'w', return_opened=True)
23392339
writer = csv.writer(fh, delimiter=delimiter)
23402340
header = r.dtype.names
23412341
writer.writerow(header)
23422342
for row in r:
23432343
writer.writerow([func(val) for func, val in zip(funcs, row)])
2344-
fh.close()
2344+
if opened:
2345+
fh.close()
23452346

23462347

23472348

0 commit comments

Comments
 (0)