Skip to content

Commit 67272bc

Browse files
committed
fix: use constants to help avoid bugs
1 parent bc87818 commit 67272bc

9 files changed

Lines changed: 98 additions & 53 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
Changes
66

7+
- Added support for `rsfitsio` as a backend in addition to `cfitsio`.
8+
- Added function `fitsio_backend` to return the current backend.
9+
- Renamed `cfitsio_*` functions to `backend_*` functions for clarity.
10+
- Added constant `CFITSIO_BACKEND` and `RSFITSIO_BACKEND` which are
11+
the return values of `fitsio_backend`.
12+
713
Bug Fixes
814

915
- Added missing memory allocation checks in C layer.

fitsio/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@
5555
backend_has_curl_support = cfitsio_has_curl_support
5656
backend_is_reentrant = cfitsio_is_reentrant
5757

58+
# return values of fitsio_backend, here to help make code
59+
# clearer and avoid mispelling errors in testing strings
60+
CFITSIO_BACKEND = "cfitsio"
61+
RSFITSIO_BACKEND = "rsfitsio"
62+
5863
from .fits_exceptions import FITSFormatError

fitsio/tests/makedata.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
import numpy as np
33
from functools import lru_cache
44

5-
from .. import backend_version, fitsio_backend
5+
from .. import (
6+
backend_version,
7+
fitsio_backend,
8+
CFITSIO_BACKEND,
9+
RSFITSIO_BACKEND,
10+
)
611

712
BACKEND_VERSION = backend_version(asfloat=True)
813

@@ -66,8 +71,8 @@ def make_data():
6671
]
6772

6873
if (
69-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
70-
) or fitsio_backend() == "rsfitsio":
74+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
75+
) or fitsio_backend() == RSFITSIO_BACKEND:
7176
dtype += [
7277
('u8scalar', 'u8'),
7378
('u8vec', 'u8', nvec),
@@ -78,8 +83,8 @@ def make_data():
7883
# handle non-space padded strings
7984
# properly
8085
if (
81-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
82-
) or fitsio_backend() == "rsfitsio":
86+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
87+
) or fitsio_backend() == RSFITSIO_BACKEND:
8388
dtype += [
8489
('Sscalar_nopad', Sdtype),
8590
('Svec_nopad', Sdtype, nvec),
@@ -97,8 +102,8 @@ def make_data():
97102
# handle non-space padded strings
98103
# properly
99104
if (
100-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
101-
) or fitsio_backend() == "rsfitsio":
105+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
106+
) or fitsio_backend() == RSFITSIO_BACKEND:
102107
dtype += [
103108
('Uscalar_nopad', Udtype),
104109
('Uvec_nopad', Udtype, nvec),
@@ -128,8 +133,8 @@ def make_data():
128133
'c16',
129134
]
130135
if (
131-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
132-
) or fitsio_backend() == "rsfitsio":
136+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
137+
) or fitsio_backend() == RSFITSIO_BACKEND:
133138
dtypes += ["u8"]
134139

135140
for t in dtypes:
@@ -184,8 +189,8 @@ def make_data():
184189
# handle non-space padded strings
185190
# properly
186191
if (
187-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
188-
) or fitsio_backend() == "rsfitsio":
192+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
193+
) or fitsio_backend() == RSFITSIO_BACKEND:
189194
data['Sscalar_nopad'] = ['hello', 'world', 'good', 'bye']
190195
data['Svec_nopad'][:, 0] = 'hello'
191196
data['Svec_nopad'][:, 1] = 'world'
@@ -209,8 +214,8 @@ def make_data():
209214
# handle non-space padded strings
210215
# properly
211216
if (
212-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
213-
) or fitsio_backend() == "rsfitsio":
217+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
218+
) or fitsio_backend() == RSFITSIO_BACKEND:
214219
data['Uscalar_nopad'] = ['hello', 'world', 'good', 'bye']
215220
data['Uvec_nopad'][:, 0] = 'hello'
216221
data['Uvec_nopad'][:, 1] = 'world'
@@ -356,8 +361,8 @@ def make_data():
356361
('Sarr', Sdtype, ashape),
357362
]
358363
if (
359-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
360-
) or fitsio_backend() == "rsfitsio":
364+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
365+
) or fitsio_backend() == RSFITSIO_BACKEND:
361366
dtype += [
362367
('u8vec', 'u8', nvec),
363368
('u8arr', 'u8', ashape),
@@ -377,8 +382,8 @@ def make_data():
377382

378383
_dtypes = ['u1', 'i1', 'u2', 'i2', 'u4', 'i4', 'i8', 'f4', 'f8']
379384
if (
380-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4
381-
) or fitsio_backend() == "rsfitsio":
385+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4
386+
) or fitsio_backend() == RSFITSIO_BACKEND:
382387
_dtypes += ["u8"]
383388
for t in _dtypes:
384389
vardata[t + 'scalar'] = 1 + np.arange(nrows, dtype=t)

fitsio/tests/test_header.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
from ..fitslib import FITS, read_header, write
1111
from ..header import FITSHDR
1212
from ..hdu.base import INVALID_HDR_CHARS
13-
from .. import backend_version, fitsio_backend
13+
from .. import (
14+
backend_version,
15+
fitsio_backend,
16+
CFITSIO_BACKEND,
17+
RSFITSIO_BACKEND,
18+
)
1419

1520
BACKEND_VERSION = backend_version(asfloat=True)
1621

@@ -107,8 +112,8 @@ def test_header_write_read():
107112
'longs': lorem_ipsum,
108113
}
109114
if (
110-
fitsio_backend() == "cfitsio" and BACKEND_VERSION > 4.02
111-
) or fitsio_backend() == "rsfitsio":
115+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION > 4.02
116+
) or fitsio_backend() == RSFITSIO_BACKEND:
112117
# force hierarch + continue
113118
header["long_keyword_name"] = lorem_ipsum
114119

fitsio/tests/test_image.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55

66
# import warnings
77
from .checks import check_header, compare_array
8-
from .. import backend_version, backend_is_bundled, fitsio_backend
8+
from .. import (
9+
backend_version,
10+
backend_is_bundled,
11+
fitsio_backend,
12+
CFITSIO_BACKEND,
13+
RSFITSIO_BACKEND,
14+
)
915
import numpy as np
1016
from ..fitslib import FITS
1117

1218
BACKEND_VERSION = backend_version(asfloat=True)
1319
DTYPES = ['u1', 'i1', 'u2', 'i2', '<u4', 'i4', 'i8', '>f4', 'f8']
1420
if (
15-
BACKEND_VERSION > 3.44 and fitsio_backend() == "cfitsio"
16-
) or fitsio_backend() == "rsfitsio":
21+
BACKEND_VERSION > 3.44 and fitsio_backend() == CFITSIO_BACKEND
22+
) or fitsio_backend() == RSFITSIO_BACKEND:
1723
DTYPES += ["u8"]
1824

1925

@@ -77,7 +83,7 @@ def test_image_write_read_unaligned(dtype, with_nan):
7783
"""
7884

7985
if (dtype == ">f4" or ("f" in dtype and with_nan)) and (
80-
fitsio_backend() == "cfitsio" and not backend_is_bundled()
86+
fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled()
8187
):
8288
pytest.xfail(
8389
reason=(
@@ -563,7 +569,7 @@ def test_image_write_subset_2d(
563569
with_nan
564570
and with_nan_base_img
565571
and partial_overlap_str in partial_overlap_str_cases
566-
and (fitsio_backend() == "cfitsio" and not backend_is_bundled())
572+
and (fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled())
567573
and compress_kws
568574
and compress_kws.get("qlevel", 0) > 0
569575
):
@@ -787,7 +793,7 @@ def test_image_read_write_ulonglong():
787793
with FITS(fname, 'rw') as fits:
788794
data = np.arange(5 * 20, dtype='u8').reshape(5, 20)
789795
header = {'DTYPE': 'u8', 'NBYTES': data.dtype.itemsize}
790-
if BACKEND_VERSION < 3.45 and fitsio_backend() == "cfitsio":
796+
if BACKEND_VERSION < 3.45 and fitsio_backend() == CFITSIO_BACKEND:
791797
with pytest.raises(TypeError) as e:
792798
fits.write_image(data, header=header)
793799
assert (
@@ -804,7 +810,7 @@ def test_image_read_write_ulonglong():
804810
check_header(header, rh)
805811

806812
if (
807-
BACKEND_VERSION >= 3.45 and fitsio_backend() == "cfitsio"
808-
) or fitsio_backend() == "rsfitsio":
813+
BACKEND_VERSION >= 3.45 and fitsio_backend() == CFITSIO_BACKEND
814+
) or fitsio_backend() == RSFITSIO_BACKEND:
809815
with FITS(fname) as fits:
810816
assert not fits[0].is_compressed(), 'not compressed'

fitsio/tests/test_image_compression.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SUBTRACTIVE_DITHER_1,
1717
)
1818
from .. import backend_is_bundled, backend_version
19-
from .. import fitsio_backend
19+
from .. import fitsio_backend, CFITSIO_BACKEND
2020

2121
BACKEND_VERSION = backend_version(asfloat=True)
2222

@@ -125,7 +125,7 @@ def test_compressed_write_read_fitsobj(compress, dtype, with_nan):
125125
if (
126126
"gzip" in compress
127127
and dtype in ["u2", "i2", "u4", "i4"]
128-
and (fitsio_backend() == "cfitsio" and not backend_is_bundled())
128+
and (fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled())
129129
):
130130
pytest.xfail(
131131
reason=(
@@ -202,7 +202,7 @@ def test_compressed_write_read_fitsobj(compress, dtype, with_nan):
202202

203203
@pytest.mark.skipif(sys.version_info < (3, 9), reason='importlib bug in 3.8')
204204
@pytest.mark.skipif(
205-
BACKEND_VERSION < 3.49 and fitsio_backend() == "cfitsio",
205+
BACKEND_VERSION < 3.49 and fitsio_backend() == CFITSIO_BACKEND,
206206
reason='bug in cfitsio < 3.49',
207207
)
208208
def test_gzip_tile_compressed_read_lossless_astropy():
@@ -535,7 +535,7 @@ def test_image_compression_raises_on_python_set(kw, val, set_val_to_none):
535535
)
536536
@pytest.mark.parametrize("fname", ["mem://", "test.fits"])
537537
def test_image_compression_inmem_lossess_int(compress, dtype, fname):
538-
if fitsio_backend() == "cfitsio" and not backend_is_bundled():
538+
if fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled():
539539
pytest.xfail(
540540
reason=(
541541
"Non-bundled cfitsio libraries have a bug. "
@@ -629,8 +629,8 @@ def test_image_mem_reopen_noop():
629629
marks=[
630630
pytest.mark.xfail(
631631
condition=BACKEND_VERSION < 4.04
632-
and fitsio_backend() == "cfitsio"
633-
and fitsio_backend() == "cfitsio",
632+
and fitsio_backend() == CFITSIO_BACKEND
633+
and fitsio_backend() == CFITSIO_BACKEND,
634634
reason=(
635635
"Writing compressed binary tables exceeding "
636636
"2**32 bytes fails for cfitsio < 4.04!"
@@ -801,7 +801,10 @@ def test_image_compression_nulls_patches_with_subnormal(
801801
# however, on read, we send nullcheck=NAN and as a side
802802
# effect the subnormal float value in this row gets truncated
803803
# to zero
804-
if fitsio_backend() == "cfitsio" and not backend_is_bundled():
804+
if (
805+
fitsio_backend() == CFITSIO_BACKEND
806+
and not backend_is_bundled()
807+
):
805808
np.testing.assert_array_equal(read_data[1, 1:], 0.0)
806809
np.testing.assert_array_equal(read_slice1[1:], 0.0)
807810

@@ -853,7 +856,9 @@ def test_image_compression_read_chunks():
853856

854857

855858
@pytest.mark.xfail(
856-
condition=(fitsio_backend() == "cfitsio" and not backend_is_bundled()),
859+
condition=(
860+
fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled()
861+
),
857862
reason=(
858863
"system cfitsio must be compiled without FMA instructions to "
859864
"enable reproducible lossy float compression"
@@ -886,7 +891,9 @@ def test_image_compression_write_read_comp_to_osx_arm64():
886891

887892

888893
@pytest.mark.xfail(
889-
condition=(fitsio_backend() == "cfitsio" and not backend_is_bundled()),
894+
condition=(
895+
fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled()
896+
),
890897
reason=(
891898
"system cfitsio must be compiled without FMA instructions to "
892899
"enable reproducible lossy float compression"
@@ -926,7 +933,7 @@ def test_image_compression_gzip_subnormal_cast_to_zero():
926933
)
927934

928935
back = read(fn)
929-
if fitsio_backend() == "cfitsio" and backend_is_bundled():
936+
if fitsio_backend() == CFITSIO_BACKEND and backend_is_bundled():
930937
assert not back.ravel()[0] == 0, back.ravel()
931938
else:
932939
assert back.ravel()[0] == 0, back.ravel()

fitsio/tests/test_segfault.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
import fitsio
7-
from .. import fitsio_backend, backend_is_bundled
7+
from .. import fitsio_backend, backend_is_bundled, CFITSIO_BACKEND
88

99
import pytest
1010

@@ -54,7 +54,7 @@ def _run_mixed(n):
5454

5555
@pytest.mark.slow
5656
@pytest.mark.skipif(
57-
(fitsio_backend() == "cfitsio" and not backend_is_bundled()),
57+
(fitsio_backend() == CFITSIO_BACKEND and not backend_is_bundled()),
5858
reason=(
5959
"small images cause a memory corruption w/ PLIO "
6060
"compression (see https://github.com/heasarc/cfitsio/issues/136)"

fitsio/tests/test_table.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
from .makedata import make_data
1616
from ..fitslib import FITS, write, read
1717
from .. import util
18-
from .. import backend_has_bzip2_support, fitsio_backend, backend_version
18+
from .. import (
19+
backend_has_bzip2_support,
20+
fitsio_backend,
21+
backend_version,
22+
CFITSIO_BACKEND,
23+
RSFITSIO_BACKEND,
24+
)
1925

2026
BACKEND_VERSION = backend_version(asfloat=True)
2127
DTYPES = ['u1', 'i1', 'u2', 'i2', '<u4', 'i4', 'i8', '>f4', 'f8']
2228
if (
23-
BACKEND_VERSION > 4 and fitsio_backend() == "cfitsio"
24-
) or fitsio_backend() == "rsfitsio":
29+
BACKEND_VERSION > 4 and fitsio_backend() == CFITSIO_BACKEND
30+
) or fitsio_backend() == RSFITSIO_BACKEND:
2531
DTYPES += ["u8"]
2632

2733

@@ -572,9 +578,9 @@ def test_ascii_table_write_read():
572578
Test write and read for an ascii table
573579
"""
574580

575-
if fitsio_backend() == "rsfitsio":
581+
if fitsio_backend() == RSFITSIO_BACKEND:
576582
tol = 1e-14
577-
elif fitsio_backend() == "cfitsio":
583+
elif fitsio_backend() == CFITSIO_BACKEND:
578584
tol = 2.15e-16
579585
else:
580586
assert False, "No valid backend specified! got " + fitsio_backend()
@@ -899,8 +905,8 @@ def test_table_resize():
899905
add_data['u4vec'] = 2**31
900906
add_data['u4arr'] = 2**31
901907
if (
902-
BACKEND_VERSION > 4 and fitsio_backend() == "cfitsio"
903-
) or fitsio_backend() == "rsfitsio":
908+
BACKEND_VERSION > 4 and fitsio_backend() == CFITSIO_BACKEND
909+
) or fitsio_backend() == RSFITSIO_BACKEND:
904910
add_data['u8scalar'] = 2**63
905911
add_data['u8vec'] = 2**63
906912
add_data['u8arr'] = 2**63
@@ -1583,7 +1589,7 @@ def test_table_big_col(table_type):
15831589
# v3 cfitsio that is not bundled fails for big
15841590
# columns
15851591
if table_type == "ascii" or (
1586-
fitsio_backend() == "cfitsio" and BACKEND_VERSION < 4
1592+
fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION < 4
15871593
):
15881594
with pytest.raises(OSError) as e:
15891595
write(pth, d, table_type=table_type)
@@ -1600,7 +1606,7 @@ def test_table_big_col(table_type):
16001606

16011607

16021608
@pytest.mark.xfail(
1603-
condition=fitsio_backend() == "cfitsio" and BACKEND_VERSION < 4,
1609+
condition=fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION < 4,
16041610
reason=(
16051611
"cfitsio versions < 4 do not easily support null-terminated strings"
16061612
),
@@ -1631,7 +1637,7 @@ def test_table_read_write_ulonglong():
16311637
fname = os.path.join(tmpdir, 'test.fits')
16321638

16331639
with FITS(fname, 'rw') as fits:
1634-
if fitsio_backend() == "cfitsio" and BACKEND_VERSION < 3.45:
1640+
if fitsio_backend() == CFITSIO_BACKEND and BACKEND_VERSION < 3.45:
16351641
with pytest.raises(IOError) as e:
16361642
fits.write_table(
16371643
adata,

0 commit comments

Comments
 (0)