Skip to content

Commit 4f4f5ec

Browse files
committed
gaia: do not query server at load time. Includes regression test
1 parent 0a65078 commit 4f4f5ec

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

astroquery/gaia/core.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, *, tap_plus_conn_handler=None,
5555
gaia_data_server='https://gea.esac.esa.int/',
5656
tap_server_context="tap-server",
5757
data_server_context="data-server",
58-
verbose=False, show_server_messages=True):
58+
verbose=False, show_server_messages=False):
5959
super(GaiaClass, self).__init__(url=gaia_tap_server,
6060
server_context=tap_server_context,
6161
tap_context="tap",
@@ -1168,23 +1168,19 @@ def get_status_messages(self):
11681168
"""Retrieve the messages to inform users about
11691169
the status of Gaia TAP
11701170
"""
1171-
try:
1172-
sub_context = self.GAIA_MESSAGES
1173-
conn_handler = self._TapPlus__getconnhandler()
1174-
response = conn_handler.execute_tapget(sub_context, verbose=False)
1175-
if response.status == 200:
1176-
if isinstance(response, Iterable):
1177-
for line in response:
1178-
1179-
try:
1180-
print(line.decode("utf-8").split('=', 1)[1])
1181-
except ValueError as e:
1182-
print(e)
1183-
except IndexError:
1184-
print("Archive down for maintenance")
1185-
1186-
except OSError:
1187-
print("Status messages could not be retrieved")
1171+
sub_context = self.GAIA_MESSAGES
1172+
conn_handler = self._TapPlus__getconnhandler()
1173+
response = conn_handler.execute_tapget(sub_context, verbose=False)
1174+
if response.status == 200:
1175+
if isinstance(response, Iterable):
1176+
for line in response:
1177+
1178+
try:
1179+
print(line.decode("utf-8").split('=', 1)[1])
1180+
except ValueError as e:
1181+
print(e)
1182+
except IndexError:
1183+
print("Archive down for maintenance")
11881184

11891185

11901186
Gaia = GaiaClass()

astroquery/tests/test_imports.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
import sys
3+
import importlib
4+
import pytest
5+
import socket
6+
from unittest.mock import patch
7+
from pytest_remotedata.disable_internet import no_internet
8+
from pytest_remotedata.disable_internet import INTERNET_OFF
9+
10+
# List of all astroquery modules to test
11+
ASTROQUERY_MODULES = [
12+
'astroquery.alma',
13+
'astroquery.astrometry_net',
14+
'astroquery.besancon',
15+
'astroquery.cadc',
16+
'astroquery.cosmosim',
17+
'astroquery.esa',
18+
'astroquery.esasky',
19+
'astroquery.eso',
20+
'astroquery.exoplanet_orbit_database',
21+
'astroquery.fermi',
22+
'astroquery.gaia',
23+
'astroquery.gama',
24+
'astroquery.gemini',
25+
'astroquery.heasarc',
26+
'astroquery.hitran',
27+
'astroquery.ipac.irsa.ibe',
28+
'astroquery.hips2fits',
29+
'astroquery.image_cutouts',
30+
'astroquery.imcce',
31+
'astroquery.ipac',
32+
'astroquery.ipac.irsa',
33+
'astroquery.ipac.irsa.irsa_dust',
34+
'astroquery.ipac.ned',
35+
'astroquery.ipac.nexsci.nasa_exoplanet_archive',
36+
'astroquery.jplhorizons',
37+
'astroquery.jplsbdb',
38+
'astroquery.jplspec',
39+
'astroquery.linelists',
40+
'astroquery.magpis',
41+
'astroquery.mast',
42+
'astroquery.mocserver',
43+
'astroquery.mpc',
44+
'astroquery.nasa_ads',
45+
'astroquery.nist',
46+
'astroquery.nvas',
47+
'astroquery.oac',
48+
'astroquery.ogle',
49+
'astroquery.open_exoplanet_catalogue',
50+
'astroquery.sdss',
51+
'astroquery.simbad',
52+
'astroquery.skyview',
53+
'astroquery.solarsystem',
54+
'astroquery.splatalogue',
55+
'astroquery.svo_fps',
56+
'astroquery.utils',
57+
'astroquery.vamdc',
58+
'astroquery.vo_conesearch',
59+
'astroquery.vizier',
60+
'astroquery.vsa',
61+
'astroquery.wfau',
62+
'astroquery.xmatch',
63+
]
64+
65+
class SocketTracker:
66+
def __init__(self):
67+
self.socket_attempts = []
68+
69+
def __call__(self, *args, **kwargs):
70+
# Record the attempt
71+
self.socket_attempts.append((args, kwargs))
72+
# Raise a clear error to indicate socket creation
73+
raise RuntimeError("Socket creation attempted during import")
74+
75+
76+
@pytest.mark.parametrize("module_name", ASTROQUERY_MODULES)
77+
def test_no_http_calls_during_import(module_name):
78+
"""
79+
Test that importing astroquery modules does not make any remote calls.
80+
81+
This is a regression test for 3343, and the error that raises is not
82+
properly caught by the framework below, but that's an unrelated issue.
83+
84+
This is the error shown if Gaia(show_server_messages=True) is called:
85+
```
86+
E TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
87+
```
88+
"""
89+
with no_internet():
90+
if module_name in sys.modules:
91+
del sys.modules[module_name]
92+
93+
tracker = SocketTracker()
94+
with patch('socket.socket', tracker):
95+
importlib.import_module(module_name)
96+
97+
assert not tracker.socket_attempts, (
98+
f"Module {module_name} attempted to create {len(tracker.socket_attempts)} "
99+
f"socket(s) during import:\n" +
100+
"\n".join(f" - {args} {kwargs}" for args, kwargs in tracker.socket_attempts)
101+
)

0 commit comments

Comments
 (0)