Skip to content

ftpfs possible issue with openbin #406

Closed

Description

@al8ba

Recently, while trying out the ftpfs, I've noticed that to open a binary file-like object for writing by some third-party packages I have to do this:

with open_fs("ftp://anonymous:anonymous@localhost:8021") as fs:
    with fs.openbin("test.bin", "wb") as f:
        ...

Note in the above the mode param used in openbin includes b: Looking at the docs for the mode param it suggests the b is implied via openbin, but b isn't implicitly added to the mode of the file-like that is returned by openbin - this means that, unless b is explicitly provided as above, third-party packages that inspect the mode of the file-like may complain that it is not opened in binary mode.

Should openbin not reflect the implied b by implicitly adding b to the mode?

Activity

  1. lurch commented on Jun 24, 2020

    @lurch
    Contributor
  2. al8ba commented on Jun 24, 2020

    @al8ba
    Author

    Yes, I was thinking of mentioning use of to_platform_bin in a similar manner.

    I guess another way of putting the question is that, given the "b is implied by openbin" in the docs, then for a file f opened via it should "b" in f.mode hold true? Sounds almost like a unit test...

  3. willmcgugan commented on Jun 24, 2020

    @willmcgugan
    Member

    The base class only guarantees that openbin will return a BinaryIO, which doesn't contain a "mode" attribute. Only the io.FileIO class can be sure of having mode.

    So it would be a mistake to assume that all file-like objects have a "mode" attribute. But people do write broken code because they make reasonable assumptions and the specifications are too difficult to understand, so perhaps it would be a good idea for ftp files to add a mode attribute.

  4. al8ba commented on Jun 24, 2020

    @al8ba
    Author

    so perhaps it would be a good idea for ftp files to add a mode attribute.

    The FTPFile object does have a mode attribute - the issue I'm bringing up is that if the FTPFile instance comes from a call to openbin then the mode within it will not contain b, unless b was explicitly supplied in the call to openbin.

    I think a solution to this, similar to the previous note about osfs, would be to have the final two lines of openbin look more akin to this:

            ...
            ftp_file = FTPFile(self, _path, mode.to_platform_bin())
        return ftp_file  # type: ignore
    
  5. added this to the v2.4.12 milestone on Sep 17, 2020
  6. althonos commented on Oct 13, 2020

    @althonos
    Member

    Actually, it is reasonable to expect FS.openbin to return an object with a mode attribute (containing a "b" flag), since it all filesystems are doing it but FTPFile and _MemoryFile. I patched them in #435

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions