Skip to content

BUGFIX: prevent gaia from querying server at import time #3344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions astroquery/gaia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
gaia_data_server='https://gea.esac.esa.int/',
tap_server_context="tap-server",
data_server_context="data-server",
verbose=False, show_server_messages=True):
verbose=False, show_server_messages=False):
super(GaiaClass, self).__init__(url=gaia_tap_server,
server_context=tap_server_context,
tap_context="tap",
Expand Down Expand Up @@ -1168,23 +1168,19 @@
"""Retrieve the messages to inform users about
the status of Gaia TAP
"""
try:
sub_context = self.GAIA_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=False)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")

except OSError:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception catching is a particular problem: it hid from the tests that there is a remote query being performed, since pytest-remotedata raises an OSError

print("Status messages could not be retrieved")
sub_context = self.GAIA_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=False)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")

Check warning on line 1183 in astroquery/gaia/core.py

View check run for this annotation

Codecov / codecov/patch

astroquery/gaia/core.py#L1180-L1183

Added lines #L1180 - L1183 were not covered by tests


Gaia = GaiaClass()
101 changes: 101 additions & 0 deletions astroquery/tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import importlib
import pytest
import socket
from unittest.mock import patch
from pytest_remotedata.disable_internet import no_internet
from pytest_remotedata.disable_internet import INTERNET_OFF

# List of all astroquery modules to test
ASTROQUERY_MODULES = [
'astroquery.alma',
'astroquery.astrometry_net',
'astroquery.besancon',
'astroquery.cadc',
'astroquery.cosmosim',
'astroquery.esa',
'astroquery.esasky',
'astroquery.eso',
'astroquery.exoplanet_orbit_database',
'astroquery.fermi',
'astroquery.gaia',
'astroquery.gama',
'astroquery.gemini',
'astroquery.heasarc',
'astroquery.hitran',
'astroquery.ipac.irsa.ibe',
'astroquery.hips2fits',
'astroquery.image_cutouts',
'astroquery.imcce',
'astroquery.ipac',
'astroquery.ipac.irsa',
'astroquery.ipac.irsa.irsa_dust',
'astroquery.ipac.ned',
'astroquery.ipac.nexsci.nasa_exoplanet_archive',
'astroquery.jplhorizons',
'astroquery.jplsbdb',
'astroquery.jplspec',
'astroquery.linelists',
'astroquery.magpis',
'astroquery.mast',
'astroquery.mocserver',
'astroquery.mpc',
'astroquery.nasa_ads',
'astroquery.nist',
'astroquery.nvas',
'astroquery.oac',
'astroquery.ogle',
'astroquery.open_exoplanet_catalogue',
'astroquery.sdss',
'astroquery.simbad',
'astroquery.skyview',
'astroquery.solarsystem',
'astroquery.splatalogue',
'astroquery.svo_fps',
'astroquery.utils',
'astroquery.vamdc',
'astroquery.vo_conesearch',
'astroquery.vizier',
'astroquery.vsa',
'astroquery.wfau',
'astroquery.xmatch',
]

class SocketTracker:
def __init__(self):
self.socket_attempts = []

def __call__(self, *args, **kwargs):
# Record the attempt
self.socket_attempts.append((args, kwargs))
# Raise a clear error to indicate socket creation
raise RuntimeError("Socket creation attempted during import")


@pytest.mark.parametrize("module_name", ASTROQUERY_MODULES)
def test_no_http_calls_during_import(module_name):
"""
Test that importing astroquery modules does not make any remote calls.

This is a regression test for 3343, and the error that raises is not
properly caught by the framework below, but that's an unrelated issue.

This is the error shown if Gaia(show_server_messages=True) is called:
```
E TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
```
"""
with no_internet():
if module_name in sys.modules:
del sys.modules[module_name]

tracker = SocketTracker()
with patch('socket.socket', tracker):
importlib.import_module(module_name)

assert not tracker.socket_attempts, (
f"Module {module_name} attempted to create {len(tracker.socket_attempts)} "
f"socket(s) during import:\n" +
"\n".join(f" - {args} {kwargs}" for args, kwargs in tracker.socket_attempts)
)
Loading