Skip to content
Merged
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
6 changes: 5 additions & 1 deletion astropy/config/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ def __init__(self, path=None, delete=False):

def __enter__(self):
self.__class__._temp_path = self._path
return self._default_path_getter('astropy')
try:
Comment thread
pllim marked this conversation as resolved.
Resolved
return self._default_path_getter('astropy')
except Exception:
self.__class__._temp_path = self._prev_path
raise

def __exit__(self, *args):
self.__class__._temp_path = self._prev_path
Expand Down
12 changes: 12 additions & 0 deletions astropy/config/tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def test_func():
assert not os.path.exists(temp_cache_dir)


def test_set_temp_cache_resets_on_exception(tmpdir):
"""Test for regression of bug #9704"""
t = paths.get_cache_dir()
a = tmpdir / 'a'
with open(a, 'wt') as f:
f.write("not a good cache\n")
with pytest.raises(OSError):
with paths.set_temp_cache(a):
pass
assert t == paths.get_cache_dir()


def test_config_file():
from astropy.config.configuration import get_config, reload_config

Expand Down
45 changes: 45 additions & 0 deletions astropy/utils/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,44 @@ def a_file(tmp_path):
yield fn, contents


def test_temp_cache(tmpdir):
dldir0, urlmapfn0 = _get_download_cache_locs()
check_download_cache(check_hashes=True)

with paths.set_temp_cache(tmpdir):
dldir1, urlmapfn1 = _get_download_cache_locs()
check_download_cache(check_hashes=True)
assert dldir1 != dldir0
assert urlmapfn1 != urlmapfn0

dldir2, urlmapfn2 = _get_download_cache_locs()
check_download_cache(check_hashes=True)
assert dldir2 != dldir1
assert urlmapfn2 != urlmapfn1
assert dldir2 == dldir0
assert urlmapfn2 == urlmapfn0
Comment thread
pllim marked this conversation as resolved.
Resolved

# Check that things are okay even if we exit via an exception
class Special(Exception):
Comment thread
pllim marked this conversation as resolved.
Resolved
pass
try:
with paths.set_temp_cache(tmpdir):
dldir3, urlmapfn3 = _get_download_cache_locs()
check_download_cache(check_hashes=True)
assert dldir3 == dldir1
assert urlmapfn3 == urlmapfn1
raise Special
except Special:
pass

dldir4, urlmapfn4 = _get_download_cache_locs()
check_download_cache(check_hashes=True)
assert dldir4 != dldir3
assert urlmapfn4 != urlmapfn3
assert dldir4 == dldir0
assert urlmapfn4 == urlmapfn0


@pytest.mark.parametrize("parallel", [False, True])
def test_download_with_sources_and_bogus_original(
valid_urls, invalid_urls, temp_cache, parallel):
Expand Down Expand Up @@ -1342,6 +1380,7 @@ def check_quietly_ignores_bogus_cache():
with pytest.raises(OSError):
check_download_cache()

dldir, urlmapfn = _get_download_cache_locs()
# set_temp_cache acts weird if it is pointed at a file (see below)
# but we want to see what happens when the cache is pointed
# at a file instead of a directory, so make a directory we can
Expand All @@ -1356,12 +1395,14 @@ def check_quietly_ignores_bogus_cache():
with pytest.raises(OSError):
paths.get_cache_dir()
check_quietly_ignores_bogus_cache()
assert (dldir, urlmapfn) == _get_download_cache_locs()
assert get_file_contents(fn) == ct, "File should not be harmed."

# See what happens when set_temp_cache is pointed at a file
with pytest.raises(OSError):
with paths.set_temp_cache(fn):
pass
assert (dldir, urlmapfn) == _get_download_cache_locs()
assert get_file_contents(str(fn)) == ct

# Now the cache directory is normal but the subdirectory it wants
Expand All @@ -1371,6 +1412,7 @@ def check_quietly_ignores_bogus_cache():
f.write(ct)
with paths.set_temp_cache(tmpdir):
check_quietly_ignores_bogus_cache()
assert (dldir, urlmapfn) == _get_download_cache_locs()
assert get_file_contents(cd) == ct
os.remove(cd)

Expand All @@ -1381,6 +1423,7 @@ def check_quietly_ignores_bogus_cache():
f.write(ct)
with paths.set_temp_cache(tmpdir):
check_quietly_ignores_bogus_cache()
assert (dldir, urlmapfn) == _get_download_cache_locs()
assert get_file_contents(cd) == ct
os.remove(cd)

Expand All @@ -1392,6 +1435,7 @@ def check_quietly_ignores_bogus_cache():
f.write(ct)
with paths.set_temp_cache(tmpdir):
check_quietly_ignores_bogus_cache()
assert (dldir, urlmapfn) == _get_download_cache_locs()
assert get_file_contents(cd) == ct
os.remove(cd)

Expand All @@ -1403,6 +1447,7 @@ def check_quietly_ignores_bogus_cache():
os.makedirs(cd)
with paths.set_temp_cache(tmpdir):
check_quietly_ignores_bogus_cache()
assert (dldir, urlmapfn) == _get_download_cache_locs()


def test_get_fileobj_str(a_file):
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ filterwarnings =
ignore:The timefunc function is deprecated
ignore:numpy.ufunc size changed:RuntimeWarning
ignore:Using or importing the ABCs from 'collections':DeprecationWarning
ignore:Remote data cache could not be accessed due to NotADirectoryError
ignore:Remote data cache could not be accessed due to FileNotFoundError
ignore:.*Cache directory cannot be read or created
ignore:can't resolve package from __spec__
ignore:::astropy.tests.plugins.display
ignore:::astropy.tests.disable_internet
Expand Down