Repository: astropy/astropy

Issue:
'CompImageHDU' object has no attribute '_tmp_bintable'
### Description

In astropy 7, when `HDUList` is read from a file that contains `CompImageHDU`, calling `fileinfo` method with any HDU index fails with AttributeError: `'CompImageHDU' object has no attribute '_tmp_bintable'`.

The issue was not present in astropy<7.0.

### Expected behavior

`astropy.io.fits.HDUList.fileinfo` should _return a dictionary detailing information about the locations of the indexed HDU within any associated file_. 

### How to Reproduce

```python
import numpy as np
from astropy.io import fits

# create FITS with compressed image HDU
primary_hdu = fits.PrimaryHDU()
compressed_image = fits.CompImageHDU(data=np.array([[0, 1], [0, 0]], dtype=np.int16))
hdul = fits.HDUList([primary_hdu, compressed_image])
hdul.writeto("compressed_img.fits", overwrite=True)

# read back HDUList from a file and call fileinfo
with fits.open("compressed_img.fits") as hdul_from_file:
    file_info = hdul_from_file.fileinfo(0)
```

Executing the code above results in an error:
```
AttributeError                            Traceback (most recent call last)
Cell In[1], line 12
     10 # read back HDUList from a file and get fileinfo on any extension
     11 with fits.open("compressed_img.fits") as hdul_from_file:
---> 12     file_info = hdul_from_file.fileinfo(0)

File lib/python3.11/site-packages/astropy/io/fits/hdu/hdulist.py:608, in HDUList.fileinfo(self, index)
    599         output = {
    600             "file": f,
    601             "filemode": fm,
   (...)
    604             "datSpan": None,
    605         }
    607     output["filename"] = self._file.name
--> 608     output["resized"] = self._wasresized()
    609 else:
    610     output = None

File lib/python3.11/site-packages/astropy/io/fits/hdu/hdulist.py#line=1592, in HDUList._wasresized(self, verbose)
   1589 for hdu in self:
   1590     # for CompImageHDU, we need to handle things a little differently
   1591     # because the HDU matching the header/data on disk is hdu._bintable
   1592     if isinstance(hdu, CompImageHDU):
-> 1593         hdu = hdu._tmp_bintable
   1594         if hdu is None:
   1595             continue

AttributeError: 'CompImageHDU' object has no attribute '_tmp_bintable'
```

### Versions

```python
import astropy
try:
    astropy.system_info()
except AttributeError:
    import platform; print(platform.platform())
    import sys; print("Python", sys.version)
    import astropy; print("astropy", astropy.__version__)
    import numpy; print("Numpy", numpy.__version__)
    import erfa; print("pyerfa", erfa.__version__)
    try:
        import scipy
        print("Scipy", scipy.__version__)
    except ImportError:
        print("Scipy not installed")
    try:
        import matplotlib
        print("Matplotlib", matplotlib.__version__)
    except ImportError:
        print("Matplotlib not installed")
```
```
platform
--------
platform.platform() = 'macOS-14.5-x86_64-i386-64bit'
platform.version() = 'Darwin Kernel Version 23.5.0: Wed May  1 20:09:52 PDT 2024; root:xnu-10063.121.3~5/RELEASE_X86_64'
platform.python_version() = '3.11.10'

packages
--------
astropy              7.0.1
numpy                1.26.4
scipy                1.14.1
matplotlib           3.10.0
pandas               2.2.2
pyerfa               2.0.1.4
```

The same error can be reproduced with the latest development version:
```
platform
--------
platform.platform() = 'macOS-14.5-x86_64-i386-64bit-Mach-O'
platform.version() = 'Darwin Kernel Version 23.5.0: Wed May  1 20:09:52 PDT 2024; root:xnu-10063.121.3~5/RELEASE_X86_64'
platform.python_version() = '3.13.2'

packages
--------
astropy              7.1.dev555+g3ad5727d61
numpy                2.2.3
scipy                1.15.2
matplotlib           3.10.0
pandas               2.2.3
pyerfa               2.0.1.5
``` 

Discussion:

Patch:
diff --git a/astropy/io/fits/hdu/compressed/compressed.py b/astropy/io/fits/hdu/compressed/compressed.py
index ab944c4a72..d308d5d28a 100644
--- a/astropy/io/fits/hdu/compressed/compressed.py
+++ b/astropy/io/fits/hdu/compressed/compressed.py
@@ -692,6 +692,10 @@ class CompImageHDU(ImageHDU):
         else:
             return errs
 
+    def fileinfo(self):
+        if self._bintable is not None:
+            return self._bintable.fileinfo()
+
     @property
     def _data_offset(self):
         if self._bintable is not None:
diff --git a/astropy/io/fits/hdu/hdulist.py b/astropy/io/fits/hdu/hdulist.py
index 7997f62056..6d42b6eaf1 100644
--- a/astropy/io/fits/hdu/hdulist.py
+++ b/astropy/io/fits/hdu/hdulist.py
@@ -1591,7 +1591,7 @@ class HDUList(list, _Verify):
                 # for CompImageHDU, we need to handle things a little differently
                 # because the HDU matching the header/data on disk is hdu._bintable
                 if isinstance(hdu, CompImageHDU):
-                    hdu = hdu._tmp_bintable
+                    hdu = hdu._bintable
                     if hdu is None:
                         continue
 
diff --git a/docs/changes/io.fits/17815.bugfix.rst b/docs/changes/io.fits/17815.bugfix.rst
new file mode 100644
index 0000000000..bb2144d146
--- /dev/null
+++ b/docs/changes/io.fits/17815.bugfix.rst
@@ -0,0 +1,1 @@
+Fix ``.fileinfo()`` for compressed HDUs.


Test patch:
diff --git a/astropy/io/fits/hdu/compressed/tests/test_compressed.py b/astropy/io/fits/hdu/compressed/tests/test_compressed.py
index 04bd5de659..9caddbfa48 100644
--- a/astropy/io/fits/hdu/compressed/tests/test_compressed.py
+++ b/astropy/io/fits/hdu/compressed/tests/test_compressed.py
@@ -996,6 +996,17 @@ class TestCompressedImage(FitsTestCase):
             ]
             assert not hdul[1]._data_loaded
 
+    def test_fileinfo(self):
+        with fits.open(self.data("comp.fits")) as hdul:
+            res = hdul.fileinfo(1)
+
+        assert res["datLoc"] == 14400
+        assert res["datSpan"] == 72000
+        assert res["filemode"] == "readonly"
+        assert res["filename"] == self.data("comp.fits")
+        assert res["hdrLoc"] == 2880
+        assert not res["resized"]
+
 
 class TestCompHDUSections:
     @pytest.fixture(autouse=True)