Skip to content

Commit db7f825

Browse files
authored
Merge pull request #2295 from keflavich/issue2294
Possible fix for issue2294: don't pickle hooks
2 parents 6400d6c + 0d9e641 commit db7f825

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Infrastructure, Utility and Other Changes and Additions
3939
- The obsolete file ``astroquery/utils/testing_tools.py`` has been removed.
4040
[#2287]
4141

42+
- Callback hooks are deleted before caching. Potentially all cached queries
43+
prior to this PR will be rendered invalid. [#2295]
44+
4245

4346
0.4.5 (2021-12-24)
4447
==================

astroquery/query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import abc
55
import inspect
66
import pickle
7+
import copy
78
import getpass
89
import hashlib
910
import keyring
@@ -26,6 +27,10 @@
2627

2728
def to_cache(response, cache_file):
2829
log.debug("Caching data to {0}".format(cache_file))
30+
response = copy.deepcopy(response)
31+
if hasattr(response, 'request'):
32+
for key in tuple(response.request.hooks.keys()):
33+
del response.request.hooks[key]
2934
with open(cache_file, "wb") as f:
3035
pickle.dump(response, f)
3136

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
3+
import pytest
4+
import shutil
5+
import tempfile
6+
7+
from astroquery.mpc import MPC
8+
9+
10+
@pytest.mark.remote_data
11+
class TestRandomThings:
12+
13+
@pytest.fixture()
14+
def temp_dir(self, request):
15+
my_temp_dir = tempfile.mkdtemp()
16+
17+
def fin():
18+
shutil.rmtree(my_temp_dir)
19+
request.addfinalizer(fin)
20+
return my_temp_dir
21+
22+
def test_quantity_hooks_cache(self, temp_dir):
23+
# Regression test for #2294
24+
mpc = MPC()
25+
mpc.cache_location = temp_dir
26+
mpc.get_observations(12893, cache=True)
27+
mpc.get_observations(12894, cache=True)

0 commit comments

Comments
 (0)