Skip to content

Misleading IOError when passing a urlopen object in to fits.open() #4165

Closed

Description

@embray

Per this SO question: http://stackoverflow.com/questions/32643206/how-do-i-open-a-fits-file-from-a-url-in-astropy

As noted in the answers, one can already pass a URL as a string in to fits.open() and that will work fine. A urlopen object has never been supported (intentionally) in the first place. But it could be supported easily so long as the file is opened in readonly (or denywrite) mode. It just needs to be handled explicitly. In any case the error message one gets is also misleading:

ERROR: IOError: File-like object does not have a 'write' method, required for mode 'ostream'. [astropy.io.fits.file]

This happens despite not explicitly requesting 'ostream' mode.

Activity

  1. SanketDG commented on Sep 20, 2015

    @SanketDG

    Do you mean a Request object?

    So something like:

    >>> import urrlib.request
    >>> from astropy.io import fits
    >>> obj = urllib.request.Request("http://google.com")
    >>> type(obj)
    <class 'urllib.request.Request'>
    >>> fits.open(obj)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/sanket/astropy/astropy/io/fits/hdu/hdulist.py", line 138, in fitsopen
        return HDUList.fromfile(name, mode, memmap, save_backup, cache, **kwargs)
      File "/home/sanket/astropy/astropy/io/fits/hdu/hdulist.py", line 280, in fromfile
        save_backup=save_backup, cache=cache, **kwargs)
      File "/home/sanket/astropy/astropy/io/fits/hdu/hdulist.py", line 801, in _readfrom
        ffo = _File(fileobj, mode=mode, memmap=memmap, cache=cache)
      File "/home/sanket/astropy/astropy/io/fits/file.py", line 143, in __init__
        self._open_filelike(fileobj, mode, clobber)
      File "/home/sanket/astropy/astropy/io/fits/file.py", line 455, in _open_filelike
        % self.mode)
    OSError: File-like object does not have a 'write' method, required for mode 'ostream'.
  2. embray commented on Sep 21, 2015

    @embray
    MemberAuthor

    @SanketDG Probably, though this needs to work on Python 2 as well, so also whatever's returned by urllib2.urlopen which I think is something else.

    Really anything with a read() method but no write() method.

  3. lingxz commented on Oct 14, 2015

    @lingxz
    Contributor

    @embray would it be better to explicitly check for the urlopen object in fitsopen()
    or in _File()?

  4. embray commented on Oct 15, 2015

    @embray
    MemberAuthor

    All the actual I/O details are handled in the _File class. fitsopen is purely a UI.

  5. AMAN3003 commented on Jan 25, 2016

    @AMAN3003

    @embray is this issue solved if not can i work on this issue

  6. embray commented on Jan 25, 2016

    @embray
    MemberAuthor

    @AMAN3003 On GitHub, issues / pull requests that are related to each other show up as cross-references when one references the other. For example, just above your comment you should be able to see two pull requests that reference this issue. One of this PRs is already merged, and references this issue in passing. The other, you can see, is already an attempt to work on this issue (which still needs some work, but it's getting there).

    Relatedly, we need to do better at setting the "assignee" to an issue that someone is working on. I think part of this is a UI problem though--GitHub doesn't allow setting an assignee to someone who isn't watching the project.

  7. drdavella commented on Jul 19, 2017

    @drdavella
    Contributor

    It appears that this actually already works as expected, at least for the latest version of astropy:

    import urllib.request
    from astropy.io import fits
    
    with urllib.request.urlopen('file:///path/to/file/that/exists.fits') as urlobj:
        with fits.open(urlobj) as fits_handle:
            # go to town ...

    I added a new unit test demonstrating as much in PR #6378, so hopefully this resolves the issue.

  8. pllim commented on Jul 19, 2017

    @pllim
    Member

    Nothing quite feels so good as a bug that resolved itself. 😂

  9. bsipocz commented on Jul 20, 2017

    @bsipocz
    Member

    @pllim - I'm sure there are still a few hiding in the crowd of the 664 open issues that are solved themselves 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions