Skip to content

Commit

Permalink
Fix an issue that caused the astropy table reader on Windows to behav…
Browse files Browse the repository at this point in the history
…e differently to other platforms and read binary files as tables
  • Loading branch information
astrofrog committed Sep 24, 2024
1 parent 1c51c7f commit 81b3a37
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions glue/core/data_factories/astropy_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def is_readable_by_astropy(filename, **kwargs):

def astropy_table_read(*args, **kwargs):

# We need to specify the encoding because otherwise on Windows the default
# encoding might be cp1252 which seems to be able to read in most binary
# files, which is an issue since it will start recognizing e.g. PNGs as
# valid tables.
encoding = 'utf-8'

from astropy.table import Table

# In Python 3, as of Astropy 0.4, if the format is not specified, the
Expand All @@ -40,15 +46,15 @@ def astropy_table_read(*args, **kwargs):
# also more generally, we should first try the ASCII readers.
if 'format' not in kwargs:
try:
t = Table.read(*args, format='ascii', **kwargs)
t = Table.read(*args, format='ascii', encoding=encoding, **kwargs)
# Double-check for certain FITS files that may be read in as ASCII in Python 3.11
if not (len(t) == 1 and [c.value[0] for c in t.itercols()][:3] == ['SIMPLE', '=', 'T']):
return t
except Exception:
pass

# If the above didn't work, attempt to read with no specified format
return Table.read(*args, **kwargs)
return Table.read(*args, encoding=encoding, **kwargs)


@data_factory(label="Catalog (astropy.table parser)",
Expand Down

0 comments on commit 81b3a37

Please sign in to comment.