Skip to content

TST: make pytables tests go thru a temporary dir and file #5422

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
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ def _tables():
return _table_mod


def h5_open(path, mode):
tables = _tables()
return tables.openFile(path, mode)


@contextmanager
def get_store(path, **kwargs):
"""
Expand Down Expand Up @@ -475,6 +470,8 @@ def open(self, mode='a'):
mode : {'a', 'w', 'r', 'r+'}, default 'a'
See HDFStore docstring or tables.openFile for info about modes
"""
tables = _tables()

if self._mode != mode:

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

try:
self._handle = h5_open(self._path, self._mode)
except IOError as e: # pragma: no cover
self._handle = tables.openFile(self._path, self._mode)
except (IOError) as e: # pragma: no cover
if 'can not be written' in str(e):
print('Opening %s in read-only mode' % self._path)
self._handle = h5_open(self._path, 'r')
self._handle = tables.openFile(self._path, 'r')
else:
raise
except (Exception) as e:

# trying to read from a non-existant file causes an error which
# is not part of IOError, make it one
if self._mode == 'r' and 'Unable to open/create file' in str(e):
raise IOError(str(e))
raise

def close(self):
"""
Expand Down
17 changes: 14 additions & 3 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import os
import warnings
import tempfile
from contextlib import contextmanager

import datetime
Expand Down Expand Up @@ -57,6 +58,11 @@ def safe_close(store):
@contextmanager
def ensure_clean(path, mode='a', complevel=None, complib=None,
fletcher32=False):

# put in the temporary path if we don't have one already
if not len(os.path.dirname(path)):
path = tempfile.mkstemp(suffix=path)[1]

store = HDFStore(path, mode=mode, complevel=complevel,
complib=complib, fletcher32=False)
try:
Expand Down Expand Up @@ -192,17 +198,22 @@ def test_api(self):

with ensure_clean(self.path) as store:

path = store._path
df = tm.makeDataFrame()

_maybe_remove(store,'df')
store.append('df',df.iloc[:10],append=True,format='table')
store.append('df',df.iloc[10:],append=True,format='table')
assert_frame_equal(read_hdf(path,'df'),df)

# append to False
_maybe_remove(store,'df')
store.append('df',df.iloc[:10],append=False,format='table')
store.append('df',df.iloc[10:],append=True,format='table')
assert_frame_equal(read_hdf(path,'df'),df)

# formats
_maybe_remove(store,'df')
store.append('df',df.iloc[:10],append=False,format='table')
store.append('df',df.iloc[10:],append=True,format='table')
assert_frame_equal(read_hdf(path,'df'),df)
Expand Down Expand Up @@ -373,7 +384,7 @@ def check(mode):
with tm.ensure_clean(self.path) as path:

# constructor
if mode in ['r','r+']:
if mode in ['r']:
self.assertRaises(IOError, HDFStore, path, mode=mode)

else:
Expand All @@ -384,7 +395,7 @@ def check(mode):
with tm.ensure_clean(self.path) as path:

# context
if mode in ['r','r+']:
if mode in ['r']:
def f():
with get_store(path,mode=mode) as store:
pass
Expand All @@ -396,7 +407,7 @@ def f():
with tm.ensure_clean(self.path) as path:

# conv write
if mode in ['r','r+']:
if mode in ['r']:
self.assertRaises(IOError, df.to_hdf, path, 'df', mode=mode)
df.to_hdf(path,'df',mode='w')
else:
Expand Down
7 changes: 4 additions & 3 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def ensure_clean(filename=None, return_filelike=False):
filename : str (optional)
if None, creates a temporary file which is then removed when out of
scope.
return_filelike: bool (default False)
return_filelike : bool (default False)
if True, returns a file-like which is *always* cleaned. Necessary for
savefig and other functions which want to append extensions. Ignores
filename if True.
Expand All @@ -329,8 +329,9 @@ def ensure_clean(filename=None, return_filelike=False):

else:
# if we are not passed a filename, generate a temporary
if filename is None:
filename = tempfile.mkstemp()[1]
# make sure that we are using a temp dir as well
suffix = filename if filename is not None and not len(os.path.dirname(filename)) else ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to just do: if len(os.path.dirname(filename)): raise ValueError("Can't pass a directory path to ensure_clean") and then just do filename = filename or '' before that.

filename = tempfile.mkstemp(suffix=suffix)[1]

try:
yield filename
Expand Down