Skip to content

Commit 49d40d4

Browse files
authored
Merge pull request astropy#3586 from emellega/fix-issue-with-caching
Fix bug caching requests with pyvo request_hooks
2 parents dca4f5d + 24c3da4 commit 49d40d4

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ Infrastructure, Utility and Other Changes and Additions
215215

216216
- Fix no expiration case for ``cache_timeout`` config option. [#3579]
217217

218+
- Workaround upstream bug when caching a response using pyvo. [#3586]
219+
218220
utils.tap
219221
^^^^^^^^^
220222

astroquery/query.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@
3030
__all__ = ['BaseVOQuery', 'BaseQuery', 'QueryWithLogin']
3131

3232

33-
def to_cache(response, cache_file):
33+
def to_cache(original_response, cache_file):
3434
log.debug("Caching data to {0}".format(cache_file))
3535

36-
response = copy.deepcopy(response)
37-
if hasattr(response, 'request'):
38-
for key in tuple(response.request.hooks.keys()):
39-
del response.request.hooks[key]
36+
# Copying a TAPService instance with an auth session produces some warnings
37+
# as a side affect. The hooks are not needed in the cache, so we remove
38+
# them before caching and restore them afterwards.
39+
# Related pyvo issue: https://github.com/astropy/pyvo/issues/755
40+
hooks = None
41+
if hasattr(original_response, 'request'):
42+
hooks = original_response.request.hooks
43+
del original_response.request.hooks
44+
if hasattr(original_response, 'history'):
45+
for r in original_response.history:
46+
if hasattr(r, 'request'):
47+
del r.request.hooks
48+
response_copy = copy.deepcopy(original_response)
49+
if hooks:
50+
original_response.request.hooks = hooks
4051
with open(cache_file, "wb") as f:
41-
pickle.dump(response, f, protocol=4)
52+
pickle.dump(response_copy, f, protocol=4)
4253

4354

4455
def _replace_none_iterable(iterable):

0 commit comments

Comments
 (0)