Skip to content

Commit 9c2ff73

Browse files
committed
Re-enable cache warnings, reveal and fix bug astropy#9704
1 parent 9015e9f commit 9c2ff73

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

‎astropy/config/paths.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ def __init__(self, path=None, delete=False):
187187

188188
def __enter__(self):
189189
self.__class__._temp_path = self._path
190-
return self._default_path_getter('astropy')
190+
try:
191+
return self._default_path_getter('astropy')
192+
except Exception:
193+
self.__class__._temp_path = self._prev_path
194+
raise
191195

192196
def __exit__(self, *args):
193197
self.__class__._temp_path = self._prev_path

‎astropy/config/tests/test_configs.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ def test_func():
7474
assert not os.path.exists(temp_cache_dir)
7575

7676

77+
def test_set_temp_cache_resets_on_exception(tmpdir):
78+
"""Test for regression of bug #9704"""
79+
t = paths.get_cache_dir()
80+
a = tmpdir / 'a'
81+
with open(a, 'wt') as f:
82+
f.write("not a good cache\n")
83+
with pytest.raises(OSError):
84+
with paths.set_temp_cache(a):
85+
pass
86+
assert t == paths.get_cache_dir()
87+
88+
7789
def test_config_file():
7890
from astropy.config.configuration import get_config, reload_config
7991

‎astropy/utils/tests/test_data.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,44 @@ def a_file(tmp_path):
257257
yield fn, contents
258258

259259

260+
def test_temp_cache(tmpdir):
261+
dldir0, urlmapfn0 = _get_download_cache_locs()
262+
check_download_cache(check_hashes=True)
263+
264+
with paths.set_temp_cache(tmpdir):
265+
dldir1, urlmapfn1 = _get_download_cache_locs()
266+
check_download_cache(check_hashes=True)
267+
assert dldir1 != dldir0
268+
assert urlmapfn1 != urlmapfn0
269+
270+
dldir2, urlmapfn2 = _get_download_cache_locs()
271+
check_download_cache(check_hashes=True)
272+
assert dldir2 != dldir1
273+
assert urlmapfn2 != urlmapfn1
274+
assert dldir2 == dldir0
275+
assert urlmapfn2 == urlmapfn0
276+
277+
# Check that things are okay even if we exit via an exception
278+
class Special(Exception):
279+
pass
280+
try:
281+
with paths.set_temp_cache(tmpdir):
282+
dldir3, urlmapfn3 = _get_download_cache_locs()
283+
check_download_cache(check_hashes=True)
284+
assert dldir3 == dldir1
285+
assert urlmapfn3 == urlmapfn1
286+
raise Special
287+
except Special:
288+
pass
289+
290+
dldir4, urlmapfn4 = _get_download_cache_locs()
291+
check_download_cache(check_hashes=True)
292+
assert dldir4 != dldir3
293+
assert urlmapfn4 != urlmapfn3
294+
assert dldir4 == dldir0
295+
assert urlmapfn4 == urlmapfn0
296+
297+
260298
@pytest.mark.parametrize("parallel", [False, True])
261299
def test_download_with_sources_and_bogus_original(
262300
valid_urls, invalid_urls, temp_cache, parallel):
@@ -1342,6 +1380,7 @@ def check_quietly_ignores_bogus_cache():
13421380
with pytest.raises(OSError):
13431381
check_download_cache()
13441382

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

13611401
# See what happens when set_temp_cache is pointed at a file
13621402
with pytest.raises(OSError):
13631403
with paths.set_temp_cache(fn):
13641404
pass
1405+
assert (dldir, urlmapfn) == _get_download_cache_locs()
13651406
assert get_file_contents(str(fn)) == ct
13661407

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

@@ -1381,6 +1423,7 @@ def check_quietly_ignores_bogus_cache():
13811423
f.write(ct)
13821424
with paths.set_temp_cache(tmpdir):
13831425
check_quietly_ignores_bogus_cache()
1426+
assert (dldir, urlmapfn) == _get_download_cache_locs()
13841427
assert get_file_contents(cd) == ct
13851428
os.remove(cd)
13861429

@@ -1392,6 +1435,7 @@ def check_quietly_ignores_bogus_cache():
13921435
f.write(ct)
13931436
with paths.set_temp_cache(tmpdir):
13941437
check_quietly_ignores_bogus_cache()
1438+
assert (dldir, urlmapfn) == _get_download_cache_locs()
13951439
assert get_file_contents(cd) == ct
13961440
os.remove(cd)
13971441

@@ -1403,6 +1447,7 @@ def check_quietly_ignores_bogus_cache():
14031447
os.makedirs(cd)
14041448
with paths.set_temp_cache(tmpdir):
14051449
check_quietly_ignores_bogus_cache()
1450+
assert (dldir, urlmapfn) == _get_download_cache_locs()
14061451

14071452

14081453
def test_get_fileobj_str(a_file):

‎setup.cfg‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ filterwarnings =
113113
ignore:The timefunc function is deprecated
114114
ignore:numpy.ufunc size changed:RuntimeWarning
115115
ignore:Using or importing the ABCs from 'collections':DeprecationWarning
116-
ignore:Remote data cache could not be accessed due to NotADirectoryError
117-
ignore:Remote data cache could not be accessed due to FileNotFoundError
118-
ignore:.*Cache directory cannot be read or created
119116
ignore:can't resolve package from __spec__
120117
ignore:::astropy.tests.plugins.display
121118
ignore:::astropy.tests.disable_internet

0 commit comments

Comments
 (0)