Skip to content

Commit f18f02b

Browse files
authored
Merge pull request #2187 from keflavich/issue2139
Fix Issue 2139: remove cache file if failed query occurs
2 parents 75cfd69 + c219c47 commit f18f02b

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

astroquery/query.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ def from_cache(self, cache_location):
115115
log.debug("Retrieving data from {0}".format(request_file))
116116
return response
117117

118+
def remove_cache_file(self, cache_location):
119+
"""
120+
Remove the cache file - may be needed if a query fails during parsing
121+
(successful request, but failed return)
122+
"""
123+
request_file = self.request_file(cache_location)
124+
125+
if os.path.exists(request_file):
126+
os.remove(request_file)
127+
else:
128+
raise OSError(f"Tried to remove cache file {request_file} but "
129+
"it does not exist")
130+
118131

119132
class LoginABCMeta(abc.ABCMeta):
120133
"""

astroquery/simbad/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,12 @@ def _parse_result(self, result, resultclass=SimbadVOTableResult,
10551055
return None
10561056
except Exception as ex:
10571057
self.last_table_parse_error = ex
1058+
try:
1059+
self._last_query.remove_cache_file(self.cache_location)
1060+
except OSError:
1061+
# this is allowed: if `cache` was set to False, this
1062+
# won't be needed
1063+
pass
10581064
raise TableParseError("Failed to parse SIMBAD result! The raw "
10591065
"response can be found in "
10601066
"self.last_response, and the error in "

astroquery/simbad/tests/test_simbad.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ... import simbad
1111
from ...utils.testing_tools import MockResponse
1212
from ...utils import commons
13+
from ...query import AstroQuery
1314
from ...exceptions import TableParseError
1415
from .test_simbad_remote import multicoords
1516

@@ -118,20 +119,23 @@ def test_get_frame_coordinates(coordinates, expected_frame):
118119

119120

120121
def test_parse_result():
121-
result1 = simbad.core.Simbad._parse_result(
122+
sb = simbad.core.Simbad()
123+
# need _last_query to be defined
124+
sb._last_query = AstroQuery('GET', 'http://dummy')
125+
result1 = sb._parse_result(
122126
MockResponseSimbad('query id '), simbad.core.SimbadVOTableResult)
123127
assert isinstance(result1, Table)
124128
with pytest.raises(TableParseError) as ex:
125-
simbad.core.Simbad._parse_result(MockResponseSimbad('query error '),
126-
simbad.core.SimbadVOTableResult)
129+
sb._parse_result(MockResponseSimbad('query error '),
130+
simbad.core.SimbadVOTableResult)
127131
assert str(ex.value) == ('Failed to parse SIMBAD result! The raw response '
128132
'can be found in self.last_response, and the '
129133
'error in self.last_table_parse_error. '
130134
'The attempted parsed result is in '
131135
'self.last_parsed_result.\n Exception: 7:115: '
132136
'no element found')
133-
assert isinstance(simbad.Simbad.last_response.text, str)
134-
assert isinstance(simbad.Simbad.last_response.content, bytes)
137+
assert isinstance(sb.last_response.text, str)
138+
assert isinstance(sb.last_response.content, bytes)
135139

136140

137141
votable_fields = ",".join(simbad.core.Simbad.get_votable_fields())

0 commit comments

Comments
 (0)