astropy / astropy/astropy

astropy.io.fits error with getdata(), stream-like object, and mode='readonly'

Open
#7,980 16 comments 0 reactions 0 assignees View on GitHub
io.fits
Dominant language
Python
Stars
5.3k
Forks
2.2k
Avg merge
1d 18h
Merged PRs (30d)
74

Description

I'm using a call to `getdata()` to pull a single HDU from an input stream. The stream does not implement a `write()` method, and nor would I expect it to, but I get this error with `getdata()`:

```
hdu = getdata(file_stream, header=True, ext=2, memmap=True, do_not_scale_image_data=True)
```
> OSError: File-like object does not have a 'write' method, required for mode 'ostream'.

I think it's related to #7533.

Then I set the mode to 'readonly' in hopes of working around it, but then I get this error:

```
hdu = getdata(file_stream, header=True, ext=2, memmap=True, do_not_scale_image_data=True, mode='readonly')
```

```
kwargs = {'do_not_scale_image_data': True, 'ext': 0, 'memmap': True, 'mode': 'readonly'}, mode = 'readonly', closed = False, header = True
lower = None, upper = None, view = None

def getdata(filename, *args, **kwargs):
...
> hdulist, extidx = _getext(filename, mode, *args, **kwargs)
E TypeError: _getext() got multiple values for keyword argument 'mode'
```

How can I use a readonly stream with a stream like object? Is the only way to omit the `mode` and wrap it in an `io.BytesIO` stream?

Thanks.

Contributor guide

Open the contributing guide

Research direction

Start with astropy.io.fits.getdata() and its _getext() call, reproducing the two failures with a read-only stream-like object. Compare the handling of mode='readonly' and the default mode, including the relationship to #7533 and the suggested io.BytesIO workaround. Done means a read-only stream can retrieve the requested HDU without requiring write() or raising the duplicate-mode error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.