Skip to content

Commit f036588

Browse files
jrebackjtratner
authored andcommitted
1 parent 223d652 commit f036588

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

pandas/io/pytables.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,6 @@ def _tables():
225225
return _table_mod
226226

227227

228-
def h5_open(path, mode):
229-
tables = _tables()
230-
return tables.openFile(path, mode)
231-
232-
233228
@contextmanager
234229
def get_store(path, **kwargs):
235230
"""
@@ -475,6 +470,8 @@ def open(self, mode='a'):
475470
mode : {'a', 'w', 'r', 'r+'}, default 'a'
476471
See HDFStore docstring or tables.openFile for info about modes
477472
"""
473+
tables = _tables()
474+
478475
if self._mode != mode:
479476

480477
# if we are chaning a write mode to read, ok
@@ -501,13 +498,20 @@ def open(self, mode='a'):
501498
fletcher32=self._fletcher32)
502499

503500
try:
504-
self._handle = h5_open(self._path, self._mode)
505-
except IOError as e: # pragma: no cover
501+
self._handle = tables.openFile(self._path, self._mode)
502+
except (IOError) as e: # pragma: no cover
506503
if 'can not be written' in str(e):
507504
print('Opening %s in read-only mode' % self._path)
508-
self._handle = h5_open(self._path, 'r')
505+
self._handle = tables.openFile(self._path, 'r')
509506
else:
510507
raise
508+
except (Exception) as e:
509+
510+
# trying to read from a non-existant file causes an error which
511+
# is not part of IOError, make it one
512+
if self._mode == 'r' and 'Unable to open/create file' in str(e):
513+
raise IOError(str(e))
514+
raise
511515

512516
def close(self):
513517
"""

pandas/io/tests/test_pytables.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import os
55
import warnings
6+
import tempfile
67
from contextlib import contextmanager
78

89
import datetime
@@ -57,6 +58,11 @@ def safe_close(store):
5758
@contextmanager
5859
def ensure_clean(path, mode='a', complevel=None, complib=None,
5960
fletcher32=False):
61+
62+
# put in the temporary path if we don't have one already
63+
if not len(os.path.dirname(path)):
64+
path = tempfile.mkstemp(suffix=path)[1]
65+
6066
store = HDFStore(path, mode=mode, complevel=complevel,
6167
complib=complib, fletcher32=False)
6268
try:
@@ -192,17 +198,22 @@ def test_api(self):
192198

193199
with ensure_clean(self.path) as store:
194200

201+
path = store._path
195202
df = tm.makeDataFrame()
203+
204+
_maybe_remove(store,'df')
196205
store.append('df',df.iloc[:10],append=True,format='table')
197206
store.append('df',df.iloc[10:],append=True,format='table')
198207
assert_frame_equal(read_hdf(path,'df'),df)
199208

200209
# append to False
210+
_maybe_remove(store,'df')
201211
store.append('df',df.iloc[:10],append=False,format='table')
202212
store.append('df',df.iloc[10:],append=True,format='table')
203213
assert_frame_equal(read_hdf(path,'df'),df)
204214

205215
# formats
216+
_maybe_remove(store,'df')
206217
store.append('df',df.iloc[:10],append=False,format='table')
207218
store.append('df',df.iloc[10:],append=True,format='table')
208219
assert_frame_equal(read_hdf(path,'df'),df)
@@ -373,7 +384,7 @@ def check(mode):
373384
with tm.ensure_clean(self.path, make_tempfile=False) as path:
374385

375386
# constructor
376-
if mode in ['r','r+']:
387+
if mode in ['r']:
377388
self.assertRaises(IOError, HDFStore, path, mode=mode)
378389

379390
else:
@@ -384,7 +395,7 @@ def check(mode):
384395
with tm.ensure_clean(self.path, make_tempfile=False) as path:
385396

386397
# context
387-
if mode in ['r','r+']:
398+
if mode in ['r']:
388399
def f():
389400
with get_store(path,mode=mode) as store:
390401
pass
@@ -396,7 +407,7 @@ def f():
396407
with tm.ensure_clean(self.path, make_tempfile=False) as path:
397408

398409
# conv write
399-
if mode in ['r','r+']:
410+
if mode in ['r']:
400411
self.assertRaises(IOError, df.to_hdf, path, 'df', mode=mode)
401412
df.to_hdf(path,'df',mode='w')
402413
else:

pandas/util/testing.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def ensure_clean(filename=None, return_filelike=False, make_tempfile=True):
314314
filename : str (optional)
315315
if None, creates a temporary file which is then removed when out of
316316
scope. if passed, creates temporary file with filename as ending.
317-
return_filelike: bool (default False)
317+
return_filelike : bool (default False)
318318
if True, returns a file-like which is *always* cleaned. Necessary for
319319
savefig and other functions which want to append extensions. Ignores
320320
filename if True.
@@ -332,15 +332,14 @@ def ensure_clean(filename=None, return_filelike=False, make_tempfile=True):
332332
f.close()
333333

334334
else:
335-
# if we are not passed a filename, generate a temporary
336-
if make_tempfile or not filename:
337-
filename = tempfile.mkstemp(suffix=filename)[1]
338-
339335
try:
336+
# if we are not passed a filename, generate a temporary
337+
if make_tempfile or not filename:
338+
filename = tempfile.mkstemp(suffix=filename)[1]
340339
yield filename
341340
finally:
342341
try:
343-
if os.path.exists(filename):
342+
if filename and os.path.exists(filename):
344343
os.remove(filename)
345344
except Exception as e:
346345
print("Exception on removing file: %s" % e)

0 commit comments

Comments
 (0)